Split type_unit_group
[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
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
41144253 117/* Size of .debug_loclists section header for 32-bit DWARF format. */
118#define LOCLIST_HEADER_SIZE32 12
119
120/* Size of .debug_loclists section header for 64-bit DWARF format. */
121#define LOCLIST_HEADER_SIZE64 20
122
3f563c84
PA
123/* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136struct name_component
137{
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146};
147
44ed8f3e
PA
148/* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151struct mapped_index_base
152{
22ca247e
TT
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
44ed8f3e
PA
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
3b00ef10
TT
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
44ed8f3e
PA
187
188 /* Prevent deleting/destroying via a base class pointer. */
189protected:
190 ~mapped_index_base() = default;
191};
192
9291a0cd
TT
193/* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
fc898b42 195struct mapped_index final : public mapped_index_base
9291a0cd 196{
f00a2de2
PA
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
559a7a62 204 /* Index data format version. */
3063847f 205 int version = 0;
559a7a62 206
f00a2de2
PA
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
b11b1f88 209
3876f04e 210 /* The symbol table, implemented as a hash table. */
f00a2de2 211 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 212
9291a0cd 213 /* A pointer to the constant pool. */
3063847f 214 const char *constant_pool = nullptr;
3f563c84 215
44ed8f3e
PA
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
9ab08412 219 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 220 }
5c58de74 221
3f563c84
PA
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
44ed8f3e 224 const char *symbol_name_at (offset_type idx) const override
f00a2de2 225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 226
44ed8f3e
PA
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
9291a0cd
TT
229};
230
927aa2e7
JK
231/* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
fc898b42 233struct mapped_debug_names final : public mapped_index_base
927aa2e7 234{
ed2dc618
SM
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
927aa2e7
JK
281};
282
cd4fb1b2 283/* See dwarf2read.h. */
ed2dc618 284
cd4fb1b2 285dwarf2_per_objfile *
ed2dc618
SM
286get_dwarf2_per_objfile (struct objfile *objfile)
287{
5bfd760d 288 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 289}
c906108c 290
251d32d9 291/* Default names of the debugging sections. */
c906108c 292
233a11ab
CS
293/* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
9cdd5dbd
DE
296static const struct dwarf2_debug_sections dwarf2_elf_names =
297{
251d32d9
TG
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
43988095 302 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 303 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 304 { ".debug_macro", ".zdebug_macro" },
251d32d9 305 { ".debug_str", ".zdebug_str" },
18a8505e 306 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 307 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 308 { ".debug_ranges", ".zdebug_ranges" },
43988095 309 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 310 { ".debug_types", ".zdebug_types" },
3019eac3 311 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
24d3216f 314 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 317 23
251d32d9 318};
c906108c 319
80626a55 320/* List of DWO/DWP sections. */
3019eac3 321
80626a55 322static const struct dwop_section_names
3019eac3
DE
323{
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
43988095 328 struct dwarf2_section_names loclists_dwo;
09262596
DE
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
3019eac3
DE
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
80626a55
DE
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
3019eac3 336}
80626a55 337dwop_section_names =
3019eac3
DE
338{
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
351};
352
c906108c
SS
353/* local data types */
354
41144253 355/* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357struct loclist_header
358{
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377};
378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
9e021579
SM
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
c24bdb02
KS
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
293e7e51
SM
424 /* Return a type that is a generic pointer type, the size of which
425 matches the address size given in the compilation unit header for
426 this CU. */
427 struct type *addr_type () const;
428
429 /* Find an integer type the same size as the address size given in
430 the compilation unit header for this CU. UNSIGNED_P controls if
431 the integer is unsigned or not. */
432 struct type *addr_sized_int_type (bool unsigned_p) const;
433
d00adf39 434 /* The header of the compilation unit. */
fcd3b13d 435 struct comp_unit_head header {};
e142c38c 436
d00adf39 437 /* Base address of this compilation unit. */
2b24b6e4 438 gdb::optional<CORE_ADDR> base_address;
d00adf39 439
e142c38c 440 /* The language we are debugging. */
fcd3b13d
SM
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
e142c38c 443
fcd3b13d 444 const char *producer = nullptr;
b0f35d58 445
c24bdb02 446private:
804d2729
TT
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
c24bdb02 449 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 450
c24bdb02 451public:
e142c38c
DJ
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
456
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
fcd3b13d 461 struct pending **list_in_scope = nullptr;
e142c38c 462
b64f50a1
JK
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 465 htab_t partial_dies = nullptr;
72bf9492
DJ
466
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
fcd3b13d 469 auto_obstack comp_unit_obstack;
72bf9492 470
ae038cb0
DJ
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
fcd3b13d 475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 476
69d751e3 477 /* Backlink to our per_cu entry. */
ae038cb0
DJ
478 struct dwarf2_per_cu_data *per_cu;
479
9e021579
SM
480 /* The dwarf2_per_objfile that owns this. */
481 struct dwarf2_per_objfile *per_objfile;
482
ae038cb0 483 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 484 int last_used = 0;
ae038cb0 485
b64f50a1
JK
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
fcd3b13d 488 htab_t die_hash = nullptr;
10b3939b
DJ
489
490 /* Full DIEs if read in. */
fcd3b13d 491 struct die_info *dies = nullptr;
10b3939b
DJ
492
493 /* A set of pointers to dwarf2_per_cu_data objects for compilation
494 units referenced by this one. Only set during full symbol processing;
495 partial symbol tables do not have dependencies. */
fcd3b13d 496 htab_t dependencies = nullptr;
10b3939b 497
cb1df416 498 /* Header data from the line table, during full symbol processing. */
fcd3b13d 499 struct line_header *line_header = nullptr;
4c8aa72d 500 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 501 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
502 this is the DW_TAG_compile_unit die for this CU. We'll hold on
503 to the line header as long as this DIE is being processed. See
504 process_die_scope. */
fcd3b13d 505 die_info *line_header_die_owner = nullptr;
cb1df416 506
3da10d80
KS
507 /* A list of methods which need to have physnames computed
508 after all type information has been read. */
c89b44cd 509 std::vector<delayed_method_info> method_list;
3da10d80 510
96408a79 511 /* To be copied to symtab->call_site_htab. */
fcd3b13d 512 htab_t call_site_htab = nullptr;
96408a79 513
034e5797
DE
514 /* Non-NULL if this CU came from a DWO file.
515 There is an invariant here that is important to remember:
516 Except for attributes copied from the top level DIE in the "main"
517 (or "stub") file in preparation for reading the DWO file
18a8505e 518 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
519 Either there isn't a DWO file (in which case this is NULL and the point
520 is moot), or there is and either we're not going to read it (in which
521 case this is NULL) or there is and we are reading it (in which case this
522 is non-NULL). */
fcd3b13d 523 struct dwo_unit *dwo_unit = nullptr;
3019eac3 524
18a8505e 525 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 526 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 527 gdb::optional<ULONGEST> addr_base;
3019eac3 528
18a8505e 529 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 530 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 531 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 536 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 538 ULONGEST ranges_base = 0;
2e3cf129 539
41144253 540 /* The DW_AT_loclists_base attribute if present. */
541 ULONGEST loclist_base = 0;
542
c9317f21
TT
543 /* When reading debug info generated by older versions of rustc, we
544 have to rewrite some union types to be struct types with a
545 variant part. This rewriting must be done after the CU is fully
546 read in, because otherwise at the point of rewriting some struct
547 type might not have been fully processed. So, we keep a list of
548 all such types here and process them after expansion. */
549 std::vector<struct type *> rust_unions;
550
18a8505e
AT
551 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
552 files, the value is implicitly zero. For DWARF 5 version DWO files, the
553 value is often implicit and is the size of the header of
554 .debug_str_offsets section (8 or 4, depending on the address size). */
555 gdb::optional<ULONGEST> str_offsets_base;
556
ae038cb0 557 /* Mark used when releasing cached dies. */
9068261f 558 bool mark : 1;
ae038cb0 559
8be455d7
JK
560 /* This CU references .debug_loc. See the symtab->locations_valid field.
561 This test is imperfect as there may exist optimized debug code not using
562 any location list and still facing inlining issues if handled as
563 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 564 bool has_loclist : 1;
ba919b58 565
9068261f 566 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
567 if all the producer_is_* fields are valid. This information is cached
568 because profiling CU expansion showed excessive time spent in
569 producer_is_gxx_lt_4_6. */
9068261f
AB
570 bool checked_producer : 1;
571 bool producer_is_gxx_lt_4_6 : 1;
572 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 573 bool producer_is_icc : 1;
9068261f 574 bool producer_is_icc_lt_14 : 1;
c258c396 575 bool producer_is_codewarrior : 1;
4d4ec4e5 576
9068261f 577 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
578 debugging info for C++ namespaces. GCC 3.3.x did not produce
579 this information, but later versions do. */
580
9068261f 581 bool processing_has_namespace_info : 1;
d590ff25
YQ
582
583 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
584
585 /* If this CU was inherited by another CU (via specification,
586 abstract_origin, etc), this is the ancestor CU. */
587 dwarf2_cu *ancestor;
588
589 /* Get the buildsym_compunit for this CU. */
590 buildsym_compunit *get_builder ()
591 {
592 /* If this CU has a builder associated with it, use that. */
593 if (m_builder != nullptr)
594 return m_builder.get ();
595
596 /* Otherwise, search ancestors for a valid builder. */
597 if (ancestor != nullptr)
598 return ancestor->get_builder ();
599
600 return nullptr;
601 }
e7c27a73
DJ
602};
603
094b34ac
DE
604/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
605 This includes type_unit_group and quick_file_names. */
606
607struct stmt_list_hash
608{
609 /* The DWO unit this table is from or NULL if there is none. */
610 struct dwo_unit *dwo_unit;
611
612 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 613 sect_offset line_sect_off;
094b34ac
DE
614};
615
5989a64e 616/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
617 an object of this type. This contains elements of type unit groups
618 that can be shared across objfiles. The non-shareable parts are in
619 type_unit_group_unshareable. */
f4dc4d17
DE
620
621struct type_unit_group
622{
0186c6a7 623 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
624 To simplify things we create an artificial CU that "includes" all the
625 type units using this stmt_list so that the rest of the code still has
197400e8 626 a "per_cu" handle on the symtab. */
094b34ac
DE
627 struct dwarf2_per_cu_data per_cu;
628
0186c6a7
DE
629 /* The TUs that share this DW_AT_stmt_list entry.
630 This is added to while parsing type units to build partial symtabs,
631 and is deleted afterwards and not used again. */
a8b3b8e9 632 std::vector<signatured_type *> *tus;
f4dc4d17 633
094b34ac
DE
634 /* The data used to construct the hash key. */
635 struct stmt_list_hash hash;
f4dc4d17
DE
636};
637
73869dc2 638/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
639
640struct dwo_sections
641{
642 struct dwarf2_section_info abbrev;
3019eac3
DE
643 struct dwarf2_section_info line;
644 struct dwarf2_section_info loc;
43988095 645 struct dwarf2_section_info loclists;
09262596
DE
646 struct dwarf2_section_info macinfo;
647 struct dwarf2_section_info macro;
3019eac3
DE
648 struct dwarf2_section_info str;
649 struct dwarf2_section_info str_offsets;
80626a55
DE
650 /* In the case of a virtual DWO file, these two are unused. */
651 struct dwarf2_section_info info;
fd5866f6 652 std::vector<dwarf2_section_info> types;
3019eac3
DE
653};
654
c88ee1f0 655/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
656
657struct dwo_unit
658{
659 /* Backlink to the containing struct dwo_file. */
660 struct dwo_file *dwo_file;
661
662 /* The "id" that distinguishes this CU/TU.
663 .debug_info calls this "dwo_id", .debug_types calls this "signature".
664 Since signatures came first, we stick with it for consistency. */
665 ULONGEST signature;
666
667 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 668 struct dwarf2_section_info *section;
3019eac3 669
9c541725
PA
670 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
671 sect_offset sect_off;
3019eac3
DE
672 unsigned int length;
673
674 /* For types, offset in the type's DIE of the type defined by this TU. */
675 cu_offset type_offset_in_tu;
676};
677
73869dc2
DE
678/* include/dwarf2.h defines the DWP section codes.
679 It defines a max value but it doesn't define a min value, which we
680 use for error checking, so provide one. */
681
682enum dwp_v2_section_ids
683{
684 DW_SECT_MIN = 1
685};
686
80626a55 687/* Data for one DWO file.
57d63ce2
DE
688
689 This includes virtual DWO files (a virtual DWO file is a DWO file as it
690 appears in a DWP file). DWP files don't really have DWO files per se -
691 comdat folding of types "loses" the DWO file they came from, and from
692 a high level view DWP files appear to contain a mass of random types.
693 However, to maintain consistency with the non-DWP case we pretend DWP
694 files contain virtual DWO files, and we assign each TU with one virtual
695 DWO file (generally based on the line and abbrev section offsets -
696 a heuristic that seems to work in practice). */
3019eac3
DE
697
698struct dwo_file
699{
51ac9db5
SM
700 dwo_file () = default;
701 DISABLE_COPY_AND_ASSIGN (dwo_file);
702
18a8505e 703 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
704 For virtual DWO files the name is constructed from the section offsets
705 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
706 from related CU+TUs. */
51ac9db5 707 const char *dwo_name = nullptr;
0ac5b59e
DE
708
709 /* The DW_AT_comp_dir attribute. */
51ac9db5 710 const char *comp_dir = nullptr;
3019eac3 711
80626a55
DE
712 /* The bfd, when the file is open. Otherwise this is NULL.
713 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 714 gdb_bfd_ref_ptr dbfd;
3019eac3 715
73869dc2
DE
716 /* The sections that make up this DWO file.
717 Remember that for virtual DWO files in DWP V2, these are virtual
718 sections (for lack of a better name). */
51ac9db5 719 struct dwo_sections sections {};
3019eac3 720
33c5cd75
DB
721 /* The CUs in the file.
722 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
723 an extension to handle LLVM's Link Time Optimization output (where
724 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 725 htab_up cus;
3019eac3
DE
726
727 /* Table of TUs in the file.
728 Each element is a struct dwo_unit. */
b0b6a987 729 htab_up tus;
3019eac3
DE
730};
731
80626a55
DE
732/* These sections are what may appear in a DWP file. */
733
734struct dwp_sections
735{
73869dc2 736 /* These are used by both DWP version 1 and 2. */
80626a55
DE
737 struct dwarf2_section_info str;
738 struct dwarf2_section_info cu_index;
739 struct dwarf2_section_info tu_index;
73869dc2
DE
740
741 /* These are only used by DWP version 2 files.
742 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
743 sections are referenced by section number, and are not recorded here.
744 In DWP version 2 there is at most one copy of all these sections, each
745 section being (effectively) comprised of the concatenation of all of the
746 individual sections that exist in the version 1 format.
747 To keep the code simple we treat each of these concatenated pieces as a
748 section itself (a virtual section?). */
749 struct dwarf2_section_info abbrev;
750 struct dwarf2_section_info info;
751 struct dwarf2_section_info line;
752 struct dwarf2_section_info loc;
753 struct dwarf2_section_info macinfo;
754 struct dwarf2_section_info macro;
755 struct dwarf2_section_info str_offsets;
756 struct dwarf2_section_info types;
80626a55
DE
757};
758
73869dc2
DE
759/* These sections are what may appear in a virtual DWO file in DWP version 1.
760 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 761
73869dc2 762struct virtual_v1_dwo_sections
80626a55
DE
763{
764 struct dwarf2_section_info abbrev;
765 struct dwarf2_section_info line;
766 struct dwarf2_section_info loc;
767 struct dwarf2_section_info macinfo;
768 struct dwarf2_section_info macro;
769 struct dwarf2_section_info str_offsets;
770 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 771 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
772 struct dwarf2_section_info info_or_types;
773};
774
73869dc2
DE
775/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
776 In version 2, the sections of the DWO files are concatenated together
777 and stored in one section of that name. Thus each ELF section contains
778 several "virtual" sections. */
779
780struct virtual_v2_dwo_sections
781{
782 bfd_size_type abbrev_offset;
783 bfd_size_type abbrev_size;
784
785 bfd_size_type line_offset;
786 bfd_size_type line_size;
787
788 bfd_size_type loc_offset;
789 bfd_size_type loc_size;
790
791 bfd_size_type macinfo_offset;
792 bfd_size_type macinfo_size;
793
794 bfd_size_type macro_offset;
795 bfd_size_type macro_size;
796
797 bfd_size_type str_offsets_offset;
798 bfd_size_type str_offsets_size;
799
800 /* Each DWP hash table entry records one CU or one TU.
801 That is recorded here, and copied to dwo_unit.section. */
802 bfd_size_type info_or_types_offset;
803 bfd_size_type info_or_types_size;
804};
805
80626a55
DE
806/* Contents of DWP hash tables. */
807
808struct dwp_hash_table
809{
73869dc2 810 uint32_t version, nr_columns;
80626a55 811 uint32_t nr_units, nr_slots;
73869dc2
DE
812 const gdb_byte *hash_table, *unit_table;
813 union
814 {
815 struct
816 {
817 const gdb_byte *indices;
818 } v1;
819 struct
820 {
821 /* This is indexed by column number and gives the id of the section
822 in that column. */
823#define MAX_NR_V2_DWO_SECTIONS \
824 (1 /* .debug_info or .debug_types */ \
825 + 1 /* .debug_abbrev */ \
826 + 1 /* .debug_line */ \
827 + 1 /* .debug_loc */ \
828 + 1 /* .debug_str_offsets */ \
829 + 1 /* .debug_macro or .debug_macinfo */)
830 int section_ids[MAX_NR_V2_DWO_SECTIONS];
831 const gdb_byte *offsets;
832 const gdb_byte *sizes;
833 } v2;
834 } section_pool;
80626a55
DE
835};
836
837/* Data for one DWP file. */
838
839struct dwp_file
840{
400174b1
TT
841 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
842 : name (name_),
843 dbfd (std::move (abfd))
844 {
845 }
846
80626a55
DE
847 /* Name of the file. */
848 const char *name;
849
73869dc2 850 /* File format version. */
400174b1 851 int version = 0;
73869dc2 852
93417882 853 /* The bfd. */
400174b1 854 gdb_bfd_ref_ptr dbfd;
80626a55
DE
855
856 /* Section info for this file. */
400174b1 857 struct dwp_sections sections {};
80626a55 858
57d63ce2 859 /* Table of CUs in the file. */
400174b1 860 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
861
862 /* Table of TUs in the file. */
400174b1 863 const struct dwp_hash_table *tus = nullptr;
80626a55 864
19ac8c2e 865 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
866 htab_up loaded_cus;
867 htab_up loaded_tus;
80626a55 868
73869dc2
DE
869 /* Table to map ELF section numbers to their sections.
870 This is only needed for the DWP V1 file format. */
400174b1
TT
871 unsigned int num_sections = 0;
872 asection **elf_sections = nullptr;
80626a55
DE
873};
874
0963b4bd
MS
875/* Struct used to pass misc. parameters to read_die_and_children, et
876 al. which are used for both .debug_info and .debug_types dies.
877 All parameters here are unchanging for the life of the call. This
dee91e82 878 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
879
880struct die_reader_specs
881{
a32a8923 882 /* The bfd of die_section. */
93311388
DE
883 bfd* abfd;
884
885 /* The CU of the DIE we are parsing. */
886 struct dwarf2_cu *cu;
887
80626a55 888 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
889 struct dwo_file *dwo_file;
890
dee91e82 891 /* The section the die comes from.
3019eac3 892 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
893 struct dwarf2_section_info *die_section;
894
895 /* die_section->buffer. */
d521ce57 896 const gdb_byte *buffer;
f664829e
DE
897
898 /* The end of the buffer. */
899 const gdb_byte *buffer_end;
a2ce51a0 900
685af9cd
TT
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
93311388
DE
903};
904
c0ab21c2
TT
905/* A subclass of die_reader_specs that holds storage and has complex
906 constructor and destructor behavior. */
907
908class cutu_reader : public die_reader_specs
909{
910public:
911
ab432490
SM
912 cutu_reader (dwarf2_per_cu_data *this_cu,
913 dwarf2_per_objfile *per_objfile,
c0ab21c2 914 struct abbrev_table *abbrev_table,
6751ebae 915 int use_existing_cu,
c0ab21c2
TT
916 bool skip_partial);
917
918 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 919 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
920 struct dwarf2_cu *parent_cu = nullptr,
921 struct dwo_file *dwo_file = nullptr);
922
c0ab21c2
TT
923 DISABLE_COPY_AND_ASSIGN (cutu_reader);
924
925 const gdb_byte *info_ptr = nullptr;
926 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
927 bool dummy_p = false;
928
6751ebae
TT
929 /* Release the new CU, putting it on the chain. This cannot be done
930 for dummy CUs. */
931 void keep ();
932
c0ab21c2 933private:
9e021579
SM
934 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
935 dwarf2_per_objfile *per_objfile,
6751ebae 936 int use_existing_cu);
c0ab21c2
TT
937
938 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
939 std::unique_ptr<dwarf2_cu> m_new_cu;
940
941 /* The ordinary abbreviation table. */
942 abbrev_table_up m_abbrev_table_holder;
943
944 /* The DWO abbreviation table. */
945 abbrev_table_up m_dwo_abbrev_table;
946};
dee91e82 947
c906108c 948/* When we construct a partial symbol table entry we only
0963b4bd 949 need this much information. */
6f06d47b 950struct partial_die_info : public allocate_on_obstack
c906108c 951 {
6f06d47b
YQ
952 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
953
954 /* Disable assign but still keep copy ctor, which is needed
955 load_partial_dies. */
956 partial_die_info& operator=(const partial_die_info& rhs) = delete;
957
52356b79
YQ
958 /* Adjust the partial die before generating a symbol for it. This
959 function may set the is_external flag or change the DIE's
960 name. */
961 void fixup (struct dwarf2_cu *cu);
962
48fbe735
YQ
963 /* Read a minimal amount of information into the minimal die
964 structure. */
965 const gdb_byte *read (const struct die_reader_specs *reader,
966 const struct abbrev_info &abbrev,
967 const gdb_byte *info_ptr);
968
72bf9492 969 /* Offset of this DIE. */
6f06d47b 970 const sect_offset sect_off;
72bf9492
DJ
971
972 /* DWARF-2 tag for this DIE. */
6f06d47b 973 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 974
72bf9492 975 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
976 const unsigned int has_children : 1;
977
72bf9492
DJ
978 unsigned int is_external : 1;
979 unsigned int is_declaration : 1;
980 unsigned int has_type : 1;
981 unsigned int has_specification : 1;
982 unsigned int has_pc_info : 1;
481860b3 983 unsigned int may_be_inlined : 1;
72bf9492 984
0c1b455e
TT
985 /* This DIE has been marked DW_AT_main_subprogram. */
986 unsigned int main_subprogram : 1;
987
72bf9492
DJ
988 /* Flag set if the SCOPE field of this structure has been
989 computed. */
990 unsigned int scope_set : 1;
991
fa4028e9
JB
992 /* Flag set if the DIE has a byte_size attribute. */
993 unsigned int has_byte_size : 1;
994
ff908ebf
AW
995 /* Flag set if the DIE has a DW_AT_const_value attribute. */
996 unsigned int has_const_value : 1;
997
98bfdba5
PA
998 /* Flag set if any of the DIE's children are template arguments. */
999 unsigned int has_template_arguments : 1;
1000
52356b79 1001 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1002 unsigned int fixup_called : 1;
1003
36586728
TT
1004 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1005 unsigned int is_dwz : 1;
1006
1007 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1008 unsigned int spec_is_dwz : 1;
1009
72bf9492 1010 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1011 sometimes a default name for unnamed DIEs. */
6f06d47b 1012 const char *name = nullptr;
72bf9492 1013
abc72ce4 1014 /* The linkage name, if present. */
6f06d47b 1015 const char *linkage_name = nullptr;
abc72ce4 1016
72bf9492
DJ
1017 /* The scope to prepend to our children. This is generally
1018 allocated on the comp_unit_obstack, so will disappear
1019 when this compilation unit leaves the cache. */
6f06d47b 1020 const char *scope = nullptr;
72bf9492 1021
95554aad
TT
1022 /* Some data associated with the partial DIE. The tag determines
1023 which field is live. */
1024 union
1025 {
1026 /* The location description associated with this DIE, if any. */
1027 struct dwarf_block *locdesc;
1028 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1029 sect_offset sect_off;
6f06d47b 1030 } d {};
72bf9492
DJ
1031
1032 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1033 CORE_ADDR lowpc = 0;
1034 CORE_ADDR highpc = 0;
72bf9492 1035
93311388 1036 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1037 DW_AT_sibling, if any. */
48fbe735
YQ
1038 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1039 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1040 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1041
1042 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1043 DW_AT_specification (or DW_AT_abstract_origin or
1044 DW_AT_extension). */
6f06d47b 1045 sect_offset spec_offset {};
72bf9492
DJ
1046
1047 /* Pointers to this DIE's parent, first child, and next sibling,
1048 if any. */
6f06d47b
YQ
1049 struct partial_die_info *die_parent = nullptr;
1050 struct partial_die_info *die_child = nullptr;
1051 struct partial_die_info *die_sibling = nullptr;
1052
1053 friend struct partial_die_info *
1054 dwarf2_cu::find_partial_die (sect_offset sect_off);
1055
1056 private:
1057 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1058 partial_die_info (sect_offset sect_off)
1059 : partial_die_info (sect_off, DW_TAG_padding, 0)
1060 {
1061 }
1062
1063 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1064 int has_children_)
1065 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1066 {
1067 is_external = 0;
1068 is_declaration = 0;
1069 has_type = 0;
1070 has_specification = 0;
1071 has_pc_info = 0;
1072 may_be_inlined = 0;
1073 main_subprogram = 0;
1074 scope_set = 0;
1075 has_byte_size = 0;
1076 has_const_value = 0;
1077 has_template_arguments = 0;
1078 fixup_called = 0;
1079 is_dwz = 0;
1080 spec_is_dwz = 0;
1081 }
c906108c
SS
1082 };
1083
c906108c
SS
1084/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1085 but this would require a corresponding change in unpack_field_as_long
1086 and friends. */
1087static int bits_per_byte = 8;
1088
9c6a1327
TT
1089struct variant_part_builder;
1090
1091/* When reading a variant, we track a bit more information about the
1092 field, and store it in an object of this type. */
2ddeaf8a
TT
1093
1094struct variant_field
1095{
9c6a1327
TT
1096 int first_field = -1;
1097 int last_field = -1;
1098
1099 /* A variant can contain other variant parts. */
1100 std::vector<variant_part_builder> variant_parts;
1101
2ddeaf8a
TT
1102 /* If we see a DW_TAG_variant, then this will be set if this is the
1103 default branch. */
9c6a1327
TT
1104 bool default_branch = false;
1105 /* If we see a DW_AT_discr_value, then this will be the discriminant
1106 value. */
1107 ULONGEST discriminant_value = 0;
1108 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1109 data. */
1110 struct dwarf_block *discr_list_data = nullptr;
1111};
1112
1113/* This represents a DW_TAG_variant_part. */
1114
1115struct variant_part_builder
1116{
1117 /* The offset of the discriminant field. */
1118 sect_offset discriminant_offset {};
1119
1120 /* Variants that are direct children of this variant part. */
1121 std::vector<variant_field> variants;
1122
1123 /* True if we're currently reading a variant. */
1124 bool processing_variant = false;
2ddeaf8a
TT
1125};
1126
52059ffd
TT
1127struct nextfield
1128{
be2daae6
TT
1129 int accessibility = 0;
1130 int virtuality = 0;
9c6a1327
TT
1131 /* Variant parts need to find the discriminant, which is a DIE
1132 reference. We track the section offset of each field to make
1133 this link. */
1134 sect_offset offset;
be2daae6 1135 struct field field {};
52059ffd
TT
1136};
1137
1138struct fnfieldlist
1139{
be2daae6
TT
1140 const char *name = nullptr;
1141 std::vector<struct fn_field> fnfields;
52059ffd
TT
1142};
1143
c906108c
SS
1144/* The routines that read and process dies for a C struct or C++ class
1145 pass lists of data member fields and lists of member function fields
1146 in an instance of a field_info structure, as defined below. */
1147struct field_info
c5aa993b 1148 {
0963b4bd 1149 /* List of data member and baseclasses fields. */
be2daae6
TT
1150 std::vector<struct nextfield> fields;
1151 std::vector<struct nextfield> baseclasses;
c906108c 1152
85102364 1153 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1154 int non_public_fields = 0;
c906108c 1155
c5aa993b
JM
1156 /* Member function fieldlist array, contains name of possibly overloaded
1157 member function, number of overloaded member functions and a pointer
1158 to the head of the member function field chain. */
be2daae6 1159 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1160
1161 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1162 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1163 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1164
1165 /* Nested types defined by this class and the number of elements in this
1166 list. */
be2daae6 1167 std::vector<struct decl_field> nested_types_list;
317f7127 1168
9c6a1327
TT
1169 /* If non-null, this is the variant part we are currently
1170 reading. */
1171 variant_part_builder *current_variant_part = nullptr;
1172 /* This holds all the top-level variant parts attached to the type
1173 we're reading. */
1174 std::vector<variant_part_builder> variant_parts;
1175
317f7127
TT
1176 /* Return the total number of fields (including baseclasses). */
1177 int nfields () const
1178 {
1179 return fields.size () + baseclasses.size ();
1180 }
c5aa993b 1181 };
c906108c 1182
ae038cb0
DJ
1183/* Loaded secondary compilation units are kept in memory until they
1184 have not been referenced for the processing of this many
1185 compilation units. Set this to zero to disable caching. Cache
1186 sizes of up to at least twenty will improve startup time for
1187 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1188static int dwarf_max_cache_age = 5;
920d2a44 1189static void
b4f54984
DE
1190show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1191 struct cmd_list_element *c, const char *value)
920d2a44 1192{
3e43a32a 1193 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1194 "DWARF compilation units is %s.\n"),
920d2a44
AC
1195 value);
1196}
4390d890 1197\f
c906108c
SS
1198/* local function prototypes */
1199
918dd910
JK
1200static void dwarf2_find_base_address (struct die_info *die,
1201 struct dwarf2_cu *cu);
1202
891813be 1203static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1204 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1205 const char *name);
0018ea6f 1206
f1902523
JK
1207static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1208 const gdb_byte *info_ptr,
3e225074 1209 struct die_info *type_unit_die);
f1902523 1210
ed2dc618
SM
1211static void dwarf2_build_psymtabs_hard
1212 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1213
72bf9492
DJ
1214static void scan_partial_symbols (struct partial_die_info *,
1215 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1216 int, struct dwarf2_cu *);
c906108c 1217
72bf9492
DJ
1218static void add_partial_symbol (struct partial_die_info *,
1219 struct dwarf2_cu *);
63d06c5c 1220
72bf9492
DJ
1221static void add_partial_namespace (struct partial_die_info *pdi,
1222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1223 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1224
5d7cb8df 1225static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1226 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1227 struct dwarf2_cu *cu);
1228
72bf9492
DJ
1229static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1230 struct dwarf2_cu *cu);
91c24f0a 1231
bc30ff58
JB
1232static void add_partial_subprogram (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1234 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1235
d521ce57 1236static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1237
dee91e82 1238static struct partial_die_info *load_partial_dies
d521ce57 1239 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1240
fb816e8b
TV
1241/* A pair of partial_die_info and compilation unit. */
1242struct cu_partial_die_info
1243{
1244 /* The compilation unit of the partial_die_info. */
1245 struct dwarf2_cu *cu;
1246 /* A partial_die_info. */
1247 struct partial_die_info *pdi;
122cf0f2
AB
1248
1249 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1250 : cu (cu),
1251 pdi (pdi)
405feb71 1252 { /* Nothing. */ }
122cf0f2
AB
1253
1254private:
1255 cu_partial_die_info () = delete;
fb816e8b
TV
1256};
1257
122cf0f2
AB
1258static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1259 struct dwarf2_cu *);
72bf9492 1260
d521ce57
TT
1261static const gdb_byte *read_attribute (const struct die_reader_specs *,
1262 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1263 const gdb_byte *, bool *need_reprocess);
1264
1265static void read_attribute_reprocess (const struct die_reader_specs *reader,
1266 struct attribute *attr);
1267
1268static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1269
ed2dc618
SM
1270static sect_offset read_abbrev_offset
1271 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1272 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1273
ed2dc618
SM
1274static const char *read_indirect_string
1275 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1276 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1277
ed2dc618 1278static const char *read_indirect_string_at_offset
4f44ae6c 1279 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1280
d521ce57
TT
1281static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1282 const gdb_byte *,
3019eac3
DE
1283 unsigned int *);
1284
18a8505e
AT
1285static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1286 ULONGEST str_index);
1287
1288static const char *read_stub_str_index (struct dwarf2_cu *cu,
1289 ULONGEST str_index);
3019eac3 1290
e142c38c 1291static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1292
e142c38c
DJ
1293static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1294 struct dwarf2_cu *);
c906108c 1295
7d45c7c3
KB
1296static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1297 struct dwarf2_cu *cu);
1298
a084a2a6
AT
1299static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1300
05cf31d1
JB
1301static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1302 struct dwarf2_cu *cu);
1303
e142c38c 1304static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1305
e142c38c 1306static struct die_info *die_specification (struct die_info *die,
f2f0e013 1307 struct dwarf2_cu **);
63d06c5c 1308
9c541725 1309static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1310 struct dwarf2_cu *cu);
debd256d 1311
f3f5162e 1312static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1313 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1314 CORE_ADDR, int decode_mapping);
c906108c 1315
804d2729
TT
1316static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1317 const char *);
c906108c 1318
a14ed312 1319static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1320 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1321
ff39bb5e 1322static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1323 struct dwarf2_cu *);
c906108c 1324
ff39bb5e 1325static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1326 struct type *type,
1327 const char *name,
1328 struct obstack *obstack,
12df843f 1329 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1330 const gdb_byte **bytes,
98bfdba5 1331 struct dwarf2_locexpr_baton **baton);
2df3850c 1332
e7c27a73 1333static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1334
b4ba55a1
JB
1335static int need_gnat_info (struct dwarf2_cu *);
1336
3e43a32a
MS
1337static struct type *die_descriptive_type (struct die_info *,
1338 struct dwarf2_cu *);
b4ba55a1
JB
1339
1340static void set_descriptive_type (struct type *, struct die_info *,
1341 struct dwarf2_cu *);
1342
e7c27a73
DJ
1343static struct type *die_containing_type (struct die_info *,
1344 struct dwarf2_cu *);
c906108c 1345
ff39bb5e 1346static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1347 struct dwarf2_cu *);
c906108c 1348
f792889a 1349static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1350
673bfd45
DE
1351static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1352
0d5cff50 1353static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1354
6e70227d 1355static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1356 const char *suffix, int physname,
1357 struct dwarf2_cu *cu);
63d06c5c 1358
e7c27a73 1359static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1360
348e048f
DE
1361static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1362
e7c27a73 1363static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1364
e7c27a73 1365static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1366
96408a79
SA
1367static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1368
71a3c369
TT
1369static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1370
ff013f42 1371static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1372 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1373
41144253 1374/* Return the .debug_loclists section to use for cu. */
1375static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1376
3a2b436a 1377/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1378 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1379enum pc_bounds_kind
1380{
e385593e 1381 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1382 PC_BOUNDS_NOT_PRESENT,
1383
e385593e
JK
1384 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1385 were present but they do not form a valid range of PC addresses. */
1386 PC_BOUNDS_INVALID,
1387
3a2b436a
JK
1388 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1389 PC_BOUNDS_RANGES,
1390
1391 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1392 PC_BOUNDS_HIGH_LOW,
1393};
1394
1395static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *,
891813be 1398 dwarf2_psymtab *);
c906108c 1399
fae299cd
DC
1400static void get_scope_pc_bounds (struct die_info *,
1401 CORE_ADDR *, CORE_ADDR *,
1402 struct dwarf2_cu *);
1403
801e3a5b
JB
1404static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1405 CORE_ADDR, struct dwarf2_cu *);
1406
a14ed312 1407static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1408 struct dwarf2_cu *);
c906108c 1409
a14ed312 1410static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1411 struct type *, struct dwarf2_cu *);
c906108c 1412
a14ed312 1413static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1414 struct die_info *, struct type *,
e7c27a73 1415 struct dwarf2_cu *);
c906108c 1416
a14ed312 1417static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1418 struct type *,
1419 struct dwarf2_cu *);
c906108c 1420
134d01f1 1421static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1422
e7c27a73 1423static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1424
e7c27a73 1425static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1426
5d7cb8df
JK
1427static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1428
804d2729 1429static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1430
27aa8d6a
SW
1431static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1432
74921315
KS
1433static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1434
f55ee35c
JK
1435static struct type *read_module_type (struct die_info *die,
1436 struct dwarf2_cu *cu);
1437
38d518c9 1438static const char *namespace_name (struct die_info *die,
e142c38c 1439 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1440
134d01f1 1441static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1442
7d79de9a
TT
1443static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1444 bool * = nullptr);
c906108c 1445
6e70227d 1446static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1447 struct dwarf2_cu *);
1448
bf6af496 1449static struct die_info *read_die_and_siblings_1
d521ce57 1450 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1451 struct die_info *);
639d11d3 1452
dee91e82 1453static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1454 const gdb_byte *info_ptr,
1455 const gdb_byte **new_info_ptr,
639d11d3
DC
1456 struct die_info *parent);
1457
d521ce57
TT
1458static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1459 struct die_info **, const gdb_byte *,
3e225074 1460 int);
3019eac3 1461
d521ce57 1462static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1463 struct die_info **, const gdb_byte *);
93311388 1464
e7c27a73 1465static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1466
15d034d0 1467static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1468 struct objfile *);
71c25dea 1469
15d034d0 1470static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1471
15d034d0 1472static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1473 struct die_info *die,
1474 struct dwarf2_cu *cu);
1475
ca69b9e6
DE
1476static const char *dwarf2_physname (const char *name, struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
e142c38c 1479static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1480 struct dwarf2_cu **);
9219021c 1481
d97bc12b
DE
1482static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1483
1484static void dump_die_for_error (struct die_info *);
1485
1486static void dump_die_1 (struct ui_file *, int level, int max_level,
1487 struct die_info *);
c906108c 1488
d97bc12b 1489/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1490
51545339 1491static void store_in_ref_table (struct die_info *,
10b3939b 1492 struct dwarf2_cu *);
c906108c 1493
348e048f 1494static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1495 const struct attribute *,
348e048f
DE
1496 struct dwarf2_cu **);
1497
10b3939b 1498static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1499 const struct attribute *,
f2f0e013 1500 struct dwarf2_cu **);
c906108c 1501
348e048f 1502static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1503 const struct attribute *,
348e048f
DE
1504 struct dwarf2_cu **);
1505
ac9ec31b
DE
1506static struct type *get_signatured_type (struct die_info *, ULONGEST,
1507 struct dwarf2_cu *);
1508
1509static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1510 const struct attribute *,
ac9ec31b
DE
1511 struct dwarf2_cu *);
1512
ab432490
SM
1513static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1514 dwarf2_per_objfile *per_objfile);
348e048f 1515
ab432490
SM
1516static void read_signatured_type (signatured_type *sig_type,
1517 dwarf2_per_objfile *per_objfile);
348e048f 1518
63e43d3a
PMR
1519static int attr_to_dynamic_prop (const struct attribute *attr,
1520 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1521 struct dynamic_prop *prop, struct type *type);
63e43d3a 1522
c906108c
SS
1523/* memory allocation interface */
1524
7b5a2f43 1525static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1526
b60c80d6 1527static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1528
43f3e411 1529static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1530
8cf6f0b1
TT
1531static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1532 struct dwarf2_loclist_baton *baton,
ff39bb5e 1533 const struct attribute *attr);
8cf6f0b1 1534
ff39bb5e 1535static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1536 struct symbol *sym,
f1e6e072
TT
1537 struct dwarf2_cu *cu,
1538 int is_block);
4c2df51b 1539
d521ce57
TT
1540static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1541 const gdb_byte *info_ptr,
1542 struct abbrev_info *abbrev);
4bb7a0a7 1543
72bf9492
DJ
1544static hashval_t partial_die_hash (const void *item);
1545
1546static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1547
ae038cb0 1548static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1549 (sect_offset sect_off, unsigned int offset_in_dwz,
1550 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1551
9816fde3 1552static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1553 struct die_info *comp_unit_die,
1554 enum language pretend_language);
93311388 1555
ed2dc618 1556static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1557
120ce1b5
SM
1558static void free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
1559 dwarf2_per_objfile *per_objfile);
ae038cb0 1560
f792889a
DJ
1561static struct type *set_die_type (struct die_info *, struct type *,
1562 struct dwarf2_cu *);
1c379e20 1563
ed2dc618 1564static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1565
ed2dc618 1566static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1567
ab432490
SM
1568static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1569 dwarf2_per_objfile *per_objfile,
1570 bool skip_partial,
1571 enum language pretend_language);
10b3939b 1572
47b14e86
SM
1573static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 enum language pretend_language);
10b3939b 1576
47b14e86
SM
1577static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1578 dwarf2_per_objfile *per_objfile,
1579 enum language pretend_language);
f4dc4d17 1580
10b3939b
DJ
1581static void dwarf2_add_dependence (struct dwarf2_cu *,
1582 struct dwarf2_per_cu_data *);
1583
ae038cb0
DJ
1584static void dwarf2_mark (struct dwarf2_cu *);
1585
1586static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1587
b64f50a1 1588static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1589 dwarf2_per_cu_data *per_cu,
1590 dwarf2_per_objfile *per_objfile);
673bfd45 1591
f792889a 1592static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1593
120ce1b5
SM
1594static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1595 dwarf2_per_objfile *per_objfile,
95554aad
TT
1596 enum language pretend_language);
1597
ed2dc618 1598static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1599
b303c6f6
AB
1600/* Class, the destructor of which frees all allocated queue entries. This
1601 will only have work to do if an error was thrown while processing the
1602 dwarf. If no error was thrown then the queue entries should have all
1603 been processed, and freed, as we went along. */
1604
1605class dwarf2_queue_guard
1606{
1607public:
39856def
TT
1608 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1609 : m_per_objfile (per_objfile)
1610 {
1611 }
b303c6f6
AB
1612
1613 /* Free any entries remaining on the queue. There should only be
1614 entries left if we hit an error while processing the dwarf. */
1615 ~dwarf2_queue_guard ()
1616 {
39856def
TT
1617 /* Ensure that no memory is allocated by the queue. */
1618 std::queue<dwarf2_queue_item> empty;
5989a64e 1619 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1620 }
b303c6f6 1621
39856def 1622 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1623
39856def
TT
1624private:
1625 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1626};
1627
39856def
TT
1628dwarf2_queue_item::~dwarf2_queue_item ()
1629{
1630 /* Anything still marked queued is likely to be in an
1631 inconsistent state, so discard it. */
1632 if (per_cu->queued)
1633 {
1634 if (per_cu->cu != NULL)
120ce1b5 1635 free_one_cached_comp_unit (per_cu, per_objfile);
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
ed2dc618
SM
1667 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1668 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1669 ULONGEST signature, int is_debug_types);
a2ce51a0 1670
ed2dc618
SM
1671static struct dwp_file *get_dwp_file
1672 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1673
3019eac3 1674static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1675 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1676 ULONGEST signature);
3019eac3
DE
1677
1678static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1679 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1680
89e63ee4
DE
1681static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1682
263db9a1
TT
1683/* A unique pointer to a dwo_file. */
1684
51ac9db5 1685typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1686
ed2dc618 1687static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1688
1b80a9fa 1689static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1690
1691static void free_line_header_voidp (void *arg);
4390d890
DE
1692\f
1693/* Various complaints about symbol reading that don't abort the process. */
1694
4390d890
DE
1695static void
1696dwarf2_debug_line_missing_file_complaint (void)
1697{
b98664d3 1698 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1699}
1700
1701static void
1702dwarf2_debug_line_missing_end_sequence_complaint (void)
1703{
b98664d3 1704 complaint (_(".debug_line section has line "
4390d890
DE
1705 "program sequence without an end"));
1706}
1707
1708static void
1709dwarf2_complex_location_expr_complaint (void)
1710{
b98664d3 1711 complaint (_("location expression too complex"));
4390d890
DE
1712}
1713
1714static void
1715dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1716 int arg3)
1717{
b98664d3 1718 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1719 arg1, arg2, arg3);
1720}
1721
4390d890
DE
1722static void
1723dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1724{
b98664d3 1725 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1726 arg1, arg2);
1727}
527f3840
JK
1728
1729/* Hash function for line_header_hash. */
1730
1731static hashval_t
1732line_header_hash (const struct line_header *ofs)
1733{
9c541725 1734 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1735}
1736
1737/* Hash function for htab_create_alloc_ex for line_header_hash. */
1738
1739static hashval_t
1740line_header_hash_voidp (const void *item)
1741{
9a3c8263 1742 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1743
1744 return line_header_hash (ofs);
1745}
1746
1747/* Equality function for line_header_hash. */
1748
1749static int
1750line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1751{
9a3c8263
SM
1752 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1753 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1754
9c541725 1755 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1756 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1757}
1758
4390d890 1759\f
9291a0cd 1760
330cdd98
PA
1761/* See declaration. */
1762
5989a64e
SM
1763dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1764 bool can_copy_)
c3699833
SM
1765 : obfd (obfd),
1766 can_copy (can_copy_)
330cdd98
PA
1767{
1768 if (names == NULL)
1769 names = &dwarf2_elf_names;
1770
330cdd98
PA
1771 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1772 locate_sections (obfd, sec, *names);
1773}
1774
5989a64e 1775dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98
PA
1776{
1777 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1778 free_cached_comp_units ();
1779
b76e467d 1780 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1781 per_cu->imported_symtabs_free ();
fc8e7e75 1782
b2bdb8cf 1783 for (signatured_type *sig_type : all_type_units)
ae640021 1784 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1785
5989a64e 1786 /* Everything else should be on this->obstack. */
330cdd98
PA
1787}
1788
1789/* See declaration. */
1790
1791void
5989a64e 1792dwarf2_per_bfd::free_cached_comp_units ()
330cdd98
PA
1793{
1794 dwarf2_per_cu_data *per_cu = read_in_chain;
1795 dwarf2_per_cu_data **last_chain = &read_in_chain;
1796 while (per_cu != NULL)
1797 {
1798 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1799
fcd3b13d 1800 delete per_cu->cu;
330cdd98
PA
1801 *last_chain = next_cu;
1802 per_cu = next_cu;
1803 }
1804}
1805
11ed8cad
TT
1806/* A helper class that calls free_cached_comp_units on
1807 destruction. */
1808
1809class free_cached_comp_units
1810{
1811public:
1812
1813 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1814 : m_per_objfile (per_objfile)
1815 {
1816 }
1817
1818 ~free_cached_comp_units ()
1819 {
5989a64e 1820 m_per_objfile->per_bfd->free_cached_comp_units ();
11ed8cad
TT
1821 }
1822
1823 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1824
1825private:
1826
1827 dwarf2_per_objfile *m_per_objfile;
1828};
1829
af758d11
SM
1830/* See read.h. */
1831
1832bool
1833dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1834{
1835 gdb_assert (per_cu->index < this->m_symtabs.size ());
1836
1837 return this->m_symtabs[per_cu->index] != nullptr;
1838}
1839
1840/* See read.h. */
1841
1842compunit_symtab *
1843dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1844{
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846
1847 return this->m_symtabs[per_cu->index];
1848}
1849
1850/* See read.h. */
1851
1852void
1853dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1854 compunit_symtab *symtab)
1855{
1856 gdb_assert (per_cu->index < this->m_symtabs.size ());
1857 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1858
1859 this->m_symtabs[per_cu->index] = symtab;
1860}
1861
c906108c 1862/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1863 information and return true if we have enough to do something.
1864 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1865 ELF names are used. CAN_COPY is true for formats where symbol
1866 interposition is possible and so symbol values must follow copy
1867 relocation rules. */
c906108c
SS
1868
1869int
251d32d9 1870dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1871 const struct dwarf2_debug_sections *names,
1872 bool can_copy)
c906108c 1873{
97cbe998
SDJ
1874 if (objfile->flags & OBJF_READNEVER)
1875 return 0;
1876
ed2dc618
SM
1877 struct dwarf2_per_objfile *dwarf2_per_objfile
1878 = get_dwarf2_per_objfile (objfile);
1879
1880 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1881 {
1882 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1883 sharing yet). */
1884 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885
1886 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1887 }
5bfd760d 1888
5989a64e
SM
1889 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1890 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1891 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1892 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1893}
1894
251d32d9
TG
1895/* When loading sections, we look either for uncompressed section or for
1896 compressed section names. */
233a11ab
CS
1897
1898static int
251d32d9
TG
1899section_is_p (const char *section_name,
1900 const struct dwarf2_section_names *names)
233a11ab 1901{
251d32d9
TG
1902 if (names->normal != NULL
1903 && strcmp (section_name, names->normal) == 0)
1904 return 1;
1905 if (names->compressed != NULL
1906 && strcmp (section_name, names->compressed) == 0)
1907 return 1;
1908 return 0;
233a11ab
CS
1909}
1910
330cdd98 1911/* See declaration. */
c906108c 1912
330cdd98 1913void
5989a64e
SM
1914dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1915 const dwarf2_debug_sections &names)
c906108c 1916{
fd361982 1917 flagword aflag = bfd_section_flags (sectp);
251d32d9 1918
dc7650b8
JK
1919 if ((aflag & SEC_HAS_CONTENTS) == 0)
1920 {
1921 }
950b7495
KS
1922 else if (elf_section_data (sectp)->this_hdr.sh_size
1923 > bfd_get_file_size (abfd))
1924 {
1925 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1926 warning (_("Discarding section %s which has a section size (%s"
1927 ") larger than the file size [in module %s]"),
1928 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1929 bfd_get_filename (abfd));
1930 }
330cdd98 1931 else if (section_is_p (sectp->name, &names.info))
c906108c 1932 {
330cdd98 1933 this->info.s.section = sectp;
fd361982 1934 this->info.size = bfd_section_size (sectp);
c906108c 1935 }
330cdd98 1936 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1937 {
330cdd98 1938 this->abbrev.s.section = sectp;
fd361982 1939 this->abbrev.size = bfd_section_size (sectp);
c906108c 1940 }
330cdd98 1941 else if (section_is_p (sectp->name, &names.line))
c906108c 1942 {
330cdd98 1943 this->line.s.section = sectp;
fd361982 1944 this->line.size = bfd_section_size (sectp);
c906108c 1945 }
330cdd98 1946 else if (section_is_p (sectp->name, &names.loc))
c906108c 1947 {
330cdd98 1948 this->loc.s.section = sectp;
fd361982 1949 this->loc.size = bfd_section_size (sectp);
c906108c 1950 }
330cdd98 1951 else if (section_is_p (sectp->name, &names.loclists))
43988095 1952 {
330cdd98 1953 this->loclists.s.section = sectp;
fd361982 1954 this->loclists.size = bfd_section_size (sectp);
43988095 1955 }
330cdd98 1956 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1957 {
330cdd98 1958 this->macinfo.s.section = sectp;
fd361982 1959 this->macinfo.size = bfd_section_size (sectp);
c906108c 1960 }
330cdd98 1961 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1962 {
330cdd98 1963 this->macro.s.section = sectp;
fd361982 1964 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1965 }
330cdd98 1966 else if (section_is_p (sectp->name, &names.str))
c906108c 1967 {
330cdd98 1968 this->str.s.section = sectp;
fd361982 1969 this->str.size = bfd_section_size (sectp);
c906108c 1970 }
18a8505e
AT
1971 else if (section_is_p (sectp->name, &names.str_offsets))
1972 {
1973 this->str_offsets.s.section = sectp;
1974 this->str_offsets.size = bfd_section_size (sectp);
1975 }
330cdd98 1976 else if (section_is_p (sectp->name, &names.line_str))
43988095 1977 {
330cdd98 1978 this->line_str.s.section = sectp;
fd361982 1979 this->line_str.size = bfd_section_size (sectp);
43988095 1980 }
330cdd98 1981 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1982 {
330cdd98 1983 this->addr.s.section = sectp;
fd361982 1984 this->addr.size = bfd_section_size (sectp);
3019eac3 1985 }
330cdd98 1986 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1987 {
330cdd98 1988 this->frame.s.section = sectp;
fd361982 1989 this->frame.size = bfd_section_size (sectp);
b6af0555 1990 }
330cdd98 1991 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1992 {
330cdd98 1993 this->eh_frame.s.section = sectp;
fd361982 1994 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1995 }
330cdd98 1996 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1997 {
330cdd98 1998 this->ranges.s.section = sectp;
fd361982 1999 this->ranges.size = bfd_section_size (sectp);
af34e669 2000 }
330cdd98 2001 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2002 {
330cdd98 2003 this->rnglists.s.section = sectp;
fd361982 2004 this->rnglists.size = bfd_section_size (sectp);
43988095 2005 }
330cdd98 2006 else if (section_is_p (sectp->name, &names.types))
348e048f 2007 {
8b70b953
TT
2008 struct dwarf2_section_info type_section;
2009
2010 memset (&type_section, 0, sizeof (type_section));
049412e3 2011 type_section.s.section = sectp;
fd361982 2012 type_section.size = bfd_section_size (sectp);
8b70b953 2013
fd5866f6 2014 this->types.push_back (type_section);
348e048f 2015 }
330cdd98 2016 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2017 {
330cdd98 2018 this->gdb_index.s.section = sectp;
fd361982 2019 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2020 }
927aa2e7
JK
2021 else if (section_is_p (sectp->name, &names.debug_names))
2022 {
2023 this->debug_names.s.section = sectp;
fd361982 2024 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2025 }
2026 else if (section_is_p (sectp->name, &names.debug_aranges))
2027 {
2028 this->debug_aranges.s.section = sectp;
fd361982 2029 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2030 }
dce234bc 2031
fd361982
AM
2032 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2033 && bfd_section_vma (sectp) == 0)
330cdd98 2034 this->has_section_at_zero = true;
c906108c
SS
2035}
2036
dce234bc 2037/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2038 SECTION_NAME. */
af34e669 2039
dce234bc 2040void
3017a003
TG
2041dwarf2_get_section_info (struct objfile *objfile,
2042 enum dwarf2_section_enum sect,
d521ce57 2043 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2044 bfd_size_type *sizep)
2045{
5bfd760d 2046 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2047 struct dwarf2_section_info *info;
a3b2a86b
TT
2048
2049 /* We may see an objfile without any DWARF, in which case we just
2050 return nothing. */
2051 if (data == NULL)
2052 {
2053 *sectp = NULL;
2054 *bufp = NULL;
2055 *sizep = 0;
2056 return;
2057 }
3017a003
TG
2058 switch (sect)
2059 {
2060 case DWARF2_DEBUG_FRAME:
5989a64e 2061 info = &data->per_bfd->frame;
3017a003
TG
2062 break;
2063 case DWARF2_EH_FRAME:
5989a64e 2064 info = &data->per_bfd->eh_frame;
3017a003
TG
2065 break;
2066 default:
2067 gdb_assert_not_reached ("unexpected section");
2068 }
dce234bc 2069
96b79293 2070 info->read (objfile);
dce234bc 2071
96b79293 2072 *sectp = info->get_bfd_section ();
dce234bc
PP
2073 *bufp = info->buffer;
2074 *sizep = info->size;
2075}
2076
36586728
TT
2077/* A helper function to find the sections for a .dwz file. */
2078
2079static void
2080locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2081{
9a3c8263 2082 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2083
2084 /* Note that we only support the standard ELF names, because .dwz
2085 is ELF-only (at the time of writing). */
2086 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2087 {
049412e3 2088 dwz_file->abbrev.s.section = sectp;
fd361982 2089 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2092 {
049412e3 2093 dwz_file->info.s.section = sectp;
fd361982 2094 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2097 {
049412e3 2098 dwz_file->str.s.section = sectp;
fd361982 2099 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2100 }
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2102 {
049412e3 2103 dwz_file->line.s.section = sectp;
fd361982 2104 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2105 }
2106 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2107 {
049412e3 2108 dwz_file->macro.s.section = sectp;
fd361982 2109 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2110 }
2ec9a5e0
TT
2111 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2112 {
049412e3 2113 dwz_file->gdb_index.s.section = sectp;
fd361982 2114 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2115 }
927aa2e7
JK
2116 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2117 {
2118 dwz_file->debug_names.s.section = sectp;
fd361982 2119 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2120 }
36586728
TT
2121}
2122
c4973306 2123/* See dwarf2read.h. */
36586728 2124
c4973306 2125struct dwz_file *
c3699833 2126dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2127{
36586728 2128 const char *filename;
acd13123 2129 bfd_size_type buildid_len_arg;
dc294be5
TT
2130 size_t buildid_len;
2131 bfd_byte *buildid;
36586728 2132
c3699833
SM
2133 if (per_bfd->dwz_file != NULL)
2134 return per_bfd->dwz_file.get ();
36586728 2135
4db1a1dc 2136 bfd_set_error (bfd_error_no_error);
791afaa2 2137 gdb::unique_xmalloc_ptr<char> data
c3699833 2138 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2139 &buildid_len_arg, &buildid));
4db1a1dc
TT
2140 if (data == NULL)
2141 {
2142 if (bfd_get_error () == bfd_error_no_error)
2143 return NULL;
2144 error (_("could not read '.gnu_debugaltlink' section: %s"),
2145 bfd_errmsg (bfd_get_error ()));
2146 }
791afaa2
TT
2147
2148 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2149
acd13123
TT
2150 buildid_len = (size_t) buildid_len_arg;
2151
791afaa2 2152 filename = data.get ();
d721ba37
PA
2153
2154 std::string abs_storage;
36586728
TT
2155 if (!IS_ABSOLUTE_PATH (filename))
2156 {
14278e1f 2157 gdb::unique_xmalloc_ptr<char> abs
c3699833 2158 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2159
14278e1f 2160 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2161 filename = abs_storage.c_str ();
36586728
TT
2162 }
2163
dc294be5
TT
2164 /* First try the file name given in the section. If that doesn't
2165 work, try to use the build-id instead. */
ad80db5b 2166 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2167 if (dwz_bfd != NULL)
36586728 2168 {
192b62ce 2169 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2170 dwz_bfd.reset (nullptr);
36586728
TT
2171 }
2172
dc294be5
TT
2173 if (dwz_bfd == NULL)
2174 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2175
0d79cdc4
AM
2176 if (dwz_bfd == nullptr)
2177 {
2178 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2179 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2180
2181 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2182 buildid_len,
2183 origname,
2184 &alt_filename));
2185
2186 if (fd.get () >= 0)
2187 {
2188 /* File successfully retrieved from server. */
ad80db5b 2189 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2190
2191 if (dwz_bfd == nullptr)
2192 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2193 alt_filename.get ());
2194 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2195 dwz_bfd.reset (nullptr);
2196 }
2197 }
2198
dc294be5
TT
2199 if (dwz_bfd == NULL)
2200 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2201 bfd_get_filename (per_bfd->obfd));
dc294be5 2202
7ff8cb8c
TT
2203 std::unique_ptr<struct dwz_file> result
2204 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2205
7ff8cb8c
TT
2206 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2207 result.get ());
36586728 2208
c3699833
SM
2209 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2210 per_bfd->dwz_file = std::move (result);
2211 return per_bfd->dwz_file.get ();
36586728 2212}
9291a0cd 2213\f
7b9f3c50
DE
2214/* DWARF quick_symbols_functions support. */
2215
2216/* TUs can share .debug_line entries, and there can be a lot more TUs than
2217 unique line tables, so we maintain a separate table of all .debug_line
2218 derived entries to support the sharing.
2219 All the quick functions need is the list of file names. We discard the
2220 line_header when we're done and don't need to record it here. */
2221struct quick_file_names
2222{
094b34ac
DE
2223 /* The data used to construct the hash key. */
2224 struct stmt_list_hash hash;
7b9f3c50
DE
2225
2226 /* The number of entries in file_names, real_names. */
2227 unsigned int num_file_names;
2228
2229 /* The file names from the line table, after being run through
2230 file_full_name. */
2231 const char **file_names;
2232
2233 /* The file names from the line table after being run through
2234 gdb_realpath. These are computed lazily. */
2235 const char **real_names;
2236};
2237
2238/* When using the index (and thus not using psymtabs), each CU has an
2239 object of this type. This is used to hold information needed by
2240 the various "quick" methods. */
2241struct dwarf2_per_cu_quick_data
2242{
2243 /* The file table. This can be NULL if there was no file table
2244 or it's currently not read in.
5989a64e 2245 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2246 struct quick_file_names *file_names;
2247
7b9f3c50
DE
2248 /* A temporary mark bit used when iterating over all CUs in
2249 expand_symtabs_matching. */
2250 unsigned int mark : 1;
2251
2252 /* True if we've tried to read the file table and found there isn't one.
2253 There will be no point in trying to read it again next time. */
2254 unsigned int no_file_data : 1;
2255};
2256
094b34ac
DE
2257/* Utility hash function for a stmt_list_hash. */
2258
2259static hashval_t
2260hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2261{
2262 hashval_t v = 0;
2263
2264 if (stmt_list_hash->dwo_unit != NULL)
2265 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2266 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2267 return v;
2268}
2269
2270/* Utility equality function for a stmt_list_hash. */
2271
2272static int
2273eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2274 const struct stmt_list_hash *rhs)
2275{
2276 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2277 return 0;
2278 if (lhs->dwo_unit != NULL
2279 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2280 return 0;
2281
9c541725 2282 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2283}
2284
7b9f3c50
DE
2285/* Hash function for a quick_file_names. */
2286
2287static hashval_t
2288hash_file_name_entry (const void *e)
2289{
9a3c8263
SM
2290 const struct quick_file_names *file_data
2291 = (const struct quick_file_names *) e;
7b9f3c50 2292
094b34ac 2293 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2294}
2295
2296/* Equality function for a quick_file_names. */
2297
2298static int
2299eq_file_name_entry (const void *a, const void *b)
2300{
9a3c8263
SM
2301 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2302 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2303
094b34ac 2304 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2305}
2306
2307/* Delete function for a quick_file_names. */
2308
2309static void
2310delete_file_name_entry (void *e)
2311{
9a3c8263 2312 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2313 int i;
2314
2315 for (i = 0; i < file_data->num_file_names; ++i)
2316 {
2317 xfree ((void*) file_data->file_names[i]);
2318 if (file_data->real_names)
2319 xfree ((void*) file_data->real_names[i]);
2320 }
2321
45940949
TT
2322 /* The space for the struct itself lives on the obstack, so we don't
2323 free it here. */
7b9f3c50
DE
2324}
2325
2326/* Create a quick_file_names hash table. */
2327
5895093f 2328static htab_up
7b9f3c50
DE
2329create_quick_file_names_table (unsigned int nr_initial_entries)
2330{
5895093f
TT
2331 return htab_up (htab_create_alloc (nr_initial_entries,
2332 hash_file_name_entry, eq_file_name_entry,
2333 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2334}
9291a0cd 2335
ab432490
SM
2336/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2337 function is unrelated to symtabs, symtab would have to be created afterwards.
2338 You should call age_cached_comp_units after processing the CU. */
918dd910
JK
2339
2340static void
ab432490
SM
2341load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2342 bool skip_partial)
918dd910 2343{
3019eac3 2344 if (per_cu->is_debug_types)
ab432490 2345 load_full_type_unit (per_cu, per_objfile);
918dd910 2346 else
ab432490 2347 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2348
cc12ce38
DE
2349 if (per_cu->cu == NULL)
2350 return; /* Dummy CU. */
2dc860c0
DE
2351
2352 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2353}
2354
97a1449a 2355/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2356
9291a0cd 2357static void
97a1449a
SM
2358dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2359 dwarf2_per_objfile *dwarf2_per_objfile,
2360 bool skip_partial)
9291a0cd 2361{
f4dc4d17
DE
2362 /* Skip type_unit_groups, reading the type units they contain
2363 is handled elsewhere. */
197400e8 2364 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2365 return;
2366
b303c6f6
AB
2367 /* The destructor of dwarf2_queue_guard frees any entries left on
2368 the queue. After this point we're guaranteed to leave this function
2369 with the dwarf queue empty. */
39856def 2370 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2371
af758d11 2372 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad 2373 {
120ce1b5 2374 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
ab432490 2375 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
89e63ee4
DE
2376
2377 /* If we just loaded a CU from a DWO, and we're working with an index
2378 that may badly handle TUs, load all the TUs in that DWO as well.
2379 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2380 if (!per_cu->is_debug_types
cc12ce38 2381 && per_cu->cu != NULL
89e63ee4 2382 && per_cu->cu->dwo_unit != NULL
5989a64e
SM
2383 && dwarf2_per_objfile->per_bfd->index_table != NULL
2384 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2385 /* DWP files aren't supported yet. */
ed2dc618 2386 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2387 queue_and_load_all_dwo_tus (per_cu);
95554aad 2388 }
9291a0cd 2389
ed2dc618 2390 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2391
2392 /* Age the cache, releasing compilation units that have not
2393 been used recently. */
ed2dc618 2394 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2395}
2396
97a1449a
SM
2397/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2398 the per-objfile for which this symtab is instantiated.
2399
2400 Returns the resulting symbol table. */
2fdf6df6 2401
43f3e411 2402static struct compunit_symtab *
97a1449a
SM
2403dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2404 dwarf2_per_objfile *dwarf2_per_objfile,
2405 bool skip_partial)
9291a0cd 2406{
5989a64e 2407 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2408
2409 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2410 {
11ed8cad 2411 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2412 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2413 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2414 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2415 }
f194fefb 2416
af758d11 2417 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2418}
2419
ff4c9fec 2420/* See declaration. */
f4dc4d17 2421
ff4c9fec 2422dwarf2_per_cu_data *
5989a64e 2423dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2424{
b76e467d 2425 if (index >= this->all_comp_units.size ())
ff4c9fec 2426 {
b76e467d 2427 index -= this->all_comp_units.size ();
b2bdb8cf 2428 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2429 return &this->all_type_units[index]->per_cu;
2430 }
f4dc4d17 2431
ff4c9fec
SM
2432 return this->all_comp_units[index];
2433}
f4dc4d17 2434
ff4c9fec 2435/* See declaration. */
2fdf6df6 2436
ff4c9fec 2437dwarf2_per_cu_data *
5989a64e 2438dwarf2_per_bfd::get_cu (int index)
1fd400ff 2439{
b76e467d 2440 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2441
ff4c9fec 2442 return this->all_comp_units[index];
f4dc4d17
DE
2443}
2444
ff4c9fec 2445/* See declaration. */
f4dc4d17 2446
ff4c9fec 2447signatured_type *
5989a64e 2448dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2449{
b2bdb8cf 2450 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2451
ff4c9fec 2452 return this->all_type_units[index];
1fd400ff
TT
2453}
2454
d3473f0c
TT
2455/* See read.h. */
2456
2457dwarf2_per_cu_data *
5989a64e 2458dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2459{
2460 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2461 result->per_bfd = this;
d3473f0c
TT
2462 result->index = m_num_psymtabs++;
2463 return result;
2464}
2465
2466/* See read.h. */
2467
2468signatured_type *
5989a64e 2469dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2470{
2471 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2472 result->per_cu.per_bfd = this;
d3473f0c
TT
2473 result->per_cu.index = m_num_psymtabs++;
2474 return result;
2475}
2476
45940949
TT
2477/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2478 obstack, and constructed with the specified field values. */
4b514bc8
JK
2479
2480static dwarf2_per_cu_data *
ed2dc618 2481create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2482 struct dwarf2_section_info *section,
2483 int is_dwz,
2484 sect_offset sect_off, ULONGEST length)
2485{
5989a64e 2486 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
4b514bc8
JK
2487 the_cu->sect_off = sect_off;
2488 the_cu->length = length;
4b514bc8 2489 the_cu->section = section;
5989a64e 2490 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
45940949 2491 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2492 the_cu->is_dwz = is_dwz;
2493 return the_cu;
2494}
2495
2ec9a5e0
TT
2496/* A helper for create_cus_from_index that handles a given list of
2497 CUs. */
2fdf6df6 2498
74a0d9f6 2499static void
12359b5e 2500create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2501 const gdb_byte *cu_list, offset_type n_elements,
2502 struct dwarf2_section_info *section,
b76e467d 2503 int is_dwz)
9291a0cd 2504{
12359b5e 2505 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2506 {
74a0d9f6 2507 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2508
2509 sect_offset sect_off
2510 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2511 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2512 cu_list += 2 * 8;
2513
b76e467d 2514 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2515 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2516 sect_off, length);
5989a64e 2517 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2518 }
9291a0cd
TT
2519}
2520
2ec9a5e0 2521/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2522 the CU objects for this objfile. */
2ec9a5e0 2523
74a0d9f6 2524static void
12359b5e 2525create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2526 const gdb_byte *cu_list, offset_type cu_list_elements,
2527 const gdb_byte *dwz_list, offset_type dwz_elements)
2528{
5989a64e
SM
2529 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2530 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
b76e467d 2531 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2532
12359b5e 2533 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
5989a64e 2534 &dwarf2_per_objfile->per_bfd->info, 0);
2ec9a5e0
TT
2535
2536 if (dwz_elements == 0)
74a0d9f6 2537 return;
2ec9a5e0 2538
c3699833 2539 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
12359b5e 2540 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2541 &dwz->info, 1);
2ec9a5e0
TT
2542}
2543
1fd400ff 2544/* Create the signatured type hash table from the index. */
673bfd45 2545
74a0d9f6 2546static void
12359b5e
SM
2547create_signatured_type_table_from_index
2548 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2549 struct dwarf2_section_info *section,
2550 const gdb_byte *bytes,
2551 offset_type elements)
1fd400ff 2552{
5989a64e
SM
2553 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2554 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2555
298e9637 2556 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2557
12359b5e 2558 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2559 {
52dc124a 2560 struct signatured_type *sig_type;
9c541725 2561 ULONGEST signature;
1fd400ff 2562 void **slot;
9c541725 2563 cu_offset type_offset_in_tu;
1fd400ff 2564
74a0d9f6 2565 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2566 sect_offset sect_off
2567 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2568 type_offset_in_tu
2569 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2570 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2571 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2572 bytes += 3 * 8;
2573
5989a64e 2574 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
52dc124a 2575 sig_type->signature = signature;
9c541725 2576 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2577 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2578 sig_type->per_cu.section = section;
9c541725 2579 sig_type->per_cu.sect_off = sect_off;
52dc124a 2580 sig_type->per_cu.v.quick
5989a64e 2581 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
1fd400ff
TT
2582 struct dwarf2_per_cu_quick_data);
2583
b0b6a987 2584 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2585 *slot = sig_type;
1fd400ff 2586
5989a64e 2587 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2588 }
2589
5989a64e 2590 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2591}
2592
927aa2e7
JK
2593/* Create the signatured type hash table from .debug_names. */
2594
2595static void
2596create_signatured_type_table_from_debug_names
ed2dc618 2597 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2598 const mapped_debug_names &map,
2599 struct dwarf2_section_info *section,
2600 struct dwarf2_section_info *abbrev_section)
2601{
ed2dc618
SM
2602 struct objfile *objfile = dwarf2_per_objfile->objfile;
2603
96b79293
TT
2604 section->read (objfile);
2605 abbrev_section->read (objfile);
927aa2e7 2606
5989a64e
SM
2607 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2608 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2609
298e9637 2610 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2611
2612 for (uint32_t i = 0; i < map.tu_count; ++i)
2613 {
2614 struct signatured_type *sig_type;
927aa2e7 2615 void **slot;
927aa2e7
JK
2616
2617 sect_offset sect_off
2618 = (sect_offset) (extract_unsigned_integer
2619 (map.tu_table_reordered + i * map.offset_size,
2620 map.offset_size,
2621 map.dwarf5_byte_order));
2622
2623 comp_unit_head cu_header;
ed2dc618
SM
2624 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2625 abbrev_section,
927aa2e7
JK
2626 section->buffer + to_underlying (sect_off),
2627 rcuh_kind::TYPE);
2628
5989a64e 2629 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2630 sig_type->signature = cu_header.signature;
2631 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2632 sig_type->per_cu.is_debug_types = 1;
2633 sig_type->per_cu.section = section;
2634 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2635 sig_type->per_cu.v.quick
5989a64e 2636 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2637 struct dwarf2_per_cu_quick_data);
2638
b0b6a987 2639 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2640 *slot = sig_type;
2641
5989a64e 2642 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2643 }
2644
5989a64e 2645 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2646}
2647
9291a0cd
TT
2648/* Read the address map data from the mapped index, and use it to
2649 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2650
9291a0cd 2651static void
ed2dc618
SM
2652create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2653 struct mapped_index *index)
9291a0cd 2654{
ed2dc618 2655 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2656 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2657 const gdb_byte *iter, *end;
9291a0cd 2658 struct addrmap *mutable_map;
9291a0cd
TT
2659 CORE_ADDR baseaddr;
2660
8268c778
PA
2661 auto_obstack temp_obstack;
2662
9291a0cd
TT
2663 mutable_map = addrmap_create_mutable (&temp_obstack);
2664
f00a2de2
PA
2665 iter = index->address_table.data ();
2666 end = iter + index->address_table.size ();
9291a0cd 2667
b3b3bada 2668 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2669
2670 while (iter < end)
2671 {
2672 ULONGEST hi, lo, cu_index;
2673 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2674 iter += 8;
2675 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2676 iter += 8;
2677 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2678 iter += 4;
f652bce2 2679
24a55014 2680 if (lo > hi)
f652bce2 2681 {
b98664d3 2682 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2683 hex_string (lo), hex_string (hi));
24a55014 2684 continue;
f652bce2 2685 }
24a55014 2686
5989a64e 2687 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2688 {
b98664d3 2689 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2690 (unsigned) cu_index);
24a55014 2691 continue;
f652bce2 2692 }
24a55014 2693
79748972
TT
2694 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2695 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2696 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2697 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2698 }
2699
d320c2b5 2700 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2701 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2702}
2703
927aa2e7
JK
2704/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2705 populate the objfile's psymtabs_addrmap. */
2706
2707static void
ed2dc618 2708create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2709 struct dwarf2_section_info *section)
2710{
ed2dc618 2711 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2712 bfd *abfd = objfile->obfd;
08feed99 2713 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2714 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2715
2716 auto_obstack temp_obstack;
2717 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2718
2719 std::unordered_map<sect_offset,
2720 dwarf2_per_cu_data *,
2721 gdb::hash_enum<sect_offset>>
2722 debug_info_offset_to_per_cu;
5989a64e 2723 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2724 {
927aa2e7
JK
2725 const auto insertpair
2726 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2727 if (!insertpair.second)
2728 {
2729 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2730 "debug_info_offset %s, ignoring .debug_aranges."),
2731 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2732 return;
2733 }
2734 }
2735
96b79293 2736 section->read (objfile);
927aa2e7
JK
2737
2738 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2739
2740 const gdb_byte *addr = section->buffer;
2741
2742 while (addr < section->buffer + section->size)
2743 {
2744 const gdb_byte *const entry_addr = addr;
2745 unsigned int bytes_read;
2746
2747 const LONGEST entry_length = read_initial_length (abfd, addr,
2748 &bytes_read);
2749 addr += bytes_read;
2750
2751 const gdb_byte *const entry_end = addr + entry_length;
2752 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2753 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2754 if (addr + entry_length > section->buffer + section->size)
2755 {
47e3f474 2756 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2757 "length %s exceeds section length %s, "
2758 "ignoring .debug_aranges."),
47e3f474
TV
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
927aa2e7
JK
2761 plongest (bytes_read + entry_length),
2762 pulongest (section->size));
2763 return;
2764 }
2765
2766 /* The version number. */
2767 const uint16_t version = read_2_bytes (abfd, addr);
2768 addr += 2;
2769 if (version != 2)
2770 {
47e3f474 2771 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2772 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2773 objfile_name (objfile),
2774 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2775 return;
2776 }
2777
2778 const uint64_t debug_info_offset
2779 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2780 addr += offset_size;
2781 const auto per_cu_it
2782 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2783 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2784 {
47e3f474 2785 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2786 "debug_info_offset %s does not exists, "
2787 "ignoring .debug_aranges."),
47e3f474
TV
2788 objfile_name (objfile),
2789 plongest (entry_addr - section->buffer),
927aa2e7
JK
2790 pulongest (debug_info_offset));
2791 return;
2792 }
2793 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2794
2795 const uint8_t address_size = *addr++;
2796 if (address_size < 1 || address_size > 8)
2797 {
47e3f474 2798 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2799 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2800 objfile_name (objfile),
2801 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2802 return;
2803 }
2804
2805 const uint8_t segment_selector_size = *addr++;
2806 if (segment_selector_size != 0)
2807 {
47e3f474 2808 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2809 "segment_selector_size %u is not supported, "
2810 "ignoring .debug_aranges."),
47e3f474
TV
2811 objfile_name (objfile),
2812 plongest (entry_addr - section->buffer),
927aa2e7
JK
2813 segment_selector_size);
2814 return;
2815 }
2816
2817 /* Must pad to an alignment boundary that is twice the address
2818 size. It is undocumented by the DWARF standard but GCC does
2819 use it. */
2820 for (size_t padding = ((-(addr - section->buffer))
2821 & (2 * address_size - 1));
2822 padding > 0; padding--)
2823 if (*addr++ != 0)
2824 {
47e3f474 2825 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2826 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2827 objfile_name (objfile),
2828 plongest (entry_addr - section->buffer));
927aa2e7
JK
2829 return;
2830 }
2831
2832 for (;;)
2833 {
2834 if (addr + 2 * address_size > entry_end)
2835 {
47e3f474 2836 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2837 "address list is not properly terminated, "
2838 "ignoring .debug_aranges."),
47e3f474
TV
2839 objfile_name (objfile),
2840 plongest (entry_addr - section->buffer));
927aa2e7
JK
2841 return;
2842 }
2843 ULONGEST start = extract_unsigned_integer (addr, address_size,
2844 dwarf5_byte_order);
2845 addr += address_size;
2846 ULONGEST length = extract_unsigned_integer (addr, address_size,
2847 dwarf5_byte_order);
2848 addr += address_size;
2849 if (start == 0 && length == 0)
2850 break;
5989a64e 2851 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2852 {
2853 /* Symbol was eliminated due to a COMDAT group. */
2854 continue;
2855 }
2856 ULONGEST end = start + length;
79748972
TT
2857 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2858 - baseaddr);
2859 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2860 - baseaddr);
927aa2e7
JK
2861 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2862 }
2863 }
2864
d320c2b5 2865 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2866 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2867}
2868
9291a0cd
TT
2869/* Find a slot in the mapped index INDEX for the object named NAME.
2870 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2871 constant pool and return true. If NAME cannot be found, return
2872 false. */
2fdf6df6 2873
109483d9 2874static bool
9291a0cd
TT
2875find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2876 offset_type **vec_out)
2877{
0cf03b49 2878 offset_type hash;
9291a0cd 2879 offset_type slot, step;
559a7a62 2880 int (*cmp) (const char *, const char *);
9291a0cd 2881
791afaa2 2882 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2883 if (current_language->la_language == language_cplus
45280282
IB
2884 || current_language->la_language == language_fortran
2885 || current_language->la_language == language_d)
0cf03b49
JK
2886 {
2887 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2888 not contain any. */
a8719064 2889
72998fb3 2890 if (strchr (name, '(') != NULL)
0cf03b49 2891 {
109483d9 2892 without_params = cp_remove_params (name);
0cf03b49 2893
72998fb3 2894 if (without_params != NULL)
791afaa2 2895 name = without_params.get ();
0cf03b49
JK
2896 }
2897 }
2898
559a7a62 2899 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2900 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2901 simulate our NAME being searched is also lowercased. */
2902 hash = mapped_index_string_hash ((index->version == 4
2903 && case_sensitivity == case_sensitive_off
2904 ? 5 : index->version),
2905 name);
2906
f00a2de2
PA
2907 slot = hash & (index->symbol_table.size () - 1);
2908 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2909 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2910
2911 for (;;)
2912 {
9291a0cd 2913 const char *str;
f00a2de2
PA
2914
2915 const auto &bucket = index->symbol_table[slot];
2916 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2917 return false;
9291a0cd 2918
f00a2de2 2919 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2920 if (!cmp (name, str))
9291a0cd
TT
2921 {
2922 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2923 + MAYBE_SWAP (bucket.vec));
109483d9 2924 return true;
9291a0cd
TT
2925 }
2926
f00a2de2 2927 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2928 }
2929}
2930
4485a1c1
SM
2931/* A helper function that reads the .gdb_index from BUFFER and fills
2932 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2933 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2934 ok to use deprecated sections.
2935
2936 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2937 out parameters that are filled in with information about the CU and
2938 TU lists in the section.
2939
4485a1c1 2940 Returns true if all went well, false otherwise. */
2fdf6df6 2941
d33bc52e 2942static bool
3810f182 2943read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2944 bool deprecated_ok,
2945 gdb::array_view<const gdb_byte> buffer,
2946 struct mapped_index *map,
2947 const gdb_byte **cu_list,
2948 offset_type *cu_list_elements,
2949 const gdb_byte **types_list,
2950 offset_type *types_list_elements)
2951{
2952 const gdb_byte *addr = &buffer[0];
82430852 2953
9291a0cd 2954 /* Version check. */
4485a1c1 2955 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2956 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2957 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2958 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2959 indices. */
831adc1f 2960 if (version < 4)
481860b3
GB
2961 {
2962 static int warning_printed = 0;
2963 if (!warning_printed)
2964 {
2965 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2966 filename);
481860b3
GB
2967 warning_printed = 1;
2968 }
2969 return 0;
2970 }
2971 /* Index version 4 uses a different hash function than index version
2972 5 and later.
2973
2974 Versions earlier than 6 did not emit psymbols for inlined
2975 functions. Using these files will cause GDB not to be able to
2976 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2977 indices unless the user has done
2978 "set use-deprecated-index-sections on". */
2ec9a5e0 2979 if (version < 6 && !deprecated_ok)
481860b3
GB
2980 {
2981 static int warning_printed = 0;
2982 if (!warning_printed)
2983 {
e615022a
DE
2984 warning (_("\
2985Skipping deprecated .gdb_index section in %s.\n\
2986Do \"set use-deprecated-index-sections on\" before the file is read\n\
2987to use the section anyway."),
2ec9a5e0 2988 filename);
481860b3
GB
2989 warning_printed = 1;
2990 }
2991 return 0;
2992 }
796a7ff8 2993 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2994 of the TU (for symbols coming from TUs),
2995 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2996 Plus gold-generated indices can have duplicate entries for global symbols,
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2998 These are just performance bugs, and we can't distinguish gdb-generated
2999 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3000
481860b3 3001 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3002 longer backward compatible. */
796a7ff8 3003 if (version > 8)
594e8718 3004 return 0;
9291a0cd 3005
559a7a62 3006 map->version = version;
9291a0cd 3007
4485a1c1 3008 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3009
4485a1c1 3010 int i = 0;
2ec9a5e0
TT
3011 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3012 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3013 / 8);
1fd400ff
TT
3014 ++i;
3015
2ec9a5e0
TT
3016 *types_list = addr + MAYBE_SWAP (metadata[i]);
3017 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3018 - MAYBE_SWAP (metadata[i]))
3019 / 8);
987d643c 3020 ++i;
1fd400ff 3021
f00a2de2
PA
3022 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3023 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3024 map->address_table
3025 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3026 ++i;
3027
f00a2de2
PA
3028 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3029 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3030 map->symbol_table
3031 = gdb::array_view<mapped_index::symbol_table_slot>
3032 ((mapped_index::symbol_table_slot *) symbol_table,
3033 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3034
f00a2de2 3035 ++i;
f9d83a0b 3036 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3037
2ec9a5e0
TT
3038 return 1;
3039}
3040
4485a1c1
SM
3041/* Callback types for dwarf2_read_gdb_index. */
3042
3043typedef gdb::function_view
5989a64e 3044 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3045 get_gdb_index_contents_ftype;
3046typedef gdb::function_view
3047 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3048 get_gdb_index_contents_dwz_ftype;
3049
927aa2e7 3050/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3051 elements of all the CUs and return 1. Otherwise, return 0. */
3052
3053static int
4485a1c1
SM
3054dwarf2_read_gdb_index
3055 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3056 get_gdb_index_contents_ftype get_gdb_index_contents,
3057 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3058{
2ec9a5e0
TT
3059 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3060 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3061 struct dwz_file *dwz;
12359b5e 3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3063
4485a1c1 3064 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3065 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3066
3067 if (main_index_contents.empty ())
3068 return 0;
3069
3063847f 3070 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3071 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3072 use_deprecated_index_sections,
3073 main_index_contents, map.get (), &cu_list,
3074 &cu_list_elements, &types_list,
3075 &types_list_elements))
2ec9a5e0
TT
3076 return 0;
3077
0fefef59 3078 /* Don't use the index if it's empty. */
3063847f 3079 if (map->symbol_table.empty ())
0fefef59
DE
3080 return 0;
3081
2ec9a5e0
TT
3082 /* If there is a .dwz file, read it so we can get its CU list as
3083 well. */
c3699833 3084 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3085 if (dwz != NULL)
2ec9a5e0 3086 {
2ec9a5e0
TT
3087 struct mapped_index dwz_map;
3088 const gdb_byte *dwz_types_ignore;
3089 offset_type dwz_types_elements_ignore;
3090
4485a1c1
SM
3091 gdb::array_view<const gdb_byte> dwz_index_content
3092 = get_gdb_index_contents_dwz (objfile, dwz);
3093
3094 if (dwz_index_content.empty ())
3095 return 0;
3096
3810f182 3097 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3098 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3099 &dwz_list, &dwz_list_elements,
3100 &dwz_types_ignore,
3101 &dwz_types_elements_ignore))
2ec9a5e0
TT
3102 {
3103 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3104 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3105 return 0;
3106 }
3107 }
3108
12359b5e
SM
3109 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3110 dwz_list, dwz_list_elements);
1fd400ff 3111
8b70b953
TT
3112 if (types_list_elements)
3113 {
8b70b953
TT
3114 /* We can only handle a single .debug_types when we have an
3115 index. */
5989a64e 3116 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3117 return 0;
3118
5989a64e 3119 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3120
12359b5e
SM
3121 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3122 types_list, types_list_elements);
8b70b953 3123 }
9291a0cd 3124
3063847f 3125 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3126
5989a64e
SM
3127 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3128 dwarf2_per_objfile->per_bfd->using_index = 1;
3129 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3130 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3131
3132 return 1;
3133}
3134
dee91e82 3135/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3136
dee91e82
DE
3137static void
3138dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3139 const gdb_byte *info_ptr,
3e225074 3140 struct die_info *comp_unit_die)
9291a0cd 3141{
dee91e82 3142 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3143 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3144 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3145 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3146 struct attribute *attr;
7b9f3c50
DE
3147 void **slot;
3148 struct quick_file_names *qfn;
9291a0cd 3149
0186c6a7
DE
3150 gdb_assert (! this_cu->is_debug_types);
3151
07261596
TT
3152 /* Our callers never want to match partial units -- instead they
3153 will match the enclosing full CU. */
3154 if (comp_unit_die->tag == DW_TAG_partial_unit)
3155 {
3156 this_cu->v.quick->no_file_data = 1;
3157 return;
3158 }
3159
0186c6a7 3160 lh_cu = this_cu;
7b9f3c50 3161 slot = NULL;
dee91e82 3162
fff8551c 3163 line_header_up lh;
9c541725 3164 sect_offset line_offset {};
fff8551c 3165
dee91e82 3166 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3167 if (attr != nullptr)
9291a0cd 3168 {
7b9f3c50
DE
3169 struct quick_file_names find_entry;
3170
9c541725 3171 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3172
3173 /* We may have already read in this line header (TU line header sharing).
3174 If we have we're done. */
094b34ac 3175 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3176 find_entry.hash.line_sect_off = line_offset;
5989a64e 3177 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3178 &find_entry, INSERT);
3179 if (*slot != NULL)
3180 {
9a3c8263 3181 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3182 return;
7b9f3c50
DE
3183 }
3184
3019eac3 3185 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3186 }
3187 if (lh == NULL)
3188 {
094b34ac 3189 lh_cu->v.quick->no_file_data = 1;
dee91e82 3190 return;
9291a0cd
TT
3191 }
3192
5989a64e 3193 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3194 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3195 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3196 gdb_assert (slot != NULL);
3197 *slot = qfn;
9291a0cd 3198
d721ba37 3199 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3200
aa391654
TT
3201 int offset = 0;
3202 if (strcmp (fnd.name, "<unknown>") != 0)
3203 ++offset;
3204
7ba99d21 3205 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3206 qfn->file_names =
5989a64e 3207 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3208 qfn->num_file_names);
aa391654
TT
3209 if (offset != 0)
3210 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3211 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3212 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3213 fnd.comp_dir).release ();
7b9f3c50 3214 qfn->real_names = NULL;
9291a0cd 3215
094b34ac 3216 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3217}
3218
3219/* A helper for the "quick" functions which attempts to read the line
3220 table for THIS_CU. */
3221
3222static struct quick_file_names *
ab432490
SM
3223dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3224 dwarf2_per_objfile *per_objfile)
dee91e82 3225{
0186c6a7
DE
3226 /* This should never be called for TUs. */
3227 gdb_assert (! this_cu->is_debug_types);
3228 /* Nor type unit groups. */
197400e8 3229 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3230
dee91e82
DE
3231 if (this_cu->v.quick->file_names != NULL)
3232 return this_cu->v.quick->file_names;
3233 /* If we know there is no line data, no point in looking again. */
3234 if (this_cu->v.quick->no_file_data)
3235 return NULL;
3236
ab432490 3237 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3238 if (!reader.dummy_p)
3e225074 3239 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3240
3241 if (this_cu->v.quick->no_file_data)
3242 return NULL;
3243 return this_cu->v.quick->file_names;
9291a0cd
TT
3244}
3245
3246/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3247 real path for a given file name from the line table. */
2fdf6df6 3248
9291a0cd 3249static const char *
45940949 3250dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3251 struct quick_file_names *qfn, int index)
9291a0cd 3252{
7b9f3c50 3253 if (qfn->real_names == NULL)
5989a64e 3254 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3255 qfn->num_file_names, const char *);
9291a0cd 3256
7b9f3c50 3257 if (qfn->real_names[index] == NULL)
14278e1f 3258 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3259
7b9f3c50 3260 return qfn->real_names[index];
9291a0cd
TT
3261}
3262
3263static struct symtab *
3264dw2_find_last_source_symtab (struct objfile *objfile)
3265{
ed2dc618
SM
3266 struct dwarf2_per_objfile *dwarf2_per_objfile
3267 = get_dwarf2_per_objfile (objfile);
5989a64e 3268 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3269 compunit_symtab *cust
3270 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3271
43f3e411
DE
3272 if (cust == NULL)
3273 return NULL;
ed2dc618 3274
43f3e411 3275 return compunit_primary_filetab (cust);
9291a0cd
TT
3276}
3277
7b9f3c50
DE
3278/* Traversal function for dw2_forget_cached_source_info. */
3279
3280static int
3281dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3282{
7b9f3c50 3283 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3284
7b9f3c50 3285 if (file_data->real_names)
9291a0cd 3286 {
7b9f3c50 3287 int i;
9291a0cd 3288
7b9f3c50 3289 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3290 {
7b9f3c50
DE
3291 xfree ((void*) file_data->real_names[i]);
3292 file_data->real_names[i] = NULL;
9291a0cd
TT
3293 }
3294 }
7b9f3c50
DE
3295
3296 return 1;
3297}
3298
3299static void
3300dw2_forget_cached_source_info (struct objfile *objfile)
3301{
ed2dc618
SM
3302 struct dwarf2_per_objfile *dwarf2_per_objfile
3303 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3304
5989a64e 3305 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3306 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3307}
3308
f8eba3c6
TT
3309/* Helper function for dw2_map_symtabs_matching_filename that expands
3310 the symtabs and calls the iterator. */
3311
3312static int
3313dw2_map_expand_apply (struct objfile *objfile,
3314 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3315 const char *name, const char *real_path,
14bc53a8 3316 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3317{
43f3e411 3318 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3319
3320 /* Don't visit already-expanded CUs. */
af758d11
SM
3321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3322 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3323 return 0;
3324
3325 /* This may expand more than one symtab, and we want to iterate over
3326 all of them. */
97a1449a 3327 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3328
14bc53a8
PA
3329 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3330 last_made, callback);
f8eba3c6
TT
3331}
3332
3333/* Implementation of the map_symtabs_matching_filename method. */
3334
14bc53a8
PA
3335static bool
3336dw2_map_symtabs_matching_filename
3337 (struct objfile *objfile, const char *name, const char *real_path,
3338 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3339{
c011a4f4 3340 const char *name_basename = lbasename (name);
ed2dc618
SM
3341 struct dwarf2_per_objfile *dwarf2_per_objfile
3342 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3343
848e3e78
DE
3344 /* The rule is CUs specify all the files, including those used by
3345 any TU, so there's no need to scan TUs here. */
f4dc4d17 3346
5989a64e 3347 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3348 {
3d7bb9d9 3349 /* We only need to look at symtabs not already expanded. */
af758d11 3350 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3351 continue;
3352
ab432490
SM
3353 quick_file_names *file_data
3354 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3355 if (file_data == NULL)
9291a0cd
TT
3356 continue;
3357
b76e467d 3358 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3359 {
7b9f3c50 3360 const char *this_name = file_data->file_names[j];
da235a7c 3361 const char *this_real_name;
9291a0cd 3362
af529f8f 3363 if (compare_filenames_for_search (this_name, name))
9291a0cd 3364 {
f5b95b50 3365 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3366 callback))
3367 return true;
288e77a7 3368 continue;
4aac40c8 3369 }
9291a0cd 3370
c011a4f4
DE
3371 /* Before we invoke realpath, which can get expensive when many
3372 files are involved, do a quick comparison of the basenames. */
3373 if (! basenames_may_differ
3374 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3375 continue;
3376
45940949
TT
3377 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3378 file_data, j);
da235a7c 3379 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3380 {
da235a7c 3381 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3382 callback))
3383 return true;
288e77a7 3384 continue;
da235a7c 3385 }
9291a0cd 3386
da235a7c
JK
3387 if (real_path != NULL)
3388 {
af529f8f
JK
3389 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3390 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3391 if (this_real_name != NULL
af529f8f 3392 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3393 {
f5b95b50 3394 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3395 callback))
3396 return true;
288e77a7 3397 continue;
9291a0cd
TT
3398 }
3399 }
3400 }
3401 }
3402
14bc53a8 3403 return false;
9291a0cd
TT
3404}
3405
da51c347
DE
3406/* Struct used to manage iterating over all CUs looking for a symbol. */
3407
3408struct dw2_symtab_iterator
9291a0cd 3409{
ed2dc618
SM
3410 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3411 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3412 /* If set, only look for symbols that match that block. Valid values are
3413 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3414 gdb::optional<block_enum> block_index;
da51c347
DE
3415 /* The kind of symbol we're looking for. */
3416 domain_enum domain;
3417 /* The list of CUs from the index entry of the symbol,
3418 or NULL if not found. */
3419 offset_type *vec;
3420 /* The next element in VEC to look at. */
3421 int next;
3422 /* The number of elements in VEC, or zero if there is no match. */
3423 int length;
8943b874
DE
3424 /* Have we seen a global version of the symbol?
3425 If so we can ignore all further global instances.
3426 This is to work around gold/15646, inefficient gold-generated
3427 indices. */
3428 int global_seen;
da51c347 3429};
9291a0cd 3430
2b79f376 3431/* Initialize the index symtab iterator ITER. */
2fdf6df6 3432
9291a0cd 3433static void
da51c347 3434dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3435 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3436 gdb::optional<block_enum> block_index,
da51c347
DE
3437 domain_enum domain,
3438 const char *name)
3439{
ed2dc618 3440 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3441 iter->block_index = block_index;
3442 iter->domain = domain;
3443 iter->next = 0;
8943b874 3444 iter->global_seen = 0;
da51c347 3445
5989a64e 3446 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3447
3448 /* index is NULL if OBJF_READNOW. */
3449 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3450 iter->length = MAYBE_SWAP (*iter->vec);
3451 else
3452 {
3453 iter->vec = NULL;
3454 iter->length = 0;
3455 }
3456}
3457
3458/* Return the next matching CU or NULL if there are no more. */
3459
3460static struct dwarf2_per_cu_data *
3461dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3462{
ed2dc618
SM
3463 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3464
da51c347
DE
3465 for ( ; iter->next < iter->length; ++iter->next)
3466 {
3467 offset_type cu_index_and_attrs =
3468 MAYBE_SWAP (iter->vec[iter->next + 1]);
3469 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3470 gdb_index_symbol_kind symbol_kind =
3471 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3472 /* Only check the symbol attributes if they're present.
3473 Indices prior to version 7 don't record them,
3474 and indices >= 7 may elide them for certain symbols
3475 (gold does this). */
3476 int attrs_valid =
5989a64e 3477 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3478 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3479
3190f0c6 3480 /* Don't crash on bad data. */
5989a64e
SM
3481 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3482 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3483 {
b98664d3 3484 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3485 " [in module %s]"),
3486 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3487 continue;
3488 }
3489
5989a64e 3490 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3491
da51c347 3492 /* Skip if already read in. */
af758d11 3493 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3494 continue;
3495
8943b874
DE
3496 /* Check static vs global. */
3497 if (attrs_valid)
3498 {
2b79f376
SM
3499 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3500
3501 if (iter->block_index.has_value ())
3502 {
3503 bool want_static = *iter->block_index == STATIC_BLOCK;
3504
3505 if (is_static != want_static)
3506 continue;
3507 }
3508
8943b874
DE
3509 /* Work around gold/15646. */
3510 if (!is_static && iter->global_seen)
3511 continue;
3512 if (!is_static)
3513 iter->global_seen = 1;
3514 }
da51c347
DE
3515
3516 /* Only check the symbol's kind if it has one. */
3517 if (attrs_valid)
3518 {
3519 switch (iter->domain)
3520 {
3521 case VAR_DOMAIN:
3522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3523 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3524 /* Some types are also in VAR_DOMAIN. */
3525 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3526 continue;
3527 break;
3528 case STRUCT_DOMAIN:
3529 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3530 continue;
3531 break;
3532 case LABEL_DOMAIN:
3533 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3534 continue;
3535 break;
59c35742
AB
3536 case MODULE_DOMAIN:
3537 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3538 continue;
3539 break;
da51c347
DE
3540 default:
3541 break;
3542 }
3543 }
3544
3545 ++iter->next;
3546 return per_cu;
3547 }
3548
3549 return NULL;
3550}
3551
43f3e411 3552static struct compunit_symtab *
c7f839cb 3553dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3554 const char *name, domain_enum domain)
9291a0cd 3555{
43f3e411 3556 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3557 struct dwarf2_per_objfile *dwarf2_per_objfile
3558 = get_dwarf2_per_objfile (objfile);
9291a0cd 3559
b5ec771e
PA
3560 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3561
ed2dc618
SM
3562 struct dw2_symtab_iterator iter;
3563 struct dwarf2_per_cu_data *per_cu;
da51c347 3564
2b79f376 3565 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3566
ed2dc618
SM
3567 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3568 {
3569 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3570 struct compunit_symtab *stab
3571 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3572 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3573 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3574
ed2dc618
SM
3575 sym = block_find_symbol (block, name, domain,
3576 block_find_non_opaque_type_preferred,
3577 &with_opaque);
b2e2f908 3578
ed2dc618
SM
3579 /* Some caution must be observed with overloaded functions
3580 and methods, since the index will not contain any overload
3581 information (but NAME might contain it). */
da51c347 3582
ed2dc618
SM
3583 if (sym != NULL
3584 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3585 return stab;
3586 if (with_opaque != NULL
3587 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3588 stab_best = stab;
da51c347 3589
ed2dc618 3590 /* Keep looking through other CUs. */
9291a0cd 3591 }
9291a0cd 3592
da51c347 3593 return stab_best;
9291a0cd
TT
3594}
3595
3596static void
3597dw2_print_stats (struct objfile *objfile)
3598{
ed2dc618
SM
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3601 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3602 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3603 int count = 0;
9291a0cd 3604
ed2dc618 3605 for (int i = 0; i < total; ++i)
9291a0cd 3606 {
5989a64e 3607 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3608
af758d11 3609 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3610 ++count;
3611 }
e4a48d9d 3612 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3613 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3614}
3615
779bd270
DE
3616/* This dumps minimal information about the index.
3617 It is called via "mt print objfiles".
3618 One use is to verify .gdb_index has been loaded by the
3619 gdb.dwarf2/gdb-index.exp testcase. */
3620
9291a0cd
TT
3621static void
3622dw2_dump (struct objfile *objfile)
3623{
ed2dc618
SM
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
3626
5989a64e 3627 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3628 printf_filtered (".gdb_index:");
5989a64e 3629 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3630 {
3631 printf_filtered (" version %d\n",
5989a64e 3632 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3633 }
3634 else
3635 printf_filtered (" faked for \"readnow\"\n");
3636 printf_filtered ("\n");
9291a0cd
TT
3637}
3638
9291a0cd
TT
3639static void
3640dw2_expand_symtabs_for_function (struct objfile *objfile,
3641 const char *func_name)
3642{
ed2dc618
SM
3643 struct dwarf2_per_objfile *dwarf2_per_objfile
3644 = get_dwarf2_per_objfile (objfile);
da51c347 3645
ed2dc618
SM
3646 struct dw2_symtab_iterator iter;
3647 struct dwarf2_per_cu_data *per_cu;
da51c347 3648
2b79f376 3649 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3650
ed2dc618 3651 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3652 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3653
9291a0cd
TT
3654}
3655
3656static void
3657dw2_expand_all_symtabs (struct objfile *objfile)
3658{
ed2dc618
SM
3659 struct dwarf2_per_objfile *dwarf2_per_objfile
3660 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3661 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3662 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3663
ed2dc618 3664 for (int i = 0; i < total_units; ++i)
9291a0cd 3665 {
5989a64e 3666 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3667
58f0c718
TT
3668 /* We don't want to directly expand a partial CU, because if we
3669 read it with the wrong language, then assertion failures can
3670 be triggered later on. See PR symtab/23010. So, tell
3671 dw2_instantiate_symtab to skip partial CUs -- any important
3672 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3673 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3674 }
3675}
3676
3677static void
652a8996
JK
3678dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3679 const char *fullname)
9291a0cd 3680{
ed2dc618
SM
3681 struct dwarf2_per_objfile *dwarf2_per_objfile
3682 = 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
5989a64e 3689 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3690 {
3d7bb9d9 3691 /* We only need to look at symtabs not already expanded. */
af758d11 3692 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3693 continue;
3694
ab432490
SM
3695 quick_file_names *file_data
3696 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3697 if (file_data == NULL)
9291a0cd
TT
3698 continue;
3699
b76e467d 3700 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3701 {
652a8996
JK
3702 const char *this_fullname = file_data->file_names[j];
3703
3704 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3705 {
97a1449a 3706 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3707 break;
3708 }
3709 }
3710 }
3711}
3712
9a0bacfb
TV
3713static void
3714dw2_expand_symtabs_matching_symbol
3715 (mapped_index_base &index,
3716 const lookup_name_info &lookup_name_in,
3717 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3718 enum search_domain kind,
3719 gdb::function_view<bool (offset_type)> match_callback);
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
TV
3736 /* Used for Ada. */
3737 struct dwarf2_per_objfile *dwarf2_per_objfile
3738 = get_dwarf2_per_objfile (objfile);
3739
9a0bacfb
TV
3740 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3741
5989a64e 3742 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3743 {
3744 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3745 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3746 using GNU index. */
5989a64e 3747 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3748
9a0bacfb
TV
3749 const char *match_name = name.ada ().lookup_name ().c_str ();
3750 auto matcher = [&] (const char *symname)
3751 {
3752 if (ordered_compare == nullptr)
3753 return true;
3754 return ordered_compare (symname, match_name) == 0;
3755 };
3756
3757 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3758 [&] (offset_type namei)
3759 {
3760 struct dw2_symtab_iterator iter;
3761 struct dwarf2_per_cu_data *per_cu;
3762
3763 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3764 match_name);
3765 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3766 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3767 nullptr);
9a0bacfb
TV
3768 return true;
3769 });
3770 }
3771 else
3772 {
3773 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3774 proceed assuming all symtabs have been read in. */
3775 }
1aa98955
TV
3776
3777 for (compunit_symtab *cust : objfile->compunits ())
3778 {
3779 const struct block *block;
3780
3781 if (cust == NULL)
3782 continue;
3783 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3784 if (!iterate_over_symbols_terminated (block, name,
3785 domain, callback))
3786 return;
3787 }
9291a0cd
TT
3788}
3789
e1ef7d7a
PA
3790/* Starting from a search name, return the string that finds the upper
3791 bound of all strings that start with SEARCH_NAME in a sorted name
3792 list. Returns the empty string to indicate that the upper bound is
3793 the end of the list. */
3794
3795static std::string
3796make_sort_after_prefix_name (const char *search_name)
3797{
3798 /* When looking to complete "func", we find the upper bound of all
3799 symbols that start with "func" by looking for where we'd insert
3800 the closest string that would follow "func" in lexicographical
3801 order. Usually, that's "func"-with-last-character-incremented,
3802 i.e. "fund". Mind non-ASCII characters, though. Usually those
3803 will be UTF-8 multi-byte sequences, but we can't be certain.
3804 Especially mind the 0xff character, which is a valid character in
3805 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3806 rule out compilers allowing it in identifiers. Note that
3807 conveniently, strcmp/strcasecmp are specified to compare
3808 characters interpreted as unsigned char. So what we do is treat
3809 the whole string as a base 256 number composed of a sequence of
3810 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3811 to 0, and carries 1 to the following more-significant position.
3812 If the very first character in SEARCH_NAME ends up incremented
3813 and carries/overflows, then the upper bound is the end of the
3814 list. The string after the empty string is also the empty
3815 string.
3816
3817 Some examples of this operation:
3818
3819 SEARCH_NAME => "+1" RESULT
3820
3821 "abc" => "abd"
3822 "ab\xff" => "ac"
3823 "\xff" "a" "\xff" => "\xff" "b"
3824 "\xff" => ""
3825 "\xff\xff" => ""
3826 "" => ""
3827
3828 Then, with these symbols for example:
3829
3830 func
3831 func1
3832 fund
3833
3834 completing "func" looks for symbols between "func" and
3835 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3836 which finds "func" and "func1", but not "fund".
3837
3838 And with:
3839
3840 funcÿ (Latin1 'ÿ' [0xff])
3841 funcÿ1
3842 fund
3843
3844 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3845 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3846
3847 And with:
3848
3849 ÿÿ (Latin1 'ÿ' [0xff])
3850 ÿÿ1
3851
3852 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3853 the end of the list.
3854 */
3855 std::string after = search_name;
3856 while (!after.empty () && (unsigned char) after.back () == 0xff)
3857 after.pop_back ();
3858 if (!after.empty ())
3859 after.back () = (unsigned char) after.back () + 1;
3860 return after;
3861}
3862
5c58de74 3863/* See declaration. */
61d96d7e 3864
5c58de74
PA
3865std::pair<std::vector<name_component>::const_iterator,
3866 std::vector<name_component>::const_iterator>
44ed8f3e 3867mapped_index_base::find_name_components_bounds
3b00ef10 3868 (const lookup_name_info &lookup_name_without_params, language lang) 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 {
5c58de74 3881 const char *elem_qualified = this->symbol_name_at (elem.idx);
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 {
5c58de74 3891 const char *elem_qualified = this->symbol_name_at (elem.idx);
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
44ed8f3e 3940mapped_index_base::build_name_components ()
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
3958 const char *name = this->symbol_name_at (idx);
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 {
3996 const char *left_qualified = this->symbol_name_at (left.idx);
3997 const char *right_qualified = this->symbol_name_at (right.idx);
3998
3999 const char *left_name = left_qualified + left.name_offset;
4000 const char *right_name = right_qualified + right.name_offset;
4001
4002 return name_cmp (left_name, right_name) < 0;
4003 };
4004
4005 std::sort (this->name_components.begin (),
4006 this->name_components.end (),
4007 name_comp_compare);
4008}
4009
4010/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4011 mapped_index_base instead of the containing objfile. This is split
4012 to a separate function in order to be able to unit test the
4013 name_components matching using a mock mapped_index_base. For each
5c58de74 4014 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4015 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4016
4017static void
4018dw2_expand_symtabs_matching_symbol
44ed8f3e 4019 (mapped_index_base &index,
5c58de74
PA
4020 const lookup_name_info &lookup_name_in,
4021 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4022 enum search_domain kind,
3b00ef10 4023 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4024{
4025 lookup_name_info lookup_name_without_params
4026 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4027
4028 /* Build the symbol name component sorted vector, if we haven't
4029 yet. */
4030 index.build_name_components ();
4031
3f563c84
PA
4032 /* The same symbol may appear more than once in the range though.
4033 E.g., if we're looking for symbols that complete "w", and we have
4034 a symbol named "w1::w2", we'll find the two name components for
4035 that same symbol in the range. To be sure we only call the
4036 callback once per symbol, we first collect the symbol name
4037 indexes that matched in a temporary vector and ignore
4038 duplicates. */
4039 std::vector<offset_type> matches;
3f563c84 4040
3b00ef10
TT
4041 struct name_and_matcher
4042 {
4043 symbol_name_matcher_ftype *matcher;
ecc6c606 4044 const char *name;
3b00ef10
TT
4045
4046 bool operator== (const name_and_matcher &other) const
3f563c84 4047 {
ecc6c606 4048 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4049 }
4050 };
4051
4052 /* A vector holding all the different symbol name matchers, for all
4053 languages. */
4054 std::vector<name_and_matcher> matchers;
4055
4056 for (int i = 0; i < nr_languages; i++)
4057 {
4058 enum language lang_e = (enum language) i;
4059
4060 const language_defn *lang = language_def (lang_e);
4061 symbol_name_matcher_ftype *name_matcher
4062 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4063
3b00ef10
TT
4064 name_and_matcher key {
4065 name_matcher,
4066 lookup_name_without_params.language_lookup_name (lang_e)
4067 };
4068
4069 /* Don't insert the same comparison routine more than once.
4070 Note that we do this linear walk. This is not a problem in
4071 practice because the number of supported languages is
4072 low. */
4073 if (std::find (matchers.begin (), matchers.end (), key)
4074 != matchers.end ())
9291a0cd 4075 continue;
3b00ef10
TT
4076 matchers.push_back (std::move (key));
4077
4078 auto bounds
4079 = index.find_name_components_bounds (lookup_name_without_params,
4080 lang_e);
4081
4082 /* Now for each symbol name in range, check to see if we have a name
4083 match, and if so, call the MATCH_CALLBACK callback. */
4084
4085 for (; bounds.first != bounds.second; ++bounds.first)
4086 {
4087 const char *qualified = index.symbol_name_at (bounds.first->idx);
4088
4089 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4090 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4091 continue;
9291a0cd 4092
3b00ef10
TT
4093 matches.push_back (bounds.first->idx);
4094 }
3f563c84
PA
4095 }
4096
4097 std::sort (matches.begin (), matches.end ());
4098
4099 /* Finally call the callback, once per match. */
4100 ULONGEST prev = -1;
4101 for (offset_type idx : matches)
4102 {
4103 if (prev != idx)
4104 {
3b00ef10
TT
4105 if (!match_callback (idx))
4106 break;
3f563c84
PA
4107 prev = idx;
4108 }
4109 }
4110
4111 /* Above we use a type wider than idx's for 'prev', since 0 and
4112 (offset_type)-1 are both possible values. */
4113 static_assert (sizeof (prev) > sizeof (offset_type), "");
4114}
4115
c62446b1
PA
4116#if GDB_SELF_TEST
4117
4118namespace selftests { namespace dw2_expand_symtabs_matching {
4119
a3c5fafd
PA
4120/* A mock .gdb_index/.debug_names-like name index table, enough to
4121 exercise dw2_expand_symtabs_matching_symbol, which works with the
4122 mapped_index_base interface. Builds an index from the symbol list
4123 passed as parameter to the constructor. */
4124class mock_mapped_index : public mapped_index_base
c62446b1
PA
4125{
4126public:
a3c5fafd
PA
4127 mock_mapped_index (gdb::array_view<const char *> symbols)
4128 : m_symbol_table (symbols)
c62446b1
PA
4129 {}
4130
a3c5fafd 4131 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4132
a3c5fafd 4133 /* Return the number of names in the symbol table. */
632e107b 4134 size_t symbol_name_count () const override
c62446b1 4135 {
a3c5fafd 4136 return m_symbol_table.size ();
c62446b1
PA
4137 }
4138
a3c5fafd 4139 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4140 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4141 {
4142 return m_symbol_table[idx];
4143 }
c62446b1 4144
a3c5fafd
PA
4145private:
4146 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4147};
4148
4149/* Convenience function that converts a NULL pointer to a "<null>"
4150 string, to pass to print routines. */
4151
4152static const char *
4153string_or_null (const char *str)
4154{
4155 return str != NULL ? str : "<null>";
4156}
4157
4158/* Check if a lookup_name_info built from
4159 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4160 index. EXPECTED_LIST is the list of expected matches, in expected
4161 matching order. If no match expected, then an empty list is
4162 specified. Returns true on success. On failure prints a warning
4163 indicating the file:line that failed, and returns false. */
4164
4165static bool
4166check_match (const char *file, int line,
4167 mock_mapped_index &mock_index,
4168 const char *name, symbol_name_match_type match_type,
4169 bool completion_mode,
4170 std::initializer_list<const char *> expected_list)
4171{
4172 lookup_name_info lookup_name (name, match_type, completion_mode);
4173
4174 bool matched = true;
4175
4176 auto mismatch = [&] (const char *expected_str,
4177 const char *got)
4178 {
4179 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4180 "expected=\"%s\", got=\"%s\"\n"),
4181 file, line,
4182 (match_type == symbol_name_match_type::FULL
4183 ? "FULL" : "WILD"),
4184 name, string_or_null (expected_str), string_or_null (got));
4185 matched = false;
4186 };
4187
4188 auto expected_it = expected_list.begin ();
4189 auto expected_end = expected_list.end ();
4190
a3c5fafd 4191 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4192 NULL, ALL_DOMAIN,
4193 [&] (offset_type idx)
4194 {
a3c5fafd 4195 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4196 const char *expected_str
4197 = expected_it == expected_end ? NULL : *expected_it++;
4198
4199 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4200 mismatch (expected_str, matched_name);
3b00ef10 4201 return true;
c62446b1
PA
4202 });
4203
4204 const char *expected_str
4205 = expected_it == expected_end ? NULL : *expected_it++;
4206 if (expected_str != NULL)
4207 mismatch (expected_str, NULL);
4208
4209 return matched;
4210}
4211
4212/* The symbols added to the mock mapped_index for testing (in
4213 canonical form). */
4214static const char *test_symbols[] = {
4215 "function",
4216 "std::bar",
4217 "std::zfunction",
4218 "std::zfunction2",
4219 "w1::w2",
4220 "ns::foo<char*>",
4221 "ns::foo<int>",
4222 "ns::foo<long>",
a20714ff
PA
4223 "ns2::tmpl<int>::foo2",
4224 "(anonymous namespace)::A::B::C",
c62446b1 4225
e1ef7d7a
PA
4226 /* These are used to check that the increment-last-char in the
4227 matching algorithm for completion doesn't match "t1_fund" when
4228 completing "t1_func". */
4229 "t1_func",
4230 "t1_func1",
4231 "t1_fund",
4232 "t1_fund1",
4233
4234 /* A UTF-8 name with multi-byte sequences to make sure that
4235 cp-name-parser understands this as a single identifier ("função"
4236 is "function" in PT). */
4237 u8"u8função",
4238
4239 /* \377 (0xff) is Latin1 'ÿ'. */
4240 "yfunc\377",
4241
4242 /* \377 (0xff) is Latin1 'ÿ'. */
4243 "\377",
4244 "\377\377123",
4245
c62446b1
PA
4246 /* A name with all sorts of complications. Starts with "z" to make
4247 it easier for the completion tests below. */
4248#define Z_SYM_NAME \
4249 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4250 "::tuple<(anonymous namespace)::ui*, " \
4251 "std::default_delete<(anonymous namespace)::ui>, void>"
4252
4253 Z_SYM_NAME
4254};
4255
a3c5fafd
PA
4256/* Returns true if the mapped_index_base::find_name_component_bounds
4257 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4258 in completion mode. */
5c58de74
PA
4259
4260static bool
a3c5fafd 4261check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4262 const char *search_name,
4263 gdb::array_view<const char *> expected_syms)
4264{
4265 lookup_name_info lookup_name (search_name,
4266 symbol_name_match_type::FULL, true);
4267
3b00ef10
TT
4268 auto bounds = index.find_name_components_bounds (lookup_name,
4269 language_cplus);
5c58de74
PA
4270
4271 size_t distance = std::distance (bounds.first, bounds.second);
4272 if (distance != expected_syms.size ())
4273 return false;
4274
4275 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4276 {
4277 auto nc_elem = bounds.first + exp_elem;
4278 const char *qualified = index.symbol_name_at (nc_elem->idx);
4279 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4280 return false;
4281 }
4282
4283 return true;
4284}
4285
4286/* Test the lower-level mapped_index::find_name_component_bounds
4287 method. */
4288
c62446b1 4289static void
5c58de74
PA
4290test_mapped_index_find_name_component_bounds ()
4291{
4292 mock_mapped_index mock_index (test_symbols);
4293
a3c5fafd 4294 mock_index.build_name_components ();
5c58de74
PA
4295
4296 /* Test the lower-level mapped_index::find_name_component_bounds
4297 method in completion mode. */
4298 {
4299 static const char *expected_syms[] = {
4300 "t1_func",
4301 "t1_func1",
5c58de74
PA
4302 };
4303
a3c5fafd 4304 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4305 "t1_func", expected_syms));
4306 }
4307
4308 /* Check that the increment-last-char in the name matching algorithm
4309 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4310 {
4311 static const char *expected_syms1[] = {
4312 "\377",
4313 "\377\377123",
4314 };
a3c5fafd 4315 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4316 "\377", expected_syms1));
4317
4318 static const char *expected_syms2[] = {
4319 "\377\377123",
4320 };
a3c5fafd 4321 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4322 "\377\377", expected_syms2));
4323 }
4324}
4325
4326/* Test dw2_expand_symtabs_matching_symbol. */
4327
4328static void
4329test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4330{
4331 mock_mapped_index mock_index (test_symbols);
4332
4333 /* We let all tests run until the end even if some fails, for debug
4334 convenience. */
4335 bool any_mismatch = false;
4336
4337 /* Create the expected symbols list (an initializer_list). Needed
4338 because lists have commas, and we need to pass them to CHECK,
4339 which is a macro. */
4340#define EXPECT(...) { __VA_ARGS__ }
4341
4342 /* Wrapper for check_match that passes down the current
4343 __FILE__/__LINE__. */
4344#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4345 any_mismatch |= !check_match (__FILE__, __LINE__, \
4346 mock_index, \
4347 NAME, MATCH_TYPE, COMPLETION_MODE, \
4348 EXPECTED_LIST)
4349
4350 /* Identity checks. */
4351 for (const char *sym : test_symbols)
4352 {
4353 /* Should be able to match all existing symbols. */
4354 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4355 EXPECT (sym));
4356
4357 /* Should be able to match all existing symbols with
4358 parameters. */
4359 std::string with_params = std::string (sym) + "(int)";
4360 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4361 EXPECT (sym));
4362
4363 /* Should be able to match all existing symbols with
4364 parameters and qualifiers. */
4365 with_params = std::string (sym) + " ( int ) const";
4366 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4367 EXPECT (sym));
4368
4369 /* This should really find sym, but cp-name-parser.y doesn't
4370 know about lvalue/rvalue qualifiers yet. */
4371 with_params = std::string (sym) + " ( int ) &&";
4372 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4373 {});
4374 }
4375
e1ef7d7a
PA
4376 /* Check that the name matching algorithm for completion doesn't get
4377 confused with Latin1 'ÿ' / 0xff. */
4378 {
4379 static const char str[] = "\377";
4380 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4381 EXPECT ("\377", "\377\377123"));
4382 }
4383
4384 /* Check that the increment-last-char in the matching algorithm for
4385 completion doesn't match "t1_fund" when completing "t1_func". */
4386 {
4387 static const char str[] = "t1_func";
4388 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4389 EXPECT ("t1_func", "t1_func1"));
4390 }
4391
c62446b1
PA
4392 /* Check that completion mode works at each prefix of the expected
4393 symbol name. */
4394 {
4395 static const char str[] = "function(int)";
4396 size_t len = strlen (str);
4397 std::string lookup;
4398
4399 for (size_t i = 1; i < len; i++)
4400 {
4401 lookup.assign (str, i);
4402 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4403 EXPECT ("function"));
4404 }
4405 }
4406
4407 /* While "w" is a prefix of both components, the match function
4408 should still only be called once. */
4409 {
4410 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4411 EXPECT ("w1::w2"));
a20714ff
PA
4412 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4413 EXPECT ("w1::w2"));
c62446b1
PA
4414 }
4415
4416 /* Same, with a "complicated" symbol. */
4417 {
4418 static const char str[] = Z_SYM_NAME;
4419 size_t len = strlen (str);
4420 std::string lookup;
4421
4422 for (size_t i = 1; i < len; i++)
4423 {
4424 lookup.assign (str, i);
4425 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4426 EXPECT (Z_SYM_NAME));
4427 }
4428 }
4429
4430 /* In FULL mode, an incomplete symbol doesn't match. */
4431 {
4432 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4433 {});
4434 }
4435
4436 /* A complete symbol with parameters matches any overload, since the
4437 index has no overload info. */
4438 {
4439 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4440 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4441 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4442 EXPECT ("std::zfunction", "std::zfunction2"));
4443 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4444 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4445 }
4446
4447 /* Check that whitespace is ignored appropriately. A symbol with a
4448 template argument list. */
4449 {
4450 static const char expected[] = "ns::foo<int>";
4451 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4452 EXPECT (expected));
a20714ff
PA
4453 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4454 EXPECT (expected));
c62446b1
PA
4455 }
4456
4457 /* Check that whitespace is ignored appropriately. A symbol with a
4458 template argument list that includes a pointer. */
4459 {
4460 static const char expected[] = "ns::foo<char*>";
4461 /* Try both completion and non-completion modes. */
4462 static const bool completion_mode[2] = {false, true};
4463 for (size_t i = 0; i < 2; i++)
4464 {
4465 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4466 completion_mode[i], EXPECT (expected));
a20714ff
PA
4467 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4468 completion_mode[i], EXPECT (expected));
c62446b1
PA
4469
4470 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4471 completion_mode[i], EXPECT (expected));
a20714ff
PA
4472 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4473 completion_mode[i], EXPECT (expected));
c62446b1
PA
4474 }
4475 }
4476
4477 {
4478 /* Check method qualifiers are ignored. */
4479 static const char expected[] = "ns::foo<char*>";
4480 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4481 symbol_name_match_type::FULL, true, EXPECT (expected));
4482 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4483 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4484 CHECK_MATCH ("foo < char * > ( int ) const",
4485 symbol_name_match_type::WILD, true, EXPECT (expected));
4486 CHECK_MATCH ("foo < char * > ( int ) &&",
4487 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4488 }
4489
4490 /* Test lookup names that don't match anything. */
4491 {
a20714ff
PA
4492 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4493 {});
4494
c62446b1
PA
4495 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4496 {});
4497 }
4498
a20714ff
PA
4499 /* Some wild matching tests, exercising "(anonymous namespace)",
4500 which should not be confused with a parameter list. */
4501 {
4502 static const char *syms[] = {
4503 "A::B::C",
4504 "B::C",
4505 "C",
4506 "A :: B :: C ( int )",
4507 "B :: C ( int )",
4508 "C ( int )",
4509 };
4510
4511 for (const char *s : syms)
4512 {
4513 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4514 EXPECT ("(anonymous namespace)::A::B::C"));
4515 }
4516 }
4517
4518 {
4519 static const char expected[] = "ns2::tmpl<int>::foo2";
4520 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4521 EXPECT (expected));
4522 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4523 EXPECT (expected));
4524 }
4525
c62446b1
PA
4526 SELF_CHECK (!any_mismatch);
4527
4528#undef EXPECT
4529#undef CHECK_MATCH
4530}
4531
5c58de74
PA
4532static void
4533run_test ()
4534{
4535 test_mapped_index_find_name_component_bounds ();
4536 test_dw2_expand_symtabs_matching_symbol ();
4537}
4538
c62446b1
PA
4539}} // namespace selftests::dw2_expand_symtabs_matching
4540
4541#endif /* GDB_SELF_TEST */
4542
4b514bc8
JK
4543/* If FILE_MATCHER is NULL or if PER_CU has
4544 dwarf2_per_cu_quick_data::MARK set (see
4545 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4546 EXPANSION_NOTIFY on it. */
4547
4548static void
4549dw2_expand_symtabs_matching_one
97a1449a
SM
4550 (dwarf2_per_cu_data *per_cu,
4551 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4554{
4555 if (file_matcher == NULL || per_cu->v.quick->mark)
4556 {
af758d11 4557 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4558
97a1449a
SM
4559 compunit_symtab *symtab
4560 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4561 gdb_assert (symtab != nullptr);
4b514bc8 4562
af758d11
SM
4563 if (expansion_notify != NULL && symtab_was_null)
4564 expansion_notify (symtab);
4b514bc8
JK
4565 }
4566}
4567
3f563c84
PA
4568/* Helper for dw2_expand_matching symtabs. Called on each symbol
4569 matched, to expand corresponding CUs that were marked. IDX is the
4570 index of the symbol name that matched. */
4571
4572static void
4573dw2_expand_marked_cus
97a1449a 4574 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4575 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4576 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4577 search_domain kind)
4578{
3f563c84
PA
4579 offset_type *vec, vec_len, vec_idx;
4580 bool global_seen = false;
5989a64e 4581 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4582
61920122 4583 vec = (offset_type *) (index.constant_pool
f00a2de2 4584 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4585 vec_len = MAYBE_SWAP (vec[0]);
4586 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4587 {
61920122
PA
4588 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4589 /* This value is only valid for index versions >= 7. */
4590 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4591 gdb_index_symbol_kind symbol_kind =
4592 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4593 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4594 /* Only check the symbol attributes if they're present.
4595 Indices prior to version 7 don't record them,
4596 and indices >= 7 may elide them for certain symbols
4597 (gold does this). */
4598 int attrs_valid =
4599 (index.version >= 7
4600 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4601
4602 /* Work around gold/15646. */
4603 if (attrs_valid)
9291a0cd 4604 {
61920122
PA
4605 if (!is_static && global_seen)
4606 continue;
4607 if (!is_static)
4608 global_seen = true;
4609 }
3190f0c6 4610
61920122
PA
4611 /* Only check the symbol's kind if it has one. */
4612 if (attrs_valid)
4613 {
4614 switch (kind)
8943b874 4615 {
61920122
PA
4616 case VARIABLES_DOMAIN:
4617 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4618 continue;
4619 break;
4620 case FUNCTIONS_DOMAIN:
4621 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4622 continue;
61920122
PA
4623 break;
4624 case TYPES_DOMAIN:
4625 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4626 continue;
4627 break;
59c35742
AB
4628 case MODULES_DOMAIN:
4629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4630 continue;
4631 break;
61920122
PA
4632 default:
4633 break;
8943b874 4634 }
61920122 4635 }
8943b874 4636
61920122 4637 /* Don't crash on bad data. */
5989a64e
SM
4638 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4639 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4640 {
b98664d3 4641 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4642 " [in module %s]"),
4643 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4644 continue;
4645 }
4646
5989a64e 4647 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4648 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4649 expansion_notify);
61920122
PA
4650 }
4651}
4652
4b514bc8
JK
4653/* If FILE_MATCHER is non-NULL, set all the
4654 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4655 that match FILE_MATCHER. */
4656
61920122 4657static void
4b514bc8 4658dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4659 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4660 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4661{
4b514bc8 4662 if (file_matcher == NULL)
61920122
PA
4663 return;
4664
4b514bc8
JK
4665 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4666 htab_eq_pointer,
4667 NULL, xcalloc, xfree));
4668 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4669 htab_eq_pointer,
4670 NULL, xcalloc, xfree));
61920122 4671
4b514bc8
JK
4672 /* The rule is CUs specify all the files, including those used by
4673 any TU, so there's no need to scan TUs here. */
61920122 4674
5989a64e 4675 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4676 {
927aa2e7
JK
4677 QUIT;
4678
4679 per_cu->v.quick->mark = 0;
4680
4681 /* We only need to look at symtabs not already expanded. */
af758d11 4682 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4683 continue;
4684
ab432490
SM
4685 quick_file_names *file_data
4686 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4687 if (file_data == NULL)
4688 continue;
4689
4690 if (htab_find (visited_not_found.get (), file_data) != NULL)
4691 continue;
4692 else if (htab_find (visited_found.get (), file_data) != NULL)
4693 {
4694 per_cu->v.quick->mark = 1;
4695 continue;
4696 }
4697
b76e467d 4698 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4699 {
4700 const char *this_real_name;
4701
4702 if (file_matcher (file_data->file_names[j], false))
4703 {
4704 per_cu->v.quick->mark = 1;
4705 break;
4706 }
4707
4708 /* Before we invoke realpath, which can get expensive when many
4709 files are involved, do a quick comparison of the basenames. */
4710 if (!basenames_may_differ
4711 && !file_matcher (lbasename (file_data->file_names[j]),
4712 true))
4713 continue;
4714
45940949
TT
4715 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4716 file_data, j);
927aa2e7
JK
4717 if (file_matcher (this_real_name, false))
4718 {
4719 per_cu->v.quick->mark = 1;
4720 break;
4721 }
4722 }
4723
b76e467d
SM
4724 void **slot = htab_find_slot (per_cu->v.quick->mark
4725 ? visited_found.get ()
4726 : visited_not_found.get (),
4727 file_data, INSERT);
927aa2e7
JK
4728 *slot = file_data;
4729 }
4730}
4731
4732static void
4733dw2_expand_symtabs_matching
4734 (struct objfile *objfile,
4735 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4736 const lookup_name_info *lookup_name,
927aa2e7
JK
4737 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4738 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4739 enum search_domain kind)
4740{
ed2dc618
SM
4741 struct dwarf2_per_objfile *dwarf2_per_objfile
4742 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4743
4744 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4745 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4746 return;
4747
ed2dc618 4748 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4749
c1a66c06
TV
4750 if (symbol_matcher == NULL && lookup_name == NULL)
4751 {
5989a64e 4752 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4753 {
4754 QUIT;
4755
97a1449a
SM
4756 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4757 file_matcher, expansion_notify);
c1a66c06
TV
4758 }
4759 return;
4760 }
4761
5989a64e 4762 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4763
c1a66c06 4764 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4765 symbol_matcher,
4766 kind, [&] (offset_type idx)
4767 {
ed2dc618 4768 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4769 expansion_notify, kind);
3b00ef10 4770 return true;
927aa2e7
JK
4771 });
4772}
4773
4774/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4775 symtab. */
4776
4777static struct compunit_symtab *
4778recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4779 CORE_ADDR pc)
4780{
4781 int i;
4782
4783 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4784 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4785 return cust;
4786
4787 if (cust->includes == NULL)
4788 return NULL;
4789
4790 for (i = 0; cust->includes[i]; ++i)
4791 {
4792 struct compunit_symtab *s = cust->includes[i];
4793
4794 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4795 if (s != NULL)
4796 return s;
4797 }
4798
4799 return NULL;
4800}
4801
4802static struct compunit_symtab *
4803dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4804 struct bound_minimal_symbol msymbol,
4805 CORE_ADDR pc,
4806 struct obj_section *section,
4807 int warn_if_readin)
4808{
4809 struct dwarf2_per_cu_data *data;
4810 struct compunit_symtab *result;
4811
d320c2b5 4812 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4813 return NULL;
4814
b3b3bada 4815 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4816 data = (struct dwarf2_per_cu_data *) addrmap_find
4817 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4818 if (!data)
4819 return NULL;
4820
af758d11
SM
4821 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4822 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4823 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4824 paddress (objfile->arch (), pc));
927aa2e7 4825
97a1449a
SM
4826 result = recursively_find_pc_sect_compunit_symtab
4827 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4828
927aa2e7
JK
4829 gdb_assert (result != NULL);
4830 return result;
4831}
4832
4833static void
4834dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4835 void *data, int need_fullname)
4836{
ed2dc618
SM
4837 struct dwarf2_per_objfile *dwarf2_per_objfile
4838 = get_dwarf2_per_objfile (objfile);
927aa2e7 4839
5989a64e 4840 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4841 {
5989a64e 4842 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4843
4844 htab_up visited (htab_create_alloc (10,
4845 htab_hash_pointer, htab_eq_pointer,
4846 NULL, xcalloc, xfree));
4847
4848 /* The rule is CUs specify all the files, including those used
4849 by any TU, so there's no need to scan TUs here. We can
4850 ignore file names coming from already-expanded CUs. */
4851
5989a64e 4852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4853 {
af758d11 4854 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4855 {
4856 void **slot = htab_find_slot (visited.get (),
4857 per_cu->v.quick->file_names,
4858 INSERT);
4859
4860 *slot = per_cu->v.quick->file_names;
4861 }
4862 }
4863
5989a64e 4864 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4865 {
927aa2e7 4866 /* We only need to look at symtabs not already expanded. */
af758d11 4867 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4868 continue;
4869
ab432490
SM
4870 quick_file_names *file_data
4871 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4872 if (file_data == NULL)
4873 continue;
4874
b76e467d 4875 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4876 if (*slot)
4877 {
4878 /* Already visited. */
4879 continue;
4880 }
4881 *slot = file_data;
4882
4883 for (int j = 0; j < file_data->num_file_names; ++j)
4884 {
4885 const char *filename = file_data->file_names[j];
5989a64e 4886 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4887 }
4888 }
4889 }
4890
5989a64e 4891 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4892 {
4893 gdb::unique_xmalloc_ptr<char> this_real_name;
4894
4895 if (need_fullname)
4896 this_real_name = gdb_realpath (filename);
4897 (*fun) (filename, this_real_name.get (), data);
4898 });
4899}
4900
4901static int
4902dw2_has_symbols (struct objfile *objfile)
4903{
4904 return 1;
4905}
4906
4907const struct quick_symbol_functions dwarf2_gdb_index_functions =
4908{
4909 dw2_has_symbols,
4910 dw2_find_last_source_symtab,
4911 dw2_forget_cached_source_info,
4912 dw2_map_symtabs_matching_filename,
4913 dw2_lookup_symbol,
d3214198 4914 NULL,
927aa2e7
JK
4915 dw2_print_stats,
4916 dw2_dump,
927aa2e7
JK
4917 dw2_expand_symtabs_for_function,
4918 dw2_expand_all_symtabs,
4919 dw2_expand_symtabs_with_fullname,
4920 dw2_map_matching_symbols,
4921 dw2_expand_symtabs_matching,
4922 dw2_find_pc_sect_compunit_symtab,
4923 NULL,
4924 dw2_map_symbol_filenames
4925};
4926
4927/* DWARF-5 debug_names reader. */
4928
4929/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4930static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4931
4932/* A helper function that reads the .debug_names section in SECTION
4933 and fills in MAP. FILENAME is the name of the file containing the
4934 section; it is used for error reporting.
4935
4936 Returns true if all went well, false otherwise. */
4937
4938static bool
4939read_debug_names_from_section (struct objfile *objfile,
4940 const char *filename,
4941 struct dwarf2_section_info *section,
4942 mapped_debug_names &map)
4943{
96b79293 4944 if (section->empty ())
927aa2e7
JK
4945 return false;
4946
4947 /* Older elfutils strip versions could keep the section in the main
4948 executable while splitting it for the separate debug info file. */
96b79293 4949 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4950 return false;
4951
96b79293 4952 section->read (objfile);
927aa2e7 4953
08feed99 4954 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4955
4956 const gdb_byte *addr = section->buffer;
4957
96b79293 4958 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4959
4960 unsigned int bytes_read;
4961 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4962 addr += bytes_read;
4963
4964 map.dwarf5_is_dwarf64 = bytes_read != 4;
4965 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4966 if (bytes_read + length != section->size)
4967 {
4968 /* There may be multiple per-CU indices. */
4969 warning (_("Section .debug_names in %s length %s does not match "
4970 "section length %s, ignoring .debug_names."),
4971 filename, plongest (bytes_read + length),
4972 pulongest (section->size));
4973 return false;
4974 }
4975
4976 /* The version number. */
4977 uint16_t version = read_2_bytes (abfd, addr);
4978 addr += 2;
4979 if (version != 5)
4980 {
4981 warning (_("Section .debug_names in %s has unsupported version %d, "
4982 "ignoring .debug_names."),
4983 filename, version);
4984 return false;
4985 }
4986
4987 /* Padding. */
4988 uint16_t padding = read_2_bytes (abfd, addr);
4989 addr += 2;
4990 if (padding != 0)
4991 {
4992 warning (_("Section .debug_names in %s has unsupported padding %d, "
4993 "ignoring .debug_names."),
4994 filename, padding);
4995 return false;
4996 }
4997
4998 /* comp_unit_count - The number of CUs in the CU list. */
4999 map.cu_count = read_4_bytes (abfd, addr);
5000 addr += 4;
5001
5002 /* local_type_unit_count - The number of TUs in the local TU
5003 list. */
5004 map.tu_count = read_4_bytes (abfd, addr);
5005 addr += 4;
5006
5007 /* foreign_type_unit_count - The number of TUs in the foreign TU
5008 list. */
5009 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5010 addr += 4;
5011 if (foreign_tu_count != 0)
5012 {
5013 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5014 "ignoring .debug_names."),
5015 filename, static_cast<unsigned long> (foreign_tu_count));
5016 return false;
5017 }
5018
5019 /* bucket_count - The number of hash buckets in the hash lookup
5020 table. */
5021 map.bucket_count = read_4_bytes (abfd, addr);
5022 addr += 4;
5023
5024 /* name_count - The number of unique names in the index. */
5025 map.name_count = read_4_bytes (abfd, addr);
5026 addr += 4;
5027
5028 /* abbrev_table_size - The size in bytes of the abbreviations
5029 table. */
5030 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5031 addr += 4;
5032
5033 /* augmentation_string_size - The size in bytes of the augmentation
5034 string. This value is rounded up to a multiple of 4. */
5035 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5036 addr += 4;
5037 map.augmentation_is_gdb = ((augmentation_string_size
5038 == sizeof (dwarf5_augmentation))
5039 && memcmp (addr, dwarf5_augmentation,
5040 sizeof (dwarf5_augmentation)) == 0);
5041 augmentation_string_size += (-augmentation_string_size) & 3;
5042 addr += augmentation_string_size;
5043
5044 /* List of CUs */
5045 map.cu_table_reordered = addr;
5046 addr += map.cu_count * map.offset_size;
5047
5048 /* List of Local TUs */
5049 map.tu_table_reordered = addr;
5050 addr += map.tu_count * map.offset_size;
5051
5052 /* Hash Lookup Table */
5053 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5054 addr += map.bucket_count * 4;
5055 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5056 addr += map.name_count * 4;
5057
5058 /* Name Table */
5059 map.name_table_string_offs_reordered = addr;
5060 addr += map.name_count * map.offset_size;
5061 map.name_table_entry_offs_reordered = addr;
5062 addr += map.name_count * map.offset_size;
5063
5064 const gdb_byte *abbrev_table_start = addr;
5065 for (;;)
5066 {
927aa2e7
JK
5067 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5068 addr += bytes_read;
5069 if (index_num == 0)
5070 break;
5071
5072 const auto insertpair
5073 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5074 if (!insertpair.second)
5075 {
5076 warning (_("Section .debug_names in %s has duplicate index %s, "
5077 "ignoring .debug_names."),
5078 filename, pulongest (index_num));
5079 return false;
5080 }
5081 mapped_debug_names::index_val &indexval = insertpair.first->second;
5082 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5083 addr += bytes_read;
5084
5085 for (;;)
5086 {
5087 mapped_debug_names::index_val::attr attr;
5088 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5089 addr += bytes_read;
5090 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5091 addr += bytes_read;
5092 if (attr.form == DW_FORM_implicit_const)
5093 {
5094 attr.implicit_const = read_signed_leb128 (abfd, addr,
5095 &bytes_read);
5096 addr += bytes_read;
5097 }
5098 if (attr.dw_idx == 0 && attr.form == 0)
5099 break;
5100 indexval.attr_vec.push_back (std::move (attr));
5101 }
5102 }
5103 if (addr != abbrev_table_start + abbrev_table_size)
5104 {
5105 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5106 "of size %s vs. written as %u, ignoring .debug_names."),
5107 filename, plongest (addr - abbrev_table_start),
5108 abbrev_table_size);
927aa2e7
JK
5109 return false;
5110 }
5111 map.entry_pool = addr;
5112
5113 return true;
5114}
5115
5116/* A helper for create_cus_from_debug_names that handles the MAP's CU
5117 list. */
5118
5119static void
ed2dc618 5120create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5121 const mapped_debug_names &map,
5122 dwarf2_section_info &section,
b76e467d 5123 bool is_dwz)
927aa2e7 5124{
3ee6bb11
TV
5125 if (!map.augmentation_is_gdb)
5126 {
5127 for (uint32_t i = 0; i < map.cu_count; ++i)
5128 {
5129 sect_offset sect_off
5130 = (sect_offset) (extract_unsigned_integer
5131 (map.cu_table_reordered + i * map.offset_size,
5132 map.offset_size,
5133 map.dwarf5_byte_order));
5134 /* We don't know the length of the CU, because the CU list in a
5135 .debug_names index can be incomplete, so we can't use the start of
5136 the next CU as end of this CU. We create the CUs here with length 0,
5137 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5138 dwarf2_per_cu_data *per_cu
5139 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5140 sect_off, 0);
5989a64e 5141 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5142 }
5143 }
5144
927aa2e7
JK
5145 sect_offset sect_off_prev;
5146 for (uint32_t i = 0; i <= map.cu_count; ++i)
5147 {
5148 sect_offset sect_off_next;
5149 if (i < map.cu_count)
5150 {
5151 sect_off_next
5152 = (sect_offset) (extract_unsigned_integer
5153 (map.cu_table_reordered + i * map.offset_size,
5154 map.offset_size,
5155 map.dwarf5_byte_order));
5156 }
5157 else
5158 sect_off_next = (sect_offset) section.size;
5159 if (i >= 1)
5160 {
5161 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5162 dwarf2_per_cu_data *per_cu
ed2dc618 5163 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5164 sect_off_prev, length);
5989a64e 5165 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5166 }
5167 sect_off_prev = sect_off_next;
5168 }
5169}
5170
5171/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5172 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5173
5174static void
ed2dc618 5175create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5176 const mapped_debug_names &map,
5177 const mapped_debug_names &dwz_map)
5178{
5989a64e
SM
5179 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5180 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5181
ed2dc618 5182 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5989a64e 5183 dwarf2_per_objfile->per_bfd->info,
b76e467d 5184 false /* is_dwz */);
927aa2e7
JK
5185
5186 if (dwz_map.cu_count == 0)
5187 return;
5188
c3699833 5189 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
ed2dc618 5190 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5191 true /* is_dwz */);
927aa2e7
JK
5192}
5193
5194/* Read .debug_names. If everything went ok, initialize the "quick"
5195 elements of all the CUs and return true. Otherwise, return false. */
5196
5197static bool
ed2dc618 5198dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5199{
22ca247e
TT
5200 std::unique_ptr<mapped_debug_names> map
5201 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5202 mapped_debug_names dwz_map (dwarf2_per_objfile);
5203 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5204
5205 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5206 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5207 *map))
927aa2e7
JK
5208 return false;
5209
5210 /* Don't use the index if it's empty. */
22ca247e 5211 if (map->name_count == 0)
927aa2e7
JK
5212 return false;
5213
5214 /* If there is a .dwz file, read it so we can get its CU list as
5215 well. */
c3699833 5216 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5217 if (dwz != NULL)
5218 {
5219 if (!read_debug_names_from_section (objfile,
00f93c44 5220 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5221 &dwz->debug_names, dwz_map))
5222 {
5223 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5224 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5225 return false;
5226 }
5227 }
5228
22ca247e 5229 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5230
22ca247e 5231 if (map->tu_count != 0)
927aa2e7
JK
5232 {
5233 /* We can only handle a single .debug_types when we have an
5234 index. */
5989a64e 5235 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5236 return false;
5237
5989a64e 5238 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5239
5240 create_signatured_type_table_from_debug_names
5989a64e 5241 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5242 }
5243
ed2dc618 5244 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5245 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5246
5989a64e
SM
5247 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5248 dwarf2_per_objfile->per_bfd->using_index = 1;
5249 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5250 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5251
5252 return true;
5253}
5254
927aa2e7
JK
5255/* Type used to manage iterating over all CUs looking for a symbol for
5256 .debug_names. */
5257
5258class dw2_debug_names_iterator
5259{
5260public:
927aa2e7 5261 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5262 gdb::optional<block_enum> block_index,
5263 domain_enum domain,
927aa2e7 5264 const char *name)
2b79f376 5265 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5266 m_addr (find_vec_in_debug_names (map, name))
5267 {}
5268
5269 dw2_debug_names_iterator (const mapped_debug_names &map,
5270 search_domain search, uint32_t namei)
5271 : m_map (map),
5272 m_search (search),
5273 m_addr (find_vec_in_debug_names (map, namei))
5274 {}
5275
3b00ef10
TT
5276 dw2_debug_names_iterator (const mapped_debug_names &map,
5277 block_enum block_index, domain_enum domain,
5278 uint32_t namei)
5279 : m_map (map), m_block_index (block_index), m_domain (domain),
5280 m_addr (find_vec_in_debug_names (map, namei))
5281 {}
5282
927aa2e7
JK
5283 /* Return the next matching CU or NULL if there are no more. */
5284 dwarf2_per_cu_data *next ();
5285
5286private:
5287 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5288 const char *name);
5289 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5290 uint32_t namei);
5291
5292 /* The internalized form of .debug_names. */
5293 const mapped_debug_names &m_map;
5294
2b79f376
SM
5295 /* If set, only look for symbols that match that block. Valid values are
5296 GLOBAL_BLOCK and STATIC_BLOCK. */
5297 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5298
5299 /* The kind of symbol we're looking for. */
5300 const domain_enum m_domain = UNDEF_DOMAIN;
5301 const search_domain m_search = ALL_DOMAIN;
5302
5303 /* The list of CUs from the index entry of the symbol, or NULL if
5304 not found. */
5305 const gdb_byte *m_addr;
5306};
5307
5308const char *
5309mapped_debug_names::namei_to_name (uint32_t namei) const
5310{
5311 const ULONGEST namei_string_offs
5312 = extract_unsigned_integer ((name_table_string_offs_reordered
5313 + namei * offset_size),
5314 offset_size,
5315 dwarf5_byte_order);
4f44ae6c
TT
5316 return read_indirect_string_at_offset (dwarf2_per_objfile,
5317 namei_string_offs);
927aa2e7
JK
5318}
5319
5320/* Find a slot in .debug_names for the object named NAME. If NAME is
5321 found, return pointer to its pool data. If NAME cannot be found,
5322 return NULL. */
5323
5324const gdb_byte *
5325dw2_debug_names_iterator::find_vec_in_debug_names
5326 (const mapped_debug_names &map, const char *name)
5327{
5328 int (*cmp) (const char *, const char *);
5329
54ee4252 5330 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5331 if (current_language->la_language == language_cplus
5332 || current_language->la_language == language_fortran
5333 || current_language->la_language == language_d)
5334 {
5335 /* NAME is already canonical. Drop any qualifiers as
5336 .debug_names does not contain any. */
5337
5338 if (strchr (name, '(') != NULL)
5339 {
54ee4252 5340 without_params = cp_remove_params (name);
927aa2e7 5341 if (without_params != NULL)
54ee4252 5342 name = without_params.get ();
927aa2e7
JK
5343 }
5344 }
5345
5346 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5347
5348 const uint32_t full_hash = dwarf5_djb_hash (name);
5349 uint32_t namei
5350 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5351 (map.bucket_table_reordered
5352 + (full_hash % map.bucket_count)), 4,
5353 map.dwarf5_byte_order);
5354 if (namei == 0)
5355 return NULL;
5356 --namei;
5357 if (namei >= map.name_count)
5358 {
b98664d3 5359 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5360 "[in module %s]"),
5361 namei, map.name_count,
ed2dc618 5362 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5363 return NULL;
5364 }
5365
5366 for (;;)
5367 {
5368 const uint32_t namei_full_hash
5369 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5370 (map.hash_table_reordered + namei), 4,
5371 map.dwarf5_byte_order);
5372 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5373 return NULL;
5374
5375 if (full_hash == namei_full_hash)
5376 {
5377 const char *const namei_string = map.namei_to_name (namei);
5378
5379#if 0 /* An expensive sanity check. */
5380 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5381 {
b98664d3 5382 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5383 "[in module %s]"),
5384 namei, objfile_name (dwarf2_per_objfile->objfile));
5385 return NULL;
5386 }
5387#endif
5388
5389 if (cmp (namei_string, name) == 0)
5390 {
5391 const ULONGEST namei_entry_offs
5392 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5393 + namei * map.offset_size),
5394 map.offset_size, map.dwarf5_byte_order);
5395 return map.entry_pool + namei_entry_offs;
5396 }
5397 }
5398
5399 ++namei;
5400 if (namei >= map.name_count)
5401 return NULL;
5402 }
5403}
5404
5405const gdb_byte *
5406dw2_debug_names_iterator::find_vec_in_debug_names
5407 (const mapped_debug_names &map, uint32_t namei)
5408{
5409 if (namei >= map.name_count)
5410 {
b98664d3 5411 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5412 "[in module %s]"),
5413 namei, map.name_count,
ed2dc618 5414 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5415 return NULL;
5416 }
5417
5418 const ULONGEST namei_entry_offs
5419 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5420 + namei * map.offset_size),
5421 map.offset_size, map.dwarf5_byte_order);
5422 return map.entry_pool + namei_entry_offs;
5423}
5424
5425/* See dw2_debug_names_iterator. */
5426
5427dwarf2_per_cu_data *
5428dw2_debug_names_iterator::next ()
5429{
5430 if (m_addr == NULL)
5431 return NULL;
5432
ed2dc618
SM
5433 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5434 struct objfile *objfile = dwarf2_per_objfile->objfile;
5435 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5436
5437 again:
5438
5439 unsigned int bytes_read;
5440 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5441 m_addr += bytes_read;
5442 if (abbrev == 0)
5443 return NULL;
5444
5445 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5446 if (indexval_it == m_map.abbrev_map.cend ())
5447 {
b98664d3 5448 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5449 "[in module %s]"),
ed2dc618 5450 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5451 return NULL;
5452 }
5453 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5454 enum class symbol_linkage {
5455 unknown,
5456 static_,
5457 extern_,
23c13d42 5458 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5459 dwarf2_per_cu_data *per_cu = NULL;
5460 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5461 {
5462 ULONGEST ull;
5463 switch (attr.form)
5464 {
5465 case DW_FORM_implicit_const:
5466 ull = attr.implicit_const;
5467 break;
5468 case DW_FORM_flag_present:
5469 ull = 1;
5470 break;
5471 case DW_FORM_udata:
5472 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5473 m_addr += bytes_read;
5474 break;
6dc55ce9 5475 case DW_FORM_ref4:
5476 ull = read_4_bytes (abfd, m_addr);
5477 m_addr += 4;
5478 break;
5479 case DW_FORM_ref8:
5480 ull = read_8_bytes (abfd, m_addr);
5481 m_addr += 8;
5482 break;
5483 case DW_FORM_ref_sig8:
5484 ull = read_8_bytes (abfd, m_addr);
5485 m_addr += 8;
5486 break;
927aa2e7 5487 default:
b98664d3 5488 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5489 dwarf_form_name (attr.form),
ed2dc618 5490 objfile_name (objfile));
927aa2e7
JK
5491 return NULL;
5492 }
5493 switch (attr.dw_idx)
5494 {
5495 case DW_IDX_compile_unit:
5496 /* Don't crash on bad data. */
5989a64e 5497 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5498 {
b98664d3 5499 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5500 " [in module %s]"),
5501 pulongest (ull),
5502 objfile_name (dwarf2_per_objfile->objfile));
5503 continue;
5504 }
5989a64e 5505 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5506 break;
8af5c486
JK
5507 case DW_IDX_type_unit:
5508 /* Don't crash on bad data. */
5989a64e 5509 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5510 {
b98664d3 5511 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5512 " [in module %s]"),
5513 pulongest (ull),
5514 objfile_name (dwarf2_per_objfile->objfile));
5515 continue;
5516 }
5989a64e 5517 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5518 break;
6dc55ce9 5519 case DW_IDX_die_offset:
5520 /* In a per-CU index (as opposed to a per-module index), index
5521 entries without CU attribute implicitly refer to the single CU. */
5522 if (per_cu == NULL)
5989a64e 5523 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5524 break;
927aa2e7
JK
5525 case DW_IDX_GNU_internal:
5526 if (!m_map.augmentation_is_gdb)
5527 break;
23c13d42 5528 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5529 break;
5530 case DW_IDX_GNU_external:
5531 if (!m_map.augmentation_is_gdb)
5532 break;
23c13d42 5533 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5534 break;
5535 }
5536 }
5537
5538 /* Skip if already read in. */
af758d11 5539 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5540 goto again;
5541
5542 /* Check static vs global. */
23c13d42 5543 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5544 {
2b79f376 5545 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5546 const bool symbol_is_static =
5547 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5548 if (want_static != symbol_is_static)
2b79f376 5549 goto again;
927aa2e7
JK
5550 }
5551
5552 /* Match dw2_symtab_iter_next, symbol_kind
5553 and debug_names::psymbol_tag. */
5554 switch (m_domain)
5555 {
5556 case VAR_DOMAIN:
5557 switch (indexval.dwarf_tag)
5558 {
5559 case DW_TAG_variable:
5560 case DW_TAG_subprogram:
5561 /* Some types are also in VAR_DOMAIN. */
5562 case DW_TAG_typedef:
5563 case DW_TAG_structure_type:
5564 break;
5565 default:
5566 goto again;
5567 }
5568 break;
5569 case STRUCT_DOMAIN:
5570 switch (indexval.dwarf_tag)
5571 {
5572 case DW_TAG_typedef:
5573 case DW_TAG_structure_type:
5574 break;
5575 default:
5576 goto again;
5577 }
5578 break;
5579 case LABEL_DOMAIN:
5580 switch (indexval.dwarf_tag)
5581 {
5582 case 0:
5583 case DW_TAG_variable:
5584 break;
5585 default:
5586 goto again;
5587 }
5588 break;
59c35742
AB
5589 case MODULE_DOMAIN:
5590 switch (indexval.dwarf_tag)
5591 {
5592 case DW_TAG_module:
5593 break;
5594 default:
5595 goto again;
5596 }
5597 break;
927aa2e7
JK
5598 default:
5599 break;
5600 }
5601
5602 /* Match dw2_expand_symtabs_matching, symbol_kind and
5603 debug_names::psymbol_tag. */
5604 switch (m_search)
4b514bc8 5605 {
927aa2e7
JK
5606 case VARIABLES_DOMAIN:
5607 switch (indexval.dwarf_tag)
4b514bc8 5608 {
927aa2e7
JK
5609 case DW_TAG_variable:
5610 break;
5611 default:
5612 goto again;
4b514bc8 5613 }
927aa2e7
JK
5614 break;
5615 case FUNCTIONS_DOMAIN:
5616 switch (indexval.dwarf_tag)
4b514bc8 5617 {
927aa2e7
JK
5618 case DW_TAG_subprogram:
5619 break;
5620 default:
5621 goto again;
4b514bc8 5622 }
927aa2e7
JK
5623 break;
5624 case TYPES_DOMAIN:
5625 switch (indexval.dwarf_tag)
5626 {
5627 case DW_TAG_typedef:
5628 case DW_TAG_structure_type:
5629 break;
5630 default:
5631 goto again;
5632 }
5633 break;
59c35742
AB
5634 case MODULES_DOMAIN:
5635 switch (indexval.dwarf_tag)
5636 {
5637 case DW_TAG_module:
5638 break;
5639 default:
5640 goto again;
5641 }
927aa2e7
JK
5642 default:
5643 break;
4b514bc8 5644 }
927aa2e7
JK
5645
5646 return per_cu;
4b514bc8 5647}
61920122 5648
927aa2e7 5649static struct compunit_symtab *
c7f839cb 5650dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5651 const char *name, domain_enum domain)
4b514bc8 5652{
ed2dc618
SM
5653 struct dwarf2_per_objfile *dwarf2_per_objfile
5654 = get_dwarf2_per_objfile (objfile);
61920122 5655
5989a64e 5656 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5657 if (!mapp)
61920122 5658 {
927aa2e7
JK
5659 /* index is NULL if OBJF_READNOW. */
5660 return NULL;
5661 }
5662 const auto &map = *mapp;
9291a0cd 5663
2b79f376 5664 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5665
927aa2e7
JK
5666 struct compunit_symtab *stab_best = NULL;
5667 struct dwarf2_per_cu_data *per_cu;
5668 while ((per_cu = iter.next ()) != NULL)
5669 {
5670 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5671 compunit_symtab *stab
5672 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5673 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5674 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5675
927aa2e7
JK
5676 sym = block_find_symbol (block, name, domain,
5677 block_find_non_opaque_type_preferred,
5678 &with_opaque);
9703b513 5679
927aa2e7
JK
5680 /* Some caution must be observed with overloaded functions and
5681 methods, since the index will not contain any overload
5682 information (but NAME might contain it). */
a3ec0bb1 5683
927aa2e7 5684 if (sym != NULL
987012b8 5685 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5686 return stab;
5687 if (with_opaque != NULL
987012b8 5688 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5689 stab_best = stab;
9703b513 5690
927aa2e7 5691 /* Keep looking through other CUs. */
9703b513
TT
5692 }
5693
927aa2e7 5694 return stab_best;
9703b513
TT
5695}
5696
927aa2e7
JK
5697/* This dumps minimal information about .debug_names. It is called
5698 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5699 uses this to verify that .debug_names has been loaded. */
9291a0cd 5700
927aa2e7
JK
5701static void
5702dw2_debug_names_dump (struct objfile *objfile)
5703{
ed2dc618
SM
5704 struct dwarf2_per_objfile *dwarf2_per_objfile
5705 = get_dwarf2_per_objfile (objfile);
5706
5989a64e 5707 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5708 printf_filtered (".debug_names:");
5989a64e 5709 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5710 printf_filtered (" exists\n");
5711 else
5712 printf_filtered (" faked for \"readnow\"\n");
5713 printf_filtered ("\n");
9291a0cd
TT
5714}
5715
9291a0cd 5716static void
927aa2e7
JK
5717dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5718 const char *func_name)
9291a0cd 5719{
ed2dc618
SM
5720 struct dwarf2_per_objfile *dwarf2_per_objfile
5721 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5722
5989a64e
SM
5723 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5724 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5725 {
5989a64e 5726 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5727
2b79f376 5728 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5729
927aa2e7
JK
5730 struct dwarf2_per_cu_data *per_cu;
5731 while ((per_cu = iter.next ()) != NULL)
97a1449a 5732 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5733 }
5734}
24c79950 5735
3b00ef10
TT
5736static void
5737dw2_debug_names_map_matching_symbols
5738 (struct objfile *objfile,
5739 const lookup_name_info &name, domain_enum domain,
5740 int global,
5741 gdb::function_view<symbol_found_callback_ftype> callback,
5742 symbol_compare_ftype *ordered_compare)
5743{
5744 struct dwarf2_per_objfile *dwarf2_per_objfile
5745 = get_dwarf2_per_objfile (objfile);
5746
5747 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5748 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5749 return;
5750
5989a64e 5751 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5752 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5753
5754 const char *match_name = name.ada ().lookup_name ().c_str ();
5755 auto matcher = [&] (const char *symname)
5756 {
5757 if (ordered_compare == nullptr)
5758 return true;
5759 return ordered_compare (symname, match_name) == 0;
5760 };
5761
5762 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5763 [&] (offset_type namei)
5764 {
5765 /* The name was matched, now expand corresponding CUs that were
5766 marked. */
5767 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5768
5769 struct dwarf2_per_cu_data *per_cu;
5770 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5771 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5772 nullptr);
3b00ef10
TT
5773 return true;
5774 });
5775
5776 /* It's a shame we couldn't do this inside the
5777 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5778 that have already been expanded. Instead, this loop matches what
5779 the psymtab code does. */
5989a64e 5780 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5781 {
af758d11
SM
5782 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5783 if (symtab != nullptr)
3b00ef10
TT
5784 {
5785 const struct block *block
af758d11 5786 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5787 if (!iterate_over_symbols_terminated (block, name,
5788 domain, callback))
5789 break;
5790 }
5791 }
5792}
5793
927aa2e7
JK
5794static void
5795dw2_debug_names_expand_symtabs_matching
5796 (struct objfile *objfile,
5797 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5798 const lookup_name_info *lookup_name,
927aa2e7
JK
5799 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5800 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5801 enum search_domain kind)
5802{
ed2dc618
SM
5803 struct dwarf2_per_objfile *dwarf2_per_objfile
5804 = get_dwarf2_per_objfile (objfile);
9291a0cd 5805
927aa2e7 5806 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5807 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5808 return;
9291a0cd 5809
ed2dc618 5810 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5811
c1a66c06
TV
5812 if (symbol_matcher == NULL && lookup_name == NULL)
5813 {
5989a64e 5814 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5815 {
5816 QUIT;
5817
97a1449a
SM
5818 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5819 file_matcher, expansion_notify);
c1a66c06
TV
5820 }
5821 return;
5822 }
5823
5989a64e 5824 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5825
c1a66c06 5826 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5827 symbol_matcher,
5828 kind, [&] (offset_type namei)
927aa2e7 5829 {
927aa2e7
JK
5830 /* The name was matched, now expand corresponding CUs that were
5831 marked. */
5832 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5833
927aa2e7
JK
5834 struct dwarf2_per_cu_data *per_cu;
5835 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5836 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5837 file_matcher, expansion_notify);
3b00ef10 5838 return true;
44ed8f3e 5839 });
9291a0cd
TT
5840}
5841
927aa2e7 5842const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5843{
5844 dw2_has_symbols,
5845 dw2_find_last_source_symtab,
5846 dw2_forget_cached_source_info,
f8eba3c6 5847 dw2_map_symtabs_matching_filename,
927aa2e7 5848 dw2_debug_names_lookup_symbol,
d3214198 5849 NULL,
9291a0cd 5850 dw2_print_stats,
927aa2e7 5851 dw2_debug_names_dump,
927aa2e7 5852 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5853 dw2_expand_all_symtabs,
652a8996 5854 dw2_expand_symtabs_with_fullname,
3b00ef10 5855 dw2_debug_names_map_matching_symbols,
927aa2e7 5856 dw2_debug_names_expand_symtabs_matching,
43f3e411 5857 dw2_find_pc_sect_compunit_symtab,
71a3c369 5858 NULL,
9291a0cd
TT
5859 dw2_map_symbol_filenames
5860};
5861
4485a1c1 5862/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5863 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5864
5865template <typename T>
5866static gdb::array_view<const gdb_byte>
5867get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5868{
5869 dwarf2_section_info *section = &section_owner->gdb_index;
5870
96b79293 5871 if (section->empty ())
4485a1c1
SM
5872 return {};
5873
5874 /* Older elfutils strip versions could keep the section in the main
5875 executable while splitting it for the separate debug info file. */
96b79293 5876 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5877 return {};
5878
96b79293 5879 section->read (obj);
4485a1c1 5880
8bebfcda
PA
5881 /* dwarf2_section_info::size is a bfd_size_type, while
5882 gdb::array_view works with size_t. On 32-bit hosts, with
5883 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5884 is 32-bit. So we need an explicit narrowing conversion here.
5885 This is fine, because it's impossible to allocate or mmap an
5886 array/buffer larger than what size_t can represent. */
5887 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5888}
5889
87d6a7aa
SM
5890/* Lookup the index cache for the contents of the index associated to
5891 DWARF2_OBJ. */
5892
5893static gdb::array_view<const gdb_byte>
5989a64e 5894get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5895{
5896 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5897 if (build_id == nullptr)
5898 return {};
5899
5900 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5901 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5902}
5903
5904/* Same as the above, but for DWZ. */
5905
5906static gdb::array_view<const gdb_byte>
5907get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5908{
5909 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5910 if (build_id == nullptr)
5911 return {};
5912
5913 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5914}
5915
3c0aa29a 5916/* See symfile.h. */
9291a0cd 5917
3c0aa29a
PA
5918bool
5919dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5920{
ed2dc618
SM
5921 struct dwarf2_per_objfile *dwarf2_per_objfile
5922 = get_dwarf2_per_objfile (objfile);
5923
9291a0cd
TT
5924 /* If we're about to read full symbols, don't bother with the
5925 indices. In this case we also don't care if some other debug
5926 format is making psymtabs, because they are all about to be
5927 expanded anyway. */
5928 if ((objfile->flags & OBJF_READNOW))
5929 {
5989a64e 5930 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5931 create_all_comp_units (dwarf2_per_objfile);
5932 create_all_type_units (dwarf2_per_objfile);
5989a64e 5933 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5934 = create_quick_file_names_table
5989a64e 5935 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5936 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5937
5989a64e
SM
5938 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5939 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5940 {
5989a64e 5941 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5942
5989a64e 5943 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5944 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5945 }
5946
5947 /* Return 1 so that gdb sees the "quick" functions. However,
5948 these functions will be no-ops because we will have expanded
5949 all symtabs. */
3c0aa29a
PA
5950 *index_kind = dw_index_kind::GDB_INDEX;
5951 return true;
9291a0cd
TT
5952 }
5953
ed2dc618 5954 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5955 {
5956 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5957 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5958 return true;
5959 }
927aa2e7 5960
4485a1c1 5961 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5962 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5963 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5964 {
5965 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5966 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5967 return true;
5968 }
9291a0cd 5969
87d6a7aa
SM
5970 /* ... otherwise, try to find the index in the index cache. */
5971 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5972 get_gdb_index_contents_from_cache,
5973 get_gdb_index_contents_from_cache_dwz))
5974 {
5975 global_index_cache.hit ();
5976 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5977 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5978 return true;
5979 }
5980
5981 global_index_cache.miss ();
3c0aa29a 5982 return false;
9291a0cd
TT
5983}
5984
5985\f
5986
dce234bc
PP
5987/* Build a partial symbol table. */
5988
5989void
f29dff0a 5990dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5991{
ed2dc618
SM
5992 struct dwarf2_per_objfile *dwarf2_per_objfile
5993 = get_dwarf2_per_objfile (objfile);
c9bf0622 5994
6eee24ce 5995 init_psymbol_list (objfile, 1024);
c906108c 5996
a70b8144 5997 try
c9bf0622
TT
5998 {
5999 /* This isn't really ideal: all the data we allocate on the
6000 objfile's obstack is still uselessly kept around. However,
6001 freeing it seems unsafe. */
906768f9 6002 psymtab_discarder psymtabs (objfile);
ed2dc618 6003 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6004 psymtabs.keep ();
87d6a7aa 6005
af758d11
SM
6006 dwarf2_per_objfile->resize_symtabs ();
6007
87d6a7aa
SM
6008 /* (maybe) store an index in the cache. */
6009 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6010 }
230d2906 6011 catch (const gdb_exception_error &except)
492d29ea
PA
6012 {
6013 exception_print (gdb_stderr, except);
6014 }
c906108c 6015}
c906108c 6016
3b80fe9b
DE
6017/* Find the base address of the compilation unit for range lists and
6018 location lists. It will normally be specified by DW_AT_low_pc.
6019 In DWARF-3 draft 4, the base address could be overridden by
6020 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6021 compilation units with discontinuous ranges. */
6022
6023static void
6024dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6025{
6026 struct attribute *attr;
6027
2b24b6e4 6028 cu->base_address.reset ();
3b80fe9b
DE
6029
6030 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6031 if (attr != nullptr)
2b24b6e4 6032 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6033 else
6034 {
6035 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6036 if (attr != nullptr)
2b24b6e4 6037 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6038 }
6039}
6040
36586728
TT
6041/* Helper function that returns the proper abbrev section for
6042 THIS_CU. */
6043
6044static struct dwarf2_section_info *
6045get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6046{
6047 struct dwarf2_section_info *abbrev;
c3699833 6048 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6049
6050 if (this_cu->is_dwz)
c3699833 6051 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6052 else
c3699833 6053 abbrev = &per_bfd->abbrev;
36586728
TT
6054
6055 return abbrev;
6056}
6057
f4dc4d17
DE
6058/* Fetch the abbreviation table offset from a comp or type unit header. */
6059
6060static sect_offset
ed2dc618
SM
6061read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6062 struct dwarf2_section_info *section,
9c541725 6063 sect_offset sect_off)
f4dc4d17 6064{
96b79293 6065 bfd *abfd = section->get_bfd_owner ();
d521ce57 6066 const gdb_byte *info_ptr;
ac298888 6067 unsigned int initial_length_size, offset_size;
43988095 6068 uint16_t version;
f4dc4d17 6069
96b79293 6070 section->read (dwarf2_per_objfile->objfile);
9c541725 6071 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6072 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6073 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6074 info_ptr += initial_length_size;
6075
6076 version = read_2_bytes (abfd, info_ptr);
6077 info_ptr += 2;
6078 if (version >= 5)
6079 {
6080 /* Skip unit type and address size. */
6081 info_ptr += 2;
6082 }
6083
24aa364d 6084 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6085}
6086
b83470bf
TT
6087/* A partial symtab that is used only for include files. */
6088struct dwarf2_include_psymtab : public partial_symtab
6089{
6090 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6091 : partial_symtab (filename, objfile)
6092 {
6093 }
6094
6095 void read_symtab (struct objfile *objfile) override
6096 {
194d088f
TV
6097 /* It's an include file, no symbols to read for it.
6098 Everything is in the includer symtab. */
6099
6100 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6101 expansion of the includer psymtab. We use the dependencies[0] field to
6102 model the includer. But if we go the regular route of calling
6103 expand_psymtab here, and having expand_psymtab call expand_dependencies
6104 to expand the includer, we'll only use expand_psymtab on the includer
6105 (making it a non-toplevel psymtab), while if we expand the includer via
6106 another path, we'll use read_symtab (making it a toplevel psymtab).
6107 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6108 psymtab, and trigger read_symtab on the includer here directly. */
6109 includer ()->read_symtab (objfile);
b83470bf
TT
6110 }
6111
6112 void expand_psymtab (struct objfile *objfile) override
6113 {
194d088f
TV
6114 /* This is not called by read_symtab, and should not be called by any
6115 expand_dependencies. */
6116 gdb_assert (false);
b83470bf
TT
6117 }
6118
5717c425 6119 bool readin_p (struct objfile *objfile) const override
b83470bf 6120 {
5717c425 6121 return includer ()->readin_p (objfile);
b83470bf
TT
6122 }
6123
5717c425 6124 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6125 {
6126 return nullptr;
6127 }
6128
6129private:
194d088f
TV
6130 partial_symtab *includer () const
6131 {
6132 /* An include psymtab has exactly one dependency: the psymtab that
6133 includes it. */
6134 gdb_assert (this->number_of_dependencies == 1);
6135 return this->dependencies[0];
6136 }
b83470bf
TT
6137};
6138
aaa75496
JB
6139/* Allocate a new partial symtab for file named NAME and mark this new
6140 partial symtab as being an include of PST. */
6141
6142static void
891813be 6143dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6144 struct objfile *objfile)
6145{
b83470bf 6146 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6147
fbd9ab74 6148 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6149 subpst->dirname = pst->dirname;
fbd9ab74 6150
a9342b62 6151 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6152 subpst->dependencies[0] = pst;
6153 subpst->number_of_dependencies = 1;
aaa75496
JB
6154}
6155
6156/* Read the Line Number Program data and extract the list of files
6157 included by the source file represented by PST. Build an include
d85a05f0 6158 partial symtab for each of these included files. */
aaa75496
JB
6159
6160static void
6161dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6162 struct die_info *die,
891813be 6163 dwarf2_psymtab *pst)
aaa75496 6164{
fff8551c 6165 line_header_up lh;
d85a05f0 6166 struct attribute *attr;
aaa75496 6167
d85a05f0 6168 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6169 if (attr != nullptr)
9c541725 6170 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6171 if (lh == NULL)
6172 return; /* No linetable, so no includes. */
6173
79748972
TT
6174 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6175 that we pass in the raw text_low here; that is ok because we're
6176 only decoding the line table to make include partial symtabs, and
6177 so the addresses aren't really used. */
4ae976d1 6178 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6179 pst->raw_text_low (), 1);
aaa75496
JB
6180}
6181
348e048f 6182static hashval_t
52dc124a 6183hash_signatured_type (const void *item)
348e048f 6184{
9a3c8263
SM
6185 const struct signatured_type *sig_type
6186 = (const struct signatured_type *) item;
9a619af0 6187
348e048f 6188 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6189 return sig_type->signature;
348e048f
DE
6190}
6191
6192static int
52dc124a 6193eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6194{
9a3c8263
SM
6195 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6196 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6197
348e048f
DE
6198 return lhs->signature == rhs->signature;
6199}
6200
1fd400ff
TT
6201/* Allocate a hash table for signatured types. */
6202
b0b6a987 6203static htab_up
298e9637 6204allocate_signatured_type_table ()
1fd400ff 6205{
b0b6a987
TT
6206 return htab_up (htab_create_alloc (41,
6207 hash_signatured_type,
6208 eq_signatured_type,
6209 NULL, xcalloc, xfree));
1fd400ff
TT
6210}
6211
d467dd73 6212/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6213
6214static int
d467dd73 6215add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6216{
9a3c8263 6217 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6218 std::vector<signatured_type *> *all_type_units
6219 = (std::vector<signatured_type *> *) datum;
1fd400ff 6220
b2bdb8cf 6221 all_type_units->push_back (sigt);
1fd400ff
TT
6222
6223 return 1;
6224}
6225
78d4d2c5 6226/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6227 and fill them into TYPES_HTAB. It will process only type units,
6228 therefore DW_UT_type. */
c88ee1f0 6229
78d4d2c5 6230static void
ed2dc618
SM
6231create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6232 struct dwo_file *dwo_file,
b0b6a987 6233 dwarf2_section_info *section, htab_up &types_htab,
43988095 6234 rcuh_kind section_kind)
348e048f 6235{
3019eac3 6236 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6237 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6238 bfd *abfd;
6239 const gdb_byte *info_ptr, *end_ptr;
348e048f 6240
4bdcc0c1
DE
6241 abbrev_section = (dwo_file != NULL
6242 ? &dwo_file->sections.abbrev
5989a64e 6243 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6244
b4f54984 6245 if (dwarf_read_debug)
43988095 6246 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6247 section->get_name (),
6248 abbrev_section->get_file_name ());
09406207 6249
96b79293 6250 section->read (objfile);
78d4d2c5 6251 info_ptr = section->buffer;
348e048f 6252
78d4d2c5
JK
6253 if (info_ptr == NULL)
6254 return;
348e048f 6255
78d4d2c5
JK
6256 /* We can't set abfd until now because the section may be empty or
6257 not present, in which case the bfd is unknown. */
96b79293 6258 abfd = section->get_bfd_owner ();
348e048f 6259
c0ab21c2
TT
6260 /* We don't use cutu_reader here because we don't need to read
6261 any dies: the signature is in the header. */
3019eac3 6262
78d4d2c5
JK
6263 end_ptr = info_ptr + section->size;
6264 while (info_ptr < end_ptr)
6265 {
78d4d2c5
JK
6266 struct signatured_type *sig_type;
6267 struct dwo_unit *dwo_tu;
6268 void **slot;
6269 const gdb_byte *ptr = info_ptr;
6270 struct comp_unit_head header;
6271 unsigned int length;
8b70b953 6272
9c541725 6273 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6274
a49dd8dd
JK
6275 /* Initialize it due to a false compiler warning. */
6276 header.signature = -1;
9c541725 6277 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6278
78d4d2c5
JK
6279 /* We need to read the type's signature in order to build the hash
6280 table, but we don't need anything else just yet. */
348e048f 6281
ed2dc618 6282 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6283 abbrev_section, ptr, section_kind);
348e048f 6284
4057dfde 6285 length = header.get_length ();
6caca83c 6286
78d4d2c5
JK
6287 /* Skip dummy type units. */
6288 if (ptr >= info_ptr + length
43988095
JK
6289 || peek_abbrev_code (abfd, ptr) == 0
6290 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6291 {
6292 info_ptr += length;
6293 continue;
6294 }
dee91e82 6295
78d4d2c5
JK
6296 if (types_htab == NULL)
6297 {
6298 if (dwo_file)
298e9637 6299 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6300 else
298e9637 6301 types_htab = allocate_signatured_type_table ();
78d4d2c5 6302 }
8b70b953 6303
78d4d2c5
JK
6304 if (dwo_file)
6305 {
6306 sig_type = NULL;
5989a64e 6307 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6308 struct dwo_unit);
6309 dwo_tu->dwo_file = dwo_file;
43988095 6310 dwo_tu->signature = header.signature;
9c541725 6311 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6312 dwo_tu->section = section;
9c541725 6313 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6314 dwo_tu->length = length;
6315 }
6316 else
6317 {
6318 /* N.B.: type_offset is not usable if this type uses a DWO file.
6319 The real type_offset is in the DWO file. */
6320 dwo_tu = NULL;
5989a64e 6321 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6322 sig_type->signature = header.signature;
9c541725 6323 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6324 sig_type->per_cu.is_debug_types = 1;
6325 sig_type->per_cu.section = section;
9c541725 6326 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6327 sig_type->per_cu.length = length;
6328 }
6329
b0b6a987 6330 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6331 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6332 INSERT);
6333 gdb_assert (slot != NULL);
6334 if (*slot != NULL)
6335 {
9c541725 6336 sect_offset dup_sect_off;
0349ea22 6337
3019eac3
DE
6338 if (dwo_file)
6339 {
78d4d2c5
JK
6340 const struct dwo_unit *dup_tu
6341 = (const struct dwo_unit *) *slot;
6342
9c541725 6343 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6344 }
6345 else
6346 {
78d4d2c5
JK
6347 const struct signatured_type *dup_tu
6348 = (const struct signatured_type *) *slot;
6349
9c541725 6350 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6351 }
8b70b953 6352
b98664d3 6353 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6354 " the entry at offset %s, signature %s"),
6355 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6356 hex_string (header.signature));
78d4d2c5
JK
6357 }
6358 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6359
78d4d2c5 6360 if (dwarf_read_debug > 1)
9d8780f0
SM
6361 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6362 sect_offset_str (sect_off),
43988095 6363 hex_string (header.signature));
3019eac3 6364
78d4d2c5
JK
6365 info_ptr += length;
6366 }
6367}
3019eac3 6368
78d4d2c5
JK
6369/* Create the hash table of all entries in the .debug_types
6370 (or .debug_types.dwo) section(s).
6371 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6372 otherwise it is NULL.
b3c8eb43 6373
78d4d2c5 6374 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6375
78d4d2c5 6376 Note: This function processes DWO files only, not DWP files. */
348e048f 6377
78d4d2c5 6378static void
ed2dc618
SM
6379create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6380 struct dwo_file *dwo_file,
fd5866f6 6381 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6382 htab_up &types_htab)
78d4d2c5 6383{
fd5866f6
SM
6384 for (dwarf2_section_info &section : type_sections)
6385 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6386 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6387}
6388
6389/* Create the hash table of all entries in the .debug_types section,
6390 and initialize all_type_units.
6391 The result is zero if there is an error (e.g. missing .debug_types section),
6392 otherwise non-zero. */
6393
6394static int
ed2dc618 6395create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6396{
b0b6a987 6397 htab_up types_htab;
3019eac3 6398
ed2dc618 6399 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6400 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6401 rcuh_kind::COMPILE);
ed2dc618 6402 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6403 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6404 if (types_htab == NULL)
6405 {
5989a64e 6406 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6407 return 0;
6408 }
6409
5989a64e 6410 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6411
5989a64e
SM
6412 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6413 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6414 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6415
5989a64e 6416 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6417 add_signatured_type_cu_to_table,
5989a64e 6418 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6419
348e048f
DE
6420 return 1;
6421}
6422
5989a64e 6423/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6424 If SLOT is non-NULL, it is the entry to use in the hash table.
6425 Otherwise we find one. */
6426
6427static struct signatured_type *
ed2dc618
SM
6428add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6429 void **slot)
6aa5f3a6 6430{
5989a64e
SM
6431 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6432 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6433 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6434
5989a64e 6435 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6436
af758d11
SM
6437 dwarf2_per_objfile->resize_symtabs ();
6438
5989a64e 6439 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6440 sig_type->signature = sig;
6441 sig_type->per_cu.is_debug_types = 1;
5989a64e 6442 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6443 {
6444 sig_type->per_cu.v.quick =
5989a64e 6445 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6446 struct dwarf2_per_cu_quick_data);
6447 }
6448
6449 if (slot == NULL)
6450 {
5989a64e 6451 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6452 sig_type, INSERT);
6453 }
6454 gdb_assert (*slot == NULL);
6455 *slot = sig_type;
6456 /* The rest of sig_type must be filled in by the caller. */
6457 return sig_type;
6458}
6459
a2ce51a0
DE
6460/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6461 Fill in SIG_ENTRY with DWO_ENTRY. */
6462
6463static void
ed2dc618 6464fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6465 struct signatured_type *sig_entry,
6466 struct dwo_unit *dwo_entry)
6467{
1859c670
SM
6468 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6469
7ee85ab1 6470 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6471 gdb_assert (! sig_entry->per_cu.queued);
6472 gdb_assert (sig_entry->per_cu.cu == NULL);
1859c670 6473 if (per_bfd->using_index)
6aa5f3a6
DE
6474 {
6475 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6476 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6477 }
6478 else
6479 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6480 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6481 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6482 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6483 gdb_assert (sig_entry->dwo_unit == NULL);
6484
6485 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6486 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6487 sig_entry->per_cu.length = dwo_entry->length;
6488 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6489 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6490 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6491 sig_entry->dwo_unit = dwo_entry;
6492}
6493
6494/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6495 If we haven't read the TU yet, create the signatured_type data structure
6496 for a TU to be read in directly from a DWO file, bypassing the stub.
6497 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6498 using .gdb_index, then when reading a CU we want to stay in the DWO file
6499 containing that CU. Otherwise we could end up reading several other DWO
6500 files (due to comdat folding) to process the transitive closure of all the
6501 mentioned TUs, and that can be slow. The current DWO file will have every
6502 type signature that it needs.
a2ce51a0
DE
6503 We only do this for .gdb_index because in the psymtab case we already have
6504 to read all the DWOs to build the type unit groups. */
6505
6506static struct signatured_type *
6507lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6508{
5e22e966 6509 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6510 struct dwo_file *dwo_file;
6511 struct dwo_unit find_dwo_entry, *dwo_entry;
6512 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6513 void **slot;
a2ce51a0 6514
5989a64e 6515 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6516
6aa5f3a6
DE
6517 /* If TU skeletons have been removed then we may not have read in any
6518 TUs yet. */
5989a64e
SM
6519 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6520 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6521
6522 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6523 Use the global signatured_types array to do our own comdat-folding
6524 of types. If this is the first time we're reading this TU, and
6525 the TU has an entry in .gdb_index, replace the recorded data from
6526 .gdb_index with this TU. */
a2ce51a0 6527
a2ce51a0 6528 find_sig_entry.signature = sig;
5989a64e 6529 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6530 &find_sig_entry, INSERT);
9a3c8263 6531 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6532
6533 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6534 read. Don't reassign the global entry to point to this DWO if that's
6535 the case. Also note that if the TU is already being read, it may not
6536 have come from a DWO, the program may be a mix of Fission-compiled
6537 code and non-Fission-compiled code. */
6538
6539 /* Have we already tried to read this TU?
6540 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6541 needn't exist in the global table yet). */
6542 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6543 return sig_entry;
6544
6aa5f3a6
DE
6545 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6546 dwo_unit of the TU itself. */
6547 dwo_file = cu->dwo_unit->dwo_file;
6548
a2ce51a0
DE
6549 /* Ok, this is the first time we're reading this TU. */
6550 if (dwo_file->tus == NULL)
6551 return NULL;
6552 find_dwo_entry.signature = sig;
b0b6a987
TT
6553 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6554 &find_dwo_entry);
a2ce51a0
DE
6555 if (dwo_entry == NULL)
6556 return NULL;
6557
6aa5f3a6
DE
6558 /* If the global table doesn't have an entry for this TU, add one. */
6559 if (sig_entry == NULL)
ed2dc618 6560 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6561
ed2dc618 6562 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6563 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6564 return sig_entry;
6565}
6566
a2ce51a0
DE
6567/* Subroutine of lookup_signatured_type.
6568 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6569 then try the DWP file. If the TU stub (skeleton) has been removed then
6570 it won't be in .gdb_index. */
a2ce51a0
DE
6571
6572static struct signatured_type *
6573lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6574{
5e22e966 6575 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6576 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6577 struct dwo_unit *dwo_entry;
6578 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6579 void **slot;
a2ce51a0 6580
5989a64e 6581 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6582 gdb_assert (dwp_file != NULL);
6583
6aa5f3a6
DE
6584 /* If TU skeletons have been removed then we may not have read in any
6585 TUs yet. */
5989a64e
SM
6586 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6587 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6588
6aa5f3a6 6589 find_sig_entry.signature = sig;
5989a64e 6590 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6591 &find_sig_entry, INSERT);
9a3c8263 6592 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6593
6594 /* Have we already tried to read this TU?
6595 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6596 needn't exist in the global table yet). */
6597 if (sig_entry != NULL)
6598 return sig_entry;
6599
a2ce51a0
DE
6600 if (dwp_file->tus == NULL)
6601 return NULL;
ed2dc618 6602 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6603 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6604 if (dwo_entry == NULL)
6605 return NULL;
6606
ed2dc618
SM
6607 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6608 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6609
a2ce51a0
DE
6610 return sig_entry;
6611}
6612
380bca97 6613/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6614 Returns NULL if signature SIG is not present in the table.
6615 It is up to the caller to complain about this. */
348e048f
DE
6616
6617static struct signatured_type *
a2ce51a0 6618lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6619{
5e22e966 6620 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6621
a2ce51a0 6622 if (cu->dwo_unit
5989a64e 6623 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6624 {
6625 /* We're in a DWO/DWP file, and we're using .gdb_index.
6626 These cases require special processing. */
ed2dc618 6627 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6628 return lookup_dwo_signatured_type (cu, sig);
6629 else
6630 return lookup_dwp_signatured_type (cu, sig);
6631 }
6632 else
6633 {
6634 struct signatured_type find_entry, *entry;
348e048f 6635
5989a64e 6636 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6637 return NULL;
6638 find_entry.signature = sig;
9a3c8263 6639 entry = ((struct signatured_type *)
5989a64e 6640 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6641 &find_entry));
a2ce51a0
DE
6642 return entry;
6643 }
348e048f 6644}
18a8505e 6645
42e7ad6c 6646/* Low level DIE reading support. */
348e048f 6647
d85a05f0
DJ
6648/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6649
6650static void
6651init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6652 struct dwarf2_cu *cu,
3019eac3 6653 struct dwarf2_section_info *section,
685af9cd
TT
6654 struct dwo_file *dwo_file,
6655 struct abbrev_table *abbrev_table)
d85a05f0 6656{
fceca515 6657 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6658 reader->abfd = section->get_bfd_owner ();
d85a05f0 6659 reader->cu = cu;
3019eac3 6660 reader->dwo_file = dwo_file;
dee91e82
DE
6661 reader->die_section = section;
6662 reader->buffer = section->buffer;
f664829e 6663 reader->buffer_end = section->buffer + section->size;
685af9cd 6664 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6665}
6666
c0ab21c2 6667/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6668 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6669 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6670 already.
6671
6672 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6673 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6674 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6675 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6676 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6677 STUB_COMP_DIR may be non-NULL.
3e225074 6678 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6679 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6680 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6681 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6682 kept around for at least as long as *RESULT_READER.
6683
b0c7bfa9
DE
6684 The result is non-zero if a valid (non-dummy) DIE was found. */
6685
6686static int
4ab09049 6687read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6688 struct dwo_unit *dwo_unit,
b0c7bfa9 6689 struct die_info *stub_comp_unit_die,
a2ce51a0 6690 const char *stub_comp_dir,
b0c7bfa9 6691 struct die_reader_specs *result_reader,
d521ce57 6692 const gdb_byte **result_info_ptr,
b0c7bfa9 6693 struct die_info **result_comp_unit_die,
685af9cd 6694 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6695{
4ab09049
SM
6696 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6697 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6698 struct objfile *objfile = dwarf2_per_objfile->objfile;
b0c7bfa9 6699 bfd *abfd;
d521ce57 6700 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6701 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6702 int i,num_extra_attrs;
6703 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6704 struct die_info *comp_unit_die;
6705
b0aeadb3
DE
6706 /* At most one of these may be provided. */
6707 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6708
b0c7bfa9
DE
6709 /* These attributes aren't processed until later:
6710 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6711 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6712 referenced later. However, these attributes are found in the stub
6713 which we won't have later. In order to not impose this complication
6714 on the rest of the code, we read them here and copy them to the
6715 DWO CU/TU die. */
b0c7bfa9
DE
6716
6717 stmt_list = NULL;
6718 low_pc = NULL;
6719 high_pc = NULL;
6720 ranges = NULL;
6721 comp_dir = NULL;
6722
6723 if (stub_comp_unit_die != NULL)
6724 {
6725 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6726 DWO file. */
4ab09049 6727 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6728 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6729 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6730 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6731 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6732 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6733
a39fdb41 6734 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6735
18a8505e
AT
6736 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6737 here (if needed). We need the value before we can process
6738 DW_AT_ranges. */
a39fdb41 6739 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6740 }
a2ce51a0
DE
6741 else if (stub_comp_dir != NULL)
6742 {
6743 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6744 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6745 comp_dir->name = DW_AT_comp_dir;
6746 comp_dir->form = DW_FORM_string;
6747 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6748 DW_STRING (comp_dir) = stub_comp_dir;
6749 }
b0c7bfa9
DE
6750
6751 /* Set up for reading the DWO CU/TU. */
6752 cu->dwo_unit = dwo_unit;
685af9cd 6753 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6754 section->read (objfile);
6755 abfd = section->get_bfd_owner ();
9c541725
PA
6756 begin_info_ptr = info_ptr = (section->buffer
6757 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6758 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6759
4ab09049 6760 if (per_cu->is_debug_types)
b0c7bfa9 6761 {
4ab09049 6762 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6763
ed2dc618
SM
6764 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6765 &cu->header, section,
b0c7bfa9 6766 dwo_abbrev_section,
43988095 6767 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6768 /* This is not an assert because it can be caused by bad debug info. */
43988095 6769 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6770 {
6771 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6772 " TU at offset %s [in module %s]"),
a2ce51a0 6773 hex_string (sig_type->signature),
43988095 6774 hex_string (cu->header.signature),
9d8780f0 6775 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6776 bfd_get_filename (abfd));
6777 }
9c541725 6778 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6779 /* For DWOs coming from DWP files, we don't know the CU length
6780 nor the type's offset in the TU until now. */
4057dfde 6781 dwo_unit->length = cu->header.get_length ();
9c541725 6782 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6783
6784 /* Establish the type offset that can be used to lookup the type.
6785 For DWO files, we don't know it until now. */
9c541725
PA
6786 sig_type->type_offset_in_section
6787 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6788 }
6789 else
6790 {
ed2dc618
SM
6791 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6792 &cu->header, section,
b0c7bfa9 6793 dwo_abbrev_section,
43988095 6794 info_ptr, rcuh_kind::COMPILE);
9c541725 6795 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6796 /* For DWOs coming from DWP files, we don't know the CU length
6797 until now. */
4057dfde 6798 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6799 }
6800
685af9cd 6801 *result_dwo_abbrev_table
86de1d91
TT
6802 = abbrev_table::read (objfile, dwo_abbrev_section,
6803 cu->header.abbrev_sect_off);
685af9cd
TT
6804 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6805 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6806
6807 /* Read in the die, but leave space to copy over the attributes
6808 from the stub. This has the benefit of simplifying the rest of
6809 the code - all the work to maintain the illusion of a single
6810 DW_TAG_{compile,type}_unit DIE is done here. */
6811 num_extra_attrs = ((stmt_list != NULL)
6812 + (low_pc != NULL)
6813 + (high_pc != NULL)
6814 + (ranges != NULL)
6815 + (comp_dir != NULL));
6816 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6817 num_extra_attrs);
b0c7bfa9
DE
6818
6819 /* Copy over the attributes from the stub to the DIE we just read in. */
6820 comp_unit_die = *result_comp_unit_die;
6821 i = comp_unit_die->num_attrs;
6822 if (stmt_list != NULL)
6823 comp_unit_die->attrs[i++] = *stmt_list;
6824 if (low_pc != NULL)
6825 comp_unit_die->attrs[i++] = *low_pc;
6826 if (high_pc != NULL)
6827 comp_unit_die->attrs[i++] = *high_pc;
6828 if (ranges != NULL)
6829 comp_unit_die->attrs[i++] = *ranges;
6830 if (comp_dir != NULL)
6831 comp_unit_die->attrs[i++] = *comp_dir;
6832 comp_unit_die->num_attrs += num_extra_attrs;
6833
b4f54984 6834 if (dwarf_die_debug)
bf6af496
DE
6835 {
6836 fprintf_unfiltered (gdb_stdlog,
6837 "Read die from %s@0x%x of %s:\n",
96b79293 6838 section->get_name (),
bf6af496
DE
6839 (unsigned) (begin_info_ptr - section->buffer),
6840 bfd_get_filename (abfd));
b4f54984 6841 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6842 }
6843
b0c7bfa9
DE
6844 /* Skip dummy compilation units. */
6845 if (info_ptr >= begin_info_ptr + dwo_unit->length
6846 || peek_abbrev_code (abfd, info_ptr) == 0)
6847 return 0;
6848
6849 *result_info_ptr = info_ptr;
6850 return 1;
6851}
6852
a084a2a6
AT
6853/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6854 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6855 signature is part of the header. */
6856static gdb::optional<ULONGEST>
6857lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6858{
6859 if (cu->header.version >= 5)
6860 return cu->header.signature;
6861 struct attribute *attr;
6862 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6863 if (attr == nullptr)
6864 return gdb::optional<ULONGEST> ();
6865 return DW_UNSND (attr);
6866}
6867
c0ab21c2 6868/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6869 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6870 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6871
6872static struct dwo_unit *
4ab09049 6873lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6874{
4ab09049 6875 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6876 struct dwo_unit *dwo_unit;
c0ab21c2 6877 const char *comp_dir;
b0c7bfa9 6878
a2ce51a0
DE
6879 gdb_assert (cu != NULL);
6880
b0c7bfa9 6881 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6882 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6883 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6884
4ab09049
SM
6885 if (per_cu->is_debug_types)
6886 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6887 else
6888 {
a084a2a6 6889 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6890
a084a2a6 6891 if (!signature.has_value ())
b0c7bfa9
DE
6892 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6893 " [in module %s]"),
4ab09049
SM
6894 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6895
6896 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6897 }
6898
b0c7bfa9
DE
6899 return dwo_unit;
6900}
6901
c0ab21c2 6902/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6903 See it for a description of the parameters.
fcd3b13d 6904 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6905
c0ab21c2 6906void
9e021579
SM
6907cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6908 dwarf2_per_objfile *per_objfile,
6751ebae 6909 int use_existing_cu)
a2ce51a0 6910{
a2ce51a0 6911 struct signatured_type *sig_type;
a2ce51a0
DE
6912
6913 /* Verify we can do the following downcast, and that we have the
6914 data we need. */
6915 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6916 sig_type = (struct signatured_type *) this_cu;
6917 gdb_assert (sig_type->dwo_unit != NULL);
6918
6aa5f3a6
DE
6919 if (use_existing_cu && this_cu->cu != NULL)
6920 {
6921 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6922 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6923 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6924 }
6925 else
6926 {
6927 /* If !use_existing_cu, this_cu->cu must be NULL. */
6928 gdb_assert (this_cu->cu == NULL);
9e021579 6929 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6aa5f3a6
DE
6930 }
6931
6932 /* A future optimization, if needed, would be to use an existing
6933 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6934 could share abbrev tables. */
a2ce51a0 6935
4ab09049 6936 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
a2ce51a0
DE
6937 NULL /* stub_comp_unit_die */,
6938 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6939 this, &info_ptr,
3e225074 6940 &comp_unit_die,
c0ab21c2 6941 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6942 {
6943 /* Dummy die. */
c0ab21c2 6944 dummy_p = true;
a2ce51a0 6945 }
a2ce51a0
DE
6946}
6947
fd820528 6948/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6949 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6950
f4dc4d17
DE
6951 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6952 Otherwise the table specified in the comp unit header is read in and used.
6953 This is an optimization for when we already have the abbrev table.
6954
dee91e82 6955 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6956 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6957
ab432490
SM
6958cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6959 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2 6960 struct abbrev_table *abbrev_table,
6751ebae 6961 int use_existing_cu,
c0ab21c2
TT
6962 bool skip_partial)
6963 : die_reader_specs {},
6751ebae 6964 m_this_cu (this_cu)
c906108c 6965{
dee91e82 6966 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6967 struct dwarf2_section_info *section = this_cu->section;
96b79293 6968 bfd *abfd = section->get_bfd_owner ();
dee91e82 6969 struct dwarf2_cu *cu;
c0ab21c2 6970 const gdb_byte *begin_info_ptr;
dee91e82 6971 struct signatured_type *sig_type = NULL;
4bdcc0c1 6972 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6973 /* Non-zero if CU currently points to a DWO file and we need to
6974 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6975 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6976 int rereading_dwo_cu = 0;
c906108c 6977
b4f54984 6978 if (dwarf_die_debug)
9d8780f0 6979 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6980 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6981 sect_offset_str (this_cu->sect_off));
09406207 6982
a2ce51a0
DE
6983 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6984 file (instead of going through the stub), short-circuit all of this. */
6985 if (this_cu->reading_dwo_directly)
6986 {
6987 /* Narrow down the scope of possibilities to have to understand. */
6988 gdb_assert (this_cu->is_debug_types);
6989 gdb_assert (abbrev_table == NULL);
9e021579 6990 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
a2ce51a0
DE
6991 return;
6992 }
6993
dee91e82 6994 /* This is cheap if the section is already read in. */
96b79293 6995 section->read (objfile);
dee91e82 6996
9c541725 6997 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6998
6999 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7000
7001 if (use_existing_cu && this_cu->cu != NULL)
7002 {
7003 cu = this_cu->cu;
42e7ad6c
DE
7004 /* If this CU is from a DWO file we need to start over, we need to
7005 refetch the attributes from the skeleton CU.
7006 This could be optimized by retrieving those attributes from when we
7007 were here the first time: the previous comp_unit_die was stored in
7008 comp_unit_obstack. But there's no data yet that we need this
7009 optimization. */
7010 if (cu->dwo_unit != NULL)
7011 rereading_dwo_cu = 1;
dee91e82
DE
7012 }
7013 else
7014 {
7015 /* If !use_existing_cu, this_cu->cu must be NULL. */
7016 gdb_assert (this_cu->cu == NULL);
9e021579 7017 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 7018 cu = m_new_cu.get ();
42e7ad6c 7019 }
dee91e82 7020
b0c7bfa9 7021 /* Get the header. */
9c541725 7022 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7023 {
7024 /* We already have the header, there's no need to read it in again. */
9c541725 7025 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7026 }
7027 else
7028 {
3019eac3 7029 if (this_cu->is_debug_types)
dee91e82 7030 {
ed2dc618
SM
7031 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7032 &cu->header, section,
4bdcc0c1 7033 abbrev_section, info_ptr,
43988095 7034 rcuh_kind::TYPE);
dee91e82 7035
42e7ad6c
DE
7036 /* Since per_cu is the first member of struct signatured_type,
7037 we can go from a pointer to one to a pointer to the other. */
7038 sig_type = (struct signatured_type *) this_cu;
43988095 7039 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7040 gdb_assert (sig_type->type_offset_in_tu
7041 == cu->header.type_cu_offset_in_tu);
7042 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7043
42e7ad6c
DE
7044 /* LENGTH has not been set yet for type units if we're
7045 using .gdb_index. */
4057dfde 7046 this_cu->length = cu->header.get_length ();
3019eac3
DE
7047
7048 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7049 sig_type->type_offset_in_section =
7050 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7051
7052 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7053 }
7054 else
7055 {
ed2dc618
SM
7056 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7057 &cu->header, section,
4bdcc0c1 7058 abbrev_section,
43988095
JK
7059 info_ptr,
7060 rcuh_kind::COMPILE);
dee91e82 7061
9c541725 7062 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7063 if (this_cu->length == 0)
7064 this_cu->length = cu->header.get_length ();
7065 else
7066 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7067 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7068 }
7069 }
10b3939b 7070
6caca83c 7071 /* Skip dummy compilation units. */
dee91e82 7072 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7073 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7074 {
7075 dummy_p = true;
7076 return;
7077 }
6caca83c 7078
433df2d4
DE
7079 /* If we don't have them yet, read the abbrevs for this compilation unit.
7080 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7081 done. */
f4dc4d17 7082 if (abbrev_table != NULL)
685af9cd
TT
7083 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7084 else
f4dc4d17 7085 {
c0ab21c2 7086 m_abbrev_table_holder
86de1d91
TT
7087 = abbrev_table::read (objfile, abbrev_section,
7088 cu->header.abbrev_sect_off);
c0ab21c2 7089 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7090 }
af703f96 7091
dee91e82 7092 /* Read the top level CU/TU die. */
c0ab21c2 7093 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7094 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7095
58f0c718 7096 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7097 {
7098 dummy_p = true;
7099 return;
7100 }
58f0c718 7101
b0c7bfa9 7102 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7103 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7104 table from the DWO file and pass the ownership over to us. It will be
7105 referenced from READER, so we must make sure to free it after we're done
7106 with READER.
7107
b0c7bfa9
DE
7108 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7109 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7110 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7111 if (dwo_name != nullptr)
3019eac3 7112 {
3019eac3 7113 struct dwo_unit *dwo_unit;
b0c7bfa9 7114 struct die_info *dwo_comp_unit_die;
3019eac3 7115
3e225074 7116 if (comp_unit_die->has_children)
6a506a2d 7117 {
b98664d3 7118 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7119 " has children (offset %s) [in module %s]"),
7120 sect_offset_str (this_cu->sect_off),
7121 bfd_get_filename (abfd));
6a506a2d 7122 }
4ab09049 7123 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7124 if (dwo_unit != NULL)
3019eac3 7125 {
4ab09049 7126 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7127 comp_unit_die, NULL,
c0ab21c2 7128 this, &info_ptr,
3e225074 7129 &dwo_comp_unit_die,
c0ab21c2 7130 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7131 {
7132 /* Dummy die. */
c0ab21c2 7133 dummy_p = true;
6a506a2d
DE
7134 return;
7135 }
7136 comp_unit_die = dwo_comp_unit_die;
7137 }
7138 else
7139 {
7140 /* Yikes, we couldn't find the rest of the DIE, we only have
7141 the stub. A complaint has already been logged. There's
7142 not much more we can do except pass on the stub DIE to
7143 die_reader_func. We don't want to throw an error on bad
7144 debug info. */
3019eac3
DE
7145 }
7146 }
c0ab21c2 7147}
3019eac3 7148
6751ebae
TT
7149void
7150cutu_reader::keep ()
c0ab21c2 7151{
b0c7bfa9 7152 /* Done, clean up. */
6751ebae
TT
7153 gdb_assert (!dummy_p);
7154 if (m_new_cu != NULL)
348e048f 7155 {
e3beb21d
SM
7156 /* We know that m_this_cu->cu is set, since we are in the process of
7157 parsing the CU. */
7158 gdb_assert (m_this_cu->cu != nullptr);
7159 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7160
fcd3b13d 7161 /* Link this CU into read_in_chain. */
5989a64e
SM
7162 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7163 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
fcd3b13d 7164 /* The chain owns it now. */
c0ab21c2 7165 m_new_cu.release ();
348e048f 7166 }
dee91e82
DE
7167}
7168
18a8505e
AT
7169/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7170 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7171 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7172
7173 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7174 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7175
7176 We fill in THIS_CU->length.
7177
dee91e82 7178 THIS_CU->cu is always freed when done.
3019eac3 7179 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7180 to care whether it refers to the "main" CU or the DWO CU.
7181
7182 When parent_cu is passed, it is used to provide a default value for
7183 str_offsets_base and addr_base from the parent. */
dee91e82 7184
ab432490
SM
7185cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7186 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2
TT
7187 struct dwarf2_cu *parent_cu,
7188 struct dwo_file *dwo_file)
7189 : die_reader_specs {},
7190 m_this_cu (this_cu)
dee91e82
DE
7191{
7192 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7193 struct dwarf2_section_info *section = this_cu->section;
96b79293 7194 bfd *abfd = section->get_bfd_owner ();
33e80786 7195 struct dwarf2_section_info *abbrev_section;
d521ce57 7196 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7197
b4f54984 7198 if (dwarf_die_debug)
9d8780f0 7199 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7200 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7201 sect_offset_str (this_cu->sect_off));
09406207 7202
dee91e82
DE
7203 gdb_assert (this_cu->cu == NULL);
7204
33e80786
DE
7205 abbrev_section = (dwo_file != NULL
7206 ? &dwo_file->sections.abbrev
7207 : get_abbrev_section_for_cu (this_cu));
7208
dee91e82 7209 /* This is cheap if the section is already read in. */
96b79293 7210 section->read (objfile);
dee91e82 7211
9e021579 7212 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7213
9c541725 7214 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7215 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7216 &m_new_cu->header, section,
4bdcc0c1 7217 abbrev_section, info_ptr,
43988095
JK
7218 (this_cu->is_debug_types
7219 ? rcuh_kind::TYPE
7220 : rcuh_kind::COMPILE));
dee91e82 7221
18a8505e
AT
7222 if (parent_cu != nullptr)
7223 {
c0ab21c2
TT
7224 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7225 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7226 }
4057dfde 7227 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7228
7229 /* Skip dummy compilation units. */
7230 if (info_ptr >= begin_info_ptr + this_cu->length
7231 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7232 {
7233 dummy_p = true;
7234 return;
7235 }
72bf9492 7236
c0ab21c2 7237 m_abbrev_table_holder
86de1d91
TT
7238 = abbrev_table::read (objfile, abbrev_section,
7239 m_new_cu->header.abbrev_sect_off);
dee91e82 7240
c0ab21c2
TT
7241 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7242 m_abbrev_table_holder.get ());
3e225074 7243 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7244}
7245
0018ea6f
DE
7246\f
7247/* Type Unit Groups.
dee91e82 7248
0018ea6f
DE
7249 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7250 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7251 so that all types coming from the same compilation (.o file) are grouped
7252 together. A future step could be to put the types in the same symtab as
7253 the CU the types ultimately came from. */
ff013f42 7254
f4dc4d17
DE
7255static hashval_t
7256hash_type_unit_group (const void *item)
7257{
9a3c8263
SM
7258 const struct type_unit_group *tu_group
7259 = (const struct type_unit_group *) item;
f4dc4d17 7260
094b34ac 7261 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7262}
348e048f
DE
7263
7264static int
f4dc4d17 7265eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7266{
9a3c8263
SM
7267 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7268 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7269
094b34ac 7270 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7271}
348e048f 7272
f4dc4d17
DE
7273/* Allocate a hash table for type unit groups. */
7274
eaa5fa8b 7275static htab_up
298e9637 7276allocate_type_unit_groups_table ()
f4dc4d17 7277{
eaa5fa8b
TT
7278 return htab_up (htab_create_alloc (3,
7279 hash_type_unit_group,
7280 eq_type_unit_group,
7281 NULL, xcalloc, xfree));
f4dc4d17 7282}
dee91e82 7283
f4dc4d17
DE
7284/* Type units that don't have DW_AT_stmt_list are grouped into their own
7285 partial symtabs. We combine several TUs per psymtab to not let the size
7286 of any one psymtab grow too big. */
7287#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7288#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7289
094b34ac 7290/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7291 Create the type_unit_group object used to hold one or more TUs. */
7292
7293static struct type_unit_group *
094b34ac 7294create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7295{
5e22e966 7296 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7297 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7298 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7299 struct type_unit_group *tu_group;
f4dc4d17 7300
5989a64e 7301 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7302 struct type_unit_group);
094b34ac 7303 per_cu = &tu_group->per_cu;
1859c670 7304 per_cu->per_bfd = per_bfd;
f4dc4d17 7305
1859c670 7306 if (per_bfd->using_index)
094b34ac 7307 {
1859c670 7308 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7309 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7310 }
7311 else
7312 {
9c541725 7313 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7314 dwarf2_psymtab *pst;
528e1572 7315 std::string name;
094b34ac
DE
7316
7317 /* Give the symtab a useful name for debug purposes. */
7318 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7319 name = string_printf ("<type_units_%d>",
7320 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7321 else
528e1572 7322 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7323
7aa104c4 7324 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
6d94535f 7325 pst->anonymous = true;
094b34ac 7326 }
f4dc4d17 7327
094b34ac 7328 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7329 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7330
7331 return tu_group;
7332}
7333
094b34ac
DE
7334/* Look up the type_unit_group for type unit CU, and create it if necessary.
7335 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7336
7337static struct type_unit_group *
ff39bb5e 7338get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7339{
5e22e966 7340 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7341 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7342 struct type_unit_group *tu_group;
7343 void **slot;
7344 unsigned int line_offset;
7345 struct type_unit_group type_unit_group_for_lookup;
7346
5989a64e
SM
7347 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7348 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7349
7350 /* Do we need to create a new group, or can we use an existing one? */
7351
7352 if (stmt_list)
7353 {
7354 line_offset = DW_UNSND (stmt_list);
7355 ++tu_stats->nr_symtab_sharers;
7356 }
7357 else
7358 {
7359 /* Ugh, no stmt_list. Rare, but we have to handle it.
7360 We can do various things here like create one group per TU or
7361 spread them over multiple groups to split up the expansion work.
7362 To avoid worst case scenarios (too many groups or too large groups)
7363 we, umm, group them in bunches. */
7364 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7365 | (tu_stats->nr_stmt_less_type_units
7366 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7367 ++tu_stats->nr_stmt_less_type_units;
7368 }
7369
094b34ac 7370 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7371 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7372 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7373 &type_unit_group_for_lookup, INSERT);
7374 if (*slot != NULL)
7375 {
9a3c8263 7376 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7377 gdb_assert (tu_group != NULL);
7378 }
7379 else
7380 {
9c541725 7381 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7382 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7383 *slot = tu_group;
7384 ++tu_stats->nr_symtabs;
7385 }
7386
7387 return tu_group;
7388}
0018ea6f
DE
7389\f
7390/* Partial symbol tables. */
7391
7392/* Create a psymtab named NAME and assign it to PER_CU.
7393
7394 The caller must fill in the following details:
7395 dirname, textlow, texthigh. */
7396
891813be 7397static dwarf2_psymtab *
7aa104c4
SM
7398create_partial_symtab (dwarf2_per_cu_data *per_cu,
7399 dwarf2_per_objfile *per_objfile,
7400 const char *name)
0018ea6f 7401{
7aa104c4 7402 struct objfile *objfile = per_objfile->objfile;
891813be 7403 dwarf2_psymtab *pst;
0018ea6f 7404
9f4e76a4 7405 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7406
6d94535f 7407 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7408
7409 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7410 per_cu->v.psymtab = pst;
7411
7412 return pst;
7413}
7414
c0ab21c2 7415/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7416
7417static void
7418process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7419 const gdb_byte *info_ptr,
0018ea6f 7420 struct die_info *comp_unit_die,
c0ab21c2 7421 enum language pretend_language)
0018ea6f
DE
7422{
7423 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7424 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7425 struct objfile *objfile = per_objfile->objfile;
08feed99 7426 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7427 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7428 CORE_ADDR baseaddr;
7429 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7430 dwarf2_psymtab *pst;
3a2b436a 7431 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7432 const char *filename;
0018ea6f 7433
0018ea6f
DE
7434 gdb_assert (! per_cu->is_debug_types);
7435
c0ab21c2 7436 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7437
0018ea6f 7438 /* Allocate a new partial symbol table structure. */
2e927613
TV
7439 gdb::unique_xmalloc_ptr<char> debug_filename;
7440 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7441 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7442 if (filename == NULL)
0018ea6f 7443 filename = "";
2e927613
TV
7444 else if (strcmp (filename, artificial) == 0)
7445 {
7446 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7447 sect_offset_str (per_cu->sect_off),
7448 (char *) NULL));
2e927613
TV
7449 filename = debug_filename.get ();
7450 }
0018ea6f 7451
7aa104c4 7452 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7453
7454 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7455 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7456
b3b3bada 7457 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7458
7459 dwarf2_find_base_address (comp_unit_die, cu);
7460
7461 /* Possibly set the default values of LOWPC and HIGHPC from
7462 `DW_AT_ranges'. */
3a2b436a
JK
7463 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7464 &best_highpc, cu, pst);
7465 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7466 {
7467 CORE_ADDR low
7468 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7469 - baseaddr);
7470 CORE_ADDR high
7471 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7472 - baseaddr - 1);
7473 /* Store the contiguous range if it is not empty; it can be
7474 empty for CUs with no code. */
d320c2b5
TT
7475 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7476 low, high, pst);
79748972 7477 }
0018ea6f
DE
7478
7479 /* Check if comp unit has_children.
7480 If so, read the rest of the partial symbols from this comp unit.
7481 If not, there's no more debug_info for this comp unit. */
3e225074 7482 if (comp_unit_die->has_children)
0018ea6f
DE
7483 {
7484 struct partial_die_info *first_die;
7485 CORE_ADDR lowpc, highpc;
7486
7487 lowpc = ((CORE_ADDR) -1);
7488 highpc = ((CORE_ADDR) 0);
7489
7490 first_die = load_partial_dies (reader, info_ptr, 1);
7491
7492 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7493 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7494
7495 /* If we didn't find a lowpc, set it to highpc to avoid
7496 complaints from `maint check'. */
7497 if (lowpc == ((CORE_ADDR) -1))
7498 lowpc = highpc;
7499
7500 /* If the compilation unit didn't have an explicit address range,
7501 then use the information extracted from its child dies. */
e385593e 7502 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7503 {
7504 best_lowpc = lowpc;
7505 best_highpc = highpc;
7506 }
7507 }
4ae976d1 7508 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7509 best_lowpc + baseaddr)
7510 - baseaddr);
4ae976d1 7511 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7512 best_highpc + baseaddr)
7513 - baseaddr);
0018ea6f 7514
8763cede 7515 end_psymtab_common (objfile, pst);
0018ea6f 7516
ae640021 7517 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7518 {
7519 int i;
ae640021 7520 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7521
7522 /* Fill in 'dependencies' here; we fill in 'users' in a
7523 post-pass. */
7524 pst->number_of_dependencies = len;
a9342b62
TT
7525 pst->dependencies
7526 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7527 for (i = 0; i < len; ++i)
7528 {
7529 pst->dependencies[i]
7530 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7531 }
0018ea6f 7532
ae640021 7533 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7534 }
7535
7536 /* Get the list of files included in the current compilation unit,
7537 and build a psymtab for each of them. */
7538 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7539
b4f54984 7540 if (dwarf_read_debug)
b926417a
TT
7541 fprintf_unfiltered (gdb_stdlog,
7542 "Psymtab for %s unit @%s: %s - %s"
7543 ", %d global, %d static syms\n",
7544 per_cu->is_debug_types ? "type" : "comp",
7545 sect_offset_str (per_cu->sect_off),
7546 paddress (gdbarch, pst->text_low (objfile)),
7547 paddress (gdbarch, pst->text_high (objfile)),
7548 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7549}
7550
7551/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7552 Process compilation unit THIS_CU for a psymtab. */
7553
7554static void
ab432490
SM
7555process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7556 dwarf2_per_objfile *per_objfile,
135f5437 7557 bool want_partial_unit,
b93601f3 7558 enum language pretend_language)
0018ea6f
DE
7559{
7560 /* If this compilation unit was already read in, free the
7561 cached copy in order to read it in again. This is
7562 necessary because we skipped some symbols when we first
7563 read in the compilation unit (see load_partial_dies).
7564 This problem could be avoided, but the benefit is unclear. */
7565 if (this_cu->cu != NULL)
120ce1b5 7566 free_one_cached_comp_unit (this_cu, per_objfile);
0018ea6f 7567
ab432490 7568 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
c0ab21c2 7569
58990295
TV
7570 switch (reader.comp_unit_die->tag)
7571 {
7572 case DW_TAG_compile_unit:
7573 this_cu->unit_type = DW_UT_compile;
7574 break;
7575 case DW_TAG_partial_unit:
7576 this_cu->unit_type = DW_UT_partial;
7577 break;
7578 default:
7579 abort ();
7580 }
7581
c0ab21c2 7582 if (reader.dummy_p)
f1902523 7583 {
c0ab21c2 7584 /* Nothing. */
f1902523 7585 }
c0ab21c2 7586 else if (this_cu->is_debug_types)
3e225074
TT
7587 build_type_psymtabs_reader (&reader, reader.info_ptr,
7588 reader.comp_unit_die);
135f5437
TT
7589 else if (want_partial_unit
7590 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7591 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7592 reader.comp_unit_die,
c0ab21c2 7593 pretend_language);
0018ea6f 7594
58990295
TV
7595 this_cu->lang = this_cu->cu->language;
7596
0018ea6f 7597 /* Age out any secondary CUs. */
f6e649dd 7598 age_cached_comp_units (per_objfile);
0018ea6f 7599}
f4dc4d17
DE
7600
7601/* Reader function for build_type_psymtabs. */
7602
7603static void
7604build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7605 const gdb_byte *info_ptr,
3e225074 7606 struct die_info *type_unit_die)
f4dc4d17 7607{
5e22e966 7608 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7609 struct objfile *objfile = dwarf2_per_objfile->objfile;
7610 struct dwarf2_cu *cu = reader->cu;
7611 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7612 struct signatured_type *sig_type;
f4dc4d17
DE
7613 struct type_unit_group *tu_group;
7614 struct attribute *attr;
7615 struct partial_die_info *first_die;
7616 CORE_ADDR lowpc, highpc;
891813be 7617 dwarf2_psymtab *pst;
f4dc4d17 7618
0186c6a7
DE
7619 gdb_assert (per_cu->is_debug_types);
7620 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7621
3e225074 7622 if (! type_unit_die->has_children)
f4dc4d17
DE
7623 return;
7624
052c8bb8 7625 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7626 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7627
df07e2c7 7628 if (tu_group->tus == nullptr)
a8b3b8e9 7629 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7630 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7631
7632 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7aa104c4 7633 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
6d94535f 7634 pst->anonymous = true;
f4dc4d17
DE
7635
7636 first_die = load_partial_dies (reader, info_ptr, 1);
7637
7638 lowpc = (CORE_ADDR) -1;
7639 highpc = (CORE_ADDR) 0;
7640 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7641
8763cede 7642 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7643}
7644
73051182
DE
7645/* Struct used to sort TUs by their abbreviation table offset. */
7646
7647struct tu_abbrev_offset
7648{
b2bdb8cf
SM
7649 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7650 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7651 {}
7652
7653 signatured_type *sig_type;
73051182
DE
7654 sect_offset abbrev_offset;
7655};
7656
484cf504 7657/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7658
484cf504
TT
7659static bool
7660sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7661 const struct tu_abbrev_offset &b)
73051182 7662{
484cf504 7663 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7664}
7665
7666/* Efficiently read all the type units.
7667 This does the bulk of the work for build_type_psymtabs.
7668
7669 The efficiency is because we sort TUs by the abbrev table they use and
7670 only read each abbrev table once. In one program there are 200K TUs
7671 sharing 8K abbrev tables.
7672
7673 The main purpose of this function is to support building the
5989a64e 7674 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7675 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7676 can collapse the search space by grouping them by stmt_list.
7677 The savings can be significant, in the same program from above the 200K TUs
7678 share 8K stmt_list tables.
7679
7680 FUNC is expected to call get_type_unit_group, which will create the
7681 struct type_unit_group if necessary and add it to
5989a64e 7682 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7683
7684static void
ed2dc618 7685build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7686{
5989a64e 7687 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7688 abbrev_table_up abbrev_table;
73051182 7689 sect_offset abbrev_offset;
73051182
DE
7690
7691 /* It's up to the caller to not call us multiple times. */
5989a64e 7692 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7693
5989a64e 7694 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7695 return;
7696
7697 /* TUs typically share abbrev tables, and there can be way more TUs than
7698 abbrev tables. Sort by abbrev table to reduce the number of times we
7699 read each abbrev table in.
7700 Alternatives are to punt or to maintain a cache of abbrev tables.
7701 This is simpler and efficient enough for now.
7702
7703 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7704 symtab to use). Typically TUs with the same abbrev offset have the same
7705 stmt_list value too so in practice this should work well.
7706
7707 The basic algorithm here is:
7708
7709 sort TUs by abbrev table
7710 for each TU with same abbrev table:
7711 read abbrev table if first user
7712 read TU top level DIE
7713 [IWBN if DWO skeletons had DW_AT_stmt_list]
7714 call FUNC */
7715
b4f54984 7716 if (dwarf_read_debug)
73051182
DE
7717 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7718
7719 /* Sort in a separate table to maintain the order of all_type_units
7720 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7721 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7722 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7723
5989a64e 7724 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7725 sorted_by_abbrev.emplace_back
7726 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7727 sig_type->per_cu.section,
7728 sig_type->per_cu.sect_off));
73051182 7729
484cf504
TT
7730 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7731 sort_tu_by_abbrev_offset);
73051182 7732
9c541725 7733 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7734
b2bdb8cf 7735 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7736 {
73051182
DE
7737 /* Switch to the next abbrev table if necessary. */
7738 if (abbrev_table == NULL
b2bdb8cf 7739 || tu.abbrev_offset != abbrev_offset)
73051182 7740 {
b2bdb8cf 7741 abbrev_offset = tu.abbrev_offset;
73051182 7742 abbrev_table =
86de1d91 7743 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7744 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7745 abbrev_offset);
73051182
DE
7746 ++tu_stats->nr_uniq_abbrev_tables;
7747 }
7748
ab432490
SM
7749 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7750 abbrev_table.get (), 0, false);
c0ab21c2
TT
7751 if (!reader.dummy_p)
7752 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7753 reader.comp_unit_die);
73051182 7754 }
6aa5f3a6 7755}
73051182 7756
6aa5f3a6
DE
7757/* Print collected type unit statistics. */
7758
7759static void
ed2dc618 7760print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7761{
5989a64e 7762 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7763
7764 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7765 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7766 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7767 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7768 tu_stats->nr_uniq_abbrev_tables);
7769 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7770 tu_stats->nr_symtabs);
7771 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7772 tu_stats->nr_symtab_sharers);
7773 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7774 tu_stats->nr_stmt_less_type_units);
7775 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7776 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7777}
7778
f4dc4d17
DE
7779/* Traversal function for build_type_psymtabs. */
7780
7781static int
7782build_type_psymtab_dependencies (void **slot, void *info)
7783{
ed2dc618
SM
7784 struct dwarf2_per_objfile *dwarf2_per_objfile
7785 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7786 struct objfile *objfile = dwarf2_per_objfile->objfile;
7787 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7788 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7789 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7790 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7791 int i;
7792
7793 gdb_assert (len > 0);
197400e8 7794 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7795
7796 pst->number_of_dependencies = len;
a9342b62 7797 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7798 for (i = 0; i < len; ++i)
f4dc4d17 7799 {
df07e2c7 7800 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7801 gdb_assert (iter->per_cu.is_debug_types);
7802 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7803 iter->type_unit_group = tu_group;
f4dc4d17
DE
7804 }
7805
df07e2c7
AB
7806 delete tu_group->tus;
7807 tu_group->tus = nullptr;
348e048f
DE
7808
7809 return 1;
7810}
7811
7812/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7813 Build partial symbol tables for the .debug_types comp-units. */
7814
7815static void
ed2dc618 7816build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7817{
ed2dc618 7818 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7819 return;
7820
ed2dc618 7821 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7822}
f4dc4d17 7823
6aa5f3a6
DE
7824/* Traversal function for process_skeletonless_type_unit.
7825 Read a TU in a DWO file and build partial symbols for it. */
7826
7827static int
7828process_skeletonless_type_unit (void **slot, void *info)
7829{
7830 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7831 struct dwarf2_per_objfile *dwarf2_per_objfile
7832 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7833 struct signatured_type find_entry, *entry;
7834
7835 /* If this TU doesn't exist in the global table, add it and read it in. */
7836
5989a64e
SM
7837 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7838 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7839
7840 find_entry.signature = dwo_unit->signature;
5989a64e 7841 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7842 &find_entry, INSERT);
6aa5f3a6
DE
7843 /* If we've already seen this type there's nothing to do. What's happening
7844 is we're doing our own version of comdat-folding here. */
7845 if (*slot != NULL)
7846 return 1;
7847
7848 /* This does the job that create_all_type_units would have done for
7849 this TU. */
ed2dc618
SM
7850 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7851 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7852 *slot = entry;
7853
7854 /* This does the job that build_type_psymtabs_1 would have done. */
ab432490 7855 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
7856 if (!reader.dummy_p)
7857 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7858 reader.comp_unit_die);
6aa5f3a6
DE
7859
7860 return 1;
7861}
7862
7863/* Traversal function for process_skeletonless_type_units. */
7864
7865static int
7866process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7867{
7868 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7869
7870 if (dwo_file->tus != NULL)
b0b6a987
TT
7871 htab_traverse_noresize (dwo_file->tus.get (),
7872 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7873
7874 return 1;
7875}
7876
7877/* Scan all TUs of DWO files, verifying we've processed them.
7878 This is needed in case a TU was emitted without its skeleton.
7879 Note: This can't be done until we know what all the DWO files are. */
7880
7881static void
ed2dc618 7882process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7883{
7884 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7885 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7886 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7887 {
5989a64e 7888 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7889 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7890 dwarf2_per_objfile);
6aa5f3a6 7891 }
348e048f
DE
7892}
7893
ed2dc618 7894/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7895
7896static void
ed2dc618 7897set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7898{
5989a64e 7899 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7900 {
891813be 7901 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7902
36586728
TT
7903 if (pst == NULL)
7904 continue;
7905
b76e467d 7906 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7907 {
7908 /* Set the 'user' field only if it is not already set. */
7909 if (pst->dependencies[j]->user == NULL)
7910 pst->dependencies[j]->user = pst;
7911 }
7912 }
7913}
7914
93311388
DE
7915/* Build the partial symbol table by doing a quick pass through the
7916 .debug_info and .debug_abbrev sections. */
72bf9492 7917
93311388 7918static void
ed2dc618 7919dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7920{
ed2dc618 7921 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7922
b4f54984 7923 if (dwarf_read_debug)
45cfd468
DE
7924 {
7925 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7926 objfile_name (objfile));
45cfd468
DE
7927 }
7928
76935768 7929 scoped_restore restore_reading_psyms
5989a64e 7930 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7931 true);
98bfdba5 7932
5989a64e 7933 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7934
93311388
DE
7935 /* Any cached compilation units will be linked by the per-objfile
7936 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7937 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7938
ed2dc618 7939 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7940
ed2dc618 7941 create_all_comp_units (dwarf2_per_objfile);
c906108c 7942
60606b2c
TT
7943 /* Create a temporary address map on a temporary obstack. We later
7944 copy this to the final obstack. */
8268c778 7945 auto_obstack temp_obstack;
791afaa2
TT
7946
7947 scoped_restore save_psymtabs_addrmap
d320c2b5 7948 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7949 addrmap_create_mutable (&temp_obstack));
72bf9492 7950
5989a64e 7951 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7952 {
7953 if (per_cu->v.psymtab != NULL)
7954 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7955 continue;
ab432490
SM
7956 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7957 language_minimal);
3d5afab3 7958 }
ff013f42 7959
6aa5f3a6 7960 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7961 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7962
7963 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7964 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7965 {
5989a64e 7966 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7967 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7968 }
7969
b4f54984 7970 if (dwarf_read_debug)
ed2dc618 7971 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7972
ed2dc618 7973 set_partial_user (dwarf2_per_objfile);
95554aad 7974
d320c2b5
TT
7975 objfile->partial_symtabs->psymtabs_addrmap
7976 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7977 objfile->partial_symtabs->obstack ());
791afaa2
TT
7978 /* At this point we want to keep the address map. */
7979 save_psymtabs_addrmap.release ();
ff013f42 7980
b4f54984 7981 if (dwarf_read_debug)
45cfd468 7982 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7983 objfile_name (objfile));
ae038cb0
DJ
7984}
7985
dee91e82
DE
7986/* Load the partial DIEs for a secondary CU into memory.
7987 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7988
dee91e82 7989static void
ab432490
SM
7990load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7991 dwarf2_per_objfile *per_objfile)
dee91e82 7992{
ab432490 7993 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
c0ab21c2
TT
7994
7995 if (!reader.dummy_p)
7996 {
7997 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7998 language_minimal);
7999
8000 /* Check if comp unit has_children.
8001 If so, read the rest of the partial symbols from this comp unit.
8002 If not, there's no more debug_info for this comp unit. */
3e225074 8003 if (reader.comp_unit_die->has_children)
c0ab21c2 8004 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8005
8006 reader.keep ();
c0ab21c2 8007 }
ae038cb0
DJ
8008}
8009
ae038cb0 8010static void
ed2dc618 8011read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8012 struct dwarf2_section_info *section,
f1902523 8013 struct dwarf2_section_info *abbrev_section,
b76e467d 8014 unsigned int is_dwz)
ae038cb0 8015{
d521ce57 8016 const gdb_byte *info_ptr;
ed2dc618 8017 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8018
b4f54984 8019 if (dwarf_read_debug)
bf6af496 8020 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8021 section->get_name (),
8022 section->get_file_name ());
bf6af496 8023
96b79293 8024 section->read (objfile);
ae038cb0 8025
36586728 8026 info_ptr = section->buffer;
6e70227d 8027
36586728 8028 while (info_ptr < section->buffer + section->size)
ae038cb0 8029 {
ae038cb0 8030 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8031
9c541725 8032 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8033
f1902523 8034 comp_unit_head cu_header;
ed2dc618
SM
8035 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8036 abbrev_section, info_ptr,
8037 rcuh_kind::COMPILE);
ae038cb0
DJ
8038
8039 /* Save the compilation unit for later lookup. */
f1902523 8040 if (cu_header.unit_type != DW_UT_type)
5989a64e 8041 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8042 else
8043 {
5989a64e 8044 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8045 sig_type->signature = cu_header.signature;
8046 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8047 this_cu = &sig_type->per_cu;
8048 }
8049 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8050 this_cu->sect_off = sect_off;
f1902523 8051 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8052 this_cu->is_dwz = is_dwz;
8a0459fd 8053 this_cu->section = section;
ae038cb0 8054
5989a64e 8055 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8056
8057 info_ptr = info_ptr + this_cu->length;
8058 }
36586728
TT
8059}
8060
8061/* Create a list of all compilation units in OBJFILE.
8062 This is only done for -readnow and building partial symtabs. */
8063
8064static void
ed2dc618 8065create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8066{
5989a64e
SM
8067 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8068 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8069 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8070
c3699833 8071 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8072 if (dwz != NULL)
ed2dc618 8073 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8074 1);
c906108c
SS
8075}
8076
5734ee8b 8077/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8078 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8079 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8080 DW_AT_ranges). See the comments of add_partial_subprogram on how
8081 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8082
72bf9492
DJ
8083static void
8084scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8085 CORE_ADDR *highpc, int set_addrmap,
8086 struct dwarf2_cu *cu)
c906108c 8087{
72bf9492 8088 struct partial_die_info *pdi;
c906108c 8089
91c24f0a
DC
8090 /* Now, march along the PDI's, descending into ones which have
8091 interesting children but skipping the children of the other ones,
8092 until we reach the end of the compilation unit. */
c906108c 8093
72bf9492 8094 pdi = first_die;
91c24f0a 8095
72bf9492
DJ
8096 while (pdi != NULL)
8097 {
52356b79 8098 pdi->fixup (cu);
c906108c 8099
f55ee35c 8100 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8101 children, so we need to look at them. Ditto for anonymous
8102 enums. */
933c6fe4 8103
72bf9492 8104 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8105 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8106 || pdi->tag == DW_TAG_imported_unit
8107 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8108 {
72bf9492 8109 switch (pdi->tag)
c906108c
SS
8110 {
8111 case DW_TAG_subprogram:
b1dc1806 8112 case DW_TAG_inlined_subroutine:
cdc07690 8113 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8114 break;
72929c62 8115 case DW_TAG_constant:
c906108c
SS
8116 case DW_TAG_variable:
8117 case DW_TAG_typedef:
91c24f0a 8118 case DW_TAG_union_type:
317d2668
TV
8119 if (!pdi->is_declaration
8120 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8121 {
72bf9492 8122 add_partial_symbol (pdi, cu);
63d06c5c
DC
8123 }
8124 break;
c906108c 8125 case DW_TAG_class_type:
680b30c7 8126 case DW_TAG_interface_type:
c906108c 8127 case DW_TAG_structure_type:
72bf9492 8128 if (!pdi->is_declaration)
c906108c 8129 {
72bf9492 8130 add_partial_symbol (pdi, cu);
c906108c 8131 }
b7fee5a3
KS
8132 if ((cu->language == language_rust
8133 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8134 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8135 set_addrmap, cu);
c906108c 8136 break;
91c24f0a 8137 case DW_TAG_enumeration_type:
72bf9492
DJ
8138 if (!pdi->is_declaration)
8139 add_partial_enumeration (pdi, cu);
c906108c
SS
8140 break;
8141 case DW_TAG_base_type:
a02abb62 8142 case DW_TAG_subrange_type:
c906108c 8143 /* File scope base type definitions are added to the partial
c5aa993b 8144 symbol table. */
72bf9492 8145 add_partial_symbol (pdi, cu);
c906108c 8146 break;
d9fa45fe 8147 case DW_TAG_namespace:
cdc07690 8148 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8149 break;
5d7cb8df 8150 case DW_TAG_module:
59c35742
AB
8151 if (!pdi->is_declaration)
8152 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8153 break;
95554aad
TT
8154 case DW_TAG_imported_unit:
8155 {
8156 struct dwarf2_per_cu_data *per_cu;
8157
f4dc4d17
DE
8158 /* For now we don't handle imported units in type units. */
8159 if (cu->per_cu->is_debug_types)
8160 {
8161 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8162 " supported in type units [in module %s]"),
5e22e966 8163 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8164 }
8165
e3b94546 8166 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8167 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8168
8169 /* Go read the partial unit, if needed. */
8170 if (per_cu->v.psymtab == NULL)
ab432490
SM
8171 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8172 cu->language);
95554aad 8173
ae640021 8174 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8175 }
8176 break;
74921315
KS
8177 case DW_TAG_imported_declaration:
8178 add_partial_symbol (pdi, cu);
8179 break;
c906108c
SS
8180 default:
8181 break;
8182 }
8183 }
8184
72bf9492
DJ
8185 /* If the die has a sibling, skip to the sibling. */
8186
8187 pdi = pdi->die_sibling;
8188 }
8189}
8190
8191/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8192
72bf9492 8193 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8194 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8195 Enumerators are an exception; they use the scope of their parent
8196 enumeration type, i.e. the name of the enumeration type is not
8197 prepended to the enumerator.
91c24f0a 8198
72bf9492
DJ
8199 There are two complexities. One is DW_AT_specification; in this
8200 case "parent" means the parent of the target of the specification,
8201 instead of the direct parent of the DIE. The other is compilers
8202 which do not emit DW_TAG_namespace; in this case we try to guess
8203 the fully qualified name of structure types from their members'
8204 linkage names. This must be done using the DIE's children rather
8205 than the children of any DW_AT_specification target. We only need
8206 to do this for structures at the top level, i.e. if the target of
8207 any DW_AT_specification (if any; otherwise the DIE itself) does not
8208 have a parent. */
8209
8210/* Compute the scope prefix associated with PDI's parent, in
8211 compilation unit CU. The result will be allocated on CU's
8212 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8213 field. NULL is returned if no prefix is necessary. */
15d034d0 8214static const char *
72bf9492
DJ
8215partial_die_parent_scope (struct partial_die_info *pdi,
8216 struct dwarf2_cu *cu)
8217{
15d034d0 8218 const char *grandparent_scope;
72bf9492 8219 struct partial_die_info *parent, *real_pdi;
91c24f0a 8220
72bf9492
DJ
8221 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8222 then this means the parent of the specification DIE. */
8223
8224 real_pdi = pdi;
72bf9492 8225 while (real_pdi->has_specification)
fb816e8b 8226 {
122cf0f2
AB
8227 auto res = find_partial_die (real_pdi->spec_offset,
8228 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8229 real_pdi = res.pdi;
8230 cu = res.cu;
8231 }
72bf9492
DJ
8232
8233 parent = real_pdi->die_parent;
8234 if (parent == NULL)
8235 return NULL;
8236
8237 if (parent->scope_set)
8238 return parent->scope;
8239
52356b79 8240 parent->fixup (cu);
72bf9492 8241
10b3939b 8242 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8243
acebe513
UW
8244 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8245 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8246 Work around this problem here. */
8247 if (cu->language == language_cplus
6e70227d 8248 && parent->tag == DW_TAG_namespace
acebe513
UW
8249 && strcmp (parent->name, "::") == 0
8250 && grandparent_scope == NULL)
8251 {
8252 parent->scope = NULL;
8253 parent->scope_set = 1;
8254 return NULL;
8255 }
8256
0a4b0913 8257 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8258 if (pdi->tag == DW_TAG_enumerator)
8259 /* Enumerators should not get the name of the enumeration as a prefix. */
8260 parent->scope = grandparent_scope;
8261 else if (parent->tag == DW_TAG_namespace
f55ee35c 8262 || parent->tag == DW_TAG_module
72bf9492
DJ
8263 || parent->tag == DW_TAG_structure_type
8264 || parent->tag == DW_TAG_class_type
680b30c7 8265 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8266 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8267 || parent->tag == DW_TAG_enumeration_type
8268 || (cu->language == language_fortran
8269 && parent->tag == DW_TAG_subprogram
8270 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8271 {
8272 if (grandparent_scope == NULL)
8273 parent->scope = parent->name;
8274 else
3e43a32a
MS
8275 parent->scope = typename_concat (&cu->comp_unit_obstack,
8276 grandparent_scope,
f55ee35c 8277 parent->name, 0, cu);
72bf9492 8278 }
72bf9492
DJ
8279 else
8280 {
8281 /* FIXME drow/2004-04-01: What should we be doing with
8282 function-local names? For partial symbols, we should probably be
8283 ignoring them. */
fa9c3fa0
TT
8284 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8285 dwarf_tag_name (parent->tag),
8286 sect_offset_str (pdi->sect_off));
72bf9492 8287 parent->scope = grandparent_scope;
c906108c
SS
8288 }
8289
72bf9492
DJ
8290 parent->scope_set = 1;
8291 return parent->scope;
8292}
8293
8294/* Return the fully scoped name associated with PDI, from compilation unit
8295 CU. The result will be allocated with malloc. */
4568ecf9 8296
43816ebc 8297static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8298partial_die_full_name (struct partial_die_info *pdi,
8299 struct dwarf2_cu *cu)
8300{
15d034d0 8301 const char *parent_scope;
72bf9492 8302
98bfdba5
PA
8303 /* If this is a template instantiation, we can not work out the
8304 template arguments from partial DIEs. So, unfortunately, we have
8305 to go through the full DIEs. At least any work we do building
8306 types here will be reused if full symbols are loaded later. */
8307 if (pdi->has_template_arguments)
8308 {
52356b79 8309 pdi->fixup (cu);
98bfdba5
PA
8310
8311 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8312 {
8313 struct die_info *die;
8314 struct attribute attr;
8315 struct dwarf2_cu *ref_cu = cu;
8316
b64f50a1 8317 /* DW_FORM_ref_addr is using section offset. */
b4069958 8318 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8319 attr.form = DW_FORM_ref_addr;
9c541725 8320 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8321 die = follow_die_ref (NULL, &attr, &ref_cu);
8322
43816ebc 8323 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8324 }
8325 }
8326
72bf9492
DJ
8327 parent_scope = partial_die_parent_scope (pdi, cu);
8328 if (parent_scope == NULL)
8329 return NULL;
8330 else
43816ebc
TT
8331 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8332 pdi->name, 0, cu));
c906108c
SS
8333}
8334
8335static void
72bf9492 8336add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8337{
5e22e966 8338 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8339 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8340 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8341 CORE_ADDR addr = 0;
15d034d0 8342 const char *actual_name = NULL;
e142c38c
DJ
8343 CORE_ADDR baseaddr;
8344
b3b3bada 8345 baseaddr = objfile->text_section_offset ();
c906108c 8346
43816ebc
TT
8347 gdb::unique_xmalloc_ptr<char> built_actual_name
8348 = partial_die_full_name (pdi, cu);
15d034d0 8349 if (built_actual_name != NULL)
43816ebc 8350 actual_name = built_actual_name.get ();
63d06c5c 8351
72bf9492
DJ
8352 if (actual_name == NULL)
8353 actual_name = pdi->name;
8354
76e288d1
TT
8355 partial_symbol psymbol;
8356 memset (&psymbol, 0, sizeof (psymbol));
8357 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8358 psymbol.ginfo.section = -1;
8359
8360 /* The code below indicates that the psymbol should be installed by
8361 setting this. */
8362 gdb::optional<psymbol_placement> where;
8363
c906108c
SS
8364 switch (pdi->tag)
8365 {
b1dc1806 8366 case DW_TAG_inlined_subroutine:
c906108c 8367 case DW_TAG_subprogram:
79748972
TT
8368 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8369 - baseaddr);
0a4b0913
AB
8370 if (pdi->is_external
8371 || cu->language == language_ada
8372 || (cu->language == language_fortran
8373 && pdi->die_parent != NULL
8374 && pdi->die_parent->tag == DW_TAG_subprogram))
8375 {
8376 /* Normally, only "external" DIEs are part of the global scope.
8377 But in Ada and Fortran, we want to be able to access nested
8378 procedures globally. So all Ada and Fortran subprograms are
8379 stored in the global scope. */
76e288d1 8380 where = psymbol_placement::GLOBAL;
c906108c
SS
8381 }
8382 else
76e288d1
TT
8383 where = psymbol_placement::STATIC;
8384
8385 psymbol.domain = VAR_DOMAIN;
8386 psymbol.aclass = LOC_BLOCK;
8387 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8388 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8389
8390 if (pdi->main_subprogram && actual_name != NULL)
8391 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8392 break;
72929c62 8393 case DW_TAG_constant:
76e288d1
TT
8394 psymbol.domain = VAR_DOMAIN;
8395 psymbol.aclass = LOC_STATIC;
8396 where = (pdi->is_external
8397 ? psymbol_placement::GLOBAL
8398 : psymbol_placement::STATIC);
72929c62 8399 break;
c906108c 8400 case DW_TAG_variable:
95554aad
TT
8401 if (pdi->d.locdesc)
8402 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8403
95554aad 8404 if (pdi->d.locdesc
caac4577 8405 && addr == 0
5989a64e 8406 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8407 {
8408 /* A global or static variable may also have been stripped
8409 out by the linker if unused, in which case its address
8410 will be nullified; do not add such variables into partial
8411 symbol table then. */
8412 }
8413 else if (pdi->is_external)
c906108c
SS
8414 {
8415 /* Global Variable.
8416 Don't enter into the minimal symbol tables as there is
8417 a minimal symbol table entry from the ELF symbols already.
8418 Enter into partial symbol table if it has a location
8419 descriptor or a type.
8420 If the location descriptor is missing, new_symbol will create
8421 a LOC_UNRESOLVED symbol, the address of the variable will then
8422 be determined from the minimal symbol table whenever the variable
8423 is referenced.
8424 The address for the partial symbol table entry is not
8425 used by GDB, but it comes in handy for debugging partial symbol
8426 table building. */
8427
95554aad 8428 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8429 {
8430 psymbol.domain = VAR_DOMAIN;
8431 psymbol.aclass = LOC_STATIC;
8432 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8433 psymbol.ginfo.value.address = addr;
8434 where = psymbol_placement::GLOBAL;
8435 }
c906108c
SS
8436 }
8437 else
8438 {
ff908ebf
AW
8439 int has_loc = pdi->d.locdesc != NULL;
8440
8441 /* Static Variable. Skip symbols whose value we cannot know (those
8442 without location descriptors or constant values). */
8443 if (!has_loc && !pdi->has_const_value)
43816ebc 8444 return;
ff908ebf 8445
76e288d1
TT
8446 psymbol.domain = VAR_DOMAIN;
8447 psymbol.aclass = LOC_STATIC;
8448 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8449 if (has_loc)
8450 psymbol.ginfo.value.address = addr;
8451 where = psymbol_placement::STATIC;
c906108c
SS
8452 }
8453 break;
8454 case DW_TAG_typedef:
8455 case DW_TAG_base_type:
a02abb62 8456 case DW_TAG_subrange_type:
76e288d1
TT
8457 psymbol.domain = VAR_DOMAIN;
8458 psymbol.aclass = LOC_TYPEDEF;
8459 where = psymbol_placement::STATIC;
c906108c 8460 break;
74921315 8461 case DW_TAG_imported_declaration:
72bf9492 8462 case DW_TAG_namespace:
76e288d1
TT
8463 psymbol.domain = VAR_DOMAIN;
8464 psymbol.aclass = LOC_TYPEDEF;
8465 where = psymbol_placement::GLOBAL;
72bf9492 8466 break;
530e8392 8467 case DW_TAG_module:
a5fd13a9
BH
8468 /* With Fortran 77 there might be a "BLOCK DATA" module
8469 available without any name. If so, we skip the module as it
8470 doesn't bring any value. */
8471 if (actual_name != nullptr)
76e288d1
TT
8472 {
8473 psymbol.domain = MODULE_DOMAIN;
8474 psymbol.aclass = LOC_TYPEDEF;
8475 where = psymbol_placement::GLOBAL;
8476 }
530e8392 8477 break;
c906108c 8478 case DW_TAG_class_type:
680b30c7 8479 case DW_TAG_interface_type:
c906108c
SS
8480 case DW_TAG_structure_type:
8481 case DW_TAG_union_type:
8482 case DW_TAG_enumeration_type:
fa4028e9
JB
8483 /* Skip external references. The DWARF standard says in the section
8484 about "Structure, Union, and Class Type Entries": "An incomplete
8485 structure, union or class type is represented by a structure,
8486 union or class entry that does not have a byte size attribute
8487 and that has a DW_AT_declaration attribute." */
8488 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8489 return;
fa4028e9 8490
63d06c5c
DC
8491 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8492 static vs. global. */
76e288d1
TT
8493 psymbol.domain = STRUCT_DOMAIN;
8494 psymbol.aclass = LOC_TYPEDEF;
8495 where = (cu->language == language_cplus
8496 ? psymbol_placement::GLOBAL
8497 : psymbol_placement::STATIC);
c906108c
SS
8498 break;
8499 case DW_TAG_enumerator:
76e288d1
TT
8500 psymbol.domain = VAR_DOMAIN;
8501 psymbol.aclass = LOC_CONST;
8502 where = (cu->language == language_cplus
8503 ? psymbol_placement::GLOBAL
8504 : psymbol_placement::STATIC);
c906108c
SS
8505 break;
8506 default:
8507 break;
8508 }
76e288d1
TT
8509
8510 if (where.has_value ())
8511 {
f049a313
TT
8512 if (built_actual_name != nullptr)
8513 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8514 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8515 psymbol.ginfo.set_linkage_name (actual_name);
8516 else
8517 {
8518 psymbol.ginfo.set_demangled_name (actual_name,
8519 &objfile->objfile_obstack);
8520 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8521 }
76e288d1
TT
8522 add_psymbol_to_list (psymbol, *where, objfile);
8523 }
c906108c
SS
8524}
8525
5c4e30ca
DC
8526/* Read a partial die corresponding to a namespace; also, add a symbol
8527 corresponding to that namespace to the symbol table. NAMESPACE is
8528 the name of the enclosing namespace. */
91c24f0a 8529
72bf9492
DJ
8530static void
8531add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8532 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8533 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8534{
72bf9492 8535 /* Add a symbol for the namespace. */
e7c27a73 8536
72bf9492 8537 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8538
8539 /* Now scan partial symbols in that namespace. */
8540
91c24f0a 8541 if (pdi->has_children)
cdc07690 8542 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8543}
8544
5d7cb8df
JK
8545/* Read a partial die corresponding to a Fortran module. */
8546
8547static void
8548add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8549 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8550{
530e8392
KB
8551 /* Add a symbol for the namespace. */
8552
8553 add_partial_symbol (pdi, cu);
8554
f55ee35c 8555 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8556
8557 if (pdi->has_children)
cdc07690 8558 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8559}
8560
b1dc1806
XR
8561/* Read a partial die corresponding to a subprogram or an inlined
8562 subprogram and create a partial symbol for that subprogram.
8563 When the CU language allows it, this routine also defines a partial
8564 symbol for each nested subprogram that this subprogram contains.
8565 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8566 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8567
cdc07690
YQ
8568 PDI may also be a lexical block, in which case we simply search
8569 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8570 Again, this is only performed when the CU language allows this
8571 type of definitions. */
8572
8573static void
8574add_partial_subprogram (struct partial_die_info *pdi,
8575 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8576 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8577{
b1dc1806 8578 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8579 {
8580 if (pdi->has_pc_info)
8581 {
8582 if (pdi->lowpc < *lowpc)
8583 *lowpc = pdi->lowpc;
8584 if (pdi->highpc > *highpc)
8585 *highpc = pdi->highpc;
cdc07690 8586 if (set_addrmap)
5734ee8b 8587 {
5e22e966 8588 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8589 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8590 CORE_ADDR baseaddr;
b926417a
TT
8591 CORE_ADDR this_highpc;
8592 CORE_ADDR this_lowpc;
5734ee8b 8593
b3b3bada 8594 baseaddr = objfile->text_section_offset ();
b926417a
TT
8595 this_lowpc
8596 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8597 pdi->lowpc + baseaddr)
8598 - baseaddr);
8599 this_highpc
8600 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8601 pdi->highpc + baseaddr)
8602 - baseaddr);
d320c2b5 8603 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8604 this_lowpc, this_highpc - 1,
9291a0cd 8605 cu->per_cu->v.psymtab);
5734ee8b 8606 }
481860b3
GB
8607 }
8608
8609 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8610 {
bc30ff58 8611 if (!pdi->is_declaration)
e8d05480
JB
8612 /* Ignore subprogram DIEs that do not have a name, they are
8613 illegal. Do not emit a complaint at this point, we will
8614 do so when we convert this psymtab into a symtab. */
8615 if (pdi->name)
8616 add_partial_symbol (pdi, cu);
bc30ff58
JB
8617 }
8618 }
6e70227d 8619
bc30ff58
JB
8620 if (! pdi->has_children)
8621 return;
8622
0a4b0913 8623 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8624 {
8625 pdi = pdi->die_child;
8626 while (pdi != NULL)
8627 {
52356b79 8628 pdi->fixup (cu);
bc30ff58 8629 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8630 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8631 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8632 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8633 pdi = pdi->die_sibling;
8634 }
8635 }
8636}
8637
91c24f0a
DC
8638/* Read a partial die corresponding to an enumeration type. */
8639
72bf9492
DJ
8640static void
8641add_partial_enumeration (struct partial_die_info *enum_pdi,
8642 struct dwarf2_cu *cu)
91c24f0a 8643{
72bf9492 8644 struct partial_die_info *pdi;
91c24f0a
DC
8645
8646 if (enum_pdi->name != NULL)
72bf9492
DJ
8647 add_partial_symbol (enum_pdi, cu);
8648
8649 pdi = enum_pdi->die_child;
8650 while (pdi)
91c24f0a 8651 {
72bf9492 8652 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8653 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8654 else
72bf9492
DJ
8655 add_partial_symbol (pdi, cu);
8656 pdi = pdi->die_sibling;
91c24f0a 8657 }
91c24f0a
DC
8658}
8659
6caca83c
CC
8660/* Return the initial uleb128 in the die at INFO_PTR. */
8661
8662static unsigned int
d521ce57 8663peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8664{
8665 unsigned int bytes_read;
8666
8667 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8668}
8669
685af9cd
TT
8670/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8671 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8672
4bb7a0a7
DJ
8673 Return the corresponding abbrev, or NULL if the number is zero (indicating
8674 an empty DIE). In either case *BYTES_READ will be set to the length of
8675 the initial number. */
8676
8677static struct abbrev_info *
685af9cd
TT
8678peek_die_abbrev (const die_reader_specs &reader,
8679 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8680{
685af9cd 8681 dwarf2_cu *cu = reader.cu;
5e22e966 8682 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8683 unsigned int abbrev_number
8684 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8685
8686 if (abbrev_number == 0)
8687 return NULL;
8688
685af9cd 8689 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8690 if (!abbrev)
8691 {
422b9917 8692 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8693 " at offset %s [in module %s]"),
422b9917 8694 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8695 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8696 }
8697
8698 return abbrev;
8699}
8700
93311388
DE
8701/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8702 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8703 DIE. Any children of the skipped DIEs will also be skipped. */
8704
d521ce57
TT
8705static const gdb_byte *
8706skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8707{
4bb7a0a7
DJ
8708 while (1)
8709 {
685af9cd
TT
8710 unsigned int bytes_read;
8711 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8712
4bb7a0a7
DJ
8713 if (abbrev == NULL)
8714 return info_ptr + bytes_read;
8715 else
dee91e82 8716 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8717 }
8718}
8719
93311388
DE
8720/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8721 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8722 abbrev corresponding to that skipped uleb128 should be passed in
8723 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8724 children. */
8725
d521ce57
TT
8726static const gdb_byte *
8727skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8728 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8729{
8730 unsigned int bytes_read;
8731 struct attribute attr;
dee91e82
DE
8732 bfd *abfd = reader->abfd;
8733 struct dwarf2_cu *cu = reader->cu;
d521ce57 8734 const gdb_byte *buffer = reader->buffer;
f664829e 8735 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8736 unsigned int form, i;
8737
8738 for (i = 0; i < abbrev->num_attrs; i++)
8739 {
8740 /* The only abbrev we care about is DW_AT_sibling. */
8741 if (abbrev->attrs[i].name == DW_AT_sibling)
8742 {
18a8505e
AT
8743 bool ignored;
8744 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8745 &ignored);
4bb7a0a7 8746 if (attr.form == DW_FORM_ref_addr)
b98664d3 8747 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8748 else
b9502d3f 8749 {
0826b30a 8750 sect_offset off = attr.get_ref_die_offset ();
9c541725 8751 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8752
8753 if (sibling_ptr < info_ptr)
b98664d3 8754 complaint (_("DW_AT_sibling points backwards"));
22869d73 8755 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8756 reader->die_section->overflow_complaint ();
b9502d3f
WN
8757 else
8758 return sibling_ptr;
8759 }
4bb7a0a7
DJ
8760 }
8761
8762 /* If it isn't DW_AT_sibling, skip this attribute. */
8763 form = abbrev->attrs[i].form;
8764 skip_attribute:
8765 switch (form)
8766 {
4bb7a0a7 8767 case DW_FORM_ref_addr:
ae411497
TT
8768 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8769 and later it is offset sized. */
8770 if (cu->header.version == 2)
8771 info_ptr += cu->header.addr_size;
8772 else
8773 info_ptr += cu->header.offset_size;
8774 break;
36586728
TT
8775 case DW_FORM_GNU_ref_alt:
8776 info_ptr += cu->header.offset_size;
8777 break;
ae411497 8778 case DW_FORM_addr:
4bb7a0a7
DJ
8779 info_ptr += cu->header.addr_size;
8780 break;
8781 case DW_FORM_data1:
8782 case DW_FORM_ref1:
8783 case DW_FORM_flag:
8fe0f950 8784 case DW_FORM_strx1:
4bb7a0a7
DJ
8785 info_ptr += 1;
8786 break;
2dc7f7b3 8787 case DW_FORM_flag_present:
43988095 8788 case DW_FORM_implicit_const:
2dc7f7b3 8789 break;
4bb7a0a7
DJ
8790 case DW_FORM_data2:
8791 case DW_FORM_ref2:
8fe0f950 8792 case DW_FORM_strx2:
4bb7a0a7
DJ
8793 info_ptr += 2;
8794 break;
8fe0f950
AT
8795 case DW_FORM_strx3:
8796 info_ptr += 3;
8797 break;
4bb7a0a7
DJ
8798 case DW_FORM_data4:
8799 case DW_FORM_ref4:
8fe0f950 8800 case DW_FORM_strx4:
4bb7a0a7
DJ
8801 info_ptr += 4;
8802 break;
8803 case DW_FORM_data8:
8804 case DW_FORM_ref8:
55f1336d 8805 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8806 info_ptr += 8;
8807 break;
0224619f
JK
8808 case DW_FORM_data16:
8809 info_ptr += 16;
8810 break;
4bb7a0a7 8811 case DW_FORM_string:
9b1c24c8 8812 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8813 info_ptr += bytes_read;
8814 break;
2dc7f7b3 8815 case DW_FORM_sec_offset:
4bb7a0a7 8816 case DW_FORM_strp:
36586728 8817 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8818 info_ptr += cu->header.offset_size;
8819 break;
2dc7f7b3 8820 case DW_FORM_exprloc:
4bb7a0a7
DJ
8821 case DW_FORM_block:
8822 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8823 info_ptr += bytes_read;
8824 break;
8825 case DW_FORM_block1:
8826 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8827 break;
8828 case DW_FORM_block2:
8829 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8830 break;
8831 case DW_FORM_block4:
8832 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8833 break;
336d760d 8834 case DW_FORM_addrx:
cf532bd1 8835 case DW_FORM_strx:
4bb7a0a7
DJ
8836 case DW_FORM_sdata:
8837 case DW_FORM_udata:
8838 case DW_FORM_ref_udata:
3019eac3
DE
8839 case DW_FORM_GNU_addr_index:
8840 case DW_FORM_GNU_str_index:
18a8505e 8841 case DW_FORM_rnglistx:
41144253 8842 case DW_FORM_loclistx:
d521ce57 8843 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8844 break;
8845 case DW_FORM_indirect:
8846 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8847 info_ptr += bytes_read;
8848 /* We need to continue parsing from here, so just go back to
8849 the top. */
8850 goto skip_attribute;
8851
8852 default:
3e43a32a
MS
8853 error (_("Dwarf Error: Cannot handle %s "
8854 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8855 dwarf_form_name (form),
8856 bfd_get_filename (abfd));
8857 }
8858 }
8859
8860 if (abbrev->has_children)
dee91e82 8861 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8862 else
8863 return info_ptr;
8864}
8865
93311388 8866/* Locate ORIG_PDI's sibling.
dee91e82 8867 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8868
d521ce57 8869static const gdb_byte *
dee91e82
DE
8870locate_pdi_sibling (const struct die_reader_specs *reader,
8871 struct partial_die_info *orig_pdi,
d521ce57 8872 const gdb_byte *info_ptr)
91c24f0a
DC
8873{
8874 /* Do we know the sibling already? */
72bf9492 8875
91c24f0a
DC
8876 if (orig_pdi->sibling)
8877 return orig_pdi->sibling;
8878
8879 /* Are there any children to deal with? */
8880
8881 if (!orig_pdi->has_children)
8882 return info_ptr;
8883
4bb7a0a7 8884 /* Skip the children the long way. */
91c24f0a 8885
dee91e82 8886 return skip_children (reader, info_ptr);
91c24f0a
DC
8887}
8888
257e7a09 8889/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8890 not NULL. */
c906108c 8891
891813be
TT
8892void
8893dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8894{
ed2dc618
SM
8895 struct dwarf2_per_objfile *dwarf2_per_objfile
8896 = get_dwarf2_per_objfile (objfile);
8897
af758d11
SM
8898 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8899
077cbab2
TT
8900 /* If this psymtab is constructed from a debug-only objfile, the
8901 has_section_at_zero flag will not necessarily be correct. We
8902 can get the correct value for this flag by looking at the data
8903 associated with the (presumably stripped) associated objfile. */
8904 if (objfile->separate_debug_objfile_backlink)
c906108c 8905 {
077cbab2
TT
8906 struct dwarf2_per_objfile *dpo_backlink
8907 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8908
5989a64e
SM
8909 dwarf2_per_objfile->per_bfd->has_section_at_zero
8910 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8911 }
98bfdba5 8912
8566b89b 8913 expand_psymtab (objfile);
95554aad 8914
ed2dc618 8915 process_cu_includes (dwarf2_per_objfile);
c906108c 8916}
9cdd5dbd
DE
8917\f
8918/* Reading in full CUs. */
c906108c 8919
10b3939b
DJ
8920/* Add PER_CU to the queue. */
8921
8922static void
120ce1b5
SM
8923queue_comp_unit (dwarf2_per_cu_data *per_cu,
8924 dwarf2_per_objfile *per_objfile,
95554aad 8925 enum language pretend_language)
10b3939b 8926{
10b3939b 8927 per_cu->queued = 1;
120ce1b5 8928 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8929}
8930
89e63ee4
DE
8931/* If PER_CU is not yet queued, add it to the queue.
8932 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8933 dependency.
0907af0c 8934 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8935 meaning either PER_CU is already queued or it is already loaded.
8936
8937 N.B. There is an invariant here that if a CU is queued then it is loaded.
8938 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8939
8940static int
89e63ee4 8941maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8942 dwarf2_per_cu_data *per_cu,
8943 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8944 enum language pretend_language)
8945{
8946 /* We may arrive here during partial symbol reading, if we need full
8947 DIEs to process an unusual case (e.g. template arguments). Do
8948 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8949 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c
DE
8950 {
8951 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8952 return 1;
8953 return 0;
8954 }
8955
8956 /* Mark the dependence relation so that we don't flush PER_CU
8957 too early. */
89e63ee4
DE
8958 if (dependent_cu != NULL)
8959 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8960
8961 /* If it's already on the queue, we have nothing to do. */
8962 if (per_cu->queued)
8963 return 0;
8964
8965 /* If the compilation unit is already loaded, just mark it as
8966 used. */
8967 if (per_cu->cu != NULL)
8968 {
8969 per_cu->cu->last_used = 0;
8970 return 0;
8971 }
8972
8973 /* Add it to the queue. */
120ce1b5 8974 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
8975
8976 return 1;
8977}
8978
10b3939b
DJ
8979/* Process the queue. */
8980
8981static void
ed2dc618 8982process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8983{
b4f54984 8984 if (dwarf_read_debug)
45cfd468
DE
8985 {
8986 fprintf_unfiltered (gdb_stdlog,
8987 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8988 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8989 }
8990
03dd20cc
DJ
8991 /* The queue starts out with one item, but following a DIE reference
8992 may load a new CU, adding it to the end of the queue. */
5989a64e 8993 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 8994 {
5989a64e 8995 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
39856def 8996
af758d11 8997 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
cc12ce38 8998 /* Skip dummy CUs. */
39856def 8999 && item.per_cu->cu != NULL)
f4dc4d17 9000 {
39856def 9001 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 9002 unsigned int debug_print_threshold;
247f5c4f 9003 char buf[100];
f4dc4d17 9004
247f5c4f 9005 if (per_cu->is_debug_types)
f4dc4d17 9006 {
247f5c4f
DE
9007 struct signatured_type *sig_type =
9008 (struct signatured_type *) per_cu;
9009
9d8780f0 9010 sprintf (buf, "TU %s at offset %s",
73be47f5 9011 hex_string (sig_type->signature),
9d8780f0 9012 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9013 /* There can be 100s of TUs.
9014 Only print them in verbose mode. */
9015 debug_print_threshold = 2;
f4dc4d17 9016 }
247f5c4f 9017 else
73be47f5 9018 {
9d8780f0
SM
9019 sprintf (buf, "CU at offset %s",
9020 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9021 debug_print_threshold = 1;
9022 }
247f5c4f 9023
b4f54984 9024 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9025 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9026
9027 if (per_cu->is_debug_types)
47b14e86
SM
9028 process_full_type_unit (per_cu, dwarf2_per_objfile,
9029 item.pretend_language);
f4dc4d17 9030 else
47b14e86
SM
9031 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9032 item.pretend_language);
f4dc4d17 9033
b4f54984 9034 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9035 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9036 }
10b3939b 9037
39856def 9038 item.per_cu->queued = 0;
5989a64e 9039 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9040 }
9041
b4f54984 9042 if (dwarf_read_debug)
45cfd468
DE
9043 {
9044 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9045 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9046 }
10b3939b
DJ
9047}
9048
10b3939b
DJ
9049/* Read in full symbols for PST, and anything it depends on. */
9050
8566b89b
TT
9051void
9052dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9053{
af758d11 9054 gdb_assert (!readin_p (objfile));
95554aad 9055
48993951 9056 expand_dependencies (objfile);
aaa75496 9057
97a1449a
SM
9058 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9059 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9060 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9061}
9062
af758d11
SM
9063/* See psympriv.h. */
9064
9065bool
9066dwarf2_psymtab::readin_p (struct objfile *objfile) const
9067{
9068 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9069 return per_objfile->symtab_set_p (per_cu_data);
9070}
9071
9072/* See psympriv.h. */
9073
9074compunit_symtab *
9075dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9076{
9077 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9078 return per_objfile->get_symtab (per_cu_data);
9079}
9080
dee91e82
DE
9081/* Trivial hash function for die_info: the hash value of a DIE
9082 is its offset in .debug_info for this objfile. */
10b3939b 9083
dee91e82
DE
9084static hashval_t
9085die_hash (const void *item)
10b3939b 9086{
9a3c8263 9087 const struct die_info *die = (const struct die_info *) item;
6502dd73 9088
9c541725 9089 return to_underlying (die->sect_off);
dee91e82 9090}
63d06c5c 9091
dee91e82
DE
9092/* Trivial comparison function for die_info structures: two DIEs
9093 are equal if they have the same offset. */
98bfdba5 9094
dee91e82
DE
9095static int
9096die_eq (const void *item_lhs, const void *item_rhs)
9097{
9a3c8263
SM
9098 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9099 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9100
9c541725 9101 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9102}
c906108c 9103
c0ab21c2 9104/* Load the DIEs associated with PER_CU into memory. */
c906108c 9105
dee91e82 9106static void
ab432490
SM
9107load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9108 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9109 bool skip_partial,
9110 enum language pretend_language)
dee91e82 9111{
c0ab21c2
TT
9112 gdb_assert (! this_cu->is_debug_types);
9113
ab432490 9114 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
c0ab21c2
TT
9115 if (reader.dummy_p)
9116 return;
9117
9118 struct dwarf2_cu *cu = reader.cu;
9119 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9120
dee91e82
DE
9121 gdb_assert (cu->die_hash == NULL);
9122 cu->die_hash =
9123 htab_create_alloc_ex (cu->header.length / 12,
9124 die_hash,
9125 die_eq,
9126 NULL,
9127 &cu->comp_unit_obstack,
9128 hashtab_obstack_allocate,
9129 dummy_obstack_deallocate);
e142c38c 9130
3e225074 9131 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9132 reader.comp_unit_die->child
9133 = read_die_and_siblings (&reader, reader.info_ptr,
9134 &info_ptr, reader.comp_unit_die);
9135 cu->dies = reader.comp_unit_die;
dee91e82 9136 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9137
9138 /* We try not to read any attributes in this function, because not
9cdd5dbd 9139 all CUs needed for references have been loaded yet, and symbol
10b3939b 9140 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9141 or we won't be able to build types correctly.
9142 Similarly, if we do not read the producer, we can not apply
9143 producer-specific interpretation. */
c0ab21c2 9144 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9145
9146 reader.keep ();
10b3939b
DJ
9147}
9148
3da10d80
KS
9149/* Add a DIE to the delayed physname list. */
9150
9151static void
9152add_to_method_list (struct type *type, int fnfield_index, int index,
9153 const char *name, struct die_info *die,
9154 struct dwarf2_cu *cu)
9155{
9156 struct delayed_method_info mi;
9157 mi.type = type;
9158 mi.fnfield_index = fnfield_index;
9159 mi.index = index;
9160 mi.name = name;
9161 mi.die = die;
c89b44cd 9162 cu->method_list.push_back (mi);
3da10d80
KS
9163}
9164
3693fdb3
PA
9165/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9166 "const" / "volatile". If so, decrements LEN by the length of the
9167 modifier and return true. Otherwise return false. */
9168
9169template<size_t N>
9170static bool
9171check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9172{
9173 size_t mod_len = sizeof (mod) - 1;
9174 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9175 {
9176 len -= mod_len;
9177 return true;
9178 }
9179 return false;
9180}
9181
3da10d80
KS
9182/* Compute the physnames of any methods on the CU's method list.
9183
9184 The computation of method physnames is delayed in order to avoid the
9185 (bad) condition that one of the method's formal parameters is of an as yet
9186 incomplete type. */
9187
9188static void
9189compute_delayed_physnames (struct dwarf2_cu *cu)
9190{
3693fdb3 9191 /* Only C++ delays computing physnames. */
c89b44cd 9192 if (cu->method_list.empty ())
3693fdb3
PA
9193 return;
9194 gdb_assert (cu->language == language_cplus);
9195
52941706 9196 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9197 {
1d06ead6 9198 const char *physname;
3da10d80 9199 struct fn_fieldlist *fn_flp
c89b44cd
TT
9200 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9201 physname = dwarf2_physname (mi.name, mi.die, cu);
9202 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9203 = physname ? physname : "";
3693fdb3
PA
9204
9205 /* Since there's no tag to indicate whether a method is a
9206 const/volatile overload, extract that information out of the
9207 demangled name. */
9208 if (physname != NULL)
9209 {
9210 size_t len = strlen (physname);
9211
9212 while (1)
9213 {
9214 if (physname[len] == ')') /* shortcut */
9215 break;
9216 else if (check_modifier (physname, len, " const"))
c89b44cd 9217 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9218 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9219 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9220 else
9221 break;
9222 }
9223 }
3da10d80 9224 }
c89b44cd
TT
9225
9226 /* The list is no longer needed. */
9227 cu->method_list.clear ();
3da10d80
KS
9228}
9229
a766d390
DE
9230/* Go objects should be embedded in a DW_TAG_module DIE,
9231 and it's not clear if/how imported objects will appear.
9232 To keep Go support simple until that's worked out,
9233 go back through what we've read and create something usable.
9234 We could do this while processing each DIE, and feels kinda cleaner,
9235 but that way is more invasive.
9236 This is to, for example, allow the user to type "p var" or "b main"
9237 without having to specify the package name, and allow lookups
9238 of module.object to work in contexts that use the expression
9239 parser. */
9240
9241static void
9242fixup_go_packaging (struct dwarf2_cu *cu)
9243{
421d1616 9244 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9245 struct pending *list;
9246 int i;
9247
c24bdb02 9248 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9249 list != NULL;
9250 list = list->next)
a766d390
DE
9251 {
9252 for (i = 0; i < list->nsyms; ++i)
9253 {
9254 struct symbol *sym = list->symbol[i];
9255
c1b5c1eb 9256 if (sym->language () == language_go
a766d390
DE
9257 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9258 {
421d1616
TT
9259 gdb::unique_xmalloc_ptr<char> this_package_name
9260 (go_symbol_package_name (sym));
a766d390
DE
9261
9262 if (this_package_name == NULL)
9263 continue;
9264 if (package_name == NULL)
421d1616 9265 package_name = std::move (this_package_name);
a766d390
DE
9266 else
9267 {
5e22e966 9268 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9269 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9270 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9271 (symbol_symtab (sym) != NULL
9272 ? symtab_to_filename_for_display
9273 (symbol_symtab (sym))
e3b94546 9274 : objfile_name (objfile)),
421d1616 9275 this_package_name.get (), package_name.get ());
a766d390
DE
9276 }
9277 }
9278 }
9279 }
9280
9281 if (package_name != NULL)
9282 {
5e22e966 9283 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9284 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9285 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9286 saved_package_name);
a766d390
DE
9287 struct symbol *sym;
9288
8c14c3a3 9289 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9290 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9291 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9292 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9293 e.g., "main" finds the "main" module and not C's main(). */
9294 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9295 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9296 SYMBOL_TYPE (sym) = type;
9297
c24bdb02 9298 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9299 }
9300}
9301
c9317f21
TT
9302/* Allocate a fully-qualified name consisting of the two parts on the
9303 obstack. */
9304
9305static const char *
9306rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9307{
9308 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9309}
9310
9c6a1327
TT
9311/* A helper that allocates a variant part to attach to a Rust enum
9312 type. OBSTACK is where the results should be allocated. TYPE is
9313 the type we're processing. DISCRIMINANT_INDEX is the index of the
9314 discriminant. It must be the index of one of the fields of TYPE.
9315 DEFAULT_INDEX is the index of the default field; or -1 if there is
9316 no default. RANGES is indexed by "effective" field number (the
9317 field index, but omitting the discriminant and default fields) and
9318 must hold the discriminant values used by the variants. Note that
9319 RANGES must have a lifetime at least as long as OBSTACK -- either
9320 already allocated on it, or static. */
c9317f21 9321
9c6a1327
TT
9322static void
9323alloc_rust_variant (struct obstack *obstack, struct type *type,
9324 int discriminant_index, int default_index,
9325 gdb::array_view<discriminant_range> ranges)
9326{
9327 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9328 must be handled by the caller. */
9329 gdb_assert (discriminant_index >= 0
1f704f76 9330 && discriminant_index < type->num_fields ());
c9317f21 9331 gdb_assert (default_index == -1
1f704f76 9332 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9333
9c6a1327 9334 /* We have one variant for each non-discriminant field. */
1f704f76 9335 int n_variants = type->num_fields () - 1;
c9317f21 9336
9c6a1327
TT
9337 variant *variants = new (obstack) variant[n_variants];
9338 int var_idx = 0;
9339 int range_idx = 0;
1f704f76 9340 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9341 {
9342 if (i == discriminant_index)
9343 continue;
c9317f21 9344
9c6a1327
TT
9345 variants[var_idx].first_field = i;
9346 variants[var_idx].last_field = i + 1;
9347
9348 /* The default field does not need a range, but other fields do.
9349 We skipped the discriminant above. */
9350 if (i != default_index)
9351 {
9352 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9353 ++range_idx;
9354 }
c9317f21 9355
9c6a1327
TT
9356 ++var_idx;
9357 }
9358
9359 gdb_assert (range_idx == ranges.size ());
9360 gdb_assert (var_idx == n_variants);
9361
9362 variant_part *part = new (obstack) variant_part;
9363 part->discriminant_index = discriminant_index;
9364 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9365 discriminant_index));
9366 part->variants = gdb::array_view<variant> (variants, n_variants);
9367
9368 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9369 gdb::array_view<variant_part> *prop_value
9370 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9371
9c6a1327
TT
9372 struct dynamic_prop prop;
9373 prop.kind = PROP_VARIANT_PARTS;
9374 prop.data.variant_parts = prop_value;
9375
5c54719c 9376 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9377}
9378
9379/* Some versions of rustc emitted enums in an unusual way.
9380
9381 Ordinary enums were emitted as unions. The first element of each
9382 structure in the union was named "RUST$ENUM$DISR". This element
9383 held the discriminant.
9384
9385 These versions of Rust also implemented the "non-zero"
9386 optimization. When the enum had two values, and one is empty and
9387 the other holds a pointer that cannot be zero, the pointer is used
9388 as the discriminant, with a zero value meaning the empty variant.
9389 Here, the union's first member is of the form
9390 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9391 where the fieldnos are the indices of the fields that should be
9392 traversed in order to find the field (which may be several fields deep)
9393 and the variantname is the name of the variant of the case when the
9394 field is zero.
9395
9396 This function recognizes whether TYPE is of one of these forms,
9397 and, if so, smashes it to be a variant type. */
9398
9399static void
9400quirk_rust_enum (struct type *type, struct objfile *objfile)
9401{
78134374 9402 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9403
9404 /* We don't need to deal with empty enums. */
1f704f76 9405 if (type->num_fields () == 0)
c9317f21
TT
9406 return;
9407
9408#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9409 if (type->num_fields () == 1
c9317f21
TT
9410 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9411 {
9412 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9413
9414 /* Decode the field name to find the offset of the
9415 discriminant. */
9416 ULONGEST bit_offset = 0;
9417 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9418 while (name[0] >= '0' && name[0] <= '9')
9419 {
9420 char *tail;
9421 unsigned long index = strtoul (name, &tail, 10);
9422 name = tail;
9423 if (*name != '$'
1f704f76 9424 || index >= field_type->num_fields ()
c9317f21
TT
9425 || (TYPE_FIELD_LOC_KIND (field_type, index)
9426 != FIELD_LOC_KIND_BITPOS))
9427 {
b98664d3 9428 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9429 "[in module %s]"),
9430 TYPE_FIELD_NAME (type, 0),
9431 objfile_name (objfile));
9432 return;
9433 }
9434 ++name;
9435
9436 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9437 field_type = TYPE_FIELD_TYPE (field_type, index);
9438 }
9439
9c6a1327
TT
9440 /* Smash this type to be a structure type. We have to do this
9441 because the type has already been recorded. */
67607e24 9442 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9443 type->set_num_fields (3);
9c6a1327 9444 /* Save the field we care about. */
ceacbf6e 9445 struct field saved_field = type->field (0);
3cabb6b0
SM
9446 type->set_fields
9447 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9448
9c6a1327
TT
9449 /* Put the discriminant at index 0. */
9450 TYPE_FIELD_TYPE (type, 0) = field_type;
9451 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9452 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9453 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9454
9455 /* The order of fields doesn't really matter, so put the real
9456 field at index 1 and the data-less field at index 2. */
ceacbf6e 9457 type->field (1) = saved_field;
9c6a1327 9458 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9459 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9460 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9461 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9462 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9463
9464 const char *dataless_name
7d93a1e0 9465 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9466 name);
9467 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9468 dataless_name);
9c6a1327 9469 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9470 /* NAME points into the original discriminant name, which
9471 already has the correct lifetime. */
9c6a1327 9472 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9473 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9474
9c6a1327
TT
9475 /* Indicate that this is a variant type. */
9476 static discriminant_range ranges[1] = { { 0, 0 } };
9477 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9478 }
77c2dba3
TT
9479 /* A union with a single anonymous field is probably an old-style
9480 univariant enum. */
1f704f76 9481 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9482 {
c9317f21
TT
9483 /* Smash this type to be a structure type. We have to do this
9484 because the type has already been recorded. */
67607e24 9485 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9486
9c6a1327 9487 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9488 const char *variant_name
7d93a1e0 9489 = rust_last_path_segment (field_type->name ());
9c6a1327 9490 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9491 field_type->set_name
9492 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9493 type->name (), variant_name));
c9317f21
TT
9494 }
9495 else
9496 {
9497 struct type *disr_type = nullptr;
1f704f76 9498 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9499 {
9500 disr_type = TYPE_FIELD_TYPE (type, i);
9501
78134374 9502 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9503 {
9504 /* All fields of a true enum will be structs. */
9505 return;
9506 }
1f704f76 9507 else if (disr_type->num_fields () == 0)
c9317f21
TT
9508 {
9509 /* Could be data-less variant, so keep going. */
a037790e 9510 disr_type = nullptr;
c9317f21
TT
9511 }
9512 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9513 "RUST$ENUM$DISR") != 0)
9514 {
9515 /* Not a Rust enum. */
9516 return;
9517 }
9518 else
9519 {
9520 /* Found one. */
9521 break;
9522 }
9523 }
9524
9525 /* If we got here without a discriminant, then it's probably
9526 just a union. */
9527 if (disr_type == nullptr)
9528 return;
9529
9530 /* Smash this type to be a structure type. We have to do this
9531 because the type has already been recorded. */
67607e24 9532 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9533
9c6a1327 9534 /* Make space for the discriminant field. */
ceacbf6e 9535 struct field *disr_field = &disr_type->field (0);
9c6a1327 9536 field *new_fields
1f704f76 9537 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9538 * sizeof (struct field)));
80fc5e77 9539 memcpy (new_fields + 1, type->fields (),
1f704f76 9540 type->num_fields () * sizeof (struct field));
3cabb6b0 9541 type->set_fields (new_fields);
1f704f76 9542 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9543
9544 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9545 type->field (0) = *disr_field;
9c6a1327
TT
9546 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9547 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9548
9549 /* We need a way to find the correct discriminant given a
9550 variant name. For convenience we build a map here. */
9551 struct type *enum_type = FIELD_TYPE (*disr_field);
9552 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9553 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9554 {
9555 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9556 {
9557 const char *name
9558 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9559 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9560 }
9561 }
9562
1f704f76 9563 int n_fields = type->num_fields ();
9c6a1327
TT
9564 /* We don't need a range entry for the discriminant, but we do
9565 need one for every other field, as there is no default
9566 variant. */
9567 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9568 discriminant_range,
9569 n_fields - 1);
c9317f21
TT
9570 /* Skip the discriminant here. */
9571 for (int i = 1; i < n_fields; ++i)
9572 {
9573 /* Find the final word in the name of this variant's type.
9574 That name can be used to look up the correct
9575 discriminant. */
9576 const char *variant_name
7d93a1e0 9577 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9578
9579 auto iter = discriminant_map.find (variant_name);
9580 if (iter != discriminant_map.end ())
9c6a1327
TT
9581 {
9582 ranges[i].low = iter->second;
9583 ranges[i].high = iter->second;
9584 }
c9317f21 9585
bedda9ac 9586 /* Remove the discriminant field, if it exists. */
9c6a1327 9587 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9588 if (sub_type->num_fields () > 0)
bedda9ac 9589 {
5e33d5f4 9590 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9591 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9592 }
9c6a1327 9593 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9594 sub_type->set_name
9595 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9596 type->name (), variant_name));
c9317f21 9597 }
9c6a1327
TT
9598
9599 /* Indicate that this is a variant type. */
9600 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9601 gdb::array_view<discriminant_range> (ranges,
9602 n_fields - 1));
c9317f21
TT
9603 }
9604}
9605
9606/* Rewrite some Rust unions to be structures with variants parts. */
9607
9608static void
9609rust_union_quirks (struct dwarf2_cu *cu)
9610{
9611 gdb_assert (cu->language == language_rust);
52941706 9612 for (type *type_ : cu->rust_unions)
5e22e966 9613 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9614 /* We don't need this any more. */
9615 cu->rust_unions.clear ();
c9317f21
TT
9616}
9617
8adb8487
TT
9618/* See read.h. */
9619
9620type_unit_group_unshareable *
9621dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9622{
9623 auto iter = this->m_type_units.find (tu_group);
9624 if (iter != this->m_type_units.end ())
9625 return iter->second.get ();
9626
9627 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9628 type_unit_group_unshareable *result = uniq.get ();
9629 this->m_type_units[tu_group] = std::move (uniq);
9630 return result;
9631}
9632
95554aad
TT
9633/* A helper function for computing the list of all symbol tables
9634 included by PER_CU. */
9635
9636static void
4c39bc03 9637recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9638 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9639 dwarf2_per_cu_data *per_cu,
9640 dwarf2_per_objfile *per_objfile,
43f3e411 9641 struct compunit_symtab *immediate_parent)
95554aad 9642{
af758d11 9643 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9644 if (*slot != NULL)
9645 {
9646 /* This inclusion and its children have been processed. */
9647 return;
9648 }
9649
9650 *slot = per_cu;
af758d11 9651
95554aad 9652 /* Only add a CU if it has a symbol table. */
43182c09 9653 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9654 if (cust != NULL)
ec94af83
DE
9655 {
9656 /* If this is a type unit only add its symbol table if we haven't
9657 seen it yet (type unit per_cu's can share symtabs). */
9658 if (per_cu->is_debug_types)
9659 {
43f3e411 9660 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9661 if (*slot == NULL)
9662 {
43f3e411 9663 *slot = cust;
4c39bc03 9664 result->push_back (cust);
43f3e411
DE
9665 if (cust->user == NULL)
9666 cust->user = immediate_parent;
ec94af83
DE
9667 }
9668 }
9669 else
f9125b6c 9670 {
4c39bc03 9671 result->push_back (cust);
43f3e411
DE
9672 if (cust->user == NULL)
9673 cust->user = immediate_parent;
f9125b6c 9674 }
ec94af83 9675 }
95554aad 9676
ae640021
AB
9677 if (!per_cu->imported_symtabs_empty ())
9678 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9679 {
9680 recursively_compute_inclusions (result, all_children,
43182c09
SM
9681 all_type_symtabs, ptr, per_objfile,
9682 cust);
ae640021 9683 }
95554aad
TT
9684}
9685
43f3e411 9686/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9687 PER_CU. */
9688
9689static void
43182c09
SM
9690compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9691 dwarf2_per_objfile *per_objfile)
95554aad 9692{
f4dc4d17
DE
9693 gdb_assert (! per_cu->is_debug_types);
9694
ae640021 9695 if (!per_cu->imported_symtabs_empty ())
95554aad 9696 {
ae640021 9697 int len;
4c39bc03 9698 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9699 htab_t all_children, all_type_symtabs;
43182c09 9700 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9701
9702 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9703 if (cust == NULL)
95554aad
TT
9704 return;
9705
9706 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9707 NULL, xcalloc, xfree);
ec94af83
DE
9708 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9709 NULL, xcalloc, xfree);
95554aad 9710
ae640021 9711 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9712 {
9713 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9714 all_type_symtabs, ptr, per_objfile,
9715 cust);
ec94af83 9716 }
95554aad 9717
ec94af83 9718 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9719 len = result_symtabs.size ();
43f3e411 9720 cust->includes
f6e649dd 9721 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9722 struct compunit_symtab *, len + 1);
4c39bc03
TT
9723 memcpy (cust->includes, result_symtabs.data (),
9724 len * sizeof (compunit_symtab *));
43f3e411 9725 cust->includes[len] = NULL;
95554aad 9726
95554aad 9727 htab_delete (all_children);
ec94af83 9728 htab_delete (all_type_symtabs);
95554aad
TT
9729 }
9730}
9731
9732/* Compute the 'includes' field for the symtabs of all the CUs we just
9733 read. */
9734
9735static void
ed2dc618 9736process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9737{
5989a64e 9738 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9739 {
9740 if (! iter->is_debug_types)
43182c09 9741 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
f4dc4d17 9742 }
95554aad 9743
5989a64e 9744 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9745}
9746
9cdd5dbd 9747/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9748 already been loaded into memory. */
9749
9750static void
47b14e86
SM
9751process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9752 dwarf2_per_objfile *dwarf2_per_objfile,
95554aad 9753 enum language pretend_language)
10b3939b 9754{
10b3939b 9755 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618 9756 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9757 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9758 CORE_ADDR lowpc, highpc;
43f3e411 9759 struct compunit_symtab *cust;
10b3939b 9760 CORE_ADDR baseaddr;
4359dff1 9761 struct block *static_block;
3e29f34a 9762 CORE_ADDR addr;
10b3939b 9763
b3b3bada 9764 baseaddr = objfile->text_section_offset ();
10b3939b 9765
c89b44cd
TT
9766 /* Clear the list here in case something was left over. */
9767 cu->method_list.clear ();
10b3939b 9768
95554aad
TT
9769 cu->language = pretend_language;
9770 cu->language_defn = language_def (cu->language);
9771
c906108c 9772 /* Do line number decoding in read_file_scope () */
10b3939b 9773 process_die (cu->dies, cu);
c906108c 9774
a766d390
DE
9775 /* For now fudge the Go package. */
9776 if (cu->language == language_go)
9777 fixup_go_packaging (cu);
9778
5f48f8f3 9779 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9780 should be complete, and it should now be safe to compute all of the
9781 physnames. */
9782 compute_delayed_physnames (cu);
3da10d80 9783
c9317f21
TT
9784 if (cu->language == language_rust)
9785 rust_union_quirks (cu);
9786
fae299cd
DC
9787 /* Some compilers don't define a DW_AT_high_pc attribute for the
9788 compilation unit. If the DW_AT_high_pc is missing, synthesize
9789 it, by scanning the DIE's below the compilation unit. */
10b3939b 9790 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9791
3e29f34a 9792 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9793 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9794
9795 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9796 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9797 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9798 addrmap to help ensure it has an accurate map of pc values belonging to
9799 this comp unit. */
9800 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9801
c24bdb02 9802 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9803 SECT_OFF_TEXT (objfile),
9804 0);
c906108c 9805
43f3e411 9806 if (cust != NULL)
c906108c 9807 {
df15bd07 9808 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9809
8be455d7
JK
9810 /* Set symtab language to language from DW_AT_language. If the
9811 compilation is from a C file generated by language preprocessors, do
9812 not set the language if it was already deduced by start_subfile. */
43f3e411 9813 if (!(cu->language == language_c
40e3ad0e 9814 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9815 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9816
9817 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9818 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9819 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9820 there were bugs in prologue debug info, fixed later in GCC-4.5
9821 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9822
9823 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9824 needed, it would be wrong due to missing DW_AT_producer there.
9825
9826 Still one can confuse GDB by using non-standard GCC compilation
9827 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9828 */
ab260dad 9829 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9830 cust->locations_valid = 1;
e0d00bc7
JK
9831
9832 if (gcc_4_minor >= 5)
43f3e411 9833 cust->epilogue_unwind_valid = 1;
96408a79 9834
43f3e411 9835 cust->call_site_htab = cu->call_site_htab;
c906108c 9836 }
9291a0cd 9837
af758d11 9838 dwarf2_per_objfile->set_symtab (per_cu, cust);
c906108c 9839
95554aad 9840 /* Push it for inclusion processing later. */
5989a64e 9841 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
804d2729
TT
9842
9843 /* Not needed any more. */
c24bdb02 9844 cu->reset_builder ();
f4dc4d17 9845}
45cfd468 9846
f4dc4d17
DE
9847/* Generate full symbol information for type unit PER_CU, whose DIEs have
9848 already been loaded into memory. */
9849
9850static void
47b14e86
SM
9851process_full_type_unit (dwarf2_per_cu_data *per_cu,
9852 dwarf2_per_objfile *dwarf2_per_objfile,
f4dc4d17
DE
9853 enum language pretend_language)
9854{
9855 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618 9856 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9857 struct compunit_symtab *cust;
0186c6a7
DE
9858 struct signatured_type *sig_type;
9859
9860 gdb_assert (per_cu->is_debug_types);
9861 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9862
c89b44cd
TT
9863 /* Clear the list here in case something was left over. */
9864 cu->method_list.clear ();
f4dc4d17 9865
f4dc4d17
DE
9866 cu->language = pretend_language;
9867 cu->language_defn = language_def (cu->language);
9868
9869 /* The symbol tables are set up in read_type_unit_scope. */
9870 process_die (cu->dies, cu);
9871
9872 /* For now fudge the Go package. */
9873 if (cu->language == language_go)
9874 fixup_go_packaging (cu);
9875
5f48f8f3 9876 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9877 should be complete, and it should now be safe to compute all of the
9878 physnames. */
9879 compute_delayed_physnames (cu);
f4dc4d17 9880
c9317f21
TT
9881 if (cu->language == language_rust)
9882 rust_union_quirks (cu);
9883
f4dc4d17
DE
9884 /* TUs share symbol tables.
9885 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9886 of it with end_expandable_symtab. Otherwise, complete the addition of
9887 this TU's symbols to the existing symtab. */
8adb8487
TT
9888 type_unit_group_unshareable *tug_unshare =
9889 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9890 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9891 {
c24bdb02
KS
9892 buildsym_compunit *builder = cu->get_builder ();
9893 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9894 tug_unshare->compunit_symtab = cust;
f4dc4d17 9895
43f3e411 9896 if (cust != NULL)
f4dc4d17
DE
9897 {
9898 /* Set symtab language to language from DW_AT_language. If the
9899 compilation is from a C file generated by language preprocessors,
9900 do not set the language if it was already deduced by
9901 start_subfile. */
43f3e411
DE
9902 if (!(cu->language == language_c
9903 && COMPUNIT_FILETABS (cust)->language != language_c))
9904 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9905 }
9906 }
9907 else
9908 {
c24bdb02 9909 cu->get_builder ()->augment_type_symtab ();
8adb8487 9910 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9911 }
9912
af758d11 9913 dwarf2_per_objfile->set_symtab (per_cu, cust);
804d2729
TT
9914
9915 /* Not needed any more. */
c24bdb02 9916 cu->reset_builder ();
c906108c
SS
9917}
9918
95554aad
TT
9919/* Process an imported unit DIE. */
9920
9921static void
9922process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9923{
9924 struct attribute *attr;
9925
f4dc4d17
DE
9926 /* For now we don't handle imported units in type units. */
9927 if (cu->per_cu->is_debug_types)
9928 {
9929 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9930 " supported in type units [in module %s]"),
5e22e966 9931 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9932 }
9933
95554aad
TT
9934 attr = dwarf2_attr (die, DW_AT_import, cu);
9935 if (attr != NULL)
9936 {
0826b30a 9937 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9938 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9939 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9940 dwarf2_per_cu_data *per_cu
ab432490 9941 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9942
58990295
TV
9943 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9944 into another compilation unit, at root level. Regard this as a hint,
9945 and ignore it. */
9946 if (die->parent && die->parent->parent == NULL
9947 && per_cu->unit_type == DW_UT_compile
9948 && per_cu->lang == language_cplus)
9949 return;
9950
69d751e3 9951 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9952 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 9953 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 9954
ae640021 9955 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9956 }
9957}
9958
4c8aa72d
PA
9959/* RAII object that represents a process_die scope: i.e.,
9960 starts/finishes processing a DIE. */
9961class process_die_scope
adde2bff 9962{
4c8aa72d
PA
9963public:
9964 process_die_scope (die_info *die, dwarf2_cu *cu)
9965 : m_die (die), m_cu (cu)
9966 {
9967 /* We should only be processing DIEs not already in process. */
9968 gdb_assert (!m_die->in_process);
9969 m_die->in_process = true;
9970 }
8c3cb9fa 9971
4c8aa72d
PA
9972 ~process_die_scope ()
9973 {
9974 m_die->in_process = false;
9975
9976 /* If we're done processing the DIE for the CU that owns the line
9977 header, we don't need the line header anymore. */
9978 if (m_cu->line_header_die_owner == m_die)
9979 {
9980 delete m_cu->line_header;
9981 m_cu->line_header = NULL;
9982 m_cu->line_header_die_owner = NULL;
9983 }
9984 }
9985
9986private:
9987 die_info *m_die;
9988 dwarf2_cu *m_cu;
9989};
adde2bff 9990
c906108c
SS
9991/* Process a die and its children. */
9992
9993static void
e7c27a73 9994process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9995{
4c8aa72d 9996 process_die_scope scope (die, cu);
adde2bff 9997
c906108c
SS
9998 switch (die->tag)
9999 {
10000 case DW_TAG_padding:
10001 break;
10002 case DW_TAG_compile_unit:
95554aad 10003 case DW_TAG_partial_unit:
e7c27a73 10004 read_file_scope (die, cu);
c906108c 10005 break;
348e048f
DE
10006 case DW_TAG_type_unit:
10007 read_type_unit_scope (die, cu);
10008 break;
c906108c 10009 case DW_TAG_subprogram:
0a4b0913
AB
10010 /* Nested subprograms in Fortran get a prefix. */
10011 if (cu->language == language_fortran
10012 && die->parent != NULL
10013 && die->parent->tag == DW_TAG_subprogram)
10014 cu->processing_has_namespace_info = true;
10015 /* Fall through. */
c906108c 10016 case DW_TAG_inlined_subroutine:
edb3359d 10017 read_func_scope (die, cu);
c906108c
SS
10018 break;
10019 case DW_TAG_lexical_block:
14898363
L
10020 case DW_TAG_try_block:
10021 case DW_TAG_catch_block:
e7c27a73 10022 read_lexical_block_scope (die, cu);
c906108c 10023 break;
216f72a1 10024 case DW_TAG_call_site:
96408a79
SA
10025 case DW_TAG_GNU_call_site:
10026 read_call_site_scope (die, cu);
10027 break;
c906108c 10028 case DW_TAG_class_type:
680b30c7 10029 case DW_TAG_interface_type:
c906108c
SS
10030 case DW_TAG_structure_type:
10031 case DW_TAG_union_type:
134d01f1 10032 process_structure_scope (die, cu);
c906108c
SS
10033 break;
10034 case DW_TAG_enumeration_type:
134d01f1 10035 process_enumeration_scope (die, cu);
c906108c 10036 break;
134d01f1 10037
f792889a
DJ
10038 /* These dies have a type, but processing them does not create
10039 a symbol or recurse to process the children. Therefore we can
10040 read them on-demand through read_type_die. */
c906108c 10041 case DW_TAG_subroutine_type:
72019c9c 10042 case DW_TAG_set_type:
c906108c 10043 case DW_TAG_array_type:
c906108c 10044 case DW_TAG_pointer_type:
c906108c 10045 case DW_TAG_ptr_to_member_type:
c906108c 10046 case DW_TAG_reference_type:
4297a3f0 10047 case DW_TAG_rvalue_reference_type:
c906108c 10048 case DW_TAG_string_type:
c906108c 10049 break;
134d01f1 10050
c906108c 10051 case DW_TAG_base_type:
a02abb62 10052 case DW_TAG_subrange_type:
cb249c71 10053 case DW_TAG_typedef:
134d01f1
DJ
10054 /* Add a typedef symbol for the type definition, if it has a
10055 DW_AT_name. */
f792889a 10056 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10057 break;
c906108c 10058 case DW_TAG_common_block:
e7c27a73 10059 read_common_block (die, cu);
c906108c
SS
10060 break;
10061 case DW_TAG_common_inclusion:
10062 break;
d9fa45fe 10063 case DW_TAG_namespace:
9068261f 10064 cu->processing_has_namespace_info = true;
e7c27a73 10065 read_namespace (die, cu);
d9fa45fe 10066 break;
5d7cb8df 10067 case DW_TAG_module:
9068261f 10068 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10069 read_module (die, cu);
10070 break;
d9fa45fe 10071 case DW_TAG_imported_declaration:
9068261f 10072 cu->processing_has_namespace_info = true;
74921315
KS
10073 if (read_namespace_alias (die, cu))
10074 break;
86a73007
TT
10075 /* The declaration is not a global namespace alias. */
10076 /* Fall through. */
d9fa45fe 10077 case DW_TAG_imported_module:
9068261f 10078 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10079 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10080 || cu->language != language_fortran))
b98664d3 10081 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10082 dwarf_tag_name (die->tag));
10083 read_import_statement (die, cu);
d9fa45fe 10084 break;
95554aad
TT
10085
10086 case DW_TAG_imported_unit:
10087 process_imported_unit_die (die, cu);
10088 break;
10089
71a3c369
TT
10090 case DW_TAG_variable:
10091 read_variable (die, cu);
10092 break;
10093
c906108c 10094 default:
e7c27a73 10095 new_symbol (die, NULL, cu);
c906108c
SS
10096 break;
10097 }
10098}
ca69b9e6
DE
10099\f
10100/* DWARF name computation. */
c906108c 10101
94af9270
KS
10102/* A helper function for dwarf2_compute_name which determines whether DIE
10103 needs to have the name of the scope prepended to the name listed in the
10104 die. */
10105
10106static int
10107die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10108{
1c809c68
TT
10109 struct attribute *attr;
10110
94af9270
KS
10111 switch (die->tag)
10112 {
10113 case DW_TAG_namespace:
10114 case DW_TAG_typedef:
10115 case DW_TAG_class_type:
10116 case DW_TAG_interface_type:
10117 case DW_TAG_structure_type:
10118 case DW_TAG_union_type:
10119 case DW_TAG_enumeration_type:
10120 case DW_TAG_enumerator:
10121 case DW_TAG_subprogram:
08a76f8a 10122 case DW_TAG_inlined_subroutine:
94af9270 10123 case DW_TAG_member:
74921315 10124 case DW_TAG_imported_declaration:
94af9270
KS
10125 return 1;
10126
10127 case DW_TAG_variable:
c2b0a229 10128 case DW_TAG_constant:
94af9270
KS
10129 /* We only need to prefix "globally" visible variables. These include
10130 any variable marked with DW_AT_external or any variable that
10131 lives in a namespace. [Variables in anonymous namespaces
10132 require prefixing, but they are not DW_AT_external.] */
10133
10134 if (dwarf2_attr (die, DW_AT_specification, cu))
10135 {
10136 struct dwarf2_cu *spec_cu = cu;
9a619af0 10137
94af9270
KS
10138 return die_needs_namespace (die_specification (die, &spec_cu),
10139 spec_cu);
10140 }
10141
1c809c68 10142 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10143 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10144 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10145 return 0;
10146 /* A variable in a lexical block of some kind does not need a
10147 namespace, even though in C++ such variables may be external
10148 and have a mangled name. */
10149 if (die->parent->tag == DW_TAG_lexical_block
10150 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10151 || die->parent->tag == DW_TAG_catch_block
10152 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10153 return 0;
10154 return 1;
94af9270
KS
10155
10156 default:
10157 return 0;
10158 }
10159}
10160
73b9be8b
KS
10161/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10162 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10163 defined for the given DIE. */
10164
10165static struct attribute *
10166dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10167{
10168 struct attribute *attr;
10169
10170 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10171 if (attr == NULL)
10172 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10173
10174 return attr;
10175}
10176
10177/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10178 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10179 defined for the given DIE. */
10180
10181static const char *
10182dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10183{
10184 const char *linkage_name;
10185
10186 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10187 if (linkage_name == NULL)
10188 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10189
787de330
TT
10190 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10191 See https://github.com/rust-lang/rust/issues/32925. */
10192 if (cu->language == language_rust && linkage_name != NULL
10193 && strchr (linkage_name, '{') != NULL)
10194 linkage_name = NULL;
10195
73b9be8b
KS
10196 return linkage_name;
10197}
10198
94af9270 10199/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10200 compute the physname for the object, which include a method's:
9c37b5ae 10201 - formal parameters (C++),
a766d390 10202 - receiver type (Go),
a766d390
DE
10203
10204 The term "physname" is a bit confusing.
10205 For C++, for example, it is the demangled name.
10206 For Go, for example, it's the mangled name.
94af9270 10207
af6b7be1
JB
10208 For Ada, return the DIE's linkage name rather than the fully qualified
10209 name. PHYSNAME is ignored..
10210
5989a64e 10211 The result is allocated on the objfile->per_bfd's obstack and
45940949 10212 canonicalized. */
94af9270
KS
10213
10214static const char *
15d034d0
TT
10215dwarf2_compute_name (const char *name,
10216 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10217 int physname)
10218{
5e22e966 10219 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10220
94af9270
KS
10221 if (name == NULL)
10222 name = dwarf2_name (die, cu);
10223
2ee7123e
DE
10224 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10225 but otherwise compute it by typename_concat inside GDB.
10226 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10227 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10228 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10229 will set the demangled name to the result of dwarf2_full_name, and it is
10230 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10231 if (cu->language == language_ada
10232 || (cu->language == language_fortran && physname))
10233 {
10234 /* For Ada unit, we prefer the linkage name over the name, as
10235 the former contains the exported name, which the user expects
10236 to be able to reference. Ideally, we want the user to be able
10237 to reference this entity using either natural or linkage name,
10238 but we haven't started looking at this enhancement yet. */
73b9be8b 10239 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10240
2ee7123e
DE
10241 if (linkage_name != NULL)
10242 return linkage_name;
f55ee35c
JK
10243 }
10244
94af9270
KS
10245 /* These are the only languages we know how to qualify names in. */
10246 if (name != NULL
9c37b5ae 10247 && (cu->language == language_cplus
c44af4eb
TT
10248 || cu->language == language_fortran || cu->language == language_d
10249 || cu->language == language_rust))
94af9270
KS
10250 {
10251 if (die_needs_namespace (die, cu))
10252 {
0d5cff50 10253 const char *prefix;
34a68019 10254 const char *canonical_name = NULL;
94af9270 10255
d7e74731
PA
10256 string_file buf;
10257
94af9270 10258 prefix = determine_prefix (die, cu);
94af9270
KS
10259 if (*prefix != '\0')
10260 {
43816ebc
TT
10261 gdb::unique_xmalloc_ptr<char> prefixed_name
10262 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10263
43816ebc 10264 buf.puts (prefixed_name.get ());
94af9270
KS
10265 }
10266 else
d7e74731 10267 buf.puts (name);
94af9270 10268
98bfdba5
PA
10269 /* Template parameters may be specified in the DIE's DW_AT_name, or
10270 as children with DW_TAG_template_type_param or
10271 DW_TAG_value_type_param. If the latter, add them to the name
10272 here. If the name already has template parameters, then
10273 skip this step; some versions of GCC emit both, and
10274 it is more efficient to use the pre-computed name.
10275
10276 Something to keep in mind about this process: it is very
10277 unlikely, or in some cases downright impossible, to produce
10278 something that will match the mangled name of a function.
10279 If the definition of the function has the same debug info,
10280 we should be able to match up with it anyway. But fallbacks
10281 using the minimal symbol, for instance to find a method
10282 implemented in a stripped copy of libstdc++, will not work.
10283 If we do not have debug info for the definition, we will have to
10284 match them up some other way.
10285
10286 When we do name matching there is a related problem with function
10287 templates; two instantiated function templates are allowed to
10288 differ only by their return types, which we do not add here. */
10289
10290 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10291 {
10292 struct attribute *attr;
10293 struct die_info *child;
10294 int first = 1;
10295
10296 die->building_fullname = 1;
10297
10298 for (child = die->child; child != NULL; child = child->sibling)
10299 {
10300 struct type *type;
12df843f 10301 LONGEST value;
d521ce57 10302 const gdb_byte *bytes;
98bfdba5
PA
10303 struct dwarf2_locexpr_baton *baton;
10304 struct value *v;
10305
10306 if (child->tag != DW_TAG_template_type_param
10307 && child->tag != DW_TAG_template_value_param)
10308 continue;
10309
10310 if (first)
10311 {
d7e74731 10312 buf.puts ("<");
98bfdba5
PA
10313 first = 0;
10314 }
10315 else
d7e74731 10316 buf.puts (", ");
98bfdba5
PA
10317
10318 attr = dwarf2_attr (child, DW_AT_type, cu);
10319 if (attr == NULL)
10320 {
b98664d3 10321 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10322 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10323 continue;
10324 }
10325 type = die_type (child, cu);
10326
10327 if (child->tag == DW_TAG_template_type_param)
10328 {
c1ec8cea
TT
10329 c_print_type (type, "", &buf, -1, 0, cu->language,
10330 &type_print_raw_options);
98bfdba5
PA
10331 continue;
10332 }
10333
10334 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10335 if (attr == NULL)
10336 {
b98664d3 10337 complaint (_("template parameter missing "
3e43a32a 10338 "DW_AT_const_value"));
d7e74731 10339 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10340 continue;
10341 }
10342
10343 dwarf2_const_value_attr (attr, type, name,
10344 &cu->comp_unit_obstack, cu,
10345 &value, &bytes, &baton);
10346
10347 if (TYPE_NOSIGN (type))
10348 /* GDB prints characters as NUMBER 'CHAR'. If that's
10349 changed, this can use value_print instead. */
d7e74731 10350 c_printchar (value, type, &buf);
98bfdba5
PA
10351 else
10352 {
10353 struct value_print_options opts;
10354
10355 if (baton != NULL)
10356 v = dwarf2_evaluate_loc_desc (type, NULL,
10357 baton->data,
10358 baton->size,
9f47c707
SM
10359 baton->per_cu,
10360 baton->per_objfile);
98bfdba5
PA
10361 else if (bytes != NULL)
10362 {
10363 v = allocate_value (type);
10364 memcpy (value_contents_writeable (v), bytes,
10365 TYPE_LENGTH (type));
10366 }
10367 else
10368 v = value_from_longest (type, value);
10369
3e43a32a
MS
10370 /* Specify decimal so that we do not depend on
10371 the radix. */
98bfdba5
PA
10372 get_formatted_print_options (&opts, 'd');
10373 opts.raw = 1;
d7e74731 10374 value_print (v, &buf, &opts);
98bfdba5 10375 release_value (v);
98bfdba5
PA
10376 }
10377 }
10378
10379 die->building_fullname = 0;
10380
10381 if (!first)
10382 {
10383 /* Close the argument list, with a space if necessary
10384 (nested templates). */
d7e74731
PA
10385 if (!buf.empty () && buf.string ().back () == '>')
10386 buf.puts (" >");
98bfdba5 10387 else
d7e74731 10388 buf.puts (">");
98bfdba5
PA
10389 }
10390 }
10391
9c37b5ae 10392 /* For C++ methods, append formal parameter type
94af9270 10393 information, if PHYSNAME. */
6e70227d 10394
94af9270 10395 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10396 && cu->language == language_cplus)
94af9270
KS
10397 {
10398 struct type *type = read_type_die (die, cu);
10399
d7e74731 10400 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10401 &type_print_raw_options);
94af9270 10402
9c37b5ae 10403 if (cu->language == language_cplus)
94af9270 10404 {
60430eff
DJ
10405 /* Assume that an artificial first parameter is
10406 "this", but do not crash if it is not. RealView
10407 marks unnamed (and thus unused) parameters as
10408 artificial; there is no way to differentiate
10409 the two cases. */
1f704f76 10410 if (type->num_fields () > 0
94af9270 10411 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10412 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10413 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10414 0))))
d7e74731 10415 buf.puts (" const");
94af9270
KS
10416 }
10417 }
10418
d7e74731 10419 const std::string &intermediate_name = buf.string ();
94af9270
KS
10420
10421 if (cu->language == language_cplus)
34a68019 10422 canonical_name
322a8516 10423 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10424 objfile);
34a68019
TT
10425
10426 /* If we only computed INTERMEDIATE_NAME, or if
10427 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10428 intern it. */
322a8516 10429 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10430 name = objfile->intern (intermediate_name);
34a68019
TT
10431 else
10432 name = canonical_name;
94af9270
KS
10433 }
10434 }
10435
10436 return name;
10437}
10438
0114d602
DJ
10439/* Return the fully qualified name of DIE, based on its DW_AT_name.
10440 If scope qualifiers are appropriate they will be added. The result
34a68019 10441 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10442 not have a name. NAME may either be from a previous call to
10443 dwarf2_name or NULL.
10444
9c37b5ae 10445 The output string will be canonicalized (if C++). */
0114d602
DJ
10446
10447static const char *
15d034d0 10448dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10449{
94af9270
KS
10450 return dwarf2_compute_name (name, die, cu, 0);
10451}
0114d602 10452
94af9270
KS
10453/* Construct a physname for the given DIE in CU. NAME may either be
10454 from a previous call to dwarf2_name or NULL. The result will be
10455 allocated on the objfile_objstack or NULL if the DIE does not have a
10456 name.
0114d602 10457
9c37b5ae 10458 The output string will be canonicalized (if C++). */
0114d602 10459
94af9270 10460static const char *
15d034d0 10461dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10462{
5e22e966 10463 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10464 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10465 int need_copy = 1;
10466
10467 /* In this case dwarf2_compute_name is just a shortcut not building anything
10468 on its own. */
10469 if (!die_needs_namespace (die, cu))
10470 return dwarf2_compute_name (name, die, cu, 1);
10471
906bb4c5
TT
10472 if (cu->language != language_rust)
10473 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10474
10475 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10476 has computed. */
791afaa2 10477 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10478 if (mangled != NULL)
900e11f9 10479 {
900e11f9 10480
59cc4834
JB
10481 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10482 {
10483 /* Do nothing (do not demangle the symbol name). */
10484 }
10485 else if (cu->language == language_go)
a766d390 10486 {
5e2db402
TT
10487 /* This is a lie, but we already lie to the caller new_symbol.
10488 new_symbol assumes we return the mangled name.
a766d390 10489 This just undoes that lie until things are cleaned up. */
a766d390
DE
10490 }
10491 else
10492 {
0eb876f5
JB
10493 /* Use DMGL_RET_DROP for C++ template functions to suppress
10494 their return type. It is easier for GDB users to search
10495 for such functions as `name(params)' than `long name(params)'.
10496 In such case the minimal symbol names do not match the full
10497 symbol names but for template functions there is never a need
10498 to look up their definition from their declaration so
10499 the only disadvantage remains the minimal symbol variant
10500 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10501 demangled.reset (gdb_demangle (mangled,
10502 (DMGL_PARAMS | DMGL_ANSI
10503 | DMGL_RET_DROP)));
a766d390 10504 }
900e11f9 10505 if (demangled)
791afaa2 10506 canon = demangled.get ();
900e11f9
JK
10507 else
10508 {
10509 canon = mangled;
10510 need_copy = 0;
10511 }
10512 }
10513
10514 if (canon == NULL || check_physname)
10515 {
10516 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10517
10518 if (canon != NULL && strcmp (physname, canon) != 0)
10519 {
10520 /* It may not mean a bug in GDB. The compiler could also
10521 compute DW_AT_linkage_name incorrectly. But in such case
10522 GDB would need to be bug-to-bug compatible. */
10523
b98664d3 10524 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10525 "(from linkage <%s>) - DIE at %s [in module %s]"),
10526 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10527 objfile_name (objfile));
900e11f9
JK
10528
10529 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10530 is available here - over computed PHYSNAME. It is safer
10531 against both buggy GDB and buggy compilers. */
10532
10533 retval = canon;
10534 }
10535 else
10536 {
10537 retval = physname;
10538 need_copy = 0;
10539 }
10540 }
10541 else
10542 retval = canon;
10543
10544 if (need_copy)
be1e3d3e 10545 retval = objfile->intern (retval);
900e11f9 10546
900e11f9 10547 return retval;
0114d602
DJ
10548}
10549
74921315
KS
10550/* Inspect DIE in CU for a namespace alias. If one exists, record
10551 a new symbol for it.
10552
10553 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10554
10555static int
10556read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10557{
10558 struct attribute *attr;
10559
10560 /* If the die does not have a name, this is not a namespace
10561 alias. */
10562 attr = dwarf2_attr (die, DW_AT_name, cu);
10563 if (attr != NULL)
10564 {
10565 int num;
10566 struct die_info *d = die;
10567 struct dwarf2_cu *imported_cu = cu;
10568
10569 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10570 keep inspecting DIEs until we hit the underlying import. */
10571#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10572 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10573 {
10574 attr = dwarf2_attr (d, DW_AT_import, cu);
10575 if (attr == NULL)
10576 break;
10577
10578 d = follow_die_ref (d, attr, &imported_cu);
10579 if (d->tag != DW_TAG_imported_declaration)
10580 break;
10581 }
10582
10583 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10584 {
b98664d3 10585 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10586 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10587 return 0;
10588 }
10589
10590 if (attr != NULL)
10591 {
10592 struct type *type;
0826b30a 10593 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10594
aa66c379 10595 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10596 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10597 {
10598 /* This declaration is a global namespace alias. Add
10599 a symbol for it whose type is the aliased namespace. */
10600 new_symbol (die, type, cu);
10601 return 1;
10602 }
10603 }
10604 }
10605
10606 return 0;
10607}
10608
22cee43f 10609/* Return the using directives repository (global or local?) to use in the
804d2729 10610 current context for CU.
22cee43f
PMR
10611
10612 For Ada, imported declarations can materialize renamings, which *may* be
10613 global. However it is impossible (for now?) in DWARF to distinguish
10614 "external" imported declarations and "static" ones. As all imported
10615 declarations seem to be static in all other languages, make them all CU-wide
10616 global only in Ada. */
10617
10618static struct using_direct **
804d2729 10619using_directives (struct dwarf2_cu *cu)
22cee43f 10620{
c24bdb02
KS
10621 if (cu->language == language_ada
10622 && cu->get_builder ()->outermost_context_p ())
10623 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10624 else
c24bdb02 10625 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10626}
10627
27aa8d6a
SW
10628/* Read the import statement specified by the given die and record it. */
10629
10630static void
10631read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10632{
5e22e966 10633 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10634 struct attribute *import_attr;
32019081 10635 struct die_info *imported_die, *child_die;
de4affc9 10636 struct dwarf2_cu *imported_cu;
27aa8d6a 10637 const char *imported_name;
794684b6 10638 const char *imported_name_prefix;
13387711
SW
10639 const char *canonical_name;
10640 const char *import_alias;
10641 const char *imported_declaration = NULL;
794684b6 10642 const char *import_prefix;
eb1e02fd 10643 std::vector<const char *> excludes;
13387711 10644
27aa8d6a
SW
10645 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10646 if (import_attr == NULL)
10647 {
b98664d3 10648 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10649 dwarf_tag_name (die->tag));
10650 return;
10651 }
10652
de4affc9
CC
10653 imported_cu = cu;
10654 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10655 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10656 if (imported_name == NULL)
10657 {
10658 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10659
10660 The import in the following code:
10661 namespace A
10662 {
10663 typedef int B;
10664 }
10665
10666 int main ()
10667 {
10668 using A::B;
10669 B b;
10670 return b;
10671 }
10672
10673 ...
10674 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10675 <52> DW_AT_decl_file : 1
10676 <53> DW_AT_decl_line : 6
10677 <54> DW_AT_import : <0x75>
10678 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10679 <59> DW_AT_name : B
10680 <5b> DW_AT_decl_file : 1
10681 <5c> DW_AT_decl_line : 2
10682 <5d> DW_AT_type : <0x6e>
10683 ...
10684 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10685 <76> DW_AT_byte_size : 4
10686 <77> DW_AT_encoding : 5 (signed)
10687
10688 imports the wrong die ( 0x75 instead of 0x58 ).
10689 This case will be ignored until the gcc bug is fixed. */
10690 return;
10691 }
10692
82856980
SW
10693 /* Figure out the local name after import. */
10694 import_alias = dwarf2_name (die, cu);
27aa8d6a 10695
794684b6
SW
10696 /* Figure out where the statement is being imported to. */
10697 import_prefix = determine_prefix (die, cu);
10698
10699 /* Figure out what the scope of the imported die is and prepend it
10700 to the name of the imported die. */
de4affc9 10701 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10702
f55ee35c
JK
10703 if (imported_die->tag != DW_TAG_namespace
10704 && imported_die->tag != DW_TAG_module)
794684b6 10705 {
13387711
SW
10706 imported_declaration = imported_name;
10707 canonical_name = imported_name_prefix;
794684b6 10708 }
13387711 10709 else if (strlen (imported_name_prefix) > 0)
12aaed36 10710 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10711 imported_name_prefix,
10712 (cu->language == language_d ? "." : "::"),
10713 imported_name, (char *) NULL);
13387711
SW
10714 else
10715 canonical_name = imported_name;
794684b6 10716
32019081
JK
10717 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10718 for (child_die = die->child; child_die && child_die->tag;
436c571c 10719 child_die = child_die->sibling)
32019081
JK
10720 {
10721 /* DWARF-4: A Fortran use statement with a “rename list” may be
10722 represented by an imported module entry with an import attribute
10723 referring to the module and owned entries corresponding to those
10724 entities that are renamed as part of being imported. */
10725
10726 if (child_die->tag != DW_TAG_imported_declaration)
10727 {
b98664d3 10728 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10729 "- DIE at %s [in module %s]"),
10730 sect_offset_str (child_die->sect_off),
10731 objfile_name (objfile));
32019081
JK
10732 continue;
10733 }
10734
10735 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10736 if (import_attr == NULL)
10737 {
b98664d3 10738 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10739 dwarf_tag_name (child_die->tag));
10740 continue;
10741 }
10742
10743 imported_cu = cu;
10744 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10745 &imported_cu);
10746 imported_name = dwarf2_name (imported_die, imported_cu);
10747 if (imported_name == NULL)
10748 {
b98664d3 10749 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10750 "imported name - DIE at %s [in module %s]"),
10751 sect_offset_str (child_die->sect_off),
10752 objfile_name (objfile));
32019081
JK
10753 continue;
10754 }
10755
eb1e02fd 10756 excludes.push_back (imported_name);
32019081
JK
10757
10758 process_die (child_die, cu);
10759 }
10760
804d2729 10761 add_using_directive (using_directives (cu),
22cee43f
PMR
10762 import_prefix,
10763 canonical_name,
10764 import_alias,
10765 imported_declaration,
10766 excludes,
10767 0,
10768 &objfile->objfile_obstack);
27aa8d6a
SW
10769}
10770
5230b05a
WT
10771/* ICC<14 does not output the required DW_AT_declaration on incomplete
10772 types, but gives them a size of zero. Starting with version 14,
10773 ICC is compatible with GCC. */
10774
9068261f 10775static bool
5230b05a
WT
10776producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10777{
10778 if (!cu->checked_producer)
10779 check_producer (cu);
10780
10781 return cu->producer_is_icc_lt_14;
10782}
10783
eb77c9df
AB
10784/* ICC generates a DW_AT_type for C void functions. This was observed on
10785 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10786 which says that void functions should not have a DW_AT_type. */
10787
10788static bool
10789producer_is_icc (struct dwarf2_cu *cu)
10790{
10791 if (!cu->checked_producer)
10792 check_producer (cu);
10793
10794 return cu->producer_is_icc;
10795}
10796
1b80a9fa
JK
10797/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10798 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10799 this, it was first present in GCC release 4.3.0. */
10800
9068261f 10801static bool
1b80a9fa
JK
10802producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10803{
10804 if (!cu->checked_producer)
10805 check_producer (cu);
10806
10807 return cu->producer_is_gcc_lt_4_3;
10808}
10809
d721ba37
PA
10810static file_and_directory
10811find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10812{
d721ba37
PA
10813 file_and_directory res;
10814
9291a0cd
TT
10815 /* Find the filename. Do not use dwarf2_name here, since the filename
10816 is not a source language identifier. */
d721ba37
PA
10817 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10818 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10819
d721ba37
PA
10820 if (res.comp_dir == NULL
10821 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10822 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10823 {
d721ba37
PA
10824 res.comp_dir_storage = ldirname (res.name);
10825 if (!res.comp_dir_storage.empty ())
10826 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10827 }
d721ba37 10828 if (res.comp_dir != NULL)
9291a0cd
TT
10829 {
10830 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10831 directory, get rid of it. */
d721ba37 10832 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10833
d721ba37
PA
10834 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10835 res.comp_dir = cp + 1;
9291a0cd
TT
10836 }
10837
d721ba37
PA
10838 if (res.name == NULL)
10839 res.name = "<unknown>";
10840
10841 return res;
9291a0cd
TT
10842}
10843
f4dc4d17
DE
10844/* Handle DW_AT_stmt_list for a compilation unit.
10845 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10846 COMP_DIR is the compilation directory. LOWPC is passed to
10847 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10848
10849static void
10850handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10851 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10852{
5e22e966 10853 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10854 struct attribute *attr;
527f3840
JK
10855 struct line_header line_header_local;
10856 hashval_t line_header_local_hash;
527f3840
JK
10857 void **slot;
10858 int decode_mapping;
2ab95328 10859
f4dc4d17
DE
10860 gdb_assert (! cu->per_cu->is_debug_types);
10861
2ab95328 10862 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10863 if (attr == NULL)
10864 return;
10865
9c541725 10866 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10867
10868 /* The line header hash table is only created if needed (it exists to
10869 prevent redundant reading of the line table for partial_units).
10870 If we're given a partial_unit, we'll need it. If we're given a
10871 compile_unit, then use the line header hash table if it's already
10872 created, but don't create one just yet. */
10873
5989a64e 10874 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10875 && die->tag == DW_TAG_partial_unit)
2ab95328 10876 {
5989a64e 10877 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10878 .reset (htab_create_alloc (127, line_header_hash_voidp,
10879 line_header_eq_voidp,
10880 free_line_header_voidp,
10881 xcalloc, xfree));
527f3840 10882 }
2ab95328 10883
9c541725 10884 line_header_local.sect_off = line_offset;
527f3840
JK
10885 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10886 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10887 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10888 {
5989a64e 10889 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10890 &line_header_local,
10891 line_header_local_hash, NO_INSERT);
10892
10893 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10894 is not present in *SLOT (since if there is something in *SLOT then
10895 it will be for a partial_unit). */
10896 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10897 {
527f3840 10898 gdb_assert (*slot != NULL);
9a3c8263 10899 cu->line_header = (struct line_header *) *slot;
527f3840 10900 return;
dee91e82 10901 }
2ab95328 10902 }
527f3840
JK
10903
10904 /* dwarf_decode_line_header does not yet provide sufficient information.
10905 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10906 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10907 if (lh == NULL)
527f3840 10908 return;
4c8aa72d
PA
10909
10910 cu->line_header = lh.release ();
10911 cu->line_header_die_owner = die;
527f3840 10912
5989a64e 10913 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10914 slot = NULL;
10915 else
10916 {
5989a64e 10917 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10918 &line_header_local,
10919 line_header_local_hash, INSERT);
10920 gdb_assert (slot != NULL);
10921 }
10922 if (slot != NULL && *slot == NULL)
10923 {
10924 /* This newly decoded line number information unit will be owned
10925 by line_header_hash hash table. */
10926 *slot = cu->line_header;
4c8aa72d 10927 cu->line_header_die_owner = NULL;
527f3840
JK
10928 }
10929 else
10930 {
10931 /* We cannot free any current entry in (*slot) as that struct line_header
10932 may be already used by multiple CUs. Create only temporary decoded
10933 line_header for this CU - it may happen at most once for each line
10934 number information unit. And if we're not using line_header_hash
10935 then this is what we want as well. */
10936 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10937 }
10938 decode_mapping = (die->tag != DW_TAG_partial_unit);
10939 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10940 decode_mapping);
fff8551c 10941
2ab95328
TT
10942}
10943
95554aad 10944/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10945
c906108c 10946static void
e7c27a73 10947read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10948{
5e22e966 10949 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 10950 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10951 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10952 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10953 CORE_ADDR highpc = ((CORE_ADDR) 0);
10954 struct attribute *attr;
c906108c 10955 struct die_info *child_die;
e142c38c 10956 CORE_ADDR baseaddr;
6e70227d 10957
380618d6 10958 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10959 baseaddr = objfile->text_section_offset ();
c906108c 10960
fae299cd 10961 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10962
10963 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10964 from finish_block. */
2acceee2 10965 if (lowpc == ((CORE_ADDR) -1))
c906108c 10966 lowpc = highpc;
3e29f34a 10967 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10968
d721ba37 10969 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10970
f4b8a18d
KW
10971 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10972 standardised yet. As a workaround for the language detection we fall
10973 back to the DW_AT_producer string. */
10974 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10975 cu->language = language_opencl;
10976
3019eac3
DE
10977 /* Similar hack for Go. */
10978 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10979 set_cu_language (DW_LANG_Go, cu);
10980
c24bdb02 10981 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10982
10983 /* Decode line number information if present. We do this before
10984 processing child DIEs, so that the line header table is available
10985 for DW_AT_decl_file. */
d721ba37 10986 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10987
10988 /* Process all dies in compilation unit. */
10989 if (die->child != NULL)
10990 {
10991 child_die = die->child;
10992 while (child_die && child_die->tag)
10993 {
10994 process_die (child_die, cu);
436c571c 10995 child_die = child_die->sibling;
3019eac3
DE
10996 }
10997 }
10998
10999 /* Decode macro information, if present. Dwarf 2 macro information
11000 refers to information in the line number info statement program
11001 header, so we can only read it if we've read the header
11002 successfully. */
0af92d60
JK
11003 attr = dwarf2_attr (die, DW_AT_macros, cu);
11004 if (attr == NULL)
11005 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11006 if (attr && cu->line_header)
11007 {
11008 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11009 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11010
43f3e411 11011 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11012 }
11013 else
11014 {
11015 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11016 if (attr && cu->line_header)
11017 {
11018 unsigned int macro_offset = DW_UNSND (attr);
11019
43f3e411 11020 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11021 }
11022 }
3019eac3
DE
11023}
11024
c24bdb02
KS
11025void
11026dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11027{
f4dc4d17
DE
11028 struct type_unit_group *tu_group;
11029 int first_time;
3019eac3 11030 struct attribute *attr;
9c541725 11031 unsigned int i;
0186c6a7 11032 struct signatured_type *sig_type;
3019eac3 11033
f4dc4d17 11034 gdb_assert (per_cu->is_debug_types);
0186c6a7 11035 sig_type = (struct signatured_type *) per_cu;
3019eac3 11036
c24bdb02 11037 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11038
f4dc4d17 11039 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11040 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11041 if (sig_type->type_unit_group == NULL)
c24bdb02 11042 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11043 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11044
11045 /* If we've already processed this stmt_list there's no real need to
11046 do it again, we could fake it and just recreate the part we need
11047 (file name,index -> symtab mapping). If data shows this optimization
11048 is useful we can do it then. */
8adb8487
TT
11049 type_unit_group_unshareable *tug_unshare
11050 = per_objfile->get_type_unit_group_unshareable (tu_group);
11051 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11052
11053 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11054 debug info. */
fff8551c 11055 line_header_up lh;
f4dc4d17 11056 if (attr != NULL)
3019eac3 11057 {
9c541725 11058 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11059 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11060 }
11061 if (lh == NULL)
11062 {
11063 if (first_time)
c24bdb02 11064 start_symtab ("", NULL, 0);
f4dc4d17
DE
11065 else
11066 {
8adb8487 11067 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11068 gdb_assert (m_builder == nullptr);
8adb8487 11069 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11070 m_builder.reset (new struct buildsym_compunit
11071 (COMPUNIT_OBJFILE (cust), "",
11072 COMPUNIT_DIRNAME (cust),
11073 compunit_language (cust),
11074 0, cust));
770479f2 11075 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11076 }
f4dc4d17 11077 return;
3019eac3
DE
11078 }
11079
c24bdb02
KS
11080 line_header = lh.release ();
11081 line_header_die_owner = die;
3019eac3 11082
f4dc4d17
DE
11083 if (first_time)
11084 {
c24bdb02 11085 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11086
1fd60fc0
DE
11087 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11088 still initializing it, and our caller (a few levels up)
11089 process_full_type_unit still needs to know if this is the first
11090 time. */
11091
8adb8487 11092 tug_unshare->symtabs
4ac93832
TT
11093 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11094 struct symtab *, line_header->file_names_size ());
3019eac3 11095
7ba99d21
AT
11096 auto &file_names = line_header->file_names ();
11097 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11098 {
7ba99d21 11099 file_entry &fe = file_names[i];
c24bdb02
KS
11100 dwarf2_start_subfile (this, fe.name,
11101 fe.include_dir (line_header));
11102 buildsym_compunit *b = get_builder ();
11103 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11104 {
4c8aa72d
PA
11105 /* NOTE: start_subfile will recognize when it's been
11106 passed a file it has already seen. So we can't
11107 assume there's a simple mapping from
11108 cu->line_header->file_names to subfiles, plus
11109 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11110 b->get_current_subfile ()->symtab
11111 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11112 }
11113
c24bdb02 11114 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11115 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11116 }
11117 }
11118 else
3019eac3 11119 {
c24bdb02 11120 gdb_assert (m_builder == nullptr);
8adb8487 11121 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11122 m_builder.reset (new struct buildsym_compunit
11123 (COMPUNIT_OBJFILE (cust), "",
11124 COMPUNIT_DIRNAME (cust),
11125 compunit_language (cust),
11126 0, cust));
770479f2 11127 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11128
7ba99d21
AT
11129 auto &file_names = line_header->file_names ();
11130 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11131 {
7ba99d21 11132 file_entry &fe = file_names[i];
8adb8487 11133 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11134 }
3019eac3
DE
11135 }
11136
f4dc4d17
DE
11137 /* The main symtab is allocated last. Type units don't have DW_AT_name
11138 so they don't have a "real" (so to speak) symtab anyway.
11139 There is later code that will assign the main symtab to all symbols
11140 that don't have one. We need to handle the case of a symbol with a
11141 missing symtab (DW_AT_decl_file) anyway. */
11142}
3019eac3 11143
f4dc4d17
DE
11144/* Process DW_TAG_type_unit.
11145 For TUs we want to skip the first top level sibling if it's not the
11146 actual type being defined by this TU. In this case the first top
11147 level sibling is there to provide context only. */
3019eac3 11148
f4dc4d17
DE
11149static void
11150read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11151{
11152 struct die_info *child_die;
3019eac3 11153
f4dc4d17
DE
11154 prepare_one_comp_unit (cu, die, language_minimal);
11155
11156 /* Initialize (or reinitialize) the machinery for building symtabs.
11157 We do this before processing child DIEs, so that the line header table
11158 is available for DW_AT_decl_file. */
c24bdb02 11159 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11160
11161 if (die->child != NULL)
11162 {
11163 child_die = die->child;
11164 while (child_die && child_die->tag)
11165 {
11166 process_die (child_die, cu);
436c571c 11167 child_die = child_die->sibling;
f4dc4d17
DE
11168 }
11169 }
3019eac3
DE
11170}
11171\f
80626a55
DE
11172/* DWO/DWP files.
11173
11174 http://gcc.gnu.org/wiki/DebugFission
11175 http://gcc.gnu.org/wiki/DebugFissionDWP
11176
11177 To simplify handling of both DWO files ("object" files with the DWARF info)
11178 and DWP files (a file with the DWOs packaged up into one file), we treat
11179 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11180
11181static hashval_t
11182hash_dwo_file (const void *item)
11183{
9a3c8263 11184 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11185 hashval_t hash;
3019eac3 11186
a2ce51a0
DE
11187 hash = htab_hash_string (dwo_file->dwo_name);
11188 if (dwo_file->comp_dir != NULL)
11189 hash += htab_hash_string (dwo_file->comp_dir);
11190 return hash;
3019eac3
DE
11191}
11192
11193static int
11194eq_dwo_file (const void *item_lhs, const void *item_rhs)
11195{
9a3c8263
SM
11196 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11197 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11198
a2ce51a0
DE
11199 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11200 return 0;
11201 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11202 return lhs->comp_dir == rhs->comp_dir;
11203 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11204}
11205
11206/* Allocate a hash table for DWO files. */
11207
51ac9db5 11208static htab_up
298e9637 11209allocate_dwo_file_hash_table ()
3019eac3 11210{
51ac9db5
SM
11211 auto delete_dwo_file = [] (void *item)
11212 {
11213 struct dwo_file *dwo_file = (struct dwo_file *) item;
11214
11215 delete dwo_file;
11216 };
11217
bc68fb19
TT
11218 return htab_up (htab_create_alloc (41,
11219 hash_dwo_file,
11220 eq_dwo_file,
11221 delete_dwo_file,
11222 xcalloc, xfree));
3019eac3
DE
11223}
11224
80626a55
DE
11225/* Lookup DWO file DWO_NAME. */
11226
11227static void **
ed2dc618
SM
11228lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11229 const char *dwo_name,
11230 const char *comp_dir)
80626a55
DE
11231{
11232 struct dwo_file find_entry;
11233 void **slot;
11234
5989a64e
SM
11235 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11236 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11237
0ac5b59e
DE
11238 find_entry.dwo_name = dwo_name;
11239 find_entry.comp_dir = comp_dir;
5989a64e 11240 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11241 INSERT);
80626a55
DE
11242
11243 return slot;
11244}
11245
3019eac3
DE
11246static hashval_t
11247hash_dwo_unit (const void *item)
11248{
9a3c8263 11249 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11250
11251 /* This drops the top 32 bits of the id, but is ok for a hash. */
11252 return dwo_unit->signature;
11253}
11254
11255static int
11256eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11257{
9a3c8263
SM
11258 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11259 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11260
11261 /* The signature is assumed to be unique within the DWO file.
11262 So while object file CU dwo_id's always have the value zero,
11263 that's OK, assuming each object file DWO file has only one CU,
11264 and that's the rule for now. */
11265 return lhs->signature == rhs->signature;
11266}
11267
11268/* Allocate a hash table for DWO CUs,TUs.
11269 There is one of these tables for each of CUs,TUs for each DWO file. */
11270
b0b6a987 11271static htab_up
298e9637 11272allocate_dwo_unit_table ()
3019eac3
DE
11273{
11274 /* Start out with a pretty small number.
11275 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11276 return htab_up (htab_create_alloc (3,
11277 hash_dwo_unit,
11278 eq_dwo_unit,
11279 NULL, xcalloc, xfree));
3019eac3
DE
11280}
11281
19c3d4c9 11282/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11283
11284static void
19c3d4c9
DE
11285create_dwo_cu_reader (const struct die_reader_specs *reader,
11286 const gdb_byte *info_ptr,
11287 struct die_info *comp_unit_die,
c0ab21c2
TT
11288 struct dwo_file *dwo_file,
11289 struct dwo_unit *dwo_unit)
3019eac3
DE
11290{
11291 struct dwarf2_cu *cu = reader->cu;
9c541725 11292 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11293 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11294
a084a2a6
AT
11295 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11296 if (!signature.has_value ())
3019eac3 11297 {
b98664d3 11298 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11299 " its dwo_id [in module %s]"),
9d8780f0 11300 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11301 return;
11302 }
11303
3019eac3 11304 dwo_unit->dwo_file = dwo_file;
a084a2a6 11305 dwo_unit->signature = *signature;
8a0459fd 11306 dwo_unit->section = section;
9c541725 11307 dwo_unit->sect_off = sect_off;
3019eac3
DE
11308 dwo_unit->length = cu->per_cu->length;
11309
b4f54984 11310 if (dwarf_read_debug)
9d8780f0
SM
11311 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11312 sect_offset_str (sect_off),
9c541725 11313 hex_string (dwo_unit->signature));
3019eac3
DE
11314}
11315
33c5cd75 11316/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11317 Note: This function processes DWO files only, not DWP files. */
3019eac3 11318
33c5cd75 11319static void
ed2dc618 11320create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11321 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11322 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11323{
11324 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11325 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11326 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11327
96b79293 11328 section.read (objfile);
33c5cd75 11329 info_ptr = section.buffer;
3019eac3
DE
11330
11331 if (info_ptr == NULL)
33c5cd75 11332 return;
3019eac3 11333
b4f54984 11334 if (dwarf_read_debug)
19c3d4c9
DE
11335 {
11336 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11337 section.get_name (),
11338 section.get_file_name ());
19c3d4c9 11339 }
3019eac3 11340
33c5cd75 11341 end_ptr = info_ptr + section.size;
3019eac3
DE
11342 while (info_ptr < end_ptr)
11343 {
11344 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11345 struct dwo_unit read_unit {};
33c5cd75
DB
11346 struct dwo_unit *dwo_unit;
11347 void **slot;
11348 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11349
11350 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11351 per_cu.per_bfd = per_bfd;
3019eac3 11352 per_cu.is_debug_types = 0;
33c5cd75
DB
11353 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11354 per_cu.section = &section;
11355
ab432490 11356 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
c0ab21c2
TT
11357 if (!reader.dummy_p)
11358 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11359 &dwo_file, &read_unit);
33c5cd75
DB
11360 info_ptr += per_cu.length;
11361
11362 // If the unit could not be parsed, skip it.
c0ab21c2 11363 if (read_unit.dwo_file == NULL)
33c5cd75 11364 continue;
3019eac3 11365
33c5cd75 11366 if (cus_htab == NULL)
298e9637 11367 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11368
1859c670 11369 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11370 struct dwo_unit);
c0ab21c2 11371 *dwo_unit = read_unit;
b0b6a987 11372 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11373 gdb_assert (slot != NULL);
11374 if (*slot != NULL)
19c3d4c9 11375 {
33c5cd75
DB
11376 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11377 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11378
b98664d3 11379 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11380 " the entry at offset %s, signature %s"),
11381 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11382 hex_string (dwo_unit->signature));
19c3d4c9 11383 }
33c5cd75 11384 *slot = (void *)dwo_unit;
3019eac3 11385 }
3019eac3
DE
11386}
11387
80626a55
DE
11388/* DWP file .debug_{cu,tu}_index section format:
11389 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11390
d2415c6c
DE
11391 DWP Version 1:
11392
80626a55
DE
11393 Both index sections have the same format, and serve to map a 64-bit
11394 signature to a set of section numbers. Each section begins with a header,
11395 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11396 indexes, and a pool of 32-bit section numbers. The index sections will be
11397 aligned at 8-byte boundaries in the file.
11398
d2415c6c
DE
11399 The index section header consists of:
11400
11401 V, 32 bit version number
11402 -, 32 bits unused
11403 N, 32 bit number of compilation units or type units in the index
11404 M, 32 bit number of slots in the hash table
80626a55 11405
d2415c6c 11406 Numbers are recorded using the byte order of the application binary.
80626a55 11407
d2415c6c
DE
11408 The hash table begins at offset 16 in the section, and consists of an array
11409 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11410 order of the application binary). Unused slots in the hash table are 0.
11411 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11412
d2415c6c
DE
11413 The parallel table begins immediately after the hash table
11414 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11415 array of 32-bit indexes (using the byte order of the application binary),
11416 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11417 table contains a 32-bit index into the pool of section numbers. For unused
11418 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11419
73869dc2
DE
11420 The pool of section numbers begins immediately following the hash table
11421 (at offset 16 + 12 * M from the beginning of the section). The pool of
11422 section numbers consists of an array of 32-bit words (using the byte order
11423 of the application binary). Each item in the array is indexed starting
11424 from 0. The hash table entry provides the index of the first section
11425 number in the set. Additional section numbers in the set follow, and the
11426 set is terminated by a 0 entry (section number 0 is not used in ELF).
11427
11428 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11429 section must be the first entry in the set, and the .debug_abbrev.dwo must
11430 be the second entry. Other members of the set may follow in any order.
11431
11432 ---
11433
11434 DWP Version 2:
11435
11436 DWP Version 2 combines all the .debug_info, etc. sections into one,
11437 and the entries in the index tables are now offsets into these sections.
11438 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11439 section.
11440
11441 Index Section Contents:
11442 Header
11443 Hash Table of Signatures dwp_hash_table.hash_table
11444 Parallel Table of Indices dwp_hash_table.unit_table
11445 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11446 Table of Section Sizes dwp_hash_table.v2.sizes
11447
11448 The index section header consists of:
11449
11450 V, 32 bit version number
11451 L, 32 bit number of columns in the table of section offsets
11452 N, 32 bit number of compilation units or type units in the index
11453 M, 32 bit number of slots in the hash table
11454
11455 Numbers are recorded using the byte order of the application binary.
11456
11457 The hash table has the same format as version 1.
11458 The parallel table of indices has the same format as version 1,
11459 except that the entries are origin-1 indices into the table of sections
11460 offsets and the table of section sizes.
11461
11462 The table of offsets begins immediately following the parallel table
11463 (at offset 16 + 12 * M from the beginning of the section). The table is
11464 a two-dimensional array of 32-bit words (using the byte order of the
11465 application binary), with L columns and N+1 rows, in row-major order.
11466 Each row in the array is indexed starting from 0. The first row provides
11467 a key to the remaining rows: each column in this row provides an identifier
11468 for a debug section, and the offsets in the same column of subsequent rows
11469 refer to that section. The section identifiers are:
11470
11471 DW_SECT_INFO 1 .debug_info.dwo
11472 DW_SECT_TYPES 2 .debug_types.dwo
11473 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11474 DW_SECT_LINE 4 .debug_line.dwo
11475 DW_SECT_LOC 5 .debug_loc.dwo
11476 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11477 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11478 DW_SECT_MACRO 8 .debug_macro.dwo
11479
11480 The offsets provided by the CU and TU index sections are the base offsets
11481 for the contributions made by each CU or TU to the corresponding section
11482 in the package file. Each CU and TU header contains an abbrev_offset
11483 field, used to find the abbreviations table for that CU or TU within the
11484 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11485 be interpreted as relative to the base offset given in the index section.
11486 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11487 should be interpreted as relative to the base offset for .debug_line.dwo,
11488 and offsets into other debug sections obtained from DWARF attributes should
11489 also be interpreted as relative to the corresponding base offset.
11490
11491 The table of sizes begins immediately following the table of offsets.
11492 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11493 with L columns and N rows, in row-major order. Each row in the array is
11494 indexed starting from 1 (row 0 is shared by the two tables).
11495
11496 ---
11497
11498 Hash table lookup is handled the same in version 1 and 2:
11499
11500 We assume that N and M will not exceed 2^32 - 1.
11501 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11502
d2415c6c
DE
11503 Given a 64-bit compilation unit signature or a type signature S, an entry
11504 in the hash table is located as follows:
80626a55 11505
d2415c6c
DE
11506 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11507 the low-order k bits all set to 1.
80626a55 11508
d2415c6c 11509 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11510
d2415c6c
DE
11511 3) If the hash table entry at index H matches the signature, use that
11512 entry. If the hash table entry at index H is unused (all zeroes),
11513 terminate the search: the signature is not present in the table.
80626a55 11514
d2415c6c 11515 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11516
d2415c6c 11517 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11518 to stop at an unused slot or find the match. */
80626a55
DE
11519
11520/* Create a hash table to map DWO IDs to their CU/TU entry in
11521 .debug_{info,types}.dwo in DWP_FILE.
11522 Returns NULL if there isn't one.
11523 Note: This function processes DWP files only, not DWO files. */
11524
11525static struct dwp_hash_table *
ed2dc618
SM
11526create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11527 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11528{
11529 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11530 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11531 const gdb_byte *index_ptr, *index_end;
80626a55 11532 struct dwarf2_section_info *index;
73869dc2 11533 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11534 struct dwp_hash_table *htab;
11535
11536 if (is_debug_types)
11537 index = &dwp_file->sections.tu_index;
11538 else
11539 index = &dwp_file->sections.cu_index;
11540
96b79293 11541 if (index->empty ())
80626a55 11542 return NULL;
96b79293 11543 index->read (objfile);
80626a55
DE
11544
11545 index_ptr = index->buffer;
11546 index_end = index_ptr + index->size;
11547
11548 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11549 index_ptr += 4;
11550 if (version == 2)
11551 nr_columns = read_4_bytes (dbfd, index_ptr);
11552 else
11553 nr_columns = 0;
11554 index_ptr += 4;
80626a55
DE
11555 nr_units = read_4_bytes (dbfd, index_ptr);
11556 index_ptr += 4;
11557 nr_slots = read_4_bytes (dbfd, index_ptr);
11558 index_ptr += 4;
11559
73869dc2 11560 if (version != 1 && version != 2)
80626a55 11561 {
21aa081e 11562 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11563 " [in module %s]"),
21aa081e 11564 pulongest (version), dwp_file->name);
80626a55
DE
11565 }
11566 if (nr_slots != (nr_slots & -nr_slots))
11567 {
21aa081e 11568 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11569 " is not power of 2 [in module %s]"),
21aa081e 11570 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11571 }
11572
5989a64e 11573 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11574 htab->version = version;
11575 htab->nr_columns = nr_columns;
80626a55
DE
11576 htab->nr_units = nr_units;
11577 htab->nr_slots = nr_slots;
11578 htab->hash_table = index_ptr;
11579 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11580
11581 /* Exit early if the table is empty. */
11582 if (nr_slots == 0 || nr_units == 0
11583 || (version == 2 && nr_columns == 0))
11584 {
11585 /* All must be zero. */
11586 if (nr_slots != 0 || nr_units != 0
11587 || (version == 2 && nr_columns != 0))
11588 {
b98664d3 11589 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11590 " all zero [in modules %s]"),
11591 dwp_file->name);
11592 }
11593 return htab;
11594 }
11595
11596 if (version == 1)
11597 {
11598 htab->section_pool.v1.indices =
11599 htab->unit_table + sizeof (uint32_t) * nr_slots;
11600 /* It's harder to decide whether the section is too small in v1.
11601 V1 is deprecated anyway so we punt. */
11602 }
11603 else
11604 {
11605 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11606 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11607 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11608 /* Reverse map for error checking. */
11609 int ids_seen[DW_SECT_MAX + 1];
11610 int i;
11611
11612 if (nr_columns < 2)
11613 {
11614 error (_("Dwarf Error: bad DWP hash table, too few columns"
11615 " in section table [in module %s]"),
11616 dwp_file->name);
11617 }
11618 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11619 {
11620 error (_("Dwarf Error: bad DWP hash table, too many columns"
11621 " in section table [in module %s]"),
11622 dwp_file->name);
11623 }
04fd5eed
GB
11624 memset (ids, 255, sizeof_ids);
11625 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11626 for (i = 0; i < nr_columns; ++i)
11627 {
11628 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11629
11630 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11631 {
11632 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11633 " in section table [in module %s]"),
11634 id, dwp_file->name);
11635 }
11636 if (ids_seen[id] != -1)
11637 {
11638 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11639 " id %d in section table [in module %s]"),
11640 id, dwp_file->name);
11641 }
11642 ids_seen[id] = i;
11643 ids[i] = id;
11644 }
11645 /* Must have exactly one info or types section. */
11646 if (((ids_seen[DW_SECT_INFO] != -1)
11647 + (ids_seen[DW_SECT_TYPES] != -1))
11648 != 1)
11649 {
11650 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11651 " DWO info/types section [in module %s]"),
11652 dwp_file->name);
11653 }
11654 /* Must have an abbrev section. */
11655 if (ids_seen[DW_SECT_ABBREV] == -1)
11656 {
11657 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11658 " section [in module %s]"),
11659 dwp_file->name);
11660 }
11661 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11662 htab->section_pool.v2.sizes =
11663 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11664 * nr_units * nr_columns);
11665 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11666 * nr_units * nr_columns))
11667 > index_end)
11668 {
11669 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11670 " [in module %s]"),
11671 dwp_file->name);
11672 }
11673 }
80626a55
DE
11674
11675 return htab;
11676}
11677
11678/* Update SECTIONS with the data from SECTP.
11679
11680 This function is like the other "locate" section routines that are
11681 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11682 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11683
11684 The result is non-zero for success, or zero if an error was found. */
11685
11686static int
73869dc2
DE
11687locate_v1_virtual_dwo_sections (asection *sectp,
11688 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11689{
11690 const struct dwop_section_names *names = &dwop_section_names;
11691
11692 if (section_is_p (sectp->name, &names->abbrev_dwo))
11693 {
11694 /* There can be only one. */
049412e3 11695 if (sections->abbrev.s.section != NULL)
80626a55 11696 return 0;
049412e3 11697 sections->abbrev.s.section = sectp;
fd361982 11698 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11699 }
11700 else if (section_is_p (sectp->name, &names->info_dwo)
11701 || section_is_p (sectp->name, &names->types_dwo))
11702 {
11703 /* There can be only one. */
049412e3 11704 if (sections->info_or_types.s.section != NULL)
80626a55 11705 return 0;
049412e3 11706 sections->info_or_types.s.section = sectp;
fd361982 11707 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11708 }
11709 else if (section_is_p (sectp->name, &names->line_dwo))
11710 {
11711 /* There can be only one. */
049412e3 11712 if (sections->line.s.section != NULL)
80626a55 11713 return 0;
049412e3 11714 sections->line.s.section = sectp;
fd361982 11715 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11716 }
11717 else if (section_is_p (sectp->name, &names->loc_dwo))
11718 {
11719 /* There can be only one. */
049412e3 11720 if (sections->loc.s.section != NULL)
80626a55 11721 return 0;
049412e3 11722 sections->loc.s.section = sectp;
fd361982 11723 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11724 }
11725 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11726 {
11727 /* There can be only one. */
049412e3 11728 if (sections->macinfo.s.section != NULL)
80626a55 11729 return 0;
049412e3 11730 sections->macinfo.s.section = sectp;
fd361982 11731 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11732 }
11733 else if (section_is_p (sectp->name, &names->macro_dwo))
11734 {
11735 /* There can be only one. */
049412e3 11736 if (sections->macro.s.section != NULL)
80626a55 11737 return 0;
049412e3 11738 sections->macro.s.section = sectp;
fd361982 11739 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11740 }
11741 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11742 {
11743 /* There can be only one. */
049412e3 11744 if (sections->str_offsets.s.section != NULL)
80626a55 11745 return 0;
049412e3 11746 sections->str_offsets.s.section = sectp;
fd361982 11747 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11748 }
11749 else
11750 {
11751 /* No other kind of section is valid. */
11752 return 0;
11753 }
11754
11755 return 1;
11756}
11757
73869dc2
DE
11758/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11759 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11760 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11761 This is for DWP version 1 files. */
80626a55
DE
11762
11763static struct dwo_unit *
ed2dc618
SM
11764create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11765 struct dwp_file *dwp_file,
73869dc2
DE
11766 uint32_t unit_index,
11767 const char *comp_dir,
11768 ULONGEST signature, int is_debug_types)
80626a55 11769{
73869dc2
DE
11770 const struct dwp_hash_table *dwp_htab =
11771 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11772 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11773 const char *kind = is_debug_types ? "TU" : "CU";
11774 struct dwo_file *dwo_file;
11775 struct dwo_unit *dwo_unit;
73869dc2 11776 struct virtual_v1_dwo_sections sections;
80626a55 11777 void **dwo_file_slot;
80626a55
DE
11778 int i;
11779
73869dc2
DE
11780 gdb_assert (dwp_file->version == 1);
11781
b4f54984 11782 if (dwarf_read_debug)
80626a55 11783 {
73869dc2 11784 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11785 kind,
73869dc2 11786 pulongest (unit_index), hex_string (signature),
80626a55
DE
11787 dwp_file->name);
11788 }
11789
19ac8c2e 11790 /* Fetch the sections of this DWO unit.
80626a55
DE
11791 Put a limit on the number of sections we look for so that bad data
11792 doesn't cause us to loop forever. */
11793
73869dc2 11794#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11795 (1 /* .debug_info or .debug_types */ \
11796 + 1 /* .debug_abbrev */ \
11797 + 1 /* .debug_line */ \
11798 + 1 /* .debug_loc */ \
11799 + 1 /* .debug_str_offsets */ \
19ac8c2e 11800 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11801 + 1 /* trailing zero */)
11802
11803 memset (&sections, 0, sizeof (sections));
80626a55 11804
73869dc2 11805 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11806 {
11807 asection *sectp;
11808 uint32_t section_nr =
11809 read_4_bytes (dbfd,
73869dc2
DE
11810 dwp_htab->section_pool.v1.indices
11811 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11812
11813 if (section_nr == 0)
11814 break;
11815 if (section_nr >= dwp_file->num_sections)
11816 {
11817 error (_("Dwarf Error: bad DWP hash table, section number too large"
11818 " [in module %s]"),
11819 dwp_file->name);
11820 }
11821
11822 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11823 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11824 {
11825 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11826 " [in module %s]"),
11827 dwp_file->name);
11828 }
11829 }
11830
11831 if (i < 2
96b79293
TT
11832 || sections.info_or_types.empty ()
11833 || sections.abbrev.empty ())
80626a55
DE
11834 {
11835 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11836 " [in module %s]"),
11837 dwp_file->name);
11838 }
73869dc2 11839 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11840 {
11841 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11842 " [in module %s]"),
11843 dwp_file->name);
11844 }
11845
11846 /* It's easier for the rest of the code if we fake a struct dwo_file and
11847 have dwo_unit "live" in that. At least for now.
11848
11849 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11850 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11851 file, we can combine them back into a virtual DWO file to save space
11852 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11853 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11854
791afaa2
TT
11855 std::string virtual_dwo_name =
11856 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11857 sections.abbrev.get_id (),
11858 sections.line.get_id (),
11859 sections.loc.get_id (),
11860 sections.str_offsets.get_id ());
80626a55 11861 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11862 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11863 virtual_dwo_name.c_str (),
11864 comp_dir);
80626a55
DE
11865 /* Create one if necessary. */
11866 if (*dwo_file_slot == NULL)
11867 {
b4f54984 11868 if (dwarf_read_debug)
80626a55
DE
11869 {
11870 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11871 virtual_dwo_name.c_str ());
80626a55 11872 }
51ac9db5 11873 dwo_file = new struct dwo_file;
45940949 11874 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11875 dwo_file->comp_dir = comp_dir;
80626a55
DE
11876 dwo_file->sections.abbrev = sections.abbrev;
11877 dwo_file->sections.line = sections.line;
11878 dwo_file->sections.loc = sections.loc;
11879 dwo_file->sections.macinfo = sections.macinfo;
11880 dwo_file->sections.macro = sections.macro;
11881 dwo_file->sections.str_offsets = sections.str_offsets;
11882 /* The "str" section is global to the entire DWP file. */
11883 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11884 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11885 there's no need to record it in dwo_file.
11886 Also, we can't simply record type sections in dwo_file because
11887 we record a pointer into the vector in dwo_unit. As we collect more
11888 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11889 for it, invalidating all copies of pointers into the previous
11890 contents. */
80626a55
DE
11891 *dwo_file_slot = dwo_file;
11892 }
11893 else
11894 {
b4f54984 11895 if (dwarf_read_debug)
80626a55
DE
11896 {
11897 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11898 virtual_dwo_name.c_str ());
80626a55 11899 }
9a3c8263 11900 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11901 }
80626a55 11902
5989a64e 11903 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11904 dwo_unit->dwo_file = dwo_file;
11905 dwo_unit->signature = signature;
8d749320 11906 dwo_unit->section =
5989a64e 11907 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11908 *dwo_unit->section = sections.info_or_types;
57d63ce2 11909 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11910
11911 return dwo_unit;
11912}
11913
73869dc2
DE
11914/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11915 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11916 piece within that section used by a TU/CU, return a virtual section
11917 of just that piece. */
11918
11919static struct dwarf2_section_info
ed2dc618
SM
11920create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11921 struct dwarf2_section_info *section,
73869dc2
DE
11922 bfd_size_type offset, bfd_size_type size)
11923{
11924 struct dwarf2_section_info result;
11925 asection *sectp;
11926
11927 gdb_assert (section != NULL);
11928 gdb_assert (!section->is_virtual);
11929
11930 memset (&result, 0, sizeof (result));
11931 result.s.containing_section = section;
dc4ccb6f 11932 result.is_virtual = true;
73869dc2
DE
11933
11934 if (size == 0)
11935 return result;
11936
96b79293 11937 sectp = section->get_bfd_section ();
73869dc2
DE
11938
11939 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11940 bounds of the real section. This is a pretty-rare event, so just
11941 flag an error (easier) instead of a warning and trying to cope. */
11942 if (sectp == NULL
fd361982 11943 || offset + size > bfd_section_size (sectp))
73869dc2 11944 {
73869dc2
DE
11945 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11946 " in section %s [in module %s]"),
fd361982 11947 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11948 objfile_name (dwarf2_per_objfile->objfile));
11949 }
11950
11951 result.virtual_offset = offset;
11952 result.size = size;
11953 return result;
11954}
11955
11956/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11957 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11958 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11959 This is for DWP version 2 files. */
11960
11961static struct dwo_unit *
ed2dc618
SM
11962create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11963 struct dwp_file *dwp_file,
73869dc2
DE
11964 uint32_t unit_index,
11965 const char *comp_dir,
11966 ULONGEST signature, int is_debug_types)
11967{
73869dc2
DE
11968 const struct dwp_hash_table *dwp_htab =
11969 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11970 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11971 const char *kind = is_debug_types ? "TU" : "CU";
11972 struct dwo_file *dwo_file;
11973 struct dwo_unit *dwo_unit;
11974 struct virtual_v2_dwo_sections sections;
11975 void **dwo_file_slot;
73869dc2
DE
11976 int i;
11977
11978 gdb_assert (dwp_file->version == 2);
11979
b4f54984 11980 if (dwarf_read_debug)
73869dc2
DE
11981 {
11982 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11983 kind,
11984 pulongest (unit_index), hex_string (signature),
11985 dwp_file->name);
11986 }
11987
11988 /* Fetch the section offsets of this DWO unit. */
11989
11990 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11991
11992 for (i = 0; i < dwp_htab->nr_columns; ++i)
11993 {
11994 uint32_t offset = read_4_bytes (dbfd,
11995 dwp_htab->section_pool.v2.offsets
11996 + (((unit_index - 1) * dwp_htab->nr_columns
11997 + i)
11998 * sizeof (uint32_t)));
11999 uint32_t size = read_4_bytes (dbfd,
12000 dwp_htab->section_pool.v2.sizes
12001 + (((unit_index - 1) * dwp_htab->nr_columns
12002 + i)
12003 * sizeof (uint32_t)));
12004
12005 switch (dwp_htab->section_pool.v2.section_ids[i])
12006 {
12007 case DW_SECT_INFO:
12008 case DW_SECT_TYPES:
12009 sections.info_or_types_offset = offset;
12010 sections.info_or_types_size = size;
12011 break;
12012 case DW_SECT_ABBREV:
12013 sections.abbrev_offset = offset;
12014 sections.abbrev_size = size;
12015 break;
12016 case DW_SECT_LINE:
12017 sections.line_offset = offset;
12018 sections.line_size = size;
12019 break;
12020 case DW_SECT_LOC:
12021 sections.loc_offset = offset;
12022 sections.loc_size = size;
12023 break;
12024 case DW_SECT_STR_OFFSETS:
12025 sections.str_offsets_offset = offset;
12026 sections.str_offsets_size = size;
12027 break;
12028 case DW_SECT_MACINFO:
12029 sections.macinfo_offset = offset;
12030 sections.macinfo_size = size;
12031 break;
12032 case DW_SECT_MACRO:
12033 sections.macro_offset = offset;
12034 sections.macro_size = size;
12035 break;
12036 }
12037 }
12038
12039 /* It's easier for the rest of the code if we fake a struct dwo_file and
12040 have dwo_unit "live" in that. At least for now.
12041
12042 The DWP file can be made up of a random collection of CUs and TUs.
12043 However, for each CU + set of TUs that came from the same original DWO
12044 file, we can combine them back into a virtual DWO file to save space
12045 (fewer struct dwo_file objects to allocate). Remember that for really
12046 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12047
791afaa2
TT
12048 std::string virtual_dwo_name =
12049 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12050 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12051 (long) (sections.line_size ? sections.line_offset : 0),
12052 (long) (sections.loc_size ? sections.loc_offset : 0),
12053 (long) (sections.str_offsets_size
12054 ? sections.str_offsets_offset : 0));
73869dc2 12055 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12056 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12057 virtual_dwo_name.c_str (),
12058 comp_dir);
73869dc2
DE
12059 /* Create one if necessary. */
12060 if (*dwo_file_slot == NULL)
12061 {
b4f54984 12062 if (dwarf_read_debug)
73869dc2
DE
12063 {
12064 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12065 virtual_dwo_name.c_str ());
73869dc2 12066 }
51ac9db5 12067 dwo_file = new struct dwo_file;
45940949 12068 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12069 dwo_file->comp_dir = comp_dir;
12070 dwo_file->sections.abbrev =
ed2dc618 12071 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12072 sections.abbrev_offset, sections.abbrev_size);
12073 dwo_file->sections.line =
ed2dc618 12074 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12075 sections.line_offset, sections.line_size);
12076 dwo_file->sections.loc =
ed2dc618 12077 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12078 sections.loc_offset, sections.loc_size);
12079 dwo_file->sections.macinfo =
ed2dc618 12080 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12081 sections.macinfo_offset, sections.macinfo_size);
12082 dwo_file->sections.macro =
ed2dc618 12083 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12084 sections.macro_offset, sections.macro_size);
12085 dwo_file->sections.str_offsets =
ed2dc618
SM
12086 create_dwp_v2_section (dwarf2_per_objfile,
12087 &dwp_file->sections.str_offsets,
73869dc2
DE
12088 sections.str_offsets_offset,
12089 sections.str_offsets_size);
12090 /* The "str" section is global to the entire DWP file. */
12091 dwo_file->sections.str = dwp_file->sections.str;
12092 /* The info or types section is assigned below to dwo_unit,
12093 there's no need to record it in dwo_file.
12094 Also, we can't simply record type sections in dwo_file because
12095 we record a pointer into the vector in dwo_unit. As we collect more
12096 types we'll grow the vector and eventually have to reallocate space
12097 for it, invalidating all copies of pointers into the previous
12098 contents. */
12099 *dwo_file_slot = dwo_file;
12100 }
12101 else
12102 {
b4f54984 12103 if (dwarf_read_debug)
73869dc2
DE
12104 {
12105 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12106 virtual_dwo_name.c_str ());
73869dc2 12107 }
9a3c8263 12108 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12109 }
73869dc2 12110
5989a64e 12111 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12112 dwo_unit->dwo_file = dwo_file;
12113 dwo_unit->signature = signature;
8d749320 12114 dwo_unit->section =
5989a64e 12115 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12116 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12117 is_debug_types
73869dc2
DE
12118 ? &dwp_file->sections.types
12119 : &dwp_file->sections.info,
12120 sections.info_or_types_offset,
12121 sections.info_or_types_size);
12122 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12123
12124 return dwo_unit;
12125}
12126
57d63ce2
DE
12127/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12128 Returns NULL if the signature isn't found. */
80626a55
DE
12129
12130static struct dwo_unit *
ed2dc618
SM
12131lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12132 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12133 ULONGEST signature, int is_debug_types)
80626a55 12134{
57d63ce2
DE
12135 const struct dwp_hash_table *dwp_htab =
12136 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12137 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12138 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12139 uint32_t hash = signature & mask;
12140 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12141 unsigned int i;
12142 void **slot;
870f88f7 12143 struct dwo_unit find_dwo_cu;
80626a55
DE
12144
12145 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12146 find_dwo_cu.signature = signature;
19ac8c2e 12147 slot = htab_find_slot (is_debug_types
48b490f2
TT
12148 ? dwp_file->loaded_tus.get ()
12149 : dwp_file->loaded_cus.get (),
19ac8c2e 12150 &find_dwo_cu, INSERT);
80626a55
DE
12151
12152 if (*slot != NULL)
9a3c8263 12153 return (struct dwo_unit *) *slot;
80626a55
DE
12154
12155 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12156 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12157 {
12158 ULONGEST signature_in_table;
12159
12160 signature_in_table =
57d63ce2 12161 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12162 if (signature_in_table == signature)
12163 {
57d63ce2
DE
12164 uint32_t unit_index =
12165 read_4_bytes (dbfd,
12166 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12167
73869dc2
DE
12168 if (dwp_file->version == 1)
12169 {
ed2dc618
SM
12170 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12171 dwp_file, unit_index,
73869dc2
DE
12172 comp_dir, signature,
12173 is_debug_types);
12174 }
12175 else
12176 {
ed2dc618
SM
12177 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12178 dwp_file, unit_index,
73869dc2
DE
12179 comp_dir, signature,
12180 is_debug_types);
12181 }
9a3c8263 12182 return (struct dwo_unit *) *slot;
80626a55
DE
12183 }
12184 if (signature_in_table == 0)
12185 return NULL;
12186 hash = (hash + hash2) & mask;
12187 }
12188
12189 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12190 " [in module %s]"),
12191 dwp_file->name);
12192}
12193
ab5088bf 12194/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12195 Open the file specified by FILE_NAME and hand it off to BFD for
12196 preliminary analysis. Return a newly initialized bfd *, which
12197 includes a canonicalized copy of FILE_NAME.
80626a55 12198 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12199 SEARCH_CWD is true if the current directory is to be searched.
12200 It will be searched before debug-file-directory.
13aaf454
DE
12201 If successful, the file is added to the bfd include table of the
12202 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12203 If unable to find/open the file, return NULL.
3019eac3
DE
12204 NOTE: This function is derived from symfile_bfd_open. */
12205
192b62ce 12206static gdb_bfd_ref_ptr
ed2dc618
SM
12207try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12208 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12209{
24b9144d 12210 int desc;
9c02c129
DE
12211 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12212 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12213 to debug_file_directory. */
e0cc99a6 12214 const char *search_path;
9c02c129
DE
12215 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12216
e0cc99a6 12217 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12218 if (search_cwd)
12219 {
12220 if (*debug_file_directory != '\0')
e0cc99a6
TT
12221 {
12222 search_path_holder.reset (concat (".", dirname_separator_string,
12223 debug_file_directory,
12224 (char *) NULL));
12225 search_path = search_path_holder.get ();
12226 }
6ac97d4c 12227 else
e0cc99a6 12228 search_path = ".";
6ac97d4c 12229 }
9c02c129 12230 else
e0cc99a6 12231 search_path = debug_file_directory;
3019eac3 12232
24b9144d 12233 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12234 if (is_dwp)
12235 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12236
12237 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12238 desc = openp (search_path, flags, file_name,
3019eac3
DE
12239 O_RDONLY | O_BINARY, &absolute_name);
12240 if (desc < 0)
12241 return NULL;
12242
e0cc99a6
TT
12243 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12244 gnutarget, desc));
9c02c129
DE
12245 if (sym_bfd == NULL)
12246 return NULL;
192b62ce 12247 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12248
192b62ce
TT
12249 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12250 return NULL;
3019eac3 12251
13aaf454
DE
12252 /* Success. Record the bfd as having been included by the objfile's bfd.
12253 This is important because things like demangled_names_hash lives in the
12254 objfile's per_bfd space and may have references to things like symbol
12255 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12256 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12257
3019eac3
DE
12258 return sym_bfd;
12259}
12260
ab5088bf 12261/* Try to open DWO file FILE_NAME.
3019eac3
DE
12262 COMP_DIR is the DW_AT_comp_dir attribute.
12263 The result is the bfd handle of the file.
12264 If there is a problem finding or opening the file, return NULL.
12265 Upon success, the canonicalized path of the file is stored in the bfd,
12266 same as symfile_bfd_open. */
12267
192b62ce 12268static gdb_bfd_ref_ptr
ed2dc618
SM
12269open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12270 const char *file_name, const char *comp_dir)
3019eac3 12271{
80626a55 12272 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12273 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12274 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12275
12276 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12277
12278 if (comp_dir != NULL)
12279 {
43816ebc
TT
12280 gdb::unique_xmalloc_ptr<char> path_to_try
12281 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12282
12283 /* NOTE: If comp_dir is a relative path, this will also try the
12284 search path, which seems useful. */
ed2dc618 12285 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12286 path_to_try.get (),
ed2dc618 12287 0 /*is_dwp*/,
192b62ce 12288 1 /*search_cwd*/));
3019eac3
DE
12289 if (abfd != NULL)
12290 return abfd;
12291 }
12292
12293 /* That didn't work, try debug-file-directory, which, despite its name,
12294 is a list of paths. */
12295
12296 if (*debug_file_directory == '\0')
12297 return NULL;
12298
ed2dc618
SM
12299 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12300 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12301}
12302
80626a55
DE
12303/* This function is mapped across the sections and remembers the offset and
12304 size of each of the DWO debugging sections we are interested in. */
12305
12306static void
12307dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12308{
9a3c8263 12309 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12310 const struct dwop_section_names *names = &dwop_section_names;
12311
12312 if (section_is_p (sectp->name, &names->abbrev_dwo))
12313 {
049412e3 12314 dwo_sections->abbrev.s.section = sectp;
fd361982 12315 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12316 }
12317 else if (section_is_p (sectp->name, &names->info_dwo))
12318 {
049412e3 12319 dwo_sections->info.s.section = sectp;
fd361982 12320 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12321 }
12322 else if (section_is_p (sectp->name, &names->line_dwo))
12323 {
049412e3 12324 dwo_sections->line.s.section = sectp;
fd361982 12325 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12326 }
12327 else if (section_is_p (sectp->name, &names->loc_dwo))
12328 {
049412e3 12329 dwo_sections->loc.s.section = sectp;
fd361982 12330 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12331 }
41144253 12332 else if (section_is_p (sectp->name, &names->loclists_dwo))
12333 {
12334 dwo_sections->loclists.s.section = sectp;
12335 dwo_sections->loclists.size = bfd_section_size (sectp);
12336 }
80626a55
DE
12337 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12338 {
049412e3 12339 dwo_sections->macinfo.s.section = sectp;
fd361982 12340 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12341 }
12342 else if (section_is_p (sectp->name, &names->macro_dwo))
12343 {
049412e3 12344 dwo_sections->macro.s.section = sectp;
fd361982 12345 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12346 }
12347 else if (section_is_p (sectp->name, &names->str_dwo))
12348 {
049412e3 12349 dwo_sections->str.s.section = sectp;
fd361982 12350 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12351 }
12352 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12353 {
049412e3 12354 dwo_sections->str_offsets.s.section = sectp;
fd361982 12355 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12356 }
12357 else if (section_is_p (sectp->name, &names->types_dwo))
12358 {
12359 struct dwarf2_section_info type_section;
12360
12361 memset (&type_section, 0, sizeof (type_section));
049412e3 12362 type_section.s.section = sectp;
fd361982 12363 type_section.size = bfd_section_size (sectp);
fd5866f6 12364 dwo_sections->types.push_back (type_section);
80626a55
DE
12365 }
12366}
12367
ab5088bf 12368/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12369 by PER_CU. This is for the non-DWP case.
80626a55 12370 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12371
12372static struct dwo_file *
4ab09049
SM
12373open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12374 const char *comp_dir)
3019eac3 12375{
4ab09049 12376 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12377
fb1eb2f9 12378 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12379 if (dbfd == NULL)
12380 {
b4f54984 12381 if (dwarf_read_debug)
80626a55
DE
12382 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12383 return NULL;
12384 }
263db9a1 12385
51ac9db5 12386 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12387 dwo_file->dwo_name = dwo_name;
12388 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12389 dwo_file->dbfd = std::move (dbfd);
3019eac3 12390
fb1eb2f9 12391 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12392 &dwo_file->sections);
3019eac3 12393
4ab09049 12394 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
18a8505e 12395 dwo_file->sections.info, dwo_file->cus);
3019eac3 12396
263db9a1 12397 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12398 dwo_file->sections.types, dwo_file->tus);
3019eac3 12399
b4f54984 12400 if (dwarf_read_debug)
80626a55
DE
12401 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12402
263db9a1 12403 return dwo_file.release ();
3019eac3
DE
12404}
12405
80626a55 12406/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12407 size of each of the DWP debugging sections common to version 1 and 2 that
12408 we are interested in. */
3019eac3 12409
80626a55 12410static void
73869dc2
DE
12411dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12412 void *dwp_file_ptr)
3019eac3 12413{
9a3c8263 12414 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12415 const struct dwop_section_names *names = &dwop_section_names;
12416 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12417
80626a55 12418 /* Record the ELF section number for later lookup: this is what the
73869dc2 12419 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12420 gdb_assert (elf_section_nr < dwp_file->num_sections);
12421 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12422
80626a55
DE
12423 /* Look for specific sections that we need. */
12424 if (section_is_p (sectp->name, &names->str_dwo))
12425 {
049412e3 12426 dwp_file->sections.str.s.section = sectp;
fd361982 12427 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12428 }
12429 else if (section_is_p (sectp->name, &names->cu_index))
12430 {
049412e3 12431 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12432 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12433 }
12434 else if (section_is_p (sectp->name, &names->tu_index))
12435 {
049412e3 12436 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12437 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12438 }
12439}
3019eac3 12440
73869dc2
DE
12441/* This function is mapped across the sections and remembers the offset and
12442 size of each of the DWP version 2 debugging sections that we are interested
12443 in. This is split into a separate function because we don't know if we
12444 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12445
12446static void
12447dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12448{
9a3c8263 12449 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12450 const struct dwop_section_names *names = &dwop_section_names;
12451 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12452
12453 /* Record the ELF section number for later lookup: this is what the
12454 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12455 gdb_assert (elf_section_nr < dwp_file->num_sections);
12456 dwp_file->elf_sections[elf_section_nr] = sectp;
12457
12458 /* Look for specific sections that we need. */
12459 if (section_is_p (sectp->name, &names->abbrev_dwo))
12460 {
049412e3 12461 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12462 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12463 }
12464 else if (section_is_p (sectp->name, &names->info_dwo))
12465 {
049412e3 12466 dwp_file->sections.info.s.section = sectp;
fd361982 12467 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12468 }
12469 else if (section_is_p (sectp->name, &names->line_dwo))
12470 {
049412e3 12471 dwp_file->sections.line.s.section = sectp;
fd361982 12472 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12473 }
12474 else if (section_is_p (sectp->name, &names->loc_dwo))
12475 {
049412e3 12476 dwp_file->sections.loc.s.section = sectp;
fd361982 12477 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12478 }
12479 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12480 {
049412e3 12481 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12482 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12483 }
12484 else if (section_is_p (sectp->name, &names->macro_dwo))
12485 {
049412e3 12486 dwp_file->sections.macro.s.section = sectp;
fd361982 12487 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12488 }
12489 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12490 {
049412e3 12491 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12492 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12493 }
12494 else if (section_is_p (sectp->name, &names->types_dwo))
12495 {
049412e3 12496 dwp_file->sections.types.s.section = sectp;
fd361982 12497 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12498 }
12499}
12500
80626a55 12501/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12502
80626a55
DE
12503static hashval_t
12504hash_dwp_loaded_cutus (const void *item)
12505{
9a3c8263 12506 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12507
80626a55
DE
12508 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12509 return dwo_unit->signature;
3019eac3
DE
12510}
12511
80626a55 12512/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12513
80626a55
DE
12514static int
12515eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12516{
9a3c8263
SM
12517 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12518 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12519
80626a55
DE
12520 return dua->signature == dub->signature;
12521}
3019eac3 12522
80626a55 12523/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12524
48b490f2 12525static htab_up
298e9637 12526allocate_dwp_loaded_cutus_table ()
80626a55 12527{
48b490f2
TT
12528 return htab_up (htab_create_alloc (3,
12529 hash_dwp_loaded_cutus,
12530 eq_dwp_loaded_cutus,
12531 NULL, xcalloc, xfree));
80626a55 12532}
3019eac3 12533
ab5088bf
DE
12534/* Try to open DWP file FILE_NAME.
12535 The result is the bfd handle of the file.
12536 If there is a problem finding or opening the file, return NULL.
12537 Upon success, the canonicalized path of the file is stored in the bfd,
12538 same as symfile_bfd_open. */
12539
192b62ce 12540static gdb_bfd_ref_ptr
ed2dc618
SM
12541open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12542 const char *file_name)
ab5088bf 12543{
ed2dc618
SM
12544 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12545 1 /*is_dwp*/,
192b62ce 12546 1 /*search_cwd*/));
6ac97d4c
DE
12547 if (abfd != NULL)
12548 return abfd;
12549
12550 /* Work around upstream bug 15652.
12551 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12552 [Whether that's a "bug" is debatable, but it is getting in our way.]
12553 We have no real idea where the dwp file is, because gdb's realpath-ing
12554 of the executable's path may have discarded the needed info.
12555 [IWBN if the dwp file name was recorded in the executable, akin to
12556 .gnu_debuglink, but that doesn't exist yet.]
12557 Strip the directory from FILE_NAME and search again. */
12558 if (*debug_file_directory != '\0')
12559 {
12560 /* Don't implicitly search the current directory here.
12561 If the user wants to search "." to handle this case,
12562 it must be added to debug-file-directory. */
ed2dc618
SM
12563 return try_open_dwop_file (dwarf2_per_objfile,
12564 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12565 0 /*search_cwd*/);
12566 }
12567
12568 return NULL;
ab5088bf
DE
12569}
12570
80626a55
DE
12571/* Initialize the use of the DWP file for the current objfile.
12572 By convention the name of the DWP file is ${objfile}.dwp.
12573 The result is NULL if it can't be found. */
a766d390 12574
400174b1 12575static std::unique_ptr<struct dwp_file>
ed2dc618 12576open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12577{
12578 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12579
82bf32bc
JK
12580 /* Try to find first .dwp for the binary file before any symbolic links
12581 resolving. */
6c447423
DE
12582
12583 /* If the objfile is a debug file, find the name of the real binary
12584 file and get the name of dwp file from there. */
d721ba37 12585 std::string dwp_name;
6c447423
DE
12586 if (objfile->separate_debug_objfile_backlink != NULL)
12587 {
12588 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12589 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12590
d721ba37 12591 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12592 }
12593 else
d721ba37
PA
12594 dwp_name = objfile->original_name;
12595
12596 dwp_name += ".dwp";
80626a55 12597
ed2dc618 12598 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12599 if (dbfd == NULL
12600 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12601 {
12602 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12603 dwp_name = objfile_name (objfile);
12604 dwp_name += ".dwp";
ed2dc618 12605 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12606 }
12607
80626a55
DE
12608 if (dbfd == NULL)
12609 {
b4f54984 12610 if (dwarf_read_debug)
d721ba37 12611 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12612 return std::unique_ptr<dwp_file> ();
3019eac3 12613 }
400174b1
TT
12614
12615 const char *name = bfd_get_filename (dbfd.get ());
12616 std::unique_ptr<struct dwp_file> dwp_file
12617 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12618
0a0f4c01 12619 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12620 dwp_file->elf_sections =
5989a64e 12621 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12622 dwp_file->num_sections, asection *);
12623
400174b1
TT
12624 bfd_map_over_sections (dwp_file->dbfd.get (),
12625 dwarf2_locate_common_dwp_sections,
12626 dwp_file.get ());
80626a55 12627
400174b1
TT
12628 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12629 0);
80626a55 12630
400174b1
TT
12631 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12632 1);
80626a55 12633
73869dc2 12634 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12635 if (dwp_file->cus && dwp_file->tus
12636 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12637 {
12638 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12639 pretty bizarre. We use pulongest here because that's the established
4d65956b 12640 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12641 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12642 " TU version %s [in DWP file %s]"),
12643 pulongest (dwp_file->cus->version),
d721ba37 12644 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12645 }
08302ed2
DE
12646
12647 if (dwp_file->cus)
12648 dwp_file->version = dwp_file->cus->version;
12649 else if (dwp_file->tus)
12650 dwp_file->version = dwp_file->tus->version;
12651 else
12652 dwp_file->version = 2;
73869dc2
DE
12653
12654 if (dwp_file->version == 2)
400174b1
TT
12655 bfd_map_over_sections (dwp_file->dbfd.get (),
12656 dwarf2_locate_v2_dwp_sections,
12657 dwp_file.get ());
73869dc2 12658
298e9637
SM
12659 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12660 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12661
b4f54984 12662 if (dwarf_read_debug)
80626a55
DE
12663 {
12664 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12665 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12666 " %s CUs, %s TUs\n",
12667 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12668 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12669 }
12670
12671 return dwp_file;
3019eac3 12672}
c906108c 12673
ab5088bf
DE
12674/* Wrapper around open_and_init_dwp_file, only open it once. */
12675
12676static struct dwp_file *
ed2dc618 12677get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12678{
5989a64e 12679 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12680 {
5989a64e 12681 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12682 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12683 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12684 }
5989a64e 12685 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12686}
12687
80626a55
DE
12688/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12689 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12690 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12691 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12692 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12693
12694 This is called, for example, when wanting to read a variable with a
12695 complex location. Therefore we don't want to do file i/o for every call.
12696 Therefore we don't want to look for a DWO file on every call.
12697 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12698 then we check if we've already seen DWO_NAME, and only THEN do we check
12699 for a DWO file.
12700
1c658ad5 12701 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12702 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12703
3019eac3 12704static struct dwo_unit *
4ab09049 12705lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12706 ULONGEST signature, int is_debug_types)
3019eac3 12707{
4ab09049 12708 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12709 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12710 const char *kind = is_debug_types ? "TU" : "CU";
12711 void **dwo_file_slot;
3019eac3 12712 struct dwo_file *dwo_file;
80626a55 12713 struct dwp_file *dwp_file;
cb1df416 12714
6a506a2d
DE
12715 /* First see if there's a DWP file.
12716 If we have a DWP file but didn't find the DWO inside it, don't
12717 look for the original DWO file. It makes gdb behave differently
12718 depending on whether one is debugging in the build tree. */
cf2c3c16 12719
ed2dc618 12720 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12721 if (dwp_file != NULL)
cf2c3c16 12722 {
80626a55
DE
12723 const struct dwp_hash_table *dwp_htab =
12724 is_debug_types ? dwp_file->tus : dwp_file->cus;
12725
12726 if (dwp_htab != NULL)
12727 {
12728 struct dwo_unit *dwo_cutu =
ed2dc618 12729 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12730 signature, is_debug_types);
80626a55
DE
12731
12732 if (dwo_cutu != NULL)
12733 {
b4f54984 12734 if (dwarf_read_debug)
80626a55
DE
12735 {
12736 fprintf_unfiltered (gdb_stdlog,
12737 "Virtual DWO %s %s found: @%s\n",
12738 kind, hex_string (signature),
12739 host_address_to_string (dwo_cutu));
12740 }
12741 return dwo_cutu;
12742 }
12743 }
12744 }
6a506a2d 12745 else
80626a55 12746 {
6a506a2d 12747 /* No DWP file, look for the DWO file. */
80626a55 12748
ed2dc618
SM
12749 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12750 dwo_name, comp_dir);
6a506a2d 12751 if (*dwo_file_slot == NULL)
80626a55 12752 {
6a506a2d 12753 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12754 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12755 }
6a506a2d 12756 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12757 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12758
6a506a2d 12759 if (dwo_file != NULL)
19c3d4c9 12760 {
6a506a2d
DE
12761 struct dwo_unit *dwo_cutu = NULL;
12762
12763 if (is_debug_types && dwo_file->tus)
12764 {
12765 struct dwo_unit find_dwo_cutu;
12766
12767 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12768 find_dwo_cutu.signature = signature;
9a3c8263 12769 dwo_cutu
b0b6a987
TT
12770 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12771 &find_dwo_cutu);
6a506a2d 12772 }
33c5cd75 12773 else if (!is_debug_types && dwo_file->cus)
80626a55 12774 {
33c5cd75
DB
12775 struct dwo_unit find_dwo_cutu;
12776
12777 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12778 find_dwo_cutu.signature = signature;
b0b6a987 12779 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12780 &find_dwo_cutu);
6a506a2d
DE
12781 }
12782
12783 if (dwo_cutu != NULL)
12784 {
b4f54984 12785 if (dwarf_read_debug)
6a506a2d
DE
12786 {
12787 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12788 kind, dwo_name, hex_string (signature),
12789 host_address_to_string (dwo_cutu));
12790 }
12791 return dwo_cutu;
80626a55
DE
12792 }
12793 }
2e276125 12794 }
9cdd5dbd 12795
80626a55
DE
12796 /* We didn't find it. This could mean a dwo_id mismatch, or
12797 someone deleted the DWO/DWP file, or the search path isn't set up
12798 correctly to find the file. */
12799
b4f54984 12800 if (dwarf_read_debug)
80626a55
DE
12801 {
12802 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12803 kind, dwo_name, hex_string (signature));
12804 }
3019eac3 12805
6656a72d
DE
12806 /* This is a warning and not a complaint because it can be caused by
12807 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12808 {
12809 /* Print the name of the DWP file if we looked there, helps the user
12810 better diagnose the problem. */
791afaa2 12811 std::string dwp_text;
43942612
DE
12812
12813 if (dwp_file != NULL)
791afaa2
TT
12814 dwp_text = string_printf (" [in DWP file %s]",
12815 lbasename (dwp_file->name));
43942612 12816
9d8780f0 12817 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12818 " [in module %s]"),
4ab09049
SM
12819 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12820 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12821 }
3019eac3 12822 return NULL;
5fb290d7
DJ
12823}
12824
80626a55
DE
12825/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12826 See lookup_dwo_cutu_unit for details. */
12827
12828static struct dwo_unit *
4ab09049 12829lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12830 ULONGEST signature)
12831{
4ab09049
SM
12832 gdb_assert (!cu->per_cu->is_debug_types);
12833
12834 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12835}
12836
12837/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12838 See lookup_dwo_cutu_unit for details. */
12839
12840static struct dwo_unit *
4ab09049 12841lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12842{
4ab09049
SM
12843 gdb_assert (cu->per_cu->is_debug_types);
12844
12845 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12846
12847 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12848}
12849
89e63ee4
DE
12850/* Traversal function for queue_and_load_all_dwo_tus. */
12851
12852static int
12853queue_and_load_dwo_tu (void **slot, void *info)
12854{
12855 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12856 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12857 ULONGEST signature = dwo_unit->signature;
d460f660 12858 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12859
12860 if (sig_type != NULL)
12861 {
12862 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12863
12864 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12865 a real dependency of PER_CU on SIG_TYPE. That is detected later
12866 while processing PER_CU. */
120ce1b5 12867 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12868 load_full_type_unit (sig_cu, cu->per_objfile);
12869 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12870 }
12871
12872 return 1;
12873}
12874
12875/* Queue all TUs contained in the DWO of PER_CU to be read in.
12876 The DWO may have the only definition of the type, though it may not be
12877 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12878 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12879
12880static void
12881queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12882{
12883 struct dwo_unit *dwo_unit;
12884 struct dwo_file *dwo_file;
12885
12886 gdb_assert (!per_cu->is_debug_types);
89e63ee4 12887 gdb_assert (per_cu->cu != NULL);
64874a40 12888 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
89e63ee4
DE
12889
12890 dwo_unit = per_cu->cu->dwo_unit;
12891 gdb_assert (dwo_unit != NULL);
12892
12893 dwo_file = dwo_unit->dwo_file;
12894 if (dwo_file->tus != NULL)
b0b6a987 12895 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
d460f660 12896 per_cu->cu);
89e63ee4
DE
12897}
12898
3019eac3 12899/* Read in various DIEs. */
348e048f 12900
d389af10 12901/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12902 Inherit only the children of the DW_AT_abstract_origin DIE not being
12903 already referenced by DW_AT_abstract_origin from the children of the
12904 current DIE. */
d389af10
JK
12905
12906static void
12907inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12908{
12909 struct die_info *child_die;
791afaa2 12910 sect_offset *offsetp;
d389af10
JK
12911 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12912 struct die_info *origin_die;
12913 /* Iterator of the ORIGIN_DIE children. */
12914 struct die_info *origin_child_die;
d389af10 12915 struct attribute *attr;
cd02d79d
PA
12916 struct dwarf2_cu *origin_cu;
12917 struct pending **origin_previous_list_in_scope;
d389af10
JK
12918
12919 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12920 if (!attr)
12921 return;
12922
cd02d79d
PA
12923 /* Note that following die references may follow to a die in a
12924 different cu. */
12925
12926 origin_cu = cu;
12927 origin_die = follow_die_ref (die, attr, &origin_cu);
12928
12929 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12930 symbols in. */
12931 origin_previous_list_in_scope = origin_cu->list_in_scope;
12932 origin_cu->list_in_scope = cu->list_in_scope;
12933
edb3359d
DJ
12934 if (die->tag != origin_die->tag
12935 && !(die->tag == DW_TAG_inlined_subroutine
12936 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12937 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12938 sect_offset_str (die->sect_off),
12939 sect_offset_str (origin_die->sect_off));
d389af10 12940
791afaa2 12941 std::vector<sect_offset> offsets;
d389af10 12942
3ea89b92
PMR
12943 for (child_die = die->child;
12944 child_die && child_die->tag;
436c571c 12945 child_die = child_die->sibling)
3ea89b92
PMR
12946 {
12947 struct die_info *child_origin_die;
12948 struct dwarf2_cu *child_origin_cu;
12949
12950 /* We are trying to process concrete instance entries:
216f72a1 12951 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12952 it's not relevant to our analysis here. i.e. detecting DIEs that are
12953 present in the abstract instance but not referenced in the concrete
12954 one. */
216f72a1
JK
12955 if (child_die->tag == DW_TAG_call_site
12956 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12957 continue;
12958
c38f313d
DJ
12959 /* For each CHILD_DIE, find the corresponding child of
12960 ORIGIN_DIE. If there is more than one layer of
12961 DW_AT_abstract_origin, follow them all; there shouldn't be,
12962 but GCC versions at least through 4.4 generate this (GCC PR
12963 40573). */
3ea89b92
PMR
12964 child_origin_die = child_die;
12965 child_origin_cu = cu;
c38f313d
DJ
12966 while (1)
12967 {
cd02d79d
PA
12968 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12969 child_origin_cu);
c38f313d
DJ
12970 if (attr == NULL)
12971 break;
cd02d79d
PA
12972 child_origin_die = follow_die_ref (child_origin_die, attr,
12973 &child_origin_cu);
c38f313d
DJ
12974 }
12975
d389af10
JK
12976 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12977 counterpart may exist. */
c38f313d 12978 if (child_origin_die != child_die)
d389af10 12979 {
edb3359d
DJ
12980 if (child_die->tag != child_origin_die->tag
12981 && !(child_die->tag == DW_TAG_inlined_subroutine
12982 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12983 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12984 "different tags"),
9d8780f0
SM
12985 sect_offset_str (child_die->sect_off),
12986 sect_offset_str (child_origin_die->sect_off));
c38f313d 12987 if (child_origin_die->parent != origin_die)
b98664d3 12988 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12989 "different parents"),
9d8780f0
SM
12990 sect_offset_str (child_die->sect_off),
12991 sect_offset_str (child_origin_die->sect_off));
c38f313d 12992 else
791afaa2 12993 offsets.push_back (child_origin_die->sect_off);
d389af10 12994 }
d389af10 12995 }
791afaa2
TT
12996 std::sort (offsets.begin (), offsets.end ());
12997 sect_offset *offsets_end = offsets.data () + offsets.size ();
12998 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12999 if (offsetp[-1] == *offsetp)
b98664d3 13000 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13001 "to DIE %s as their abstract origin"),
13002 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13003
791afaa2 13004 offsetp = offsets.data ();
d389af10
JK
13005 origin_child_die = origin_die->child;
13006 while (origin_child_die && origin_child_die->tag)
13007 {
13008 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13009 while (offsetp < offsets_end
9c541725 13010 && *offsetp < origin_child_die->sect_off)
d389af10 13011 offsetp++;
b64f50a1 13012 if (offsetp >= offsets_end
9c541725 13013 || *offsetp > origin_child_die->sect_off)
d389af10 13014 {
adde2bff
DE
13015 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13016 Check whether we're already processing ORIGIN_CHILD_DIE.
13017 This can happen with mutually referenced abstract_origins.
13018 PR 16581. */
13019 if (!origin_child_die->in_process)
13020 process_die (origin_child_die, origin_cu);
d389af10 13021 }
436c571c 13022 origin_child_die = origin_child_die->sibling;
d389af10 13023 }
cd02d79d 13024 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13025
13026 if (cu != origin_cu)
13027 compute_delayed_physnames (origin_cu);
d389af10
JK
13028}
13029
c906108c 13030static void
e7c27a73 13031read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13032{
5e22e966 13033 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13034 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13035 struct context_stack *newobj;
c906108c
SS
13036 CORE_ADDR lowpc;
13037 CORE_ADDR highpc;
13038 struct die_info *child_die;
edb3359d 13039 struct attribute *attr, *call_line, *call_file;
15d034d0 13040 const char *name;
e142c38c 13041 CORE_ADDR baseaddr;
801e3a5b 13042 struct block *block;
edb3359d 13043 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13044 std::vector<struct symbol *> template_args;
34eaf542 13045 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13046
13047 if (inlined_func)
13048 {
13049 /* If we do not have call site information, we can't show the
13050 caller of this inlined function. That's too confusing, so
13051 only use the scope for local variables. */
13052 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13053 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13054 if (call_line == NULL || call_file == NULL)
13055 {
13056 read_lexical_block_scope (die, cu);
13057 return;
13058 }
13059 }
c906108c 13060
b3b3bada 13061 baseaddr = objfile->text_section_offset ();
e142c38c 13062
94af9270 13063 name = dwarf2_name (die, cu);
c906108c 13064
e8d05480
JB
13065 /* Ignore functions with missing or empty names. These are actually
13066 illegal according to the DWARF standard. */
13067 if (name == NULL)
13068 {
b98664d3 13069 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13070 sect_offset_str (die->sect_off));
e8d05480
JB
13071 return;
13072 }
13073
13074 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13075 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13076 <= PC_BOUNDS_INVALID)
e8d05480 13077 {
ae4d0c03
PM
13078 attr = dwarf2_attr (die, DW_AT_external, cu);
13079 if (!attr || !DW_UNSND (attr))
b98664d3 13080 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13081 "for subprogram DIE at %s"),
13082 sect_offset_str (die->sect_off));
e8d05480
JB
13083 return;
13084 }
c906108c 13085
3e29f34a
MR
13086 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13087 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13088
34eaf542
TT
13089 /* If we have any template arguments, then we must allocate a
13090 different sort of symbol. */
436c571c 13091 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13092 {
13093 if (child_die->tag == DW_TAG_template_type_param
13094 || child_die->tag == DW_TAG_template_value_param)
13095 {
8c14c3a3 13096 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13097 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13098 break;
13099 }
13100 }
13101
c24bdb02 13102 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13103 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13104 (struct symbol *) templ_func);
4c2df51b 13105
81873cc8 13106 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13107 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13108 cu->language);
13109
4cecd739
DJ
13110 /* If there is a location expression for DW_AT_frame_base, record
13111 it. */
e142c38c 13112 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13113 if (attr != nullptr)
fe978cb0 13114 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13115
63e43d3a
PMR
13116 /* If there is a location for the static link, record it. */
13117 newobj->static_link = NULL;
13118 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13119 if (attr != nullptr)
63e43d3a 13120 {
224c3ddb
SM
13121 newobj->static_link
13122 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13123 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13124 cu->addr_type ());
63e43d3a
PMR
13125 }
13126
c24bdb02 13127 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13128
639d11d3 13129 if (die->child != NULL)
c906108c 13130 {
639d11d3 13131 child_die = die->child;
c906108c
SS
13132 while (child_die && child_die->tag)
13133 {
34eaf542
TT
13134 if (child_die->tag == DW_TAG_template_type_param
13135 || child_die->tag == DW_TAG_template_value_param)
13136 {
13137 struct symbol *arg = new_symbol (child_die, NULL, cu);
13138
f1078f66 13139 if (arg != NULL)
2f4732b0 13140 template_args.push_back (arg);
34eaf542
TT
13141 }
13142 else
13143 process_die (child_die, cu);
436c571c 13144 child_die = child_die->sibling;
c906108c
SS
13145 }
13146 }
13147
d389af10
JK
13148 inherit_abstract_dies (die, cu);
13149
4a811a97
UW
13150 /* If we have a DW_AT_specification, we might need to import using
13151 directives from the context of the specification DIE. See the
13152 comment in determine_prefix. */
13153 if (cu->language == language_cplus
13154 && dwarf2_attr (die, DW_AT_specification, cu))
13155 {
13156 struct dwarf2_cu *spec_cu = cu;
13157 struct die_info *spec_die = die_specification (die, &spec_cu);
13158
13159 while (spec_die)
13160 {
13161 child_die = spec_die->child;
13162 while (child_die && child_die->tag)
13163 {
13164 if (child_die->tag == DW_TAG_imported_module)
13165 process_die (child_die, spec_cu);
436c571c 13166 child_die = child_die->sibling;
4a811a97
UW
13167 }
13168
13169 /* In some cases, GCC generates specification DIEs that
13170 themselves contain DW_AT_specification attributes. */
13171 spec_die = die_specification (spec_die, &spec_cu);
13172 }
13173 }
13174
c24bdb02 13175 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13176 /* Make a block for the local symbols within. */
c24bdb02 13177 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13178 cstk.static_link, lowpc, highpc);
801e3a5b 13179
df8a16a1 13180 /* For C++, set the block's scope. */
45280282
IB
13181 if ((cu->language == language_cplus
13182 || cu->language == language_fortran
c44af4eb
TT
13183 || cu->language == language_d
13184 || cu->language == language_rust)
4d4ec4e5 13185 && cu->processing_has_namespace_info)
195a3f6c
TT
13186 block_set_scope (block, determine_prefix (die, cu),
13187 &objfile->objfile_obstack);
df8a16a1 13188
801e3a5b
JB
13189 /* If we have address ranges, record them. */
13190 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13191
a60f3166 13192 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13193
34eaf542 13194 /* Attach template arguments to function. */
2f4732b0 13195 if (!template_args.empty ())
34eaf542
TT
13196 {
13197 gdb_assert (templ_func != NULL);
13198
2f4732b0 13199 templ_func->n_template_arguments = template_args.size ();
34eaf542 13200 templ_func->template_arguments
8d749320
SM
13201 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13202 templ_func->n_template_arguments);
34eaf542 13203 memcpy (templ_func->template_arguments,
2f4732b0 13204 template_args.data (),
34eaf542 13205 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13206
13207 /* Make sure that the symtab is set on the new symbols. Even
13208 though they don't appear in this symtab directly, other parts
13209 of gdb assume that symbols do, and this is reasonably
13210 true. */
8634679f 13211 for (symbol *sym : template_args)
3e1d3d8c 13212 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13213 }
13214
208d8187
JB
13215 /* In C++, we can have functions nested inside functions (e.g., when
13216 a function declares a class that has methods). This means that
13217 when we finish processing a function scope, we may need to go
13218 back to building a containing block's symbol lists. */
c24bdb02
KS
13219 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13220 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13221
921e78cf
JB
13222 /* If we've finished processing a top-level function, subsequent
13223 symbols go in the file symbol list. */
c24bdb02
KS
13224 if (cu->get_builder ()->outermost_context_p ())
13225 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13226}
13227
13228/* Process all the DIES contained within a lexical block scope. Start
13229 a new scope, process the dies, and then close the scope. */
13230
13231static void
e7c27a73 13232read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13233{
5e22e966 13234 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13235 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13236 CORE_ADDR lowpc, highpc;
13237 struct die_info *child_die;
e142c38c
DJ
13238 CORE_ADDR baseaddr;
13239
b3b3bada 13240 baseaddr = objfile->text_section_offset ();
c906108c
SS
13241
13242 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13243 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13244 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13245 be nasty. Might be easier to properly extend generic blocks to
af34e669 13246 describe ranges. */
e385593e
JK
13247 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13248 {
13249 case PC_BOUNDS_NOT_PRESENT:
13250 /* DW_TAG_lexical_block has no attributes, process its children as if
13251 there was no wrapping by that DW_TAG_lexical_block.
13252 GCC does no longer produces such DWARF since GCC r224161. */
13253 for (child_die = die->child;
13254 child_die != NULL && child_die->tag;
436c571c 13255 child_die = child_die->sibling)
4f7bc5ed
TT
13256 {
13257 /* We might already be processing this DIE. This can happen
13258 in an unusual circumstance -- where a subroutine A
13259 appears lexically in another subroutine B, but A actually
13260 inlines B. The recursion is broken here, rather than in
13261 inherit_abstract_dies, because it seems better to simply
13262 drop concrete children here. */
13263 if (!child_die->in_process)
13264 process_die (child_die, cu);
13265 }
e385593e
JK
13266 return;
13267 case PC_BOUNDS_INVALID:
13268 return;
13269 }
3e29f34a
MR
13270 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13271 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13272
c24bdb02 13273 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13274 if (die->child != NULL)
c906108c 13275 {
639d11d3 13276 child_die = die->child;
c906108c
SS
13277 while (child_die && child_die->tag)
13278 {
e7c27a73 13279 process_die (child_die, cu);
436c571c 13280 child_die = child_die->sibling;
c906108c
SS
13281 }
13282 }
3ea89b92 13283 inherit_abstract_dies (die, cu);
c24bdb02 13284 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13285
c24bdb02
KS
13286 if (*cu->get_builder ()->get_local_symbols () != NULL
13287 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13288 {
801e3a5b 13289 struct block *block
c24bdb02 13290 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13291 cstk.start_addr, highpc);
801e3a5b
JB
13292
13293 /* Note that recording ranges after traversing children, as we
13294 do here, means that recording a parent's ranges entails
13295 walking across all its children's ranges as they appear in
13296 the address map, which is quadratic behavior.
13297
13298 It would be nicer to record the parent's ranges before
13299 traversing its children, simply overriding whatever you find
13300 there. But since we don't even decide whether to create a
13301 block until after we've traversed its children, that's hard
13302 to do. */
13303 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13304 }
c24bdb02
KS
13305 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13306 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13307}
13308
216f72a1 13309/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13310
13311static void
13312read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13313{
5e22e966 13314 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13315 struct objfile *objfile = per_objfile->objfile;
08feed99 13316 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13317 CORE_ADDR pc, baseaddr;
13318 struct attribute *attr;
13319 struct call_site *call_site, call_site_local;
13320 void **slot;
13321 int nparams;
13322 struct die_info *child_die;
13323
b3b3bada 13324 baseaddr = objfile->text_section_offset ();
96408a79 13325
216f72a1
JK
13326 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13327 if (attr == NULL)
13328 {
13329 /* This was a pre-DWARF-5 GNU extension alias
13330 for DW_AT_call_return_pc. */
13331 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13332 }
96408a79
SA
13333 if (!attr)
13334 {
b98664d3 13335 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13336 "DIE %s [in module %s]"),
13337 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13338 return;
13339 }
cd6c91b4 13340 pc = attr->value_as_address () + baseaddr;
3e29f34a 13341 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13342
13343 if (cu->call_site_htab == NULL)
13344 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13345 NULL, &objfile->objfile_obstack,
13346 hashtab_obstack_allocate, NULL);
13347 call_site_local.pc = pc;
13348 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13349 if (*slot != NULL)
13350 {
b98664d3 13351 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13352 "DIE %s [in module %s]"),
13353 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13354 objfile_name (objfile));
96408a79
SA
13355 return;
13356 }
13357
13358 /* Count parameters at the caller. */
13359
13360 nparams = 0;
13361 for (child_die = die->child; child_die && child_die->tag;
436c571c 13362 child_die = child_die->sibling)
96408a79 13363 {
216f72a1
JK
13364 if (child_die->tag != DW_TAG_call_site_parameter
13365 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13366 {
b98664d3 13367 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13368 "DW_TAG_call_site child DIE %s [in module %s]"),
13369 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13370 objfile_name (objfile));
96408a79
SA
13371 continue;
13372 }
13373
13374 nparams++;
13375 }
13376
224c3ddb
SM
13377 call_site
13378 = ((struct call_site *)
13379 obstack_alloc (&objfile->objfile_obstack,
13380 sizeof (*call_site)
13381 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13382 *slot = call_site;
13383 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13384 call_site->pc = pc;
13385
216f72a1
JK
13386 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13387 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13388 {
13389 struct die_info *func_die;
13390
13391 /* Skip also over DW_TAG_inlined_subroutine. */
13392 for (func_die = die->parent;
13393 func_die && func_die->tag != DW_TAG_subprogram
13394 && func_die->tag != DW_TAG_subroutine_type;
13395 func_die = func_die->parent);
13396
216f72a1
JK
13397 /* DW_AT_call_all_calls is a superset
13398 of DW_AT_call_all_tail_calls. */
96408a79 13399 if (func_die
216f72a1 13400 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13401 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13402 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13403 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13404 {
13405 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13406 not complete. But keep CALL_SITE for look ups via call_site_htab,
13407 both the initial caller containing the real return address PC and
13408 the final callee containing the current PC of a chain of tail
13409 calls do not need to have the tail call list complete. But any
13410 function candidate for a virtual tail call frame searched via
13411 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13412 determined unambiguously. */
13413 }
13414 else
13415 {
13416 struct type *func_type = NULL;
13417
13418 if (func_die)
13419 func_type = get_die_type (func_die, cu);
13420 if (func_type != NULL)
13421 {
78134374 13422 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13423
13424 /* Enlist this call site to the function. */
13425 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13426 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13427 }
13428 else
b98664d3 13429 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13430 "DIE %s [in module %s]"),
13431 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13432 }
13433 }
13434
216f72a1
JK
13435 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13436 if (attr == NULL)
13437 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13438 if (attr == NULL)
13439 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13440 if (attr == NULL)
216f72a1
JK
13441 {
13442 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13443 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13444 }
96408a79 13445 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13446 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13447 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13448 else if (attr->form_is_block ())
96408a79
SA
13449 {
13450 struct dwarf2_locexpr_baton *dlbaton;
13451
8d749320 13452 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13453 dlbaton->data = DW_BLOCK (attr)->data;
13454 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13455 dlbaton->per_objfile = per_objfile;
96408a79
SA
13456 dlbaton->per_cu = cu->per_cu;
13457
13458 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13459 }
cd6c91b4 13460 else if (attr->form_is_ref ())
96408a79 13461 {
96408a79
SA
13462 struct dwarf2_cu *target_cu = cu;
13463 struct die_info *target_die;
13464
ac9ec31b 13465 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13466 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13467 if (die_is_declaration (target_die, target_cu))
13468 {
7d45c7c3 13469 const char *target_physname;
9112db09
JK
13470
13471 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13472 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13473 if (target_physname == NULL)
9112db09 13474 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13475 if (target_physname == NULL)
b98664d3 13476 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13477 "physname, for referencing DIE %s [in module %s]"),
13478 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13479 else
7d455152 13480 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13481 }
13482 else
13483 {
13484 CORE_ADDR lowpc;
13485
13486 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13487 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13488 <= PC_BOUNDS_INVALID)
b98664d3 13489 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13490 "low pc, for referencing DIE %s [in module %s]"),
13491 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13492 else
3e29f34a
MR
13493 {
13494 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13495 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13496 }
96408a79
SA
13497 }
13498 }
13499 else
b98664d3 13500 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13501 "block nor reference, for DIE %s [in module %s]"),
13502 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13503
13504 call_site->per_cu = cu->per_cu;
9f47c707 13505 call_site->per_objfile = per_objfile;
96408a79
SA
13506
13507 for (child_die = die->child;
13508 child_die && child_die->tag;
436c571c 13509 child_die = child_die->sibling)
96408a79 13510 {
96408a79 13511 struct call_site_parameter *parameter;
1788b2d3 13512 struct attribute *loc, *origin;
96408a79 13513
216f72a1
JK
13514 if (child_die->tag != DW_TAG_call_site_parameter
13515 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13516 {
13517 /* Already printed the complaint above. */
13518 continue;
13519 }
13520
13521 gdb_assert (call_site->parameter_count < nparams);
13522 parameter = &call_site->parameter[call_site->parameter_count];
13523
1788b2d3
JK
13524 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13525 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13526 register is contained in DW_AT_call_value. */
96408a79 13527
24c5c679 13528 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13529 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13530 if (origin == NULL)
13531 {
13532 /* This was a pre-DWARF-5 GNU extension alias
13533 for DW_AT_call_parameter. */
13534 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13535 }
cd6c91b4 13536 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13537 {
1788b2d3 13538 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13539
0826b30a 13540 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13541 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13542 {
13543 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13544 binding can be done only inside one CU. Such referenced DIE
13545 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13546 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13547 "DW_TAG_call_site child DIE %s [in module %s]"),
13548 sect_offset_str (child_die->sect_off),
9c541725 13549 objfile_name (objfile));
d76b7dbc
JK
13550 continue;
13551 }
9c541725
PA
13552 parameter->u.param_cu_off
13553 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13554 }
4fc6c0d5 13555 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13556 {
b98664d3 13557 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13558 "DW_TAG_call_site child DIE %s [in module %s]"),
13559 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13560 continue;
13561 }
24c5c679 13562 else
96408a79 13563 {
24c5c679
JK
13564 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13565 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13566 if (parameter->u.dwarf_reg != -1)
13567 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13568 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13569 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13570 &parameter->u.fb_offset))
13571 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13572 else
13573 {
b98664d3 13574 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13575 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13576 "DW_TAG_call_site child DIE %s "
24c5c679 13577 "[in module %s]"),
9d8780f0 13578 sect_offset_str (child_die->sect_off),
9c541725 13579 objfile_name (objfile));
24c5c679
JK
13580 continue;
13581 }
96408a79
SA
13582 }
13583
216f72a1
JK
13584 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13585 if (attr == NULL)
13586 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13587 if (attr == NULL || !attr->form_is_block ())
96408a79 13588 {
b98664d3 13589 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13590 "DW_TAG_call_site child DIE %s [in module %s]"),
13591 sect_offset_str (child_die->sect_off),
9c541725 13592 objfile_name (objfile));
96408a79
SA
13593 continue;
13594 }
13595 parameter->value = DW_BLOCK (attr)->data;
13596 parameter->value_size = DW_BLOCK (attr)->size;
13597
13598 /* Parameters are not pre-cleared by memset above. */
13599 parameter->data_value = NULL;
13600 parameter->data_value_size = 0;
13601 call_site->parameter_count++;
13602
216f72a1
JK
13603 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13604 if (attr == NULL)
13605 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13606 if (attr != nullptr)
96408a79 13607 {
4fc6c0d5 13608 if (!attr->form_is_block ())
b98664d3 13609 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13610 "DW_TAG_call_site child DIE %s [in module %s]"),
13611 sect_offset_str (child_die->sect_off),
9c541725 13612 objfile_name (objfile));
96408a79
SA
13613 else
13614 {
13615 parameter->data_value = DW_BLOCK (attr)->data;
13616 parameter->data_value_size = DW_BLOCK (attr)->size;
13617 }
13618 }
13619 }
13620}
13621
71a3c369
TT
13622/* Helper function for read_variable. If DIE represents a virtual
13623 table, then return the type of the concrete object that is
13624 associated with the virtual table. Otherwise, return NULL. */
13625
13626static struct type *
13627rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13628{
13629 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13630 if (attr == NULL)
13631 return NULL;
13632
13633 /* Find the type DIE. */
13634 struct die_info *type_die = NULL;
13635 struct dwarf2_cu *type_cu = cu;
13636
cd6c91b4 13637 if (attr->form_is_ref ())
71a3c369
TT
13638 type_die = follow_die_ref (die, attr, &type_cu);
13639 if (type_die == NULL)
13640 return NULL;
13641
13642 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13643 return NULL;
13644 return die_containing_type (type_die, type_cu);
13645}
13646
13647/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13648
13649static void
13650read_variable (struct die_info *die, struct dwarf2_cu *cu)
13651{
13652 struct rust_vtable_symbol *storage = NULL;
13653
13654 if (cu->language == language_rust)
13655 {
13656 struct type *containing_type = rust_containing_type (die, cu);
13657
13658 if (containing_type != NULL)
13659 {
5e22e966 13660 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13661
8c14c3a3 13662 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13663 storage->concrete_type = containing_type;
cf724bc9 13664 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13665 }
13666 }
13667
e4a62c65
TV
13668 struct symbol *res = new_symbol (die, NULL, cu, storage);
13669 struct attribute *abstract_origin
13670 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13671 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13672 if (res == NULL && loc && abstract_origin)
13673 {
13674 /* We have a variable without a name, but with a location and an abstract
13675 origin. This may be a concrete instance of an abstract variable
13676 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13677 later. */
13678 struct dwarf2_cu *origin_cu = cu;
13679 struct die_info *origin_die
13680 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13681 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13682 per_objfile->per_bfd->abstract_to_concrete
13683 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13684 }
71a3c369
TT
13685}
13686
43988095
JK
13687/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13688 reading .debug_rnglists.
13689 Callback's type should be:
13690 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13691 Return true if the attributes are present and valid, otherwise,
13692 return false. */
13693
13694template <typename Callback>
13695static bool
13696dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13697 Callback &&callback)
13698{
5e22e966 13699 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13700 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13701 bfd *obfd = objfile->obfd;
43988095 13702 /* Base address selection entry. */
2b24b6e4 13703 gdb::optional<CORE_ADDR> base;
43988095 13704 const gdb_byte *buffer;
43988095
JK
13705 CORE_ADDR baseaddr;
13706 bool overflow = false;
13707
43988095
JK
13708 base = cu->base_address;
13709
5989a64e
SM
13710 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13711 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13712 {
b98664d3 13713 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13714 offset);
13715 return false;
13716 }
5989a64e 13717 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13718
b3b3bada 13719 baseaddr = objfile->text_section_offset ();
43988095
JK
13720
13721 while (1)
13722 {
7814882a
JK
13723 /* Initialize it due to a false compiler warning. */
13724 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13725 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13726 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13727 unsigned int bytes_read;
13728
13729 if (buffer == buf_end)
13730 {
13731 overflow = true;
13732 break;
13733 }
13734 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13735 switch (rlet)
13736 {
13737 case DW_RLE_end_of_list:
13738 break;
13739 case DW_RLE_base_address:
13740 if (buffer + cu->header.addr_size > buf_end)
13741 {
13742 overflow = true;
13743 break;
13744 }
c8a7a66f 13745 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13746 buffer += bytes_read;
13747 break;
13748 case DW_RLE_start_length:
13749 if (buffer + cu->header.addr_size > buf_end)
13750 {
13751 overflow = true;
13752 break;
13753 }
c8a7a66f
TT
13754 range_beginning = cu->header.read_address (obfd, buffer,
13755 &bytes_read);
43988095
JK
13756 buffer += bytes_read;
13757 range_end = (range_beginning
13758 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13759 buffer += bytes_read;
13760 if (buffer > buf_end)
13761 {
13762 overflow = true;
13763 break;
13764 }
13765 break;
13766 case DW_RLE_offset_pair:
13767 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13768 buffer += bytes_read;
13769 if (buffer > buf_end)
13770 {
13771 overflow = true;
13772 break;
13773 }
13774 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13775 buffer += bytes_read;
13776 if (buffer > buf_end)
13777 {
13778 overflow = true;
13779 break;
13780 }
13781 break;
13782 case DW_RLE_start_end:
13783 if (buffer + 2 * cu->header.addr_size > buf_end)
13784 {
13785 overflow = true;
13786 break;
13787 }
c8a7a66f
TT
13788 range_beginning = cu->header.read_address (obfd, buffer,
13789 &bytes_read);
43988095 13790 buffer += bytes_read;
c8a7a66f 13791 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13792 buffer += bytes_read;
13793 break;
13794 default:
b98664d3 13795 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13796 return false;
13797 }
13798 if (rlet == DW_RLE_end_of_list || overflow)
13799 break;
13800 if (rlet == DW_RLE_base_address)
13801 continue;
13802
2b24b6e4 13803 if (!base.has_value ())
43988095
JK
13804 {
13805 /* We have no valid base address for the ranges
13806 data. */
b98664d3 13807 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13808 return false;
13809 }
13810
13811 if (range_beginning > range_end)
13812 {
13813 /* Inverted range entries are invalid. */
b98664d3 13814 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13815 return false;
13816 }
13817
13818 /* Empty range entries have no effect. */
13819 if (range_beginning == range_end)
13820 continue;
13821
2b24b6e4
TT
13822 range_beginning += *base;
13823 range_end += *base;
43988095
JK
13824
13825 /* A not-uncommon case of bad debug info.
13826 Don't pollute the addrmap with bad data. */
13827 if (range_beginning + baseaddr == 0
5989a64e 13828 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13829 {
b98664d3 13830 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13831 " [in module %s]"), objfile_name (objfile));
13832 continue;
13833 }
13834
13835 callback (range_beginning, range_end);
13836 }
13837
13838 if (overflow)
13839 {
b98664d3 13840 complaint (_("Offset %d is not terminated "
43988095
JK
13841 "for DW_AT_ranges attribute"),
13842 offset);
13843 return false;
13844 }
13845
13846 return true;
13847}
13848
13849/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13850 Callback's type should be:
13851 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13852 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13853
43988095 13854template <typename Callback>
43039443 13855static int
5f46c5a5 13856dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13857 Callback &&callback)
43039443 13858{
5e22e966
SM
13859 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13860 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13861 struct comp_unit_head *cu_header = &cu->header;
13862 bfd *obfd = objfile->obfd;
13863 unsigned int addr_size = cu_header->addr_size;
13864 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13865 /* Base address selection entry. */
2b24b6e4 13866 gdb::optional<CORE_ADDR> base;
43039443 13867 unsigned int dummy;
d521ce57 13868 const gdb_byte *buffer;
ff013f42 13869 CORE_ADDR baseaddr;
43039443 13870
43988095
JK
13871 if (cu_header->version >= 5)
13872 return dwarf2_rnglists_process (offset, cu, callback);
13873
d00adf39 13874 base = cu->base_address;
43039443 13875
5e22e966
SM
13876 per_objfile->per_bfd->ranges.read (objfile);
13877 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13878 {
b98664d3 13879 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13880 offset);
13881 return 0;
13882 }
5e22e966 13883 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13884
b3b3bada 13885 baseaddr = objfile->text_section_offset ();
ff013f42 13886
43039443
JK
13887 while (1)
13888 {
13889 CORE_ADDR range_beginning, range_end;
13890
c8a7a66f 13891 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13892 buffer += addr_size;
c8a7a66f 13893 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13894 buffer += addr_size;
13895 offset += 2 * addr_size;
13896
13897 /* An end of list marker is a pair of zero addresses. */
13898 if (range_beginning == 0 && range_end == 0)
13899 /* Found the end of list entry. */
13900 break;
13901
13902 /* Each base address selection entry is a pair of 2 values.
13903 The first is the largest possible address, the second is
13904 the base address. Check for a base address here. */
13905 if ((range_beginning & mask) == mask)
13906 {
28d2bfb9
AB
13907 /* If we found the largest possible address, then we already
13908 have the base address in range_end. */
13909 base = range_end;
43039443
JK
13910 continue;
13911 }
13912
2b24b6e4 13913 if (!base.has_value ())
43039443
JK
13914 {
13915 /* We have no valid base address for the ranges
13916 data. */
b98664d3 13917 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13918 return 0;
13919 }
13920
9277c30c
UW
13921 if (range_beginning > range_end)
13922 {
13923 /* Inverted range entries are invalid. */
b98664d3 13924 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13925 return 0;
13926 }
13927
13928 /* Empty range entries have no effect. */
13929 if (range_beginning == range_end)
13930 continue;
13931
2b24b6e4
TT
13932 range_beginning += *base;
13933 range_end += *base;
43039443 13934
01093045
DE
13935 /* A not-uncommon case of bad debug info.
13936 Don't pollute the addrmap with bad data. */
13937 if (range_beginning + baseaddr == 0
5e22e966 13938 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13939 {
b98664d3 13940 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13941 " [in module %s]"), objfile_name (objfile));
01093045
DE
13942 continue;
13943 }
13944
5f46c5a5
JK
13945 callback (range_beginning, range_end);
13946 }
13947
13948 return 1;
13949}
13950
13951/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13952 Return 1 if the attributes are present and valid, otherwise, return 0.
13953 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13954
13955static int
13956dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13957 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13958 dwarf2_psymtab *ranges_pst)
5f46c5a5 13959{
5e22e966 13960 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13961 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13962 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13963 int low_set = 0;
13964 CORE_ADDR low = 0;
13965 CORE_ADDR high = 0;
13966 int retval;
13967
13968 retval = dwarf2_ranges_process (offset, cu,
13969 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13970 {
9277c30c 13971 if (ranges_pst != NULL)
3e29f34a
MR
13972 {
13973 CORE_ADDR lowpc;
13974 CORE_ADDR highpc;
13975
79748972
TT
13976 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13977 range_beginning + baseaddr)
13978 - baseaddr);
13979 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13980 range_end + baseaddr)
13981 - baseaddr);
d320c2b5
TT
13982 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13983 lowpc, highpc - 1, ranges_pst);
3e29f34a 13984 }
ff013f42 13985
43039443
JK
13986 /* FIXME: This is recording everything as a low-high
13987 segment of consecutive addresses. We should have a
13988 data structure for discontiguous block ranges
13989 instead. */
13990 if (! low_set)
13991 {
13992 low = range_beginning;
13993 high = range_end;
13994 low_set = 1;
13995 }
13996 else
13997 {
13998 if (range_beginning < low)
13999 low = range_beginning;
14000 if (range_end > high)
14001 high = range_end;
14002 }
5f46c5a5
JK
14003 });
14004 if (!retval)
14005 return 0;
43039443
JK
14006
14007 if (! low_set)
14008 /* If the first entry is an end-of-list marker, the range
14009 describes an empty scope, i.e. no instructions. */
14010 return 0;
14011
14012 if (low_return)
14013 *low_return = low;
14014 if (high_return)
14015 *high_return = high;
14016 return 1;
14017}
14018
3a2b436a
JK
14019/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14020 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14021 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14022
3a2b436a 14023static enum pc_bounds_kind
af34e669 14024dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14025 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14026 dwarf2_psymtab *pst)
c906108c 14027{
5e22e966 14028 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 14029 struct attribute *attr;
91da1414 14030 struct attribute *attr_high;
af34e669
DJ
14031 CORE_ADDR low = 0;
14032 CORE_ADDR high = 0;
e385593e 14033 enum pc_bounds_kind ret;
c906108c 14034
91da1414
MW
14035 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14036 if (attr_high)
af34e669 14037 {
e142c38c 14038 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14039 if (attr != nullptr)
91da1414 14040 {
cd6c91b4
TT
14041 low = attr->value_as_address ();
14042 high = attr_high->value_as_address ();
14043 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14044 high += low;
91da1414 14045 }
af34e669
DJ
14046 else
14047 /* Found high w/o low attribute. */
e385593e 14048 return PC_BOUNDS_INVALID;
af34e669
DJ
14049
14050 /* Found consecutive range of addresses. */
3a2b436a 14051 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14052 }
c906108c 14053 else
af34e669 14054 {
e142c38c 14055 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14056 if (attr != NULL)
14057 {
18a8505e 14058 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14059 We take advantage of the fact that DW_AT_ranges does not appear
14060 in DW_TAG_compile_unit of DWO files. */
14061 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14062 unsigned int ranges_offset = (DW_UNSND (attr)
14063 + (need_ranges_base
14064 ? cu->ranges_base
14065 : 0));
2e3cf129 14066
af34e669 14067 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14068 .debug_ranges section. */
2e3cf129 14069 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14070 return PC_BOUNDS_INVALID;
43039443 14071 /* Found discontinuous range of addresses. */
3a2b436a 14072 ret = PC_BOUNDS_RANGES;
af34e669 14073 }
e385593e
JK
14074 else
14075 return PC_BOUNDS_NOT_PRESENT;
af34e669 14076 }
c906108c 14077
48fbe735 14078 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14079 if (high <= low)
e385593e 14080 return PC_BOUNDS_INVALID;
c906108c
SS
14081
14082 /* When using the GNU linker, .gnu.linkonce. sections are used to
14083 eliminate duplicate copies of functions and vtables and such.
14084 The linker will arbitrarily choose one and discard the others.
14085 The AT_*_pc values for such functions refer to local labels in
14086 these sections. If the section from that file was discarded, the
14087 labels are not in the output, so the relocs get a value of 0.
14088 If this is a discarded function, mark the pc bounds as invalid,
14089 so that GDB will ignore it. */
5989a64e 14090 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14091 return PC_BOUNDS_INVALID;
c906108c
SS
14092
14093 *lowpc = low;
96408a79
SA
14094 if (highpc)
14095 *highpc = high;
af34e669 14096 return ret;
c906108c
SS
14097}
14098
b084d499
JB
14099/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14100 its low and high PC addresses. Do nothing if these addresses could not
14101 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14102 and HIGHPC to the high address if greater than HIGHPC. */
14103
14104static void
14105dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14106 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14107 struct dwarf2_cu *cu)
14108{
14109 CORE_ADDR low, high;
14110 struct die_info *child = die->child;
14111
e385593e 14112 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14113 {
325fac50
PA
14114 *lowpc = std::min (*lowpc, low);
14115 *highpc = std::max (*highpc, high);
b084d499
JB
14116 }
14117
14118 /* If the language does not allow nested subprograms (either inside
14119 subprograms or lexical blocks), we're done. */
14120 if (cu->language != language_ada)
14121 return;
6e70227d 14122
b084d499
JB
14123 /* Check all the children of the given DIE. If it contains nested
14124 subprograms, then check their pc bounds. Likewise, we need to
14125 check lexical blocks as well, as they may also contain subprogram
14126 definitions. */
14127 while (child && child->tag)
14128 {
14129 if (child->tag == DW_TAG_subprogram
14130 || child->tag == DW_TAG_lexical_block)
14131 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14132 child = child->sibling;
b084d499
JB
14133 }
14134}
14135
fae299cd
DC
14136/* Get the low and high pc's represented by the scope DIE, and store
14137 them in *LOWPC and *HIGHPC. If the correct values can't be
14138 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14139
14140static void
14141get_scope_pc_bounds (struct die_info *die,
14142 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14143 struct dwarf2_cu *cu)
14144{
14145 CORE_ADDR best_low = (CORE_ADDR) -1;
14146 CORE_ADDR best_high = (CORE_ADDR) 0;
14147 CORE_ADDR current_low, current_high;
14148
3a2b436a 14149 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14150 >= PC_BOUNDS_RANGES)
fae299cd
DC
14151 {
14152 best_low = current_low;
14153 best_high = current_high;
14154 }
14155 else
14156 {
14157 struct die_info *child = die->child;
14158
14159 while (child && child->tag)
14160 {
14161 switch (child->tag) {
14162 case DW_TAG_subprogram:
b084d499 14163 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14164 break;
14165 case DW_TAG_namespace:
f55ee35c 14166 case DW_TAG_module:
fae299cd
DC
14167 /* FIXME: carlton/2004-01-16: Should we do this for
14168 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14169 that current GCC's always emit the DIEs corresponding
14170 to definitions of methods of classes as children of a
14171 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14172 the DIEs giving the declarations, which could be
14173 anywhere). But I don't see any reason why the
14174 standards says that they have to be there. */
14175 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14176
14177 if (current_low != ((CORE_ADDR) -1))
14178 {
325fac50
PA
14179 best_low = std::min (best_low, current_low);
14180 best_high = std::max (best_high, current_high);
fae299cd
DC
14181 }
14182 break;
14183 default:
0963b4bd 14184 /* Ignore. */
fae299cd
DC
14185 break;
14186 }
14187
436c571c 14188 child = child->sibling;
fae299cd
DC
14189 }
14190 }
14191
14192 *lowpc = best_low;
14193 *highpc = best_high;
14194}
14195
801e3a5b
JB
14196/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14197 in DIE. */
380bca97 14198
801e3a5b
JB
14199static void
14200dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14201 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14202{
5e22e966 14203 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14204 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14205 struct attribute *attr;
91da1414 14206 struct attribute *attr_high;
801e3a5b 14207
91da1414
MW
14208 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14209 if (attr_high)
801e3a5b 14210 {
801e3a5b 14211 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14212 if (attr != nullptr)
801e3a5b 14213 {
cd6c91b4
TT
14214 CORE_ADDR low = attr->value_as_address ();
14215 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14216
cd6c91b4 14217 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14218 high += low;
9a619af0 14219
3e29f34a
MR
14220 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14221 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14222 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14223 }
14224 }
14225
14226 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14227 if (attr != nullptr)
801e3a5b 14228 {
18a8505e 14229 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14230 We take advantage of the fact that DW_AT_ranges does not appear
14231 in DW_TAG_compile_unit of DWO files. */
14232 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14233
14234 /* The value of the DW_AT_ranges attribute is the offset of the
14235 address range list in the .debug_ranges section. */
ab435259
DE
14236 unsigned long offset = (DW_UNSND (attr)
14237 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14238
2d5f09ec 14239 std::vector<blockrange> blockvec;
5f46c5a5
JK
14240 dwarf2_ranges_process (offset, cu,
14241 [&] (CORE_ADDR start, CORE_ADDR end)
14242 {
58fdfd2c
JK
14243 start += baseaddr;
14244 end += baseaddr;
5f46c5a5
JK
14245 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14246 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14247 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14248 blockvec.emplace_back (start, end);
5f46c5a5 14249 });
2d5f09ec
KB
14250
14251 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14252 }
14253}
14254
685b1105
JK
14255/* Check whether the producer field indicates either of GCC < 4.6, or the
14256 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14257
685b1105
JK
14258static void
14259check_producer (struct dwarf2_cu *cu)
60d5a603 14260{
38360086 14261 int major, minor;
60d5a603
JK
14262
14263 if (cu->producer == NULL)
14264 {
14265 /* For unknown compilers expect their behavior is DWARF version
14266 compliant.
14267
14268 GCC started to support .debug_types sections by -gdwarf-4 since
14269 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14270 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14271 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14272 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14273 }
b1ffba5a 14274 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14275 {
38360086
MW
14276 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14277 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14278 }
5230b05a 14279 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14280 {
14281 cu->producer_is_icc = true;
14282 cu->producer_is_icc_lt_14 = major < 14;
14283 }
c258c396
JD
14284 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14285 cu->producer_is_codewarrior = true;
685b1105
JK
14286 else
14287 {
14288 /* For other non-GCC compilers, expect their behavior is DWARF version
14289 compliant. */
60d5a603
JK
14290 }
14291
9068261f 14292 cu->checked_producer = true;
685b1105 14293}
ba919b58 14294
685b1105
JK
14295/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14296 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14297 during 4.6.0 experimental. */
14298
9068261f 14299static bool
685b1105
JK
14300producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14301{
14302 if (!cu->checked_producer)
14303 check_producer (cu);
14304
14305 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14306}
14307
c258c396
JD
14308
14309/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14310 with incorrect is_stmt attributes. */
14311
14312static bool
14313producer_is_codewarrior (struct dwarf2_cu *cu)
14314{
14315 if (!cu->checked_producer)
14316 check_producer (cu);
14317
14318 return cu->producer_is_codewarrior;
14319}
14320
405feb71 14321/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14322 DW_AT_accessibility. */
14323
14324static enum dwarf_access_attribute
14325dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14326{
14327 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14328 {
14329 /* The default DWARF 2 accessibility for members is public, the default
14330 accessibility for inheritance is private. */
14331
14332 if (die->tag != DW_TAG_inheritance)
14333 return DW_ACCESS_public;
14334 else
14335 return DW_ACCESS_private;
14336 }
14337 else
14338 {
14339 /* DWARF 3+ defines the default accessibility a different way. The same
14340 rules apply now for DW_TAG_inheritance as for the members and it only
14341 depends on the container kind. */
14342
14343 if (die->parent->tag == DW_TAG_class_type)
14344 return DW_ACCESS_private;
14345 else
14346 return DW_ACCESS_public;
14347 }
14348}
14349
74ac6d43
TT
14350/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14351 offset. If the attribute was not found return 0, otherwise return
14352 1. If it was found but could not properly be handled, set *OFFSET
14353 to 0. */
14354
14355static int
14356handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14357 LONGEST *offset)
14358{
14359 struct attribute *attr;
14360
14361 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14362 if (attr != NULL)
14363 {
14364 *offset = 0;
14365
14366 /* Note that we do not check for a section offset first here.
14367 This is because DW_AT_data_member_location is new in DWARF 4,
14368 so if we see it, we can assume that a constant form is really
14369 a constant and not a section offset. */
cd6c91b4 14370 if (attr->form_is_constant ())
0826b30a 14371 *offset = attr->constant_value (0);
cd6c91b4 14372 else if (attr->form_is_section_offset ())
74ac6d43 14373 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14374 else if (attr->form_is_block ())
74ac6d43
TT
14375 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14376 else
14377 dwarf2_complex_location_expr_complaint ();
14378
14379 return 1;
14380 }
14381
14382 return 0;
14383}
14384
7d79de9a
TT
14385/* Look for DW_AT_data_member_location and store the results in FIELD. */
14386
14387static void
14388handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14389 struct field *field)
14390{
14391 struct attribute *attr;
14392
14393 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14394 if (attr != NULL)
14395 {
14396 if (attr->form_is_constant ())
14397 {
14398 LONGEST offset = attr->constant_value (0);
14399 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14400 }
14401 else if (attr->form_is_section_offset ())
14402 dwarf2_complex_location_expr_complaint ();
14403 else if (attr->form_is_block ())
14404 {
14405 bool handled;
14406 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14407 if (handled)
14408 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14409 else
14410 {
5e22e966
SM
14411 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14412 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14413 struct dwarf2_locexpr_baton *dlbaton
14414 = XOBNEW (&objfile->objfile_obstack,
14415 struct dwarf2_locexpr_baton);
14416 dlbaton->data = DW_BLOCK (attr)->data;
14417 dlbaton->size = DW_BLOCK (attr)->size;
14418 /* When using this baton, we want to compute the address
14419 of the field, not the value. This is why
14420 is_reference is set to false here. */
14421 dlbaton->is_reference = false;
5e22e966 14422 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14423 dlbaton->per_cu = cu->per_cu;
14424
14425 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14426 }
14427 }
14428 else
14429 dwarf2_complex_location_expr_complaint ();
14430 }
14431}
14432
c906108c
SS
14433/* Add an aggregate field to the field list. */
14434
14435static void
107d2387 14436dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14437 struct dwarf2_cu *cu)
6e70227d 14438{
5e22e966 14439 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14440 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14441 struct nextfield *new_field;
14442 struct attribute *attr;
14443 struct field *fp;
15d034d0 14444 const char *fieldname = "";
c906108c 14445
7d0ccb61
DJ
14446 if (die->tag == DW_TAG_inheritance)
14447 {
be2daae6
TT
14448 fip->baseclasses.emplace_back ();
14449 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14450 }
14451 else
14452 {
be2daae6
TT
14453 fip->fields.emplace_back ();
14454 new_field = &fip->fields.back ();
7d0ccb61 14455 }
be2daae6 14456
9c6a1327
TT
14457 new_field->offset = die->sect_off;
14458
e142c38c 14459 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14460 if (attr != nullptr)
c906108c 14461 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14462 else
14463 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14464 if (new_field->accessibility != DW_ACCESS_public)
14465 fip->non_public_fields = 1;
60d5a603 14466
e142c38c 14467 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14468 if (attr != nullptr)
c906108c 14469 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14470 else
14471 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14472
14473 fp = &new_field->field;
a9a9bd0f 14474
e142c38c 14475 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14476 {
a9a9bd0f 14477 /* Data member other than a C++ static data member. */
6e70227d 14478
c906108c 14479 /* Get type of field. */
e7c27a73 14480 fp->type = die_type (die, cu);
c906108c 14481
d6a843b5 14482 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14483
c906108c 14484 /* Get bit size of field (zero if none). */
e142c38c 14485 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14486 if (attr != nullptr)
c906108c
SS
14487 {
14488 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14489 }
14490 else
14491 {
14492 FIELD_BITSIZE (*fp) = 0;
14493 }
14494
14495 /* Get bit offset of field. */
7d79de9a 14496 handle_data_member_location (die, cu, fp);
e142c38c 14497 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14498 if (attr != nullptr)
c906108c 14499 {
d5a22e77 14500 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14501 {
14502 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14503 additional bit offset from the MSB of the containing
14504 anonymous object to the MSB of the field. We don't
14505 have to do anything special since we don't need to
14506 know the size of the anonymous object. */
f41f5e61 14507 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14508 }
14509 else
14510 {
14511 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14512 MSB of the anonymous object, subtract off the number of
14513 bits from the MSB of the field to the MSB of the
14514 object, and then subtract off the number of bits of
14515 the field itself. The result is the bit offset of
14516 the LSB of the field. */
c906108c
SS
14517 int anonymous_size;
14518 int bit_offset = DW_UNSND (attr);
14519
e142c38c 14520 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14521 if (attr != nullptr)
c906108c
SS
14522 {
14523 /* The size of the anonymous object containing
14524 the bit field is explicit, so use the
14525 indicated size (in bytes). */
14526 anonymous_size = DW_UNSND (attr);
14527 }
14528 else
14529 {
14530 /* The size of the anonymous object containing
14531 the bit field must be inferred from the type
14532 attribute of the data member containing the
14533 bit field. */
14534 anonymous_size = TYPE_LENGTH (fp->type);
14535 }
f41f5e61
PA
14536 SET_FIELD_BITPOS (*fp,
14537 (FIELD_BITPOS (*fp)
14538 + anonymous_size * bits_per_byte
14539 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14540 }
14541 }
da5b30da
AA
14542 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14543 if (attr != NULL)
14544 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14545 + attr->constant_value (0)));
c906108c
SS
14546
14547 /* Get name of field. */
39cbfefa
DJ
14548 fieldname = dwarf2_name (die, cu);
14549 if (fieldname == NULL)
14550 fieldname = "";
d8151005
DJ
14551
14552 /* The name is already allocated along with this objfile, so we don't
14553 need to duplicate it for the type. */
14554 fp->name = fieldname;
c906108c
SS
14555
14556 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14557 pointer or virtual base class pointer) to private. */
e142c38c 14558 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14559 {
d48cc9dd 14560 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14561 new_field->accessibility = DW_ACCESS_private;
14562 fip->non_public_fields = 1;
14563 }
14564 }
a9a9bd0f 14565 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14566 {
a9a9bd0f
DC
14567 /* C++ static member. */
14568
14569 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14570 is a declaration, but all versions of G++ as of this writing
14571 (so through at least 3.2.1) incorrectly generate
14572 DW_TAG_variable tags. */
6e70227d 14573
ff355380 14574 const char *physname;
c906108c 14575
a9a9bd0f 14576 /* Get name of field. */
39cbfefa
DJ
14577 fieldname = dwarf2_name (die, cu);
14578 if (fieldname == NULL)
c906108c
SS
14579 return;
14580
254e6b9e 14581 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14582 if (attr
14583 /* Only create a symbol if this is an external value.
14584 new_symbol checks this and puts the value in the global symbol
14585 table, which we want. If it is not external, new_symbol
14586 will try to put the value in cu->list_in_scope which is wrong. */
14587 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14588 {
14589 /* A static const member, not much different than an enum as far as
14590 we're concerned, except that we can support more types. */
14591 new_symbol (die, NULL, cu);
14592 }
14593
2df3850c 14594 /* Get physical name. */
ff355380 14595 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14596
d8151005
DJ
14597 /* The name is already allocated along with this objfile, so we don't
14598 need to duplicate it for the type. */
14599 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14600 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14601 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14602 }
14603 else if (die->tag == DW_TAG_inheritance)
14604 {
74ac6d43 14605 /* C++ base class field. */
7d79de9a 14606 handle_data_member_location (die, cu, fp);
c906108c 14607 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14608 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14609 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14610 }
2ddeaf8a
TT
14611 else
14612 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14613}
14614
883fd55a
KS
14615/* Can the type given by DIE define another type? */
14616
14617static bool
14618type_can_define_types (const struct die_info *die)
14619{
14620 switch (die->tag)
14621 {
14622 case DW_TAG_typedef:
14623 case DW_TAG_class_type:
14624 case DW_TAG_structure_type:
14625 case DW_TAG_union_type:
14626 case DW_TAG_enumeration_type:
14627 return true;
14628
14629 default:
14630 return false;
14631 }
14632}
14633
14634/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14635
14636static void
883fd55a
KS
14637dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14638 struct dwarf2_cu *cu)
6e70227d 14639{
be2daae6
TT
14640 struct decl_field fp;
14641 memset (&fp, 0, sizeof (fp));
98751a41 14642
883fd55a 14643 gdb_assert (type_can_define_types (die));
98751a41 14644
883fd55a 14645 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14646 fp.name = dwarf2_name (die, cu);
14647 fp.type = read_type_die (die, cu);
98751a41 14648
c191a687
KS
14649 /* Save accessibility. */
14650 enum dwarf_access_attribute accessibility;
14651 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14652 if (attr != NULL)
14653 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14654 else
14655 accessibility = dwarf2_default_access_attribute (die, cu);
14656 switch (accessibility)
14657 {
14658 case DW_ACCESS_public:
14659 /* The assumed value if neither private nor protected. */
14660 break;
14661 case DW_ACCESS_private:
be2daae6 14662 fp.is_private = 1;
c191a687
KS
14663 break;
14664 case DW_ACCESS_protected:
be2daae6 14665 fp.is_protected = 1;
c191a687
KS
14666 break;
14667 default:
b98664d3 14668 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14669 }
14670
883fd55a 14671 if (die->tag == DW_TAG_typedef)
be2daae6 14672 fip->typedef_field_list.push_back (fp);
883fd55a 14673 else
be2daae6 14674 fip->nested_types_list.push_back (fp);
98751a41
JK
14675}
14676
9c6a1327
TT
14677/* A convenience typedef that's used when finding the discriminant
14678 field for a variant part. */
1b95cdb7
SM
14679typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14680 offset_map_type;
9c6a1327
TT
14681
14682/* Compute the discriminant range for a given variant. OBSTACK is
14683 where the results will be stored. VARIANT is the variant to
14684 process. IS_UNSIGNED indicates whether the discriminant is signed
14685 or unsigned. */
14686
14687static const gdb::array_view<discriminant_range>
14688convert_variant_range (struct obstack *obstack, const variant_field &variant,
14689 bool is_unsigned)
14690{
14691 std::vector<discriminant_range> ranges;
14692
14693 if (variant.default_branch)
14694 return {};
14695
14696 if (variant.discr_list_data == nullptr)
14697 {
14698 discriminant_range r
14699 = {variant.discriminant_value, variant.discriminant_value};
14700 ranges.push_back (r);
14701 }
14702 else
14703 {
14704 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14705 variant.discr_list_data->size);
14706 while (!data.empty ())
14707 {
14708 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14709 {
14710 complaint (_("invalid discriminant marker: %d"), data[0]);
14711 break;
14712 }
14713 bool is_range = data[0] == DW_DSC_range;
14714 data = data.slice (1);
14715
14716 ULONGEST low, high;
14717 unsigned int bytes_read;
14718
14719 if (data.empty ())
14720 {
14721 complaint (_("DW_AT_discr_list missing low value"));
14722 break;
14723 }
14724 if (is_unsigned)
14725 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14726 else
14727 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14728 &bytes_read);
14729 data = data.slice (bytes_read);
14730
14731 if (is_range)
14732 {
14733 if (data.empty ())
14734 {
14735 complaint (_("DW_AT_discr_list missing high value"));
14736 break;
14737 }
14738 if (is_unsigned)
14739 high = read_unsigned_leb128 (nullptr, data.data (),
14740 &bytes_read);
14741 else
14742 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14743 &bytes_read);
14744 data = data.slice (bytes_read);
14745 }
14746 else
14747 high = low;
14748
14749 ranges.push_back ({ low, high });
14750 }
14751 }
14752
14753 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14754 ranges.size ());
14755 std::copy (ranges.begin (), ranges.end (), result);
14756 return gdb::array_view<discriminant_range> (result, ranges.size ());
14757}
14758
14759static const gdb::array_view<variant_part> create_variant_parts
14760 (struct obstack *obstack,
14761 const offset_map_type &offset_map,
14762 struct field_info *fi,
14763 const std::vector<variant_part_builder> &variant_parts);
14764
14765/* Fill in a "struct variant" for a given variant field. RESULT is
14766 the variant to fill in. OBSTACK is where any needed allocations
14767 will be done. OFFSET_MAP holds the mapping from section offsets to
14768 fields for the type. FI describes the fields of the type we're
14769 processing. FIELD is the variant field we're converting. */
14770
14771static void
14772create_one_variant (variant &result, struct obstack *obstack,
14773 const offset_map_type &offset_map,
14774 struct field_info *fi, const variant_field &field)
14775{
14776 result.discriminants = convert_variant_range (obstack, field, false);
14777 result.first_field = field.first_field + fi->baseclasses.size ();
14778 result.last_field = field.last_field + fi->baseclasses.size ();
14779 result.parts = create_variant_parts (obstack, offset_map, fi,
14780 field.variant_parts);
14781}
14782
14783/* Fill in a "struct variant_part" for a given variant part. RESULT
14784 is the variant part to fill in. OBSTACK is where any needed
14785 allocations will be done. OFFSET_MAP holds the mapping from
14786 section offsets to fields for the type. FI describes the fields of
14787 the type we're processing. BUILDER is the variant part to be
14788 converted. */
14789
14790static void
14791create_one_variant_part (variant_part &result,
14792 struct obstack *obstack,
14793 const offset_map_type &offset_map,
14794 struct field_info *fi,
14795 const variant_part_builder &builder)
14796{
14797 auto iter = offset_map.find (builder.discriminant_offset);
14798 if (iter == offset_map.end ())
14799 {
14800 result.discriminant_index = -1;
14801 /* Doesn't matter. */
14802 result.is_unsigned = false;
14803 }
14804 else
14805 {
14806 result.discriminant_index = iter->second;
14807 result.is_unsigned
14808 = TYPE_UNSIGNED (FIELD_TYPE
14809 (fi->fields[result.discriminant_index].field));
14810 }
14811
14812 size_t n = builder.variants.size ();
14813 variant *output = new (obstack) variant[n];
14814 for (size_t i = 0; i < n; ++i)
14815 create_one_variant (output[i], obstack, offset_map, fi,
14816 builder.variants[i]);
14817
14818 result.variants = gdb::array_view<variant> (output, n);
14819}
14820
14821/* Create a vector of variant parts that can be attached to a type.
14822 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14823 holds the mapping from section offsets to fields for the type. FI
14824 describes the fields of the type we're processing. VARIANT_PARTS
14825 is the vector to convert. */
14826
14827static const gdb::array_view<variant_part>
14828create_variant_parts (struct obstack *obstack,
14829 const offset_map_type &offset_map,
14830 struct field_info *fi,
14831 const std::vector<variant_part_builder> &variant_parts)
14832{
14833 if (variant_parts.empty ())
14834 return {};
14835
14836 size_t n = variant_parts.size ();
14837 variant_part *result = new (obstack) variant_part[n];
14838 for (size_t i = 0; i < n; ++i)
14839 create_one_variant_part (result[i], obstack, offset_map, fi,
14840 variant_parts[i]);
14841
14842 return gdb::array_view<variant_part> (result, n);
14843}
14844
14845/* Compute the variant part vector for FIP, attaching it to TYPE when
14846 done. */
14847
14848static void
14849add_variant_property (struct field_info *fip, struct type *type,
14850 struct dwarf2_cu *cu)
14851{
14852 /* Map section offsets of fields to their field index. Note the
14853 field index here does not take the number of baseclasses into
14854 account. */
14855 offset_map_type offset_map;
14856 for (int i = 0; i < fip->fields.size (); ++i)
14857 offset_map[fip->fields[i].offset] = i;
14858
5e22e966 14859 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14860 gdb::array_view<variant_part> parts
14861 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14862 fip->variant_parts);
14863
14864 struct dynamic_prop prop;
14865 prop.kind = PROP_VARIANT_PARTS;
14866 prop.data.variant_parts
14867 = ((gdb::array_view<variant_part> *)
14868 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14869
5c54719c 14870 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14871}
14872
c906108c
SS
14873/* Create the vector of fields, and attach it to the type. */
14874
14875static void
fba45db2 14876dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14877 struct dwarf2_cu *cu)
c906108c 14878{
317f7127 14879 int nfields = fip->nfields ();
c906108c
SS
14880
14881 /* Record the field count, allocate space for the array of fields,
14882 and create blank accessibility bitfields if necessary. */
5e33d5f4 14883 type->set_num_fields (nfields);
3cabb6b0
SM
14884 type->set_fields
14885 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14886
b4ba55a1 14887 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14888 {
14889 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14890
14891 TYPE_FIELD_PRIVATE_BITS (type) =
14892 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14893 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14894
14895 TYPE_FIELD_PROTECTED_BITS (type) =
14896 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14897 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14898
774b6a14
TT
14899 TYPE_FIELD_IGNORE_BITS (type) =
14900 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14901 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14902 }
14903
14904 /* If the type has baseclasses, allocate and clear a bit vector for
14905 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14906 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14907 {
be2daae6 14908 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14909 unsigned char *pointer;
c906108c
SS
14910
14911 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14912 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14913 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14914 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14915 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14916 }
14917
9c6a1327
TT
14918 if (!fip->variant_parts.empty ())
14919 add_variant_property (fip, type, cu);
2ddeaf8a 14920
be2daae6
TT
14921 /* Copy the saved-up fields into the field vector. */
14922 for (int i = 0; i < nfields; ++i)
c906108c 14923 {
be2daae6
TT
14924 struct nextfield &field
14925 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14926 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14927
ceacbf6e 14928 type->field (i) = field.field;
be2daae6 14929 switch (field.accessibility)
c906108c 14930 {
c5aa993b 14931 case DW_ACCESS_private:
b4ba55a1 14932 if (cu->language != language_ada)
be2daae6 14933 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14934 break;
c906108c 14935
c5aa993b 14936 case DW_ACCESS_protected:
b4ba55a1 14937 if (cu->language != language_ada)
be2daae6 14938 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14939 break;
c906108c 14940
c5aa993b
JM
14941 case DW_ACCESS_public:
14942 break;
c906108c 14943
c5aa993b
JM
14944 default:
14945 /* Unknown accessibility. Complain and treat it as public. */
14946 {
b98664d3 14947 complaint (_("unsupported accessibility %d"),
be2daae6 14948 field.accessibility);
c5aa993b
JM
14949 }
14950 break;
c906108c 14951 }
be2daae6 14952 if (i < fip->baseclasses.size ())
c906108c 14953 {
be2daae6 14954 switch (field.virtuality)
c906108c 14955 {
c5aa993b
JM
14956 case DW_VIRTUALITY_virtual:
14957 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14958 if (cu->language == language_ada)
a73c6dcd 14959 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14960 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14961 break;
c906108c
SS
14962 }
14963 }
c906108c
SS
14964 }
14965}
14966
7d27a96d
TT
14967/* Return true if this member function is a constructor, false
14968 otherwise. */
14969
14970static int
14971dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14972{
14973 const char *fieldname;
fe978cb0 14974 const char *type_name;
7d27a96d
TT
14975 int len;
14976
14977 if (die->parent == NULL)
14978 return 0;
14979
14980 if (die->parent->tag != DW_TAG_structure_type
14981 && die->parent->tag != DW_TAG_union_type
14982 && die->parent->tag != DW_TAG_class_type)
14983 return 0;
14984
14985 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14986 type_name = dwarf2_name (die->parent, cu);
14987 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14988 return 0;
14989
14990 len = strlen (fieldname);
fe978cb0
PA
14991 return (strncmp (fieldname, type_name, len) == 0
14992 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14993}
14994
e35000a7
TBA
14995/* Check if the given VALUE is a recognized enum
14996 dwarf_defaulted_attribute constant according to DWARF5 spec,
14997 Table 7.24. */
14998
14999static bool
15000is_valid_DW_AT_defaulted (ULONGEST value)
15001{
15002 switch (value)
15003 {
15004 case DW_DEFAULTED_no:
15005 case DW_DEFAULTED_in_class:
15006 case DW_DEFAULTED_out_of_class:
15007 return true;
15008 }
15009
3142e908 15010 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15011 return false;
15012}
15013
c906108c
SS
15014/* Add a member function to the proper fieldlist. */
15015
15016static void
107d2387 15017dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15018 struct type *type, struct dwarf2_cu *cu)
c906108c 15019{
5e22e966 15020 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15021 struct attribute *attr;
c906108c 15022 int i;
be2daae6 15023 struct fnfieldlist *flp = nullptr;
c906108c 15024 struct fn_field *fnp;
15d034d0 15025 const char *fieldname;
f792889a 15026 struct type *this_type;
60d5a603 15027 enum dwarf_access_attribute accessibility;
c906108c 15028
b4ba55a1 15029 if (cu->language == language_ada)
a73c6dcd 15030 error (_("unexpected member function in Ada type"));
b4ba55a1 15031
2df3850c 15032 /* Get name of member function. */
39cbfefa
DJ
15033 fieldname = dwarf2_name (die, cu);
15034 if (fieldname == NULL)
2df3850c 15035 return;
c906108c 15036
c906108c 15037 /* Look up member function name in fieldlist. */
be2daae6 15038 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15039 {
27bfe10e 15040 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15041 {
15042 flp = &fip->fnfieldlists[i];
15043 break;
15044 }
c906108c
SS
15045 }
15046
be2daae6
TT
15047 /* Create a new fnfieldlist if necessary. */
15048 if (flp == nullptr)
c906108c 15049 {
be2daae6
TT
15050 fip->fnfieldlists.emplace_back ();
15051 flp = &fip->fnfieldlists.back ();
c906108c 15052 flp->name = fieldname;
be2daae6 15053 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15054 }
15055
be2daae6
TT
15056 /* Create a new member function field and add it to the vector of
15057 fnfieldlists. */
15058 flp->fnfields.emplace_back ();
15059 fnp = &flp->fnfields.back ();
3da10d80
KS
15060
15061 /* Delay processing of the physname until later. */
9c37b5ae 15062 if (cu->language == language_cplus)
be2daae6
TT
15063 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15064 die, cu);
3da10d80
KS
15065 else
15066 {
1d06ead6 15067 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15068 fnp->physname = physname ? physname : "";
15069 }
15070
c906108c 15071 fnp->type = alloc_type (objfile);
f792889a 15072 this_type = read_type_die (die, cu);
78134374 15073 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15074 {
1f704f76 15075 int nparams = this_type->num_fields ();
c906108c 15076
f792889a 15077 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15078 of the method itself (TYPE_CODE_METHOD). */
15079 smash_to_method_type (fnp->type, type,
f792889a 15080 TYPE_TARGET_TYPE (this_type),
80fc5e77 15081 this_type->fields (),
1f704f76 15082 this_type->num_fields (),
f792889a 15083 TYPE_VARARGS (this_type));
c906108c
SS
15084
15085 /* Handle static member functions.
c5aa993b 15086 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15087 member functions. G++ helps GDB by marking the first
15088 parameter for non-static member functions (which is the this
15089 pointer) as artificial. We obtain this information from
15090 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15091 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15092 fnp->voffset = VOFFSET_STATIC;
15093 }
15094 else
b98664d3 15095 complaint (_("member function type missing for '%s'"),
3da10d80 15096 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15097
15098 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15099 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15100 fnp->fcontext = die_containing_type (die, cu);
c906108c 15101
3e43a32a
MS
15102 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15103 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15104
15105 /* Get accessibility. */
e142c38c 15106 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15107 if (attr != nullptr)
aead7601 15108 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15109 else
15110 accessibility = dwarf2_default_access_attribute (die, cu);
15111 switch (accessibility)
c906108c 15112 {
60d5a603
JK
15113 case DW_ACCESS_private:
15114 fnp->is_private = 1;
15115 break;
15116 case DW_ACCESS_protected:
15117 fnp->is_protected = 1;
15118 break;
c906108c
SS
15119 }
15120
b02dede2 15121 /* Check for artificial methods. */
e142c38c 15122 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15123 if (attr && DW_UNSND (attr) != 0)
15124 fnp->is_artificial = 1;
15125
e35000a7
TBA
15126 /* Check for defaulted methods. */
15127 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15128 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15129 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15130
15131 /* Check for deleted methods. */
15132 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15133 if (attr != nullptr && DW_UNSND (attr) != 0)
15134 fnp->is_deleted = 1;
15135
7d27a96d
TT
15136 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15137
0d564a31 15138 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15139 function. For older versions of GCC, this is an offset in the
15140 appropriate virtual table, as specified by DW_AT_containing_type.
15141 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15142 to the object address. */
15143
e142c38c 15144 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15145 if (attr != nullptr)
8e19ed76 15146 {
4fc6c0d5 15147 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15148 {
aec5aa8b
TT
15149 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15150 {
15151 /* Old-style GCC. */
15152 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15153 }
15154 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15155 || (DW_BLOCK (attr)->size > 1
15156 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15157 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15158 {
aec5aa8b
TT
15159 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15160 if ((fnp->voffset % cu->header.addr_size) != 0)
15161 dwarf2_complex_location_expr_complaint ();
15162 else
15163 fnp->voffset /= cu->header.addr_size;
15164 fnp->voffset += 2;
15165 }
15166 else
15167 dwarf2_complex_location_expr_complaint ();
15168
15169 if (!fnp->fcontext)
7e993ebf
KS
15170 {
15171 /* If there is no `this' field and no DW_AT_containing_type,
15172 we cannot actually find a base class context for the
15173 vtable! */
1f704f76 15174 if (this_type->num_fields () == 0
7e993ebf
KS
15175 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15176 {
b98664d3 15177 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15178 "function \"%s\" (offset %s)"),
15179 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15180 }
15181 else
15182 {
15183 fnp->fcontext
15184 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15185 }
15186 }
aec5aa8b 15187 }
cd6c91b4 15188 else if (attr->form_is_section_offset ())
8e19ed76 15189 {
4d3c2250 15190 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15191 }
15192 else
15193 {
4d3c2250
KB
15194 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15195 fieldname);
8e19ed76 15196 }
0d564a31 15197 }
d48cc9dd
DJ
15198 else
15199 {
15200 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15201 if (attr && DW_UNSND (attr))
15202 {
15203 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15204 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15205 "but the vtable offset is not specified"),
9d8780f0 15206 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15207 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15208 TYPE_CPLUS_DYNAMIC (type) = 1;
15209 }
15210 }
c906108c
SS
15211}
15212
15213/* Create the vector of member function fields, and attach it to the type. */
15214
15215static void
fba45db2 15216dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15217 struct dwarf2_cu *cu)
c906108c 15218{
b4ba55a1 15219 if (cu->language == language_ada)
a73c6dcd 15220 error (_("unexpected member functions in Ada type"));
b4ba55a1 15221
c906108c
SS
15222 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15223 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15224 TYPE_ALLOC (type,
15225 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15226
be2daae6 15227 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15228 {
be2daae6 15229 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15230 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15231
be2daae6
TT
15232 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15233 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15234 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15235 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15236
15237 for (int k = 0; k < nf.fnfields.size (); ++k)
15238 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15239 }
15240
be2daae6 15241 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15242}
15243
1168df01
JB
15244/* Returns non-zero if NAME is the name of a vtable member in CU's
15245 language, zero otherwise. */
15246static int
15247is_vtable_name (const char *name, struct dwarf2_cu *cu)
15248{
15249 static const char vptr[] = "_vptr";
15250
9c37b5ae
TT
15251 /* Look for the C++ form of the vtable. */
15252 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15253 return 1;
15254
15255 return 0;
15256}
15257
c0dd20ea 15258/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15259 functions, with the ABI-specified layout. If TYPE describes
15260 such a structure, smash it into a member function type.
61049d3b
DJ
15261
15262 GCC shouldn't do this; it should just output pointer to member DIEs.
15263 This is GCC PR debug/28767. */
c0dd20ea 15264
0b92b5bb
TT
15265static void
15266quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15267{
09e2d7c7 15268 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15269
15270 /* Check for a structure with no name and two children. */
1f704f76 15271 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15272 return;
c0dd20ea
DJ
15273
15274 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15275 if (TYPE_FIELD_NAME (type, 0) == NULL
15276 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15277 || TYPE_FIELD_NAME (type, 1) == NULL
15278 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15279 return;
c0dd20ea
DJ
15280
15281 /* Find the type of the method. */
0b92b5bb 15282 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15283 if (pfn_type == NULL
78134374
SM
15284 || pfn_type->code () != TYPE_CODE_PTR
15285 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15286 return;
c0dd20ea
DJ
15287
15288 /* Look for the "this" argument. */
15289 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15290 if (pfn_type->num_fields () == 0
0b92b5bb 15291 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15292 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15293 return;
c0dd20ea 15294
09e2d7c7 15295 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15296 new_type = alloc_type (objfile);
09e2d7c7 15297 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15298 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15299 TYPE_VARARGS (pfn_type));
0b92b5bb 15300 smash_to_methodptr_type (type, new_type);
c0dd20ea 15301}
1168df01 15302
2b4424c3
TT
15303/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15304 appropriate error checking and issuing complaints if there is a
15305 problem. */
15306
15307static ULONGEST
15308get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15309{
15310 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15311
15312 if (attr == nullptr)
15313 return 0;
15314
cd6c91b4 15315 if (!attr->form_is_constant ())
2b4424c3 15316 {
b98664d3 15317 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15318 " - DIE at %s [in module %s]"),
15319 sect_offset_str (die->sect_off),
5e22e966 15320 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15321 return 0;
15322 }
15323
15324 ULONGEST align;
15325 if (attr->form == DW_FORM_sdata)
15326 {
15327 LONGEST val = DW_SND (attr);
15328 if (val < 0)
15329 {
b98664d3 15330 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15331 " - DIE at %s [in module %s]"),
15332 sect_offset_str (die->sect_off),
5e22e966 15333 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15334 return 0;
15335 }
15336 align = val;
15337 }
15338 else
15339 align = DW_UNSND (attr);
15340
15341 if (align == 0)
15342 {
b98664d3 15343 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15344 " - DIE at %s [in module %s]"),
15345 sect_offset_str (die->sect_off),
5e22e966 15346 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15347 return 0;
15348 }
15349 if ((align & (align - 1)) != 0)
15350 {
b98664d3 15351 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15352 " - DIE at %s [in module %s]"),
15353 sect_offset_str (die->sect_off),
5e22e966 15354 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15355 return 0;
15356 }
15357
15358 return align;
15359}
15360
15361/* If the DIE has a DW_AT_alignment attribute, use its value to set
15362 the alignment for TYPE. */
15363
15364static void
15365maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15366 struct type *type)
15367{
15368 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15369 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15370 " - DIE at %s [in module %s]"),
15371 sect_offset_str (die->sect_off),
5e22e966 15372 objfile_name (cu->per_objfile->objfile));
2b4424c3 15373}
685b1105 15374
e35000a7
TBA
15375/* Check if the given VALUE is a valid enum dwarf_calling_convention
15376 constant for a type, according to DWARF5 spec, Table 5.5. */
15377
15378static bool
15379is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15380{
15381 switch (value)
15382 {
15383 case DW_CC_normal:
15384 case DW_CC_pass_by_reference:
15385 case DW_CC_pass_by_value:
15386 return true;
15387
15388 default:
15389 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15390 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15391 return false;
15392 }
15393}
15394
d0922fcf
TBA
15395/* Check if the given VALUE is a valid enum dwarf_calling_convention
15396 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15397 also according to GNU-specific values (see include/dwarf2.h). */
15398
15399static bool
15400is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15401{
15402 switch (value)
15403 {
15404 case DW_CC_normal:
15405 case DW_CC_program:
15406 case DW_CC_nocall:
15407 return true;
15408
15409 case DW_CC_GNU_renesas_sh:
15410 case DW_CC_GNU_borland_fastcall_i386:
15411 case DW_CC_GDB_IBM_OpenCL:
15412 return true;
15413
15414 default:
15415 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15416 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15417 return false;
15418 }
15419}
15420
c906108c 15421/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15422 (definition) to create a type for the structure or union. Fill in
15423 the type's name and general properties; the members will not be
83655187
DE
15424 processed until process_structure_scope. A symbol table entry for
15425 the type will also not be done until process_structure_scope (assuming
15426 the type has a name).
c906108c 15427
c767944b
DJ
15428 NOTE: we need to call these functions regardless of whether or not the
15429 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15430 structure or union. This gets the type entered into our set of
83655187 15431 user defined types. */
c906108c 15432
f792889a 15433static struct type *
134d01f1 15434read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15435{
5e22e966 15436 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15437 struct type *type;
15438 struct attribute *attr;
15d034d0 15439 const char *name;
c906108c 15440
348e048f
DE
15441 /* If the definition of this type lives in .debug_types, read that type.
15442 Don't follow DW_AT_specification though, that will take us back up
15443 the chain and we want to go down. */
052c8bb8 15444 attr = die->attr (DW_AT_signature);
435d3d88 15445 if (attr != nullptr)
348e048f 15446 {
ac9ec31b 15447 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15448
ac9ec31b 15449 /* The type's CU may not be the same as CU.
02142a6c 15450 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15451 return set_die_type (die, type, cu);
15452 }
15453
c0dd20ea 15454 type = alloc_type (objfile);
c906108c 15455 INIT_CPLUS_SPECIFIC (type);
93311388 15456
39cbfefa
DJ
15457 name = dwarf2_name (die, cu);
15458 if (name != NULL)
c906108c 15459 {
987504bb 15460 if (cu->language == language_cplus
c44af4eb
TT
15461 || cu->language == language_d
15462 || cu->language == language_rust)
63d06c5c 15463 {
15d034d0 15464 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15465
15466 /* dwarf2_full_name might have already finished building the DIE's
15467 type. If so, there is no need to continue. */
15468 if (get_die_type (die, cu) != NULL)
15469 return get_die_type (die, cu);
15470
d0e39ea2 15471 type->set_name (full_name);
63d06c5c
DC
15472 }
15473 else
15474 {
d8151005
DJ
15475 /* The name is already allocated along with this objfile, so
15476 we don't need to duplicate it for the type. */
d0e39ea2 15477 type->set_name (name);
63d06c5c 15478 }
c906108c
SS
15479 }
15480
15481 if (die->tag == DW_TAG_structure_type)
15482 {
67607e24 15483 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15484 }
15485 else if (die->tag == DW_TAG_union_type)
15486 {
67607e24 15487 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15488 }
15489 else
15490 {
67607e24 15491 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15492 }
15493
0cc2414c
TT
15494 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15495 TYPE_DECLARED_CLASS (type) = 1;
15496
e35000a7
TBA
15497 /* Store the calling convention in the type if it's available in
15498 the die. Otherwise the calling convention remains set to
15499 the default value DW_CC_normal. */
15500 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15501 if (attr != nullptr
15502 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15503 {
15504 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15505 TYPE_CPLUS_CALLING_CONVENTION (type)
15506 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15507 }
15508
e142c38c 15509 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15510 if (attr != nullptr)
c906108c 15511 {
cd6c91b4 15512 if (attr->form_is_constant ())
155bfbd3
JB
15513 TYPE_LENGTH (type) = DW_UNSND (attr);
15514 else
15515 {
f8e89861 15516 struct dynamic_prop prop;
293e7e51 15517 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15518 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15519 TYPE_LENGTH (type) = 0;
15520 }
c906108c
SS
15521 }
15522 else
15523 {
15524 TYPE_LENGTH (type) = 0;
15525 }
15526
2b4424c3
TT
15527 maybe_set_alignment (cu, die, type);
15528
5230b05a 15529 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15530 {
5230b05a
WT
15531 /* ICC<14 does not output the required DW_AT_declaration on
15532 incomplete types, but gives them a size of zero. */
422b1cb0 15533 TYPE_STUB (type) = 1;
685b1105
JK
15534 }
15535 else
15536 TYPE_STUB_SUPPORTED (type) = 1;
15537
dc718098 15538 if (die_is_declaration (die, cu))
876cecd0 15539 TYPE_STUB (type) = 1;
a6c727b2
DJ
15540 else if (attr == NULL && die->child == NULL
15541 && producer_is_realview (cu->producer))
15542 /* RealView does not output the required DW_AT_declaration
15543 on incomplete types. */
15544 TYPE_STUB (type) = 1;
dc718098 15545
c906108c
SS
15546 /* We need to add the type field to the die immediately so we don't
15547 infinitely recurse when dealing with pointers to the structure
0963b4bd 15548 type within the structure itself. */
1c379e20 15549 set_die_type (die, type, cu);
c906108c 15550
7e314c57
JK
15551 /* set_die_type should be already done. */
15552 set_descriptive_type (type, die, cu);
15553
c767944b
DJ
15554 return type;
15555}
15556
9c6a1327
TT
15557static void handle_struct_member_die
15558 (struct die_info *child_die,
15559 struct type *type,
15560 struct field_info *fi,
15561 std::vector<struct symbol *> *template_args,
15562 struct dwarf2_cu *cu);
15563
15564/* A helper for handle_struct_member_die that handles
15565 DW_TAG_variant_part. */
15566
15567static void
15568handle_variant_part (struct die_info *die, struct type *type,
15569 struct field_info *fi,
15570 std::vector<struct symbol *> *template_args,
15571 struct dwarf2_cu *cu)
15572{
15573 variant_part_builder *new_part;
15574 if (fi->current_variant_part == nullptr)
15575 {
15576 fi->variant_parts.emplace_back ();
15577 new_part = &fi->variant_parts.back ();
15578 }
15579 else if (!fi->current_variant_part->processing_variant)
15580 {
15581 complaint (_("nested DW_TAG_variant_part seen "
15582 "- DIE at %s [in module %s]"),
15583 sect_offset_str (die->sect_off),
5e22e966 15584 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15585 return;
15586 }
15587 else
15588 {
15589 variant_field &current = fi->current_variant_part->variants.back ();
15590 current.variant_parts.emplace_back ();
15591 new_part = &current.variant_parts.back ();
15592 }
15593
15594 /* When we recurse, we want callees to add to this new variant
15595 part. */
15596 scoped_restore save_current_variant_part
15597 = make_scoped_restore (&fi->current_variant_part, new_part);
15598
15599 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15600 if (discr == NULL)
15601 {
15602 /* It's a univariant form, an extension we support. */
15603 }
15604 else if (discr->form_is_ref ())
15605 {
15606 struct dwarf2_cu *target_cu = cu;
15607 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15608
15609 new_part->discriminant_offset = target_die->sect_off;
15610 }
15611 else
15612 {
15613 complaint (_("DW_AT_discr does not have DIE reference form"
15614 " - DIE at %s [in module %s]"),
15615 sect_offset_str (die->sect_off),
5e22e966 15616 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15617 }
15618
15619 for (die_info *child_die = die->child;
15620 child_die != NULL;
15621 child_die = child_die->sibling)
15622 handle_struct_member_die (child_die, type, fi, template_args, cu);
15623}
15624
15625/* A helper for handle_struct_member_die that handles
15626 DW_TAG_variant. */
15627
15628static void
15629handle_variant (struct die_info *die, struct type *type,
15630 struct field_info *fi,
15631 std::vector<struct symbol *> *template_args,
15632 struct dwarf2_cu *cu)
15633{
15634 if (fi->current_variant_part == nullptr)
15635 {
15636 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15637 "- DIE at %s [in module %s]"),
15638 sect_offset_str (die->sect_off),
5e22e966 15639 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15640 return;
15641 }
15642 if (fi->current_variant_part->processing_variant)
15643 {
15644 complaint (_("nested DW_TAG_variant seen "
15645 "- DIE at %s [in module %s]"),
15646 sect_offset_str (die->sect_off),
5e22e966 15647 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15648 return;
15649 }
15650
15651 scoped_restore save_processing_variant
15652 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15653 true);
15654
15655 fi->current_variant_part->variants.emplace_back ();
15656 variant_field &variant = fi->current_variant_part->variants.back ();
15657 variant.first_field = fi->fields.size ();
15658
15659 /* In a variant we want to get the discriminant and also add a
15660 field for our sole member child. */
15661 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15662 if (discr == nullptr)
15663 {
15664 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15665 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15666 variant.default_branch = true;
15667 else
15668 variant.discr_list_data = DW_BLOCK (discr);
15669 }
15670 else
15671 variant.discriminant_value = DW_UNSND (discr);
15672
15673 for (die_info *variant_child = die->child;
15674 variant_child != NULL;
15675 variant_child = variant_child->sibling)
15676 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15677
15678 variant.last_field = fi->fields.size ();
15679}
15680
2ddeaf8a
TT
15681/* A helper for process_structure_scope that handles a single member
15682 DIE. */
15683
15684static void
15685handle_struct_member_die (struct die_info *child_die, struct type *type,
15686 struct field_info *fi,
15687 std::vector<struct symbol *> *template_args,
15688 struct dwarf2_cu *cu)
15689{
15690 if (child_die->tag == DW_TAG_member
9c6a1327 15691 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15692 {
15693 /* NOTE: carlton/2002-11-05: A C++ static data member
15694 should be a DW_TAG_member that is a declaration, but
15695 all versions of G++ as of this writing (so through at
15696 least 3.2.1) incorrectly generate DW_TAG_variable
15697 tags for them instead. */
15698 dwarf2_add_field (fi, child_die, cu);
15699 }
15700 else if (child_die->tag == DW_TAG_subprogram)
15701 {
15702 /* Rust doesn't have member functions in the C++ sense.
15703 However, it does emit ordinary functions as children
15704 of a struct DIE. */
15705 if (cu->language == language_rust)
15706 read_func_scope (child_die, cu);
15707 else
15708 {
15709 /* C++ member function. */
15710 dwarf2_add_member_fn (fi, child_die, type, cu);
15711 }
15712 }
15713 else if (child_die->tag == DW_TAG_inheritance)
15714 {
15715 /* C++ base class field. */
15716 dwarf2_add_field (fi, child_die, cu);
15717 }
15718 else if (type_can_define_types (child_die))
15719 dwarf2_add_type_defn (fi, child_die, cu);
15720 else if (child_die->tag == DW_TAG_template_type_param
15721 || child_die->tag == DW_TAG_template_value_param)
15722 {
15723 struct symbol *arg = new_symbol (child_die, NULL, cu);
15724
15725 if (arg != NULL)
15726 template_args->push_back (arg);
15727 }
9c6a1327
TT
15728 else if (child_die->tag == DW_TAG_variant_part)
15729 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15730 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15731 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15732}
15733
c767944b
DJ
15734/* Finish creating a structure or union type, including filling in
15735 its members and creating a symbol for it. */
15736
15737static void
15738process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15739{
5e22e966 15740 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15741 struct die_info *child_die;
c767944b
DJ
15742 struct type *type;
15743
15744 type = get_die_type (die, cu);
15745 if (type == NULL)
15746 type = read_structure_type (die, cu);
15747
3e1d3d8c 15748 bool has_template_parameters = false;
e142c38c 15749 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15750 {
15751 struct field_info fi;
2f4732b0 15752 std::vector<struct symbol *> template_args;
c906108c 15753
639d11d3 15754 child_die = die->child;
c906108c
SS
15755
15756 while (child_die && child_die->tag)
15757 {
2ddeaf8a 15758 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15759 child_die = child_die->sibling;
c906108c
SS
15760 }
15761
34eaf542 15762 /* Attach template arguments to type. */
2f4732b0 15763 if (!template_args.empty ())
34eaf542 15764 {
3e1d3d8c 15765 has_template_parameters = true;
34eaf542 15766 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15767 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15768 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15769 = XOBNEWVEC (&objfile->objfile_obstack,
15770 struct symbol *,
15771 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15772 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15773 template_args.data (),
34eaf542
TT
15774 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15775 * sizeof (struct symbol *)));
34eaf542
TT
15776 }
15777
c906108c 15778 /* Attach fields and member functions to the type. */
317f7127 15779 if (fi.nfields () > 0)
e7c27a73 15780 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15781 if (!fi.fnfieldlists.empty ())
c906108c 15782 {
e7c27a73 15783 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15784
c5aa993b 15785 /* Get the type which refers to the base class (possibly this
c906108c 15786 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15787 class from the DW_AT_containing_type attribute. This use of
15788 DW_AT_containing_type is a GNU extension. */
c906108c 15789
e142c38c 15790 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15791 {
e7c27a73 15792 struct type *t = die_containing_type (die, cu);
c906108c 15793
ae6ae975 15794 set_type_vptr_basetype (type, t);
c906108c
SS
15795 if (type == t)
15796 {
c906108c
SS
15797 int i;
15798
15799 /* Our own class provides vtbl ptr. */
1f704f76 15800 for (i = t->num_fields () - 1;
c906108c
SS
15801 i >= TYPE_N_BASECLASSES (t);
15802 --i)
15803 {
0d5cff50 15804 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15805
1168df01 15806 if (is_vtable_name (fieldname, cu))
c906108c 15807 {
ae6ae975 15808 set_type_vptr_fieldno (type, i);
c906108c
SS
15809 break;
15810 }
15811 }
15812
15813 /* Complain if virtual function table field not found. */
15814 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15815 complaint (_("virtual function table pointer "
3e43a32a 15816 "not found when defining class '%s'"),
7d93a1e0 15817 type->name () ? type->name () : "");
c906108c
SS
15818 }
15819 else
15820 {
ae6ae975 15821 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15822 }
15823 }
f6235d4c 15824 else if (cu->producer
61012eef 15825 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15826 {
15827 /* The IBM XLC compiler does not provide direct indication
15828 of the containing type, but the vtable pointer is
15829 always named __vfp. */
15830
15831 int i;
15832
1f704f76 15833 for (i = type->num_fields () - 1;
f6235d4c
EZ
15834 i >= TYPE_N_BASECLASSES (type);
15835 --i)
15836 {
15837 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15838 {
ae6ae975
DE
15839 set_type_vptr_fieldno (type, i);
15840 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15841 break;
15842 }
15843 }
15844 }
c906108c 15845 }
98751a41
JK
15846
15847 /* Copy fi.typedef_field_list linked list elements content into the
15848 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15849 if (!fi.typedef_field_list.empty ())
98751a41 15850 {
be2daae6 15851 int count = fi.typedef_field_list.size ();
98751a41 15852
a0d7a4ff 15853 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15854 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15855 = ((struct decl_field *)
be2daae6
TT
15856 TYPE_ALLOC (type,
15857 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15858 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15859
be2daae6
TT
15860 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15861 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15862 }
c767944b 15863
883fd55a
KS
15864 /* Copy fi.nested_types_list linked list elements content into the
15865 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15866 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15867 {
be2daae6 15868 int count = fi.nested_types_list.size ();
883fd55a
KS
15869
15870 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15871 TYPE_NESTED_TYPES_ARRAY (type)
15872 = ((struct decl_field *)
be2daae6
TT
15873 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15874 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15875
be2daae6
TT
15876 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15877 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15878 }
c906108c 15879 }
63d06c5c 15880
bb5ed363 15881 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15882 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15883 cu->rust_unions.push_back (type);
0b92b5bb 15884
90aeadfc
DC
15885 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15886 snapshots) has been known to create a die giving a declaration
15887 for a class that has, as a child, a die giving a definition for a
15888 nested class. So we have to process our children even if the
15889 current die is a declaration. Normally, of course, a declaration
15890 won't have any children at all. */
134d01f1 15891
ca040673
DE
15892 child_die = die->child;
15893
90aeadfc
DC
15894 while (child_die != NULL && child_die->tag)
15895 {
15896 if (child_die->tag == DW_TAG_member
15897 || child_die->tag == DW_TAG_variable
34eaf542
TT
15898 || child_die->tag == DW_TAG_inheritance
15899 || child_die->tag == DW_TAG_template_value_param
15900 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15901 {
90aeadfc 15902 /* Do nothing. */
134d01f1 15903 }
90aeadfc
DC
15904 else
15905 process_die (child_die, cu);
134d01f1 15906
436c571c 15907 child_die = child_die->sibling;
134d01f1
DJ
15908 }
15909
fa4028e9
JB
15910 /* Do not consider external references. According to the DWARF standard,
15911 these DIEs are identified by the fact that they have no byte_size
15912 attribute, and a declaration attribute. */
15913 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15914 || !die_is_declaration (die, cu)
15915 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15916 {
15917 struct symbol *sym = new_symbol (die, type, cu);
15918
15919 if (has_template_parameters)
15920 {
a776957c
TT
15921 struct symtab *symtab;
15922 if (sym != nullptr)
15923 symtab = symbol_symtab (sym);
15924 else if (cu->line_header != nullptr)
15925 {
15926 /* Any related symtab will do. */
15927 symtab
7ba99d21 15928 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15929 }
15930 else
15931 {
15932 symtab = nullptr;
15933 complaint (_("could not find suitable "
15934 "symtab for template parameter"
15935 " - DIE at %s [in module %s]"),
15936 sect_offset_str (die->sect_off),
15937 objfile_name (objfile));
15938 }
15939
15940 if (symtab != nullptr)
15941 {
15942 /* Make sure that the symtab is set on the new symbols.
15943 Even though they don't appear in this symtab directly,
15944 other parts of gdb assume that symbols do, and this is
15945 reasonably true. */
15946 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15947 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15948 }
3e1d3d8c
TT
15949 }
15950 }
134d01f1
DJ
15951}
15952
ed6acedd
TT
15953/* Assuming DIE is an enumeration type, and TYPE is its associated
15954 type, update TYPE using some information only available in DIE's
15955 children. In particular, the fields are computed. */
55426c9d
JB
15956
15957static void
15958update_enumeration_type_from_children (struct die_info *die,
15959 struct type *type,
15960 struct dwarf2_cu *cu)
15961{
60f7655a 15962 struct die_info *child_die;
55426c9d
JB
15963 int unsigned_enum = 1;
15964 int flag_enum = 1;
55426c9d 15965
8268c778 15966 auto_obstack obstack;
ed6acedd 15967 std::vector<struct field> fields;
55426c9d 15968
60f7655a
DE
15969 for (child_die = die->child;
15970 child_die != NULL && child_die->tag;
436c571c 15971 child_die = child_die->sibling)
55426c9d
JB
15972 {
15973 struct attribute *attr;
15974 LONGEST value;
15975 const gdb_byte *bytes;
15976 struct dwarf2_locexpr_baton *baton;
15977 const char *name;
60f7655a 15978
55426c9d
JB
15979 if (child_die->tag != DW_TAG_enumerator)
15980 continue;
15981
15982 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15983 if (attr == NULL)
15984 continue;
15985
15986 name = dwarf2_name (child_die, cu);
15987 if (name == NULL)
15988 name = "<anonymous enumerator>";
15989
15990 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15991 &value, &bytes, &baton);
15992 if (value < 0)
15993 {
15994 unsigned_enum = 0;
15995 flag_enum = 0;
15996 }
55426c9d 15997 else
edd45eb0
SM
15998 {
15999 if (count_one_bits_ll (value) >= 2)
16000 flag_enum = 0;
edd45eb0 16001 }
55426c9d 16002
ed6acedd
TT
16003 fields.emplace_back ();
16004 struct field &field = fields.back ();
16005 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16006 SET_FIELD_ENUMVAL (field, value);
16007 }
16008
16009 if (!fields.empty ())
16010 {
5e33d5f4 16011 type->set_num_fields (fields.size ());
3cabb6b0
SM
16012 type->set_fields
16013 ((struct field *)
16014 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16015 memcpy (type->fields (), fields.data (),
ed6acedd 16016 sizeof (struct field) * fields.size ());
55426c9d
JB
16017 }
16018
16019 if (unsigned_enum)
16020 TYPE_UNSIGNED (type) = 1;
16021 if (flag_enum)
16022 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16023}
16024
134d01f1
DJ
16025/* Given a DW_AT_enumeration_type die, set its type. We do not
16026 complete the type's fields yet, or create any symbols. */
c906108c 16027
f792889a 16028static struct type *
134d01f1 16029read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16030{
5e22e966 16031 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16032 struct type *type;
c906108c 16033 struct attribute *attr;
0114d602 16034 const char *name;
134d01f1 16035
348e048f
DE
16036 /* If the definition of this type lives in .debug_types, read that type.
16037 Don't follow DW_AT_specification though, that will take us back up
16038 the chain and we want to go down. */
052c8bb8 16039 attr = die->attr (DW_AT_signature);
435d3d88 16040 if (attr != nullptr)
348e048f 16041 {
ac9ec31b 16042 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16043
ac9ec31b 16044 /* The type's CU may not be the same as CU.
02142a6c 16045 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16046 return set_die_type (die, type, cu);
16047 }
16048
c906108c
SS
16049 type = alloc_type (objfile);
16050
67607e24 16051 type->set_code (TYPE_CODE_ENUM);
94af9270 16052 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16053 if (name != NULL)
d0e39ea2 16054 type->set_name (name);
c906108c 16055
0626fc76
TT
16056 attr = dwarf2_attr (die, DW_AT_type, cu);
16057 if (attr != NULL)
16058 {
16059 struct type *underlying_type = die_type (die, cu);
16060
16061 TYPE_TARGET_TYPE (type) = underlying_type;
16062 }
16063
e142c38c 16064 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16065 if (attr != nullptr)
c906108c
SS
16066 {
16067 TYPE_LENGTH (type) = DW_UNSND (attr);
16068 }
16069 else
16070 {
16071 TYPE_LENGTH (type) = 0;
16072 }
16073
2b4424c3
TT
16074 maybe_set_alignment (cu, die, type);
16075
137033e9
JB
16076 /* The enumeration DIE can be incomplete. In Ada, any type can be
16077 declared as private in the package spec, and then defined only
16078 inside the package body. Such types are known as Taft Amendment
16079 Types. When another package uses such a type, an incomplete DIE
16080 may be generated by the compiler. */
02eb380e 16081 if (die_is_declaration (die, cu))
876cecd0 16082 TYPE_STUB (type) = 1;
02eb380e 16083
0626fc76
TT
16084 /* If this type has an underlying type that is not a stub, then we
16085 may use its attributes. We always use the "unsigned" attribute
16086 in this situation, because ordinarily we guess whether the type
16087 is unsigned -- but the guess can be wrong and the underlying type
16088 can tell us the reality. However, we defer to a local size
16089 attribute if one exists, because this lets the compiler override
16090 the underlying type if needed. */
16091 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16092 {
9e7c9a03
HD
16093 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16094 underlying_type = check_typedef (underlying_type);
16095 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16096 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16097 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16098 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16099 && TYPE_RAW_ALIGN (underlying_type) != 0)
16100 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16101 }
16102
3d567982
TT
16103 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16104
ed6acedd
TT
16105 set_die_type (die, type, cu);
16106
16107 /* Finish the creation of this type by using the enum's children.
16108 Note that, as usual, this must come after set_die_type to avoid
16109 infinite recursion when trying to compute the names of the
16110 enumerators. */
16111 update_enumeration_type_from_children (die, type, cu);
16112
16113 return type;
134d01f1
DJ
16114}
16115
16116/* Given a pointer to a die which begins an enumeration, process all
16117 the dies that define the members of the enumeration, and create the
16118 symbol for the enumeration type.
16119
16120 NOTE: We reverse the order of the element list. */
16121
16122static void
16123process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16124{
f792889a 16125 struct type *this_type;
134d01f1 16126
f792889a
DJ
16127 this_type = get_die_type (die, cu);
16128 if (this_type == NULL)
16129 this_type = read_enumeration_type (die, cu);
9dc481d3 16130
639d11d3 16131 if (die->child != NULL)
c906108c 16132 {
9dc481d3 16133 struct die_info *child_die;
15d034d0 16134 const char *name;
9dc481d3 16135
639d11d3 16136 child_die = die->child;
c906108c
SS
16137 while (child_die && child_die->tag)
16138 {
16139 if (child_die->tag != DW_TAG_enumerator)
16140 {
e7c27a73 16141 process_die (child_die, cu);
c906108c
SS
16142 }
16143 else
16144 {
39cbfefa
DJ
16145 name = dwarf2_name (child_die, cu);
16146 if (name)
ed6acedd 16147 new_symbol (child_die, this_type, cu);
c906108c
SS
16148 }
16149
436c571c 16150 child_die = child_die->sibling;
c906108c 16151 }
c906108c 16152 }
134d01f1 16153
6c83ed52
TT
16154 /* If we are reading an enum from a .debug_types unit, and the enum
16155 is a declaration, and the enum is not the signatured type in the
16156 unit, then we do not want to add a symbol for it. Adding a
16157 symbol would in some cases obscure the true definition of the
16158 enum, giving users an incomplete type when the definition is
16159 actually available. Note that we do not want to do this for all
16160 enums which are just declarations, because C++0x allows forward
16161 enum declarations. */
3019eac3 16162 if (cu->per_cu->is_debug_types
6c83ed52
TT
16163 && die_is_declaration (die, cu))
16164 {
52dc124a 16165 struct signatured_type *sig_type;
6c83ed52 16166
c0f78cd4 16167 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16168 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16169 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16170 return;
16171 }
16172
f792889a 16173 new_symbol (die, this_type, cu);
c906108c
SS
16174}
16175
16176/* Extract all information from a DW_TAG_array_type DIE and put it in
16177 the DIE's type field. For now, this only handles one dimensional
16178 arrays. */
16179
f792889a 16180static struct type *
e7c27a73 16181read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16182{
5e22e966 16183 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16184 struct die_info *child_die;
7e314c57 16185 struct type *type;
c906108c 16186 struct type *element_type, *range_type, *index_type;
c906108c 16187 struct attribute *attr;
15d034d0 16188 const char *name;
a405673c 16189 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16190 unsigned int bit_stride = 0;
c906108c 16191
e7c27a73 16192 element_type = die_type (die, cu);
c906108c 16193
7e314c57
JK
16194 /* The die_type call above may have already set the type for this DIE. */
16195 type = get_die_type (die, cu);
16196 if (type)
16197 return type;
16198
dc53a7ad
JB
16199 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16200 if (attr != NULL)
a405673c
JB
16201 {
16202 int stride_ok;
293e7e51 16203 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16204
16205 byte_stride_prop
16206 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16207 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16208 prop_type);
a405673c
JB
16209 if (!stride_ok)
16210 {
b98664d3 16211 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16212 " - DIE at %s [in module %s]"),
16213 sect_offset_str (die->sect_off),
5e22e966 16214 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16215 /* Ignore this attribute. We will likely not be able to print
16216 arrays of this type correctly, but there is little we can do
16217 to help if we cannot read the attribute's value. */
16218 byte_stride_prop = NULL;
16219 }
16220 }
dc53a7ad
JB
16221
16222 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16223 if (attr != NULL)
16224 bit_stride = DW_UNSND (attr);
16225
c906108c
SS
16226 /* Irix 6.2 native cc creates array types without children for
16227 arrays with unspecified length. */
639d11d3 16228 if (die->child == NULL)
c906108c 16229 {
46bf5051 16230 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16231 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16232 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16233 byte_stride_prop, bit_stride);
f792889a 16234 return set_die_type (die, type, cu);
c906108c
SS
16235 }
16236
791afaa2 16237 std::vector<struct type *> range_types;
639d11d3 16238 child_die = die->child;
c906108c
SS
16239 while (child_die && child_die->tag)
16240 {
16241 if (child_die->tag == DW_TAG_subrange_type)
16242 {
f792889a 16243 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16244
f792889a 16245 if (child_type != NULL)
a02abb62 16246 {
0963b4bd
MS
16247 /* The range type was succesfully read. Save it for the
16248 array type creation. */
791afaa2 16249 range_types.push_back (child_type);
a02abb62 16250 }
c906108c 16251 }
436c571c 16252 child_die = child_die->sibling;
c906108c
SS
16253 }
16254
16255 /* Dwarf2 dimensions are output from left to right, create the
16256 necessary array types in backwards order. */
7ca2d3a3 16257
c906108c 16258 type = element_type;
7ca2d3a3
DL
16259
16260 if (read_array_order (die, cu) == DW_ORD_col_major)
16261 {
16262 int i = 0;
9a619af0 16263
791afaa2 16264 while (i < range_types.size ())
dc53a7ad 16265 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16266 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16267 }
16268 else
16269 {
791afaa2 16270 size_t ndim = range_types.size ();
7ca2d3a3 16271 while (ndim-- > 0)
dc53a7ad 16272 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16273 byte_stride_prop, bit_stride);
7ca2d3a3 16274 }
c906108c 16275
f5f8a009
EZ
16276 /* Understand Dwarf2 support for vector types (like they occur on
16277 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16278 array type. This is not part of the Dwarf2/3 standard yet, but a
16279 custom vendor extension. The main difference between a regular
16280 array and the vector variant is that vectors are passed by value
16281 to functions. */
e142c38c 16282 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16283 if (attr != nullptr)
ea37ba09 16284 make_vector_type (type);
f5f8a009 16285
dbc98a8b
KW
16286 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16287 implementation may choose to implement triple vectors using this
16288 attribute. */
16289 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16290 if (attr != nullptr)
dbc98a8b
KW
16291 {
16292 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16293 TYPE_LENGTH (type) = DW_UNSND (attr);
16294 else
b98664d3 16295 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16296 "than the total size of elements"));
dbc98a8b
KW
16297 }
16298
39cbfefa
DJ
16299 name = dwarf2_name (die, cu);
16300 if (name)
d0e39ea2 16301 type->set_name (name);
6e70227d 16302
2b4424c3
TT
16303 maybe_set_alignment (cu, die, type);
16304
0963b4bd 16305 /* Install the type in the die. */
7e314c57
JK
16306 set_die_type (die, type, cu);
16307
16308 /* set_die_type should be already done. */
b4ba55a1
JB
16309 set_descriptive_type (type, die, cu);
16310
7e314c57 16311 return type;
c906108c
SS
16312}
16313
7ca2d3a3 16314static enum dwarf_array_dim_ordering
6e70227d 16315read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16316{
16317 struct attribute *attr;
16318
16319 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16320
435d3d88 16321 if (attr != nullptr)
aead7601 16322 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16323
0963b4bd
MS
16324 /* GNU F77 is a special case, as at 08/2004 array type info is the
16325 opposite order to the dwarf2 specification, but data is still
16326 laid out as per normal fortran.
7ca2d3a3 16327
0963b4bd
MS
16328 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16329 version checking. */
7ca2d3a3 16330
905e0470
PM
16331 if (cu->language == language_fortran
16332 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16333 {
16334 return DW_ORD_row_major;
16335 }
16336
6e70227d 16337 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16338 {
16339 case array_column_major:
16340 return DW_ORD_col_major;
16341 case array_row_major:
16342 default:
16343 return DW_ORD_row_major;
16344 };
16345}
16346
72019c9c 16347/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16348 the DIE's type field. */
72019c9c 16349
f792889a 16350static struct type *
72019c9c
GM
16351read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16352{
7e314c57
JK
16353 struct type *domain_type, *set_type;
16354 struct attribute *attr;
f792889a 16355
7e314c57
JK
16356 domain_type = die_type (die, cu);
16357
16358 /* The die_type call above may have already set the type for this DIE. */
16359 set_type = get_die_type (die, cu);
16360 if (set_type)
16361 return set_type;
16362
16363 set_type = create_set_type (NULL, domain_type);
16364
16365 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16366 if (attr != nullptr)
d09039dd 16367 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16368
2b4424c3
TT
16369 maybe_set_alignment (cu, die, set_type);
16370
f792889a 16371 return set_die_type (die, set_type, cu);
72019c9c 16372}
7ca2d3a3 16373
0971de02
TT
16374/* A helper for read_common_block that creates a locexpr baton.
16375 SYM is the symbol which we are marking as computed.
16376 COMMON_DIE is the DIE for the common block.
16377 COMMON_LOC is the location expression attribute for the common
16378 block itself.
16379 MEMBER_LOC is the location expression attribute for the particular
16380 member of the common block that we are processing.
16381 CU is the CU from which the above come. */
16382
16383static void
16384mark_common_block_symbol_computed (struct symbol *sym,
16385 struct die_info *common_die,
16386 struct attribute *common_loc,
16387 struct attribute *member_loc,
16388 struct dwarf2_cu *cu)
16389{
5e22e966 16390 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16391 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16392 struct dwarf2_locexpr_baton *baton;
16393 gdb_byte *ptr;
16394 unsigned int cu_off;
08feed99 16395 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16396 LONGEST offset = 0;
16397
16398 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16399 gdb_assert (common_loc->form_is_block ());
16400 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16401 || member_loc->form_is_constant ());
0971de02 16402
8d749320 16403 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16404 baton->per_objfile = per_objfile;
0971de02
TT
16405 baton->per_cu = cu->per_cu;
16406 gdb_assert (baton->per_cu);
16407
16408 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16409
cd6c91b4 16410 if (member_loc->form_is_constant ())
0971de02 16411 {
0826b30a 16412 offset = member_loc->constant_value (0);
0971de02
TT
16413 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16414 }
16415 else
16416 baton->size += DW_BLOCK (member_loc)->size;
16417
224c3ddb 16418 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16419 baton->data = ptr;
16420
16421 *ptr++ = DW_OP_call4;
9c541725 16422 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16423 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16424 ptr += 4;
16425
cd6c91b4 16426 if (member_loc->form_is_constant ())
0971de02
TT
16427 {
16428 *ptr++ = DW_OP_addr;
16429 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16430 ptr += cu->header.addr_size;
16431 }
16432 else
16433 {
16434 /* We have to copy the data here, because DW_OP_call4 will only
16435 use a DW_AT_location attribute. */
16436 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16437 ptr += DW_BLOCK (member_loc)->size;
16438 }
16439
16440 *ptr++ = DW_OP_plus;
16441 gdb_assert (ptr - baton->data == baton->size);
16442
0971de02 16443 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16444 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16445}
16446
4357ac6c
TT
16447/* Create appropriate locally-scoped variables for all the
16448 DW_TAG_common_block entries. Also create a struct common_block
16449 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16450 is used to separate the common blocks name namespace from regular
4357ac6c 16451 variable names. */
c906108c
SS
16452
16453static void
e7c27a73 16454read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16455{
0971de02
TT
16456 struct attribute *attr;
16457
16458 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16459 if (attr != nullptr)
0971de02
TT
16460 {
16461 /* Support the .debug_loc offsets. */
4fc6c0d5 16462 if (attr->form_is_block ())
0971de02
TT
16463 {
16464 /* Ok. */
16465 }
cd6c91b4 16466 else if (attr->form_is_section_offset ())
0971de02
TT
16467 {
16468 dwarf2_complex_location_expr_complaint ();
16469 attr = NULL;
16470 }
16471 else
16472 {
16473 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16474 "common block member");
16475 attr = NULL;
16476 }
16477 }
16478
639d11d3 16479 if (die->child != NULL)
c906108c 16480 {
5e22e966 16481 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16482 struct die_info *child_die;
16483 size_t n_entries = 0, size;
16484 struct common_block *common_block;
16485 struct symbol *sym;
74ac6d43 16486
4357ac6c
TT
16487 for (child_die = die->child;
16488 child_die && child_die->tag;
436c571c 16489 child_die = child_die->sibling)
4357ac6c
TT
16490 ++n_entries;
16491
16492 size = (sizeof (struct common_block)
16493 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16494 common_block
16495 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16496 size);
4357ac6c
TT
16497 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16498 common_block->n_entries = 0;
16499
16500 for (child_die = die->child;
16501 child_die && child_die->tag;
436c571c 16502 child_die = child_die->sibling)
4357ac6c
TT
16503 {
16504 /* Create the symbol in the DW_TAG_common_block block in the current
16505 symbol scope. */
e7c27a73 16506 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16507 if (sym != NULL)
16508 {
16509 struct attribute *member_loc;
16510
16511 common_block->contents[common_block->n_entries++] = sym;
16512
16513 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16514 cu);
16515 if (member_loc)
16516 {
16517 /* GDB has handled this for a long time, but it is
16518 not specified by DWARF. It seems to have been
16519 emitted by gfortran at least as recently as:
16520 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16521 complaint (_("Variable in common block has "
0971de02 16522 "DW_AT_data_member_location "
9d8780f0
SM
16523 "- DIE at %s [in module %s]"),
16524 sect_offset_str (child_die->sect_off),
518817b3 16525 objfile_name (objfile));
0971de02 16526
cd6c91b4 16527 if (member_loc->form_is_section_offset ())
0971de02 16528 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16529 else if (member_loc->form_is_constant ()
4fc6c0d5 16530 || member_loc->form_is_block ())
0971de02 16531 {
435d3d88 16532 if (attr != nullptr)
0971de02
TT
16533 mark_common_block_symbol_computed (sym, die, attr,
16534 member_loc, cu);
16535 }
16536 else
16537 dwarf2_complex_location_expr_complaint ();
16538 }
16539 }
c906108c 16540 }
4357ac6c
TT
16541
16542 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16543 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16544 }
16545}
16546
0114d602 16547/* Create a type for a C++ namespace. */
d9fa45fe 16548
0114d602
DJ
16549static struct type *
16550read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16551{
5e22e966 16552 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16553 const char *previous_prefix, *name;
9219021c 16554 int is_anonymous;
0114d602
DJ
16555 struct type *type;
16556
16557 /* For extensions, reuse the type of the original namespace. */
16558 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16559 {
16560 struct die_info *ext_die;
16561 struct dwarf2_cu *ext_cu = cu;
9a619af0 16562
0114d602
DJ
16563 ext_die = dwarf2_extension (die, &ext_cu);
16564 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16565
16566 /* EXT_CU may not be the same as CU.
02142a6c 16567 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16568 return set_die_type (die, type, cu);
16569 }
9219021c 16570
e142c38c 16571 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16572
16573 /* Now build the name of the current namespace. */
16574
0114d602
DJ
16575 previous_prefix = determine_prefix (die, cu);
16576 if (previous_prefix[0] != '\0')
16577 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16578 previous_prefix, name, 0, cu);
0114d602
DJ
16579
16580 /* Create the type. */
19f392bc 16581 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16582
60531b24 16583 return set_die_type (die, type, cu);
0114d602
DJ
16584}
16585
22cee43f 16586/* Read a namespace scope. */
0114d602
DJ
16587
16588static void
16589read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16590{
5e22e966 16591 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16592 int is_anonymous;
9219021c 16593
5c4e30ca
DC
16594 /* Add a symbol associated to this if we haven't seen the namespace
16595 before. Also, add a using directive if it's an anonymous
16596 namespace. */
9219021c 16597
f2f0e013 16598 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16599 {
16600 struct type *type;
16601
0114d602 16602 type = read_type_die (die, cu);
e7c27a73 16603 new_symbol (die, type, cu);
5c4e30ca 16604
e8e80198 16605 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16606 if (is_anonymous)
0114d602
DJ
16607 {
16608 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16609
eb1e02fd 16610 std::vector<const char *> excludes;
804d2729 16611 add_using_directive (using_directives (cu),
7d93a1e0 16612 previous_prefix, type->name (), NULL,
eb1e02fd 16613 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16614 }
5c4e30ca 16615 }
9219021c 16616
639d11d3 16617 if (die->child != NULL)
d9fa45fe 16618 {
639d11d3 16619 struct die_info *child_die = die->child;
6e70227d 16620
d9fa45fe
DC
16621 while (child_die && child_die->tag)
16622 {
e7c27a73 16623 process_die (child_die, cu);
436c571c 16624 child_die = child_die->sibling;
d9fa45fe
DC
16625 }
16626 }
38d518c9
EZ
16627}
16628
f55ee35c
JK
16629/* Read a Fortran module as type. This DIE can be only a declaration used for
16630 imported module. Still we need that type as local Fortran "use ... only"
16631 declaration imports depend on the created type in determine_prefix. */
16632
16633static struct type *
16634read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16635{
5e22e966 16636 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16637 const char *module_name;
f55ee35c
JK
16638 struct type *type;
16639
16640 module_name = dwarf2_name (die, cu);
19f392bc 16641 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16642
f55ee35c
JK
16643 return set_die_type (die, type, cu);
16644}
16645
5d7cb8df
JK
16646/* Read a Fortran module. */
16647
16648static void
16649read_module (struct die_info *die, struct dwarf2_cu *cu)
16650{
16651 struct die_info *child_die = die->child;
530e8392
KB
16652 struct type *type;
16653
16654 type = read_type_die (die, cu);
16655 new_symbol (die, type, cu);
5d7cb8df 16656
5d7cb8df
JK
16657 while (child_die && child_die->tag)
16658 {
16659 process_die (child_die, cu);
436c571c 16660 child_die = child_die->sibling;
5d7cb8df
JK
16661 }
16662}
16663
38d518c9
EZ
16664/* Return the name of the namespace represented by DIE. Set
16665 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16666 namespace. */
16667
16668static const char *
e142c38c 16669namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16670{
16671 struct die_info *current_die;
16672 const char *name = NULL;
16673
16674 /* Loop through the extensions until we find a name. */
16675
16676 for (current_die = die;
16677 current_die != NULL;
f2f0e013 16678 current_die = dwarf2_extension (die, &cu))
38d518c9 16679 {
96553a0c
DE
16680 /* We don't use dwarf2_name here so that we can detect the absence
16681 of a name -> anonymous namespace. */
7d45c7c3 16682 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16683
38d518c9
EZ
16684 if (name != NULL)
16685 break;
16686 }
16687
16688 /* Is it an anonymous namespace? */
16689
16690 *is_anonymous = (name == NULL);
16691 if (*is_anonymous)
2b1dbab0 16692 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16693
16694 return name;
d9fa45fe
DC
16695}
16696
c906108c
SS
16697/* Extract all information from a DW_TAG_pointer_type DIE and add to
16698 the user defined type vector. */
16699
f792889a 16700static struct type *
e7c27a73 16701read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16702{
5e22e966 16703 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16704 struct comp_unit_head *cu_header = &cu->header;
c906108c 16705 struct type *type;
8b2dbe47
KB
16706 struct attribute *attr_byte_size;
16707 struct attribute *attr_address_class;
16708 int byte_size, addr_class;
7e314c57
JK
16709 struct type *target_type;
16710
16711 target_type = die_type (die, cu);
c906108c 16712
7e314c57
JK
16713 /* The die_type call above may have already set the type for this DIE. */
16714 type = get_die_type (die, cu);
16715 if (type)
16716 return type;
16717
16718 type = lookup_pointer_type (target_type);
8b2dbe47 16719
e142c38c 16720 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16721 if (attr_byte_size)
16722 byte_size = DW_UNSND (attr_byte_size);
c906108c 16723 else
8b2dbe47
KB
16724 byte_size = cu_header->addr_size;
16725
e142c38c 16726 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16727 if (attr_address_class)
16728 addr_class = DW_UNSND (attr_address_class);
16729 else
16730 addr_class = DW_ADDR_none;
16731
2b4424c3
TT
16732 ULONGEST alignment = get_alignment (cu, die);
16733
16734 /* If the pointer size, alignment, or address class is different
16735 than the default, create a type variant marked as such and set
16736 the length accordingly. */
16737 if (TYPE_LENGTH (type) != byte_size
16738 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16739 && alignment != TYPE_RAW_ALIGN (type))
16740 || addr_class != DW_ADDR_none)
c906108c 16741 {
5e2b427d 16742 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16743 {
16744 int type_flags;
16745
849957d9 16746 type_flags = gdbarch_address_class_type_flags
5e2b427d 16747 (gdbarch, byte_size, addr_class);
876cecd0
TT
16748 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16749 == 0);
8b2dbe47
KB
16750 type = make_type_with_address_space (type, type_flags);
16751 }
16752 else if (TYPE_LENGTH (type) != byte_size)
16753 {
b98664d3 16754 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16755 }
2b4424c3
TT
16756 else if (TYPE_RAW_ALIGN (type) != alignment)
16757 {
b98664d3 16758 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16759 " - DIE at %s [in module %s]"),
16760 sect_offset_str (die->sect_off),
5e22e966 16761 objfile_name (cu->per_objfile->objfile));
2b4424c3 16762 }
6e70227d 16763 else
9a619af0
MS
16764 {
16765 /* Should we also complain about unhandled address classes? */
16766 }
c906108c 16767 }
8b2dbe47
KB
16768
16769 TYPE_LENGTH (type) = byte_size;
2b4424c3 16770 set_type_align (type, alignment);
f792889a 16771 return set_die_type (die, type, cu);
c906108c
SS
16772}
16773
16774/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16775 the user defined type vector. */
16776
f792889a 16777static struct type *
e7c27a73 16778read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16779{
16780 struct type *type;
16781 struct type *to_type;
16782 struct type *domain;
16783
e7c27a73
DJ
16784 to_type = die_type (die, cu);
16785 domain = die_containing_type (die, cu);
0d5de010 16786
7e314c57
JK
16787 /* The calls above may have already set the type for this DIE. */
16788 type = get_die_type (die, cu);
16789 if (type)
16790 return type;
16791
78134374 16792 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16793 type = lookup_methodptr_type (to_type);
78134374 16794 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16795 {
5e22e966 16796 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16797
16798 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16799 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16800 TYPE_VARARGS (to_type));
16801 type = lookup_methodptr_type (new_type);
16802 }
0d5de010
DJ
16803 else
16804 type = lookup_memberptr_type (to_type, domain);
c906108c 16805
f792889a 16806 return set_die_type (die, type, cu);
c906108c
SS
16807}
16808
4297a3f0 16809/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16810 the user defined type vector. */
16811
f792889a 16812static struct type *
4297a3f0
AV
16813read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16814 enum type_code refcode)
c906108c 16815{
e7c27a73 16816 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16817 struct type *type, *target_type;
c906108c
SS
16818 struct attribute *attr;
16819
4297a3f0
AV
16820 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16821
7e314c57
JK
16822 target_type = die_type (die, cu);
16823
16824 /* The die_type call above may have already set the type for this DIE. */
16825 type = get_die_type (die, cu);
16826 if (type)
16827 return type;
16828
4297a3f0 16829 type = lookup_reference_type (target_type, refcode);
e142c38c 16830 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16831 if (attr != nullptr)
c906108c
SS
16832 {
16833 TYPE_LENGTH (type) = DW_UNSND (attr);
16834 }
16835 else
16836 {
107d2387 16837 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16838 }
2b4424c3 16839 maybe_set_alignment (cu, die, type);
f792889a 16840 return set_die_type (die, type, cu);
c906108c
SS
16841}
16842
cf363f18
MW
16843/* Add the given cv-qualifiers to the element type of the array. GCC
16844 outputs DWARF type qualifiers that apply to an array, not the
16845 element type. But GDB relies on the array element type to carry
16846 the cv-qualifiers. This mimics section 6.7.3 of the C99
16847 specification. */
16848
16849static struct type *
16850add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16851 struct type *base_type, int cnst, int voltl)
16852{
16853 struct type *el_type, *inner_array;
16854
16855 base_type = copy_type (base_type);
16856 inner_array = base_type;
16857
78134374 16858 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16859 {
16860 TYPE_TARGET_TYPE (inner_array) =
16861 copy_type (TYPE_TARGET_TYPE (inner_array));
16862 inner_array = TYPE_TARGET_TYPE (inner_array);
16863 }
16864
16865 el_type = TYPE_TARGET_TYPE (inner_array);
16866 cnst |= TYPE_CONST (el_type);
16867 voltl |= TYPE_VOLATILE (el_type);
16868 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16869
16870 return set_die_type (die, base_type, cu);
16871}
16872
f792889a 16873static struct type *
e7c27a73 16874read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16875{
f792889a 16876 struct type *base_type, *cv_type;
c906108c 16877
e7c27a73 16878 base_type = die_type (die, cu);
7e314c57
JK
16879
16880 /* The die_type call above may have already set the type for this DIE. */
16881 cv_type = get_die_type (die, cu);
16882 if (cv_type)
16883 return cv_type;
16884
2f608a3a
KW
16885 /* In case the const qualifier is applied to an array type, the element type
16886 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16887 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16888 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16889
f792889a
DJ
16890 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16891 return set_die_type (die, cv_type, cu);
c906108c
SS
16892}
16893
f792889a 16894static struct type *
e7c27a73 16895read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16896{
f792889a 16897 struct type *base_type, *cv_type;
c906108c 16898
e7c27a73 16899 base_type = die_type (die, cu);
7e314c57
JK
16900
16901 /* The die_type call above may have already set the type for this DIE. */
16902 cv_type = get_die_type (die, cu);
16903 if (cv_type)
16904 return cv_type;
16905
cf363f18
MW
16906 /* In case the volatile qualifier is applied to an array type, the
16907 element type is so qualified, not the array type (section 6.7.3
16908 of C99). */
78134374 16909 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16910 return add_array_cv_type (die, cu, base_type, 0, 1);
16911
f792889a
DJ
16912 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16913 return set_die_type (die, cv_type, cu);
c906108c
SS
16914}
16915
06d66ee9
TT
16916/* Handle DW_TAG_restrict_type. */
16917
16918static struct type *
16919read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16920{
16921 struct type *base_type, *cv_type;
16922
16923 base_type = die_type (die, cu);
16924
16925 /* The die_type call above may have already set the type for this DIE. */
16926 cv_type = get_die_type (die, cu);
16927 if (cv_type)
16928 return cv_type;
16929
16930 cv_type = make_restrict_type (base_type);
16931 return set_die_type (die, cv_type, cu);
16932}
16933
a2c2acaf
MW
16934/* Handle DW_TAG_atomic_type. */
16935
16936static struct type *
16937read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16938{
16939 struct type *base_type, *cv_type;
16940
16941 base_type = die_type (die, cu);
16942
16943 /* The die_type call above may have already set the type for this DIE. */
16944 cv_type = get_die_type (die, cu);
16945 if (cv_type)
16946 return cv_type;
16947
16948 cv_type = make_atomic_type (base_type);
16949 return set_die_type (die, cv_type, cu);
16950}
16951
c906108c
SS
16952/* Extract all information from a DW_TAG_string_type DIE and add to
16953 the user defined type vector. It isn't really a user defined type,
16954 but it behaves like one, with other DIE's using an AT_user_def_type
16955 attribute to reference it. */
16956
f792889a 16957static struct type *
e7c27a73 16958read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16959{
5e22e966 16960 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 16961 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16962 struct type *type, *range_type, *index_type, *char_type;
16963 struct attribute *attr;
216a7e6b
AB
16964 struct dynamic_prop prop;
16965 bool length_is_constant = true;
16966 LONGEST length;
16967
16968 /* There are a couple of places where bit sizes might be made use of
16969 when parsing a DW_TAG_string_type, however, no producer that we know
16970 of make use of these. Handling bit sizes that are a multiple of the
16971 byte size is easy enough, but what about other bit sizes? Lets deal
16972 with that problem when we have to. Warn about these attributes being
16973 unsupported, then parse the type and ignore them like we always
16974 have. */
16975 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16976 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16977 {
16978 static bool warning_printed = false;
16979 if (!warning_printed)
16980 {
16981 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16982 "currently supported on DW_TAG_string_type."));
16983 warning_printed = true;
16984 }
16985 }
c906108c 16986
e142c38c 16987 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16988 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16989 {
16990 /* The string length describes the location at which the length of
16991 the string can be found. The size of the length field can be
16992 specified with one of the attributes below. */
16993 struct type *prop_type;
16994 struct attribute *len
16995 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16996 if (len == nullptr)
16997 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16998 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16999 {
17000 /* Pass 0 as the default as we know this attribute is constant
17001 and the default value will not be returned. */
0826b30a 17002 LONGEST sz = len->constant_value (0);
293e7e51 17003 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17004 }
17005 else
17006 {
17007 /* If the size is not specified then we assume it is the size of
17008 an address on this target. */
293e7e51 17009 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17010 }
17011
17012 /* Convert the attribute into a dynamic property. */
17013 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17014 length = 1;
17015 else
17016 length_is_constant = false;
17017 }
17018 else if (attr != nullptr)
17019 {
17020 /* This DW_AT_string_length just contains the length with no
17021 indirection. There's no need to create a dynamic property in this
17022 case. Pass 0 for the default value as we know it will not be
17023 returned in this case. */
0826b30a 17024 length = attr->constant_value (0);
216a7e6b
AB
17025 }
17026 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17027 {
216a7e6b 17028 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17029 length = attr->constant_value (1);
c906108c
SS
17030 }
17031 else
17032 {
216a7e6b
AB
17033 /* Use 1 as a fallback length if we have nothing else. */
17034 length = 1;
c906108c 17035 }
6ccb9162 17036
46bf5051 17037 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17038 if (length_is_constant)
17039 range_type = create_static_range_type (NULL, index_type, 1, length);
17040 else
17041 {
17042 struct dynamic_prop low_bound;
17043
17044 low_bound.kind = PROP_CONST;
17045 low_bound.data.const_val = 1;
17046 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17047 }
3b7538c0
UW
17048 char_type = language_string_char_type (cu->language_defn, gdbarch);
17049 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17050
f792889a 17051 return set_die_type (die, type, cu);
c906108c
SS
17052}
17053
4d804846
JB
17054/* Assuming that DIE corresponds to a function, returns nonzero
17055 if the function is prototyped. */
17056
17057static int
17058prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17059{
17060 struct attribute *attr;
17061
17062 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17063 if (attr && (DW_UNSND (attr) != 0))
17064 return 1;
17065
17066 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17067 is only meaningful for C, but the concept also extends to other
4d804846
JB
17068 languages that allow unprototyped functions (Eg: Objective C).
17069 For all other languages, assume that functions are always
17070 prototyped. */
17071 if (cu->language != language_c
17072 && cu->language != language_objc
17073 && cu->language != language_opencl)
17074 return 1;
17075
17076 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17077 prototyped and unprototyped functions; default to prototyped,
17078 since that is more common in modern code (and RealView warns
17079 about unprototyped functions). */
17080 if (producer_is_realview (cu->producer))
17081 return 1;
17082
17083 return 0;
17084}
17085
c906108c
SS
17086/* Handle DIES due to C code like:
17087
17088 struct foo
c5aa993b
JM
17089 {
17090 int (*funcp)(int a, long l);
17091 int b;
17092 };
c906108c 17093
0963b4bd 17094 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17095
f792889a 17096static struct type *
e7c27a73 17097read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17098{
5e22e966 17099 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17100 struct type *type; /* Type that this function returns. */
17101 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17102 struct attribute *attr;
17103
e7c27a73 17104 type = die_type (die, cu);
7e314c57
JK
17105
17106 /* The die_type call above may have already set the type for this DIE. */
17107 ftype = get_die_type (die, cu);
17108 if (ftype)
17109 return ftype;
17110
0c8b41f1 17111 ftype = lookup_function_type (type);
c906108c 17112
4d804846 17113 if (prototyped_function_p (die, cu))
a6c727b2 17114 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17115
c055b101
CV
17116 /* Store the calling convention in the type if it's available in
17117 the subroutine die. Otherwise set the calling convention to
17118 the default value DW_CC_normal. */
17119 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17120 if (attr != nullptr
17121 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17122 TYPE_CALLING_CONVENTION (ftype)
17123 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17124 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17125 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17126 else
17127 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17128
743649fd
MW
17129 /* Record whether the function returns normally to its caller or not
17130 if the DWARF producer set that information. */
17131 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17132 if (attr && (DW_UNSND (attr) != 0))
17133 TYPE_NO_RETURN (ftype) = 1;
17134
76c10ea2
GM
17135 /* We need to add the subroutine type to the die immediately so
17136 we don't infinitely recurse when dealing with parameters
0963b4bd 17137 declared as the same subroutine type. */
76c10ea2 17138 set_die_type (die, ftype, cu);
6e70227d 17139
639d11d3 17140 if (die->child != NULL)
c906108c 17141 {
bb5ed363 17142 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17143 struct die_info *child_die;
8072405b 17144 int nparams, iparams;
c906108c
SS
17145
17146 /* Count the number of parameters.
17147 FIXME: GDB currently ignores vararg functions, but knows about
17148 vararg member functions. */
8072405b 17149 nparams = 0;
639d11d3 17150 child_die = die->child;
c906108c
SS
17151 while (child_die && child_die->tag)
17152 {
17153 if (child_die->tag == DW_TAG_formal_parameter)
17154 nparams++;
17155 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17156 TYPE_VARARGS (ftype) = 1;
436c571c 17157 child_die = child_die->sibling;
c906108c
SS
17158 }
17159
17160 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17161 ftype->set_num_fields (nparams);
3cabb6b0
SM
17162 ftype->set_fields
17163 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17164
8072405b
JK
17165 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17166 even if we error out during the parameters reading below. */
17167 for (iparams = 0; iparams < nparams; iparams++)
17168 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17169
17170 iparams = 0;
639d11d3 17171 child_die = die->child;
c906108c
SS
17172 while (child_die && child_die->tag)
17173 {
17174 if (child_die->tag == DW_TAG_formal_parameter)
17175 {
3ce3b1ba
PA
17176 struct type *arg_type;
17177
17178 /* DWARF version 2 has no clean way to discern C++
17179 static and non-static member functions. G++ helps
17180 GDB by marking the first parameter for non-static
17181 member functions (which is the this pointer) as
17182 artificial. We pass this information to
17183 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17184
17185 DWARF version 3 added DW_AT_object_pointer, which GCC
17186 4.5 does not yet generate. */
e142c38c 17187 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17188 if (attr != nullptr)
c906108c
SS
17189 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17190 else
9c37b5ae 17191 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17192 arg_type = die_type (child_die, cu);
17193
17194 /* RealView does not mark THIS as const, which the testsuite
17195 expects. GCC marks THIS as const in method definitions,
17196 but not in the class specifications (GCC PR 43053). */
17197 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17198 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17199 {
17200 int is_this = 0;
17201 struct dwarf2_cu *arg_cu = cu;
17202 const char *name = dwarf2_name (child_die, cu);
17203
17204 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17205 if (attr != nullptr)
3ce3b1ba
PA
17206 {
17207 /* If the compiler emits this, use it. */
17208 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17209 is_this = 1;
17210 }
17211 else if (name && strcmp (name, "this") == 0)
17212 /* Function definitions will have the argument names. */
17213 is_this = 1;
17214 else if (name == NULL && iparams == 0)
17215 /* Declarations may not have the names, so like
17216 elsewhere in GDB, assume an artificial first
17217 argument is "this". */
17218 is_this = 1;
17219
17220 if (is_this)
17221 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17222 arg_type, 0);
17223 }
17224
17225 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17226 iparams++;
17227 }
436c571c 17228 child_die = child_die->sibling;
c906108c
SS
17229 }
17230 }
17231
76c10ea2 17232 return ftype;
c906108c
SS
17233}
17234
f792889a 17235static struct type *
e7c27a73 17236read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17237{
5e22e966 17238 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17239 const char *name = NULL;
3c8e0968 17240 struct type *this_type, *target_type;
c906108c 17241
94af9270 17242 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17243 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17244 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17245 set_die_type (die, this_type, cu);
3c8e0968
DE
17246 target_type = die_type (die, cu);
17247 if (target_type != this_type)
17248 TYPE_TARGET_TYPE (this_type) = target_type;
17249 else
17250 {
17251 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17252 spec and cause infinite loops in GDB. */
b98664d3 17253 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17254 "- DIE at %s [in module %s]"),
17255 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17256 TYPE_TARGET_TYPE (this_type) = NULL;
17257 }
e4003a34
TV
17258 if (name == NULL)
17259 {
17260 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17261 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17262 Handle these by just returning the target type, rather than
17263 constructing an anonymous typedef type and trying to handle this
17264 elsewhere. */
17265 set_die_type (die, target_type, cu);
17266 return target_type;
17267 }
f792889a 17268 return this_type;
c906108c
SS
17269}
17270
9b790ce7
UW
17271/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17272 (which may be different from NAME) to the architecture back-end to allow
17273 it to guess the correct format if necessary. */
17274
17275static struct type *
17276dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17277 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17278{
08feed99 17279 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17280 const struct floatformat **format;
17281 struct type *type;
17282
17283 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17284 if (format)
103a685e 17285 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17286 else
77b7c781 17287 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17288
17289 return type;
17290}
17291
eb77c9df
AB
17292/* Allocate an integer type of size BITS and name NAME. */
17293
17294static struct type *
17295dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17296 int bits, int unsigned_p, const char *name)
17297{
17298 struct type *type;
17299
17300 /* Versions of Intel's C Compiler generate an integer type called "void"
17301 instead of using DW_TAG_unspecified_type. This has been seen on
17302 at least versions 14, 17, and 18. */
35ee2dc2
AB
17303 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17304 && strcmp (name, "void") == 0)
eb77c9df
AB
17305 type = objfile_type (objfile)->builtin_void;
17306 else
17307 type = init_integer_type (objfile, bits, unsigned_p, name);
17308
17309 return type;
17310}
17311
8bdc1658
AB
17312/* Initialise and return a floating point type of size BITS suitable for
17313 use as a component of a complex number. The NAME_HINT is passed through
17314 when initialising the floating point type and is the name of the complex
17315 type.
17316
17317 As DWARF doesn't currently provide an explicit name for the components
17318 of a complex number, but it can be helpful to have these components
17319 named, we try to select a suitable name based on the size of the
17320 component. */
17321static struct type *
17322dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17323 struct objfile *objfile,
103a685e
TT
17324 int bits, const char *name_hint,
17325 enum bfd_endian byte_order)
8bdc1658 17326{
08feed99 17327 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17328 struct type *tt = nullptr;
17329
35add35e
AB
17330 /* Try to find a suitable floating point builtin type of size BITS.
17331 We're going to use the name of this type as the name for the complex
17332 target type that we are about to create. */
1db455a7 17333 switch (cu->language)
8bdc1658 17334 {
1db455a7
AB
17335 case language_fortran:
17336 switch (bits)
17337 {
17338 case 32:
17339 tt = builtin_f_type (gdbarch)->builtin_real;
17340 break;
17341 case 64:
17342 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17343 break;
17344 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17345 case 128:
17346 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17347 break;
17348 }
8bdc1658 17349 break;
1db455a7
AB
17350 default:
17351 switch (bits)
17352 {
17353 case 32:
17354 tt = builtin_type (gdbarch)->builtin_float;
17355 break;
17356 case 64:
17357 tt = builtin_type (gdbarch)->builtin_double;
17358 break;
17359 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17360 case 128:
17361 tt = builtin_type (gdbarch)->builtin_long_double;
17362 break;
17363 }
8bdc1658
AB
17364 break;
17365 }
17366
35add35e
AB
17367 /* If the type we found doesn't match the size we were looking for, then
17368 pretend we didn't find a type at all, the complex target type we
17369 create will then be nameless. */
a12e5744 17370 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17371 tt = nullptr;
17372
7d93a1e0 17373 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17374 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17375}
17376
c906108c
SS
17377/* Find a representation of a given base type and install
17378 it in the TYPE field of the die. */
17379
f792889a 17380static struct type *
e7c27a73 17381read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17382{
5e22e966 17383 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17384 struct type *type;
17385 struct attribute *attr;
19f392bc 17386 int encoding = 0, bits = 0;
15d034d0 17387 const char *name;
34877895 17388 gdbarch *arch;
c906108c 17389
e142c38c 17390 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17391 if (attr != nullptr)
34877895 17392 encoding = DW_UNSND (attr);
e142c38c 17393 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17394 if (attr != nullptr)
34877895 17395 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17396 name = dwarf2_name (die, cu);
6ccb9162 17397 if (!name)
34877895 17398 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17399
08feed99 17400 arch = objfile->arch ();
103a685e
TT
17401 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17402
34877895
PJ
17403 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17404 if (attr)
103a685e
TT
17405 {
17406 int endianity = DW_UNSND (attr);
17407
17408 switch (endianity)
17409 {
17410 case DW_END_big:
17411 byte_order = BFD_ENDIAN_BIG;
17412 break;
17413 case DW_END_little:
17414 byte_order = BFD_ENDIAN_LITTLE;
17415 break;
17416 default:
17417 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17418 break;
17419 }
17420 }
6ccb9162
UW
17421
17422 switch (encoding)
c906108c 17423 {
6ccb9162
UW
17424 case DW_ATE_address:
17425 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17426 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17427 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17428 break;
17429 case DW_ATE_boolean:
19f392bc 17430 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17431 break;
17432 case DW_ATE_complex_float:
103a685e
TT
17433 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17434 byte_order);
78134374 17435 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17436 {
17437 if (name == nullptr)
17438 {
17439 struct obstack *obstack
5e22e966 17440 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17441 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17442 nullptr);
17443 }
17444 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17445 }
17446 else
17447 type = init_complex_type (name, type);
6ccb9162
UW
17448 break;
17449 case DW_ATE_decimal_float:
19f392bc 17450 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17451 break;
17452 case DW_ATE_float:
103a685e 17453 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17454 break;
17455 case DW_ATE_signed:
eb77c9df 17456 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17457 break;
17458 case DW_ATE_unsigned:
3b2b8fea
TT
17459 if (cu->language == language_fortran
17460 && name
61012eef 17461 && startswith (name, "character("))
19f392bc
UW
17462 type = init_character_type (objfile, bits, 1, name);
17463 else
eb77c9df 17464 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17465 break;
17466 case DW_ATE_signed_char:
6e70227d 17467 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17468 || cu->language == language_pascal
17469 || cu->language == language_fortran)
19f392bc
UW
17470 type = init_character_type (objfile, bits, 0, name);
17471 else
eb77c9df 17472 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17473 break;
17474 case DW_ATE_unsigned_char:
868a0084 17475 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17476 || cu->language == language_pascal
c44af4eb
TT
17477 || cu->language == language_fortran
17478 || cu->language == language_rust)
19f392bc
UW
17479 type = init_character_type (objfile, bits, 1, name);
17480 else
eb77c9df 17481 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17482 break;
75079b2b 17483 case DW_ATE_UTF:
53e710ac 17484 {
53e710ac
PA
17485 if (bits == 16)
17486 type = builtin_type (arch)->builtin_char16;
17487 else if (bits == 32)
17488 type = builtin_type (arch)->builtin_char32;
17489 else
17490 {
b98664d3 17491 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17492 bits);
eb77c9df 17493 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17494 }
17495 return set_die_type (die, type, cu);
17496 }
75079b2b
TT
17497 break;
17498
6ccb9162 17499 default:
b98664d3 17500 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17501 dwarf_type_encoding_name (encoding));
77b7c781 17502 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17503 break;
c906108c 17504 }
6ccb9162 17505
0114d602 17506 if (name && strcmp (name, "char") == 0)
876cecd0 17507 TYPE_NOSIGN (type) = 1;
0114d602 17508
2b4424c3
TT
17509 maybe_set_alignment (cu, die, type);
17510
103a685e 17511 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17512
f792889a 17513 return set_die_type (die, type, cu);
c906108c
SS
17514}
17515
80180f79
SA
17516/* Parse dwarf attribute if it's a block, reference or constant and put the
17517 resulting value of the attribute into struct bound_prop.
17518 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17519
17520static int
17521attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17522 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17523 struct type *default_type)
80180f79
SA
17524{
17525 struct dwarf2_property_baton *baton;
5e22e966 17526 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17527 struct objfile *objfile = per_objfile->objfile;
17528 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17529
9a49df9d
AB
17530 gdb_assert (default_type != NULL);
17531
80180f79
SA
17532 if (attr == NULL || prop == NULL)
17533 return 0;
17534
4fc6c0d5 17535 if (attr->form_is_block ())
80180f79 17536 {
8d749320 17537 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17538 baton->property_type = default_type;
80180f79 17539 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17540 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17541 baton->locexpr.size = DW_BLOCK (attr)->size;
17542 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17543 switch (attr->name)
17544 {
17545 case DW_AT_string_length:
17546 baton->locexpr.is_reference = true;
17547 break;
17548 default:
17549 baton->locexpr.is_reference = false;
17550 break;
17551 }
80180f79
SA
17552 prop->data.baton = baton;
17553 prop->kind = PROP_LOCEXPR;
17554 gdb_assert (prop->data.baton != NULL);
17555 }
cd6c91b4 17556 else if (attr->form_is_ref ())
80180f79
SA
17557 {
17558 struct dwarf2_cu *target_cu = cu;
17559 struct die_info *target_die;
17560 struct attribute *target_attr;
17561
17562 target_die = follow_die_ref (die, attr, &target_cu);
17563 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17564 if (target_attr == NULL)
17565 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17566 target_cu);
80180f79
SA
17567 if (target_attr == NULL)
17568 return 0;
17569
df25ebbd 17570 switch (target_attr->name)
80180f79 17571 {
df25ebbd 17572 case DW_AT_location:
cd6c91b4 17573 if (target_attr->form_is_section_offset ())
df25ebbd 17574 {
8d749320 17575 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17576 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17577 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17578 prop->data.baton = baton;
17579 prop->kind = PROP_LOCLIST;
17580 gdb_assert (prop->data.baton != NULL);
17581 }
4fc6c0d5 17582 else if (target_attr->form_is_block ())
df25ebbd 17583 {
8d749320 17584 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17585 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17586 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17587 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17588 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17589 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17590 baton->locexpr.is_reference = true;
df25ebbd
JB
17591 prop->data.baton = baton;
17592 prop->kind = PROP_LOCEXPR;
17593 gdb_assert (prop->data.baton != NULL);
17594 }
17595 else
17596 {
17597 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17598 "dynamic property");
17599 return 0;
17600 }
17601 break;
17602 case DW_AT_data_member_location:
17603 {
17604 LONGEST offset;
17605
17606 if (!handle_data_member_location (target_die, target_cu,
17607 &offset))
17608 return 0;
17609
8d749320 17610 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17611 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17612 target_cu);
df25ebbd
JB
17613 baton->offset_info.offset = offset;
17614 baton->offset_info.type = die_type (target_die, target_cu);
17615 prop->data.baton = baton;
17616 prop->kind = PROP_ADDR_OFFSET;
17617 break;
17618 }
80180f79
SA
17619 }
17620 }
cd6c91b4 17621 else if (attr->form_is_constant ())
80180f79 17622 {
0826b30a 17623 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17624 prop->kind = PROP_CONST;
17625 }
17626 else
17627 {
17628 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17629 dwarf2_name (die, cu));
17630 return 0;
17631 }
17632
17633 return 1;
17634}
17635
09ba997f 17636/* See read.h. */
9a49df9d 17637
09ba997f 17638struct type *
293e7e51 17639dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17640{
9a49df9d
AB
17641 struct type *int_type;
17642
17643 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17644#define TRY_TYPE(F) \
17645 int_type = (unsigned_p \
17646 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17647 : objfile_type (objfile)->builtin_ ## F); \
17648 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17649 return int_type
17650
17651 TRY_TYPE (char);
17652 TRY_TYPE (short);
17653 TRY_TYPE (int);
17654 TRY_TYPE (long);
17655 TRY_TYPE (long_long);
17656
17657#undef TRY_TYPE
17658
17659 gdb_assert_not_reached ("unable to find suitable integer type");
17660}
17661
09ba997f 17662/* See read.h. */
11a8b164 17663
09ba997f 17664struct type *
293e7e51 17665dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17666{
293e7e51
SM
17667 int addr_size = this->per_cu->addr_size ();
17668 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17669}
17670
b86352cf
AB
17671/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17672 present (which is valid) then compute the default type based on the
17673 compilation units address size. */
17674
17675static struct type *
17676read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17677{
17678 struct type *index_type = die_type (die, cu);
17679
17680 /* Dwarf-2 specifications explicitly allows to create subrange types
17681 without specifying a base type.
17682 In that case, the base type must be set to the type of
17683 the lower bound, upper bound or count, in that order, if any of these
17684 three attributes references an object that has a type.
17685 If no base type is found, the Dwarf-2 specifications say that
17686 a signed integer type of size equal to the size of an address should
17687 be used.
17688 For the following C code: `extern char gdb_int [];'
17689 GCC produces an empty range DIE.
17690 FIXME: muller/2010-05-28: Possible references to object for low bound,
17691 high bound or count are not yet handled by this code. */
78134374 17692 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17693 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17694
17695 return index_type;
17696}
17697
a02abb62
JB
17698/* Read the given DW_AT_subrange DIE. */
17699
f792889a 17700static struct type *
a02abb62
JB
17701read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17702{
4c9ad8c2 17703 struct type *base_type, *orig_base_type;
a02abb62
JB
17704 struct type *range_type;
17705 struct attribute *attr;
729efb13 17706 struct dynamic_prop low, high;
4fae6e18 17707 int low_default_is_valid;
c451ebe5 17708 int high_bound_is_count = 0;
15d034d0 17709 const char *name;
d359392f 17710 ULONGEST negative_mask;
e77813c8 17711
b86352cf
AB
17712 orig_base_type = read_subrange_index_type (die, cu);
17713
4c9ad8c2
TT
17714 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17715 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17716 creating the range type, but we use the result of check_typedef
17717 when examining properties of the type. */
17718 base_type = check_typedef (orig_base_type);
a02abb62 17719
7e314c57
JK
17720 /* The die_type call above may have already set the type for this DIE. */
17721 range_type = get_die_type (die, cu);
17722 if (range_type)
17723 return range_type;
17724
729efb13
SA
17725 low.kind = PROP_CONST;
17726 high.kind = PROP_CONST;
17727 high.data.const_val = 0;
17728
4fae6e18
JK
17729 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17730 omitting DW_AT_lower_bound. */
17731 switch (cu->language)
6e70227d 17732 {
4fae6e18
JK
17733 case language_c:
17734 case language_cplus:
729efb13 17735 low.data.const_val = 0;
4fae6e18
JK
17736 low_default_is_valid = 1;
17737 break;
17738 case language_fortran:
729efb13 17739 low.data.const_val = 1;
4fae6e18
JK
17740 low_default_is_valid = 1;
17741 break;
17742 case language_d:
4fae6e18 17743 case language_objc:
c44af4eb 17744 case language_rust:
729efb13 17745 low.data.const_val = 0;
4fae6e18
JK
17746 low_default_is_valid = (cu->header.version >= 4);
17747 break;
17748 case language_ada:
17749 case language_m2:
17750 case language_pascal:
729efb13 17751 low.data.const_val = 1;
4fae6e18
JK
17752 low_default_is_valid = (cu->header.version >= 4);
17753 break;
17754 default:
729efb13 17755 low.data.const_val = 0;
4fae6e18
JK
17756 low_default_is_valid = 0;
17757 break;
a02abb62
JB
17758 }
17759
e142c38c 17760 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17761 if (attr != nullptr)
9a49df9d 17762 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17763 else if (!low_default_is_valid)
b98664d3 17764 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17765 "- DIE at %s [in module %s]"),
17766 sect_offset_str (die->sect_off),
5e22e966 17767 objfile_name (cu->per_objfile->objfile));
a02abb62 17768
506f5c41
TV
17769 struct attribute *attr_ub, *attr_count;
17770 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17771 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17772 {
506f5c41 17773 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17774 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17775 {
c451ebe5
SA
17776 /* If bounds are constant do the final calculation here. */
17777 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17778 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17779 else
17780 high_bound_is_count = 1;
c2ff108b 17781 }
506f5c41
TV
17782 else
17783 {
17784 if (attr_ub != NULL)
17785 complaint (_("Unresolved DW_AT_upper_bound "
17786 "- DIE at %s [in module %s]"),
17787 sect_offset_str (die->sect_off),
5e22e966 17788 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17789 if (attr_count != NULL)
17790 complaint (_("Unresolved DW_AT_count "
17791 "- DIE at %s [in module %s]"),
17792 sect_offset_str (die->sect_off),
5e22e966 17793 objfile_name (cu->per_objfile->objfile));
506f5c41 17794 }
e77813c8 17795 }
a02abb62 17796
4e962e74
TT
17797 LONGEST bias = 0;
17798 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17799 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17800 bias = bias_attr->constant_value (0);
4e962e74 17801
dbb9c2b1
JB
17802 /* Normally, the DWARF producers are expected to use a signed
17803 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17804 But this is unfortunately not always the case, as witnessed
17805 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17806 is used instead. To work around that ambiguity, we treat
17807 the bounds as signed, and thus sign-extend their values, when
17808 the base type is signed. */
6e70227d 17809 negative_mask =
d359392f 17810 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17811 if (low.kind == PROP_CONST
17812 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17813 low.data.const_val |= negative_mask;
17814 if (high.kind == PROP_CONST
17815 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17816 high.data.const_val |= negative_mask;
43bbcdc2 17817
5bbd8269
AB
17818 /* Check for bit and byte strides. */
17819 struct dynamic_prop byte_stride_prop;
17820 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17821 if (attr_byte_stride != nullptr)
17822 {
293e7e51 17823 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17824 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17825 prop_type);
17826 }
17827
17828 struct dynamic_prop bit_stride_prop;
17829 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17830 if (attr_bit_stride != nullptr)
17831 {
17832 /* It only makes sense to have either a bit or byte stride. */
17833 if (attr_byte_stride != nullptr)
17834 {
17835 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17836 "- DIE at %s [in module %s]"),
17837 sect_offset_str (die->sect_off),
5e22e966 17838 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17839 attr_bit_stride = nullptr;
17840 }
17841 else
17842 {
293e7e51 17843 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17844 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17845 prop_type);
17846 }
17847 }
17848
17849 if (attr_byte_stride != nullptr
17850 || attr_bit_stride != nullptr)
17851 {
17852 bool byte_stride_p = (attr_byte_stride != nullptr);
17853 struct dynamic_prop *stride
17854 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17855
17856 range_type
17857 = create_range_type_with_stride (NULL, orig_base_type, &low,
17858 &high, bias, stride, byte_stride_p);
17859 }
17860 else
17861 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17862
c451ebe5
SA
17863 if (high_bound_is_count)
17864 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17865
c2ff108b
JK
17866 /* Ada expects an empty array on no boundary attributes. */
17867 if (attr == NULL && cu->language != language_ada)
729efb13 17868 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17869
39cbfefa
DJ
17870 name = dwarf2_name (die, cu);
17871 if (name)
d0e39ea2 17872 range_type->set_name (name);
6e70227d 17873
e142c38c 17874 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17875 if (attr != nullptr)
a02abb62
JB
17876 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17877
2b4424c3
TT
17878 maybe_set_alignment (cu, die, range_type);
17879
7e314c57
JK
17880 set_die_type (die, range_type, cu);
17881
17882 /* set_die_type should be already done. */
b4ba55a1
JB
17883 set_descriptive_type (range_type, die, cu);
17884
7e314c57 17885 return range_type;
a02abb62 17886}
6e70227d 17887
f792889a 17888static struct type *
81a17f79
JB
17889read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17890{
17891 struct type *type;
81a17f79 17892
5e22e966 17893 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17894 type->set_name (dwarf2_name (die, cu));
81a17f79 17895
74a2f8ff 17896 /* In Ada, an unspecified type is typically used when the description
85102364 17897 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17898 such a type, we treat it as a stub, and try to resolve it later on,
17899 when needed. */
17900 if (cu->language == language_ada)
17901 TYPE_STUB (type) = 1;
17902
f792889a 17903 return set_die_type (die, type, cu);
81a17f79 17904}
a02abb62 17905
639d11d3
DC
17906/* Read a single die and all its descendents. Set the die's sibling
17907 field to NULL; set other fields in the die correctly, and set all
17908 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17909 location of the info_ptr after reading all of those dies. PARENT
17910 is the parent of the die in question. */
17911
17912static struct die_info *
dee91e82 17913read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17914 const gdb_byte *info_ptr,
17915 const gdb_byte **new_info_ptr,
dee91e82 17916 struct die_info *parent)
639d11d3
DC
17917{
17918 struct die_info *die;
d521ce57 17919 const gdb_byte *cur_ptr;
639d11d3 17920
3e225074 17921 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17922 if (die == NULL)
17923 {
17924 *new_info_ptr = cur_ptr;
17925 return NULL;
17926 }
93311388 17927 store_in_ref_table (die, reader->cu);
639d11d3 17928
3e225074 17929 if (die->has_children)
bf6af496 17930 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17931 else
17932 {
17933 die->child = NULL;
17934 *new_info_ptr = cur_ptr;
17935 }
17936
17937 die->sibling = NULL;
17938 die->parent = parent;
17939 return die;
17940}
17941
17942/* Read a die, all of its descendents, and all of its siblings; set
17943 all of the fields of all of the dies correctly. Arguments are as
17944 in read_die_and_children. */
17945
17946static struct die_info *
bf6af496 17947read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17948 const gdb_byte *info_ptr,
17949 const gdb_byte **new_info_ptr,
bf6af496 17950 struct die_info *parent)
639d11d3
DC
17951{
17952 struct die_info *first_die, *last_sibling;
d521ce57 17953 const gdb_byte *cur_ptr;
639d11d3 17954
c906108c 17955 cur_ptr = info_ptr;
639d11d3
DC
17956 first_die = last_sibling = NULL;
17957
17958 while (1)
c906108c 17959 {
639d11d3 17960 struct die_info *die
dee91e82 17961 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17962
1d325ec1 17963 if (die == NULL)
c906108c 17964 {
639d11d3
DC
17965 *new_info_ptr = cur_ptr;
17966 return first_die;
c906108c 17967 }
1d325ec1
DJ
17968
17969 if (!first_die)
17970 first_die = die;
c906108c 17971 else
1d325ec1
DJ
17972 last_sibling->sibling = die;
17973
17974 last_sibling = die;
c906108c 17975 }
c906108c
SS
17976}
17977
bf6af496
DE
17978/* Read a die, all of its descendents, and all of its siblings; set
17979 all of the fields of all of the dies correctly. Arguments are as
17980 in read_die_and_children.
17981 This the main entry point for reading a DIE and all its children. */
17982
17983static struct die_info *
17984read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17985 const gdb_byte *info_ptr,
17986 const gdb_byte **new_info_ptr,
bf6af496
DE
17987 struct die_info *parent)
17988{
17989 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17990 new_info_ptr, parent);
17991
b4f54984 17992 if (dwarf_die_debug)
bf6af496
DE
17993 {
17994 fprintf_unfiltered (gdb_stdlog,
17995 "Read die from %s@0x%x of %s:\n",
96b79293 17996 reader->die_section->get_name (),
bf6af496
DE
17997 (unsigned) (info_ptr - reader->die_section->buffer),
17998 bfd_get_filename (reader->abfd));
b4f54984 17999 dump_die (die, dwarf_die_debug);
bf6af496
DE
18000 }
18001
18002 return die;
18003}
18004
3019eac3
DE
18005/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18006 attributes.
18007 The caller is responsible for filling in the extra attributes
18008 and updating (*DIEP)->num_attrs.
18009 Set DIEP to point to a newly allocated die with its information,
3e225074 18010 except for its child, sibling, and parent fields. */
93311388 18011
d521ce57 18012static const gdb_byte *
3019eac3 18013read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18014 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18015 int num_extra_attrs)
93311388 18016{
b64f50a1 18017 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18018 struct abbrev_info *abbrev;
18019 struct die_info *die;
18020 struct dwarf2_cu *cu = reader->cu;
18021 bfd *abfd = reader->abfd;
18022
9c541725 18023 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18024 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18025 info_ptr += bytes_read;
18026 if (!abbrev_number)
18027 {
18028 *diep = NULL;
93311388
DE
18029 return info_ptr;
18030 }
18031
685af9cd 18032 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18033 if (!abbrev)
348e048f
DE
18034 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18035 abbrev_number,
18036 bfd_get_filename (abfd));
18037
3019eac3 18038 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18039 die->sect_off = sect_off;
93311388
DE
18040 die->tag = abbrev->tag;
18041 die->abbrev = abbrev_number;
3e225074 18042 die->has_children = abbrev->has_children;
93311388 18043
3019eac3
DE
18044 /* Make the result usable.
18045 The caller needs to update num_attrs after adding the extra
18046 attributes. */
93311388
DE
18047 die->num_attrs = abbrev->num_attrs;
18048
18a8505e 18049 std::vector<int> indexes_that_need_reprocess;
93311388 18050 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18051 {
18052 bool need_reprocess;
18053 info_ptr =
18054 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18055 info_ptr, &need_reprocess);
18056 if (need_reprocess)
18057 indexes_that_need_reprocess.push_back (i);
18058 }
18059
052c8bb8 18060 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18061 if (attr != nullptr)
18062 cu->str_offsets_base = DW_UNSND (attr);
93311388 18063
41144253 18064 attr = die->attr (DW_AT_loclists_base);
18065 if (attr != nullptr)
18066 cu->loclist_base = DW_UNSND (attr);
18067
a39fdb41 18068 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18069 if (maybe_addr_base.has_value ())
18070 cu->addr_base = *maybe_addr_base;
18071 for (int index : indexes_that_need_reprocess)
18072 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18073 *diep = die;
93311388
DE
18074 return info_ptr;
18075}
18076
3019eac3
DE
18077/* Read a die and all its attributes.
18078 Set DIEP to point to a newly allocated die with its information,
3e225074 18079 except for its child, sibling, and parent fields. */
3019eac3 18080
d521ce57 18081static const gdb_byte *
3019eac3 18082read_full_die (const struct die_reader_specs *reader,
3e225074 18083 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18084{
d521ce57 18085 const gdb_byte *result;
bf6af496 18086
3e225074 18087 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18088
b4f54984 18089 if (dwarf_die_debug)
bf6af496
DE
18090 {
18091 fprintf_unfiltered (gdb_stdlog,
18092 "Read die from %s@0x%x of %s:\n",
96b79293 18093 reader->die_section->get_name (),
bf6af496
DE
18094 (unsigned) (info_ptr - reader->die_section->buffer),
18095 bfd_get_filename (reader->abfd));
b4f54984 18096 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18097 }
18098
18099 return result;
3019eac3 18100}
433df2d4 18101\f
c906108c 18102
72bf9492
DJ
18103/* Returns nonzero if TAG represents a type that we might generate a partial
18104 symbol for. */
18105
18106static int
18107is_type_tag_for_partial (int tag)
18108{
18109 switch (tag)
18110 {
18111#if 0
18112 /* Some types that would be reasonable to generate partial symbols for,
18113 that we don't at present. */
18114 case DW_TAG_array_type:
18115 case DW_TAG_file_type:
18116 case DW_TAG_ptr_to_member_type:
18117 case DW_TAG_set_type:
18118 case DW_TAG_string_type:
18119 case DW_TAG_subroutine_type:
18120#endif
18121 case DW_TAG_base_type:
18122 case DW_TAG_class_type:
680b30c7 18123 case DW_TAG_interface_type:
72bf9492
DJ
18124 case DW_TAG_enumeration_type:
18125 case DW_TAG_structure_type:
18126 case DW_TAG_subrange_type:
18127 case DW_TAG_typedef:
18128 case DW_TAG_union_type:
18129 return 1;
18130 default:
18131 return 0;
18132 }
18133}
18134
18135/* Load all DIEs that are interesting for partial symbols into memory. */
18136
18137static struct partial_die_info *
dee91e82 18138load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18139 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18140{
dee91e82 18141 struct dwarf2_cu *cu = reader->cu;
5e22e966 18142 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18143 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18144 unsigned int bytes_read;
5afb4e99 18145 unsigned int load_all = 0;
72bf9492
DJ
18146 int nesting_level = 1;
18147
18148 parent_die = NULL;
18149 last_die = NULL;
18150
7adf1e79
DE
18151 gdb_assert (cu->per_cu != NULL);
18152 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18153 load_all = 1;
18154
72bf9492
DJ
18155 cu->partial_dies
18156 = htab_create_alloc_ex (cu->header.length / 12,
18157 partial_die_hash,
18158 partial_die_eq,
18159 NULL,
18160 &cu->comp_unit_obstack,
18161 hashtab_obstack_allocate,
18162 dummy_obstack_deallocate);
18163
72bf9492
DJ
18164 while (1)
18165 {
685af9cd 18166 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18167
18168 /* A NULL abbrev means the end of a series of children. */
18169 if (abbrev == NULL)
18170 {
18171 if (--nesting_level == 0)
cd9983dd
YQ
18172 return first_die;
18173
72bf9492
DJ
18174 info_ptr += bytes_read;
18175 last_die = parent_die;
18176 parent_die = parent_die->die_parent;
18177 continue;
18178 }
18179
98bfdba5
PA
18180 /* Check for template arguments. We never save these; if
18181 they're seen, we just mark the parent, and go on our way. */
18182 if (parent_die != NULL
18183 && cu->language == language_cplus
18184 && (abbrev->tag == DW_TAG_template_type_param
18185 || abbrev->tag == DW_TAG_template_value_param))
18186 {
18187 parent_die->has_template_arguments = 1;
18188
18189 if (!load_all)
18190 {
18191 /* We don't need a partial DIE for the template argument. */
dee91e82 18192 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18193 continue;
18194 }
18195 }
18196
0d99eb77 18197 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18198 Skip their other children. */
18199 if (!load_all
18200 && cu->language == language_cplus
18201 && parent_die != NULL
18202 && parent_die->tag == DW_TAG_subprogram)
18203 {
dee91e82 18204 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18205 continue;
18206 }
18207
5afb4e99
DJ
18208 /* Check whether this DIE is interesting enough to save. Normally
18209 we would not be interested in members here, but there may be
18210 later variables referencing them via DW_AT_specification (for
18211 static members). */
18212 if (!load_all
18213 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18214 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18215 && abbrev->tag != DW_TAG_enumerator
18216 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18217 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18218 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18219 && abbrev->tag != DW_TAG_variable
5afb4e99 18220 && abbrev->tag != DW_TAG_namespace
f55ee35c 18221 && abbrev->tag != DW_TAG_module
95554aad 18222 && abbrev->tag != DW_TAG_member
74921315
KS
18223 && abbrev->tag != DW_TAG_imported_unit
18224 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18225 {
18226 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18227 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18228 continue;
18229 }
18230
6f06d47b
YQ
18231 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18232 abbrev);
cd9983dd 18233
48fbe735 18234 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18235
18236 /* This two-pass algorithm for processing partial symbols has a
18237 high cost in cache pressure. Thus, handle some simple cases
18238 here which cover the majority of C partial symbols. DIEs
18239 which neither have specification tags in them, nor could have
18240 specification tags elsewhere pointing at them, can simply be
18241 processed and discarded.
18242
18243 This segment is also optional; scan_partial_symbols and
18244 add_partial_symbol will handle these DIEs if we chain
18245 them in normally. When compilers which do not emit large
18246 quantities of duplicate debug information are more common,
18247 this code can probably be removed. */
18248
18249 /* Any complete simple types at the top level (pretty much all
18250 of them, for a language without namespaces), can be processed
18251 directly. */
18252 if (parent_die == NULL
cd9983dd
YQ
18253 && pdi.has_specification == 0
18254 && pdi.is_declaration == 0
18255 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18256 || pdi.tag == DW_TAG_base_type
18257 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18258 {
cd9983dd 18259 if (building_psymtab && pdi.name != NULL)
31edb802 18260 add_psymbol_to_list (pdi.name, false,
79748972 18261 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18262 psymbol_placement::STATIC,
1762568f 18263 0, cu->language, objfile);
cd9983dd 18264 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18265 continue;
18266 }
18267
d8228535
JK
18268 /* The exception for DW_TAG_typedef with has_children above is
18269 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18270 type_name_or_error will error on such types later.
d8228535
JK
18271
18272 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18273 it could not find the child DIEs referenced later, this is checked
18274 above. In correct DWARF DW_TAG_typedef should have no children. */
18275
cd9983dd 18276 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18277 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18278 "- DIE at %s [in module %s]"),
cd9983dd 18279 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18280
72bf9492
DJ
18281 /* If we're at the second level, and we're an enumerator, and
18282 our parent has no specification (meaning possibly lives in a
18283 namespace elsewhere), then we can add the partial symbol now
18284 instead of queueing it. */
cd9983dd 18285 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18286 && parent_die != NULL
18287 && parent_die->die_parent == NULL
18288 && parent_die->tag == DW_TAG_enumeration_type
18289 && parent_die->has_specification == 0)
18290 {
cd9983dd 18291 if (pdi.name == NULL)
b98664d3 18292 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18293 else if (building_psymtab)
31edb802 18294 add_psymbol_to_list (pdi.name, false,
79748972 18295 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18296 cu->language == language_cplus
75aedd27
TT
18297 ? psymbol_placement::GLOBAL
18298 : psymbol_placement::STATIC,
1762568f 18299 0, cu->language, objfile);
72bf9492 18300
cd9983dd 18301 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18302 continue;
18303 }
18304
cd9983dd 18305 struct partial_die_info *part_die
6f06d47b 18306 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18307
72bf9492
DJ
18308 /* We'll save this DIE so link it in. */
18309 part_die->die_parent = parent_die;
18310 part_die->die_sibling = NULL;
18311 part_die->die_child = NULL;
18312
18313 if (last_die && last_die == parent_die)
18314 last_die->die_child = part_die;
18315 else if (last_die)
18316 last_die->die_sibling = part_die;
18317
18318 last_die = part_die;
18319
18320 if (first_die == NULL)
18321 first_die = part_die;
18322
18323 /* Maybe add the DIE to the hash table. Not all DIEs that we
18324 find interesting need to be in the hash table, because we
18325 also have the parent/sibling/child chains; only those that we
18326 might refer to by offset later during partial symbol reading.
18327
18328 For now this means things that might have be the target of a
18329 DW_AT_specification, DW_AT_abstract_origin, or
18330 DW_AT_extension. DW_AT_extension will refer only to
18331 namespaces; DW_AT_abstract_origin refers to functions (and
18332 many things under the function DIE, but we do not recurse
18333 into function DIEs during partial symbol reading) and
18334 possibly variables as well; DW_AT_specification refers to
18335 declarations. Declarations ought to have the DW_AT_declaration
18336 flag. It happens that GCC forgets to put it in sometimes, but
18337 only for functions, not for types.
18338
18339 Adding more things than necessary to the hash table is harmless
18340 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18341 wasted time in find_partial_die, when we reread the compilation
18342 unit with load_all_dies set. */
72bf9492 18343
5afb4e99 18344 if (load_all
72929c62 18345 || abbrev->tag == DW_TAG_constant
5afb4e99 18346 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18347 || abbrev->tag == DW_TAG_variable
18348 || abbrev->tag == DW_TAG_namespace
18349 || part_die->is_declaration)
18350 {
18351 void **slot;
18352
18353 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18354 to_underlying (part_die->sect_off),
18355 INSERT);
72bf9492
DJ
18356 *slot = part_die;
18357 }
18358
72bf9492 18359 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18360 we have no reason to follow the children of structures; for other
98bfdba5
PA
18361 languages we have to, so that we can get at method physnames
18362 to infer fully qualified class names, for DW_AT_specification,
18363 and for C++ template arguments. For C++, we also look one level
18364 inside functions to find template arguments (if the name of the
18365 function does not already contain the template arguments).
bc30ff58 18366
0a4b0913
AB
18367 For Ada and Fortran, we need to scan the children of subprograms
18368 and lexical blocks as well because these languages allow the
18369 definition of nested entities that could be interesting for the
18370 debugger, such as nested subprograms for instance. */
72bf9492 18371 if (last_die->has_children
5afb4e99
DJ
18372 && (load_all
18373 || last_die->tag == DW_TAG_namespace
f55ee35c 18374 || last_die->tag == DW_TAG_module
72bf9492 18375 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18376 || (cu->language == language_cplus
18377 && last_die->tag == DW_TAG_subprogram
18378 && (last_die->name == NULL
18379 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18380 || (cu->language != language_c
18381 && (last_die->tag == DW_TAG_class_type
680b30c7 18382 || last_die->tag == DW_TAG_interface_type
72bf9492 18383 || last_die->tag == DW_TAG_structure_type
bc30ff58 18384 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18385 || ((cu->language == language_ada
18386 || cu->language == language_fortran)
bc30ff58
JB
18387 && (last_die->tag == DW_TAG_subprogram
18388 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18389 {
18390 nesting_level++;
18391 parent_die = last_die;
18392 continue;
18393 }
18394
18395 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18396 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18397
18398 /* Back to the top, do it again. */
18399 }
18400}
18401
6f06d47b
YQ
18402partial_die_info::partial_die_info (sect_offset sect_off_,
18403 struct abbrev_info *abbrev)
18404 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18405{
18406}
18407
35cc7ed7
YQ
18408/* Read a minimal amount of information into the minimal die structure.
18409 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18410
48fbe735
YQ
18411const gdb_byte *
18412partial_die_info::read (const struct die_reader_specs *reader,
18413 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18414{
dee91e82 18415 struct dwarf2_cu *cu = reader->cu;
5e22e966 18416 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18417 unsigned int i;
c5aa993b 18418 int has_low_pc_attr = 0;
c906108c 18419 int has_high_pc_attr = 0;
91da1414 18420 int high_pc_relative = 0;
c906108c 18421
fd0a254f 18422 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18423 {
e7da7f8f 18424 attribute attr;
18a8505e 18425 bool need_reprocess;
e7da7f8f 18426 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18427 info_ptr, &need_reprocess);
18428 /* String and address offsets that need to do the reprocessing have
18429 already been read at this point, so there is no need to wait until
18430 the loop terminates to do the reprocessing. */
18431 if (need_reprocess)
e7da7f8f 18432 read_attribute_reprocess (reader, &attr);
c906108c 18433 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18434 partial symbol table. */
c906108c
SS
18435 switch (attr.name)
18436 {
18437 case DW_AT_name:
48fbe735 18438 switch (tag)
71c25dea
TT
18439 {
18440 case DW_TAG_compile_unit:
95554aad 18441 case DW_TAG_partial_unit:
348e048f 18442 case DW_TAG_type_unit:
71c25dea
TT
18443 /* Compilation units have a DW_AT_name that is a filename, not
18444 a source language identifier. */
18445 case DW_TAG_enumeration_type:
18446 case DW_TAG_enumerator:
18447 /* These tags always have simple identifiers already; no need
18448 to canonicalize them. */
48fbe735 18449 name = DW_STRING (&attr);
71c25dea
TT
18450 break;
18451 default:
48fbe735
YQ
18452 {
18453 struct objfile *objfile = dwarf2_per_objfile->objfile;
18454
18455 name
be1e3d3e 18456 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18457 }
71c25dea
TT
18458 break;
18459 }
c906108c 18460 break;
31ef98ae 18461 case DW_AT_linkage_name:
c906108c 18462 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18463 /* Note that both forms of linkage name might appear. We
18464 assume they will be the same, and we only store the last
18465 one we see. */
e61108c9 18466 linkage_name = attr.value_as_string ();
787de330
TT
18467 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18468 See https://github.com/rust-lang/rust/issues/32925. */
18469 if (cu->language == language_rust && linkage_name != NULL
18470 && strchr (linkage_name, '{') != NULL)
18471 linkage_name = NULL;
c906108c
SS
18472 break;
18473 case DW_AT_low_pc:
18474 has_low_pc_attr = 1;
cd6c91b4 18475 lowpc = attr.value_as_address ();
c906108c
SS
18476 break;
18477 case DW_AT_high_pc:
18478 has_high_pc_attr = 1;
cd6c91b4
TT
18479 highpc = attr.value_as_address ();
18480 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18481 high_pc_relative = 1;
c906108c
SS
18482 break;
18483 case DW_AT_location:
0963b4bd 18484 /* Support the .debug_loc offsets. */
4fc6c0d5 18485 if (attr.form_is_block ())
8e19ed76 18486 {
48fbe735 18487 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18488 }
cd6c91b4 18489 else if (attr.form_is_section_offset ())
8e19ed76 18490 {
4d3c2250 18491 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18492 }
18493 else
18494 {
4d3c2250
KB
18495 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18496 "partial symbol information");
8e19ed76 18497 }
c906108c 18498 break;
c906108c 18499 case DW_AT_external:
48fbe735 18500 is_external = DW_UNSND (&attr);
c906108c
SS
18501 break;
18502 case DW_AT_declaration:
48fbe735 18503 is_declaration = DW_UNSND (&attr);
c906108c
SS
18504 break;
18505 case DW_AT_type:
48fbe735 18506 has_type = 1;
c906108c
SS
18507 break;
18508 case DW_AT_abstract_origin:
18509 case DW_AT_specification:
72bf9492 18510 case DW_AT_extension:
48fbe735 18511 has_specification = 1;
0826b30a 18512 spec_offset = attr.get_ref_die_offset ();
48fbe735 18513 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18514 || cu->per_cu->is_dwz);
c906108c
SS
18515 break;
18516 case DW_AT_sibling:
18517 /* Ignore absolute siblings, they might point outside of
18518 the current compile unit. */
18519 if (attr.form == DW_FORM_ref_addr)
b98664d3 18520 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18521 else
b9502d3f 18522 {
48fbe735 18523 const gdb_byte *buffer = reader->buffer;
0826b30a 18524 sect_offset off = attr.get_ref_die_offset ();
9c541725 18525 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18526
18527 if (sibling_ptr < info_ptr)
b98664d3 18528 complaint (_("DW_AT_sibling points backwards"));
22869d73 18529 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18530 reader->die_section->overflow_complaint ();
b9502d3f 18531 else
48fbe735 18532 sibling = sibling_ptr;
b9502d3f 18533 }
c906108c 18534 break;
fa4028e9 18535 case DW_AT_byte_size:
48fbe735 18536 has_byte_size = 1;
fa4028e9 18537 break;
ff908ebf 18538 case DW_AT_const_value:
48fbe735 18539 has_const_value = 1;
ff908ebf 18540 break;
68511cec
CES
18541 case DW_AT_calling_convention:
18542 /* DWARF doesn't provide a way to identify a program's source-level
18543 entry point. DW_AT_calling_convention attributes are only meant
18544 to describe functions' calling conventions.
18545
18546 However, because it's a necessary piece of information in
0c1b455e
TT
18547 Fortran, and before DWARF 4 DW_CC_program was the only
18548 piece of debugging information whose definition refers to
18549 a 'main program' at all, several compilers marked Fortran
18550 main programs with DW_CC_program --- even when those
18551 functions use the standard calling conventions.
18552
18553 Although DWARF now specifies a way to provide this
18554 information, we support this practice for backward
18555 compatibility. */
68511cec 18556 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18557 && cu->language == language_fortran)
48fbe735 18558 main_subprogram = 1;
68511cec 18559 break;
481860b3
GB
18560 case DW_AT_inline:
18561 if (DW_UNSND (&attr) == DW_INL_inlined
18562 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18563 may_be_inlined = 1;
481860b3 18564 break;
95554aad
TT
18565
18566 case DW_AT_import:
48fbe735 18567 if (tag == DW_TAG_imported_unit)
36586728 18568 {
0826b30a 18569 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18570 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18571 || cu->per_cu->is_dwz);
18572 }
95554aad
TT
18573 break;
18574
0c1b455e 18575 case DW_AT_main_subprogram:
48fbe735 18576 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18577 break;
18578
05caa1d2
TT
18579 case DW_AT_ranges:
18580 {
18581 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18582 but that requires a full DIE, so instead we just
18583 reimplement it. */
18584 int need_ranges_base = tag != DW_TAG_compile_unit;
18585 unsigned int ranges_offset = (DW_UNSND (&attr)
18586 + (need_ranges_base
18587 ? cu->ranges_base
18588 : 0));
18589
18590 /* Value of the DW_AT_ranges attribute is the offset in the
18591 .debug_ranges section. */
18592 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18593 nullptr))
18594 has_pc_info = 1;
18595 }
18596 break;
18597
c906108c
SS
18598 default:
18599 break;
18600 }
18601 }
18602
10d06d82
TT
18603 /* For Ada, if both the name and the linkage name appear, we prefer
18604 the latter. This lets "catch exception" work better, regardless
18605 of the order in which the name and linkage name were emitted.
18606 Really, though, this is just a workaround for the fact that gdb
18607 doesn't store both the name and the linkage name. */
18608 if (cu->language == language_ada && linkage_name != nullptr)
18609 name = linkage_name;
18610
91da1414 18611 if (high_pc_relative)
48fbe735 18612 highpc += lowpc;
91da1414 18613
9373cf26
JK
18614 if (has_low_pc_attr && has_high_pc_attr)
18615 {
18616 /* When using the GNU linker, .gnu.linkonce. sections are used to
18617 eliminate duplicate copies of functions and vtables and such.
18618 The linker will arbitrarily choose one and discard the others.
18619 The AT_*_pc values for such functions refer to local labels in
18620 these sections. If the section from that file was discarded, the
18621 labels are not in the output, so the relocs get a value of 0.
18622 If this is a discarded function, mark the pc bounds as invalid,
18623 so that GDB will ignore it. */
5989a64e 18624 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18625 {
48fbe735 18626 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18627 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18628
b98664d3 18629 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18630 "for DIE at %s [in module %s]"),
48fbe735
YQ
18631 paddress (gdbarch, lowpc),
18632 sect_offset_str (sect_off),
9d8780f0 18633 objfile_name (objfile));
9373cf26
JK
18634 }
18635 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18636 else if (lowpc >= highpc)
9373cf26 18637 {
48fbe735 18638 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18639 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18640
b98664d3 18641 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18642 "for DIE at %s [in module %s]"),
48fbe735
YQ
18643 paddress (gdbarch, lowpc),
18644 paddress (gdbarch, highpc),
18645 sect_offset_str (sect_off),
9c541725 18646 objfile_name (objfile));
9373cf26
JK
18647 }
18648 else
48fbe735 18649 has_pc_info = 1;
9373cf26 18650 }
85cbf3d3 18651
c906108c
SS
18652 return info_ptr;
18653}
18654
72bf9492
DJ
18655/* Find a cached partial DIE at OFFSET in CU. */
18656
d590ff25
YQ
18657struct partial_die_info *
18658dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18659{
18660 struct partial_die_info *lookup_die = NULL;
6f06d47b 18661 struct partial_die_info part_die (sect_off);
72bf9492 18662
9a3c8263 18663 lookup_die = ((struct partial_die_info *)
d590ff25 18664 htab_find_with_hash (partial_dies, &part_die,
9c541725 18665 to_underlying (sect_off)));
72bf9492 18666
72bf9492
DJ
18667 return lookup_die;
18668}
18669
348e048f
DE
18670/* Find a partial DIE at OFFSET, which may or may not be in CU,
18671 except in the case of .debug_types DIEs which do not reference
18672 outside their CU (they do however referencing other types via
55f1336d 18673 DW_FORM_ref_sig8). */
72bf9492 18674
122cf0f2 18675static const struct cu_partial_die_info
9c541725 18676find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18677{
5e22e966 18678 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18679 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18680 struct dwarf2_per_cu_data *per_cu = NULL;
18681 struct partial_die_info *pd = NULL;
72bf9492 18682
36586728 18683 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18684 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18685 {
d590ff25 18686 pd = cu->find_partial_die (sect_off);
5afb4e99 18687 if (pd != NULL)
fb816e8b 18688 return { cu, pd };
0d99eb77
DE
18689 /* We missed recording what we needed.
18690 Load all dies and try again. */
18691 per_cu = cu->per_cu;
5afb4e99 18692 }
0d99eb77
DE
18693 else
18694 {
18695 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18696 if (cu->per_cu->is_debug_types)
0d99eb77 18697 {
9d8780f0
SM
18698 error (_("Dwarf Error: Type Unit at offset %s contains"
18699 " external reference to offset %s [in module %s].\n"),
18700 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18701 bfd_get_filename (objfile->obfd));
18702 }
9c541725 18703 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18704 dwarf2_per_objfile);
72bf9492 18705
0d99eb77 18706 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
ab432490 18707 load_partial_comp_unit (per_cu, cu->per_objfile);
ae038cb0 18708
0d99eb77 18709 per_cu->cu->last_used = 0;
d590ff25 18710 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18711 }
5afb4e99 18712
dee91e82
DE
18713 /* If we didn't find it, and not all dies have been loaded,
18714 load them all and try again. */
18715
5afb4e99
DJ
18716 if (pd == NULL && per_cu->load_all_dies == 0)
18717 {
5afb4e99 18718 per_cu->load_all_dies = 1;
fd820528
DE
18719
18720 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18721 THIS_CU->cu may already be in use. So we can't just free it and
18722 replace its DIEs with the ones we read in. Instead, we leave those
18723 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18724 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18725 set. */
ab432490 18726 load_partial_comp_unit (per_cu, cu->per_objfile);
5afb4e99 18727
d590ff25 18728 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18729 }
18730
18731 if (pd == NULL)
18732 internal_error (__FILE__, __LINE__,
9d8780f0 18733 _("could not find partial DIE %s "
3e43a32a 18734 "in cache [from module %s]\n"),
9d8780f0 18735 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18736 return { per_cu->cu, pd };
72bf9492
DJ
18737}
18738
abc72ce4
DE
18739/* See if we can figure out if the class lives in a namespace. We do
18740 this by looking for a member function; its demangled name will
18741 contain namespace info, if there is any. */
18742
18743static void
18744guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18745 struct dwarf2_cu *cu)
18746{
18747 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18748 what template types look like, because the demangler
18749 frequently doesn't give the same name as the debug info. We
18750 could fix this by only using the demangled name to get the
18751 prefix (but see comment in read_structure_type). */
18752
18753 struct partial_die_info *real_pdi;
18754 struct partial_die_info *child_pdi;
18755
18756 /* If this DIE (this DIE's specification, if any) has a parent, then
18757 we should not do this. We'll prepend the parent's fully qualified
18758 name when we create the partial symbol. */
18759
18760 real_pdi = struct_pdi;
18761 while (real_pdi->has_specification)
fb816e8b 18762 {
122cf0f2
AB
18763 auto res = find_partial_die (real_pdi->spec_offset,
18764 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18765 real_pdi = res.pdi;
18766 cu = res.cu;
18767 }
abc72ce4
DE
18768
18769 if (real_pdi->die_parent != NULL)
18770 return;
18771
18772 for (child_pdi = struct_pdi->die_child;
18773 child_pdi != NULL;
18774 child_pdi = child_pdi->die_sibling)
18775 {
18776 if (child_pdi->tag == DW_TAG_subprogram
18777 && child_pdi->linkage_name != NULL)
18778 {
43816ebc
TT
18779 gdb::unique_xmalloc_ptr<char> actual_class_name
18780 (language_class_name_from_physname (cu->language_defn,
18781 child_pdi->linkage_name));
abc72ce4
DE
18782 if (actual_class_name != NULL)
18783 {
5e22e966 18784 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18785 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18786 }
18787 break;
18788 }
18789 }
18790}
18791
25c11aca
TV
18792/* Return true if a DIE with TAG may have the DW_AT_const_value
18793 attribute. */
18794
18795static bool
18796can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18797{
18798 switch (tag)
18799 {
18800 case DW_TAG_constant:
18801 case DW_TAG_enumerator:
18802 case DW_TAG_formal_parameter:
18803 case DW_TAG_template_value_param:
18804 case DW_TAG_variable:
18805 return true;
18806 }
18807
18808 return false;
18809}
18810
52356b79
YQ
18811void
18812partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18813{
abc72ce4
DE
18814 /* Once we've fixed up a die, there's no point in doing so again.
18815 This also avoids a memory leak if we were to call
18816 guess_partial_die_structure_name multiple times. */
52356b79 18817 if (fixup_called)
abc72ce4
DE
18818 return;
18819
72bf9492
DJ
18820 /* If we found a reference attribute and the DIE has no name, try
18821 to find a name in the referred to DIE. */
18822
52356b79 18823 if (name == NULL && has_specification)
72bf9492
DJ
18824 {
18825 struct partial_die_info *spec_die;
72bf9492 18826
122cf0f2 18827 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18828 spec_die = res.pdi;
18829 cu = res.cu;
72bf9492 18830
52356b79 18831 spec_die->fixup (cu);
72bf9492
DJ
18832
18833 if (spec_die->name)
18834 {
52356b79 18835 name = spec_die->name;
72bf9492
DJ
18836
18837 /* Copy DW_AT_external attribute if it is set. */
18838 if (spec_die->is_external)
52356b79 18839 is_external = spec_die->is_external;
72bf9492
DJ
18840 }
18841 }
18842
25c11aca
TV
18843 if (!has_const_value && has_specification
18844 && can_have_DW_AT_const_value_p (tag))
18845 {
18846 struct partial_die_info *spec_die;
18847
18848 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18849 spec_die = res.pdi;
18850 cu = res.cu;
18851
18852 spec_die->fixup (cu);
18853
18854 if (spec_die->has_const_value)
18855 {
18856 /* Copy DW_AT_const_value attribute if it is set. */
18857 has_const_value = spec_die->has_const_value;
18858 }
18859 }
18860
72bf9492 18861 /* Set default names for some unnamed DIEs. */
72bf9492 18862
52356b79
YQ
18863 if (name == NULL && tag == DW_TAG_namespace)
18864 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18865
abc72ce4
DE
18866 /* If there is no parent die to provide a namespace, and there are
18867 children, see if we can determine the namespace from their linkage
122d1940 18868 name. */
abc72ce4 18869 if (cu->language == language_cplus
5e22e966 18870 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18871 && die_parent == NULL
18872 && has_children
18873 && (tag == DW_TAG_class_type
18874 || tag == DW_TAG_structure_type
18875 || tag == DW_TAG_union_type))
18876 guess_partial_die_structure_name (this, cu);
abc72ce4 18877
53832f31
TT
18878 /* GCC might emit a nameless struct or union that has a linkage
18879 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18880 if (name == NULL
18881 && (tag == DW_TAG_class_type
18882 || tag == DW_TAG_interface_type
18883 || tag == DW_TAG_structure_type
18884 || tag == DW_TAG_union_type)
18885 && linkage_name != NULL)
53832f31 18886 {
43816ebc
TT
18887 gdb::unique_xmalloc_ptr<char> demangled
18888 (gdb_demangle (linkage_name, DMGL_TYPES));
18889 if (demangled != nullptr)
53832f31 18890 {
96408a79
SA
18891 const char *base;
18892
18893 /* Strip any leading namespaces/classes, keep only the base name.
18894 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18895 base = strrchr (demangled.get (), ':');
18896 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18897 base++;
18898 else
43816ebc 18899 base = demangled.get ();
96408a79 18900
5e22e966 18901 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18902 name = objfile->intern (base);
53832f31
TT
18903 }
18904 }
18905
52356b79 18906 fixup_called = 1;
72bf9492
DJ
18907}
18908
41144253 18909/* Read the .debug_loclists header contents from the given SECTION in the
18910 HEADER. */
18911static void
18912read_loclist_header (struct loclist_header *header,
18913 struct dwarf2_section_info *section)
18914{
18915 unsigned int bytes_read;
18916 bfd *abfd = section->get_bfd_owner ();
18917 const gdb_byte *info_ptr = section->buffer;
18918 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18919 info_ptr += bytes_read;
18920 header->version = read_2_bytes (abfd, info_ptr);
18921 info_ptr += 2;
18922 header->addr_size = read_1_byte (abfd, info_ptr);
18923 info_ptr += 1;
18924 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18925 info_ptr += 1;
18926 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18927}
18928
18929/* Return the DW_AT_loclists_base value for the CU. */
18930static ULONGEST
18931lookup_loclist_base (struct dwarf2_cu *cu)
18932{
18933 /* For the .dwo unit, the loclist_base points to the first offset following
18934 the header. The header consists of the following entities-
18935 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18936 bit format)
18937 2. version (2 bytes)
18938 3. address size (1 byte)
18939 4. segment selector size (1 byte)
18940 5. offset entry count (4 bytes)
18941 These sizes are derived as per the DWARFv5 standard. */
18942 if (cu->dwo_unit != nullptr)
18943 {
18944 if (cu->header.initial_length_size == 4)
18945 return LOCLIST_HEADER_SIZE32;
18946 return LOCLIST_HEADER_SIZE64;
18947 }
18948 return cu->loclist_base;
18949}
18950
18951/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18952 array of offsets in the .debug_loclists section. */
18953static CORE_ADDR
18954read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18955{
5e22e966 18956 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 18957 struct objfile *objfile = dwarf2_per_objfile->objfile;
18958 bfd *abfd = objfile->obfd;
18959 ULONGEST loclist_base = lookup_loclist_base (cu);
18960 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18961
18962 section->read (objfile);
18963 if (section->buffer == NULL)
18964 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18965 "section [in module %s]"), objfile_name (objfile));
18966 struct loclist_header header;
18967 read_loclist_header (&header, section);
18968 if (loclist_index >= header.offset_entry_count)
18969 complaint (_("DW_FORM_loclistx pointing outside of "
18970 ".debug_loclists offset array [in module %s]"),
18971 objfile_name (objfile));
18972 if (loclist_base + loclist_index * cu->header.offset_size
18973 >= section->size)
18974 complaint (_("DW_FORM_loclistx pointing outside of "
18975 ".debug_loclists section [in module %s]"),
18976 objfile_name (objfile));
18977 const gdb_byte *info_ptr
18978 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18979
18980 if (cu->header.offset_size == 4)
18981 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18982 else
18983 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18984}
18985
18a8505e
AT
18986/* Process the attributes that had to be skipped in the first round. These
18987 attributes are the ones that need str_offsets_base or addr_base attributes.
18988 They could not have been processed in the first round, because at the time
18989 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18990static void
18991read_attribute_reprocess (const struct die_reader_specs *reader,
18992 struct attribute *attr)
18a8505e
AT
18993{
18994 struct dwarf2_cu *cu = reader->cu;
18995 switch (attr->form)
18996 {
18997 case DW_FORM_addrx:
18998 case DW_FORM_GNU_addr_index:
18999 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19000 break;
41144253 19001 case DW_FORM_loclistx:
19002 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19003 break;
18a8505e
AT
19004 case DW_FORM_strx:
19005 case DW_FORM_strx1:
19006 case DW_FORM_strx2:
19007 case DW_FORM_strx3:
19008 case DW_FORM_strx4:
19009 case DW_FORM_GNU_str_index:
19010 {
19011 unsigned int str_index = DW_UNSND (attr);
19012 if (reader->dwo_file != NULL)
19013 {
19014 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19015 DW_STRING_IS_CANONICAL (attr) = 0;
19016 }
19017 else
19018 {
19019 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19020 DW_STRING_IS_CANONICAL (attr) = 0;
19021 }
19022 break;
19023 }
19024 default:
19025 gdb_assert_not_reached (_("Unexpected DWARF form."));
19026 }
19027}
19028
a8329558 19029/* Read an attribute value described by an attribute form. */
c906108c 19030
d521ce57 19031static const gdb_byte *
dee91e82
DE
19032read_attribute_value (const struct die_reader_specs *reader,
19033 struct attribute *attr, unsigned form,
18a8505e
AT
19034 LONGEST implicit_const, const gdb_byte *info_ptr,
19035 bool *need_reprocess)
c906108c 19036{
dee91e82 19037 struct dwarf2_cu *cu = reader->cu;
5e22e966 19038 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19039 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19040 bfd *abfd = reader->abfd;
e7c27a73 19041 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19042 unsigned int bytes_read;
19043 struct dwarf_block *blk;
18a8505e 19044 *need_reprocess = false;
c906108c 19045
aead7601 19046 attr->form = (enum dwarf_form) form;
a8329558 19047 switch (form)
c906108c 19048 {
c906108c 19049 case DW_FORM_ref_addr:
ae411497 19050 if (cu->header.version == 2)
c8a7a66f
TT
19051 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19052 &bytes_read);
ae411497 19053 else
8266302d
TT
19054 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19055 &bytes_read);
ae411497
TT
19056 info_ptr += bytes_read;
19057 break;
36586728 19058 case DW_FORM_GNU_ref_alt:
8266302d 19059 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19060 info_ptr += bytes_read;
19061 break;
ae411497 19062 case DW_FORM_addr:
08feed99
TT
19063 {
19064 struct gdbarch *gdbarch = objfile->arch ();
19065 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19066 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19067 info_ptr += bytes_read;
19068 }
c906108c
SS
19069 break;
19070 case DW_FORM_block2:
7b5a2f43 19071 blk = dwarf_alloc_block (cu);
c906108c
SS
19072 blk->size = read_2_bytes (abfd, info_ptr);
19073 info_ptr += 2;
19074 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19075 info_ptr += blk->size;
19076 DW_BLOCK (attr) = blk;
19077 break;
19078 case DW_FORM_block4:
7b5a2f43 19079 blk = dwarf_alloc_block (cu);
c906108c
SS
19080 blk->size = read_4_bytes (abfd, info_ptr);
19081 info_ptr += 4;
19082 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19083 info_ptr += blk->size;
19084 DW_BLOCK (attr) = blk;
19085 break;
19086 case DW_FORM_data2:
19087 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19088 info_ptr += 2;
19089 break;
19090 case DW_FORM_data4:
19091 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19092 info_ptr += 4;
19093 break;
19094 case DW_FORM_data8:
19095 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19096 info_ptr += 8;
19097 break;
0224619f
JK
19098 case DW_FORM_data16:
19099 blk = dwarf_alloc_block (cu);
19100 blk->size = 16;
19101 blk->data = read_n_bytes (abfd, info_ptr, 16);
19102 info_ptr += 16;
19103 DW_BLOCK (attr) = blk;
19104 break;
2dc7f7b3 19105 case DW_FORM_sec_offset:
8266302d 19106 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19107 info_ptr += bytes_read;
19108 break;
41144253 19109 case DW_FORM_loclistx:
19110 {
19111 *need_reprocess = true;
19112 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19113 info_ptr += bytes_read;
19114 }
19115 break;
c906108c 19116 case DW_FORM_string:
9b1c24c8 19117 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19118 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19119 info_ptr += bytes_read;
19120 break;
4bdf3d34 19121 case DW_FORM_strp:
36586728
TT
19122 if (!cu->per_cu->is_dwz)
19123 {
ed2dc618
SM
19124 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19125 abfd, info_ptr, cu_header,
36586728
TT
19126 &bytes_read);
19127 DW_STRING_IS_CANONICAL (attr) = 0;
19128 info_ptr += bytes_read;
19129 break;
19130 }
19131 /* FALLTHROUGH */
43988095
JK
19132 case DW_FORM_line_strp:
19133 if (!cu->per_cu->is_dwz)
19134 {
86c0bb4c
TT
19135 DW_STRING (attr)
19136 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19137 &bytes_read);
43988095
JK
19138 DW_STRING_IS_CANONICAL (attr) = 0;
19139 info_ptr += bytes_read;
19140 break;
19141 }
19142 /* FALLTHROUGH */
36586728
TT
19143 case DW_FORM_GNU_strp_alt:
19144 {
c3699833 19145 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19146 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19147 &bytes_read);
36586728 19148
0314b390 19149 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19150 DW_STRING_IS_CANONICAL (attr) = 0;
19151 info_ptr += bytes_read;
19152 }
4bdf3d34 19153 break;
2dc7f7b3 19154 case DW_FORM_exprloc:
c906108c 19155 case DW_FORM_block:
7b5a2f43 19156 blk = dwarf_alloc_block (cu);
c906108c
SS
19157 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19158 info_ptr += bytes_read;
19159 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19160 info_ptr += blk->size;
19161 DW_BLOCK (attr) = blk;
19162 break;
19163 case DW_FORM_block1:
7b5a2f43 19164 blk = dwarf_alloc_block (cu);
c906108c
SS
19165 blk->size = read_1_byte (abfd, info_ptr);
19166 info_ptr += 1;
19167 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19168 info_ptr += blk->size;
19169 DW_BLOCK (attr) = blk;
19170 break;
19171 case DW_FORM_data1:
19172 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19173 info_ptr += 1;
19174 break;
19175 case DW_FORM_flag:
19176 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19177 info_ptr += 1;
19178 break;
2dc7f7b3
TT
19179 case DW_FORM_flag_present:
19180 DW_UNSND (attr) = 1;
19181 break;
c906108c
SS
19182 case DW_FORM_sdata:
19183 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19184 info_ptr += bytes_read;
19185 break;
19186 case DW_FORM_udata:
18a8505e 19187 case DW_FORM_rnglistx:
c906108c
SS
19188 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19189 info_ptr += bytes_read;
19190 break;
19191 case DW_FORM_ref1:
9c541725 19192 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19193 + read_1_byte (abfd, info_ptr));
c906108c
SS
19194 info_ptr += 1;
19195 break;
19196 case DW_FORM_ref2:
9c541725 19197 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19198 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19199 info_ptr += 2;
19200 break;
19201 case DW_FORM_ref4:
9c541725 19202 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19203 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19204 info_ptr += 4;
19205 break;
613e1657 19206 case DW_FORM_ref8:
9c541725 19207 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19208 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19209 info_ptr += 8;
19210 break;
55f1336d 19211 case DW_FORM_ref_sig8:
ac9ec31b 19212 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19213 info_ptr += 8;
19214 break;
c906108c 19215 case DW_FORM_ref_udata:
9c541725 19216 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19217 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19218 info_ptr += bytes_read;
19219 break;
c906108c 19220 case DW_FORM_indirect:
a8329558
KW
19221 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19222 info_ptr += bytes_read;
43988095
JK
19223 if (form == DW_FORM_implicit_const)
19224 {
19225 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19226 info_ptr += bytes_read;
19227 }
19228 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19229 info_ptr, need_reprocess);
43988095
JK
19230 break;
19231 case DW_FORM_implicit_const:
19232 DW_SND (attr) = implicit_const;
a8329558 19233 break;
336d760d 19234 case DW_FORM_addrx:
3019eac3 19235 case DW_FORM_GNU_addr_index:
18a8505e
AT
19236 *need_reprocess = true;
19237 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19238 info_ptr += bytes_read;
19239 break;
cf532bd1 19240 case DW_FORM_strx:
15f18d14
AT
19241 case DW_FORM_strx1:
19242 case DW_FORM_strx2:
19243 case DW_FORM_strx3:
19244 case DW_FORM_strx4:
3019eac3 19245 case DW_FORM_GNU_str_index:
3019eac3 19246 {
15f18d14
AT
19247 ULONGEST str_index;
19248 if (form == DW_FORM_strx1)
19249 {
19250 str_index = read_1_byte (abfd, info_ptr);
19251 info_ptr += 1;
19252 }
19253 else if (form == DW_FORM_strx2)
19254 {
19255 str_index = read_2_bytes (abfd, info_ptr);
19256 info_ptr += 2;
19257 }
19258 else if (form == DW_FORM_strx3)
19259 {
19260 str_index = read_3_bytes (abfd, info_ptr);
19261 info_ptr += 3;
19262 }
19263 else if (form == DW_FORM_strx4)
19264 {
19265 str_index = read_4_bytes (abfd, info_ptr);
19266 info_ptr += 4;
19267 }
19268 else
19269 {
19270 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19271 info_ptr += bytes_read;
19272 }
18a8505e
AT
19273 *need_reprocess = true;
19274 DW_UNSND (attr) = str_index;
19275 }
3019eac3 19276 break;
c906108c 19277 default:
8a3fe4f8 19278 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19279 dwarf_form_name (form),
19280 bfd_get_filename (abfd));
c906108c 19281 }
28e94949 19282
36586728 19283 /* Super hack. */
cd6c91b4 19284 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19285 attr->form = DW_FORM_GNU_ref_alt;
19286
28e94949
JB
19287 /* We have seen instances where the compiler tried to emit a byte
19288 size attribute of -1 which ended up being encoded as an unsigned
19289 0xffffffff. Although 0xffffffff is technically a valid size value,
19290 an object of this size seems pretty unlikely so we can relatively
19291 safely treat these cases as if the size attribute was invalid and
19292 treat them as zero by default. */
19293 if (attr->name == DW_AT_byte_size
19294 && form == DW_FORM_data4
19295 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19296 {
19297 complaint
b98664d3 19298 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19299 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19300 DW_UNSND (attr) = 0;
19301 }
28e94949 19302
c906108c
SS
19303 return info_ptr;
19304}
19305
a8329558
KW
19306/* Read an attribute described by an abbreviated attribute. */
19307
d521ce57 19308static const gdb_byte *
dee91e82
DE
19309read_attribute (const struct die_reader_specs *reader,
19310 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19311 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19312{
19313 attr->name = abbrev->name;
43988095 19314 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19315 abbrev->implicit_const, info_ptr,
19316 need_reprocess);
a8329558
KW
19317}
19318
43988095
JK
19319/* Return pointer to string at .debug_str offset STR_OFFSET. */
19320
19321static const char *
ed2dc618 19322read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19323 LONGEST str_offset)
43988095 19324{
5989a64e
SM
19325 return dwarf2_per_objfile->per_bfd->str.read_string
19326 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19327}
19328
43988095
JK
19329/* Return pointer to string at .debug_str offset as read from BUF.
19330 BUF is assumed to be in a compilation unit described by CU_HEADER.
19331 Return *BYTES_READ_PTR count of bytes read from BUF. */
19332
d521ce57 19333static const char *
ed2dc618
SM
19334read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19335 const gdb_byte *buf,
cf2c3c16
TT
19336 const struct comp_unit_head *cu_header,
19337 unsigned int *bytes_read_ptr)
19338{
8266302d 19339 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19340
4f44ae6c 19341 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19342}
19343
86c0bb4c 19344/* See read.h. */
43988095 19345
86c0bb4c
TT
19346const char *
19347dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19348 const struct comp_unit_head *cu_header,
19349 unsigned int *bytes_read_ptr)
43988095 19350{
86c0bb4c 19351 bfd *abfd = objfile->obfd;
8266302d 19352 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19353
5989a64e 19354 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19355}
19356
3019eac3 19357/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19358 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19359 ADDR_SIZE is the size of addresses from the CU header. */
19360
19361static CORE_ADDR
ed2dc618 19362read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19363 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19364 int addr_size)
3019eac3
DE
19365{
19366 struct objfile *objfile = dwarf2_per_objfile->objfile;
19367 bfd *abfd = objfile->obfd;
19368 const gdb_byte *info_ptr;
18a8505e 19369 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19370
5989a64e
SM
19371 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19372 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19373 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19374 objfile_name (objfile));
18a8505e 19375 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19376 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19377 error (_("DW_FORM_addr_index pointing outside of "
19378 ".debug_addr section [in module %s]"),
4262abfb 19379 objfile_name (objfile));
5989a64e 19380 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19381 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19382 if (addr_size == 4)
19383 return bfd_get_32 (abfd, info_ptr);
19384 else
19385 return bfd_get_64 (abfd, info_ptr);
19386}
19387
19388/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19389
19390static CORE_ADDR
19391read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19392{
5e22e966 19393 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19394 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19395}
19396
19397/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19398
19399static CORE_ADDR
d521ce57 19400read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19401 unsigned int *bytes_read)
19402{
5e22e966 19403 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19404 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19405
19406 return read_addr_index (cu, addr_index);
19407}
19408
450a1bfc 19409/* See read.h. */
3019eac3
DE
19410
19411CORE_ADDR
82ca3f51
SM
19412dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19413 dwarf2_per_objfile *dwarf2_per_objfile,
19414 unsigned int addr_index)
3019eac3 19415{
3019eac3 19416 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19417 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19418 int addr_size;
19419
3019eac3
DE
19420 /* We need addr_base and addr_size.
19421 If we don't have PER_CU->cu, we have to get it.
19422 Nasty, but the alternative is storing the needed info in PER_CU,
19423 which at this point doesn't seem justified: it's not clear how frequently
19424 it would get used and it would increase the size of every PER_CU.
19425 Entry points like dwarf2_per_cu_addr_size do a similar thing
19426 so we're not in uncharted territory here.
19427 Alas we need to be a bit more complicated as addr_base is contained
19428 in the DIE.
19429
19430 We don't need to read the entire CU(/TU).
19431 We just need the header and top level die.
a1b64ce1 19432
3019eac3 19433 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19434 For now we skip this optimization. */
3019eac3
DE
19435
19436 if (cu != NULL)
19437 {
19438 addr_base = cu->addr_base;
19439 addr_size = cu->header.addr_size;
19440 }
19441 else
19442 {
ab432490 19443 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
19444 addr_base = reader.cu->addr_base;
19445 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19446 }
19447
ed2dc618
SM
19448 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19449 addr_size);
3019eac3
DE
19450}
19451
18a8505e
AT
19452/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19453 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19454 DWO file. */
3019eac3 19455
d521ce57 19456static const char *
18a8505e
AT
19457read_str_index (struct dwarf2_cu *cu,
19458 struct dwarf2_section_info *str_section,
19459 struct dwarf2_section_info *str_offsets_section,
19460 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19461{
5e22e966 19462 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19463 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19464 const char *objf_name = objfile_name (objfile);
3019eac3 19465 bfd *abfd = objfile->obfd;
d521ce57 19466 const gdb_byte *info_ptr;
3019eac3 19467 ULONGEST str_offset;
cf532bd1 19468 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19469
96b79293
TT
19470 str_section->read (objfile);
19471 str_offsets_section->read (objfile);
73869dc2 19472 if (str_section->buffer == NULL)
18a8505e 19473 error (_("%s used without %s section"
9d8780f0 19474 " in CU at offset %s [in module %s]"),
96b79293 19475 form_name, str_section->get_name (),
18a8505e 19476 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19477 if (str_offsets_section->buffer == NULL)
18a8505e 19478 error (_("%s used without %s section"
9d8780f0 19479 " in CU at offset %s [in module %s]"),
96b79293 19480 form_name, str_section->get_name (),
18a8505e 19481 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19482 info_ptr = (str_offsets_section->buffer
18a8505e 19483 + str_offsets_base
3019eac3
DE
19484 + str_index * cu->header.offset_size);
19485 if (cu->header.offset_size == 4)
19486 str_offset = bfd_get_32 (abfd, info_ptr);
19487 else
19488 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19489 if (str_offset >= str_section->size)
57d63ce2 19490 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19491 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19492 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19493 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19494}
19495
18a8505e
AT
19496/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19497
19498static const char *
19499read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19500{
19501 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19502 ? reader->cu->header.addr_size : 0;
19503 return read_str_index (reader->cu,
19504 &reader->dwo_file->sections.str,
19505 &reader->dwo_file->sections.str_offsets,
19506 str_offsets_base, str_index);
19507}
19508
19509/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19510
19511static const char *
19512read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19513{
5e22e966 19514 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19515 const char *objf_name = objfile_name (objfile);
19516 static const char form_name[] = "DW_FORM_GNU_str_index";
19517 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19518
19519 if (!cu->str_offsets_base.has_value ())
19520 error (_("%s used in Fission stub without %s"
19521 " in CU at offset 0x%lx [in module %s]"),
19522 form_name, str_offsets_attr_name,
19523 (long) cu->header.offset_size, objf_name);
19524
19525 return read_str_index (cu,
5e22e966
SM
19526 &cu->per_objfile->per_bfd->str,
19527 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19528 *cu->str_offsets_base, str_index);
19529}
19530
3019eac3
DE
19531/* Return the length of an LEB128 number in BUF. */
19532
19533static int
19534leb128_size (const gdb_byte *buf)
19535{
19536 const gdb_byte *begin = buf;
19537 gdb_byte byte;
19538
19539 while (1)
19540 {
19541 byte = *buf++;
19542 if ((byte & 128) == 0)
19543 return buf - begin;
19544 }
19545}
19546
c906108c 19547static void
e142c38c 19548set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19549{
19550 switch (lang)
19551 {
19552 case DW_LANG_C89:
76bee0cc 19553 case DW_LANG_C99:
0cfd832f 19554 case DW_LANG_C11:
c906108c 19555 case DW_LANG_C:
d1be3247 19556 case DW_LANG_UPC:
e142c38c 19557 cu->language = language_c;
c906108c 19558 break;
9c37b5ae 19559 case DW_LANG_Java:
c906108c 19560 case DW_LANG_C_plus_plus:
0cfd832f
MW
19561 case DW_LANG_C_plus_plus_11:
19562 case DW_LANG_C_plus_plus_14:
e142c38c 19563 cu->language = language_cplus;
c906108c 19564 break;
6aecb9c2
JB
19565 case DW_LANG_D:
19566 cu->language = language_d;
19567 break;
c906108c
SS
19568 case DW_LANG_Fortran77:
19569 case DW_LANG_Fortran90:
b21b22e0 19570 case DW_LANG_Fortran95:
f7de9aab
MW
19571 case DW_LANG_Fortran03:
19572 case DW_LANG_Fortran08:
e142c38c 19573 cu->language = language_fortran;
c906108c 19574 break;
a766d390
DE
19575 case DW_LANG_Go:
19576 cu->language = language_go;
19577 break;
c906108c 19578 case DW_LANG_Mips_Assembler:
e142c38c 19579 cu->language = language_asm;
c906108c
SS
19580 break;
19581 case DW_LANG_Ada83:
8aaf0b47 19582 case DW_LANG_Ada95:
bc5f45f8
JB
19583 cu->language = language_ada;
19584 break;
72019c9c
GM
19585 case DW_LANG_Modula2:
19586 cu->language = language_m2;
19587 break;
fe8e67fd
PM
19588 case DW_LANG_Pascal83:
19589 cu->language = language_pascal;
19590 break;
22566fbd
DJ
19591 case DW_LANG_ObjC:
19592 cu->language = language_objc;
19593 break;
c44af4eb
TT
19594 case DW_LANG_Rust:
19595 case DW_LANG_Rust_old:
19596 cu->language = language_rust;
19597 break;
c906108c
SS
19598 case DW_LANG_Cobol74:
19599 case DW_LANG_Cobol85:
c906108c 19600 default:
e142c38c 19601 cu->language = language_minimal;
c906108c
SS
19602 break;
19603 }
e142c38c 19604 cu->language_defn = language_def (cu->language);
c906108c
SS
19605}
19606
19607/* Return the named attribute or NULL if not there. */
19608
19609static struct attribute *
e142c38c 19610dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19611{
a48e046c 19612 for (;;)
c906108c 19613 {
a48e046c
TT
19614 unsigned int i;
19615 struct attribute *spec = NULL;
19616
19617 for (i = 0; i < die->num_attrs; ++i)
19618 {
19619 if (die->attrs[i].name == name)
19620 return &die->attrs[i];
19621 if (die->attrs[i].name == DW_AT_specification
19622 || die->attrs[i].name == DW_AT_abstract_origin)
19623 spec = &die->attrs[i];
19624 }
19625
19626 if (!spec)
19627 break;
c906108c 19628
f2f0e013 19629 die = follow_die_ref (die, spec, &cu);
f2f0e013 19630 }
c5aa993b 19631
c906108c
SS
19632 return NULL;
19633}
19634
7d45c7c3
KB
19635/* Return the string associated with a string-typed attribute, or NULL if it
19636 is either not found or is of an incorrect type. */
19637
19638static const char *
19639dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19640{
19641 struct attribute *attr;
19642 const char *str = NULL;
19643
19644 attr = dwarf2_attr (die, name, cu);
19645
19646 if (attr != NULL)
19647 {
e61108c9
TT
19648 str = attr->value_as_string ();
19649 if (str == nullptr)
b98664d3 19650 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19651 "DIE at %s in module %s"),
19652 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19653 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19654 }
19655
19656 return str;
19657}
19658
a084a2a6 19659/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19660 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19661static const char *
19662dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19663{
19664 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19665 if (dwo_name == nullptr)
19666 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19667 return dwo_name;
19668}
19669
05cf31d1
JB
19670/* Return non-zero iff the attribute NAME is defined for the given DIE,
19671 and holds a non-zero value. This function should only be used for
2dc7f7b3 19672 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19673
19674static int
19675dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19676{
19677 struct attribute *attr = dwarf2_attr (die, name, cu);
19678
19679 return (attr && DW_UNSND (attr));
19680}
19681
3ca72b44 19682static int
e142c38c 19683die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19684{
05cf31d1
JB
19685 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19686 which value is non-zero. However, we have to be careful with
19687 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19688 (via dwarf2_flag_true_p) follows this attribute. So we may
19689 end up accidently finding a declaration attribute that belongs
19690 to a different DIE referenced by the specification attribute,
19691 even though the given DIE does not have a declaration attribute. */
19692 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19693 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19694}
19695
63d06c5c 19696/* Return the die giving the specification for DIE, if there is
f2f0e013 19697 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19698 containing the return value on output. If there is no
19699 specification, but there is an abstract origin, that is
19700 returned. */
63d06c5c
DC
19701
19702static struct die_info *
f2f0e013 19703die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19704{
f2f0e013
DJ
19705 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19706 *spec_cu);
63d06c5c 19707
edb3359d
DJ
19708 if (spec_attr == NULL)
19709 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19710
63d06c5c
DC
19711 if (spec_attr == NULL)
19712 return NULL;
19713 else
f2f0e013 19714 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19715}
c906108c 19716
527f3840
JK
19717/* Stub for free_line_header to match void * callback types. */
19718
19719static void
19720free_line_header_voidp (void *arg)
19721{
9a3c8263 19722 struct line_header *lh = (struct line_header *) arg;
527f3840 19723
fff8551c 19724 delete lh;
527f3840
JK
19725}
19726
83769d0b 19727/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19728
19729static struct dwarf2_section_info *
19730get_debug_line_section (struct dwarf2_cu *cu)
19731{
19732 struct dwarf2_section_info *section;
5e22e966 19733 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19734
19735 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19736 DWO file. */
19737 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19738 section = &cu->dwo_unit->dwo_file->sections.line;
19739 else if (cu->per_cu->is_dwz)
19740 {
c3699833 19741 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19742
19743 section = &dwz->line;
19744 }
19745 else
5989a64e 19746 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19747
19748 return section;
19749}
19750
debd256d 19751/* Read the statement program header starting at OFFSET in
3019eac3 19752 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19753 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19754 Returns NULL if there is a problem reading the header, e.g., if it
19755 has a version we don't understand.
debd256d
JB
19756
19757 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19758 the returned object point into the dwarf line section buffer,
19759 and must not be freed. */
ae2de4f8 19760
fff8551c 19761static line_header_up
9c541725 19762dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19763{
3019eac3 19764 struct dwarf2_section_info *section;
5e22e966 19765 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19766
36586728 19767 section = get_debug_line_section (cu);
96b79293 19768 section->read (dwarf2_per_objfile->objfile);
3019eac3 19769 if (section->buffer == NULL)
debd256d 19770 {
3019eac3 19771 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19772 complaint (_("missing .debug_line.dwo section"));
3019eac3 19773 else
b98664d3 19774 complaint (_("missing .debug_line section"));
debd256d
JB
19775 return 0;
19776 }
19777
0df7ad3a
TT
19778 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19779 dwarf2_per_objfile, section,
19780 &cu->header);
debd256d 19781}
c906108c 19782
c6da4cef 19783/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19784 Return the file name of the psymtab for the given file_entry.
c6da4cef 19785 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19786 If space for the result is malloc'd, *NAME_HOLDER will be set.
19787 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19788
d521ce57 19789static const char *
7ba99d21 19790psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19791 const dwarf2_psymtab *pst,
c89b44cd
TT
19792 const char *comp_dir,
19793 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19794{
d521ce57
TT
19795 const char *include_name = fe.name;
19796 const char *include_name_to_compare = include_name;
72b9f47f 19797 const char *pst_filename;
c6da4cef
DE
19798 int file_is_pst;
19799
8c43009f 19800 const char *dir_name = fe.include_dir (lh);
c6da4cef 19801
c89b44cd 19802 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19803 if (!IS_ABSOLUTE_PATH (include_name)
19804 && (dir_name != NULL || comp_dir != NULL))
19805 {
19806 /* Avoid creating a duplicate psymtab for PST.
19807 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19808 Before we do the comparison, however, we need to account
19809 for DIR_NAME and COMP_DIR.
19810 First prepend dir_name (if non-NULL). If we still don't
19811 have an absolute path prepend comp_dir (if non-NULL).
19812 However, the directory we record in the include-file's
19813 psymtab does not contain COMP_DIR (to match the
19814 corresponding symtab(s)).
19815
19816 Example:
19817
19818 bash$ cd /tmp
19819 bash$ gcc -g ./hello.c
19820 include_name = "hello.c"
19821 dir_name = "."
19822 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19823 DW_AT_name = "./hello.c"
19824
19825 */
c6da4cef
DE
19826
19827 if (dir_name != NULL)
19828 {
c89b44cd
TT
19829 name_holder->reset (concat (dir_name, SLASH_STRING,
19830 include_name, (char *) NULL));
19831 include_name = name_holder->get ();
c6da4cef 19832 include_name_to_compare = include_name;
c6da4cef
DE
19833 }
19834 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19835 {
c89b44cd
TT
19836 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19837 include_name, (char *) NULL));
19838 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19839 }
19840 }
19841
19842 pst_filename = pst->filename;
c89b44cd 19843 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19844 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19845 {
c89b44cd
TT
19846 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19847 pst_filename, (char *) NULL));
19848 pst_filename = copied_name.get ();
c6da4cef
DE
19849 }
19850
1e3fad37 19851 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19852
c6da4cef
DE
19853 if (file_is_pst)
19854 return NULL;
19855 return include_name;
19856}
19857
d9b3de22
DE
19858/* State machine to track the state of the line number program. */
19859
6f77053d 19860class lnp_state_machine
d9b3de22 19861{
6f77053d
PA
19862public:
19863 /* Initialize a machine state for the start of a line number
19864 program. */
804d2729
TT
19865 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19866 bool record_lines_p);
6f77053d 19867
8c43009f
PA
19868 file_entry *current_file ()
19869 {
19870 /* lh->file_names is 0-based, but the file name numbers in the
19871 statement program are 1-based. */
6f77053d
PA
19872 return m_line_header->file_name_at (m_file);
19873 }
19874
19875 /* Record the line in the state machine. END_SEQUENCE is true if
19876 we're processing the end of a sequence. */
19877 void record_line (bool end_sequence);
19878
7ab6656f
OJ
19879 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19880 nop-out rest of the lines in this sequence. */
6f77053d
PA
19881 void check_line_address (struct dwarf2_cu *cu,
19882 const gdb_byte *line_ptr,
7ab6656f 19883 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19884
19885 void handle_set_discriminator (unsigned int discriminator)
19886 {
19887 m_discriminator = discriminator;
19888 m_line_has_non_zero_discriminator |= discriminator != 0;
19889 }
19890
19891 /* Handle DW_LNE_set_address. */
19892 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19893 {
19894 m_op_index = 0;
19895 address += baseaddr;
19896 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19897 }
19898
19899 /* Handle DW_LNS_advance_pc. */
19900 void handle_advance_pc (CORE_ADDR adjust);
19901
19902 /* Handle a special opcode. */
19903 void handle_special_opcode (unsigned char op_code);
19904
19905 /* Handle DW_LNS_advance_line. */
19906 void handle_advance_line (int line_delta)
19907 {
19908 advance_line (line_delta);
19909 }
19910
19911 /* Handle DW_LNS_set_file. */
19912 void handle_set_file (file_name_index file);
19913
19914 /* Handle DW_LNS_negate_stmt. */
19915 void handle_negate_stmt ()
19916 {
19917 m_is_stmt = !m_is_stmt;
19918 }
19919
19920 /* Handle DW_LNS_const_add_pc. */
19921 void handle_const_add_pc ();
19922
19923 /* Handle DW_LNS_fixed_advance_pc. */
19924 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19925 {
19926 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19927 m_op_index = 0;
19928 }
19929
19930 /* Handle DW_LNS_copy. */
19931 void handle_copy ()
19932 {
19933 record_line (false);
19934 m_discriminator = 0;
19935 }
19936
19937 /* Handle DW_LNE_end_sequence. */
19938 void handle_end_sequence ()
19939 {
804d2729 19940 m_currently_recording_lines = true;
6f77053d
PA
19941 }
19942
19943private:
19944 /* Advance the line by LINE_DELTA. */
19945 void advance_line (int line_delta)
19946 {
19947 m_line += line_delta;
19948
19949 if (line_delta != 0)
19950 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19951 }
19952
804d2729
TT
19953 struct dwarf2_cu *m_cu;
19954
6f77053d
PA
19955 gdbarch *m_gdbarch;
19956
19957 /* True if we're recording lines.
19958 Otherwise we're building partial symtabs and are just interested in
19959 finding include files mentioned by the line number program. */
19960 bool m_record_lines_p;
19961
8c43009f 19962 /* The line number header. */
6f77053d 19963 line_header *m_line_header;
8c43009f 19964
6f77053d
PA
19965 /* These are part of the standard DWARF line number state machine,
19966 and initialized according to the DWARF spec. */
d9b3de22 19967
6f77053d 19968 unsigned char m_op_index = 0;
7ba99d21
AT
19969 /* The line table index of the current file. */
19970 file_name_index m_file = 1;
6f77053d
PA
19971 unsigned int m_line = 1;
19972
19973 /* These are initialized in the constructor. */
19974
19975 CORE_ADDR m_address;
19976 bool m_is_stmt;
19977 unsigned int m_discriminator;
d9b3de22
DE
19978
19979 /* Additional bits of state we need to track. */
19980
19981 /* The last file that we called dwarf2_start_subfile for.
19982 This is only used for TLLs. */
6f77053d 19983 unsigned int m_last_file = 0;
d9b3de22 19984 /* The last file a line number was recorded for. */
6f77053d 19985 struct subfile *m_last_subfile = NULL;
d9b3de22 19986
804d2729
TT
19987 /* When true, record the lines we decode. */
19988 bool m_currently_recording_lines = false;
d9b3de22
DE
19989
19990 /* The last line number that was recorded, used to coalesce
19991 consecutive entries for the same line. This can happen, for
19992 example, when discriminators are present. PR 17276. */
6f77053d
PA
19993 unsigned int m_last_line = 0;
19994 bool m_line_has_non_zero_discriminator = false;
8c43009f 19995};
d9b3de22 19996
6f77053d
PA
19997void
19998lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19999{
20000 CORE_ADDR addr_adj = (((m_op_index + adjust)
20001 / m_line_header->maximum_ops_per_instruction)
20002 * m_line_header->minimum_instruction_length);
20003 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20004 m_op_index = ((m_op_index + adjust)
20005 % m_line_header->maximum_ops_per_instruction);
20006}
d9b3de22 20007
6f77053d
PA
20008void
20009lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20010{
6f77053d 20011 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20012 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20013 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20014 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20015 / m_line_header->maximum_ops_per_instruction)
20016 * m_line_header->minimum_instruction_length);
20017 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20018 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20019 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20020
258bf0ee 20021 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20022 advance_line (line_delta);
20023 record_line (false);
20024 m_discriminator = 0;
20025}
d9b3de22 20026
6f77053d
PA
20027void
20028lnp_state_machine::handle_set_file (file_name_index file)
20029{
20030 m_file = file;
20031
20032 const file_entry *fe = current_file ();
20033 if (fe == NULL)
20034 dwarf2_debug_line_missing_file_complaint ();
20035 else if (m_record_lines_p)
20036 {
20037 const char *dir = fe->include_dir (m_line_header);
20038
c24bdb02 20039 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20040 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20041 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20042 }
20043}
20044
20045void
20046lnp_state_machine::handle_const_add_pc ()
20047{
20048 CORE_ADDR adjust
20049 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20050
20051 CORE_ADDR addr_adj
20052 = (((m_op_index + adjust)
20053 / m_line_header->maximum_ops_per_instruction)
20054 * m_line_header->minimum_instruction_length);
20055
20056 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20057 m_op_index = ((m_op_index + adjust)
20058 % m_line_header->maximum_ops_per_instruction);
20059}
d9b3de22 20060
a05a36a5
DE
20061/* Return non-zero if we should add LINE to the line number table.
20062 LINE is the line to add, LAST_LINE is the last line that was added,
20063 LAST_SUBFILE is the subfile for LAST_LINE.
20064 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20065 had a non-zero discriminator.
20066
20067 We have to be careful in the presence of discriminators.
20068 E.g., for this line:
20069
20070 for (i = 0; i < 100000; i++);
20071
20072 clang can emit four line number entries for that one line,
20073 each with a different discriminator.
20074 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20075
20076 However, we want gdb to coalesce all four entries into one.
20077 Otherwise the user could stepi into the middle of the line and
20078 gdb would get confused about whether the pc really was in the
20079 middle of the line.
20080
20081 Things are further complicated by the fact that two consecutive
20082 line number entries for the same line is a heuristic used by gcc
20083 to denote the end of the prologue. So we can't just discard duplicate
20084 entries, we have to be selective about it. The heuristic we use is
20085 that we only collapse consecutive entries for the same line if at least
20086 one of those entries has a non-zero discriminator. PR 17276.
20087
20088 Note: Addresses in the line number state machine can never go backwards
20089 within one sequence, thus this coalescing is ok. */
20090
20091static int
804d2729
TT
20092dwarf_record_line_p (struct dwarf2_cu *cu,
20093 unsigned int line, unsigned int last_line,
a05a36a5
DE
20094 int line_has_non_zero_discriminator,
20095 struct subfile *last_subfile)
20096{
c24bdb02 20097 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20098 return 1;
20099 if (line != last_line)
20100 return 1;
20101 /* Same line for the same file that we've seen already.
20102 As a last check, for pr 17276, only record the line if the line
20103 has never had a non-zero discriminator. */
20104 if (!line_has_non_zero_discriminator)
20105 return 1;
20106 return 0;
20107}
20108
804d2729
TT
20109/* Use the CU's builder to record line number LINE beginning at
20110 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20111
20112static void
d9b3de22 20113dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20114 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20115 struct dwarf2_cu *cu)
252a6764
DE
20116{
20117 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20118
27e0867f
DE
20119 if (dwarf_line_debug)
20120 {
20121 fprintf_unfiltered (gdb_stdlog,
20122 "Recording line %u, file %s, address %s\n",
20123 line, lbasename (subfile->name),
20124 paddress (gdbarch, address));
20125 }
20126
804d2729 20127 if (cu != nullptr)
8c95582d 20128 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20129}
20130
20131/* Subroutine of dwarf_decode_lines_1 to simplify it.
20132 Mark the end of a set of line number records.
d9b3de22 20133 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20134 If SUBFILE is NULL the request is ignored. */
20135
20136static void
20137dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20138 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20139{
27e0867f
DE
20140 if (subfile == NULL)
20141 return;
20142
20143 if (dwarf_line_debug)
20144 {
20145 fprintf_unfiltered (gdb_stdlog,
20146 "Finishing current line, file %s, address %s\n",
20147 lbasename (subfile->name),
20148 paddress (gdbarch, address));
20149 }
20150
8c95582d 20151 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20152}
20153
6f77053d
PA
20154void
20155lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20156{
d9b3de22
DE
20157 if (dwarf_line_debug)
20158 {
20159 fprintf_unfiltered (gdb_stdlog,
20160 "Processing actual line %u: file %u,"
94a72be7 20161 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20162 m_line, m_file,
6f77053d 20163 paddress (m_gdbarch, m_address),
94a72be7
AB
20164 m_is_stmt, m_discriminator,
20165 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20166 }
20167
6f77053d 20168 file_entry *fe = current_file ();
8c43009f
PA
20169
20170 if (fe == NULL)
d9b3de22
DE
20171 dwarf2_debug_line_missing_file_complaint ();
20172 /* For now we ignore lines not starting on an instruction boundary.
20173 But not when processing end_sequence for compatibility with the
20174 previous version of the code. */
6f77053d 20175 else if (m_op_index == 0 || end_sequence)
d9b3de22 20176 {
8c43009f 20177 fe->included_p = 1;
8c95582d 20178 if (m_record_lines_p)
d9b3de22 20179 {
c24bdb02 20180 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20181 || end_sequence)
d9b3de22 20182 {
804d2729
TT
20183 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20184 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20185 }
20186
20187 if (!end_sequence)
20188 {
8c95582d
AB
20189 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20190
804d2729 20191 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20192 m_line_has_non_zero_discriminator,
20193 m_last_subfile))
d9b3de22 20194 {
c24bdb02 20195 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20196 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20197 builder->get_current_subfile (),
8c95582d 20198 m_line, m_address, is_stmt,
804d2729 20199 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20200 }
c24bdb02 20201 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20202 m_last_line = m_line;
d9b3de22
DE
20203 }
20204 }
20205 }
20206}
20207
804d2729
TT
20208lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20209 line_header *lh, bool record_lines_p)
d9b3de22 20210{
804d2729 20211 m_cu = cu;
6f77053d
PA
20212 m_gdbarch = arch;
20213 m_record_lines_p = record_lines_p;
20214 m_line_header = lh;
d9b3de22 20215
804d2729 20216 m_currently_recording_lines = true;
d9b3de22 20217
d9b3de22
DE
20218 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20219 was a line entry for it so that the backend has a chance to adjust it
20220 and also record it in case it needs it. This is currently used by MIPS
20221 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20222 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20223 m_is_stmt = lh->default_is_stmt;
20224 m_discriminator = 0;
252a6764
DE
20225}
20226
6f77053d
PA
20227void
20228lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20229 const gdb_byte *line_ptr,
7ab6656f 20230 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20231{
7ab6656f
OJ
20232 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20233 the pc range of the CU. However, we restrict the test to only ADDRESS
20234 values of zero to preserve GDB's previous behaviour which is to handle
20235 the specific case of a function being GC'd by the linker. */
924c2928 20236
7ab6656f 20237 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20238 {
20239 /* This line table is for a function which has been
20240 GCd by the linker. Ignore it. PR gdb/12528 */
20241
5e22e966 20242 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20243 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20244
b98664d3 20245 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20246 line_offset, objfile_name (objfile));
804d2729
TT
20247 m_currently_recording_lines = false;
20248 /* Note: m_currently_recording_lines is left as false until we see
20249 DW_LNE_end_sequence. */
924c2928
DE
20250 }
20251}
20252
f3f5162e 20253/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20254 Process the line number information in LH.
20255 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20256 program in order to set included_p for every referenced header. */
debd256d 20257
c906108c 20258static void
43f3e411
DE
20259dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20260 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20261{
d521ce57
TT
20262 const gdb_byte *line_ptr, *extended_end;
20263 const gdb_byte *line_end;
a8c50c1f 20264 unsigned int bytes_read, extended_len;
699ca60a 20265 unsigned char op_code, extended_op;
e142c38c 20266 CORE_ADDR baseaddr;
5e22e966 20267 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20268 bfd *abfd = objfile->obfd;
08feed99 20269 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20270 /* True if we're recording line info (as opposed to building partial
20271 symtabs and just interested in finding include files mentioned by
20272 the line number program). */
20273 bool record_lines_p = !decode_for_pst_p;
e142c38c 20274
b3b3bada 20275 baseaddr = objfile->text_section_offset ();
c906108c 20276
debd256d
JB
20277 line_ptr = lh->statement_program_start;
20278 line_end = lh->statement_program_end;
c906108c
SS
20279
20280 /* Read the statement sequences until there's nothing left. */
20281 while (line_ptr < line_end)
20282 {
6f77053d
PA
20283 /* The DWARF line number program state machine. Reset the state
20284 machine at the start of each sequence. */
804d2729 20285 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20286 bool end_sequence = false;
d9b3de22 20287
8c43009f 20288 if (record_lines_p)
c906108c 20289 {
8c43009f
PA
20290 /* Start a subfile for the current file of the state
20291 machine. */
20292 const file_entry *fe = state_machine.current_file ();
20293
20294 if (fe != NULL)
804d2729 20295 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20296 }
20297
a738430d 20298 /* Decode the table. */
d9b3de22 20299 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20300 {
20301 op_code = read_1_byte (abfd, line_ptr);
20302 line_ptr += 1;
9aa1fe7e 20303
debd256d 20304 if (op_code >= lh->opcode_base)
6e70227d 20305 {
8e07a239 20306 /* Special opcode. */
6f77053d 20307 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20308 }
20309 else switch (op_code)
c906108c
SS
20310 {
20311 case DW_LNS_extended_op:
3e43a32a
MS
20312 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20313 &bytes_read);
473b7be6 20314 line_ptr += bytes_read;
a8c50c1f 20315 extended_end = line_ptr + extended_len;
c906108c
SS
20316 extended_op = read_1_byte (abfd, line_ptr);
20317 line_ptr += 1;
20318 switch (extended_op)
20319 {
20320 case DW_LNE_end_sequence:
6f77053d
PA
20321 state_machine.handle_end_sequence ();
20322 end_sequence = true;
c906108c
SS
20323 break;
20324 case DW_LNE_set_address:
d9b3de22
DE
20325 {
20326 CORE_ADDR address
c8a7a66f 20327 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20328 line_ptr += bytes_read;
6f77053d
PA
20329
20330 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20331 lowpc - baseaddr, address);
6f77053d 20332 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20333 }
c906108c
SS
20334 break;
20335 case DW_LNE_define_file:
debd256d 20336 {
d521ce57 20337 const char *cur_file;
ecfb656c
PA
20338 unsigned int mod_time, length;
20339 dir_index dindex;
6e70227d 20340
3e43a32a
MS
20341 cur_file = read_direct_string (abfd, line_ptr,
20342 &bytes_read);
debd256d 20343 line_ptr += bytes_read;
ecfb656c 20344 dindex = (dir_index)
debd256d
JB
20345 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20346 line_ptr += bytes_read;
20347 mod_time =
20348 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20349 line_ptr += bytes_read;
20350 length =
20351 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20352 line_ptr += bytes_read;
ecfb656c 20353 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20354 }
c906108c 20355 break;
d0c6ba3d 20356 case DW_LNE_set_discriminator:
6f77053d
PA
20357 {
20358 /* The discriminator is not interesting to the
20359 debugger; just ignore it. We still need to
20360 check its value though:
20361 if there are consecutive entries for the same
20362 (non-prologue) line we want to coalesce them.
20363 PR 17276. */
20364 unsigned int discr
20365 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20366 line_ptr += bytes_read;
20367
20368 state_machine.handle_set_discriminator (discr);
20369 }
d0c6ba3d 20370 break;
c906108c 20371 default:
b98664d3 20372 complaint (_("mangled .debug_line section"));
debd256d 20373 return;
c906108c 20374 }
a8c50c1f
DJ
20375 /* Make sure that we parsed the extended op correctly. If e.g.
20376 we expected a different address size than the producer used,
20377 we may have read the wrong number of bytes. */
20378 if (line_ptr != extended_end)
20379 {
b98664d3 20380 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20381 return;
20382 }
c906108c
SS
20383 break;
20384 case DW_LNS_copy:
6f77053d 20385 state_machine.handle_copy ();
c906108c
SS
20386 break;
20387 case DW_LNS_advance_pc:
2dc7f7b3
TT
20388 {
20389 CORE_ADDR adjust
20390 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20391 line_ptr += bytes_read;
6f77053d
PA
20392
20393 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20394 }
c906108c
SS
20395 break;
20396 case DW_LNS_advance_line:
a05a36a5
DE
20397 {
20398 int line_delta
20399 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20400 line_ptr += bytes_read;
6f77053d
PA
20401
20402 state_machine.handle_advance_line (line_delta);
a05a36a5 20403 }
c906108c
SS
20404 break;
20405 case DW_LNS_set_file:
d9b3de22 20406 {
6f77053d 20407 file_name_index file
ecfb656c
PA
20408 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20409 &bytes_read);
d9b3de22 20410 line_ptr += bytes_read;
8c43009f 20411
6f77053d 20412 state_machine.handle_set_file (file);
d9b3de22 20413 }
c906108c
SS
20414 break;
20415 case DW_LNS_set_column:
0ad93d4f 20416 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20417 line_ptr += bytes_read;
20418 break;
20419 case DW_LNS_negate_stmt:
6f77053d 20420 state_machine.handle_negate_stmt ();
c906108c
SS
20421 break;
20422 case DW_LNS_set_basic_block:
c906108c 20423 break;
c2c6d25f
JM
20424 /* Add to the address register of the state machine the
20425 address increment value corresponding to special opcode
a738430d
MK
20426 255. I.e., this value is scaled by the minimum
20427 instruction length since special opcode 255 would have
b021a221 20428 scaled the increment. */
c906108c 20429 case DW_LNS_const_add_pc:
6f77053d 20430 state_machine.handle_const_add_pc ();
c906108c
SS
20431 break;
20432 case DW_LNS_fixed_advance_pc:
3e29f34a 20433 {
6f77053d 20434 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20435 line_ptr += 2;
6f77053d
PA
20436
20437 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20438 }
c906108c 20439 break;
9aa1fe7e 20440 default:
a738430d
MK
20441 {
20442 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20443 int i;
a738430d 20444
debd256d 20445 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20446 {
20447 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20448 line_ptr += bytes_read;
20449 }
20450 }
c906108c
SS
20451 }
20452 }
d9b3de22
DE
20453
20454 if (!end_sequence)
20455 dwarf2_debug_line_missing_end_sequence_complaint ();
20456
20457 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20458 in which case we still finish recording the last line). */
6f77053d 20459 state_machine.record_line (true);
c906108c 20460 }
f3f5162e
DE
20461}
20462
20463/* Decode the Line Number Program (LNP) for the given line_header
20464 structure and CU. The actual information extracted and the type
20465 of structures created from the LNP depends on the value of PST.
20466
20467 1. If PST is NULL, then this procedure uses the data from the program
20468 to create all necessary symbol tables, and their linetables.
20469
20470 2. If PST is not NULL, this procedure reads the program to determine
20471 the list of files included by the unit represented by PST, and
20472 builds all the associated partial symbol tables.
20473
20474 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20475 It is used for relative paths in the line table.
20476 NOTE: When processing partial symtabs (pst != NULL),
20477 comp_dir == pst->dirname.
20478
20479 NOTE: It is important that psymtabs have the same file name (via strcmp)
20480 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20481 symtab we don't use it in the name of the psymtabs we create.
20482 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20483 A good testcase for this is mb-inline.exp.
20484
527f3840
JK
20485 LOWPC is the lowest address in CU (or 0 if not known).
20486
20487 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20488 for its PC<->lines mapping information. Otherwise only the filename
20489 table is read in. */
f3f5162e
DE
20490
20491static void
20492dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20493 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20494 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20495{
5e22e966 20496 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20497 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20498
527f3840
JK
20499 if (decode_mapping)
20500 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20501
20502 if (decode_for_pst_p)
20503 {
aaa75496
JB
20504 /* Now that we're done scanning the Line Header Program, we can
20505 create the psymtab of each included file. */
7ba99d21
AT
20506 for (auto &file_entry : lh->file_names ())
20507 if (file_entry.included_p == 1)
aaa75496 20508 {
c89b44cd 20509 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20510 const char *include_name =
7ba99d21
AT
20511 psymtab_include_file_name (lh, file_entry, pst,
20512 comp_dir, &name_holder);
c6da4cef 20513 if (include_name != NULL)
aaa75496
JB
20514 dwarf2_create_include_psymtab (include_name, pst, objfile);
20515 }
20516 }
cb1df416
DJ
20517 else
20518 {
20519 /* Make sure a symtab is created for every file, even files
20520 which contain only variables (i.e. no code with associated
20521 line numbers). */
c24bdb02
KS
20522 buildsym_compunit *builder = cu->get_builder ();
20523 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20524
7ba99d21 20525 for (auto &fe : lh->file_names ())
cb1df416 20526 {
804d2729 20527 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20528 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20529 {
c24bdb02 20530 builder->get_current_subfile ()->symtab
804d2729 20531 = allocate_symtab (cust,
c24bdb02 20532 builder->get_current_subfile ()->name);
43f3e411 20533 }
c24bdb02 20534 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20535 }
20536 }
c906108c
SS
20537}
20538
20539/* Start a subfile for DWARF. FILENAME is the name of the file and
20540 DIRNAME the name of the source directory which contains FILENAME
4d663531 20541 or NULL if not known.
c906108c
SS
20542 This routine tries to keep line numbers from identical absolute and
20543 relative file names in a common subfile.
20544
20545 Using the `list' example from the GDB testsuite, which resides in
20546 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20547 of /srcdir/list0.c yields the following debugging information for list0.c:
20548
c5aa993b 20549 DW_AT_name: /srcdir/list0.c
4d663531 20550 DW_AT_comp_dir: /compdir
357e46e7 20551 files.files[0].name: list0.h
c5aa993b 20552 files.files[0].dir: /srcdir
357e46e7 20553 files.files[1].name: list0.c
c5aa993b 20554 files.files[1].dir: /srcdir
c906108c
SS
20555
20556 The line number information for list0.c has to end up in a single
4f1520fb
FR
20557 subfile, so that `break /srcdir/list0.c:1' works as expected.
20558 start_subfile will ensure that this happens provided that we pass the
20559 concatenation of files.files[1].dir and files.files[1].name as the
20560 subfile's name. */
c906108c
SS
20561
20562static void
804d2729
TT
20563dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20564 const char *dirname)
c906108c 20565{
43816ebc 20566 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20567
4d663531 20568 /* In order not to lose the line information directory,
4f1520fb
FR
20569 we concatenate it to the filename when it makes sense.
20570 Note that the Dwarf3 standard says (speaking of filenames in line
20571 information): ``The directory index is ignored for file names
20572 that represent full path names''. Thus ignoring dirname in the
20573 `else' branch below isn't an issue. */
c906108c 20574
d5166ae1 20575 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20576 {
43816ebc
TT
20577 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20578 filename = copy.get ();
d521ce57 20579 }
c906108c 20580
c24bdb02 20581 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20582}
20583
804d2729
TT
20584/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20585 buildsym_compunit constructor. */
f4dc4d17 20586
c24bdb02
KS
20587struct compunit_symtab *
20588dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20589 CORE_ADDR low_pc)
f4dc4d17 20590{
c24bdb02 20591 gdb_assert (m_builder == nullptr);
43f3e411 20592
c24bdb02 20593 m_builder.reset (new struct buildsym_compunit
f6e649dd 20594 (this->per_objfile->objfile,
c24bdb02 20595 name, comp_dir, language, low_pc));
93b8bea4 20596
c24bdb02 20597 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20598
c24bdb02
KS
20599 get_builder ()->record_debugformat ("DWARF 2");
20600 get_builder ()->record_producer (producer);
f4dc4d17 20601
c24bdb02 20602 processing_has_namespace_info = false;
43f3e411 20603
c24bdb02 20604 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20605}
20606
4c2df51b
DJ
20607static void
20608var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20609 struct dwarf2_cu *cu)
4c2df51b 20610{
5e22e966 20611 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20612 struct comp_unit_head *cu_header = &cu->header;
20613
4c2df51b
DJ
20614 /* NOTE drow/2003-01-30: There used to be a comment and some special
20615 code here to turn a symbol with DW_AT_external and a
20616 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20617 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20618 with some versions of binutils) where shared libraries could have
20619 relocations against symbols in their debug information - the
20620 minimal symbol would have the right address, but the debug info
20621 would not. It's no longer necessary, because we will explicitly
20622 apply relocations when we read in the debug information now. */
20623
20624 /* A DW_AT_location attribute with no contents indicates that a
20625 variable has been optimized away. */
4fc6c0d5 20626 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20627 {
f1e6e072 20628 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20629 return;
20630 }
20631
20632 /* Handle one degenerate form of location expression specially, to
20633 preserve GDB's previous behavior when section offsets are
336d760d
AT
20634 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20635 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20636
4fc6c0d5 20637 if (attr->form_is_block ()
3019eac3
DE
20638 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20639 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20640 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20641 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20642 && (DW_BLOCK (attr)->size
20643 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20644 {
891d2f0b 20645 unsigned int dummy;
4c2df51b 20646
3019eac3 20647 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20648 SET_SYMBOL_VALUE_ADDRESS
20649 (sym, cu->header.read_address (objfile->obfd,
20650 DW_BLOCK (attr)->data + 1,
20651 &dummy));
3019eac3 20652 else
38583298
TT
20653 SET_SYMBOL_VALUE_ADDRESS
20654 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20655 &dummy));
f1e6e072 20656 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20657 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20658 SET_SYMBOL_VALUE_ADDRESS
20659 (sym,
20660 SYMBOL_VALUE_ADDRESS (sym)
20661 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20662 return;
20663 }
20664
20665 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20666 expression evaluator, and use LOC_COMPUTED only when necessary
20667 (i.e. when the value of a register or memory location is
20668 referenced, or a thread-local block, etc.). Then again, it might
20669 not be worthwhile. I'm assuming that it isn't unless performance
20670 or memory numbers show me otherwise. */
20671
f1e6e072 20672 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20673
f1e6e072 20674 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20675 cu->has_loclist = true;
4c2df51b
DJ
20676}
20677
c906108c
SS
20678/* Given a pointer to a DWARF information entry, figure out if we need
20679 to make a symbol table entry for it, and if so, create a new entry
20680 and return a pointer to it.
20681 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20682 used the passed type.
20683 If SPACE is not NULL, use it to hold the new symbol. If it is
20684 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20685
20686static struct symbol *
5e2db402
TT
20687new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20688 struct symbol *space)
c906108c 20689{
5e22e966 20690 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20691 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20692 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20693 struct symbol *sym = NULL;
15d034d0 20694 const char *name;
c906108c
SS
20695 struct attribute *attr = NULL;
20696 struct attribute *attr2 = NULL;
e142c38c 20697 CORE_ADDR baseaddr;
e37fd15a
SW
20698 struct pending **list_to_add = NULL;
20699
edb3359d 20700 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20701
b3b3bada 20702 baseaddr = objfile->text_section_offset ();
c906108c 20703
94af9270 20704 name = dwarf2_name (die, cu);
c906108c
SS
20705 if (name)
20706 {
34eaf542 20707 int suppress_add = 0;
94af9270 20708
34eaf542
TT
20709 if (space)
20710 sym = space;
20711 else
8c14c3a3 20712 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20713 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20714
20715 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20716 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20717 /* Fortran does not have mangling standard and the mangling does differ
20718 between gfortran, iFort etc. */
bcfe6157
TT
20719 const char *physname
20720 = (cu->language == language_fortran
20721 ? dwarf2_full_name (name, die, cu)
20722 : dwarf2_physname (name, die, cu));
20723 const char *linkagename = dw2_linkage_name (die, cu);
20724
20725 if (linkagename == nullptr || cu->language == language_ada)
20726 sym->set_linkage_name (physname);
20727 else
20728 {
20729 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20730 sym->set_linkage_name (linkagename);
20731 }
f55ee35c 20732
c906108c 20733 /* Default assumptions.
c5aa993b 20734 Use the passed type or decode it from the die. */
176620f1 20735 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20736 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20737 if (type != NULL)
20738 SYMBOL_TYPE (sym) = type;
20739 else
e7c27a73 20740 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20741 attr = dwarf2_attr (die,
20742 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20743 cu);
435d3d88 20744 if (attr != nullptr)
c906108c
SS
20745 {
20746 SYMBOL_LINE (sym) = DW_UNSND (attr);
20747 }
cb1df416 20748
edb3359d
DJ
20749 attr = dwarf2_attr (die,
20750 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20751 cu);
435d3d88 20752 if (attr != nullptr)
cb1df416 20753 {
ecfb656c 20754 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20755 struct file_entry *fe;
9a619af0 20756
ecfb656c
PA
20757 if (cu->line_header != NULL)
20758 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20759 else
20760 fe = NULL;
20761
20762 if (fe == NULL)
b98664d3 20763 complaint (_("file index out of range"));
8c43009f
PA
20764 else
20765 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20766 }
20767
c906108c
SS
20768 switch (die->tag)
20769 {
20770 case DW_TAG_label:
e142c38c 20771 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20772 if (attr != nullptr)
3e29f34a
MR
20773 {
20774 CORE_ADDR addr;
20775
cd6c91b4 20776 addr = attr->value_as_address ();
3e29f34a 20777 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20778 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20779 }
0f5238ed
TT
20780 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20781 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20782 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20783 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20784 break;
20785 case DW_TAG_subprogram:
20786 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20787 finish_block. */
f1e6e072 20788 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20789 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20790 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20791 || cu->language == language_ada
20792 || cu->language == language_fortran)
c906108c 20793 {
2cfa0c8d 20794 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20795 Ada and Fortran subprograms, whether marked external or
20796 not, are always stored as a global symbol, because we want
20797 to be able to access them globally. For instance, we want
20798 to be able to break on a nested subprogram without having
20799 to specify the context. */
c24bdb02 20800 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20801 }
20802 else
20803 {
e37fd15a 20804 list_to_add = cu->list_in_scope;
c906108c
SS
20805 }
20806 break;
edb3359d
DJ
20807 case DW_TAG_inlined_subroutine:
20808 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20809 finish_block. */
f1e6e072 20810 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20811 SYMBOL_INLINED (sym) = 1;
481860b3 20812 list_to_add = cu->list_in_scope;
edb3359d 20813 break;
34eaf542
TT
20814 case DW_TAG_template_value_param:
20815 suppress_add = 1;
20816 /* Fall through. */
72929c62 20817 case DW_TAG_constant:
c906108c 20818 case DW_TAG_variable:
254e6b9e 20819 case DW_TAG_member:
0963b4bd
MS
20820 /* Compilation with minimal debug info may result in
20821 variables with missing type entries. Change the
20822 misleading `void' type to something sensible. */
78134374 20823 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20824 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20825
e142c38c 20826 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20827 /* In the case of DW_TAG_member, we should only be called for
20828 static const members. */
20829 if (die->tag == DW_TAG_member)
20830 {
3863f96c
DE
20831 /* dwarf2_add_field uses die_is_declaration,
20832 so we do the same. */
254e6b9e
DE
20833 gdb_assert (die_is_declaration (die, cu));
20834 gdb_assert (attr);
20835 }
435d3d88 20836 if (attr != nullptr)
c906108c 20837 {
e7c27a73 20838 dwarf2_const_value (attr, sym, cu);
e142c38c 20839 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20840 if (!suppress_add)
34eaf542
TT
20841 {
20842 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20843 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20844 else
e37fd15a 20845 list_to_add = cu->list_in_scope;
34eaf542 20846 }
c906108c
SS
20847 break;
20848 }
e142c38c 20849 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20850 if (attr != nullptr)
c906108c 20851 {
e7c27a73 20852 var_decode_location (attr, sym, cu);
e142c38c 20853 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20854
20855 /* Fortran explicitly imports any global symbols to the local
20856 scope by DW_TAG_common_block. */
20857 if (cu->language == language_fortran && die->parent
20858 && die->parent->tag == DW_TAG_common_block)
20859 attr2 = NULL;
20860
caac4577
JG
20861 if (SYMBOL_CLASS (sym) == LOC_STATIC
20862 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20863 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20864 {
20865 /* When a static variable is eliminated by the linker,
20866 the corresponding debug information is not stripped
20867 out, but the variable address is set to null;
20868 do not add such variables into symbol table. */
20869 }
20870 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20871 {
4b610737
TT
20872 if (SYMBOL_CLASS (sym) == LOC_STATIC
20873 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20874 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20875 {
20876 /* A global static variable might be subject to
20877 copy relocation. We first check for a local
20878 minsym, though, because maybe the symbol was
20879 marked hidden, in which case this would not
20880 apply. */
20881 bound_minimal_symbol found
20882 = (lookup_minimal_symbol_linkage
987012b8 20883 (sym->linkage_name (), objfile));
4b610737
TT
20884 if (found.minsym != nullptr)
20885 sym->maybe_copied = 1;
20886 }
f55ee35c 20887
1c809c68
TT
20888 /* A variable with DW_AT_external is never static,
20889 but it may be block-scoped. */
804d2729 20890 list_to_add
c24bdb02
KS
20891 = ((cu->list_in_scope
20892 == cu->get_builder ()->get_file_symbols ())
20893 ? cu->get_builder ()->get_global_symbols ()
804d2729 20894 : cu->list_in_scope);
1c809c68 20895 }
c906108c 20896 else
e37fd15a 20897 list_to_add = cu->list_in_scope;
c906108c
SS
20898 }
20899 else
20900 {
20901 /* We do not know the address of this symbol.
c5aa993b
JM
20902 If it is an external symbol and we have type information
20903 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20904 The address of the variable will then be determined from
20905 the minimal symbol table whenever the variable is
20906 referenced. */
e142c38c 20907 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20908
20909 /* Fortran explicitly imports any global symbols to the local
20910 scope by DW_TAG_common_block. */
20911 if (cu->language == language_fortran && die->parent
20912 && die->parent->tag == DW_TAG_common_block)
20913 {
20914 /* SYMBOL_CLASS doesn't matter here because
20915 read_common_block is going to reset it. */
20916 if (!suppress_add)
20917 list_to_add = cu->list_in_scope;
20918 }
20919 else if (attr2 && (DW_UNSND (attr2) != 0)
20920 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20921 {
0fe7935b
DJ
20922 /* A variable with DW_AT_external is never static, but it
20923 may be block-scoped. */
804d2729 20924 list_to_add
c24bdb02
KS
20925 = ((cu->list_in_scope
20926 == cu->get_builder ()->get_file_symbols ())
20927 ? cu->get_builder ()->get_global_symbols ()
804d2729 20928 : cu->list_in_scope);
0fe7935b 20929
f1e6e072 20930 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20931 }
442ddf59
JK
20932 else if (!die_is_declaration (die, cu))
20933 {
20934 /* Use the default LOC_OPTIMIZED_OUT class. */
20935 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20936 if (!suppress_add)
20937 list_to_add = cu->list_in_scope;
442ddf59 20938 }
c906108c
SS
20939 }
20940 break;
20941 case DW_TAG_formal_parameter:
a60f3166
TT
20942 {
20943 /* If we are inside a function, mark this as an argument. If
20944 not, we might be looking at an argument to an inlined function
20945 when we do not have enough information to show inlined frames;
20946 pretend it's a local variable in that case so that the user can
20947 still see it. */
804d2729 20948 struct context_stack *curr
c24bdb02 20949 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20950 if (curr != nullptr && curr->name != nullptr)
20951 SYMBOL_IS_ARGUMENT (sym) = 1;
20952 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20953 if (attr != nullptr)
a60f3166
TT
20954 {
20955 var_decode_location (attr, sym, cu);
20956 }
20957 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20958 if (attr != nullptr)
a60f3166
TT
20959 {
20960 dwarf2_const_value (attr, sym, cu);
20961 }
f346a30d 20962
a60f3166
TT
20963 list_to_add = cu->list_in_scope;
20964 }
c906108c
SS
20965 break;
20966 case DW_TAG_unspecified_parameters:
20967 /* From varargs functions; gdb doesn't seem to have any
20968 interest in this information, so just ignore it for now.
20969 (FIXME?) */
20970 break;
34eaf542
TT
20971 case DW_TAG_template_type_param:
20972 suppress_add = 1;
20973 /* Fall through. */
c906108c 20974 case DW_TAG_class_type:
680b30c7 20975 case DW_TAG_interface_type:
c906108c
SS
20976 case DW_TAG_structure_type:
20977 case DW_TAG_union_type:
72019c9c 20978 case DW_TAG_set_type:
c906108c 20979 case DW_TAG_enumeration_type:
f1e6e072 20980 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20981 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20982
63d06c5c 20983 {
9c37b5ae 20984 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20985 really ever be static objects: otherwise, if you try
20986 to, say, break of a class's method and you're in a file
20987 which doesn't mention that class, it won't work unless
20988 the check for all static symbols in lookup_symbol_aux
20989 saves you. See the OtherFileClass tests in
20990 gdb.c++/namespace.exp. */
20991
e37fd15a 20992 if (!suppress_add)
34eaf542 20993 {
c24bdb02 20994 buildsym_compunit *builder = cu->get_builder ();
804d2729 20995 list_to_add
c24bdb02 20996 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20997 && cu->language == language_cplus
c24bdb02 20998 ? builder->get_global_symbols ()
804d2729 20999 : cu->list_in_scope);
63d06c5c 21000
64382290 21001 /* The semantics of C++ state that "struct foo {
9c37b5ae 21002 ... }" also defines a typedef for "foo". */
64382290 21003 if (cu->language == language_cplus
45280282 21004 || cu->language == language_ada
c44af4eb
TT
21005 || cu->language == language_d
21006 || cu->language == language_rust)
64382290
TT
21007 {
21008 /* The symbol's name is already allocated along
21009 with this objfile, so we don't need to
21010 duplicate it for the type. */
7d93a1e0 21011 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21012 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21013 }
63d06c5c
DC
21014 }
21015 }
c906108c
SS
21016 break;
21017 case DW_TAG_typedef:
f1e6e072 21018 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21019 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21020 list_to_add = cu->list_in_scope;
63d06c5c 21021 break;
c906108c 21022 case DW_TAG_base_type:
a02abb62 21023 case DW_TAG_subrange_type:
f1e6e072 21024 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21025 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21026 list_to_add = cu->list_in_scope;
c906108c
SS
21027 break;
21028 case DW_TAG_enumerator:
e142c38c 21029 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21030 if (attr != nullptr)
c906108c 21031 {
e7c27a73 21032 dwarf2_const_value (attr, sym, cu);
c906108c 21033 }
63d06c5c
DC
21034 {
21035 /* NOTE: carlton/2003-11-10: See comment above in the
21036 DW_TAG_class_type, etc. block. */
21037
804d2729 21038 list_to_add
c24bdb02 21039 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21040 && cu->language == language_cplus
c24bdb02 21041 ? cu->get_builder ()->get_global_symbols ()
804d2729 21042 : cu->list_in_scope);
63d06c5c 21043 }
c906108c 21044 break;
74921315 21045 case DW_TAG_imported_declaration:
5c4e30ca 21046 case DW_TAG_namespace:
f1e6e072 21047 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21048 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21049 break;
530e8392
KB
21050 case DW_TAG_module:
21051 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21052 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21053 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21054 break;
4357ac6c 21055 case DW_TAG_common_block:
f1e6e072 21056 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21057 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21058 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21059 break;
c906108c
SS
21060 default:
21061 /* Not a tag we recognize. Hopefully we aren't processing
21062 trash data, but since we must specifically ignore things
21063 we don't recognize, there is nothing else we should do at
0963b4bd 21064 this point. */
b98664d3 21065 complaint (_("unsupported tag: '%s'"),
4d3c2250 21066 dwarf_tag_name (die->tag));
c906108c
SS
21067 break;
21068 }
df8a16a1 21069
e37fd15a
SW
21070 if (suppress_add)
21071 {
21072 sym->hash_next = objfile->template_symbols;
21073 objfile->template_symbols = sym;
21074 list_to_add = NULL;
21075 }
21076
21077 if (list_to_add != NULL)
d3cb6808 21078 add_symbol_to_list (sym, list_to_add);
e37fd15a 21079
df8a16a1
DJ
21080 /* For the benefit of old versions of GCC, check for anonymous
21081 namespaces based on the demangled name. */
4d4ec4e5 21082 if (!cu->processing_has_namespace_info
94af9270 21083 && cu->language == language_cplus)
c24bdb02 21084 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21085 }
21086 return (sym);
21087}
21088
98bfdba5
PA
21089/* Given an attr with a DW_FORM_dataN value in host byte order,
21090 zero-extend it as appropriate for the symbol's type. The DWARF
21091 standard (v4) is not entirely clear about the meaning of using
21092 DW_FORM_dataN for a constant with a signed type, where the type is
21093 wider than the data. The conclusion of a discussion on the DWARF
21094 list was that this is unspecified. We choose to always zero-extend
21095 because that is the interpretation long in use by GCC. */
c906108c 21096
98bfdba5 21097static gdb_byte *
ff39bb5e 21098dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21099 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21100{
5e22e966 21101 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21102 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21103 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21104 LONGEST l = DW_UNSND (attr);
21105
21106 if (bits < sizeof (*value) * 8)
21107 {
21108 l &= ((LONGEST) 1 << bits) - 1;
21109 *value = l;
21110 }
21111 else if (bits == sizeof (*value) * 8)
21112 *value = l;
21113 else
21114 {
224c3ddb 21115 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21116 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21117 return bytes;
21118 }
21119
21120 return NULL;
21121}
21122
21123/* Read a constant value from an attribute. Either set *VALUE, or if
21124 the value does not fit in *VALUE, set *BYTES - either already
21125 allocated on the objfile obstack, or newly allocated on OBSTACK,
21126 or, set *BATON, if we translated the constant to a location
21127 expression. */
21128
21129static void
ff39bb5e 21130dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21131 const char *name, struct obstack *obstack,
21132 struct dwarf2_cu *cu,
d521ce57 21133 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21134 struct dwarf2_locexpr_baton **baton)
21135{
5e22e966 21136 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21137 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21138 struct comp_unit_head *cu_header = &cu->header;
c906108c 21139 struct dwarf_block *blk;
98bfdba5
PA
21140 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21141 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21142
21143 *value = 0;
21144 *bytes = NULL;
21145 *baton = NULL;
c906108c
SS
21146
21147 switch (attr->form)
21148 {
21149 case DW_FORM_addr:
336d760d 21150 case DW_FORM_addrx:
3019eac3 21151 case DW_FORM_GNU_addr_index:
ac56253d 21152 {
ac56253d
TT
21153 gdb_byte *data;
21154
98bfdba5
PA
21155 if (TYPE_LENGTH (type) != cu_header->addr_size)
21156 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21157 cu_header->addr_size,
98bfdba5 21158 TYPE_LENGTH (type));
ac56253d
TT
21159 /* Symbols of this form are reasonably rare, so we just
21160 piggyback on the existing location code rather than writing
21161 a new implementation of symbol_computed_ops. */
8d749320 21162 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21163 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21164 (*baton)->per_cu = cu->per_cu;
21165 gdb_assert ((*baton)->per_cu);
ac56253d 21166
98bfdba5 21167 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21168 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21169 (*baton)->data = data;
ac56253d
TT
21170
21171 data[0] = DW_OP_addr;
21172 store_unsigned_integer (&data[1], cu_header->addr_size,
21173 byte_order, DW_ADDR (attr));
21174 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21175 }
c906108c 21176 break;
4ac36638 21177 case DW_FORM_string:
93b5768b 21178 case DW_FORM_strp:
cf532bd1 21179 case DW_FORM_strx:
3019eac3 21180 case DW_FORM_GNU_str_index:
36586728 21181 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21182 /* DW_STRING is already allocated on the objfile obstack, point
21183 directly to it. */
d521ce57 21184 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21185 break;
c906108c
SS
21186 case DW_FORM_block1:
21187 case DW_FORM_block2:
21188 case DW_FORM_block4:
21189 case DW_FORM_block:
2dc7f7b3 21190 case DW_FORM_exprloc:
0224619f 21191 case DW_FORM_data16:
c906108c 21192 blk = DW_BLOCK (attr);
98bfdba5
PA
21193 if (TYPE_LENGTH (type) != blk->size)
21194 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21195 TYPE_LENGTH (type));
21196 *bytes = blk->data;
c906108c 21197 break;
2df3850c
JM
21198
21199 /* The DW_AT_const_value attributes are supposed to carry the
21200 symbol's value "represented as it would be on the target
21201 architecture." By the time we get here, it's already been
21202 converted to host endianness, so we just need to sign- or
21203 zero-extend it as appropriate. */
21204 case DW_FORM_data1:
3aef2284 21205 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21206 break;
c906108c 21207 case DW_FORM_data2:
3aef2284 21208 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21209 break;
c906108c 21210 case DW_FORM_data4:
3aef2284 21211 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21212 break;
c906108c 21213 case DW_FORM_data8:
3aef2284 21214 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21215 break;
21216
c906108c 21217 case DW_FORM_sdata:
663c44ac 21218 case DW_FORM_implicit_const:
98bfdba5 21219 *value = DW_SND (attr);
2df3850c
JM
21220 break;
21221
c906108c 21222 case DW_FORM_udata:
98bfdba5 21223 *value = DW_UNSND (attr);
c906108c 21224 break;
2df3850c 21225
c906108c 21226 default:
b98664d3 21227 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21228 dwarf_form_name (attr->form));
98bfdba5 21229 *value = 0;
c906108c
SS
21230 break;
21231 }
21232}
21233
2df3850c 21234
98bfdba5
PA
21235/* Copy constant value from an attribute to a symbol. */
21236
2df3850c 21237static void
ff39bb5e 21238dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21239 struct dwarf2_cu *cu)
2df3850c 21240{
5e22e966 21241 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21242 LONGEST value;
d521ce57 21243 const gdb_byte *bytes;
98bfdba5 21244 struct dwarf2_locexpr_baton *baton;
2df3850c 21245
98bfdba5 21246 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21247 sym->print_name (),
98bfdba5
PA
21248 &objfile->objfile_obstack, cu,
21249 &value, &bytes, &baton);
2df3850c 21250
98bfdba5
PA
21251 if (baton != NULL)
21252 {
98bfdba5 21253 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21254 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21255 }
21256 else if (bytes != NULL)
21257 {
21258 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21259 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21260 }
21261 else
21262 {
21263 SYMBOL_VALUE (sym) = value;
f1e6e072 21264 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21265 }
2df3850c
JM
21266}
21267
c906108c
SS
21268/* Return the type of the die in question using its DW_AT_type attribute. */
21269
21270static struct type *
e7c27a73 21271die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21272{
c906108c 21273 struct attribute *type_attr;
c906108c 21274
e142c38c 21275 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21276 if (!type_attr)
21277 {
5e22e966 21278 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21279 /* A missing DW_AT_type represents a void type. */
518817b3 21280 return objfile_type (objfile)->builtin_void;
c906108c 21281 }
348e048f 21282
673bfd45 21283 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21284}
21285
b4ba55a1
JB
21286/* True iff CU's producer generates GNAT Ada auxiliary information
21287 that allows to find parallel types through that information instead
21288 of having to do expensive parallel lookups by type name. */
21289
21290static int
21291need_gnat_info (struct dwarf2_cu *cu)
21292{
de4cb04a
JB
21293 /* Assume that the Ada compiler was GNAT, which always produces
21294 the auxiliary information. */
21295 return (cu->language == language_ada);
b4ba55a1
JB
21296}
21297
b4ba55a1
JB
21298/* Return the auxiliary type of the die in question using its
21299 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21300 attribute is not present. */
21301
21302static struct type *
21303die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21304{
b4ba55a1 21305 struct attribute *type_attr;
b4ba55a1
JB
21306
21307 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21308 if (!type_attr)
21309 return NULL;
21310
673bfd45 21311 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21312}
21313
21314/* If DIE has a descriptive_type attribute, then set the TYPE's
21315 descriptive type accordingly. */
21316
21317static void
21318set_descriptive_type (struct type *type, struct die_info *die,
21319 struct dwarf2_cu *cu)
21320{
21321 struct type *descriptive_type = die_descriptive_type (die, cu);
21322
21323 if (descriptive_type)
21324 {
21325 ALLOCATE_GNAT_AUX_TYPE (type);
21326 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21327 }
21328}
21329
c906108c
SS
21330/* Return the containing type of the die in question using its
21331 DW_AT_containing_type attribute. */
21332
21333static struct type *
e7c27a73 21334die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21335{
c906108c 21336 struct attribute *type_attr;
5e22e966 21337 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21338
e142c38c 21339 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21340 if (!type_attr)
21341 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21342 "[in module %s]"), objfile_name (objfile));
33ac96f0 21343
673bfd45 21344 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21345}
21346
ac9ec31b
DE
21347/* Return an error marker type to use for the ill formed type in DIE/CU. */
21348
21349static struct type *
21350build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21351{
5e22e966 21352 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21353 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21354 char *saved;
ac9ec31b 21355
528e1572
SM
21356 std::string message
21357 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21358 objfile_name (objfile),
21359 sect_offset_str (cu->header.sect_off),
21360 sect_offset_str (die->sect_off));
efba19b0 21361 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21362
19f392bc 21363 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21364}
21365
673bfd45 21366/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21367 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21368 DW_AT_containing_type.
673bfd45
DE
21369 If there is no type substitute an error marker. */
21370
c906108c 21371static struct type *
ff39bb5e 21372lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21373 struct dwarf2_cu *cu)
c906108c 21374{
5e22e966 21375 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21376 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21377 struct type *this_type;
21378
ac9ec31b
DE
21379 gdb_assert (attr->name == DW_AT_type
21380 || attr->name == DW_AT_GNAT_descriptive_type
21381 || attr->name == DW_AT_containing_type);
21382
673bfd45
DE
21383 /* First see if we have it cached. */
21384
36586728
TT
21385 if (attr->form == DW_FORM_GNU_ref_alt)
21386 {
21387 struct dwarf2_per_cu_data *per_cu;
0826b30a 21388 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21389
ed2dc618
SM
21390 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21391 dwarf2_per_objfile);
aa66c379 21392 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
36586728 21393 }
cd6c91b4 21394 else if (attr->form_is_ref ())
673bfd45 21395 {
0826b30a 21396 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21397
aa66c379
SM
21398 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21399 dwarf2_per_objfile);
673bfd45 21400 }
55f1336d 21401 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21402 {
ac9ec31b 21403 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21404
ac9ec31b 21405 return get_signatured_type (die, signature, cu);
673bfd45
DE
21406 }
21407 else
21408 {
b98664d3 21409 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21410 " at %s [in module %s]"),
21411 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21412 objfile_name (objfile));
ac9ec31b 21413 return build_error_marker_type (cu, die);
673bfd45
DE
21414 }
21415
21416 /* If not cached we need to read it in. */
21417
21418 if (this_type == NULL)
21419 {
ac9ec31b 21420 struct die_info *type_die = NULL;
673bfd45
DE
21421 struct dwarf2_cu *type_cu = cu;
21422
cd6c91b4 21423 if (attr->form_is_ref ())
ac9ec31b
DE
21424 type_die = follow_die_ref (die, attr, &type_cu);
21425 if (type_die == NULL)
21426 return build_error_marker_type (cu, die);
21427 /* If we find the type now, it's probably because the type came
3019eac3
DE
21428 from an inter-CU reference and the type's CU got expanded before
21429 ours. */
ac9ec31b 21430 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21431 }
21432
21433 /* If we still don't have a type use an error marker. */
21434
21435 if (this_type == NULL)
ac9ec31b 21436 return build_error_marker_type (cu, die);
673bfd45 21437
f792889a 21438 return this_type;
c906108c
SS
21439}
21440
673bfd45
DE
21441/* Return the type in DIE, CU.
21442 Returns NULL for invalid types.
21443
02142a6c 21444 This first does a lookup in die_type_hash,
673bfd45
DE
21445 and only reads the die in if necessary.
21446
21447 NOTE: This can be called when reading in partial or full symbols. */
21448
f792889a 21449static struct type *
e7c27a73 21450read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21451{
f792889a
DJ
21452 struct type *this_type;
21453
21454 this_type = get_die_type (die, cu);
21455 if (this_type)
21456 return this_type;
21457
673bfd45
DE
21458 return read_type_die_1 (die, cu);
21459}
21460
21461/* Read the type in DIE, CU.
21462 Returns NULL for invalid types. */
21463
21464static struct type *
21465read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21466{
21467 struct type *this_type = NULL;
21468
c906108c
SS
21469 switch (die->tag)
21470 {
21471 case DW_TAG_class_type:
680b30c7 21472 case DW_TAG_interface_type:
c906108c
SS
21473 case DW_TAG_structure_type:
21474 case DW_TAG_union_type:
f792889a 21475 this_type = read_structure_type (die, cu);
c906108c
SS
21476 break;
21477 case DW_TAG_enumeration_type:
f792889a 21478 this_type = read_enumeration_type (die, cu);
c906108c
SS
21479 break;
21480 case DW_TAG_subprogram:
21481 case DW_TAG_subroutine_type:
edb3359d 21482 case DW_TAG_inlined_subroutine:
f792889a 21483 this_type = read_subroutine_type (die, cu);
c906108c
SS
21484 break;
21485 case DW_TAG_array_type:
f792889a 21486 this_type = read_array_type (die, cu);
c906108c 21487 break;
72019c9c 21488 case DW_TAG_set_type:
f792889a 21489 this_type = read_set_type (die, cu);
72019c9c 21490 break;
c906108c 21491 case DW_TAG_pointer_type:
f792889a 21492 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21493 break;
21494 case DW_TAG_ptr_to_member_type:
f792889a 21495 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21496 break;
21497 case DW_TAG_reference_type:
4297a3f0
AV
21498 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21499 break;
21500 case DW_TAG_rvalue_reference_type:
21501 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21502 break;
21503 case DW_TAG_const_type:
f792889a 21504 this_type = read_tag_const_type (die, cu);
c906108c
SS
21505 break;
21506 case DW_TAG_volatile_type:
f792889a 21507 this_type = read_tag_volatile_type (die, cu);
c906108c 21508 break;
06d66ee9
TT
21509 case DW_TAG_restrict_type:
21510 this_type = read_tag_restrict_type (die, cu);
21511 break;
c906108c 21512 case DW_TAG_string_type:
f792889a 21513 this_type = read_tag_string_type (die, cu);
c906108c
SS
21514 break;
21515 case DW_TAG_typedef:
f792889a 21516 this_type = read_typedef (die, cu);
c906108c 21517 break;
a02abb62 21518 case DW_TAG_subrange_type:
f792889a 21519 this_type = read_subrange_type (die, cu);
a02abb62 21520 break;
c906108c 21521 case DW_TAG_base_type:
f792889a 21522 this_type = read_base_type (die, cu);
c906108c 21523 break;
81a17f79 21524 case DW_TAG_unspecified_type:
f792889a 21525 this_type = read_unspecified_type (die, cu);
81a17f79 21526 break;
0114d602
DJ
21527 case DW_TAG_namespace:
21528 this_type = read_namespace_type (die, cu);
21529 break;
f55ee35c
JK
21530 case DW_TAG_module:
21531 this_type = read_module_type (die, cu);
21532 break;
a2c2acaf
MW
21533 case DW_TAG_atomic_type:
21534 this_type = read_tag_atomic_type (die, cu);
21535 break;
c906108c 21536 default:
b98664d3 21537 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21538 dwarf_tag_name (die->tag));
c906108c
SS
21539 break;
21540 }
63d06c5c 21541
f792889a 21542 return this_type;
63d06c5c
DC
21543}
21544
abc72ce4
DE
21545/* See if we can figure out if the class lives in a namespace. We do
21546 this by looking for a member function; its demangled name will
21547 contain namespace info, if there is any.
21548 Return the computed name or NULL.
21549 Space for the result is allocated on the objfile's obstack.
21550 This is the full-die version of guess_partial_die_structure_name.
21551 In this case we know DIE has no useful parent. */
21552
43816ebc 21553static const char *
abc72ce4
DE
21554guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21555{
21556 struct die_info *spec_die;
21557 struct dwarf2_cu *spec_cu;
21558 struct die_info *child;
5e22e966 21559 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21560
21561 spec_cu = cu;
21562 spec_die = die_specification (die, &spec_cu);
21563 if (spec_die != NULL)
21564 {
21565 die = spec_die;
21566 cu = spec_cu;
21567 }
21568
21569 for (child = die->child;
21570 child != NULL;
21571 child = child->sibling)
21572 {
21573 if (child->tag == DW_TAG_subprogram)
21574 {
73b9be8b 21575 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21576
7d45c7c3 21577 if (linkage_name != NULL)
abc72ce4 21578 {
43816ebc
TT
21579 gdb::unique_xmalloc_ptr<char> actual_name
21580 (language_class_name_from_physname (cu->language_defn,
21581 linkage_name));
21582 const char *name = NULL;
abc72ce4
DE
21583
21584 if (actual_name != NULL)
21585 {
15d034d0 21586 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21587
21588 if (die_name != NULL
43816ebc 21589 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21590 {
21591 /* Strip off the class name from the full name.
21592 We want the prefix. */
21593 int die_name_len = strlen (die_name);
43816ebc
TT
21594 int actual_name_len = strlen (actual_name.get ());
21595 const char *ptr = actual_name.get ();
abc72ce4
DE
21596
21597 /* Test for '::' as a sanity check. */
21598 if (actual_name_len > die_name_len + 2
43816ebc 21599 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21600 name = obstack_strndup (
e3b94546 21601 &objfile->per_bfd->storage_obstack,
43816ebc 21602 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21603 }
21604 }
abc72ce4
DE
21605 return name;
21606 }
21607 }
21608 }
21609
21610 return NULL;
21611}
21612
96408a79
SA
21613/* GCC might emit a nameless typedef that has a linkage name. Determine the
21614 prefix part in such case. See
21615 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21616
a121b7c1 21617static const char *
96408a79
SA
21618anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21619{
21620 struct attribute *attr;
e6a959d6 21621 const char *base;
96408a79
SA
21622
21623 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21624 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21625 return NULL;
21626
7d45c7c3 21627 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21628 return NULL;
21629
73b9be8b 21630 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21631 if (attr == NULL || DW_STRING (attr) == NULL)
21632 return NULL;
21633
21634 /* dwarf2_name had to be already called. */
21635 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21636
21637 /* Strip the base name, keep any leading namespaces/classes. */
21638 base = strrchr (DW_STRING (attr), ':');
21639 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21640 return "";
21641
5e22e966 21642 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21643 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21644 DW_STRING (attr),
21645 &base[-1] - DW_STRING (attr));
96408a79
SA
21646}
21647
fdde2d81 21648/* Return the name of the namespace/class that DIE is defined within,
0114d602 21649 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21650
0114d602
DJ
21651 For example, if we're within the method foo() in the following
21652 code:
21653
21654 namespace N {
21655 class C {
21656 void foo () {
21657 }
21658 };
21659 }
21660
21661 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21662
0d5cff50 21663static const char *
e142c38c 21664determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21665{
5e22e966 21666 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21667 struct die_info *parent, *spec_die;
21668 struct dwarf2_cu *spec_cu;
21669 struct type *parent_type;
a121b7c1 21670 const char *retval;
63d06c5c 21671
9c37b5ae 21672 if (cu->language != language_cplus
c44af4eb
TT
21673 && cu->language != language_fortran && cu->language != language_d
21674 && cu->language != language_rust)
0114d602
DJ
21675 return "";
21676
96408a79
SA
21677 retval = anonymous_struct_prefix (die, cu);
21678 if (retval)
21679 return retval;
21680
0114d602
DJ
21681 /* We have to be careful in the presence of DW_AT_specification.
21682 For example, with GCC 3.4, given the code
21683
21684 namespace N {
21685 void foo() {
21686 // Definition of N::foo.
21687 }
21688 }
21689
21690 then we'll have a tree of DIEs like this:
21691
21692 1: DW_TAG_compile_unit
21693 2: DW_TAG_namespace // N
21694 3: DW_TAG_subprogram // declaration of N::foo
21695 4: DW_TAG_subprogram // definition of N::foo
21696 DW_AT_specification // refers to die #3
21697
21698 Thus, when processing die #4, we have to pretend that we're in
21699 the context of its DW_AT_specification, namely the contex of die
21700 #3. */
21701 spec_cu = cu;
21702 spec_die = die_specification (die, &spec_cu);
21703 if (spec_die == NULL)
21704 parent = die->parent;
21705 else
63d06c5c 21706 {
0114d602
DJ
21707 parent = spec_die->parent;
21708 cu = spec_cu;
63d06c5c 21709 }
0114d602
DJ
21710
21711 if (parent == NULL)
21712 return "";
98bfdba5
PA
21713 else if (parent->building_fullname)
21714 {
21715 const char *name;
21716 const char *parent_name;
21717
21718 /* It has been seen on RealView 2.2 built binaries,
21719 DW_TAG_template_type_param types actually _defined_ as
21720 children of the parent class:
21721
21722 enum E {};
21723 template class <class Enum> Class{};
21724 Class<enum E> class_e;
21725
21726 1: DW_TAG_class_type (Class)
21727 2: DW_TAG_enumeration_type (E)
21728 3: DW_TAG_enumerator (enum1:0)
21729 3: DW_TAG_enumerator (enum2:1)
21730 ...
21731 2: DW_TAG_template_type_param
21732 DW_AT_type DW_FORM_ref_udata (E)
21733
21734 Besides being broken debug info, it can put GDB into an
21735 infinite loop. Consider:
21736
21737 When we're building the full name for Class<E>, we'll start
21738 at Class, and go look over its template type parameters,
21739 finding E. We'll then try to build the full name of E, and
21740 reach here. We're now trying to build the full name of E,
21741 and look over the parent DIE for containing scope. In the
21742 broken case, if we followed the parent DIE of E, we'd again
21743 find Class, and once again go look at its template type
21744 arguments, etc., etc. Simply don't consider such parent die
21745 as source-level parent of this die (it can't be, the language
21746 doesn't allow it), and break the loop here. */
21747 name = dwarf2_name (die, cu);
21748 parent_name = dwarf2_name (parent, cu);
b98664d3 21749 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21750 name ? name : "<unknown>",
21751 parent_name ? parent_name : "<unknown>");
21752 return "";
21753 }
63d06c5c 21754 else
0114d602
DJ
21755 switch (parent->tag)
21756 {
63d06c5c 21757 case DW_TAG_namespace:
0114d602 21758 parent_type = read_type_die (parent, cu);
acebe513
UW
21759 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21760 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21761 Work around this problem here. */
21762 if (cu->language == language_cplus
7d93a1e0 21763 && strcmp (parent_type->name (), "::") == 0)
acebe513 21764 return "";
0114d602 21765 /* We give a name to even anonymous namespaces. */
7d93a1e0 21766 return parent_type->name ();
63d06c5c 21767 case DW_TAG_class_type:
680b30c7 21768 case DW_TAG_interface_type:
63d06c5c 21769 case DW_TAG_structure_type:
0114d602 21770 case DW_TAG_union_type:
f55ee35c 21771 case DW_TAG_module:
0114d602 21772 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21773 if (parent_type->name () != NULL)
21774 return parent_type->name ();
0114d602
DJ
21775 else
21776 /* An anonymous structure is only allowed non-static data
21777 members; no typedefs, no member functions, et cetera.
21778 So it does not need a prefix. */
21779 return "";
abc72ce4 21780 case DW_TAG_compile_unit:
95554aad 21781 case DW_TAG_partial_unit:
abc72ce4
DE
21782 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21783 if (cu->language == language_cplus
5989a64e 21784 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21785 && die->child != NULL
21786 && (die->tag == DW_TAG_class_type
21787 || die->tag == DW_TAG_structure_type
21788 || die->tag == DW_TAG_union_type))
21789 {
43816ebc 21790 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21791 if (name != NULL)
21792 return name;
21793 }
21794 return "";
0a4b0913
AB
21795 case DW_TAG_subprogram:
21796 /* Nested subroutines in Fortran get a prefix with the name
21797 of the parent's subroutine. */
21798 if (cu->language == language_fortran)
21799 {
21800 if ((die->tag == DW_TAG_subprogram)
21801 && (dwarf2_name (parent, cu) != NULL))
21802 return dwarf2_name (parent, cu);
21803 }
21804 return determine_prefix (parent, cu);
3d567982
TT
21805 case DW_TAG_enumeration_type:
21806 parent_type = read_type_die (parent, cu);
21807 if (TYPE_DECLARED_CLASS (parent_type))
21808 {
7d93a1e0
SM
21809 if (parent_type->name () != NULL)
21810 return parent_type->name ();
3d567982
TT
21811 return "";
21812 }
21813 /* Fall through. */
63d06c5c 21814 default:
8176b9b8 21815 return determine_prefix (parent, cu);
63d06c5c 21816 }
63d06c5c
DC
21817}
21818
3e43a32a
MS
21819/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21820 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21821 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21822 an obconcat, otherwise allocate storage for the result. The CU argument is
21823 used to determine the language and hence, the appropriate separator. */
987504bb 21824
f55ee35c 21825#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21826
21827static char *
f55ee35c
JK
21828typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21829 int physname, struct dwarf2_cu *cu)
63d06c5c 21830{
f55ee35c 21831 const char *lead = "";
5c315b68 21832 const char *sep;
63d06c5c 21833
3e43a32a
MS
21834 if (suffix == NULL || suffix[0] == '\0'
21835 || prefix == NULL || prefix[0] == '\0')
987504bb 21836 sep = "";
45280282
IB
21837 else if (cu->language == language_d)
21838 {
21839 /* For D, the 'main' function could be defined in any module, but it
21840 should never be prefixed. */
21841 if (strcmp (suffix, "D main") == 0)
21842 {
21843 prefix = "";
21844 sep = "";
21845 }
21846 else
21847 sep = ".";
21848 }
f55ee35c
JK
21849 else if (cu->language == language_fortran && physname)
21850 {
21851 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21852 DW_AT_MIPS_linkage_name is preferred and used instead. */
21853
21854 lead = "__";
21855 sep = "_MOD_";
21856 }
987504bb
JJ
21857 else
21858 sep = "::";
63d06c5c 21859
6dd47d34
DE
21860 if (prefix == NULL)
21861 prefix = "";
21862 if (suffix == NULL)
21863 suffix = "";
21864
987504bb
JJ
21865 if (obs == NULL)
21866 {
3e43a32a 21867 char *retval
224c3ddb
SM
21868 = ((char *)
21869 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21870
f55ee35c
JK
21871 strcpy (retval, lead);
21872 strcat (retval, prefix);
6dd47d34
DE
21873 strcat (retval, sep);
21874 strcat (retval, suffix);
63d06c5c
DC
21875 return retval;
21876 }
987504bb
JJ
21877 else
21878 {
21879 /* We have an obstack. */
f55ee35c 21880 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21881 }
63d06c5c
DC
21882}
21883
71c25dea
TT
21884/* Get name of a die, return NULL if not found. */
21885
15d034d0
TT
21886static const char *
21887dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21888 struct objfile *objfile)
71c25dea
TT
21889{
21890 if (name && cu->language == language_cplus)
21891 {
596dc4ad
TT
21892 gdb::unique_xmalloc_ptr<char> canon_name
21893 = cp_canonicalize_string (name);
71c25dea 21894
596dc4ad
TT
21895 if (canon_name != nullptr)
21896 name = objfile->intern (canon_name.get ());
71c25dea
TT
21897 }
21898
21899 return name;
c906108c
SS
21900}
21901
96553a0c
DE
21902/* Get name of a die, return NULL if not found.
21903 Anonymous namespaces are converted to their magic string. */
9219021c 21904
15d034d0 21905static const char *
e142c38c 21906dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21907{
21908 struct attribute *attr;
5e22e966 21909 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21910
e142c38c 21911 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21912 if ((!attr || !DW_STRING (attr))
96553a0c 21913 && die->tag != DW_TAG_namespace
53832f31
TT
21914 && die->tag != DW_TAG_class_type
21915 && die->tag != DW_TAG_interface_type
21916 && die->tag != DW_TAG_structure_type
21917 && die->tag != DW_TAG_union_type)
71c25dea
TT
21918 return NULL;
21919
21920 switch (die->tag)
21921 {
21922 case DW_TAG_compile_unit:
95554aad 21923 case DW_TAG_partial_unit:
71c25dea
TT
21924 /* Compilation units have a DW_AT_name that is a filename, not
21925 a source language identifier. */
21926 case DW_TAG_enumeration_type:
21927 case DW_TAG_enumerator:
21928 /* These tags always have simple identifiers already; no need
21929 to canonicalize them. */
21930 return DW_STRING (attr);
907af001 21931
96553a0c
DE
21932 case DW_TAG_namespace:
21933 if (attr != NULL && DW_STRING (attr) != NULL)
21934 return DW_STRING (attr);
21935 return CP_ANONYMOUS_NAMESPACE_STR;
21936
907af001
UW
21937 case DW_TAG_class_type:
21938 case DW_TAG_interface_type:
21939 case DW_TAG_structure_type:
21940 case DW_TAG_union_type:
21941 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21942 structures or unions. These were of the form "._%d" in GCC 4.1,
21943 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21944 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21945 if (attr && DW_STRING (attr)
61012eef
GB
21946 && (startswith (DW_STRING (attr), "._")
21947 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21948 return NULL;
53832f31
TT
21949
21950 /* GCC might emit a nameless typedef that has a linkage name. See
21951 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21952 if (!attr || DW_STRING (attr) == NULL)
21953 {
73b9be8b 21954 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21955 if (attr == NULL || DW_STRING (attr) == NULL)
21956 return NULL;
21957
df5c6c50
JK
21958 /* Avoid demangling DW_STRING (attr) the second time on a second
21959 call for the same DIE. */
21960 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21961 {
43816ebc
TT
21962 gdb::unique_xmalloc_ptr<char> demangled
21963 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21964 if (demangled == nullptr)
21965 return nullptr;
43816ebc 21966
be1e3d3e 21967 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21968 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21969 }
67430cd0
TT
21970
21971 /* Strip any leading namespaces/classes, keep only the base name.
21972 DW_AT_name for named DIEs does not contain the prefixes. */
21973 const char *base = strrchr (DW_STRING (attr), ':');
21974 if (base && base > DW_STRING (attr) && base[-1] == ':')
21975 return &base[1];
21976 else
21977 return DW_STRING (attr);
53832f31 21978 }
907af001
UW
21979 break;
21980
71c25dea 21981 default:
907af001
UW
21982 break;
21983 }
21984
21985 if (!DW_STRING_IS_CANONICAL (attr))
21986 {
be1e3d3e
TT
21987 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21988 objfile);
907af001 21989 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21990 }
907af001 21991 return DW_STRING (attr);
9219021c
DC
21992}
21993
21994/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21995 is none. *EXT_CU is the CU containing DIE on input, and the CU
21996 containing the return value on output. */
9219021c
DC
21997
21998static struct die_info *
f2f0e013 21999dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22000{
22001 struct attribute *attr;
9219021c 22002
f2f0e013 22003 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22004 if (attr == NULL)
22005 return NULL;
22006
f2f0e013 22007 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22008}
22009
f9aca02d 22010static void
d97bc12b 22011dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22012{
22013 unsigned int i;
22014
d97bc12b 22015 print_spaces (indent, f);
9d8780f0 22016 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22017 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22018 sect_offset_str (die->sect_off));
d97bc12b
DE
22019
22020 if (die->parent != NULL)
22021 {
22022 print_spaces (indent, f);
9d8780f0
SM
22023 fprintf_unfiltered (f, " parent at offset: %s\n",
22024 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22025 }
22026
22027 print_spaces (indent, f);
22028 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22029 dwarf_bool_name (die->child != NULL));
c906108c 22030
d97bc12b
DE
22031 print_spaces (indent, f);
22032 fprintf_unfiltered (f, " attributes:\n");
22033
c906108c
SS
22034 for (i = 0; i < die->num_attrs; ++i)
22035 {
d97bc12b
DE
22036 print_spaces (indent, f);
22037 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22038 dwarf_attr_name (die->attrs[i].name),
22039 dwarf_form_name (die->attrs[i].form));
d97bc12b 22040
c906108c
SS
22041 switch (die->attrs[i].form)
22042 {
c906108c 22043 case DW_FORM_addr:
336d760d 22044 case DW_FORM_addrx:
3019eac3 22045 case DW_FORM_GNU_addr_index:
d97bc12b 22046 fprintf_unfiltered (f, "address: ");
5af949e3 22047 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22048 break;
22049 case DW_FORM_block2:
22050 case DW_FORM_block4:
22051 case DW_FORM_block:
22052 case DW_FORM_block1:
56eb65bd
SP
22053 fprintf_unfiltered (f, "block: size %s",
22054 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22055 break;
2dc7f7b3 22056 case DW_FORM_exprloc:
56eb65bd
SP
22057 fprintf_unfiltered (f, "expression: size %s",
22058 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22059 break;
0224619f
JK
22060 case DW_FORM_data16:
22061 fprintf_unfiltered (f, "constant of 16 bytes");
22062 break;
4568ecf9
DE
22063 case DW_FORM_ref_addr:
22064 fprintf_unfiltered (f, "ref address: ");
22065 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22066 break;
36586728
TT
22067 case DW_FORM_GNU_ref_alt:
22068 fprintf_unfiltered (f, "alt ref address: ");
22069 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22070 break;
10b3939b
DJ
22071 case DW_FORM_ref1:
22072 case DW_FORM_ref2:
22073 case DW_FORM_ref4:
4568ecf9
DE
22074 case DW_FORM_ref8:
22075 case DW_FORM_ref_udata:
d97bc12b 22076 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22077 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22078 break;
c906108c
SS
22079 case DW_FORM_data1:
22080 case DW_FORM_data2:
22081 case DW_FORM_data4:
ce5d95e1 22082 case DW_FORM_data8:
c906108c
SS
22083 case DW_FORM_udata:
22084 case DW_FORM_sdata:
43bbcdc2
PH
22085 fprintf_unfiltered (f, "constant: %s",
22086 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22087 break;
2dc7f7b3
TT
22088 case DW_FORM_sec_offset:
22089 fprintf_unfiltered (f, "section offset: %s",
22090 pulongest (DW_UNSND (&die->attrs[i])));
22091 break;
55f1336d 22092 case DW_FORM_ref_sig8:
ac9ec31b
DE
22093 fprintf_unfiltered (f, "signature: %s",
22094 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22095 break;
c906108c 22096 case DW_FORM_string:
4bdf3d34 22097 case DW_FORM_strp:
43988095 22098 case DW_FORM_line_strp:
cf532bd1 22099 case DW_FORM_strx:
3019eac3 22100 case DW_FORM_GNU_str_index:
36586728 22101 case DW_FORM_GNU_strp_alt:
8285870a 22102 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22103 DW_STRING (&die->attrs[i])
8285870a
JK
22104 ? DW_STRING (&die->attrs[i]) : "",
22105 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22106 break;
22107 case DW_FORM_flag:
22108 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22109 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22110 else
d97bc12b 22111 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22112 break;
2dc7f7b3
TT
22113 case DW_FORM_flag_present:
22114 fprintf_unfiltered (f, "flag: TRUE");
22115 break;
a8329558 22116 case DW_FORM_indirect:
0963b4bd
MS
22117 /* The reader will have reduced the indirect form to
22118 the "base form" so this form should not occur. */
5f48f8f3 22119 fprintf_unfiltered (f,
3e43a32a 22120 "unexpected attribute form: DW_FORM_indirect");
a8329558 22121 break;
663c44ac
JK
22122 case DW_FORM_implicit_const:
22123 fprintf_unfiltered (f, "constant: %s",
22124 plongest (DW_SND (&die->attrs[i])));
22125 break;
c906108c 22126 default:
d97bc12b 22127 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22128 die->attrs[i].form);
d97bc12b 22129 break;
c906108c 22130 }
d97bc12b 22131 fprintf_unfiltered (f, "\n");
c906108c
SS
22132 }
22133}
22134
f9aca02d 22135static void
d97bc12b 22136dump_die_for_error (struct die_info *die)
c906108c 22137{
d97bc12b
DE
22138 dump_die_shallow (gdb_stderr, 0, die);
22139}
22140
22141static void
22142dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22143{
22144 int indent = level * 4;
22145
22146 gdb_assert (die != NULL);
22147
22148 if (level >= max_level)
22149 return;
22150
22151 dump_die_shallow (f, indent, die);
22152
22153 if (die->child != NULL)
c906108c 22154 {
d97bc12b
DE
22155 print_spaces (indent, f);
22156 fprintf_unfiltered (f, " Children:");
22157 if (level + 1 < max_level)
22158 {
22159 fprintf_unfiltered (f, "\n");
22160 dump_die_1 (f, level + 1, max_level, die->child);
22161 }
22162 else
22163 {
3e43a32a
MS
22164 fprintf_unfiltered (f,
22165 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22166 }
22167 }
22168
22169 if (die->sibling != NULL && level > 0)
22170 {
22171 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22172 }
22173}
22174
d97bc12b
DE
22175/* This is called from the pdie macro in gdbinit.in.
22176 It's not static so gcc will keep a copy callable from gdb. */
22177
22178void
22179dump_die (struct die_info *die, int max_level)
22180{
22181 dump_die_1 (gdb_stdlog, 0, max_level, die);
22182}
22183
f9aca02d 22184static void
51545339 22185store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22186{
51545339 22187 void **slot;
c906108c 22188
9c541725
PA
22189 slot = htab_find_slot_with_hash (cu->die_hash, die,
22190 to_underlying (die->sect_off),
b64f50a1 22191 INSERT);
51545339
DJ
22192
22193 *slot = die;
c906108c
SS
22194}
22195
348e048f
DE
22196/* Follow reference or signature attribute ATTR of SRC_DIE.
22197 On entry *REF_CU is the CU of SRC_DIE.
22198 On exit *REF_CU is the CU of the result. */
22199
22200static struct die_info *
ff39bb5e 22201follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22202 struct dwarf2_cu **ref_cu)
22203{
22204 struct die_info *die;
22205
cd6c91b4 22206 if (attr->form_is_ref ())
348e048f 22207 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22208 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22209 die = follow_die_sig (src_die, attr, ref_cu);
22210 else
22211 {
22212 dump_die_for_error (src_die);
22213 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22214 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22215 }
22216
22217 return die;
03dd20cc
DJ
22218}
22219
5c631832 22220/* Follow reference OFFSET.
673bfd45
DE
22221 On entry *REF_CU is the CU of the source die referencing OFFSET.
22222 On exit *REF_CU is the CU of the result.
22223 Returns NULL if OFFSET is invalid. */
f504f079 22224
f9aca02d 22225static struct die_info *
9c541725 22226follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22227 struct dwarf2_cu **ref_cu)
c906108c 22228{
10b3939b 22229 struct die_info temp_die;
f2f0e013 22230 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22231 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22232
348e048f
DE
22233 gdb_assert (cu->per_cu != NULL);
22234
98bfdba5
PA
22235 target_cu = cu;
22236
3019eac3 22237 if (cu->per_cu->is_debug_types)
348e048f
DE
22238 {
22239 /* .debug_types CUs cannot reference anything outside their CU.
22240 If they need to, they have to reference a signatured type via
55f1336d 22241 DW_FORM_ref_sig8. */
4057dfde 22242 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22243 return NULL;
348e048f 22244 }
36586728 22245 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22246 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22247 {
22248 struct dwarf2_per_cu_data *per_cu;
9a619af0 22249
9c541725 22250 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22251 dwarf2_per_objfile);
03dd20cc
DJ
22252
22253 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 22254 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
ab432490 22255 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
03dd20cc 22256
10b3939b
DJ
22257 target_cu = per_cu->cu;
22258 }
98bfdba5
PA
22259 else if (cu->dies == NULL)
22260 {
22261 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22262 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
ab432490
SM
22263 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22264 language_minimal);
98bfdba5 22265 }
c906108c 22266
f2f0e013 22267 *ref_cu = target_cu;
9c541725 22268 temp_die.sect_off = sect_off;
c24bdb02
KS
22269
22270 if (target_cu != cu)
22271 target_cu->ancestor = cu;
22272
9a3c8263 22273 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22274 &temp_die,
22275 to_underlying (sect_off));
5c631832 22276}
10b3939b 22277
5c631832
JK
22278/* Follow reference attribute ATTR of SRC_DIE.
22279 On entry *REF_CU is the CU of SRC_DIE.
22280 On exit *REF_CU is the CU of the result. */
22281
22282static struct die_info *
ff39bb5e 22283follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22284 struct dwarf2_cu **ref_cu)
22285{
0826b30a 22286 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22287 struct dwarf2_cu *cu = *ref_cu;
22288 struct die_info *die;
22289
9c541725 22290 die = follow_die_offset (sect_off,
36586728
TT
22291 (attr->form == DW_FORM_GNU_ref_alt
22292 || cu->per_cu->is_dwz),
22293 ref_cu);
5c631832 22294 if (!die)
9d8780f0
SM
22295 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22296 "at %s [in module %s]"),
22297 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22298 objfile_name (cu->per_objfile->objfile));
348e048f 22299
5c631832
JK
22300 return die;
22301}
22302
d4c9a4f8 22303/* See read.h. */
5c631832
JK
22304
22305struct dwarf2_locexpr_baton
9c541725 22306dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22307 dwarf2_per_cu_data *per_cu,
14095eb3 22308 dwarf2_per_objfile *dwarf2_per_objfile,
8b9737bf 22309 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22310 void *baton, bool resolve_abstract_p)
5c631832 22311{
918dd910 22312 struct dwarf2_cu *cu;
5c631832
JK
22313 struct die_info *die;
22314 struct attribute *attr;
22315 struct dwarf2_locexpr_baton retval;
12359b5e 22316 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22317
918dd910 22318 if (per_cu->cu == NULL)
ab432490 22319 load_cu (per_cu, dwarf2_per_objfile, false);
918dd910 22320 cu = per_cu->cu;
cc12ce38
DE
22321 if (cu == NULL)
22322 {
22323 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22324 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22325 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22326 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22327 }
918dd910 22328
9c541725 22329 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22330 if (!die)
9d8780f0
SM
22331 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22332 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22333
22334 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22335 if (!attr && resolve_abstract_p
5989a64e
SM
22336 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22337 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22338 {
22339 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22340 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22341 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22342
3360b6e7 22343 for (const auto &cand_off
5989a64e 22344 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22345 {
3360b6e7
TV
22346 struct dwarf2_cu *cand_cu = cu;
22347 struct die_info *cand
22348 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22349 if (!cand
22350 || !cand->parent
e4a62c65
TV
22351 || cand->parent->tag != DW_TAG_subprogram)
22352 continue;
22353
22354 CORE_ADDR pc_low, pc_high;
22355 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22356 if (pc_low == ((CORE_ADDR) -1))
22357 continue;
22358 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22359 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22360 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22361 continue;
22362
22363 die = cand;
22364 attr = dwarf2_attr (die, DW_AT_location, cu);
22365 break;
22366 }
22367 }
22368
5c631832
JK
22369 if (!attr)
22370 {
e103e986
JK
22371 /* DWARF: "If there is no such attribute, then there is no effect.".
22372 DATA is ignored if SIZE is 0. */
5c631832 22373
e103e986 22374 retval.data = NULL;
5c631832
JK
22375 retval.size = 0;
22376 }
cd6c91b4 22377 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22378 {
22379 struct dwarf2_loclist_baton loclist_baton;
22380 CORE_ADDR pc = (*get_frame_pc) (baton);
22381 size_t size;
22382
22383 fill_in_loclist_baton (cu, &loclist_baton, attr);
22384
22385 retval.data = dwarf2_find_location_expression (&loclist_baton,
22386 &size, pc);
22387 retval.size = size;
22388 }
5c631832
JK
22389 else
22390 {
4fc6c0d5 22391 if (!attr->form_is_block ())
9d8780f0 22392 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22393 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22394 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22395
22396 retval.data = DW_BLOCK (attr)->data;
22397 retval.size = DW_BLOCK (attr)->size;
22398 }
a50264ba 22399 retval.per_objfile = dwarf2_per_objfile;
5c631832 22400 retval.per_cu = cu->per_cu;
918dd910 22401
ed2dc618 22402 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22403
5c631832 22404 return retval;
348e048f
DE
22405}
22406
d4c9a4f8 22407/* See read.h. */
8b9737bf
TT
22408
22409struct dwarf2_locexpr_baton
22410dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22411 dwarf2_per_cu_data *per_cu,
14095eb3 22412 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22413 CORE_ADDR (*get_frame_pc) (void *baton),
22414 void *baton)
22415{
9c541725 22416 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22417
14095eb3
SM
22418 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22419 get_frame_pc, baton);
8b9737bf
TT
22420}
22421
b6807d98
TT
22422/* Write a constant of a given type as target-ordered bytes into
22423 OBSTACK. */
22424
22425static const gdb_byte *
22426write_constant_as_bytes (struct obstack *obstack,
22427 enum bfd_endian byte_order,
22428 struct type *type,
22429 ULONGEST value,
22430 LONGEST *len)
22431{
22432 gdb_byte *result;
22433
22434 *len = TYPE_LENGTH (type);
224c3ddb 22435 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22436 store_unsigned_integer (result, *len, byte_order, value);
22437
22438 return result;
22439}
22440
d4c9a4f8 22441/* See read.h. */
b6807d98
TT
22442
22443const gdb_byte *
9c541725 22444dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22445 dwarf2_per_cu_data *per_cu,
14095eb3 22446 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22447 obstack *obstack,
b6807d98
TT
22448 LONGEST *len)
22449{
22450 struct dwarf2_cu *cu;
22451 struct die_info *die;
22452 struct attribute *attr;
22453 const gdb_byte *result = NULL;
22454 struct type *type;
22455 LONGEST value;
22456 enum bfd_endian byte_order;
14095eb3 22457 struct objfile *objfile = per_objfile->objfile;
b6807d98 22458
b6807d98 22459 if (per_cu->cu == NULL)
14095eb3 22460 load_cu (per_cu, per_objfile, false);
b6807d98 22461 cu = per_cu->cu;
cc12ce38
DE
22462 if (cu == NULL)
22463 {
22464 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22465 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22466 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22467 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22468 }
b6807d98 22469
9c541725 22470 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22471 if (!die)
9d8780f0
SM
22472 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22473 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22474
22475 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22476 if (attr == NULL)
22477 return NULL;
22478
e3b94546 22479 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22480 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22481
22482 switch (attr->form)
22483 {
22484 case DW_FORM_addr:
336d760d 22485 case DW_FORM_addrx:
b6807d98
TT
22486 case DW_FORM_GNU_addr_index:
22487 {
22488 gdb_byte *tem;
22489
22490 *len = cu->header.addr_size;
224c3ddb 22491 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22492 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22493 result = tem;
22494 }
22495 break;
22496 case DW_FORM_string:
22497 case DW_FORM_strp:
cf532bd1 22498 case DW_FORM_strx:
b6807d98
TT
22499 case DW_FORM_GNU_str_index:
22500 case DW_FORM_GNU_strp_alt:
22501 /* DW_STRING is already allocated on the objfile obstack, point
22502 directly to it. */
22503 result = (const gdb_byte *) DW_STRING (attr);
22504 *len = strlen (DW_STRING (attr));
22505 break;
22506 case DW_FORM_block1:
22507 case DW_FORM_block2:
22508 case DW_FORM_block4:
22509 case DW_FORM_block:
22510 case DW_FORM_exprloc:
0224619f 22511 case DW_FORM_data16:
b6807d98
TT
22512 result = DW_BLOCK (attr)->data;
22513 *len = DW_BLOCK (attr)->size;
22514 break;
22515
22516 /* The DW_AT_const_value attributes are supposed to carry the
22517 symbol's value "represented as it would be on the target
22518 architecture." By the time we get here, it's already been
22519 converted to host endianness, so we just need to sign- or
22520 zero-extend it as appropriate. */
22521 case DW_FORM_data1:
22522 type = die_type (die, cu);
22523 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22524 if (result == NULL)
22525 result = write_constant_as_bytes (obstack, byte_order,
22526 type, value, len);
22527 break;
22528 case DW_FORM_data2:
22529 type = die_type (die, cu);
22530 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22531 if (result == NULL)
22532 result = write_constant_as_bytes (obstack, byte_order,
22533 type, value, len);
22534 break;
22535 case DW_FORM_data4:
22536 type = die_type (die, cu);
22537 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22538 if (result == NULL)
22539 result = write_constant_as_bytes (obstack, byte_order,
22540 type, value, len);
22541 break;
22542 case DW_FORM_data8:
22543 type = die_type (die, cu);
22544 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22545 if (result == NULL)
22546 result = write_constant_as_bytes (obstack, byte_order,
22547 type, value, len);
22548 break;
22549
22550 case DW_FORM_sdata:
663c44ac 22551 case DW_FORM_implicit_const:
b6807d98
TT
22552 type = die_type (die, cu);
22553 result = write_constant_as_bytes (obstack, byte_order,
22554 type, DW_SND (attr), len);
22555 break;
22556
22557 case DW_FORM_udata:
22558 type = die_type (die, cu);
22559 result = write_constant_as_bytes (obstack, byte_order,
22560 type, DW_UNSND (attr), len);
22561 break;
22562
22563 default:
b98664d3 22564 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22565 dwarf_form_name (attr->form));
22566 break;
22567 }
22568
22569 return result;
22570}
22571
d4c9a4f8 22572/* See read.h. */
7942e96e
AA
22573
22574struct type *
9c541725 22575dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22576 dwarf2_per_cu_data *per_cu,
22577 dwarf2_per_objfile *per_objfile)
7942e96e
AA
22578{
22579 struct dwarf2_cu *cu;
22580 struct die_info *die;
22581
7942e96e 22582 if (per_cu->cu == NULL)
14095eb3 22583 load_cu (per_cu, per_objfile, false);
7942e96e
AA
22584 cu = per_cu->cu;
22585 if (!cu)
22586 return NULL;
22587
9c541725 22588 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22589 if (!die)
22590 return NULL;
22591
22592 return die_type (die, cu);
22593}
22594
8cb5117c 22595/* See read.h. */
8a9b8146
TT
22596
22597struct type *
b64f50a1 22598dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22599 dwarf2_per_cu_data *per_cu,
22600 dwarf2_per_objfile *per_objfile)
8a9b8146 22601{
9c541725 22602 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22603 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22604}
22605
ac9ec31b 22606/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22607 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22608 On exit *REF_CU is the CU of the result.
22609 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22610
22611static struct die_info *
ac9ec31b
DE
22612follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22613 struct dwarf2_cu **ref_cu)
348e048f 22614{
348e048f 22615 struct die_info temp_die;
c24bdb02 22616 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22617 struct die_info *die;
120ce1b5
SM
22618 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22619
348e048f 22620
ac9ec31b
DE
22621 /* While it might be nice to assert sig_type->type == NULL here,
22622 we can get here for DW_AT_imported_declaration where we need
22623 the DIE not the type. */
348e048f
DE
22624
22625 /* If necessary, add it to the queue and load its DIEs. */
22626
120ce1b5
SM
22627 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22628 language_minimal))
22629 read_signatured_type (sig_type, dwarf2_per_objfile);
348e048f 22630
348e048f 22631 sig_cu = sig_type->per_cu.cu;
69d751e3 22632 gdb_assert (sig_cu != NULL);
9c541725
PA
22633 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22634 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22635 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22636 to_underlying (temp_die.sect_off));
348e048f
DE
22637 if (die)
22638 {
796a7ff8
DE
22639 /* For .gdb_index version 7 keep track of included TUs.
22640 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22641 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22642 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22643 {
ae640021 22644 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22645 }
22646
348e048f 22647 *ref_cu = sig_cu;
c24bdb02
KS
22648 if (sig_cu != cu)
22649 sig_cu->ancestor = cu;
22650
348e048f
DE
22651 return die;
22652 }
22653
ac9ec31b
DE
22654 return NULL;
22655}
22656
22657/* Follow signatured type referenced by ATTR in SRC_DIE.
22658 On entry *REF_CU is the CU of SRC_DIE.
22659 On exit *REF_CU is the CU of the result.
22660 The result is the DIE of the type.
22661 If the referenced type cannot be found an error is thrown. */
22662
22663static struct die_info *
ff39bb5e 22664follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22665 struct dwarf2_cu **ref_cu)
22666{
22667 ULONGEST signature = DW_SIGNATURE (attr);
22668 struct signatured_type *sig_type;
22669 struct die_info *die;
22670
22671 gdb_assert (attr->form == DW_FORM_ref_sig8);
22672
a2ce51a0 22673 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22674 /* sig_type will be NULL if the signatured type is missing from
22675 the debug info. */
22676 if (sig_type == NULL)
22677 {
22678 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22679 " from DIE at %s [in module %s]"),
22680 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22681 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22682 }
22683
22684 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22685 if (die == NULL)
22686 {
22687 dump_die_for_error (src_die);
22688 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22689 " from DIE at %s [in module %s]"),
22690 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22691 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22692 }
22693
22694 return die;
22695}
22696
22697/* Get the type specified by SIGNATURE referenced in DIE/CU,
22698 reading in and processing the type unit if necessary. */
22699
22700static struct type *
22701get_signatured_type (struct die_info *die, ULONGEST signature,
22702 struct dwarf2_cu *cu)
22703{
5e22e966 22704 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b
DE
22705 struct signatured_type *sig_type;
22706 struct dwarf2_cu *type_cu;
22707 struct die_info *type_die;
22708 struct type *type;
22709
a2ce51a0 22710 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22711 /* sig_type will be NULL if the signatured type is missing from
22712 the debug info. */
22713 if (sig_type == NULL)
22714 {
b98664d3 22715 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22716 " from DIE at %s [in module %s]"),
22717 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22718 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22719 return build_error_marker_type (cu, die);
22720 }
22721
22722 /* If we already know the type we're done. */
22723 if (sig_type->type != NULL)
22724 return sig_type->type;
22725
22726 type_cu = cu;
22727 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22728 if (type_die != NULL)
22729 {
22730 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22731 is created. This is important, for example, because for c++ classes
22732 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22733 type = read_type_die (type_die, type_cu);
22734 if (type == NULL)
22735 {
b98664d3 22736 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22737 " referenced from DIE at %s [in module %s]"),
22738 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22739 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22740 type = build_error_marker_type (cu, die);
22741 }
22742 }
22743 else
22744 {
b98664d3 22745 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22746 " from DIE at %s [in module %s]"),
22747 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22748 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22749 type = build_error_marker_type (cu, die);
22750 }
22751 sig_type->type = type;
22752
22753 return type;
22754}
22755
22756/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22757 reading in and processing the type unit if necessary. */
22758
22759static struct type *
ff39bb5e 22760get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22761 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22762{
22763 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22764 if (attr->form_is_ref ())
ac9ec31b
DE
22765 {
22766 struct dwarf2_cu *type_cu = cu;
22767 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22768
22769 return read_type_die (type_die, type_cu);
22770 }
22771 else if (attr->form == DW_FORM_ref_sig8)
22772 {
22773 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22774 }
22775 else
22776 {
5e22e966 22777 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22778
b98664d3 22779 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22780 " at %s [in module %s]"),
22781 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22782 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22783 return build_error_marker_type (cu, die);
22784 }
348e048f
DE
22785}
22786
e5fe5e75 22787/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22788
22789static void
ab432490
SM
22790load_full_type_unit (dwarf2_per_cu_data *per_cu,
22791 dwarf2_per_objfile *per_objfile)
348e048f 22792{
52dc124a 22793 struct signatured_type *sig_type;
348e048f 22794
f4dc4d17 22795 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22796 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22797
6721b2ec
DE
22798 /* We have the per_cu, but we need the signatured_type.
22799 Fortunately this is an easy translation. */
22800 gdb_assert (per_cu->is_debug_types);
22801 sig_type = (struct signatured_type *) per_cu;
348e048f 22802
6721b2ec 22803 gdb_assert (per_cu->cu == NULL);
348e048f 22804
ab432490 22805 read_signatured_type (sig_type, per_objfile);
348e048f 22806
6721b2ec 22807 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22808}
22809
3019eac3
DE
22810/* Read in a signatured type and build its CU and DIEs.
22811 If the type is a stub for the real type in a DWO file,
22812 read in the real type from the DWO file as well. */
dee91e82
DE
22813
22814static void
ab432490
SM
22815read_signatured_type (signatured_type *sig_type,
22816 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22817{
22818 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22819
3019eac3 22820 gdb_assert (per_cu->is_debug_types);
dee91e82 22821 gdb_assert (per_cu->cu == NULL);
348e048f 22822
ab432490 22823 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
c0ab21c2
TT
22824
22825 if (!reader.dummy_p)
22826 {
22827 struct dwarf2_cu *cu = reader.cu;
22828 const gdb_byte *info_ptr = reader.info_ptr;
22829
22830 gdb_assert (cu->die_hash == NULL);
22831 cu->die_hash =
22832 htab_create_alloc_ex (cu->header.length / 12,
22833 die_hash,
22834 die_eq,
22835 NULL,
22836 &cu->comp_unit_obstack,
22837 hashtab_obstack_allocate,
22838 dummy_obstack_deallocate);
22839
3e225074 22840 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22841 reader.comp_unit_die->child
22842 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22843 reader.comp_unit_die);
22844 cu->dies = reader.comp_unit_die;
22845 /* comp_unit_die is not stored in die_hash, no need. */
22846
22847 /* We try not to read any attributes in this function, because
22848 not all CUs needed for references have been loaded yet, and
22849 symbol table processing isn't initialized. But we have to
22850 set the CU language, or we won't be able to build types
22851 correctly. Similarly, if we do not read the producer, we can
22852 not apply producer-specific interpretation. */
22853 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22854
22855 reader.keep ();
c0ab21c2
TT
22856 }
22857
7ee85ab1 22858 sig_type->per_cu.tu_read = 1;
c906108c
SS
22859}
22860
c906108c
SS
22861/* Decode simple location descriptions.
22862 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22863 the location and return the value. If COMPUTED is non-null, it is
22864 set to true to indicate that decoding was successful, and false
22865 otherwise. If COMPUTED is null, then this function may emit a
22866 complaint. */
c906108c
SS
22867
22868static CORE_ADDR
7d79de9a 22869decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22870{
5e22e966 22871 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22872 size_t i;
22873 size_t size = blk->size;
d521ce57 22874 const gdb_byte *data = blk->data;
21ae7a4d
JK
22875 CORE_ADDR stack[64];
22876 int stacki;
22877 unsigned int bytes_read, unsnd;
22878 gdb_byte op;
c906108c 22879
7d79de9a
TT
22880 if (computed != nullptr)
22881 *computed = false;
22882
21ae7a4d
JK
22883 i = 0;
22884 stacki = 0;
22885 stack[stacki] = 0;
22886 stack[++stacki] = 0;
22887
22888 while (i < size)
22889 {
22890 op = data[i++];
22891 switch (op)
22892 {
22893 case DW_OP_lit0:
22894 case DW_OP_lit1:
22895 case DW_OP_lit2:
22896 case DW_OP_lit3:
22897 case DW_OP_lit4:
22898 case DW_OP_lit5:
22899 case DW_OP_lit6:
22900 case DW_OP_lit7:
22901 case DW_OP_lit8:
22902 case DW_OP_lit9:
22903 case DW_OP_lit10:
22904 case DW_OP_lit11:
22905 case DW_OP_lit12:
22906 case DW_OP_lit13:
22907 case DW_OP_lit14:
22908 case DW_OP_lit15:
22909 case DW_OP_lit16:
22910 case DW_OP_lit17:
22911 case DW_OP_lit18:
22912 case DW_OP_lit19:
22913 case DW_OP_lit20:
22914 case DW_OP_lit21:
22915 case DW_OP_lit22:
22916 case DW_OP_lit23:
22917 case DW_OP_lit24:
22918 case DW_OP_lit25:
22919 case DW_OP_lit26:
22920 case DW_OP_lit27:
22921 case DW_OP_lit28:
22922 case DW_OP_lit29:
22923 case DW_OP_lit30:
22924 case DW_OP_lit31:
22925 stack[++stacki] = op - DW_OP_lit0;
22926 break;
f1bea926 22927
21ae7a4d
JK
22928 case DW_OP_reg0:
22929 case DW_OP_reg1:
22930 case DW_OP_reg2:
22931 case DW_OP_reg3:
22932 case DW_OP_reg4:
22933 case DW_OP_reg5:
22934 case DW_OP_reg6:
22935 case DW_OP_reg7:
22936 case DW_OP_reg8:
22937 case DW_OP_reg9:
22938 case DW_OP_reg10:
22939 case DW_OP_reg11:
22940 case DW_OP_reg12:
22941 case DW_OP_reg13:
22942 case DW_OP_reg14:
22943 case DW_OP_reg15:
22944 case DW_OP_reg16:
22945 case DW_OP_reg17:
22946 case DW_OP_reg18:
22947 case DW_OP_reg19:
22948 case DW_OP_reg20:
22949 case DW_OP_reg21:
22950 case DW_OP_reg22:
22951 case DW_OP_reg23:
22952 case DW_OP_reg24:
22953 case DW_OP_reg25:
22954 case DW_OP_reg26:
22955 case DW_OP_reg27:
22956 case DW_OP_reg28:
22957 case DW_OP_reg29:
22958 case DW_OP_reg30:
22959 case DW_OP_reg31:
22960 stack[++stacki] = op - DW_OP_reg0;
22961 if (i < size)
7d79de9a
TT
22962 {
22963 if (computed == nullptr)
22964 dwarf2_complex_location_expr_complaint ();
22965 else
22966 return 0;
22967 }
21ae7a4d 22968 break;
c906108c 22969
21ae7a4d
JK
22970 case DW_OP_regx:
22971 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22972 i += bytes_read;
22973 stack[++stacki] = unsnd;
22974 if (i < size)
7d79de9a
TT
22975 {
22976 if (computed == nullptr)
22977 dwarf2_complex_location_expr_complaint ();
22978 else
22979 return 0;
22980 }
21ae7a4d 22981 break;
c906108c 22982
21ae7a4d 22983 case DW_OP_addr:
c8a7a66f
TT
22984 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22985 &bytes_read);
21ae7a4d
JK
22986 i += bytes_read;
22987 break;
d53d4ac5 22988
21ae7a4d
JK
22989 case DW_OP_const1u:
22990 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22991 i += 1;
22992 break;
22993
22994 case DW_OP_const1s:
22995 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22996 i += 1;
22997 break;
22998
22999 case DW_OP_const2u:
23000 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23001 i += 2;
23002 break;
23003
23004 case DW_OP_const2s:
23005 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23006 i += 2;
23007 break;
d53d4ac5 23008
21ae7a4d
JK
23009 case DW_OP_const4u:
23010 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23011 i += 4;
23012 break;
23013
23014 case DW_OP_const4s:
23015 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23016 i += 4;
23017 break;
23018
585861ea
JK
23019 case DW_OP_const8u:
23020 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23021 i += 8;
23022 break;
23023
21ae7a4d
JK
23024 case DW_OP_constu:
23025 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23026 &bytes_read);
23027 i += bytes_read;
23028 break;
23029
23030 case DW_OP_consts:
23031 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23032 i += bytes_read;
23033 break;
23034
23035 case DW_OP_dup:
23036 stack[stacki + 1] = stack[stacki];
23037 stacki++;
23038 break;
23039
23040 case DW_OP_plus:
23041 stack[stacki - 1] += stack[stacki];
23042 stacki--;
23043 break;
23044
23045 case DW_OP_plus_uconst:
23046 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23047 &bytes_read);
23048 i += bytes_read;
23049 break;
23050
23051 case DW_OP_minus:
23052 stack[stacki - 1] -= stack[stacki];
23053 stacki--;
23054 break;
23055
23056 case DW_OP_deref:
23057 /* If we're not the last op, then we definitely can't encode
23058 this using GDB's address_class enum. This is valid for partial
23059 global symbols, although the variable's address will be bogus
23060 in the psymtab. */
23061 if (i < size)
7d79de9a
TT
23062 {
23063 if (computed == nullptr)
23064 dwarf2_complex_location_expr_complaint ();
23065 else
23066 return 0;
23067 }
21ae7a4d
JK
23068 break;
23069
23070 case DW_OP_GNU_push_tls_address:
4aa4e28b 23071 case DW_OP_form_tls_address:
21ae7a4d
JK
23072 /* The top of the stack has the offset from the beginning
23073 of the thread control block at which the variable is located. */
23074 /* Nothing should follow this operator, so the top of stack would
23075 be returned. */
23076 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23077 address will be bogus in the psymtab. Make it always at least
23078 non-zero to not look as a variable garbage collected by linker
23079 which have DW_OP_addr 0. */
21ae7a4d 23080 if (i < size)
7d79de9a
TT
23081 {
23082 if (computed == nullptr)
23083 dwarf2_complex_location_expr_complaint ();
23084 else
23085 return 0;
23086 }
585861ea 23087 stack[stacki]++;
21ae7a4d
JK
23088 break;
23089
23090 case DW_OP_GNU_uninit:
7d79de9a
TT
23091 if (computed != nullptr)
23092 return 0;
21ae7a4d
JK
23093 break;
23094
336d760d 23095 case DW_OP_addrx:
3019eac3 23096 case DW_OP_GNU_addr_index:
49f6c839 23097 case DW_OP_GNU_const_index:
3019eac3
DE
23098 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23099 &bytes_read);
23100 i += bytes_read;
23101 break;
23102
21ae7a4d 23103 default:
7d79de9a
TT
23104 if (computed == nullptr)
23105 {
23106 const char *name = get_DW_OP_name (op);
21ae7a4d 23107
7d79de9a
TT
23108 if (name)
23109 complaint (_("unsupported stack op: '%s'"),
23110 name);
23111 else
23112 complaint (_("unsupported stack op: '%02x'"),
23113 op);
23114 }
21ae7a4d
JK
23115
23116 return (stack[stacki]);
d53d4ac5 23117 }
3c6e0cb3 23118
21ae7a4d
JK
23119 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23120 outside of the allocated space. Also enforce minimum>0. */
23121 if (stacki >= ARRAY_SIZE (stack) - 1)
23122 {
7d79de9a
TT
23123 if (computed == nullptr)
23124 complaint (_("location description stack overflow"));
21ae7a4d
JK
23125 return 0;
23126 }
23127
23128 if (stacki <= 0)
23129 {
7d79de9a
TT
23130 if (computed == nullptr)
23131 complaint (_("location description stack underflow"));
21ae7a4d
JK
23132 return 0;
23133 }
23134 }
7d79de9a
TT
23135
23136 if (computed != nullptr)
23137 *computed = true;
21ae7a4d 23138 return (stack[stacki]);
c906108c
SS
23139}
23140
23141/* memory allocation interface */
23142
c906108c 23143static struct dwarf_block *
7b5a2f43 23144dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23145{
8d749320 23146 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23147}
23148
c906108c 23149static struct die_info *
b60c80d6 23150dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23151{
23152 struct die_info *die;
b60c80d6
DJ
23153 size_t size = sizeof (struct die_info);
23154
23155 if (num_attrs > 1)
23156 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23157
b60c80d6 23158 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23159 memset (die, 0, sizeof (struct die_info));
23160 return (die);
23161}
2e276125
JB
23162
23163\f
a036ba48 23164
c90ec28a 23165/* Macro support. */
cf2c3c16 23166
9eac9650
TT
23167/* An overload of dwarf_decode_macros that finds the correct section
23168 and ensures it is read in before calling the other overload. */
23169
23170static void
23171dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23172 int section_is_gnu)
23173{
5e22e966 23174 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23175 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23176 const struct line_header *lh = cu->line_header;
9eac9650
TT
23177 unsigned int offset_size = cu->header.offset_size;
23178 struct dwarf2_section_info *section;
23179 const char *section_name;
23180
23181 if (cu->dwo_unit != nullptr)
23182 {
23183 if (section_is_gnu)
23184 {
23185 section = &cu->dwo_unit->dwo_file->sections.macro;
23186 section_name = ".debug_macro.dwo";
23187 }
23188 else
23189 {
23190 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23191 section_name = ".debug_macinfo.dwo";
23192 }
23193 }
23194 else
23195 {
23196 if (section_is_gnu)
23197 {
5989a64e 23198 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23199 section_name = ".debug_macro";
23200 }
23201 else
23202 {
5989a64e 23203 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23204 section_name = ".debug_macinfo";
23205 }
23206 }
23207
23208 section->read (objfile);
23209 if (section->buffer == nullptr)
23210 {
23211 complaint (_("missing %s section"), section_name);
23212 return;
23213 }
23214
23215 buildsym_compunit *builder = cu->get_builder ();
23216
23217 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23218 offset_size, offset, section_is_gnu);
23219}
23220
3019eac3
DE
23221/* Return the .debug_loc section to use for CU.
23222 For DWO files use .debug_loc.dwo. */
23223
23224static struct dwarf2_section_info *
23225cu_debug_loc_section (struct dwarf2_cu *cu)
23226{
5e22e966 23227 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23228
3019eac3 23229 if (cu->dwo_unit)
43988095
JK
23230 {
23231 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23232
43988095
JK
23233 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23234 }
5989a64e
SM
23235 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23236 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23237}
23238
8cf6f0b1
TT
23239/* A helper function that fills in a dwarf2_loclist_baton. */
23240
23241static void
23242fill_in_loclist_baton (struct dwarf2_cu *cu,
23243 struct dwarf2_loclist_baton *baton,
ff39bb5e 23244 const struct attribute *attr)
8cf6f0b1 23245{
5e22e966 23246 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23247 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23248
96b79293 23249 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23250
a50264ba 23251 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23252 baton->per_cu = cu->per_cu;
23253 gdb_assert (baton->per_cu);
23254 /* We don't know how long the location list is, but make sure we
23255 don't run off the edge of the section. */
3019eac3
DE
23256 baton->size = section->size - DW_UNSND (attr);
23257 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23258 if (cu->base_address.has_value ())
23259 baton->base_address = *cu->base_address;
23260 else
23261 baton->base_address = 0;
f664829e 23262 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23263}
23264
4c2df51b 23265static void
ff39bb5e 23266dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23267 struct dwarf2_cu *cu, int is_block)
4c2df51b 23268{
5e22e966 23269 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23270 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23271 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23272
cd6c91b4 23273 if (attr->form_is_section_offset ()
3019eac3 23274 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23275 the section. If so, fall through to the complaint in the
23276 other branch. */
2c7d5afc 23277 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23278 {
0d53c4c4 23279 struct dwarf2_loclist_baton *baton;
4c2df51b 23280
8d749320 23281 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23282
8cf6f0b1 23283 fill_in_loclist_baton (cu, baton, attr);
be391dca 23284
2b24b6e4 23285 if (!cu->base_address.has_value ())
b98664d3 23286 complaint (_("Location list used without "
3e43a32a 23287 "specifying the CU base address."));
4c2df51b 23288
f1e6e072
TT
23289 SYMBOL_ACLASS_INDEX (sym) = (is_block
23290 ? dwarf2_loclist_block_index
23291 : dwarf2_loclist_index);
0d53c4c4
DJ
23292 SYMBOL_LOCATION_BATON (sym) = baton;
23293 }
23294 else
23295 {
23296 struct dwarf2_locexpr_baton *baton;
23297
8d749320 23298 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23299 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23300 baton->per_cu = cu->per_cu;
23301 gdb_assert (baton->per_cu);
0d53c4c4 23302
4fc6c0d5 23303 if (attr->form_is_block ())
0d53c4c4
DJ
23304 {
23305 /* Note that we're just copying the block's data pointer
23306 here, not the actual data. We're still pointing into the
6502dd73
DJ
23307 info_buffer for SYM's objfile; right now we never release
23308 that buffer, but when we do clean up properly this may
23309 need to change. */
0d53c4c4
DJ
23310 baton->size = DW_BLOCK (attr)->size;
23311 baton->data = DW_BLOCK (attr)->data;
23312 }
23313 else
23314 {
23315 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23316 sym->natural_name ());
0d53c4c4 23317 baton->size = 0;
0d53c4c4 23318 }
6e70227d 23319
f1e6e072
TT
23320 SYMBOL_ACLASS_INDEX (sym) = (is_block
23321 ? dwarf2_locexpr_block_index
23322 : dwarf2_locexpr_index);
0d53c4c4
DJ
23323 SYMBOL_LOCATION_BATON (sym) = baton;
23324 }
4c2df51b 23325}
6502dd73 23326
96408a79
SA
23327/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23328 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23329 CU_HEADERP first. */
23330
23331static const struct comp_unit_head *
23332per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23333 const struct dwarf2_per_cu_data *per_cu)
96408a79 23334{
d521ce57 23335 const gdb_byte *info_ptr;
96408a79
SA
23336
23337 if (per_cu->cu)
23338 return &per_cu->cu->header;
23339
9c541725 23340 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23341
23342 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23343 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23344 rcuh_kind::COMPILE);
96408a79
SA
23345
23346 return cu_headerp;
23347}
23348
09ba997f 23349/* See read.h. */
ae0d2f24 23350
98714339 23351int
09ba997f 23352dwarf2_per_cu_data::addr_size () const
ae0d2f24 23353{
96408a79
SA
23354 struct comp_unit_head cu_header_local;
23355 const struct comp_unit_head *cu_headerp;
c471e790 23356
09ba997f 23357 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23358
23359 return cu_headerp->addr_size;
ae0d2f24
UW
23360}
23361
09ba997f 23362/* See read.h. */
9eae7c52
TT
23363
23364int
09ba997f 23365dwarf2_per_cu_data::offset_size () const
9eae7c52 23366{
96408a79
SA
23367 struct comp_unit_head cu_header_local;
23368 const struct comp_unit_head *cu_headerp;
9c6c53f7 23369
09ba997f 23370 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23371
23372 return cu_headerp->offset_size;
23373}
23374
09ba997f 23375/* See read.h. */
96408a79
SA
23376
23377int
09ba997f 23378dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23379{
23380 struct comp_unit_head cu_header_local;
23381 const struct comp_unit_head *cu_headerp;
23382
09ba997f 23383 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23384
23385 if (cu_headerp->version == 2)
23386 return cu_headerp->addr_size;
23387 else
23388 return cu_headerp->offset_size;
181cebd4
JK
23389}
23390
09ba997f 23391/* See read.h. */
9aa1f1e3 23392
09ba997f 23393struct type *
293e7e51 23394dwarf2_cu::addr_type () const
9a49df9d 23395{
293e7e51 23396 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23397 struct type *void_type = objfile_type (objfile)->builtin_void;
23398 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23399 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23400
23401 if (TYPE_LENGTH (addr_type) == addr_size)
23402 return addr_type;
23403
09ba997f 23404 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23405 return addr_type;
23406}
23407
22b6cd70
TT
23408/* A helper function for dwarf2_find_containing_comp_unit that returns
23409 the index of the result, and that searches a vector. It will
23410 return a result even if the offset in question does not actually
23411 occur in any CU. This is separate so that it can be unit
23412 tested. */
ae038cb0 23413
22b6cd70
TT
23414static int
23415dwarf2_find_containing_comp_unit
23416 (sect_offset sect_off,
23417 unsigned int offset_in_dwz,
23418 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23419{
ae038cb0
DJ
23420 int low, high;
23421
ae038cb0 23422 low = 0;
22b6cd70 23423 high = all_comp_units.size () - 1;
ae038cb0
DJ
23424 while (high > low)
23425 {
36586728 23426 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23427 int mid = low + (high - low) / 2;
9a619af0 23428
22b6cd70 23429 mid_cu = all_comp_units[mid];
36586728 23430 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23431 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23432 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23433 high = mid;
23434 else
23435 low = mid + 1;
23436 }
23437 gdb_assert (low == high);
22b6cd70
TT
23438 return low;
23439}
23440
23441/* Locate the .debug_info compilation unit from CU's objfile which contains
23442 the DIE at OFFSET. Raises an error on failure. */
23443
23444static struct dwarf2_per_cu_data *
23445dwarf2_find_containing_comp_unit (sect_offset sect_off,
23446 unsigned int offset_in_dwz,
23447 struct dwarf2_per_objfile *dwarf2_per_objfile)
23448{
23449 int low
23450 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23451 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23452 struct dwarf2_per_cu_data *this_cu
5989a64e 23453 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23454
45b8ae0c 23455 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23456 {
36586728 23457 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23458 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23459 "offset %s [in module %s]"),
23460 sect_offset_str (sect_off),
ed2dc618 23461 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23462
5989a64e 23463 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23464 <= sect_off);
5989a64e 23465 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23466 }
23467 else
23468 {
5989a64e 23469 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23470 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23471 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23472 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23473 return this_cu;
23474 }
23475}
23476
22b6cd70
TT
23477#if GDB_SELF_TEST
23478
23479namespace selftests {
23480namespace find_containing_comp_unit {
23481
23482static void
23483run_test ()
23484{
23485 struct dwarf2_per_cu_data one {};
23486 struct dwarf2_per_cu_data two {};
23487 struct dwarf2_per_cu_data three {};
23488 struct dwarf2_per_cu_data four {};
23489
23490 one.length = 5;
23491 two.sect_off = sect_offset (one.length);
23492 two.length = 7;
23493
23494 three.length = 5;
23495 three.is_dwz = 1;
23496 four.sect_off = sect_offset (three.length);
23497 four.length = 7;
23498 four.is_dwz = 1;
23499
23500 std::vector<dwarf2_per_cu_data *> units;
23501 units.push_back (&one);
23502 units.push_back (&two);
23503 units.push_back (&three);
23504 units.push_back (&four);
23505
23506 int result;
23507
23508 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23509 SELF_CHECK (units[result] == &one);
23510 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23511 SELF_CHECK (units[result] == &one);
23512 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23513 SELF_CHECK (units[result] == &two);
23514
23515 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23516 SELF_CHECK (units[result] == &three);
23517 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23518 SELF_CHECK (units[result] == &three);
23519 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23520 SELF_CHECK (units[result] == &four);
23521}
23522
23523}
23524}
23525
23526#endif /* GDB_SELF_TEST */
23527
9e021579 23528/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23529
9e021579
SM
23530dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23531 dwarf2_per_objfile *per_objfile)
23532 : per_cu (per_cu),
23533 per_objfile (per_objfile),
9068261f
AB
23534 mark (false),
23535 has_loclist (false),
23536 checked_producer (false),
23537 producer_is_gxx_lt_4_6 (false),
23538 producer_is_gcc_lt_4_3 (false),
eb77c9df 23539 producer_is_icc (false),
9068261f 23540 producer_is_icc_lt_14 (false),
c258c396 23541 producer_is_codewarrior (false),
9068261f 23542 processing_has_namespace_info (false)
93311388 23543{
fcd3b13d
SM
23544 per_cu->cu = this;
23545}
23546
23547/* Destroy a dwarf2_cu. */
23548
23549dwarf2_cu::~dwarf2_cu ()
23550{
23551 per_cu->cu = NULL;
9816fde3
JK
23552}
23553
23554/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23555
23556static void
95554aad
TT
23557prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23558 enum language pretend_language)
9816fde3
JK
23559{
23560 struct attribute *attr;
23561
23562 /* Set the language we're debugging. */
23563 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23564 if (attr != nullptr)
9816fde3
JK
23565 set_cu_language (DW_UNSND (attr), cu);
23566 else
9cded63f 23567 {
95554aad 23568 cu->language = pretend_language;
9cded63f
TT
23569 cu->language_defn = language_def (cu->language);
23570 }
dee91e82 23571
7d45c7c3 23572 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23573}
23574
ae038cb0
DJ
23575/* Increase the age counter on each cached compilation unit, and free
23576 any that are too old. */
23577
23578static void
ed2dc618 23579age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23580{
23581 struct dwarf2_per_cu_data *per_cu, **last_chain;
23582
5989a64e
SM
23583 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23584 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23585 while (per_cu != NULL)
23586 {
23587 per_cu->cu->last_used ++;
b4f54984 23588 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23589 dwarf2_mark (per_cu->cu);
23590 per_cu = per_cu->cu->read_in_chain;
23591 }
23592
5989a64e
SM
23593 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23594 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23595 while (per_cu != NULL)
23596 {
23597 struct dwarf2_per_cu_data *next_cu;
23598
23599 next_cu = per_cu->cu->read_in_chain;
23600
23601 if (!per_cu->cu->mark)
23602 {
fcd3b13d 23603 delete per_cu->cu;
ae038cb0
DJ
23604 *last_chain = next_cu;
23605 }
23606 else
23607 last_chain = &per_cu->cu->read_in_chain;
23608
23609 per_cu = next_cu;
23610 }
23611}
23612
23613/* Remove a single compilation unit from the cache. */
23614
23615static void
120ce1b5
SM
23616free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
23617 dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23618{
23619 struct dwarf2_per_cu_data *per_cu, **last_chain;
23620
5989a64e
SM
23621 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23622 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23623 while (per_cu != NULL)
23624 {
23625 struct dwarf2_per_cu_data *next_cu;
23626
23627 next_cu = per_cu->cu->read_in_chain;
23628
dee91e82 23629 if (per_cu == target_per_cu)
ae038cb0 23630 {
fcd3b13d 23631 delete per_cu->cu;
dee91e82 23632 per_cu->cu = NULL;
ae038cb0
DJ
23633 *last_chain = next_cu;
23634 break;
23635 }
23636 else
23637 last_chain = &per_cu->cu->read_in_chain;
23638
23639 per_cu = next_cu;
23640 }
23641}
23642
dee91e82
DE
23643/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23644 We store these in a hash table separate from the DIEs, and preserve them
23645 when the DIEs are flushed out of cache.
23646
23647 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23648 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23649 or the type may come from a DWO file. Furthermore, while it's more logical
23650 to use per_cu->section+offset, with Fission the section with the data is in
23651 the DWO file but we don't know that section at the point we need it.
23652 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23653 because we can enter the lookup routine, get_die_type_at_offset, from
23654 outside this file, and thus won't necessarily have PER_CU->cu.
23655 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23656
dee91e82 23657struct dwarf2_per_cu_offset_and_type
1c379e20 23658{
dee91e82 23659 const struct dwarf2_per_cu_data *per_cu;
9c541725 23660 sect_offset sect_off;
1c379e20
DJ
23661 struct type *type;
23662};
23663
dee91e82 23664/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23665
23666static hashval_t
dee91e82 23667per_cu_offset_and_type_hash (const void *item)
1c379e20 23668{
9a3c8263
SM
23669 const struct dwarf2_per_cu_offset_and_type *ofs
23670 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23671
9c541725 23672 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23673}
23674
dee91e82 23675/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23676
23677static int
dee91e82 23678per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23679{
9a3c8263
SM
23680 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23681 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23682 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23683 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23684
dee91e82 23685 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23686 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23687}
23688
23689/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23690 table if necessary. For convenience, return TYPE.
23691
23692 The DIEs reading must have careful ordering to:
85102364 23693 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23694 reading current DIE.
23695 * Not trying to dereference contents of still incompletely read in types
23696 while reading in other DIEs.
23697 * Enable referencing still incompletely read in types just by a pointer to
23698 the type without accessing its fields.
23699
23700 Therefore caller should follow these rules:
23701 * Try to fetch any prerequisite types we may need to build this DIE type
23702 before building the type and calling set_die_type.
e71ec853 23703 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23704 possible before fetching more types to complete the current type.
23705 * Make the type as complete as possible before fetching more types. */
1c379e20 23706
f792889a 23707static struct type *
1c379e20
DJ
23708set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23709{
5e22e966 23710 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23711 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23712 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23713 struct attribute *attr;
23714 struct dynamic_prop prop;
1c379e20 23715
b4ba55a1
JB
23716 /* For Ada types, make sure that the gnat-specific data is always
23717 initialized (if not already set). There are a few types where
23718 we should not be doing so, because the type-specific area is
23719 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23720 where the type-specific area is used to store the floatformat).
23721 But this is not a problem, because the gnat-specific information
23722 is actually not needed for these types. */
23723 if (need_gnat_info (cu)
78134374
SM
23724 && type->code () != TYPE_CODE_FUNC
23725 && type->code () != TYPE_CODE_FLT
23726 && type->code () != TYPE_CODE_METHODPTR
23727 && type->code () != TYPE_CODE_MEMBERPTR
23728 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23729 && !HAVE_GNAT_AUX_INFO (type))
23730 INIT_GNAT_SPECIFIC (type);
23731
3f2f83dd
KB
23732 /* Read DW_AT_allocated and set in type. */
23733 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23734 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23735 {
293e7e51 23736 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23737 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23738 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23739 }
23740 else if (attr != NULL)
23741 {
b98664d3 23742 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23743 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23744 sect_offset_str (die->sect_off));
3f2f83dd
KB
23745 }
23746
23747 /* Read DW_AT_associated and set in type. */
23748 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23749 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23750 {
293e7e51 23751 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23752 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23753 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23754 }
23755 else if (attr != NULL)
23756 {
b98664d3 23757 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23758 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23759 sect_offset_str (die->sect_off));
3f2f83dd
KB
23760 }
23761
3cdcd0ce
JB
23762 /* Read DW_AT_data_location and set in type. */
23763 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23764 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23765 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23766
ae090bdb
SM
23767 if (dwarf2_per_objfile->die_type_hash == NULL)
23768 dwarf2_per_objfile->die_type_hash
0335378b
TT
23769 = htab_up (htab_create_alloc (127,
23770 per_cu_offset_and_type_hash,
23771 per_cu_offset_and_type_eq,
23772 NULL, xcalloc, xfree));
1c379e20 23773
dee91e82 23774 ofs.per_cu = cu->per_cu;
9c541725 23775 ofs.sect_off = die->sect_off;
1c379e20 23776 ofs.type = type;
dee91e82 23777 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23778 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23779 if (*slot)
b98664d3 23780 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23781 sect_offset_str (die->sect_off));
8d749320
SM
23782 *slot = XOBNEW (&objfile->objfile_obstack,
23783 struct dwarf2_per_cu_offset_and_type);
1c379e20 23784 **slot = ofs;
f792889a 23785 return type;
1c379e20
DJ
23786}
23787
9c541725 23788/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23789 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23790
23791static struct type *
9c541725 23792get_die_type_at_offset (sect_offset sect_off,
aa66c379
SM
23793 dwarf2_per_cu_data *per_cu,
23794 dwarf2_per_objfile *dwarf2_per_objfile)
1c379e20 23795{
dee91e82 23796 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23797
ae090bdb 23798 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23799 return NULL;
1c379e20 23800
dee91e82 23801 ofs.per_cu = per_cu;
9c541725 23802 ofs.sect_off = sect_off;
9a3c8263 23803 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23804 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23805 if (slot)
23806 return slot->type;
23807 else
23808 return NULL;
23809}
23810
02142a6c 23811/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23812 or return NULL if DIE does not have a saved type. */
23813
23814static struct type *
23815get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23816{
aa66c379 23817 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23818}
23819
10b3939b
DJ
23820/* Add a dependence relationship from CU to REF_PER_CU. */
23821
23822static void
23823dwarf2_add_dependence (struct dwarf2_cu *cu,
23824 struct dwarf2_per_cu_data *ref_per_cu)
23825{
23826 void **slot;
23827
23828 if (cu->dependencies == NULL)
23829 cu->dependencies
23830 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23831 NULL, &cu->comp_unit_obstack,
23832 hashtab_obstack_allocate,
23833 dummy_obstack_deallocate);
23834
23835 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23836 if (*slot == NULL)
23837 *slot = ref_per_cu;
23838}
1c379e20 23839
f504f079
DE
23840/* Subroutine of dwarf2_mark to pass to htab_traverse.
23841 Set the mark field in every compilation unit in the
ae038cb0
DJ
23842 cache that we must keep because we are keeping CU. */
23843
10b3939b
DJ
23844static int
23845dwarf2_mark_helper (void **slot, void *data)
23846{
23847 struct dwarf2_per_cu_data *per_cu;
23848
23849 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23850
23851 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23852 reading of the chain. As such dependencies remain valid it is not much
23853 useful to track and undo them during QUIT cleanups. */
23854 if (per_cu->cu == NULL)
23855 return 1;
23856
10b3939b
DJ
23857 if (per_cu->cu->mark)
23858 return 1;
9068261f 23859 per_cu->cu->mark = true;
10b3939b
DJ
23860
23861 if (per_cu->cu->dependencies != NULL)
23862 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23863
23864 return 1;
23865}
23866
f504f079
DE
23867/* Set the mark field in CU and in every other compilation unit in the
23868 cache that we must keep because we are keeping CU. */
23869
ae038cb0
DJ
23870static void
23871dwarf2_mark (struct dwarf2_cu *cu)
23872{
23873 if (cu->mark)
23874 return;
9068261f 23875 cu->mark = true;
10b3939b
DJ
23876 if (cu->dependencies != NULL)
23877 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23878}
23879
23880static void
23881dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23882{
23883 while (per_cu)
23884 {
9068261f 23885 per_cu->cu->mark = false;
ae038cb0
DJ
23886 per_cu = per_cu->cu->read_in_chain;
23887 }
72bf9492
DJ
23888}
23889
72bf9492
DJ
23890/* Trivial hash function for partial_die_info: the hash value of a DIE
23891 is its offset in .debug_info for this objfile. */
23892
23893static hashval_t
23894partial_die_hash (const void *item)
23895{
9a3c8263
SM
23896 const struct partial_die_info *part_die
23897 = (const struct partial_die_info *) item;
9a619af0 23898
9c541725 23899 return to_underlying (part_die->sect_off);
72bf9492
DJ
23900}
23901
23902/* Trivial comparison function for partial_die_info structures: two DIEs
23903 are equal if they have the same offset. */
23904
23905static int
23906partial_die_eq (const void *item_lhs, const void *item_rhs)
23907{
9a3c8263
SM
23908 const struct partial_die_info *part_die_lhs
23909 = (const struct partial_die_info *) item_lhs;
23910 const struct partial_die_info *part_die_rhs
23911 = (const struct partial_die_info *) item_rhs;
9a619af0 23912
9c541725 23913 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23914}
23915
3c3bb058
AB
23916struct cmd_list_element *set_dwarf_cmdlist;
23917struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23918
9291a0cd 23919static void
cd4fb1b2
SM
23920show_check_physname (struct ui_file *file, int from_tty,
23921 struct cmd_list_element *c, const char *value)
9291a0cd 23922{
cd4fb1b2
SM
23923 fprintf_filtered (file,
23924 _("Whether to check \"physname\" is %s.\n"),
23925 value);
9291a0cd
TT
23926}
23927
6c265988 23928void _initialize_dwarf2_read ();
cd4fb1b2 23929void
6c265988 23930_initialize_dwarf2_read ()
9291a0cd 23931{
0743fc83 23932 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23933Set DWARF specific variables.\n\
590042fc 23934Configure DWARF variables such as the cache size."),
0743fc83
TT
23935 &set_dwarf_cmdlist, "maintenance set dwarf ",
23936 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23937
0743fc83 23938 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23939Show DWARF specific variables.\n\
23940Show DWARF variables such as the cache size."),
0743fc83
TT
23941 &show_dwarf_cmdlist, "maintenance show dwarf ",
23942 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23943
cd4fb1b2
SM
23944 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23945 &dwarf_max_cache_age, _("\
23946Set the upper bound on the age of cached DWARF compilation units."), _("\
23947Show the upper bound on the age of cached DWARF compilation units."), _("\
23948A higher limit means that cached compilation units will be stored\n\
23949in memory longer, and more total memory will be used. Zero disables\n\
23950caching, which can slow down startup."),
23951 NULL,
23952 show_dwarf_max_cache_age,
23953 &set_dwarf_cmdlist,
23954 &show_dwarf_cmdlist);
156942c7 23955
cd4fb1b2
SM
23956 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23957Set debugging of the DWARF reader."), _("\
23958Show debugging of the DWARF reader."), _("\
23959When enabled (non-zero), debugging messages are printed during DWARF\n\
23960reading and symtab expansion. A value of 1 (one) provides basic\n\
23961information. A value greater than 1 provides more verbose information."),
23962 NULL,
23963 NULL,
23964 &setdebuglist, &showdebuglist);
9291a0cd 23965
cd4fb1b2
SM
23966 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23967Set debugging of the DWARF DIE reader."), _("\
23968Show debugging of the DWARF DIE reader."), _("\
23969When enabled (non-zero), DIEs are dumped after they are read in.\n\
23970The value is the maximum depth to print."),
23971 NULL,
23972 NULL,
23973 &setdebuglist, &showdebuglist);
9291a0cd 23974
cd4fb1b2
SM
23975 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23976Set debugging of the dwarf line reader."), _("\
23977Show debugging of the dwarf line reader."), _("\
23978When enabled (non-zero), line number entries are dumped as they are read in.\n\
23979A value of 1 (one) provides basic information.\n\
23980A value greater than 1 provides more verbose information."),
23981 NULL,
23982 NULL,
23983 &setdebuglist, &showdebuglist);
437afbb8 23984
cd4fb1b2
SM
23985 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23986Set cross-checking of \"physname\" code against demangler."), _("\
23987Show cross-checking of \"physname\" code against demangler."), _("\
23988When enabled, GDB's internal \"physname\" code is checked against\n\
23989the demangler."),
23990 NULL, show_check_physname,
23991 &setdebuglist, &showdebuglist);
900e11f9 23992
e615022a
DE
23993 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23994 no_class, &use_deprecated_index_sections, _("\
23995Set whether to use deprecated gdb_index sections."), _("\
23996Show whether to use deprecated gdb_index sections."), _("\
23997When enabled, deprecated .gdb_index sections are used anyway.\n\
23998Normally they are ignored either because of a missing feature or\n\
23999performance issue.\n\
24000Warning: This option must be enabled before gdb reads the file."),
24001 NULL,
24002 NULL,
24003 &setlist, &showlist);
24004
f1e6e072
TT
24005 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24006 &dwarf2_locexpr_funcs);
24007 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24008 &dwarf2_loclist_funcs);
24009
24010 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24011 &dwarf2_block_frame_base_locexpr_funcs);
24012 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24013 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24014
24015#if GDB_SELF_TEST
24016 selftests::register_test ("dw2_expand_symtabs_matching",
24017 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24018 selftests::register_test ("dwarf2_find_containing_comp_unit",
24019 selftests::find_containing_comp_unit::run_test);
c62446b1 24020#endif
6502dd73 24021}
This page took 7.346289 seconds and 4 git commands to generate.