gdb: remove TYPE_NAME macro
[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{
fcd3b13d
SM
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
c24bdb02
KS
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
d00adf39 423 /* The header of the compilation unit. */
fcd3b13d 424 struct comp_unit_head header {};
e142c38c 425
d00adf39 426 /* Base address of this compilation unit. */
2b24b6e4 427 gdb::optional<CORE_ADDR> base_address;
d00adf39 428
e142c38c 429 /* The language we are debugging. */
fcd3b13d
SM
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
e142c38c 432
fcd3b13d 433 const char *producer = nullptr;
b0f35d58 434
c24bdb02 435private:
804d2729
TT
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
c24bdb02 438 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 439
c24bdb02 440public:
e142c38c
DJ
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
fcd3b13d 450 struct pending **list_in_scope = nullptr;
e142c38c 451
b64f50a1
JK
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 454 htab_t partial_dies = nullptr;
72bf9492
DJ
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
fcd3b13d 458 auto_obstack comp_unit_obstack;
72bf9492 459
ae038cb0
DJ
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
fcd3b13d 464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 465
69d751e3 466 /* Backlink to our per_cu entry. */
ae038cb0
DJ
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 470 int last_used = 0;
ae038cb0 471
b64f50a1
JK
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
fcd3b13d 474 htab_t die_hash = nullptr;
10b3939b
DJ
475
476 /* Full DIEs if read in. */
fcd3b13d 477 struct die_info *dies = nullptr;
10b3939b
DJ
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
fcd3b13d 482 htab_t dependencies = nullptr;
10b3939b 483
cb1df416 484 /* Header data from the line table, during full symbol processing. */
fcd3b13d 485 struct line_header *line_header = nullptr;
4c8aa72d
PA
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
fcd3b13d 491 die_info *line_header_die_owner = nullptr;
cb1df416 492
3da10d80
KS
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
c89b44cd 495 std::vector<delayed_method_info> method_list;
3da10d80 496
96408a79 497 /* To be copied to symtab->call_site_htab. */
fcd3b13d 498 htab_t call_site_htab = nullptr;
96408a79 499
034e5797
DE
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
18a8505e 504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
fcd3b13d 509 struct dwo_unit *dwo_unit = nullptr;
3019eac3 510
18a8505e 511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 513 gdb::optional<ULONGEST> addr_base;
3019eac3 514
18a8505e 515 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 517 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 524 ULONGEST ranges_base = 0;
2e3cf129 525
41144253 526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
c9317f21
TT
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
18a8505e
AT
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
ae038cb0 543 /* Mark used when releasing cached dies. */
9068261f 544 bool mark : 1;
ae038cb0 545
8be455d7
JK
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 550 bool has_loclist : 1;
ba919b58 551
9068261f 552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
9068261f
AB
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 559 bool producer_is_icc : 1;
9068261f 560 bool producer_is_icc_lt_14 : 1;
c258c396 561 bool producer_is_codewarrior : 1;
4d4ec4e5 562
9068261f 563 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
9068261f 567 bool processing_has_namespace_info : 1;
d590ff25
YQ
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
e7c27a73
DJ
588};
589
094b34ac
DE
590/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593struct stmt_list_hash
594{
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 599 sect_offset line_sect_off;
094b34ac
DE
600};
601
f4dc4d17
DE
602/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605struct type_unit_group
606{
0186c6a7 607 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
197400e8 610 a "per_cu" handle on the symtab. */
094b34ac
DE
611 struct dwarf2_per_cu_data per_cu;
612
0186c6a7
DE
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
a8b3b8e9 616 std::vector<signatured_type *> *tus;
f4dc4d17 617
43f3e411 618 /* The compunit symtab.
094b34ac 619 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
f4dc4d17 622
094b34ac
DE
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
f4dc4d17 625
f4dc4d17
DE
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635};
636
73869dc2 637/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
638
639struct dwo_sections
640{
641 struct dwarf2_section_info abbrev;
3019eac3
DE
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
43988095 644 struct dwarf2_section_info loclists;
09262596
DE
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
3019eac3
DE
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
80626a55
DE
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
fd5866f6 651 std::vector<dwarf2_section_info> types;
3019eac3
DE
652};
653
c88ee1f0 654/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
655
656struct dwo_unit
657{
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 667 struct dwarf2_section_info *section;
3019eac3 668
9c541725
PA
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
3019eac3
DE
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675};
676
73869dc2
DE
677/* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681enum dwp_v2_section_ids
682{
683 DW_SECT_MIN = 1
684};
685
80626a55 686/* Data for one DWO file.
57d63ce2
DE
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
3019eac3
DE
696
697struct dwo_file
698{
51ac9db5
SM
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
18a8505e 702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
51ac9db5 706 const char *dwo_name = nullptr;
0ac5b59e
DE
707
708 /* The DW_AT_comp_dir attribute. */
51ac9db5 709 const char *comp_dir = nullptr;
3019eac3 710
80626a55
DE
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 713 gdb_bfd_ref_ptr dbfd;
3019eac3 714
73869dc2
DE
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
51ac9db5 718 struct dwo_sections sections {};
3019eac3 719
33c5cd75
DB
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 724 htab_up cus;
3019eac3
DE
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
b0b6a987 728 htab_up tus;
3019eac3
DE
729};
730
80626a55
DE
731/* These sections are what may appear in a DWP file. */
732
733struct dwp_sections
734{
73869dc2 735 /* These are used by both DWP version 1 and 2. */
80626a55
DE
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
73869dc2
DE
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
80626a55
DE
756};
757
73869dc2
DE
758/* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 760
73869dc2 761struct virtual_v1_dwo_sections
80626a55
DE
762{
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 770 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
771 struct dwarf2_section_info info_or_types;
772};
773
73869dc2
DE
774/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779struct virtual_v2_dwo_sections
780{
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803};
804
80626a55
DE
805/* Contents of DWP hash tables. */
806
807struct dwp_hash_table
808{
73869dc2 809 uint32_t version, nr_columns;
80626a55 810 uint32_t nr_units, nr_slots;
73869dc2
DE
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822#define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
80626a55
DE
834};
835
836/* Data for one DWP file. */
837
838struct dwp_file
839{
400174b1
TT
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
80626a55
DE
846 /* Name of the file. */
847 const char *name;
848
73869dc2 849 /* File format version. */
400174b1 850 int version = 0;
73869dc2 851
93417882 852 /* The bfd. */
400174b1 853 gdb_bfd_ref_ptr dbfd;
80626a55
DE
854
855 /* Section info for this file. */
400174b1 856 struct dwp_sections sections {};
80626a55 857
57d63ce2 858 /* Table of CUs in the file. */
400174b1 859 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
860
861 /* Table of TUs in the file. */
400174b1 862 const struct dwp_hash_table *tus = nullptr;
80626a55 863
19ac8c2e 864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
865 htab_up loaded_cus;
866 htab_up loaded_tus;
80626a55 867
73869dc2
DE
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
400174b1
TT
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
80626a55
DE
872};
873
0963b4bd
MS
874/* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
dee91e82 877 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
878
879struct die_reader_specs
880{
a32a8923 881 /* The bfd of die_section. */
93311388
DE
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
80626a55 887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
888 struct dwo_file *dwo_file;
889
dee91e82 890 /* The section the die comes from.
3019eac3 891 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
d521ce57 895 const gdb_byte *buffer;
f664829e
DE
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
a2ce51a0 899
685af9cd
TT
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
93311388
DE
902};
903
c0ab21c2
TT
904/* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907class cutu_reader : public die_reader_specs
908{
909public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
6751ebae 913 int use_existing_cu,
c0ab21c2
TT
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
c0ab21c2
TT
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
924 bool dummy_p = false;
925
6751ebae
TT
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
c0ab21c2
TT
930private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 932 int use_existing_cu);
c0ab21c2
TT
933
934 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942};
dee91e82 943
c906108c 944/* When we construct a partial symbol table entry we only
0963b4bd 945 need this much information. */
6f06d47b 946struct partial_die_info : public allocate_on_obstack
c906108c 947 {
6f06d47b
YQ
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
52356b79
YQ
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
48fbe735
YQ
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
72bf9492 965 /* Offset of this DIE. */
6f06d47b 966 const sect_offset sect_off;
72bf9492
DJ
967
968 /* DWARF-2 tag for this DIE. */
6f06d47b 969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 970
72bf9492 971 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
972 const unsigned int has_children : 1;
973
72bf9492
DJ
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
481860b3 979 unsigned int may_be_inlined : 1;
72bf9492 980
0c1b455e
TT
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
72bf9492
DJ
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
fa4028e9
JB
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
ff908ebf
AW
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
98bfdba5
PA
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
52356b79 997 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
998 unsigned int fixup_called : 1;
999
36586728
TT
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
72bf9492 1006 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1007 sometimes a default name for unnamed DIEs. */
6f06d47b 1008 const char *name = nullptr;
72bf9492 1009
abc72ce4 1010 /* The linkage name, if present. */
6f06d47b 1011 const char *linkage_name = nullptr;
abc72ce4 1012
72bf9492
DJ
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
6f06d47b 1016 const char *scope = nullptr;
72bf9492 1017
95554aad
TT
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1025 sect_offset sect_off;
6f06d47b 1026 } d {};
72bf9492
DJ
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
72bf9492 1031
93311388 1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1033 DW_AT_sibling, if any. */
48fbe735
YQ
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1036 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
6f06d47b 1041 sect_offset spec_offset {};
72bf9492
DJ
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
6f06d47b
YQ
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
c906108c
SS
1078 };
1079
c906108c
SS
1080/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083static int bits_per_byte = 8;
1084
9c6a1327
TT
1085struct variant_part_builder;
1086
1087/* When reading a variant, we track a bit more information about the
1088 field, and store it in an object of this type. */
2ddeaf8a
TT
1089
1090struct variant_field
1091{
9c6a1327
TT
1092 int first_field = -1;
1093 int last_field = -1;
1094
1095 /* A variant can contain other variant parts. */
1096 std::vector<variant_part_builder> variant_parts;
1097
2ddeaf8a
TT
1098 /* If we see a DW_TAG_variant, then this will be set if this is the
1099 default branch. */
9c6a1327
TT
1100 bool default_branch = false;
1101 /* If we see a DW_AT_discr_value, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value = 0;
1104 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1105 data. */
1106 struct dwarf_block *discr_list_data = nullptr;
1107};
1108
1109/* This represents a DW_TAG_variant_part. */
1110
1111struct variant_part_builder
1112{
1113 /* The offset of the discriminant field. */
1114 sect_offset discriminant_offset {};
1115
1116 /* Variants that are direct children of this variant part. */
1117 std::vector<variant_field> variants;
1118
1119 /* True if we're currently reading a variant. */
1120 bool processing_variant = false;
2ddeaf8a
TT
1121};
1122
52059ffd
TT
1123struct nextfield
1124{
be2daae6
TT
1125 int accessibility = 0;
1126 int virtuality = 0;
9c6a1327
TT
1127 /* Variant parts need to find the discriminant, which is a DIE
1128 reference. We track the section offset of each field to make
1129 this link. */
1130 sect_offset offset;
be2daae6 1131 struct field field {};
52059ffd
TT
1132};
1133
1134struct fnfieldlist
1135{
be2daae6
TT
1136 const char *name = nullptr;
1137 std::vector<struct fn_field> fnfields;
52059ffd
TT
1138};
1139
c906108c
SS
1140/* The routines that read and process dies for a C struct or C++ class
1141 pass lists of data member fields and lists of member function fields
1142 in an instance of a field_info structure, as defined below. */
1143struct field_info
c5aa993b 1144 {
0963b4bd 1145 /* List of data member and baseclasses fields. */
be2daae6
TT
1146 std::vector<struct nextfield> fields;
1147 std::vector<struct nextfield> baseclasses;
c906108c 1148
85102364 1149 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1150 int non_public_fields = 0;
c906108c 1151
c5aa993b
JM
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
be2daae6 1155 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1156
1157 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1158 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1159 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1160
1161 /* Nested types defined by this class and the number of elements in this
1162 list. */
be2daae6 1163 std::vector<struct decl_field> nested_types_list;
317f7127 1164
9c6a1327
TT
1165 /* If non-null, this is the variant part we are currently
1166 reading. */
1167 variant_part_builder *current_variant_part = nullptr;
1168 /* This holds all the top-level variant parts attached to the type
1169 we're reading. */
1170 std::vector<variant_part_builder> variant_parts;
1171
317f7127
TT
1172 /* Return the total number of fields (including baseclasses). */
1173 int nfields () const
1174 {
1175 return fields.size () + baseclasses.size ();
1176 }
c5aa993b 1177 };
c906108c 1178
ae038cb0
DJ
1179/* Loaded secondary compilation units are kept in memory until they
1180 have not been referenced for the processing of this many
1181 compilation units. Set this to zero to disable caching. Cache
1182 sizes of up to at least twenty will improve startup time for
1183 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1184static int dwarf_max_cache_age = 5;
920d2a44 1185static void
b4f54984
DE
1186show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1187 struct cmd_list_element *c, const char *value)
920d2a44 1188{
3e43a32a 1189 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1190 "DWARF compilation units is %s.\n"),
920d2a44
AC
1191 value);
1192}
4390d890 1193\f
c906108c
SS
1194/* local function prototypes */
1195
918dd910
JK
1196static void dwarf2_find_base_address (struct die_info *die,
1197 struct dwarf2_cu *cu);
1198
891813be 1199static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1200 (struct dwarf2_per_cu_data *per_cu, const char *name);
1201
f1902523
JK
1202static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1203 const gdb_byte *info_ptr,
3e225074 1204 struct die_info *type_unit_die);
f1902523 1205
ed2dc618
SM
1206static void dwarf2_build_psymtabs_hard
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1208
72bf9492
DJ
1209static void scan_partial_symbols (struct partial_die_info *,
1210 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1211 int, struct dwarf2_cu *);
c906108c 1212
72bf9492
DJ
1213static void add_partial_symbol (struct partial_die_info *,
1214 struct dwarf2_cu *);
63d06c5c 1215
72bf9492
DJ
1216static void add_partial_namespace (struct partial_die_info *pdi,
1217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1218 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1219
5d7cb8df 1220static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1221 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1222 struct dwarf2_cu *cu);
1223
72bf9492
DJ
1224static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1225 struct dwarf2_cu *cu);
91c24f0a 1226
bc30ff58
JB
1227static void add_partial_subprogram (struct partial_die_info *pdi,
1228 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1229 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1230
d521ce57 1231static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1232
dee91e82 1233static struct partial_die_info *load_partial_dies
d521ce57 1234 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1235
fb816e8b
TV
1236/* A pair of partial_die_info and compilation unit. */
1237struct cu_partial_die_info
1238{
1239 /* The compilation unit of the partial_die_info. */
1240 struct dwarf2_cu *cu;
1241 /* A partial_die_info. */
1242 struct partial_die_info *pdi;
122cf0f2
AB
1243
1244 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1245 : cu (cu),
1246 pdi (pdi)
405feb71 1247 { /* Nothing. */ }
122cf0f2
AB
1248
1249private:
1250 cu_partial_die_info () = delete;
fb816e8b
TV
1251};
1252
122cf0f2
AB
1253static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1254 struct dwarf2_cu *);
72bf9492 1255
d521ce57
TT
1256static const gdb_byte *read_attribute (const struct die_reader_specs *,
1257 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1258 const gdb_byte *, bool *need_reprocess);
1259
1260static void read_attribute_reprocess (const struct die_reader_specs *reader,
1261 struct attribute *attr);
1262
1263static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1264
ed2dc618
SM
1265static sect_offset read_abbrev_offset
1266 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1267 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1268
ed2dc618
SM
1269static const char *read_indirect_string
1270 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1271 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1272
ed2dc618 1273static const char *read_indirect_string_at_offset
4f44ae6c 1274 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1275
d521ce57
TT
1276static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1277 const gdb_byte *,
3019eac3
DE
1278 unsigned int *);
1279
18a8505e
AT
1280static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1281 ULONGEST str_index);
1282
1283static const char *read_stub_str_index (struct dwarf2_cu *cu,
1284 ULONGEST str_index);
3019eac3 1285
e142c38c 1286static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1287
e142c38c
DJ
1288static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289 struct dwarf2_cu *);
c906108c 1290
7d45c7c3
KB
1291static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1292 struct dwarf2_cu *cu);
1293
a084a2a6
AT
1294static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1295
05cf31d1
JB
1296static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1297 struct dwarf2_cu *cu);
1298
e142c38c 1299static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1300
e142c38c 1301static struct die_info *die_specification (struct die_info *die,
f2f0e013 1302 struct dwarf2_cu **);
63d06c5c 1303
9c541725 1304static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1305 struct dwarf2_cu *cu);
debd256d 1306
f3f5162e 1307static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1308 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1309 CORE_ADDR, int decode_mapping);
c906108c 1310
804d2729
TT
1311static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1312 const char *);
c906108c 1313
a14ed312 1314static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1315 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1316
ff39bb5e 1317static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1318 struct dwarf2_cu *);
c906108c 1319
ff39bb5e 1320static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1321 struct type *type,
1322 const char *name,
1323 struct obstack *obstack,
12df843f 1324 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1325 const gdb_byte **bytes,
98bfdba5 1326 struct dwarf2_locexpr_baton **baton);
2df3850c 1327
e7c27a73 1328static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1329
b4ba55a1
JB
1330static int need_gnat_info (struct dwarf2_cu *);
1331
3e43a32a
MS
1332static struct type *die_descriptive_type (struct die_info *,
1333 struct dwarf2_cu *);
b4ba55a1
JB
1334
1335static void set_descriptive_type (struct type *, struct die_info *,
1336 struct dwarf2_cu *);
1337
e7c27a73
DJ
1338static struct type *die_containing_type (struct die_info *,
1339 struct dwarf2_cu *);
c906108c 1340
ff39bb5e 1341static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1342 struct dwarf2_cu *);
c906108c 1343
f792889a 1344static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1345
673bfd45
DE
1346static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1347
0d5cff50 1348static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1349
6e70227d 1350static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1351 const char *suffix, int physname,
1352 struct dwarf2_cu *cu);
63d06c5c 1353
e7c27a73 1354static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1355
348e048f
DE
1356static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1357
e7c27a73 1358static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1359
e7c27a73 1360static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1361
96408a79
SA
1362static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1363
71a3c369
TT
1364static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1365
ff013f42 1366static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1367 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1368
41144253 1369/* Return the .debug_loclists section to use for cu. */
1370static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1371
3a2b436a 1372/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1373 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1374enum pc_bounds_kind
1375{
e385593e 1376 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1377 PC_BOUNDS_NOT_PRESENT,
1378
e385593e
JK
1379 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1380 were present but they do not form a valid range of PC addresses. */
1381 PC_BOUNDS_INVALID,
1382
3a2b436a
JK
1383 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1384 PC_BOUNDS_RANGES,
1385
1386 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1387 PC_BOUNDS_HIGH_LOW,
1388};
1389
1390static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1391 CORE_ADDR *, CORE_ADDR *,
1392 struct dwarf2_cu *,
891813be 1393 dwarf2_psymtab *);
c906108c 1394
fae299cd
DC
1395static void get_scope_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *);
1398
801e3a5b
JB
1399static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1400 CORE_ADDR, struct dwarf2_cu *);
1401
a14ed312 1402static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1403 struct dwarf2_cu *);
c906108c 1404
a14ed312 1405static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1406 struct type *, struct dwarf2_cu *);
c906108c 1407
a14ed312 1408static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1409 struct die_info *, struct type *,
e7c27a73 1410 struct dwarf2_cu *);
c906108c 1411
a14ed312 1412static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1413 struct type *,
1414 struct dwarf2_cu *);
c906108c 1415
134d01f1 1416static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1417
e7c27a73 1418static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1419
e7c27a73 1420static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1421
5d7cb8df
JK
1422static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1423
804d2729 1424static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1425
27aa8d6a
SW
1426static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1427
74921315
KS
1428static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1429
f55ee35c
JK
1430static struct type *read_module_type (struct die_info *die,
1431 struct dwarf2_cu *cu);
1432
38d518c9 1433static const char *namespace_name (struct die_info *die,
e142c38c 1434 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1435
134d01f1 1436static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1437
7d79de9a
TT
1438static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1439 bool * = nullptr);
c906108c 1440
6e70227d 1441static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1442 struct dwarf2_cu *);
1443
bf6af496 1444static struct die_info *read_die_and_siblings_1
d521ce57 1445 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1446 struct die_info *);
639d11d3 1447
dee91e82 1448static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1449 const gdb_byte *info_ptr,
1450 const gdb_byte **new_info_ptr,
639d11d3
DC
1451 struct die_info *parent);
1452
d521ce57
TT
1453static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1454 struct die_info **, const gdb_byte *,
3e225074 1455 int);
3019eac3 1456
d521ce57 1457static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1458 struct die_info **, const gdb_byte *);
93311388 1459
e7c27a73 1460static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1461
15d034d0 1462static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1463 struct objfile *);
71c25dea 1464
15d034d0 1465static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1466
15d034d0 1467static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1468 struct die_info *die,
1469 struct dwarf2_cu *cu);
1470
ca69b9e6
DE
1471static const char *dwarf2_physname (const char *name, struct die_info *die,
1472 struct dwarf2_cu *cu);
1473
e142c38c 1474static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1475 struct dwarf2_cu **);
9219021c 1476
d97bc12b
DE
1477static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479static void dump_die_for_error (struct die_info *);
1480
1481static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
c906108c 1483
d97bc12b 1484/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1485
51545339 1486static void store_in_ref_table (struct die_info *,
10b3939b 1487 struct dwarf2_cu *);
c906108c 1488
348e048f 1489static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1490 const struct attribute *,
348e048f
DE
1491 struct dwarf2_cu **);
1492
10b3939b 1493static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1494 const struct attribute *,
f2f0e013 1495 struct dwarf2_cu **);
c906108c 1496
348e048f 1497static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1498 const struct attribute *,
348e048f
DE
1499 struct dwarf2_cu **);
1500
ac9ec31b
DE
1501static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1505 const struct attribute *,
ac9ec31b
DE
1506 struct dwarf2_cu *);
1507
e5fe5e75 1508static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1509
52dc124a 1510static void read_signatured_type (struct signatured_type *);
348e048f 1511
63e43d3a
PMR
1512static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1514 struct dynamic_prop *prop, struct type *type);
63e43d3a 1515
c906108c
SS
1516/* memory allocation interface */
1517
7b5a2f43 1518static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1519
b60c80d6 1520static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1521
43f3e411 1522static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1523
8cf6f0b1
TT
1524static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
ff39bb5e 1526 const struct attribute *attr);
8cf6f0b1 1527
ff39bb5e 1528static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1529 struct symbol *sym,
f1e6e072
TT
1530 struct dwarf2_cu *cu,
1531 int is_block);
4c2df51b 1532
d521ce57
TT
1533static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
4bb7a0a7 1536
72bf9492
DJ
1537static hashval_t partial_die_hash (const void *item);
1538
1539static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
ae038cb0 1541static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1544
9816fde3 1545static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
93311388 1548
ed2dc618 1549static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1550
dee91e82 1551static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1552
f792889a
DJ
1553static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1c379e20 1555
ed2dc618 1556static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1557
ed2dc618 1558static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1559
58f0c718 1560static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1561 enum language);
10b3939b 1562
95554aad
TT
1563static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
10b3939b 1565
f4dc4d17
DE
1566static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
10b3939b
DJ
1569static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
ae038cb0
DJ
1572static void dwarf2_mark (struct dwarf2_cu *);
1573
1574static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
b64f50a1 1576static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1577 struct dwarf2_per_cu_data *);
673bfd45 1578
f792889a 1579static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1580
95554aad
TT
1581static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
ed2dc618 1584static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1585
b303c6f6
AB
1586/* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591class dwarf2_queue_guard
1592{
1593public:
39856def
TT
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
b303c6f6
AB
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
39856def
TT
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->queue, empty);
1606 }
b303c6f6 1607
39856def 1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1609
39856def
TT
1610private:
1611 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1612};
1613
39856def
TT
1614dwarf2_queue_item::~dwarf2_queue_item ()
1615{
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624}
1625
d721ba37
PA
1626/* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629struct file_and_directory
1630{
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643};
1644
1645static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
9291a0cd 1647
298e9637 1648static htab_up allocate_signatured_type_table ();
1fd400ff 1649
298e9637 1650static htab_up allocate_dwo_unit_table ();
3019eac3 1651
57d63ce2 1652static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1655 ULONGEST signature, int is_debug_types);
a2ce51a0 1656
ed2dc618
SM
1657static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1659
3019eac3 1660static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1662
1663static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1664 (struct signatured_type *, const char *, const char *);
3019eac3 1665
89e63ee4
DE
1666static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
263db9a1
TT
1668/* A unique pointer to a dwo_file. */
1669
51ac9db5 1670typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1671
ed2dc618 1672static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1673
1b80a9fa 1674static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1675
1676static void free_line_header_voidp (void *arg);
4390d890
DE
1677\f
1678/* Various complaints about symbol reading that don't abort the process. */
1679
4390d890
DE
1680static void
1681dwarf2_debug_line_missing_file_complaint (void)
1682{
b98664d3 1683 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1684}
1685
1686static void
1687dwarf2_debug_line_missing_end_sequence_complaint (void)
1688{
b98664d3 1689 complaint (_(".debug_line section has line "
4390d890
DE
1690 "program sequence without an end"));
1691}
1692
1693static void
1694dwarf2_complex_location_expr_complaint (void)
1695{
b98664d3 1696 complaint (_("location expression too complex"));
4390d890
DE
1697}
1698
1699static void
1700dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1701 int arg3)
1702{
b98664d3 1703 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1704 arg1, arg2, arg3);
1705}
1706
4390d890
DE
1707static void
1708dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1709{
b98664d3 1710 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1711 arg1, arg2);
1712}
527f3840
JK
1713
1714/* Hash function for line_header_hash. */
1715
1716static hashval_t
1717line_header_hash (const struct line_header *ofs)
1718{
9c541725 1719 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1720}
1721
1722/* Hash function for htab_create_alloc_ex for line_header_hash. */
1723
1724static hashval_t
1725line_header_hash_voidp (const void *item)
1726{
9a3c8263 1727 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1728
1729 return line_header_hash (ofs);
1730}
1731
1732/* Equality function for line_header_hash. */
1733
1734static int
1735line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1736{
9a3c8263
SM
1737 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1738 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1739
9c541725 1740 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1741 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1742}
1743
4390d890 1744\f
9291a0cd 1745
330cdd98
PA
1746/* See declaration. */
1747
1748dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1749 const dwarf2_debug_sections *names,
1750 bool can_copy_)
1751 : objfile (objfile_),
1752 can_copy (can_copy_)
330cdd98
PA
1753{
1754 if (names == NULL)
1755 names = &dwarf2_elf_names;
1756
1757 bfd *obfd = objfile->obfd;
1758
1759 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1760 locate_sections (obfd, sec, *names);
1761}
1762
1763dwarf2_per_objfile::~dwarf2_per_objfile ()
1764{
1765 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1766 free_cached_comp_units ();
1767
b76e467d 1768 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1769 per_cu->imported_symtabs_free ();
fc8e7e75 1770
b2bdb8cf 1771 for (signatured_type *sig_type : all_type_units)
ae640021 1772 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1773
330cdd98
PA
1774 /* Everything else should be on the objfile obstack. */
1775}
1776
1777/* See declaration. */
1778
1779void
1780dwarf2_per_objfile::free_cached_comp_units ()
1781{
1782 dwarf2_per_cu_data *per_cu = read_in_chain;
1783 dwarf2_per_cu_data **last_chain = &read_in_chain;
1784 while (per_cu != NULL)
1785 {
1786 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1787
fcd3b13d 1788 delete per_cu->cu;
330cdd98
PA
1789 *last_chain = next_cu;
1790 per_cu = next_cu;
1791 }
1792}
1793
11ed8cad
TT
1794/* A helper class that calls free_cached_comp_units on
1795 destruction. */
1796
1797class free_cached_comp_units
1798{
1799public:
1800
1801 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1802 : m_per_objfile (per_objfile)
1803 {
1804 }
1805
1806 ~free_cached_comp_units ()
1807 {
1808 m_per_objfile->free_cached_comp_units ();
1809 }
1810
1811 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1812
1813private:
1814
1815 dwarf2_per_objfile *m_per_objfile;
1816};
1817
c906108c 1818/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1819 information and return true if we have enough to do something.
1820 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1821 ELF names are used. CAN_COPY is true for formats where symbol
1822 interposition is possible and so symbol values must follow copy
1823 relocation rules. */
c906108c
SS
1824
1825int
251d32d9 1826dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1827 const struct dwarf2_debug_sections *names,
1828 bool can_copy)
c906108c 1829{
97cbe998
SDJ
1830 if (objfile->flags & OBJF_READNEVER)
1831 return 0;
1832
ed2dc618
SM
1833 struct dwarf2_per_objfile *dwarf2_per_objfile
1834 = get_dwarf2_per_objfile (objfile);
1835
1836 if (dwarf2_per_objfile == NULL)
5bfd760d 1837 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1838 names,
1839 can_copy);
5bfd760d 1840
73869dc2 1841 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1842 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1843 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1844 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1845}
1846
251d32d9
TG
1847/* When loading sections, we look either for uncompressed section or for
1848 compressed section names. */
233a11ab
CS
1849
1850static int
251d32d9
TG
1851section_is_p (const char *section_name,
1852 const struct dwarf2_section_names *names)
233a11ab 1853{
251d32d9
TG
1854 if (names->normal != NULL
1855 && strcmp (section_name, names->normal) == 0)
1856 return 1;
1857 if (names->compressed != NULL
1858 && strcmp (section_name, names->compressed) == 0)
1859 return 1;
1860 return 0;
233a11ab
CS
1861}
1862
330cdd98 1863/* See declaration. */
c906108c 1864
330cdd98
PA
1865void
1866dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1867 const dwarf2_debug_sections &names)
c906108c 1868{
fd361982 1869 flagword aflag = bfd_section_flags (sectp);
251d32d9 1870
dc7650b8
JK
1871 if ((aflag & SEC_HAS_CONTENTS) == 0)
1872 {
1873 }
950b7495
KS
1874 else if (elf_section_data (sectp)->this_hdr.sh_size
1875 > bfd_get_file_size (abfd))
1876 {
1877 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1878 warning (_("Discarding section %s which has a section size (%s"
1879 ") larger than the file size [in module %s]"),
1880 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1881 bfd_get_filename (abfd));
1882 }
330cdd98 1883 else if (section_is_p (sectp->name, &names.info))
c906108c 1884 {
330cdd98 1885 this->info.s.section = sectp;
fd361982 1886 this->info.size = bfd_section_size (sectp);
c906108c 1887 }
330cdd98 1888 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1889 {
330cdd98 1890 this->abbrev.s.section = sectp;
fd361982 1891 this->abbrev.size = bfd_section_size (sectp);
c906108c 1892 }
330cdd98 1893 else if (section_is_p (sectp->name, &names.line))
c906108c 1894 {
330cdd98 1895 this->line.s.section = sectp;
fd361982 1896 this->line.size = bfd_section_size (sectp);
c906108c 1897 }
330cdd98 1898 else if (section_is_p (sectp->name, &names.loc))
c906108c 1899 {
330cdd98 1900 this->loc.s.section = sectp;
fd361982 1901 this->loc.size = bfd_section_size (sectp);
c906108c 1902 }
330cdd98 1903 else if (section_is_p (sectp->name, &names.loclists))
43988095 1904 {
330cdd98 1905 this->loclists.s.section = sectp;
fd361982 1906 this->loclists.size = bfd_section_size (sectp);
43988095 1907 }
330cdd98 1908 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1909 {
330cdd98 1910 this->macinfo.s.section = sectp;
fd361982 1911 this->macinfo.size = bfd_section_size (sectp);
c906108c 1912 }
330cdd98 1913 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1914 {
330cdd98 1915 this->macro.s.section = sectp;
fd361982 1916 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1917 }
330cdd98 1918 else if (section_is_p (sectp->name, &names.str))
c906108c 1919 {
330cdd98 1920 this->str.s.section = sectp;
fd361982 1921 this->str.size = bfd_section_size (sectp);
c906108c 1922 }
18a8505e
AT
1923 else if (section_is_p (sectp->name, &names.str_offsets))
1924 {
1925 this->str_offsets.s.section = sectp;
1926 this->str_offsets.size = bfd_section_size (sectp);
1927 }
330cdd98 1928 else if (section_is_p (sectp->name, &names.line_str))
43988095 1929 {
330cdd98 1930 this->line_str.s.section = sectp;
fd361982 1931 this->line_str.size = bfd_section_size (sectp);
43988095 1932 }
330cdd98 1933 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1934 {
330cdd98 1935 this->addr.s.section = sectp;
fd361982 1936 this->addr.size = bfd_section_size (sectp);
3019eac3 1937 }
330cdd98 1938 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1939 {
330cdd98 1940 this->frame.s.section = sectp;
fd361982 1941 this->frame.size = bfd_section_size (sectp);
b6af0555 1942 }
330cdd98 1943 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1944 {
330cdd98 1945 this->eh_frame.s.section = sectp;
fd361982 1946 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1947 }
330cdd98 1948 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1949 {
330cdd98 1950 this->ranges.s.section = sectp;
fd361982 1951 this->ranges.size = bfd_section_size (sectp);
af34e669 1952 }
330cdd98 1953 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1954 {
330cdd98 1955 this->rnglists.s.section = sectp;
fd361982 1956 this->rnglists.size = bfd_section_size (sectp);
43988095 1957 }
330cdd98 1958 else if (section_is_p (sectp->name, &names.types))
348e048f 1959 {
8b70b953
TT
1960 struct dwarf2_section_info type_section;
1961
1962 memset (&type_section, 0, sizeof (type_section));
049412e3 1963 type_section.s.section = sectp;
fd361982 1964 type_section.size = bfd_section_size (sectp);
8b70b953 1965
fd5866f6 1966 this->types.push_back (type_section);
348e048f 1967 }
330cdd98 1968 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1969 {
330cdd98 1970 this->gdb_index.s.section = sectp;
fd361982 1971 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1972 }
927aa2e7
JK
1973 else if (section_is_p (sectp->name, &names.debug_names))
1974 {
1975 this->debug_names.s.section = sectp;
fd361982 1976 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1977 }
1978 else if (section_is_p (sectp->name, &names.debug_aranges))
1979 {
1980 this->debug_aranges.s.section = sectp;
fd361982 1981 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1982 }
dce234bc 1983
fd361982
AM
1984 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1985 && bfd_section_vma (sectp) == 0)
330cdd98 1986 this->has_section_at_zero = true;
c906108c
SS
1987}
1988
dce234bc 1989/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1990 SECTION_NAME. */
af34e669 1991
dce234bc 1992void
3017a003
TG
1993dwarf2_get_section_info (struct objfile *objfile,
1994 enum dwarf2_section_enum sect,
d521ce57 1995 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1996 bfd_size_type *sizep)
1997{
5bfd760d 1998 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 1999 struct dwarf2_section_info *info;
a3b2a86b
TT
2000
2001 /* We may see an objfile without any DWARF, in which case we just
2002 return nothing. */
2003 if (data == NULL)
2004 {
2005 *sectp = NULL;
2006 *bufp = NULL;
2007 *sizep = 0;
2008 return;
2009 }
3017a003
TG
2010 switch (sect)
2011 {
2012 case DWARF2_DEBUG_FRAME:
2013 info = &data->frame;
2014 break;
2015 case DWARF2_EH_FRAME:
2016 info = &data->eh_frame;
2017 break;
2018 default:
2019 gdb_assert_not_reached ("unexpected section");
2020 }
dce234bc 2021
96b79293 2022 info->read (objfile);
dce234bc 2023
96b79293 2024 *sectp = info->get_bfd_section ();
dce234bc
PP
2025 *bufp = info->buffer;
2026 *sizep = info->size;
2027}
2028
36586728
TT
2029/* A helper function to find the sections for a .dwz file. */
2030
2031static void
2032locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2033{
9a3c8263 2034 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2035
2036 /* Note that we only support the standard ELF names, because .dwz
2037 is ELF-only (at the time of writing). */
2038 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2039 {
049412e3 2040 dwz_file->abbrev.s.section = sectp;
fd361982 2041 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2042 }
2043 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2044 {
049412e3 2045 dwz_file->info.s.section = sectp;
fd361982 2046 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2047 }
2048 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2049 {
049412e3 2050 dwz_file->str.s.section = sectp;
fd361982 2051 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2052 }
2053 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2054 {
049412e3 2055 dwz_file->line.s.section = sectp;
fd361982 2056 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2057 }
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2059 {
049412e3 2060 dwz_file->macro.s.section = sectp;
fd361982 2061 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2062 }
2ec9a5e0
TT
2063 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2064 {
049412e3 2065 dwz_file->gdb_index.s.section = sectp;
fd361982 2066 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2067 }
927aa2e7
JK
2068 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2069 {
2070 dwz_file->debug_names.s.section = sectp;
fd361982 2071 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2072 }
36586728
TT
2073}
2074
c4973306 2075/* See dwarf2read.h. */
36586728 2076
c4973306 2077struct dwz_file *
ed2dc618 2078dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2079{
36586728 2080 const char *filename;
acd13123 2081 bfd_size_type buildid_len_arg;
dc294be5
TT
2082 size_t buildid_len;
2083 bfd_byte *buildid;
36586728
TT
2084
2085 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2086 return dwarf2_per_objfile->dwz_file.get ();
36586728 2087
4db1a1dc 2088 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2089 gdb::unique_xmalloc_ptr<char> data
2090 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2091 &buildid_len_arg, &buildid));
4db1a1dc
TT
2092 if (data == NULL)
2093 {
2094 if (bfd_get_error () == bfd_error_no_error)
2095 return NULL;
2096 error (_("could not read '.gnu_debugaltlink' section: %s"),
2097 bfd_errmsg (bfd_get_error ()));
2098 }
791afaa2
TT
2099
2100 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2101
acd13123
TT
2102 buildid_len = (size_t) buildid_len_arg;
2103
791afaa2 2104 filename = data.get ();
d721ba37
PA
2105
2106 std::string abs_storage;
36586728
TT
2107 if (!IS_ABSOLUTE_PATH (filename))
2108 {
14278e1f
TT
2109 gdb::unique_xmalloc_ptr<char> abs
2110 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2111
14278e1f 2112 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2113 filename = abs_storage.c_str ();
36586728
TT
2114 }
2115
dc294be5
TT
2116 /* First try the file name given in the section. If that doesn't
2117 work, try to use the build-id instead. */
192b62ce 2118 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2119 if (dwz_bfd != NULL)
36586728 2120 {
192b62ce 2121 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2122 dwz_bfd.reset (nullptr);
36586728
TT
2123 }
2124
dc294be5
TT
2125 if (dwz_bfd == NULL)
2126 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2127
0d79cdc4
AM
2128 if (dwz_bfd == nullptr)
2129 {
2130 gdb::unique_xmalloc_ptr<char> alt_filename;
2131 const char *origname = dwarf2_per_objfile->objfile->original_name;
2132
2133 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2134 buildid_len,
2135 origname,
2136 &alt_filename));
2137
2138 if (fd.get () >= 0)
2139 {
2140 /* File successfully retrieved from server. */
2141 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2142
2143 if (dwz_bfd == nullptr)
2144 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2145 alt_filename.get ());
2146 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2147 dwz_bfd.reset (nullptr);
2148 }
2149 }
2150
dc294be5
TT
2151 if (dwz_bfd == NULL)
2152 error (_("could not find '.gnu_debugaltlink' file for %s"),
2153 objfile_name (dwarf2_per_objfile->objfile));
2154
7ff8cb8c
TT
2155 std::unique_ptr<struct dwz_file> result
2156 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2157
7ff8cb8c
TT
2158 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2159 result.get ());
36586728 2160
7ff8cb8c
TT
2161 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2162 result->dwz_bfd.get ());
2163 dwarf2_per_objfile->dwz_file = std::move (result);
2164 return dwarf2_per_objfile->dwz_file.get ();
36586728 2165}
9291a0cd 2166\f
7b9f3c50
DE
2167/* DWARF quick_symbols_functions support. */
2168
2169/* TUs can share .debug_line entries, and there can be a lot more TUs than
2170 unique line tables, so we maintain a separate table of all .debug_line
2171 derived entries to support the sharing.
2172 All the quick functions need is the list of file names. We discard the
2173 line_header when we're done and don't need to record it here. */
2174struct quick_file_names
2175{
094b34ac
DE
2176 /* The data used to construct the hash key. */
2177 struct stmt_list_hash hash;
7b9f3c50
DE
2178
2179 /* The number of entries in file_names, real_names. */
2180 unsigned int num_file_names;
2181
2182 /* The file names from the line table, after being run through
2183 file_full_name. */
2184 const char **file_names;
2185
2186 /* The file names from the line table after being run through
2187 gdb_realpath. These are computed lazily. */
2188 const char **real_names;
2189};
2190
2191/* When using the index (and thus not using psymtabs), each CU has an
2192 object of this type. This is used to hold information needed by
2193 the various "quick" methods. */
2194struct dwarf2_per_cu_quick_data
2195{
2196 /* The file table. This can be NULL if there was no file table
2197 or it's currently not read in.
2198 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2199 struct quick_file_names *file_names;
2200
2201 /* The corresponding symbol table. This is NULL if symbols for this
2202 CU have not yet been read. */
43f3e411 2203 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2204
2205 /* A temporary mark bit used when iterating over all CUs in
2206 expand_symtabs_matching. */
2207 unsigned int mark : 1;
2208
2209 /* True if we've tried to read the file table and found there isn't one.
2210 There will be no point in trying to read it again next time. */
2211 unsigned int no_file_data : 1;
2212};
2213
094b34ac
DE
2214/* Utility hash function for a stmt_list_hash. */
2215
2216static hashval_t
2217hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2218{
2219 hashval_t v = 0;
2220
2221 if (stmt_list_hash->dwo_unit != NULL)
2222 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2223 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2224 return v;
2225}
2226
2227/* Utility equality function for a stmt_list_hash. */
2228
2229static int
2230eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2231 const struct stmt_list_hash *rhs)
2232{
2233 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2234 return 0;
2235 if (lhs->dwo_unit != NULL
2236 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2237 return 0;
2238
9c541725 2239 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2240}
2241
7b9f3c50
DE
2242/* Hash function for a quick_file_names. */
2243
2244static hashval_t
2245hash_file_name_entry (const void *e)
2246{
9a3c8263
SM
2247 const struct quick_file_names *file_data
2248 = (const struct quick_file_names *) e;
7b9f3c50 2249
094b34ac 2250 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2251}
2252
2253/* Equality function for a quick_file_names. */
2254
2255static int
2256eq_file_name_entry (const void *a, const void *b)
2257{
9a3c8263
SM
2258 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2259 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2260
094b34ac 2261 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2262}
2263
2264/* Delete function for a quick_file_names. */
2265
2266static void
2267delete_file_name_entry (void *e)
2268{
9a3c8263 2269 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2270 int i;
2271
2272 for (i = 0; i < file_data->num_file_names; ++i)
2273 {
2274 xfree ((void*) file_data->file_names[i]);
2275 if (file_data->real_names)
2276 xfree ((void*) file_data->real_names[i]);
2277 }
2278
2279 /* The space for the struct itself lives on objfile_obstack,
2280 so we don't free it here. */
2281}
2282
2283/* Create a quick_file_names hash table. */
2284
5895093f 2285static htab_up
7b9f3c50
DE
2286create_quick_file_names_table (unsigned int nr_initial_entries)
2287{
5895093f
TT
2288 return htab_up (htab_create_alloc (nr_initial_entries,
2289 hash_file_name_entry, eq_file_name_entry,
2290 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2291}
9291a0cd 2292
918dd910
JK
2293/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2294 have to be created afterwards. You should call age_cached_comp_units after
2295 processing PER_CU->CU. dw2_setup must have been already called. */
2296
2297static void
58f0c718 2298load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2299{
3019eac3 2300 if (per_cu->is_debug_types)
e5fe5e75 2301 load_full_type_unit (per_cu);
918dd910 2302 else
58f0c718 2303 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2304
cc12ce38
DE
2305 if (per_cu->cu == NULL)
2306 return; /* Dummy CU. */
2dc860c0
DE
2307
2308 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2309}
2310
a0f42c21 2311/* Read in the symbols for PER_CU. */
2fdf6df6 2312
9291a0cd 2313static void
58f0c718 2314dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2315{
ed2dc618 2316 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2317
f4dc4d17
DE
2318 /* Skip type_unit_groups, reading the type units they contain
2319 is handled elsewhere. */
197400e8 2320 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2321 return;
2322
b303c6f6
AB
2323 /* The destructor of dwarf2_queue_guard frees any entries left on
2324 the queue. After this point we're guaranteed to leave this function
2325 with the dwarf queue empty. */
39856def 2326 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2327
95554aad 2328 if (dwarf2_per_objfile->using_index
43f3e411 2329 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2330 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2331 {
2332 queue_comp_unit (per_cu, language_minimal);
58f0c718 2333 load_cu (per_cu, skip_partial);
89e63ee4
DE
2334
2335 /* If we just loaded a CU from a DWO, and we're working with an index
2336 that may badly handle TUs, load all the TUs in that DWO as well.
2337 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2338 if (!per_cu->is_debug_types
cc12ce38 2339 && per_cu->cu != NULL
89e63ee4
DE
2340 && per_cu->cu->dwo_unit != NULL
2341 && dwarf2_per_objfile->index_table != NULL
2342 && dwarf2_per_objfile->index_table->version <= 7
2343 /* DWP files aren't supported yet. */
ed2dc618 2344 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2345 queue_and_load_all_dwo_tus (per_cu);
95554aad 2346 }
9291a0cd 2347
ed2dc618 2348 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2349
2350 /* Age the cache, releasing compilation units that have not
2351 been used recently. */
ed2dc618 2352 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2353}
2354
2355/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2357 table. */
2fdf6df6 2358
43f3e411 2359static struct compunit_symtab *
58f0c718 2360dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2361{
ed2dc618
SM
2362 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2363
95554aad 2364 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2365 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2366 {
11ed8cad 2367 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2368 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2369 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2370 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2371 }
f194fefb 2372
43f3e411 2373 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2374}
2375
ff4c9fec 2376/* See declaration. */
f4dc4d17 2377
ff4c9fec
SM
2378dwarf2_per_cu_data *
2379dwarf2_per_objfile::get_cutu (int index)
2380{
b76e467d 2381 if (index >= this->all_comp_units.size ())
ff4c9fec 2382 {
b76e467d 2383 index -= this->all_comp_units.size ();
b2bdb8cf 2384 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2385 return &this->all_type_units[index]->per_cu;
2386 }
f4dc4d17 2387
ff4c9fec
SM
2388 return this->all_comp_units[index];
2389}
f4dc4d17 2390
ff4c9fec 2391/* See declaration. */
2fdf6df6 2392
ff4c9fec
SM
2393dwarf2_per_cu_data *
2394dwarf2_per_objfile::get_cu (int index)
1fd400ff 2395{
b76e467d 2396 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2397
ff4c9fec 2398 return this->all_comp_units[index];
f4dc4d17
DE
2399}
2400
ff4c9fec 2401/* See declaration. */
f4dc4d17 2402
ff4c9fec
SM
2403signatured_type *
2404dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2405{
b2bdb8cf 2406 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2407
ff4c9fec 2408 return this->all_type_units[index];
1fd400ff
TT
2409}
2410
4b514bc8
JK
2411/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2412 objfile_obstack, and constructed with the specified field
2413 values. */
2414
2415static dwarf2_per_cu_data *
ed2dc618 2416create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2417 struct dwarf2_section_info *section,
2418 int is_dwz,
2419 sect_offset sect_off, ULONGEST length)
2420{
ed2dc618 2421 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2422 dwarf2_per_cu_data *the_cu
2423 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2424 struct dwarf2_per_cu_data);
2425 the_cu->sect_off = sect_off;
2426 the_cu->length = length;
e3b94546 2427 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2428 the_cu->section = section;
2429 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2430 struct dwarf2_per_cu_quick_data);
2431 the_cu->is_dwz = is_dwz;
2432 return the_cu;
2433}
2434
2ec9a5e0
TT
2435/* A helper for create_cus_from_index that handles a given list of
2436 CUs. */
2fdf6df6 2437
74a0d9f6 2438static void
12359b5e 2439create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2440 const gdb_byte *cu_list, offset_type n_elements,
2441 struct dwarf2_section_info *section,
b76e467d 2442 int is_dwz)
9291a0cd 2443{
12359b5e 2444 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2445 {
74a0d9f6 2446 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2447
2448 sect_offset sect_off
2449 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2450 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2451 cu_list += 2 * 8;
2452
b76e467d 2453 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2454 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2455 sect_off, length);
b76e467d 2456 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2457 }
9291a0cd
TT
2458}
2459
2ec9a5e0 2460/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2461 the CU objects for this objfile. */
2ec9a5e0 2462
74a0d9f6 2463static void
12359b5e 2464create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2465 const gdb_byte *cu_list, offset_type cu_list_elements,
2466 const gdb_byte *dwz_list, offset_type dwz_elements)
2467{
b76e467d
SM
2468 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2469 dwarf2_per_objfile->all_comp_units.reserve
2470 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2471
12359b5e 2472 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2473 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2474
2475 if (dwz_elements == 0)
74a0d9f6 2476 return;
2ec9a5e0 2477
12359b5e
SM
2478 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2479 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2480 &dwz->info, 1);
2ec9a5e0
TT
2481}
2482
1fd400ff 2483/* Create the signatured type hash table from the index. */
673bfd45 2484
74a0d9f6 2485static void
12359b5e
SM
2486create_signatured_type_table_from_index
2487 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2488 struct dwarf2_section_info *section,
2489 const gdb_byte *bytes,
2490 offset_type elements)
1fd400ff 2491{
12359b5e 2492 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2493
b2bdb8cf
SM
2494 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2495 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2496
298e9637 2497 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2498
12359b5e 2499 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2500 {
52dc124a 2501 struct signatured_type *sig_type;
9c541725 2502 ULONGEST signature;
1fd400ff 2503 void **slot;
9c541725 2504 cu_offset type_offset_in_tu;
1fd400ff 2505
74a0d9f6 2506 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2509 type_offset_in_tu
2510 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2511 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2512 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2513 bytes += 3 * 8;
2514
52dc124a 2515 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2516 struct signatured_type);
52dc124a 2517 sig_type->signature = signature;
9c541725 2518 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2519 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2520 sig_type->per_cu.section = section;
9c541725 2521 sig_type->per_cu.sect_off = sect_off;
e3b94546 2522 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2523 sig_type->per_cu.v.quick
1fd400ff
TT
2524 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2525 struct dwarf2_per_cu_quick_data);
2526
b0b6a987 2527 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2528 *slot = sig_type;
1fd400ff 2529
b2bdb8cf 2530 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2531 }
2532
b0b6a987 2533 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2534}
2535
927aa2e7
JK
2536/* Create the signatured type hash table from .debug_names. */
2537
2538static void
2539create_signatured_type_table_from_debug_names
ed2dc618 2540 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2541 const mapped_debug_names &map,
2542 struct dwarf2_section_info *section,
2543 struct dwarf2_section_info *abbrev_section)
2544{
ed2dc618
SM
2545 struct objfile *objfile = dwarf2_per_objfile->objfile;
2546
96b79293
TT
2547 section->read (objfile);
2548 abbrev_section->read (objfile);
927aa2e7 2549
b2bdb8cf
SM
2550 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2551 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2552
298e9637 2553 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2554
2555 for (uint32_t i = 0; i < map.tu_count; ++i)
2556 {
2557 struct signatured_type *sig_type;
927aa2e7 2558 void **slot;
927aa2e7
JK
2559
2560 sect_offset sect_off
2561 = (sect_offset) (extract_unsigned_integer
2562 (map.tu_table_reordered + i * map.offset_size,
2563 map.offset_size,
2564 map.dwarf5_byte_order));
2565
2566 comp_unit_head cu_header;
ed2dc618
SM
2567 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2568 abbrev_section,
927aa2e7
JK
2569 section->buffer + to_underlying (sect_off),
2570 rcuh_kind::TYPE);
2571
2572 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2573 struct signatured_type);
2574 sig_type->signature = cu_header.signature;
2575 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2576 sig_type->per_cu.is_debug_types = 1;
2577 sig_type->per_cu.section = section;
2578 sig_type->per_cu.sect_off = sect_off;
e3b94546 2579 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2580 sig_type->per_cu.v.quick
2581 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2582 struct dwarf2_per_cu_quick_data);
2583
b0b6a987 2584 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2585 *slot = sig_type;
2586
b2bdb8cf 2587 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2588 }
2589
b0b6a987 2590 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2591}
2592
9291a0cd
TT
2593/* Read the address map data from the mapped index, and use it to
2594 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2595
9291a0cd 2596static void
ed2dc618
SM
2597create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2598 struct mapped_index *index)
9291a0cd 2599{
ed2dc618 2600 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2601 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2602 const gdb_byte *iter, *end;
9291a0cd 2603 struct addrmap *mutable_map;
9291a0cd
TT
2604 CORE_ADDR baseaddr;
2605
8268c778
PA
2606 auto_obstack temp_obstack;
2607
9291a0cd
TT
2608 mutable_map = addrmap_create_mutable (&temp_obstack);
2609
f00a2de2
PA
2610 iter = index->address_table.data ();
2611 end = iter + index->address_table.size ();
9291a0cd 2612
b3b3bada 2613 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2614
2615 while (iter < end)
2616 {
2617 ULONGEST hi, lo, cu_index;
2618 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2619 iter += 8;
2620 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2621 iter += 8;
2622 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2623 iter += 4;
f652bce2 2624
24a55014 2625 if (lo > hi)
f652bce2 2626 {
b98664d3 2627 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2628 hex_string (lo), hex_string (hi));
24a55014 2629 continue;
f652bce2 2630 }
24a55014 2631
b76e467d 2632 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2633 {
b98664d3 2634 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2635 (unsigned) cu_index);
24a55014 2636 continue;
f652bce2 2637 }
24a55014 2638
79748972
TT
2639 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2640 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2641 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2642 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2643 }
2644
d320c2b5 2645 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2646 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2647}
2648
927aa2e7
JK
2649/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2650 populate the objfile's psymtabs_addrmap. */
2651
2652static void
ed2dc618 2653create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2654 struct dwarf2_section_info *section)
2655{
ed2dc618 2656 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2657 bfd *abfd = objfile->obfd;
08feed99 2658 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2659 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2660
2661 auto_obstack temp_obstack;
2662 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2663
2664 std::unordered_map<sect_offset,
2665 dwarf2_per_cu_data *,
2666 gdb::hash_enum<sect_offset>>
2667 debug_info_offset_to_per_cu;
b76e467d 2668 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2669 {
927aa2e7
JK
2670 const auto insertpair
2671 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2672 if (!insertpair.second)
2673 {
2674 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2675 "debug_info_offset %s, ignoring .debug_aranges."),
2676 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2677 return;
2678 }
2679 }
2680
96b79293 2681 section->read (objfile);
927aa2e7
JK
2682
2683 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2684
2685 const gdb_byte *addr = section->buffer;
2686
2687 while (addr < section->buffer + section->size)
2688 {
2689 const gdb_byte *const entry_addr = addr;
2690 unsigned int bytes_read;
2691
2692 const LONGEST entry_length = read_initial_length (abfd, addr,
2693 &bytes_read);
2694 addr += bytes_read;
2695
2696 const gdb_byte *const entry_end = addr + entry_length;
2697 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2698 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2699 if (addr + entry_length > section->buffer + section->size)
2700 {
47e3f474 2701 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2702 "length %s exceeds section length %s, "
2703 "ignoring .debug_aranges."),
47e3f474
TV
2704 objfile_name (objfile),
2705 plongest (entry_addr - section->buffer),
927aa2e7
JK
2706 plongest (bytes_read + entry_length),
2707 pulongest (section->size));
2708 return;
2709 }
2710
2711 /* The version number. */
2712 const uint16_t version = read_2_bytes (abfd, addr);
2713 addr += 2;
2714 if (version != 2)
2715 {
47e3f474 2716 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2717 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2718 objfile_name (objfile),
2719 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2720 return;
2721 }
2722
2723 const uint64_t debug_info_offset
2724 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2725 addr += offset_size;
2726 const auto per_cu_it
2727 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2728 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2729 {
47e3f474 2730 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2731 "debug_info_offset %s does not exists, "
2732 "ignoring .debug_aranges."),
47e3f474
TV
2733 objfile_name (objfile),
2734 plongest (entry_addr - section->buffer),
927aa2e7
JK
2735 pulongest (debug_info_offset));
2736 return;
2737 }
2738 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2739
2740 const uint8_t address_size = *addr++;
2741 if (address_size < 1 || address_size > 8)
2742 {
47e3f474 2743 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2744 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2745 objfile_name (objfile),
2746 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2747 return;
2748 }
2749
2750 const uint8_t segment_selector_size = *addr++;
2751 if (segment_selector_size != 0)
2752 {
47e3f474 2753 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2754 "segment_selector_size %u is not supported, "
2755 "ignoring .debug_aranges."),
47e3f474
TV
2756 objfile_name (objfile),
2757 plongest (entry_addr - section->buffer),
927aa2e7
JK
2758 segment_selector_size);
2759 return;
2760 }
2761
2762 /* Must pad to an alignment boundary that is twice the address
2763 size. It is undocumented by the DWARF standard but GCC does
2764 use it. */
2765 for (size_t padding = ((-(addr - section->buffer))
2766 & (2 * address_size - 1));
2767 padding > 0; padding--)
2768 if (*addr++ != 0)
2769 {
47e3f474 2770 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2771 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer));
927aa2e7
JK
2774 return;
2775 }
2776
2777 for (;;)
2778 {
2779 if (addr + 2 * address_size > entry_end)
2780 {
47e3f474 2781 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2782 "address list is not properly terminated, "
2783 "ignoring .debug_aranges."),
47e3f474
TV
2784 objfile_name (objfile),
2785 plongest (entry_addr - section->buffer));
927aa2e7
JK
2786 return;
2787 }
2788 ULONGEST start = extract_unsigned_integer (addr, address_size,
2789 dwarf5_byte_order);
2790 addr += address_size;
2791 ULONGEST length = extract_unsigned_integer (addr, address_size,
2792 dwarf5_byte_order);
2793 addr += address_size;
2794 if (start == 0 && length == 0)
2795 break;
2796 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2797 {
2798 /* Symbol was eliminated due to a COMDAT group. */
2799 continue;
2800 }
2801 ULONGEST end = start + length;
79748972
TT
2802 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2803 - baseaddr);
2804 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2805 - baseaddr);
927aa2e7
JK
2806 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2807 }
2808 }
2809
d320c2b5 2810 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2811 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2812}
2813
9291a0cd
TT
2814/* Find a slot in the mapped index INDEX for the object named NAME.
2815 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2816 constant pool and return true. If NAME cannot be found, return
2817 false. */
2fdf6df6 2818
109483d9 2819static bool
9291a0cd
TT
2820find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2821 offset_type **vec_out)
2822{
0cf03b49 2823 offset_type hash;
9291a0cd 2824 offset_type slot, step;
559a7a62 2825 int (*cmp) (const char *, const char *);
9291a0cd 2826
791afaa2 2827 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2828 if (current_language->la_language == language_cplus
45280282
IB
2829 || current_language->la_language == language_fortran
2830 || current_language->la_language == language_d)
0cf03b49
JK
2831 {
2832 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2833 not contain any. */
a8719064 2834
72998fb3 2835 if (strchr (name, '(') != NULL)
0cf03b49 2836 {
109483d9 2837 without_params = cp_remove_params (name);
0cf03b49 2838
72998fb3 2839 if (without_params != NULL)
791afaa2 2840 name = without_params.get ();
0cf03b49
JK
2841 }
2842 }
2843
559a7a62 2844 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2845 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2846 simulate our NAME being searched is also lowercased. */
2847 hash = mapped_index_string_hash ((index->version == 4
2848 && case_sensitivity == case_sensitive_off
2849 ? 5 : index->version),
2850 name);
2851
f00a2de2
PA
2852 slot = hash & (index->symbol_table.size () - 1);
2853 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2854 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2855
2856 for (;;)
2857 {
9291a0cd 2858 const char *str;
f00a2de2
PA
2859
2860 const auto &bucket = index->symbol_table[slot];
2861 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2862 return false;
9291a0cd 2863
f00a2de2 2864 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2865 if (!cmp (name, str))
9291a0cd
TT
2866 {
2867 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2868 + MAYBE_SWAP (bucket.vec));
109483d9 2869 return true;
9291a0cd
TT
2870 }
2871
f00a2de2 2872 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2873 }
2874}
2875
4485a1c1
SM
2876/* A helper function that reads the .gdb_index from BUFFER and fills
2877 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2878 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2879 ok to use deprecated sections.
2880
2881 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2882 out parameters that are filled in with information about the CU and
2883 TU lists in the section.
2884
4485a1c1 2885 Returns true if all went well, false otherwise. */
2fdf6df6 2886
d33bc52e 2887static bool
3810f182 2888read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2889 bool deprecated_ok,
2890 gdb::array_view<const gdb_byte> buffer,
2891 struct mapped_index *map,
2892 const gdb_byte **cu_list,
2893 offset_type *cu_list_elements,
2894 const gdb_byte **types_list,
2895 offset_type *types_list_elements)
2896{
2897 const gdb_byte *addr = &buffer[0];
82430852 2898
9291a0cd 2899 /* Version check. */
4485a1c1 2900 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2901 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2902 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2903 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2904 indices. */
831adc1f 2905 if (version < 4)
481860b3
GB
2906 {
2907 static int warning_printed = 0;
2908 if (!warning_printed)
2909 {
2910 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2911 filename);
481860b3
GB
2912 warning_printed = 1;
2913 }
2914 return 0;
2915 }
2916 /* Index version 4 uses a different hash function than index version
2917 5 and later.
2918
2919 Versions earlier than 6 did not emit psymbols for inlined
2920 functions. Using these files will cause GDB not to be able to
2921 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2922 indices unless the user has done
2923 "set use-deprecated-index-sections on". */
2ec9a5e0 2924 if (version < 6 && !deprecated_ok)
481860b3
GB
2925 {
2926 static int warning_printed = 0;
2927 if (!warning_printed)
2928 {
e615022a
DE
2929 warning (_("\
2930Skipping deprecated .gdb_index section in %s.\n\
2931Do \"set use-deprecated-index-sections on\" before the file is read\n\
2932to use the section anyway."),
2ec9a5e0 2933 filename);
481860b3
GB
2934 warning_printed = 1;
2935 }
2936 return 0;
2937 }
796a7ff8 2938 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2939 of the TU (for symbols coming from TUs),
2940 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2941 Plus gold-generated indices can have duplicate entries for global symbols,
2942 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2943 These are just performance bugs, and we can't distinguish gdb-generated
2944 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2945
481860b3 2946 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2947 longer backward compatible. */
796a7ff8 2948 if (version > 8)
594e8718 2949 return 0;
9291a0cd 2950
559a7a62 2951 map->version = version;
9291a0cd 2952
4485a1c1 2953 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2954
4485a1c1 2955 int i = 0;
2ec9a5e0
TT
2956 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2957 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2958 / 8);
1fd400ff
TT
2959 ++i;
2960
2ec9a5e0
TT
2961 *types_list = addr + MAYBE_SWAP (metadata[i]);
2962 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2963 - MAYBE_SWAP (metadata[i]))
2964 / 8);
987d643c 2965 ++i;
1fd400ff 2966
f00a2de2
PA
2967 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2968 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2969 map->address_table
2970 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2971 ++i;
2972
f00a2de2
PA
2973 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2974 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2975 map->symbol_table
2976 = gdb::array_view<mapped_index::symbol_table_slot>
2977 ((mapped_index::symbol_table_slot *) symbol_table,
2978 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2979
f00a2de2 2980 ++i;
f9d83a0b 2981 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2982
2ec9a5e0
TT
2983 return 1;
2984}
2985
4485a1c1
SM
2986/* Callback types for dwarf2_read_gdb_index. */
2987
2988typedef gdb::function_view
2989 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2990 get_gdb_index_contents_ftype;
2991typedef gdb::function_view
2992 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2993 get_gdb_index_contents_dwz_ftype;
2994
927aa2e7 2995/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2996 elements of all the CUs and return 1. Otherwise, return 0. */
2997
2998static int
4485a1c1
SM
2999dwarf2_read_gdb_index
3000 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3001 get_gdb_index_contents_ftype get_gdb_index_contents,
3002 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3003{
2ec9a5e0
TT
3004 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3005 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3006 struct dwz_file *dwz;
12359b5e 3007 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3008
4485a1c1
SM
3009 gdb::array_view<const gdb_byte> main_index_contents
3010 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3011
3012 if (main_index_contents.empty ())
3013 return 0;
3014
3063847f 3015 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3016 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3017 use_deprecated_index_sections,
3018 main_index_contents, map.get (), &cu_list,
3019 &cu_list_elements, &types_list,
3020 &types_list_elements))
2ec9a5e0
TT
3021 return 0;
3022
0fefef59 3023 /* Don't use the index if it's empty. */
3063847f 3024 if (map->symbol_table.empty ())
0fefef59
DE
3025 return 0;
3026
2ec9a5e0
TT
3027 /* If there is a .dwz file, read it so we can get its CU list as
3028 well. */
ed2dc618 3029 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3030 if (dwz != NULL)
2ec9a5e0 3031 {
2ec9a5e0
TT
3032 struct mapped_index dwz_map;
3033 const gdb_byte *dwz_types_ignore;
3034 offset_type dwz_types_elements_ignore;
3035
4485a1c1
SM
3036 gdb::array_view<const gdb_byte> dwz_index_content
3037 = get_gdb_index_contents_dwz (objfile, dwz);
3038
3039 if (dwz_index_content.empty ())
3040 return 0;
3041
3810f182 3042 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3043 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3044 &dwz_list, &dwz_list_elements,
3045 &dwz_types_ignore,
3046 &dwz_types_elements_ignore))
2ec9a5e0
TT
3047 {
3048 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3049 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3050 return 0;
3051 }
3052 }
3053
12359b5e
SM
3054 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3055 dwz_list, dwz_list_elements);
1fd400ff 3056
8b70b953
TT
3057 if (types_list_elements)
3058 {
8b70b953
TT
3059 /* We can only handle a single .debug_types when we have an
3060 index. */
fd5866f6 3061 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3062 return 0;
3063
fd5866f6 3064 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3065
12359b5e
SM
3066 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3067 types_list, types_list_elements);
8b70b953 3068 }
9291a0cd 3069
3063847f 3070 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3071
3063847f 3072 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3073 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3074 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3075 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3076
3077 return 1;
3078}
3079
dee91e82 3080/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3081
dee91e82
DE
3082static void
3083dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3084 const gdb_byte *info_ptr,
3e225074 3085 struct die_info *comp_unit_die)
9291a0cd 3086{
dee91e82 3087 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3088 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3089 struct dwarf2_per_objfile *dwarf2_per_objfile
3090 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3091 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3092 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3093 struct attribute *attr;
7b9f3c50
DE
3094 void **slot;
3095 struct quick_file_names *qfn;
9291a0cd 3096
0186c6a7
DE
3097 gdb_assert (! this_cu->is_debug_types);
3098
07261596
TT
3099 /* Our callers never want to match partial units -- instead they
3100 will match the enclosing full CU. */
3101 if (comp_unit_die->tag == DW_TAG_partial_unit)
3102 {
3103 this_cu->v.quick->no_file_data = 1;
3104 return;
3105 }
3106
0186c6a7 3107 lh_cu = this_cu;
7b9f3c50 3108 slot = NULL;
dee91e82 3109
fff8551c 3110 line_header_up lh;
9c541725 3111 sect_offset line_offset {};
fff8551c 3112
dee91e82 3113 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3114 if (attr != nullptr)
9291a0cd 3115 {
7b9f3c50
DE
3116 struct quick_file_names find_entry;
3117
9c541725 3118 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3119
3120 /* We may have already read in this line header (TU line header sharing).
3121 If we have we're done. */
094b34ac 3122 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3123 find_entry.hash.line_sect_off = line_offset;
5895093f 3124 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3125 &find_entry, INSERT);
3126 if (*slot != NULL)
3127 {
9a3c8263 3128 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3129 return;
7b9f3c50
DE
3130 }
3131
3019eac3 3132 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3133 }
3134 if (lh == NULL)
3135 {
094b34ac 3136 lh_cu->v.quick->no_file_data = 1;
dee91e82 3137 return;
9291a0cd
TT
3138 }
3139
8d749320 3140 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3141 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3142 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3143 gdb_assert (slot != NULL);
3144 *slot = qfn;
9291a0cd 3145
d721ba37 3146 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3147
aa391654
TT
3148 int offset = 0;
3149 if (strcmp (fnd.name, "<unknown>") != 0)
3150 ++offset;
3151
7ba99d21 3152 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3153 qfn->file_names =
aa391654
TT
3154 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3155 if (offset != 0)
3156 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3157 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3158 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3159 fnd.comp_dir).release ();
7b9f3c50 3160 qfn->real_names = NULL;
9291a0cd 3161
094b34ac 3162 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3163}
3164
3165/* A helper for the "quick" functions which attempts to read the line
3166 table for THIS_CU. */
3167
3168static struct quick_file_names *
e4a48d9d 3169dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3170{
0186c6a7
DE
3171 /* This should never be called for TUs. */
3172 gdb_assert (! this_cu->is_debug_types);
3173 /* Nor type unit groups. */
197400e8 3174 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3175
dee91e82
DE
3176 if (this_cu->v.quick->file_names != NULL)
3177 return this_cu->v.quick->file_names;
3178 /* If we know there is no line data, no point in looking again. */
3179 if (this_cu->v.quick->no_file_data)
3180 return NULL;
3181
c0ab21c2
TT
3182 cutu_reader reader (this_cu);
3183 if (!reader.dummy_p)
3e225074 3184 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3185
3186 if (this_cu->v.quick->no_file_data)
3187 return NULL;
3188 return this_cu->v.quick->file_names;
9291a0cd
TT
3189}
3190
3191/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3192 real path for a given file name from the line table. */
2fdf6df6 3193
9291a0cd 3194static const char *
7b9f3c50
DE
3195dw2_get_real_path (struct objfile *objfile,
3196 struct quick_file_names *qfn, int index)
9291a0cd 3197{
7b9f3c50
DE
3198 if (qfn->real_names == NULL)
3199 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3200 qfn->num_file_names, const char *);
9291a0cd 3201
7b9f3c50 3202 if (qfn->real_names[index] == NULL)
14278e1f 3203 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3204
7b9f3c50 3205 return qfn->real_names[index];
9291a0cd
TT
3206}
3207
3208static struct symtab *
3209dw2_find_last_source_symtab (struct objfile *objfile)
3210{
ed2dc618
SM
3211 struct dwarf2_per_objfile *dwarf2_per_objfile
3212 = get_dwarf2_per_objfile (objfile);
b76e467d 3213 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3214 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3215
43f3e411
DE
3216 if (cust == NULL)
3217 return NULL;
ed2dc618 3218
43f3e411 3219 return compunit_primary_filetab (cust);
9291a0cd
TT
3220}
3221
7b9f3c50
DE
3222/* Traversal function for dw2_forget_cached_source_info. */
3223
3224static int
3225dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3226{
7b9f3c50 3227 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3228
7b9f3c50 3229 if (file_data->real_names)
9291a0cd 3230 {
7b9f3c50 3231 int i;
9291a0cd 3232
7b9f3c50 3233 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3234 {
7b9f3c50
DE
3235 xfree ((void*) file_data->real_names[i]);
3236 file_data->real_names[i] = NULL;
9291a0cd
TT
3237 }
3238 }
7b9f3c50
DE
3239
3240 return 1;
3241}
3242
3243static void
3244dw2_forget_cached_source_info (struct objfile *objfile)
3245{
ed2dc618
SM
3246 struct dwarf2_per_objfile *dwarf2_per_objfile
3247 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3248
5895093f 3249 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3250 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3251}
3252
f8eba3c6
TT
3253/* Helper function for dw2_map_symtabs_matching_filename that expands
3254 the symtabs and calls the iterator. */
3255
3256static int
3257dw2_map_expand_apply (struct objfile *objfile,
3258 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3259 const char *name, const char *real_path,
14bc53a8 3260 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3261{
43f3e411 3262 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3263
3264 /* Don't visit already-expanded CUs. */
43f3e411 3265 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3266 return 0;
3267
3268 /* This may expand more than one symtab, and we want to iterate over
3269 all of them. */
58f0c718 3270 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3271
14bc53a8
PA
3272 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3273 last_made, callback);
f8eba3c6
TT
3274}
3275
3276/* Implementation of the map_symtabs_matching_filename method. */
3277
14bc53a8
PA
3278static bool
3279dw2_map_symtabs_matching_filename
3280 (struct objfile *objfile, const char *name, const char *real_path,
3281 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3282{
c011a4f4 3283 const char *name_basename = lbasename (name);
ed2dc618
SM
3284 struct dwarf2_per_objfile *dwarf2_per_objfile
3285 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3286
848e3e78
DE
3287 /* The rule is CUs specify all the files, including those used by
3288 any TU, so there's no need to scan TUs here. */
f4dc4d17 3289
b76e467d 3290 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3291 {
3d7bb9d9 3292 /* We only need to look at symtabs not already expanded. */
43f3e411 3293 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3294 continue;
3295
b76e467d 3296 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3297 if (file_data == NULL)
9291a0cd
TT
3298 continue;
3299
b76e467d 3300 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3301 {
7b9f3c50 3302 const char *this_name = file_data->file_names[j];
da235a7c 3303 const char *this_real_name;
9291a0cd 3304
af529f8f 3305 if (compare_filenames_for_search (this_name, name))
9291a0cd 3306 {
f5b95b50 3307 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3308 callback))
3309 return true;
288e77a7 3310 continue;
4aac40c8 3311 }
9291a0cd 3312
c011a4f4
DE
3313 /* Before we invoke realpath, which can get expensive when many
3314 files are involved, do a quick comparison of the basenames. */
3315 if (! basenames_may_differ
3316 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3317 continue;
3318
da235a7c
JK
3319 this_real_name = dw2_get_real_path (objfile, file_data, j);
3320 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3321 {
da235a7c 3322 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3323 callback))
3324 return true;
288e77a7 3325 continue;
da235a7c 3326 }
9291a0cd 3327
da235a7c
JK
3328 if (real_path != NULL)
3329 {
af529f8f
JK
3330 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3331 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3332 if (this_real_name != NULL
af529f8f 3333 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3334 {
f5b95b50 3335 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3336 callback))
3337 return true;
288e77a7 3338 continue;
9291a0cd
TT
3339 }
3340 }
3341 }
3342 }
3343
14bc53a8 3344 return false;
9291a0cd
TT
3345}
3346
da51c347
DE
3347/* Struct used to manage iterating over all CUs looking for a symbol. */
3348
3349struct dw2_symtab_iterator
9291a0cd 3350{
ed2dc618
SM
3351 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3352 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3353 /* If set, only look for symbols that match that block. Valid values are
3354 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3355 gdb::optional<block_enum> block_index;
da51c347
DE
3356 /* The kind of symbol we're looking for. */
3357 domain_enum domain;
3358 /* The list of CUs from the index entry of the symbol,
3359 or NULL if not found. */
3360 offset_type *vec;
3361 /* The next element in VEC to look at. */
3362 int next;
3363 /* The number of elements in VEC, or zero if there is no match. */
3364 int length;
8943b874
DE
3365 /* Have we seen a global version of the symbol?
3366 If so we can ignore all further global instances.
3367 This is to work around gold/15646, inefficient gold-generated
3368 indices. */
3369 int global_seen;
da51c347 3370};
9291a0cd 3371
2b79f376 3372/* Initialize the index symtab iterator ITER. */
2fdf6df6 3373
9291a0cd 3374static void
da51c347 3375dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3376 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3377 gdb::optional<block_enum> block_index,
da51c347
DE
3378 domain_enum domain,
3379 const char *name)
3380{
ed2dc618 3381 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3382 iter->block_index = block_index;
3383 iter->domain = domain;
3384 iter->next = 0;
8943b874 3385 iter->global_seen = 0;
da51c347 3386
3063847f 3387 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3388
3389 /* index is NULL if OBJF_READNOW. */
3390 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3391 iter->length = MAYBE_SWAP (*iter->vec);
3392 else
3393 {
3394 iter->vec = NULL;
3395 iter->length = 0;
3396 }
3397}
3398
3399/* Return the next matching CU or NULL if there are no more. */
3400
3401static struct dwarf2_per_cu_data *
3402dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3403{
ed2dc618
SM
3404 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3405
da51c347
DE
3406 for ( ; iter->next < iter->length; ++iter->next)
3407 {
3408 offset_type cu_index_and_attrs =
3409 MAYBE_SWAP (iter->vec[iter->next + 1]);
3410 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3411 gdb_index_symbol_kind symbol_kind =
3412 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3413 /* Only check the symbol attributes if they're present.
3414 Indices prior to version 7 don't record them,
3415 and indices >= 7 may elide them for certain symbols
3416 (gold does this). */
3417 int attrs_valid =
ed2dc618 3418 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3419 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3420
3190f0c6 3421 /* Don't crash on bad data. */
b76e467d 3422 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3423 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3424 {
b98664d3 3425 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3426 " [in module %s]"),
3427 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3428 continue;
3429 }
3430
ff4c9fec 3431 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3432
da51c347 3433 /* Skip if already read in. */
43f3e411 3434 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3435 continue;
3436
8943b874
DE
3437 /* Check static vs global. */
3438 if (attrs_valid)
3439 {
2b79f376
SM
3440 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3441
3442 if (iter->block_index.has_value ())
3443 {
3444 bool want_static = *iter->block_index == STATIC_BLOCK;
3445
3446 if (is_static != want_static)
3447 continue;
3448 }
3449
8943b874
DE
3450 /* Work around gold/15646. */
3451 if (!is_static && iter->global_seen)
3452 continue;
3453 if (!is_static)
3454 iter->global_seen = 1;
3455 }
da51c347
DE
3456
3457 /* Only check the symbol's kind if it has one. */
3458 if (attrs_valid)
3459 {
3460 switch (iter->domain)
3461 {
3462 case VAR_DOMAIN:
3463 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3464 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3465 /* Some types are also in VAR_DOMAIN. */
3466 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3467 continue;
3468 break;
3469 case STRUCT_DOMAIN:
3470 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3471 continue;
3472 break;
3473 case LABEL_DOMAIN:
3474 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3475 continue;
3476 break;
59c35742
AB
3477 case MODULE_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3479 continue;
3480 break;
da51c347
DE
3481 default:
3482 break;
3483 }
3484 }
3485
3486 ++iter->next;
3487 return per_cu;
3488 }
3489
3490 return NULL;
3491}
3492
43f3e411 3493static struct compunit_symtab *
c7f839cb 3494dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3495 const char *name, domain_enum domain)
9291a0cd 3496{
43f3e411 3497 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3498 struct dwarf2_per_objfile *dwarf2_per_objfile
3499 = get_dwarf2_per_objfile (objfile);
9291a0cd 3500
b5ec771e
PA
3501 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3502
ed2dc618
SM
3503 struct dw2_symtab_iterator iter;
3504 struct dwarf2_per_cu_data *per_cu;
da51c347 3505
2b79f376 3506 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3507
ed2dc618
SM
3508 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3509 {
3510 struct symbol *sym, *with_opaque = NULL;
58f0c718 3511 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3512 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3513 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3514
ed2dc618
SM
3515 sym = block_find_symbol (block, name, domain,
3516 block_find_non_opaque_type_preferred,
3517 &with_opaque);
b2e2f908 3518
ed2dc618
SM
3519 /* Some caution must be observed with overloaded functions
3520 and methods, since the index will not contain any overload
3521 information (but NAME might contain it). */
da51c347 3522
ed2dc618
SM
3523 if (sym != NULL
3524 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3525 return stab;
3526 if (with_opaque != NULL
3527 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3528 stab_best = stab;
da51c347 3529
ed2dc618 3530 /* Keep looking through other CUs. */
9291a0cd 3531 }
9291a0cd 3532
da51c347 3533 return stab_best;
9291a0cd
TT
3534}
3535
3536static void
3537dw2_print_stats (struct objfile *objfile)
3538{
ed2dc618
SM
3539 struct dwarf2_per_objfile *dwarf2_per_objfile
3540 = get_dwarf2_per_objfile (objfile);
b76e467d 3541 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3542 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3543 int count = 0;
9291a0cd 3544
ed2dc618 3545 for (int i = 0; i < total; ++i)
9291a0cd 3546 {
ff4c9fec 3547 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3548
43f3e411 3549 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3550 ++count;
3551 }
e4a48d9d 3552 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3553 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3554}
3555
779bd270
DE
3556/* This dumps minimal information about the index.
3557 It is called via "mt print objfiles".
3558 One use is to verify .gdb_index has been loaded by the
3559 gdb.dwarf2/gdb-index.exp testcase. */
3560
9291a0cd
TT
3561static void
3562dw2_dump (struct objfile *objfile)
3563{
ed2dc618
SM
3564 struct dwarf2_per_objfile *dwarf2_per_objfile
3565 = get_dwarf2_per_objfile (objfile);
3566
779bd270
DE
3567 gdb_assert (dwarf2_per_objfile->using_index);
3568 printf_filtered (".gdb_index:");
3569 if (dwarf2_per_objfile->index_table != NULL)
3570 {
3571 printf_filtered (" version %d\n",
3572 dwarf2_per_objfile->index_table->version);
3573 }
3574 else
3575 printf_filtered (" faked for \"readnow\"\n");
3576 printf_filtered ("\n");
9291a0cd
TT
3577}
3578
9291a0cd
TT
3579static void
3580dw2_expand_symtabs_for_function (struct objfile *objfile,
3581 const char *func_name)
3582{
ed2dc618
SM
3583 struct dwarf2_per_objfile *dwarf2_per_objfile
3584 = get_dwarf2_per_objfile (objfile);
da51c347 3585
ed2dc618
SM
3586 struct dw2_symtab_iterator iter;
3587 struct dwarf2_per_cu_data *per_cu;
da51c347 3588
2b79f376 3589 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3590
ed2dc618 3591 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3592 dw2_instantiate_symtab (per_cu, false);
da51c347 3593
9291a0cd
TT
3594}
3595
3596static void
3597dw2_expand_all_symtabs (struct objfile *objfile)
3598{
ed2dc618
SM
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
b76e467d 3601 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3602 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3603
ed2dc618 3604 for (int i = 0; i < total_units; ++i)
9291a0cd 3605 {
ff4c9fec 3606 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3607
58f0c718
TT
3608 /* We don't want to directly expand a partial CU, because if we
3609 read it with the wrong language, then assertion failures can
3610 be triggered later on. See PR symtab/23010. So, tell
3611 dw2_instantiate_symtab to skip partial CUs -- any important
3612 partial CU will be read via DW_TAG_imported_unit anyway. */
3613 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3614 }
3615}
3616
3617static void
652a8996
JK
3618dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3619 const char *fullname)
9291a0cd 3620{
ed2dc618
SM
3621 struct dwarf2_per_objfile *dwarf2_per_objfile
3622 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3623
3624 /* We don't need to consider type units here.
3625 This is only called for examining code, e.g. expand_line_sal.
3626 There can be an order of magnitude (or more) more type units
3627 than comp units, and we avoid them if we can. */
3628
b76e467d 3629 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3630 {
3d7bb9d9 3631 /* We only need to look at symtabs not already expanded. */
43f3e411 3632 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3633 continue;
3634
b76e467d 3635 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3636 if (file_data == NULL)
9291a0cd
TT
3637 continue;
3638
b76e467d 3639 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3640 {
652a8996
JK
3641 const char *this_fullname = file_data->file_names[j];
3642
3643 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3644 {
58f0c718 3645 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3646 break;
3647 }
3648 }
3649 }
3650}
3651
9291a0cd 3652static void
199b4314
TT
3653dw2_map_matching_symbols
3654 (struct objfile *objfile,
b054970d 3655 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3656 int global,
3657 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3658 symbol_compare_ftype *ordered_compare)
9291a0cd 3659{
1aa98955
TV
3660 /* Used for Ada. */
3661 struct dwarf2_per_objfile *dwarf2_per_objfile
3662 = get_dwarf2_per_objfile (objfile);
3663
3664 if (dwarf2_per_objfile->index_table != nullptr)
3665 {
3666 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3667 here though if the current language is Ada for a non-Ada objfile
3668 using GNU index. As Ada does not look for non-Ada symbols this
3669 function should just return. */
3670 return;
3671 }
3672
3673 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3674 inline psym_map_matching_symbols here, assuming all partial symtabs have
3675 been read in. */
3676 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3677
3678 for (compunit_symtab *cust : objfile->compunits ())
3679 {
3680 const struct block *block;
3681
3682 if (cust == NULL)
3683 continue;
3684 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3685 if (!iterate_over_symbols_terminated (block, name,
3686 domain, callback))
3687 return;
3688 }
9291a0cd
TT
3689}
3690
e1ef7d7a
PA
3691/* Starting from a search name, return the string that finds the upper
3692 bound of all strings that start with SEARCH_NAME in a sorted name
3693 list. Returns the empty string to indicate that the upper bound is
3694 the end of the list. */
3695
3696static std::string
3697make_sort_after_prefix_name (const char *search_name)
3698{
3699 /* When looking to complete "func", we find the upper bound of all
3700 symbols that start with "func" by looking for where we'd insert
3701 the closest string that would follow "func" in lexicographical
3702 order. Usually, that's "func"-with-last-character-incremented,
3703 i.e. "fund". Mind non-ASCII characters, though. Usually those
3704 will be UTF-8 multi-byte sequences, but we can't be certain.
3705 Especially mind the 0xff character, which is a valid character in
3706 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3707 rule out compilers allowing it in identifiers. Note that
3708 conveniently, strcmp/strcasecmp are specified to compare
3709 characters interpreted as unsigned char. So what we do is treat
3710 the whole string as a base 256 number composed of a sequence of
3711 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3712 to 0, and carries 1 to the following more-significant position.
3713 If the very first character in SEARCH_NAME ends up incremented
3714 and carries/overflows, then the upper bound is the end of the
3715 list. The string after the empty string is also the empty
3716 string.
3717
3718 Some examples of this operation:
3719
3720 SEARCH_NAME => "+1" RESULT
3721
3722 "abc" => "abd"
3723 "ab\xff" => "ac"
3724 "\xff" "a" "\xff" => "\xff" "b"
3725 "\xff" => ""
3726 "\xff\xff" => ""
3727 "" => ""
3728
3729 Then, with these symbols for example:
3730
3731 func
3732 func1
3733 fund
3734
3735 completing "func" looks for symbols between "func" and
3736 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3737 which finds "func" and "func1", but not "fund".
3738
3739 And with:
3740
3741 funcÿ (Latin1 'ÿ' [0xff])
3742 funcÿ1
3743 fund
3744
3745 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3746 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3747
3748 And with:
3749
3750 ÿÿ (Latin1 'ÿ' [0xff])
3751 ÿÿ1
3752
3753 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3754 the end of the list.
3755 */
3756 std::string after = search_name;
3757 while (!after.empty () && (unsigned char) after.back () == 0xff)
3758 after.pop_back ();
3759 if (!after.empty ())
3760 after.back () = (unsigned char) after.back () + 1;
3761 return after;
3762}
3763
5c58de74 3764/* See declaration. */
61d96d7e 3765
5c58de74
PA
3766std::pair<std::vector<name_component>::const_iterator,
3767 std::vector<name_component>::const_iterator>
44ed8f3e 3768mapped_index_base::find_name_components_bounds
3b00ef10 3769 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3770{
5c58de74
PA
3771 auto *name_cmp
3772 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3773
3b00ef10 3774 const char *lang_name
e0802d59 3775 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3776
3f563c84
PA
3777 /* Comparison function object for lower_bound that matches against a
3778 given symbol name. */
3779 auto lookup_compare_lower = [&] (const name_component &elem,
3780 const char *name)
3781 {
5c58de74 3782 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3783 const char *elem_name = elem_qualified + elem.name_offset;
3784 return name_cmp (elem_name, name) < 0;
3785 };
3786
3787 /* Comparison function object for upper_bound that matches against a
3788 given symbol name. */
3789 auto lookup_compare_upper = [&] (const char *name,
3790 const name_component &elem)
3791 {
5c58de74 3792 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3793 const char *elem_name = elem_qualified + elem.name_offset;
3794 return name_cmp (name, elem_name) < 0;
3795 };
3796
5c58de74
PA
3797 auto begin = this->name_components.begin ();
3798 auto end = this->name_components.end ();
3f563c84
PA
3799
3800 /* Find the lower bound. */
3801 auto lower = [&] ()
3802 {
3b00ef10 3803 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3804 return begin;
3805 else
3b00ef10 3806 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3807 } ();
3808
3809 /* Find the upper bound. */
3810 auto upper = [&] ()
3811 {
5c58de74 3812 if (lookup_name_without_params.completion_mode ())
3f563c84 3813 {
e1ef7d7a
PA
3814 /* In completion mode, we want UPPER to point past all
3815 symbols names that have the same prefix. I.e., with
3816 these symbols, and completing "func":
3817
3818 function << lower bound
3819 function1
3820 other_function << upper bound
3821
3822 We find the upper bound by looking for the insertion
3823 point of "func"-with-last-character-incremented,
3824 i.e. "fund". */
3b00ef10 3825 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3826 if (after.empty ())
3f563c84 3827 return end;
e6b2f5ef
PA
3828 return std::lower_bound (lower, end, after.c_str (),
3829 lookup_compare_lower);
3f563c84
PA
3830 }
3831 else
3b00ef10 3832 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3833 } ();
3834
5c58de74
PA
3835 return {lower, upper};
3836}
3837
3838/* See declaration. */
3839
3840void
44ed8f3e 3841mapped_index_base::build_name_components ()
5c58de74
PA
3842{
3843 if (!this->name_components.empty ())
3844 return;
3845
3846 this->name_components_casing = case_sensitivity;
3847 auto *name_cmp
3848 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3849
3850 /* The code below only knows how to break apart components of C++
3851 symbol names (and other languages that use '::' as
3b00ef10 3852 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3853 auto count = this->symbol_name_count ();
3854 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3855 {
44ed8f3e 3856 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3857 continue;
3858
3859 const char *name = this->symbol_name_at (idx);
3860
3861 /* Add each name component to the name component table. */
3862 unsigned int previous_len = 0;
3b00ef10
TT
3863
3864 if (strstr (name, "::") != nullptr)
3865 {
3866 for (unsigned int current_len = cp_find_first_component (name);
3867 name[current_len] != '\0';
3868 current_len += cp_find_first_component (name + current_len))
3869 {
3870 gdb_assert (name[current_len] == ':');
3871 this->name_components.push_back ({previous_len, idx});
3872 /* Skip the '::'. */
3873 current_len += 2;
3874 previous_len = current_len;
3875 }
3876 }
3877 else
5c58de74 3878 {
3b00ef10
TT
3879 /* Handle the Ada encoded (aka mangled) form here. */
3880 for (const char *iter = strstr (name, "__");
3881 iter != nullptr;
3882 iter = strstr (iter, "__"))
3883 {
3884 this->name_components.push_back ({previous_len, idx});
3885 iter += 2;
3886 previous_len = iter - name;
3887 }
5c58de74 3888 }
3b00ef10 3889
5c58de74
PA
3890 this->name_components.push_back ({previous_len, idx});
3891 }
3892
3893 /* Sort name_components elements by name. */
3894 auto name_comp_compare = [&] (const name_component &left,
3895 const name_component &right)
3896 {
3897 const char *left_qualified = this->symbol_name_at (left.idx);
3898 const char *right_qualified = this->symbol_name_at (right.idx);
3899
3900 const char *left_name = left_qualified + left.name_offset;
3901 const char *right_name = right_qualified + right.name_offset;
3902
3903 return name_cmp (left_name, right_name) < 0;
3904 };
3905
3906 std::sort (this->name_components.begin (),
3907 this->name_components.end (),
3908 name_comp_compare);
3909}
3910
3911/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3912 mapped_index_base instead of the containing objfile. This is split
3913 to a separate function in order to be able to unit test the
3914 name_components matching using a mock mapped_index_base. For each
5c58de74 3915 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3916 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3917
3918static void
3919dw2_expand_symtabs_matching_symbol
44ed8f3e 3920 (mapped_index_base &index,
5c58de74
PA
3921 const lookup_name_info &lookup_name_in,
3922 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3923 enum search_domain kind,
3b00ef10 3924 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3925{
3926 lookup_name_info lookup_name_without_params
3927 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3928
3929 /* Build the symbol name component sorted vector, if we haven't
3930 yet. */
3931 index.build_name_components ();
3932
3f563c84
PA
3933 /* The same symbol may appear more than once in the range though.
3934 E.g., if we're looking for symbols that complete "w", and we have
3935 a symbol named "w1::w2", we'll find the two name components for
3936 that same symbol in the range. To be sure we only call the
3937 callback once per symbol, we first collect the symbol name
3938 indexes that matched in a temporary vector and ignore
3939 duplicates. */
3940 std::vector<offset_type> matches;
3f563c84 3941
3b00ef10
TT
3942 struct name_and_matcher
3943 {
3944 symbol_name_matcher_ftype *matcher;
ecc6c606 3945 const char *name;
3b00ef10
TT
3946
3947 bool operator== (const name_and_matcher &other) const
3f563c84 3948 {
ecc6c606 3949 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3950 }
3951 };
3952
3953 /* A vector holding all the different symbol name matchers, for all
3954 languages. */
3955 std::vector<name_and_matcher> matchers;
3956
3957 for (int i = 0; i < nr_languages; i++)
3958 {
3959 enum language lang_e = (enum language) i;
3960
3961 const language_defn *lang = language_def (lang_e);
3962 symbol_name_matcher_ftype *name_matcher
3963 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3964
3b00ef10
TT
3965 name_and_matcher key {
3966 name_matcher,
3967 lookup_name_without_params.language_lookup_name (lang_e)
3968 };
3969
3970 /* Don't insert the same comparison routine more than once.
3971 Note that we do this linear walk. This is not a problem in
3972 practice because the number of supported languages is
3973 low. */
3974 if (std::find (matchers.begin (), matchers.end (), key)
3975 != matchers.end ())
9291a0cd 3976 continue;
3b00ef10
TT
3977 matchers.push_back (std::move (key));
3978
3979 auto bounds
3980 = index.find_name_components_bounds (lookup_name_without_params,
3981 lang_e);
3982
3983 /* Now for each symbol name in range, check to see if we have a name
3984 match, and if so, call the MATCH_CALLBACK callback. */
3985
3986 for (; bounds.first != bounds.second; ++bounds.first)
3987 {
3988 const char *qualified = index.symbol_name_at (bounds.first->idx);
3989
3990 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3991 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3992 continue;
9291a0cd 3993
3b00ef10
TT
3994 matches.push_back (bounds.first->idx);
3995 }
3f563c84
PA
3996 }
3997
3998 std::sort (matches.begin (), matches.end ());
3999
4000 /* Finally call the callback, once per match. */
4001 ULONGEST prev = -1;
4002 for (offset_type idx : matches)
4003 {
4004 if (prev != idx)
4005 {
3b00ef10
TT
4006 if (!match_callback (idx))
4007 break;
3f563c84
PA
4008 prev = idx;
4009 }
4010 }
4011
4012 /* Above we use a type wider than idx's for 'prev', since 0 and
4013 (offset_type)-1 are both possible values. */
4014 static_assert (sizeof (prev) > sizeof (offset_type), "");
4015}
4016
c62446b1
PA
4017#if GDB_SELF_TEST
4018
4019namespace selftests { namespace dw2_expand_symtabs_matching {
4020
a3c5fafd
PA
4021/* A mock .gdb_index/.debug_names-like name index table, enough to
4022 exercise dw2_expand_symtabs_matching_symbol, which works with the
4023 mapped_index_base interface. Builds an index from the symbol list
4024 passed as parameter to the constructor. */
4025class mock_mapped_index : public mapped_index_base
c62446b1
PA
4026{
4027public:
a3c5fafd
PA
4028 mock_mapped_index (gdb::array_view<const char *> symbols)
4029 : m_symbol_table (symbols)
c62446b1
PA
4030 {}
4031
a3c5fafd 4032 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4033
a3c5fafd 4034 /* Return the number of names in the symbol table. */
632e107b 4035 size_t symbol_name_count () const override
c62446b1 4036 {
a3c5fafd 4037 return m_symbol_table.size ();
c62446b1
PA
4038 }
4039
a3c5fafd 4040 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4041 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4042 {
4043 return m_symbol_table[idx];
4044 }
c62446b1 4045
a3c5fafd
PA
4046private:
4047 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4048};
4049
4050/* Convenience function that converts a NULL pointer to a "<null>"
4051 string, to pass to print routines. */
4052
4053static const char *
4054string_or_null (const char *str)
4055{
4056 return str != NULL ? str : "<null>";
4057}
4058
4059/* Check if a lookup_name_info built from
4060 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4061 index. EXPECTED_LIST is the list of expected matches, in expected
4062 matching order. If no match expected, then an empty list is
4063 specified. Returns true on success. On failure prints a warning
4064 indicating the file:line that failed, and returns false. */
4065
4066static bool
4067check_match (const char *file, int line,
4068 mock_mapped_index &mock_index,
4069 const char *name, symbol_name_match_type match_type,
4070 bool completion_mode,
4071 std::initializer_list<const char *> expected_list)
4072{
4073 lookup_name_info lookup_name (name, match_type, completion_mode);
4074
4075 bool matched = true;
4076
4077 auto mismatch = [&] (const char *expected_str,
4078 const char *got)
4079 {
4080 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4081 "expected=\"%s\", got=\"%s\"\n"),
4082 file, line,
4083 (match_type == symbol_name_match_type::FULL
4084 ? "FULL" : "WILD"),
4085 name, string_or_null (expected_str), string_or_null (got));
4086 matched = false;
4087 };
4088
4089 auto expected_it = expected_list.begin ();
4090 auto expected_end = expected_list.end ();
4091
a3c5fafd 4092 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4093 NULL, ALL_DOMAIN,
4094 [&] (offset_type idx)
4095 {
a3c5fafd 4096 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4097 const char *expected_str
4098 = expected_it == expected_end ? NULL : *expected_it++;
4099
4100 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4101 mismatch (expected_str, matched_name);
3b00ef10 4102 return true;
c62446b1
PA
4103 });
4104
4105 const char *expected_str
4106 = expected_it == expected_end ? NULL : *expected_it++;
4107 if (expected_str != NULL)
4108 mismatch (expected_str, NULL);
4109
4110 return matched;
4111}
4112
4113/* The symbols added to the mock mapped_index for testing (in
4114 canonical form). */
4115static const char *test_symbols[] = {
4116 "function",
4117 "std::bar",
4118 "std::zfunction",
4119 "std::zfunction2",
4120 "w1::w2",
4121 "ns::foo<char*>",
4122 "ns::foo<int>",
4123 "ns::foo<long>",
a20714ff
PA
4124 "ns2::tmpl<int>::foo2",
4125 "(anonymous namespace)::A::B::C",
c62446b1 4126
e1ef7d7a
PA
4127 /* These are used to check that the increment-last-char in the
4128 matching algorithm for completion doesn't match "t1_fund" when
4129 completing "t1_func". */
4130 "t1_func",
4131 "t1_func1",
4132 "t1_fund",
4133 "t1_fund1",
4134
4135 /* A UTF-8 name with multi-byte sequences to make sure that
4136 cp-name-parser understands this as a single identifier ("função"
4137 is "function" in PT). */
4138 u8"u8função",
4139
4140 /* \377 (0xff) is Latin1 'ÿ'. */
4141 "yfunc\377",
4142
4143 /* \377 (0xff) is Latin1 'ÿ'. */
4144 "\377",
4145 "\377\377123",
4146
c62446b1
PA
4147 /* A name with all sorts of complications. Starts with "z" to make
4148 it easier for the completion tests below. */
4149#define Z_SYM_NAME \
4150 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4151 "::tuple<(anonymous namespace)::ui*, " \
4152 "std::default_delete<(anonymous namespace)::ui>, void>"
4153
4154 Z_SYM_NAME
4155};
4156
a3c5fafd
PA
4157/* Returns true if the mapped_index_base::find_name_component_bounds
4158 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4159 in completion mode. */
5c58de74
PA
4160
4161static bool
a3c5fafd 4162check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4163 const char *search_name,
4164 gdb::array_view<const char *> expected_syms)
4165{
4166 lookup_name_info lookup_name (search_name,
4167 symbol_name_match_type::FULL, true);
4168
3b00ef10
TT
4169 auto bounds = index.find_name_components_bounds (lookup_name,
4170 language_cplus);
5c58de74
PA
4171
4172 size_t distance = std::distance (bounds.first, bounds.second);
4173 if (distance != expected_syms.size ())
4174 return false;
4175
4176 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4177 {
4178 auto nc_elem = bounds.first + exp_elem;
4179 const char *qualified = index.symbol_name_at (nc_elem->idx);
4180 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4181 return false;
4182 }
4183
4184 return true;
4185}
4186
4187/* Test the lower-level mapped_index::find_name_component_bounds
4188 method. */
4189
c62446b1 4190static void
5c58de74
PA
4191test_mapped_index_find_name_component_bounds ()
4192{
4193 mock_mapped_index mock_index (test_symbols);
4194
a3c5fafd 4195 mock_index.build_name_components ();
5c58de74
PA
4196
4197 /* Test the lower-level mapped_index::find_name_component_bounds
4198 method in completion mode. */
4199 {
4200 static const char *expected_syms[] = {
4201 "t1_func",
4202 "t1_func1",
5c58de74
PA
4203 };
4204
a3c5fafd 4205 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4206 "t1_func", expected_syms));
4207 }
4208
4209 /* Check that the increment-last-char in the name matching algorithm
4210 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4211 {
4212 static const char *expected_syms1[] = {
4213 "\377",
4214 "\377\377123",
4215 };
a3c5fafd 4216 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4217 "\377", expected_syms1));
4218
4219 static const char *expected_syms2[] = {
4220 "\377\377123",
4221 };
a3c5fafd 4222 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4223 "\377\377", expected_syms2));
4224 }
4225}
4226
4227/* Test dw2_expand_symtabs_matching_symbol. */
4228
4229static void
4230test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4231{
4232 mock_mapped_index mock_index (test_symbols);
4233
4234 /* We let all tests run until the end even if some fails, for debug
4235 convenience. */
4236 bool any_mismatch = false;
4237
4238 /* Create the expected symbols list (an initializer_list). Needed
4239 because lists have commas, and we need to pass them to CHECK,
4240 which is a macro. */
4241#define EXPECT(...) { __VA_ARGS__ }
4242
4243 /* Wrapper for check_match that passes down the current
4244 __FILE__/__LINE__. */
4245#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4246 any_mismatch |= !check_match (__FILE__, __LINE__, \
4247 mock_index, \
4248 NAME, MATCH_TYPE, COMPLETION_MODE, \
4249 EXPECTED_LIST)
4250
4251 /* Identity checks. */
4252 for (const char *sym : test_symbols)
4253 {
4254 /* Should be able to match all existing symbols. */
4255 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4256 EXPECT (sym));
4257
4258 /* Should be able to match all existing symbols with
4259 parameters. */
4260 std::string with_params = std::string (sym) + "(int)";
4261 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4262 EXPECT (sym));
4263
4264 /* Should be able to match all existing symbols with
4265 parameters and qualifiers. */
4266 with_params = std::string (sym) + " ( int ) const";
4267 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4268 EXPECT (sym));
4269
4270 /* This should really find sym, but cp-name-parser.y doesn't
4271 know about lvalue/rvalue qualifiers yet. */
4272 with_params = std::string (sym) + " ( int ) &&";
4273 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4274 {});
4275 }
4276
e1ef7d7a
PA
4277 /* Check that the name matching algorithm for completion doesn't get
4278 confused with Latin1 'ÿ' / 0xff. */
4279 {
4280 static const char str[] = "\377";
4281 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4282 EXPECT ("\377", "\377\377123"));
4283 }
4284
4285 /* Check that the increment-last-char in the matching algorithm for
4286 completion doesn't match "t1_fund" when completing "t1_func". */
4287 {
4288 static const char str[] = "t1_func";
4289 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4290 EXPECT ("t1_func", "t1_func1"));
4291 }
4292
c62446b1
PA
4293 /* Check that completion mode works at each prefix of the expected
4294 symbol name. */
4295 {
4296 static const char str[] = "function(int)";
4297 size_t len = strlen (str);
4298 std::string lookup;
4299
4300 for (size_t i = 1; i < len; i++)
4301 {
4302 lookup.assign (str, i);
4303 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4304 EXPECT ("function"));
4305 }
4306 }
4307
4308 /* While "w" is a prefix of both components, the match function
4309 should still only be called once. */
4310 {
4311 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4312 EXPECT ("w1::w2"));
a20714ff
PA
4313 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4314 EXPECT ("w1::w2"));
c62446b1
PA
4315 }
4316
4317 /* Same, with a "complicated" symbol. */
4318 {
4319 static const char str[] = Z_SYM_NAME;
4320 size_t len = strlen (str);
4321 std::string lookup;
4322
4323 for (size_t i = 1; i < len; i++)
4324 {
4325 lookup.assign (str, i);
4326 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4327 EXPECT (Z_SYM_NAME));
4328 }
4329 }
4330
4331 /* In FULL mode, an incomplete symbol doesn't match. */
4332 {
4333 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4334 {});
4335 }
4336
4337 /* A complete symbol with parameters matches any overload, since the
4338 index has no overload info. */
4339 {
4340 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4341 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4342 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4343 EXPECT ("std::zfunction", "std::zfunction2"));
4344 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4345 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4346 }
4347
4348 /* Check that whitespace is ignored appropriately. A symbol with a
4349 template argument list. */
4350 {
4351 static const char expected[] = "ns::foo<int>";
4352 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4353 EXPECT (expected));
a20714ff
PA
4354 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4355 EXPECT (expected));
c62446b1
PA
4356 }
4357
4358 /* Check that whitespace is ignored appropriately. A symbol with a
4359 template argument list that includes a pointer. */
4360 {
4361 static const char expected[] = "ns::foo<char*>";
4362 /* Try both completion and non-completion modes. */
4363 static const bool completion_mode[2] = {false, true};
4364 for (size_t i = 0; i < 2; i++)
4365 {
4366 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4367 completion_mode[i], EXPECT (expected));
a20714ff
PA
4368 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4369 completion_mode[i], EXPECT (expected));
c62446b1
PA
4370
4371 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4372 completion_mode[i], EXPECT (expected));
a20714ff
PA
4373 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4374 completion_mode[i], EXPECT (expected));
c62446b1
PA
4375 }
4376 }
4377
4378 {
4379 /* Check method qualifiers are ignored. */
4380 static const char expected[] = "ns::foo<char*>";
4381 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4382 symbol_name_match_type::FULL, true, EXPECT (expected));
4383 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4384 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4385 CHECK_MATCH ("foo < char * > ( int ) const",
4386 symbol_name_match_type::WILD, true, EXPECT (expected));
4387 CHECK_MATCH ("foo < char * > ( int ) &&",
4388 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4389 }
4390
4391 /* Test lookup names that don't match anything. */
4392 {
a20714ff
PA
4393 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4394 {});
4395
c62446b1
PA
4396 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4397 {});
4398 }
4399
a20714ff
PA
4400 /* Some wild matching tests, exercising "(anonymous namespace)",
4401 which should not be confused with a parameter list. */
4402 {
4403 static const char *syms[] = {
4404 "A::B::C",
4405 "B::C",
4406 "C",
4407 "A :: B :: C ( int )",
4408 "B :: C ( int )",
4409 "C ( int )",
4410 };
4411
4412 for (const char *s : syms)
4413 {
4414 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4415 EXPECT ("(anonymous namespace)::A::B::C"));
4416 }
4417 }
4418
4419 {
4420 static const char expected[] = "ns2::tmpl<int>::foo2";
4421 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4422 EXPECT (expected));
4423 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4424 EXPECT (expected));
4425 }
4426
c62446b1
PA
4427 SELF_CHECK (!any_mismatch);
4428
4429#undef EXPECT
4430#undef CHECK_MATCH
4431}
4432
5c58de74
PA
4433static void
4434run_test ()
4435{
4436 test_mapped_index_find_name_component_bounds ();
4437 test_dw2_expand_symtabs_matching_symbol ();
4438}
4439
c62446b1
PA
4440}} // namespace selftests::dw2_expand_symtabs_matching
4441
4442#endif /* GDB_SELF_TEST */
4443
4b514bc8
JK
4444/* If FILE_MATCHER is NULL or if PER_CU has
4445 dwarf2_per_cu_quick_data::MARK set (see
4446 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4447 EXPANSION_NOTIFY on it. */
4448
4449static void
4450dw2_expand_symtabs_matching_one
4451 (struct dwarf2_per_cu_data *per_cu,
4452 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4453 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4454{
4455 if (file_matcher == NULL || per_cu->v.quick->mark)
4456 {
4457 bool symtab_was_null
4458 = (per_cu->v.quick->compunit_symtab == NULL);
4459
58f0c718 4460 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4461
4462 if (expansion_notify != NULL
4463 && symtab_was_null
4464 && per_cu->v.quick->compunit_symtab != NULL)
4465 expansion_notify (per_cu->v.quick->compunit_symtab);
4466 }
4467}
4468
3f563c84
PA
4469/* Helper for dw2_expand_matching symtabs. Called on each symbol
4470 matched, to expand corresponding CUs that were marked. IDX is the
4471 index of the symbol name that matched. */
4472
4473static void
4474dw2_expand_marked_cus
ed2dc618 4475 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4476 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4477 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4478 search_domain kind)
4479{
3f563c84
PA
4480 offset_type *vec, vec_len, vec_idx;
4481 bool global_seen = false;
ed2dc618 4482 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4483
61920122 4484 vec = (offset_type *) (index.constant_pool
f00a2de2 4485 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4486 vec_len = MAYBE_SWAP (vec[0]);
4487 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4488 {
61920122
PA
4489 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4490 /* This value is only valid for index versions >= 7. */
4491 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4492 gdb_index_symbol_kind symbol_kind =
4493 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4494 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4495 /* Only check the symbol attributes if they're present.
4496 Indices prior to version 7 don't record them,
4497 and indices >= 7 may elide them for certain symbols
4498 (gold does this). */
4499 int attrs_valid =
4500 (index.version >= 7
4501 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4502
4503 /* Work around gold/15646. */
4504 if (attrs_valid)
9291a0cd 4505 {
61920122
PA
4506 if (!is_static && global_seen)
4507 continue;
4508 if (!is_static)
4509 global_seen = true;
4510 }
3190f0c6 4511
61920122
PA
4512 /* Only check the symbol's kind if it has one. */
4513 if (attrs_valid)
4514 {
4515 switch (kind)
8943b874 4516 {
61920122
PA
4517 case VARIABLES_DOMAIN:
4518 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4519 continue;
4520 break;
4521 case FUNCTIONS_DOMAIN:
4522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4523 continue;
61920122
PA
4524 break;
4525 case TYPES_DOMAIN:
4526 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4527 continue;
4528 break;
59c35742
AB
4529 case MODULES_DOMAIN:
4530 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4531 continue;
4532 break;
61920122
PA
4533 default:
4534 break;
8943b874 4535 }
61920122 4536 }
8943b874 4537
61920122 4538 /* Don't crash on bad data. */
b76e467d 4539 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4540 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4541 {
b98664d3 4542 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4543 " [in module %s]"),
4544 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4545 continue;
4546 }
4547
ff4c9fec 4548 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4549 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4550 expansion_notify);
61920122
PA
4551 }
4552}
4553
4b514bc8
JK
4554/* If FILE_MATCHER is non-NULL, set all the
4555 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4556 that match FILE_MATCHER. */
4557
61920122 4558static void
4b514bc8 4559dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4560 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4561 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4562{
4b514bc8 4563 if (file_matcher == NULL)
61920122
PA
4564 return;
4565
4b514bc8
JK
4566 objfile *const objfile = dwarf2_per_objfile->objfile;
4567
4568 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4569 htab_eq_pointer,
4570 NULL, xcalloc, xfree));
4571 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4572 htab_eq_pointer,
4573 NULL, xcalloc, xfree));
61920122 4574
4b514bc8
JK
4575 /* The rule is CUs specify all the files, including those used by
4576 any TU, so there's no need to scan TUs here. */
61920122 4577
b76e467d 4578 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4579 {
927aa2e7
JK
4580 QUIT;
4581
4582 per_cu->v.quick->mark = 0;
4583
4584 /* We only need to look at symtabs not already expanded. */
4585 if (per_cu->v.quick->compunit_symtab)
4586 continue;
4587
b76e467d 4588 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4589 if (file_data == NULL)
4590 continue;
4591
4592 if (htab_find (visited_not_found.get (), file_data) != NULL)
4593 continue;
4594 else if (htab_find (visited_found.get (), file_data) != NULL)
4595 {
4596 per_cu->v.quick->mark = 1;
4597 continue;
4598 }
4599
b76e467d 4600 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4601 {
4602 const char *this_real_name;
4603
4604 if (file_matcher (file_data->file_names[j], false))
4605 {
4606 per_cu->v.quick->mark = 1;
4607 break;
4608 }
4609
4610 /* Before we invoke realpath, which can get expensive when many
4611 files are involved, do a quick comparison of the basenames. */
4612 if (!basenames_may_differ
4613 && !file_matcher (lbasename (file_data->file_names[j]),
4614 true))
4615 continue;
4616
4617 this_real_name = dw2_get_real_path (objfile, file_data, j);
4618 if (file_matcher (this_real_name, false))
4619 {
4620 per_cu->v.quick->mark = 1;
4621 break;
4622 }
4623 }
4624
b76e467d
SM
4625 void **slot = htab_find_slot (per_cu->v.quick->mark
4626 ? visited_found.get ()
4627 : visited_not_found.get (),
4628 file_data, INSERT);
927aa2e7
JK
4629 *slot = file_data;
4630 }
4631}
4632
4633static void
4634dw2_expand_symtabs_matching
4635 (struct objfile *objfile,
4636 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4637 const lookup_name_info *lookup_name,
927aa2e7
JK
4638 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4639 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4640 enum search_domain kind)
4641{
ed2dc618
SM
4642 struct dwarf2_per_objfile *dwarf2_per_objfile
4643 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4644
4645 /* index_table is NULL if OBJF_READNOW. */
4646 if (!dwarf2_per_objfile->index_table)
4647 return;
4648
ed2dc618 4649 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4650
c1a66c06
TV
4651 if (symbol_matcher == NULL && lookup_name == NULL)
4652 {
4653 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4654 {
4655 QUIT;
4656
4657 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4658 expansion_notify);
4659 }
4660 return;
4661 }
4662
927aa2e7
JK
4663 mapped_index &index = *dwarf2_per_objfile->index_table;
4664
c1a66c06 4665 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4666 symbol_matcher,
4667 kind, [&] (offset_type idx)
4668 {
ed2dc618 4669 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4670 expansion_notify, kind);
3b00ef10 4671 return true;
927aa2e7
JK
4672 });
4673}
4674
4675/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4676 symtab. */
4677
4678static struct compunit_symtab *
4679recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4680 CORE_ADDR pc)
4681{
4682 int i;
4683
4684 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4685 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4686 return cust;
4687
4688 if (cust->includes == NULL)
4689 return NULL;
4690
4691 for (i = 0; cust->includes[i]; ++i)
4692 {
4693 struct compunit_symtab *s = cust->includes[i];
4694
4695 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4696 if (s != NULL)
4697 return s;
4698 }
4699
4700 return NULL;
4701}
4702
4703static struct compunit_symtab *
4704dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4705 struct bound_minimal_symbol msymbol,
4706 CORE_ADDR pc,
4707 struct obj_section *section,
4708 int warn_if_readin)
4709{
4710 struct dwarf2_per_cu_data *data;
4711 struct compunit_symtab *result;
4712
d320c2b5 4713 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4714 return NULL;
4715
b3b3bada 4716 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4717 data = (struct dwarf2_per_cu_data *) addrmap_find
4718 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4719 if (!data)
4720 return NULL;
4721
4722 if (warn_if_readin && data->v.quick->compunit_symtab)
4723 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4724 paddress (objfile->arch (), pc));
927aa2e7
JK
4725
4726 result
58f0c718
TT
4727 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4728 false),
927aa2e7
JK
4729 pc);
4730 gdb_assert (result != NULL);
4731 return result;
4732}
4733
4734static void
4735dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4736 void *data, int need_fullname)
4737{
ed2dc618
SM
4738 struct dwarf2_per_objfile *dwarf2_per_objfile
4739 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4740
4741 if (!dwarf2_per_objfile->filenames_cache)
4742 {
4743 dwarf2_per_objfile->filenames_cache.emplace ();
4744
4745 htab_up visited (htab_create_alloc (10,
4746 htab_hash_pointer, htab_eq_pointer,
4747 NULL, xcalloc, xfree));
4748
4749 /* The rule is CUs specify all the files, including those used
4750 by any TU, so there's no need to scan TUs here. We can
4751 ignore file names coming from already-expanded CUs. */
4752
b76e467d 4753 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4754 {
927aa2e7
JK
4755 if (per_cu->v.quick->compunit_symtab)
4756 {
4757 void **slot = htab_find_slot (visited.get (),
4758 per_cu->v.quick->file_names,
4759 INSERT);
4760
4761 *slot = per_cu->v.quick->file_names;
4762 }
4763 }
4764
b76e467d 4765 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4766 {
927aa2e7
JK
4767 /* We only need to look at symtabs not already expanded. */
4768 if (per_cu->v.quick->compunit_symtab)
4769 continue;
4770
b76e467d 4771 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4772 if (file_data == NULL)
4773 continue;
4774
b76e467d 4775 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4776 if (*slot)
4777 {
4778 /* Already visited. */
4779 continue;
4780 }
4781 *slot = file_data;
4782
4783 for (int j = 0; j < file_data->num_file_names; ++j)
4784 {
4785 const char *filename = file_data->file_names[j];
4786 dwarf2_per_objfile->filenames_cache->seen (filename);
4787 }
4788 }
4789 }
4790
4791 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4792 {
4793 gdb::unique_xmalloc_ptr<char> this_real_name;
4794
4795 if (need_fullname)
4796 this_real_name = gdb_realpath (filename);
4797 (*fun) (filename, this_real_name.get (), data);
4798 });
4799}
4800
4801static int
4802dw2_has_symbols (struct objfile *objfile)
4803{
4804 return 1;
4805}
4806
4807const struct quick_symbol_functions dwarf2_gdb_index_functions =
4808{
4809 dw2_has_symbols,
4810 dw2_find_last_source_symtab,
4811 dw2_forget_cached_source_info,
4812 dw2_map_symtabs_matching_filename,
4813 dw2_lookup_symbol,
d3214198 4814 NULL,
927aa2e7
JK
4815 dw2_print_stats,
4816 dw2_dump,
927aa2e7
JK
4817 dw2_expand_symtabs_for_function,
4818 dw2_expand_all_symtabs,
4819 dw2_expand_symtabs_with_fullname,
4820 dw2_map_matching_symbols,
4821 dw2_expand_symtabs_matching,
4822 dw2_find_pc_sect_compunit_symtab,
4823 NULL,
4824 dw2_map_symbol_filenames
4825};
4826
4827/* DWARF-5 debug_names reader. */
4828
4829/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4830static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4831
4832/* A helper function that reads the .debug_names section in SECTION
4833 and fills in MAP. FILENAME is the name of the file containing the
4834 section; it is used for error reporting.
4835
4836 Returns true if all went well, false otherwise. */
4837
4838static bool
4839read_debug_names_from_section (struct objfile *objfile,
4840 const char *filename,
4841 struct dwarf2_section_info *section,
4842 mapped_debug_names &map)
4843{
96b79293 4844 if (section->empty ())
927aa2e7
JK
4845 return false;
4846
4847 /* Older elfutils strip versions could keep the section in the main
4848 executable while splitting it for the separate debug info file. */
96b79293 4849 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4850 return false;
4851
96b79293 4852 section->read (objfile);
927aa2e7 4853
08feed99 4854 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4855
4856 const gdb_byte *addr = section->buffer;
4857
96b79293 4858 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4859
4860 unsigned int bytes_read;
4861 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4862 addr += bytes_read;
4863
4864 map.dwarf5_is_dwarf64 = bytes_read != 4;
4865 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4866 if (bytes_read + length != section->size)
4867 {
4868 /* There may be multiple per-CU indices. */
4869 warning (_("Section .debug_names in %s length %s does not match "
4870 "section length %s, ignoring .debug_names."),
4871 filename, plongest (bytes_read + length),
4872 pulongest (section->size));
4873 return false;
4874 }
4875
4876 /* The version number. */
4877 uint16_t version = read_2_bytes (abfd, addr);
4878 addr += 2;
4879 if (version != 5)
4880 {
4881 warning (_("Section .debug_names in %s has unsupported version %d, "
4882 "ignoring .debug_names."),
4883 filename, version);
4884 return false;
4885 }
4886
4887 /* Padding. */
4888 uint16_t padding = read_2_bytes (abfd, addr);
4889 addr += 2;
4890 if (padding != 0)
4891 {
4892 warning (_("Section .debug_names in %s has unsupported padding %d, "
4893 "ignoring .debug_names."),
4894 filename, padding);
4895 return false;
4896 }
4897
4898 /* comp_unit_count - The number of CUs in the CU list. */
4899 map.cu_count = read_4_bytes (abfd, addr);
4900 addr += 4;
4901
4902 /* local_type_unit_count - The number of TUs in the local TU
4903 list. */
4904 map.tu_count = read_4_bytes (abfd, addr);
4905 addr += 4;
4906
4907 /* foreign_type_unit_count - The number of TUs in the foreign TU
4908 list. */
4909 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4910 addr += 4;
4911 if (foreign_tu_count != 0)
4912 {
4913 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4914 "ignoring .debug_names."),
4915 filename, static_cast<unsigned long> (foreign_tu_count));
4916 return false;
4917 }
4918
4919 /* bucket_count - The number of hash buckets in the hash lookup
4920 table. */
4921 map.bucket_count = read_4_bytes (abfd, addr);
4922 addr += 4;
4923
4924 /* name_count - The number of unique names in the index. */
4925 map.name_count = read_4_bytes (abfd, addr);
4926 addr += 4;
4927
4928 /* abbrev_table_size - The size in bytes of the abbreviations
4929 table. */
4930 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4931 addr += 4;
4932
4933 /* augmentation_string_size - The size in bytes of the augmentation
4934 string. This value is rounded up to a multiple of 4. */
4935 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4936 addr += 4;
4937 map.augmentation_is_gdb = ((augmentation_string_size
4938 == sizeof (dwarf5_augmentation))
4939 && memcmp (addr, dwarf5_augmentation,
4940 sizeof (dwarf5_augmentation)) == 0);
4941 augmentation_string_size += (-augmentation_string_size) & 3;
4942 addr += augmentation_string_size;
4943
4944 /* List of CUs */
4945 map.cu_table_reordered = addr;
4946 addr += map.cu_count * map.offset_size;
4947
4948 /* List of Local TUs */
4949 map.tu_table_reordered = addr;
4950 addr += map.tu_count * map.offset_size;
4951
4952 /* Hash Lookup Table */
4953 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4954 addr += map.bucket_count * 4;
4955 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4956 addr += map.name_count * 4;
4957
4958 /* Name Table */
4959 map.name_table_string_offs_reordered = addr;
4960 addr += map.name_count * map.offset_size;
4961 map.name_table_entry_offs_reordered = addr;
4962 addr += map.name_count * map.offset_size;
4963
4964 const gdb_byte *abbrev_table_start = addr;
4965 for (;;)
4966 {
927aa2e7
JK
4967 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4968 addr += bytes_read;
4969 if (index_num == 0)
4970 break;
4971
4972 const auto insertpair
4973 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4974 if (!insertpair.second)
4975 {
4976 warning (_("Section .debug_names in %s has duplicate index %s, "
4977 "ignoring .debug_names."),
4978 filename, pulongest (index_num));
4979 return false;
4980 }
4981 mapped_debug_names::index_val &indexval = insertpair.first->second;
4982 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4983 addr += bytes_read;
4984
4985 for (;;)
4986 {
4987 mapped_debug_names::index_val::attr attr;
4988 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4989 addr += bytes_read;
4990 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4991 addr += bytes_read;
4992 if (attr.form == DW_FORM_implicit_const)
4993 {
4994 attr.implicit_const = read_signed_leb128 (abfd, addr,
4995 &bytes_read);
4996 addr += bytes_read;
4997 }
4998 if (attr.dw_idx == 0 && attr.form == 0)
4999 break;
5000 indexval.attr_vec.push_back (std::move (attr));
5001 }
5002 }
5003 if (addr != abbrev_table_start + abbrev_table_size)
5004 {
5005 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5006 "of size %s vs. written as %u, ignoring .debug_names."),
5007 filename, plongest (addr - abbrev_table_start),
5008 abbrev_table_size);
927aa2e7
JK
5009 return false;
5010 }
5011 map.entry_pool = addr;
5012
5013 return true;
5014}
5015
5016/* A helper for create_cus_from_debug_names that handles the MAP's CU
5017 list. */
5018
5019static void
ed2dc618 5020create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5021 const mapped_debug_names &map,
5022 dwarf2_section_info &section,
b76e467d 5023 bool is_dwz)
927aa2e7 5024{
3ee6bb11
TV
5025 if (!map.augmentation_is_gdb)
5026 {
5027 for (uint32_t i = 0; i < map.cu_count; ++i)
5028 {
5029 sect_offset sect_off
5030 = (sect_offset) (extract_unsigned_integer
5031 (map.cu_table_reordered + i * map.offset_size,
5032 map.offset_size,
5033 map.dwarf5_byte_order));
5034 /* We don't know the length of the CU, because the CU list in a
5035 .debug_names index can be incomplete, so we can't use the start of
5036 the next CU as end of this CU. We create the CUs here with length 0,
5037 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5038 dwarf2_per_cu_data *per_cu
5039 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5040 sect_off, 0);
5041 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5042 }
5043 }
5044
927aa2e7
JK
5045 sect_offset sect_off_prev;
5046 for (uint32_t i = 0; i <= map.cu_count; ++i)
5047 {
5048 sect_offset sect_off_next;
5049 if (i < map.cu_count)
5050 {
5051 sect_off_next
5052 = (sect_offset) (extract_unsigned_integer
5053 (map.cu_table_reordered + i * map.offset_size,
5054 map.offset_size,
5055 map.dwarf5_byte_order));
5056 }
5057 else
5058 sect_off_next = (sect_offset) section.size;
5059 if (i >= 1)
5060 {
5061 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5062 dwarf2_per_cu_data *per_cu
ed2dc618 5063 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5064 sect_off_prev, length);
b76e467d 5065 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5066 }
5067 sect_off_prev = sect_off_next;
5068 }
5069}
5070
5071/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5072 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5073
5074static void
ed2dc618 5075create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5076 const mapped_debug_names &map,
5077 const mapped_debug_names &dwz_map)
5078{
b76e467d
SM
5079 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5080 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5081
ed2dc618
SM
5082 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5083 dwarf2_per_objfile->info,
b76e467d 5084 false /* is_dwz */);
927aa2e7
JK
5085
5086 if (dwz_map.cu_count == 0)
5087 return;
5088
ed2dc618
SM
5089 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5090 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5091 true /* is_dwz */);
927aa2e7
JK
5092}
5093
5094/* Read .debug_names. If everything went ok, initialize the "quick"
5095 elements of all the CUs and return true. Otherwise, return false. */
5096
5097static bool
ed2dc618 5098dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5099{
22ca247e
TT
5100 std::unique_ptr<mapped_debug_names> map
5101 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5102 mapped_debug_names dwz_map (dwarf2_per_objfile);
5103 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5104
5105 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5106 &dwarf2_per_objfile->debug_names,
22ca247e 5107 *map))
927aa2e7
JK
5108 return false;
5109
5110 /* Don't use the index if it's empty. */
22ca247e 5111 if (map->name_count == 0)
927aa2e7
JK
5112 return false;
5113
5114 /* If there is a .dwz file, read it so we can get its CU list as
5115 well. */
ed2dc618 5116 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5117 if (dwz != NULL)
5118 {
5119 if (!read_debug_names_from_section (objfile,
00f93c44 5120 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5121 &dwz->debug_names, dwz_map))
5122 {
5123 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5124 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5125 return false;
5126 }
5127 }
5128
22ca247e 5129 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5130
22ca247e 5131 if (map->tu_count != 0)
927aa2e7
JK
5132 {
5133 /* We can only handle a single .debug_types when we have an
5134 index. */
fd5866f6 5135 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5136 return false;
5137
fd5866f6 5138 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5139
5140 create_signatured_type_table_from_debug_names
22ca247e 5141 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5142 }
5143
ed2dc618
SM
5144 create_addrmap_from_aranges (dwarf2_per_objfile,
5145 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5146
22ca247e 5147 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5148 dwarf2_per_objfile->using_index = 1;
5149 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5150 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5151
5152 return true;
5153}
5154
927aa2e7
JK
5155/* Type used to manage iterating over all CUs looking for a symbol for
5156 .debug_names. */
5157
5158class dw2_debug_names_iterator
5159{
5160public:
927aa2e7 5161 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5162 gdb::optional<block_enum> block_index,
5163 domain_enum domain,
927aa2e7 5164 const char *name)
2b79f376 5165 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5166 m_addr (find_vec_in_debug_names (map, name))
5167 {}
5168
5169 dw2_debug_names_iterator (const mapped_debug_names &map,
5170 search_domain search, uint32_t namei)
5171 : m_map (map),
5172 m_search (search),
5173 m_addr (find_vec_in_debug_names (map, namei))
5174 {}
5175
3b00ef10
TT
5176 dw2_debug_names_iterator (const mapped_debug_names &map,
5177 block_enum block_index, domain_enum domain,
5178 uint32_t namei)
5179 : m_map (map), m_block_index (block_index), m_domain (domain),
5180 m_addr (find_vec_in_debug_names (map, namei))
5181 {}
5182
927aa2e7
JK
5183 /* Return the next matching CU or NULL if there are no more. */
5184 dwarf2_per_cu_data *next ();
5185
5186private:
5187 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5188 const char *name);
5189 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5190 uint32_t namei);
5191
5192 /* The internalized form of .debug_names. */
5193 const mapped_debug_names &m_map;
5194
2b79f376
SM
5195 /* If set, only look for symbols that match that block. Valid values are
5196 GLOBAL_BLOCK and STATIC_BLOCK. */
5197 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5198
5199 /* The kind of symbol we're looking for. */
5200 const domain_enum m_domain = UNDEF_DOMAIN;
5201 const search_domain m_search = ALL_DOMAIN;
5202
5203 /* The list of CUs from the index entry of the symbol, or NULL if
5204 not found. */
5205 const gdb_byte *m_addr;
5206};
5207
5208const char *
5209mapped_debug_names::namei_to_name (uint32_t namei) const
5210{
5211 const ULONGEST namei_string_offs
5212 = extract_unsigned_integer ((name_table_string_offs_reordered
5213 + namei * offset_size),
5214 offset_size,
5215 dwarf5_byte_order);
4f44ae6c
TT
5216 return read_indirect_string_at_offset (dwarf2_per_objfile,
5217 namei_string_offs);
927aa2e7
JK
5218}
5219
5220/* Find a slot in .debug_names for the object named NAME. If NAME is
5221 found, return pointer to its pool data. If NAME cannot be found,
5222 return NULL. */
5223
5224const gdb_byte *
5225dw2_debug_names_iterator::find_vec_in_debug_names
5226 (const mapped_debug_names &map, const char *name)
5227{
5228 int (*cmp) (const char *, const char *);
5229
54ee4252 5230 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5231 if (current_language->la_language == language_cplus
5232 || current_language->la_language == language_fortran
5233 || current_language->la_language == language_d)
5234 {
5235 /* NAME is already canonical. Drop any qualifiers as
5236 .debug_names does not contain any. */
5237
5238 if (strchr (name, '(') != NULL)
5239 {
54ee4252 5240 without_params = cp_remove_params (name);
927aa2e7 5241 if (without_params != NULL)
54ee4252 5242 name = without_params.get ();
927aa2e7
JK
5243 }
5244 }
5245
5246 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5247
5248 const uint32_t full_hash = dwarf5_djb_hash (name);
5249 uint32_t namei
5250 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5251 (map.bucket_table_reordered
5252 + (full_hash % map.bucket_count)), 4,
5253 map.dwarf5_byte_order);
5254 if (namei == 0)
5255 return NULL;
5256 --namei;
5257 if (namei >= map.name_count)
5258 {
b98664d3 5259 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5260 "[in module %s]"),
5261 namei, map.name_count,
ed2dc618 5262 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5263 return NULL;
5264 }
5265
5266 for (;;)
5267 {
5268 const uint32_t namei_full_hash
5269 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5270 (map.hash_table_reordered + namei), 4,
5271 map.dwarf5_byte_order);
5272 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5273 return NULL;
5274
5275 if (full_hash == namei_full_hash)
5276 {
5277 const char *const namei_string = map.namei_to_name (namei);
5278
5279#if 0 /* An expensive sanity check. */
5280 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5281 {
b98664d3 5282 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5283 "[in module %s]"),
5284 namei, objfile_name (dwarf2_per_objfile->objfile));
5285 return NULL;
5286 }
5287#endif
5288
5289 if (cmp (namei_string, name) == 0)
5290 {
5291 const ULONGEST namei_entry_offs
5292 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5293 + namei * map.offset_size),
5294 map.offset_size, map.dwarf5_byte_order);
5295 return map.entry_pool + namei_entry_offs;
5296 }
5297 }
5298
5299 ++namei;
5300 if (namei >= map.name_count)
5301 return NULL;
5302 }
5303}
5304
5305const gdb_byte *
5306dw2_debug_names_iterator::find_vec_in_debug_names
5307 (const mapped_debug_names &map, uint32_t namei)
5308{
5309 if (namei >= map.name_count)
5310 {
b98664d3 5311 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5312 "[in module %s]"),
5313 namei, map.name_count,
ed2dc618 5314 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5315 return NULL;
5316 }
5317
5318 const ULONGEST namei_entry_offs
5319 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5320 + namei * map.offset_size),
5321 map.offset_size, map.dwarf5_byte_order);
5322 return map.entry_pool + namei_entry_offs;
5323}
5324
5325/* See dw2_debug_names_iterator. */
5326
5327dwarf2_per_cu_data *
5328dw2_debug_names_iterator::next ()
5329{
5330 if (m_addr == NULL)
5331 return NULL;
5332
ed2dc618
SM
5333 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5334 struct objfile *objfile = dwarf2_per_objfile->objfile;
5335 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5336
5337 again:
5338
5339 unsigned int bytes_read;
5340 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5341 m_addr += bytes_read;
5342 if (abbrev == 0)
5343 return NULL;
5344
5345 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5346 if (indexval_it == m_map.abbrev_map.cend ())
5347 {
b98664d3 5348 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5349 "[in module %s]"),
ed2dc618 5350 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5351 return NULL;
5352 }
5353 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5354 enum class symbol_linkage {
5355 unknown,
5356 static_,
5357 extern_,
23c13d42 5358 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5359 dwarf2_per_cu_data *per_cu = NULL;
5360 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5361 {
5362 ULONGEST ull;
5363 switch (attr.form)
5364 {
5365 case DW_FORM_implicit_const:
5366 ull = attr.implicit_const;
5367 break;
5368 case DW_FORM_flag_present:
5369 ull = 1;
5370 break;
5371 case DW_FORM_udata:
5372 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5373 m_addr += bytes_read;
5374 break;
6dc55ce9 5375 case DW_FORM_ref4:
5376 ull = read_4_bytes (abfd, m_addr);
5377 m_addr += 4;
5378 break;
5379 case DW_FORM_ref8:
5380 ull = read_8_bytes (abfd, m_addr);
5381 m_addr += 8;
5382 break;
5383 case DW_FORM_ref_sig8:
5384 ull = read_8_bytes (abfd, m_addr);
5385 m_addr += 8;
5386 break;
927aa2e7 5387 default:
b98664d3 5388 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5389 dwarf_form_name (attr.form),
ed2dc618 5390 objfile_name (objfile));
927aa2e7
JK
5391 return NULL;
5392 }
5393 switch (attr.dw_idx)
5394 {
5395 case DW_IDX_compile_unit:
5396 /* Don't crash on bad data. */
b76e467d 5397 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5398 {
b98664d3 5399 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5400 " [in module %s]"),
5401 pulongest (ull),
5402 objfile_name (dwarf2_per_objfile->objfile));
5403 continue;
5404 }
ff4c9fec 5405 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5406 break;
8af5c486
JK
5407 case DW_IDX_type_unit:
5408 /* Don't crash on bad data. */
b2bdb8cf 5409 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5410 {
b98664d3 5411 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5412 " [in module %s]"),
5413 pulongest (ull),
5414 objfile_name (dwarf2_per_objfile->objfile));
5415 continue;
5416 }
ff4c9fec 5417 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5418 break;
6dc55ce9 5419 case DW_IDX_die_offset:
5420 /* In a per-CU index (as opposed to a per-module index), index
5421 entries without CU attribute implicitly refer to the single CU. */
5422 if (per_cu == NULL)
5423 per_cu = dwarf2_per_objfile->get_cu (0);
5424 break;
927aa2e7
JK
5425 case DW_IDX_GNU_internal:
5426 if (!m_map.augmentation_is_gdb)
5427 break;
23c13d42 5428 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5429 break;
5430 case DW_IDX_GNU_external:
5431 if (!m_map.augmentation_is_gdb)
5432 break;
23c13d42 5433 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5434 break;
5435 }
5436 }
5437
5438 /* Skip if already read in. */
5439 if (per_cu->v.quick->compunit_symtab)
5440 goto again;
5441
5442 /* Check static vs global. */
23c13d42 5443 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5444 {
2b79f376 5445 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5446 const bool symbol_is_static =
5447 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5448 if (want_static != symbol_is_static)
2b79f376 5449 goto again;
927aa2e7
JK
5450 }
5451
5452 /* Match dw2_symtab_iter_next, symbol_kind
5453 and debug_names::psymbol_tag. */
5454 switch (m_domain)
5455 {
5456 case VAR_DOMAIN:
5457 switch (indexval.dwarf_tag)
5458 {
5459 case DW_TAG_variable:
5460 case DW_TAG_subprogram:
5461 /* Some types are also in VAR_DOMAIN. */
5462 case DW_TAG_typedef:
5463 case DW_TAG_structure_type:
5464 break;
5465 default:
5466 goto again;
5467 }
5468 break;
5469 case STRUCT_DOMAIN:
5470 switch (indexval.dwarf_tag)
5471 {
5472 case DW_TAG_typedef:
5473 case DW_TAG_structure_type:
5474 break;
5475 default:
5476 goto again;
5477 }
5478 break;
5479 case LABEL_DOMAIN:
5480 switch (indexval.dwarf_tag)
5481 {
5482 case 0:
5483 case DW_TAG_variable:
5484 break;
5485 default:
5486 goto again;
5487 }
5488 break;
59c35742
AB
5489 case MODULE_DOMAIN:
5490 switch (indexval.dwarf_tag)
5491 {
5492 case DW_TAG_module:
5493 break;
5494 default:
5495 goto again;
5496 }
5497 break;
927aa2e7
JK
5498 default:
5499 break;
5500 }
5501
5502 /* Match dw2_expand_symtabs_matching, symbol_kind and
5503 debug_names::psymbol_tag. */
5504 switch (m_search)
4b514bc8 5505 {
927aa2e7
JK
5506 case VARIABLES_DOMAIN:
5507 switch (indexval.dwarf_tag)
4b514bc8 5508 {
927aa2e7
JK
5509 case DW_TAG_variable:
5510 break;
5511 default:
5512 goto again;
4b514bc8 5513 }
927aa2e7
JK
5514 break;
5515 case FUNCTIONS_DOMAIN:
5516 switch (indexval.dwarf_tag)
4b514bc8 5517 {
927aa2e7
JK
5518 case DW_TAG_subprogram:
5519 break;
5520 default:
5521 goto again;
4b514bc8 5522 }
927aa2e7
JK
5523 break;
5524 case TYPES_DOMAIN:
5525 switch (indexval.dwarf_tag)
5526 {
5527 case DW_TAG_typedef:
5528 case DW_TAG_structure_type:
5529 break;
5530 default:
5531 goto again;
5532 }
5533 break;
59c35742
AB
5534 case MODULES_DOMAIN:
5535 switch (indexval.dwarf_tag)
5536 {
5537 case DW_TAG_module:
5538 break;
5539 default:
5540 goto again;
5541 }
927aa2e7
JK
5542 default:
5543 break;
4b514bc8 5544 }
927aa2e7
JK
5545
5546 return per_cu;
4b514bc8 5547}
61920122 5548
927aa2e7 5549static struct compunit_symtab *
c7f839cb 5550dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5551 const char *name, domain_enum domain)
4b514bc8 5552{
ed2dc618
SM
5553 struct dwarf2_per_objfile *dwarf2_per_objfile
5554 = get_dwarf2_per_objfile (objfile);
61920122 5555
927aa2e7
JK
5556 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5557 if (!mapp)
61920122 5558 {
927aa2e7
JK
5559 /* index is NULL if OBJF_READNOW. */
5560 return NULL;
5561 }
5562 const auto &map = *mapp;
9291a0cd 5563
2b79f376 5564 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5565
927aa2e7
JK
5566 struct compunit_symtab *stab_best = NULL;
5567 struct dwarf2_per_cu_data *per_cu;
5568 while ((per_cu = iter.next ()) != NULL)
5569 {
5570 struct symbol *sym, *with_opaque = NULL;
58f0c718 5571 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5572 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5573 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5574
927aa2e7
JK
5575 sym = block_find_symbol (block, name, domain,
5576 block_find_non_opaque_type_preferred,
5577 &with_opaque);
9703b513 5578
927aa2e7
JK
5579 /* Some caution must be observed with overloaded functions and
5580 methods, since the index will not contain any overload
5581 information (but NAME might contain it). */
a3ec0bb1 5582
927aa2e7 5583 if (sym != NULL
987012b8 5584 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5585 return stab;
5586 if (with_opaque != NULL
987012b8 5587 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5588 stab_best = stab;
9703b513 5589
927aa2e7 5590 /* Keep looking through other CUs. */
9703b513
TT
5591 }
5592
927aa2e7 5593 return stab_best;
9703b513
TT
5594}
5595
927aa2e7
JK
5596/* This dumps minimal information about .debug_names. It is called
5597 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5598 uses this to verify that .debug_names has been loaded. */
9291a0cd 5599
927aa2e7
JK
5600static void
5601dw2_debug_names_dump (struct objfile *objfile)
5602{
ed2dc618
SM
5603 struct dwarf2_per_objfile *dwarf2_per_objfile
5604 = get_dwarf2_per_objfile (objfile);
5605
927aa2e7
JK
5606 gdb_assert (dwarf2_per_objfile->using_index);
5607 printf_filtered (".debug_names:");
5608 if (dwarf2_per_objfile->debug_names_table)
5609 printf_filtered (" exists\n");
5610 else
5611 printf_filtered (" faked for \"readnow\"\n");
5612 printf_filtered ("\n");
9291a0cd
TT
5613}
5614
9291a0cd 5615static void
927aa2e7
JK
5616dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5617 const char *func_name)
9291a0cd 5618{
ed2dc618
SM
5619 struct dwarf2_per_objfile *dwarf2_per_objfile
5620 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5621
927aa2e7
JK
5622 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5623 if (dwarf2_per_objfile->debug_names_table)
24c79950 5624 {
927aa2e7 5625 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5626
2b79f376 5627 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5628
927aa2e7
JK
5629 struct dwarf2_per_cu_data *per_cu;
5630 while ((per_cu = iter.next ()) != NULL)
58f0c718 5631 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5632 }
5633}
24c79950 5634
3b00ef10
TT
5635static void
5636dw2_debug_names_map_matching_symbols
5637 (struct objfile *objfile,
5638 const lookup_name_info &name, domain_enum domain,
5639 int global,
5640 gdb::function_view<symbol_found_callback_ftype> callback,
5641 symbol_compare_ftype *ordered_compare)
5642{
5643 struct dwarf2_per_objfile *dwarf2_per_objfile
5644 = get_dwarf2_per_objfile (objfile);
5645
5646 /* debug_names_table is NULL if OBJF_READNOW. */
5647 if (!dwarf2_per_objfile->debug_names_table)
5648 return;
5649
5650 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5651 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5652
5653 const char *match_name = name.ada ().lookup_name ().c_str ();
5654 auto matcher = [&] (const char *symname)
5655 {
5656 if (ordered_compare == nullptr)
5657 return true;
5658 return ordered_compare (symname, match_name) == 0;
5659 };
5660
5661 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5662 [&] (offset_type namei)
5663 {
5664 /* The name was matched, now expand corresponding CUs that were
5665 marked. */
5666 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5667
5668 struct dwarf2_per_cu_data *per_cu;
5669 while ((per_cu = iter.next ()) != NULL)
5670 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5671 return true;
5672 });
5673
5674 /* It's a shame we couldn't do this inside the
5675 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5676 that have already been expanded. Instead, this loop matches what
5677 the psymtab code does. */
5678 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5679 {
5680 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5681 if (cust != nullptr)
5682 {
5683 const struct block *block
5684 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5685 if (!iterate_over_symbols_terminated (block, name,
5686 domain, callback))
5687 break;
5688 }
5689 }
5690}
5691
927aa2e7
JK
5692static void
5693dw2_debug_names_expand_symtabs_matching
5694 (struct objfile *objfile,
5695 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5696 const lookup_name_info *lookup_name,
927aa2e7
JK
5697 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5698 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5699 enum search_domain kind)
5700{
ed2dc618
SM
5701 struct dwarf2_per_objfile *dwarf2_per_objfile
5702 = get_dwarf2_per_objfile (objfile);
9291a0cd 5703
927aa2e7
JK
5704 /* debug_names_table is NULL if OBJF_READNOW. */
5705 if (!dwarf2_per_objfile->debug_names_table)
5706 return;
9291a0cd 5707
ed2dc618 5708 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5709
c1a66c06
TV
5710 if (symbol_matcher == NULL && lookup_name == NULL)
5711 {
5712 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5713 {
5714 QUIT;
5715
5716 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5717 expansion_notify);
5718 }
5719 return;
5720 }
5721
44ed8f3e 5722 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5723
c1a66c06 5724 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5725 symbol_matcher,
5726 kind, [&] (offset_type namei)
927aa2e7 5727 {
927aa2e7
JK
5728 /* The name was matched, now expand corresponding CUs that were
5729 marked. */
5730 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5731
927aa2e7
JK
5732 struct dwarf2_per_cu_data *per_cu;
5733 while ((per_cu = iter.next ()) != NULL)
5734 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5735 expansion_notify);
3b00ef10 5736 return true;
44ed8f3e 5737 });
9291a0cd
TT
5738}
5739
927aa2e7 5740const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5741{
5742 dw2_has_symbols,
5743 dw2_find_last_source_symtab,
5744 dw2_forget_cached_source_info,
f8eba3c6 5745 dw2_map_symtabs_matching_filename,
927aa2e7 5746 dw2_debug_names_lookup_symbol,
d3214198 5747 NULL,
9291a0cd 5748 dw2_print_stats,
927aa2e7 5749 dw2_debug_names_dump,
927aa2e7 5750 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5751 dw2_expand_all_symtabs,
652a8996 5752 dw2_expand_symtabs_with_fullname,
3b00ef10 5753 dw2_debug_names_map_matching_symbols,
927aa2e7 5754 dw2_debug_names_expand_symtabs_matching,
43f3e411 5755 dw2_find_pc_sect_compunit_symtab,
71a3c369 5756 NULL,
9291a0cd
TT
5757 dw2_map_symbol_filenames
5758};
5759
4485a1c1
SM
5760/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5761 to either a dwarf2_per_objfile or dwz_file object. */
5762
5763template <typename T>
5764static gdb::array_view<const gdb_byte>
5765get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5766{
5767 dwarf2_section_info *section = &section_owner->gdb_index;
5768
96b79293 5769 if (section->empty ())
4485a1c1
SM
5770 return {};
5771
5772 /* Older elfutils strip versions could keep the section in the main
5773 executable while splitting it for the separate debug info file. */
96b79293 5774 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5775 return {};
5776
96b79293 5777 section->read (obj);
4485a1c1 5778
8bebfcda
PA
5779 /* dwarf2_section_info::size is a bfd_size_type, while
5780 gdb::array_view works with size_t. On 32-bit hosts, with
5781 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5782 is 32-bit. So we need an explicit narrowing conversion here.
5783 This is fine, because it's impossible to allocate or mmap an
5784 array/buffer larger than what size_t can represent. */
5785 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5786}
5787
87d6a7aa
SM
5788/* Lookup the index cache for the contents of the index associated to
5789 DWARF2_OBJ. */
5790
5791static gdb::array_view<const gdb_byte>
5792get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5793{
5794 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5795 if (build_id == nullptr)
5796 return {};
5797
5798 return global_index_cache.lookup_gdb_index (build_id,
5799 &dwarf2_obj->index_cache_res);
5800}
5801
5802/* Same as the above, but for DWZ. */
5803
5804static gdb::array_view<const gdb_byte>
5805get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5806{
5807 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5808 if (build_id == nullptr)
5809 return {};
5810
5811 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5812}
5813
3c0aa29a 5814/* See symfile.h. */
9291a0cd 5815
3c0aa29a
PA
5816bool
5817dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5818{
ed2dc618
SM
5819 struct dwarf2_per_objfile *dwarf2_per_objfile
5820 = get_dwarf2_per_objfile (objfile);
5821
9291a0cd
TT
5822 /* If we're about to read full symbols, don't bother with the
5823 indices. In this case we also don't care if some other debug
5824 format is making psymtabs, because they are all about to be
5825 expanded anyway. */
5826 if ((objfile->flags & OBJF_READNOW))
5827 {
9291a0cd 5828 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5829 create_all_comp_units (dwarf2_per_objfile);
5830 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5831 dwarf2_per_objfile->quick_file_names_table
5832 = create_quick_file_names_table
5833 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5834
b76e467d 5835 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5836 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5837 {
ff4c9fec 5838 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5839
e254ef6a
DE
5840 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5841 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5842 }
5843
5844 /* Return 1 so that gdb sees the "quick" functions. However,
5845 these functions will be no-ops because we will have expanded
5846 all symtabs. */
3c0aa29a
PA
5847 *index_kind = dw_index_kind::GDB_INDEX;
5848 return true;
9291a0cd
TT
5849 }
5850
ed2dc618 5851 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5852 {
5853 *index_kind = dw_index_kind::DEBUG_NAMES;
5854 return true;
5855 }
927aa2e7 5856
4485a1c1
SM
5857 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5858 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5859 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5860 {
5861 *index_kind = dw_index_kind::GDB_INDEX;
5862 return true;
5863 }
9291a0cd 5864
87d6a7aa
SM
5865 /* ... otherwise, try to find the index in the index cache. */
5866 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5867 get_gdb_index_contents_from_cache,
5868 get_gdb_index_contents_from_cache_dwz))
5869 {
5870 global_index_cache.hit ();
5871 *index_kind = dw_index_kind::GDB_INDEX;
5872 return true;
5873 }
5874
5875 global_index_cache.miss ();
3c0aa29a 5876 return false;
9291a0cd
TT
5877}
5878
5879\f
5880
dce234bc
PP
5881/* Build a partial symbol table. */
5882
5883void
f29dff0a 5884dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5885{
ed2dc618
SM
5886 struct dwarf2_per_objfile *dwarf2_per_objfile
5887 = get_dwarf2_per_objfile (objfile);
c9bf0622 5888
6eee24ce 5889 init_psymbol_list (objfile, 1024);
c906108c 5890
a70b8144 5891 try
c9bf0622
TT
5892 {
5893 /* This isn't really ideal: all the data we allocate on the
5894 objfile's obstack is still uselessly kept around. However,
5895 freeing it seems unsafe. */
906768f9 5896 psymtab_discarder psymtabs (objfile);
ed2dc618 5897 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5898 psymtabs.keep ();
87d6a7aa
SM
5899
5900 /* (maybe) store an index in the cache. */
5901 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5902 }
230d2906 5903 catch (const gdb_exception_error &except)
492d29ea
PA
5904 {
5905 exception_print (gdb_stderr, except);
5906 }
c906108c 5907}
c906108c 5908
3b80fe9b
DE
5909/* Find the base address of the compilation unit for range lists and
5910 location lists. It will normally be specified by DW_AT_low_pc.
5911 In DWARF-3 draft 4, the base address could be overridden by
5912 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5913 compilation units with discontinuous ranges. */
5914
5915static void
5916dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5917{
5918 struct attribute *attr;
5919
2b24b6e4 5920 cu->base_address.reset ();
3b80fe9b
DE
5921
5922 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5923 if (attr != nullptr)
2b24b6e4 5924 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5925 else
5926 {
5927 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5928 if (attr != nullptr)
2b24b6e4 5929 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5930 }
5931}
5932
36586728
TT
5933/* Helper function that returns the proper abbrev section for
5934 THIS_CU. */
5935
5936static struct dwarf2_section_info *
5937get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5938{
5939 struct dwarf2_section_info *abbrev;
ed2dc618 5940 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5941
5942 if (this_cu->is_dwz)
ed2dc618 5943 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5944 else
5945 abbrev = &dwarf2_per_objfile->abbrev;
5946
5947 return abbrev;
5948}
5949
f4dc4d17
DE
5950/* Fetch the abbreviation table offset from a comp or type unit header. */
5951
5952static sect_offset
ed2dc618
SM
5953read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5954 struct dwarf2_section_info *section,
9c541725 5955 sect_offset sect_off)
f4dc4d17 5956{
96b79293 5957 bfd *abfd = section->get_bfd_owner ();
d521ce57 5958 const gdb_byte *info_ptr;
ac298888 5959 unsigned int initial_length_size, offset_size;
43988095 5960 uint16_t version;
f4dc4d17 5961
96b79293 5962 section->read (dwarf2_per_objfile->objfile);
9c541725 5963 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5964 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5965 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5966 info_ptr += initial_length_size;
5967
5968 version = read_2_bytes (abfd, info_ptr);
5969 info_ptr += 2;
5970 if (version >= 5)
5971 {
5972 /* Skip unit type and address size. */
5973 info_ptr += 2;
5974 }
5975
24aa364d 5976 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5977}
5978
b83470bf
TT
5979/* A partial symtab that is used only for include files. */
5980struct dwarf2_include_psymtab : public partial_symtab
5981{
5982 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5983 : partial_symtab (filename, objfile)
5984 {
5985 }
5986
5987 void read_symtab (struct objfile *objfile) override
5988 {
194d088f
TV
5989 /* It's an include file, no symbols to read for it.
5990 Everything is in the includer symtab. */
5991
5992 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5993 expansion of the includer psymtab. We use the dependencies[0] field to
5994 model the includer. But if we go the regular route of calling
5995 expand_psymtab here, and having expand_psymtab call expand_dependencies
5996 to expand the includer, we'll only use expand_psymtab on the includer
5997 (making it a non-toplevel psymtab), while if we expand the includer via
5998 another path, we'll use read_symtab (making it a toplevel psymtab).
5999 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6000 psymtab, and trigger read_symtab on the includer here directly. */
6001 includer ()->read_symtab (objfile);
b83470bf
TT
6002 }
6003
6004 void expand_psymtab (struct objfile *objfile) override
6005 {
194d088f
TV
6006 /* This is not called by read_symtab, and should not be called by any
6007 expand_dependencies. */
6008 gdb_assert (false);
b83470bf
TT
6009 }
6010
6011 bool readin_p () const override
6012 {
194d088f 6013 return includer ()->readin_p ();
b83470bf
TT
6014 }
6015
6016 struct compunit_symtab *get_compunit_symtab () const override
6017 {
6018 return nullptr;
6019 }
6020
6021private:
194d088f
TV
6022 partial_symtab *includer () const
6023 {
6024 /* An include psymtab has exactly one dependency: the psymtab that
6025 includes it. */
6026 gdb_assert (this->number_of_dependencies == 1);
6027 return this->dependencies[0];
6028 }
b83470bf
TT
6029};
6030
aaa75496
JB
6031/* Allocate a new partial symtab for file named NAME and mark this new
6032 partial symtab as being an include of PST. */
6033
6034static void
891813be 6035dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6036 struct objfile *objfile)
6037{
b83470bf 6038 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6039
fbd9ab74
JK
6040 if (!IS_ABSOLUTE_PATH (subpst->filename))
6041 {
6042 /* It shares objfile->objfile_obstack. */
6043 subpst->dirname = pst->dirname;
6044 }
6045
a9342b62 6046 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6047 subpst->dependencies[0] = pst;
6048 subpst->number_of_dependencies = 1;
aaa75496
JB
6049}
6050
6051/* Read the Line Number Program data and extract the list of files
6052 included by the source file represented by PST. Build an include
d85a05f0 6053 partial symtab for each of these included files. */
aaa75496
JB
6054
6055static void
6056dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6057 struct die_info *die,
891813be 6058 dwarf2_psymtab *pst)
aaa75496 6059{
fff8551c 6060 line_header_up lh;
d85a05f0 6061 struct attribute *attr;
aaa75496 6062
d85a05f0 6063 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6064 if (attr != nullptr)
9c541725 6065 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6066 if (lh == NULL)
6067 return; /* No linetable, so no includes. */
6068
79748972
TT
6069 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6070 that we pass in the raw text_low here; that is ok because we're
6071 only decoding the line table to make include partial symtabs, and
6072 so the addresses aren't really used. */
4ae976d1 6073 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6074 pst->raw_text_low (), 1);
aaa75496
JB
6075}
6076
348e048f 6077static hashval_t
52dc124a 6078hash_signatured_type (const void *item)
348e048f 6079{
9a3c8263
SM
6080 const struct signatured_type *sig_type
6081 = (const struct signatured_type *) item;
9a619af0 6082
348e048f 6083 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6084 return sig_type->signature;
348e048f
DE
6085}
6086
6087static int
52dc124a 6088eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6089{
9a3c8263
SM
6090 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6091 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6092
348e048f
DE
6093 return lhs->signature == rhs->signature;
6094}
6095
1fd400ff
TT
6096/* Allocate a hash table for signatured types. */
6097
b0b6a987 6098static htab_up
298e9637 6099allocate_signatured_type_table ()
1fd400ff 6100{
b0b6a987
TT
6101 return htab_up (htab_create_alloc (41,
6102 hash_signatured_type,
6103 eq_signatured_type,
6104 NULL, xcalloc, xfree));
1fd400ff
TT
6105}
6106
d467dd73 6107/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6108
6109static int
d467dd73 6110add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6111{
9a3c8263 6112 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6113 std::vector<signatured_type *> *all_type_units
6114 = (std::vector<signatured_type *> *) datum;
1fd400ff 6115
b2bdb8cf 6116 all_type_units->push_back (sigt);
1fd400ff
TT
6117
6118 return 1;
6119}
6120
78d4d2c5 6121/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6122 and fill them into TYPES_HTAB. It will process only type units,
6123 therefore DW_UT_type. */
c88ee1f0 6124
78d4d2c5 6125static void
ed2dc618
SM
6126create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6127 struct dwo_file *dwo_file,
b0b6a987 6128 dwarf2_section_info *section, htab_up &types_htab,
43988095 6129 rcuh_kind section_kind)
348e048f 6130{
3019eac3 6131 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6132 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6133 bfd *abfd;
6134 const gdb_byte *info_ptr, *end_ptr;
348e048f 6135
4bdcc0c1
DE
6136 abbrev_section = (dwo_file != NULL
6137 ? &dwo_file->sections.abbrev
6138 : &dwarf2_per_objfile->abbrev);
6139
b4f54984 6140 if (dwarf_read_debug)
43988095 6141 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6142 section->get_name (),
6143 abbrev_section->get_file_name ());
09406207 6144
96b79293 6145 section->read (objfile);
78d4d2c5 6146 info_ptr = section->buffer;
348e048f 6147
78d4d2c5
JK
6148 if (info_ptr == NULL)
6149 return;
348e048f 6150
78d4d2c5
JK
6151 /* We can't set abfd until now because the section may be empty or
6152 not present, in which case the bfd is unknown. */
96b79293 6153 abfd = section->get_bfd_owner ();
348e048f 6154
c0ab21c2
TT
6155 /* We don't use cutu_reader here because we don't need to read
6156 any dies: the signature is in the header. */
3019eac3 6157
78d4d2c5
JK
6158 end_ptr = info_ptr + section->size;
6159 while (info_ptr < end_ptr)
6160 {
78d4d2c5
JK
6161 struct signatured_type *sig_type;
6162 struct dwo_unit *dwo_tu;
6163 void **slot;
6164 const gdb_byte *ptr = info_ptr;
6165 struct comp_unit_head header;
6166 unsigned int length;
8b70b953 6167
9c541725 6168 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6169
a49dd8dd
JK
6170 /* Initialize it due to a false compiler warning. */
6171 header.signature = -1;
9c541725 6172 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6173
78d4d2c5
JK
6174 /* We need to read the type's signature in order to build the hash
6175 table, but we don't need anything else just yet. */
348e048f 6176
ed2dc618 6177 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6178 abbrev_section, ptr, section_kind);
348e048f 6179
4057dfde 6180 length = header.get_length ();
6caca83c 6181
78d4d2c5
JK
6182 /* Skip dummy type units. */
6183 if (ptr >= info_ptr + length
43988095
JK
6184 || peek_abbrev_code (abfd, ptr) == 0
6185 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6186 {
6187 info_ptr += length;
6188 continue;
6189 }
dee91e82 6190
78d4d2c5
JK
6191 if (types_htab == NULL)
6192 {
6193 if (dwo_file)
298e9637 6194 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6195 else
298e9637 6196 types_htab = allocate_signatured_type_table ();
78d4d2c5 6197 }
8b70b953 6198
78d4d2c5
JK
6199 if (dwo_file)
6200 {
6201 sig_type = NULL;
6202 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6203 struct dwo_unit);
6204 dwo_tu->dwo_file = dwo_file;
43988095 6205 dwo_tu->signature = header.signature;
9c541725 6206 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6207 dwo_tu->section = section;
9c541725 6208 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6209 dwo_tu->length = length;
6210 }
6211 else
6212 {
6213 /* N.B.: type_offset is not usable if this type uses a DWO file.
6214 The real type_offset is in the DWO file. */
6215 dwo_tu = NULL;
6216 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6217 struct signatured_type);
43988095 6218 sig_type->signature = header.signature;
9c541725 6219 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6220 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6221 sig_type->per_cu.is_debug_types = 1;
6222 sig_type->per_cu.section = section;
9c541725 6223 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6224 sig_type->per_cu.length = length;
6225 }
6226
b0b6a987 6227 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6228 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6229 INSERT);
6230 gdb_assert (slot != NULL);
6231 if (*slot != NULL)
6232 {
9c541725 6233 sect_offset dup_sect_off;
0349ea22 6234
3019eac3
DE
6235 if (dwo_file)
6236 {
78d4d2c5
JK
6237 const struct dwo_unit *dup_tu
6238 = (const struct dwo_unit *) *slot;
6239
9c541725 6240 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6241 }
6242 else
6243 {
78d4d2c5
JK
6244 const struct signatured_type *dup_tu
6245 = (const struct signatured_type *) *slot;
6246
9c541725 6247 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6248 }
8b70b953 6249
b98664d3 6250 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6251 " the entry at offset %s, signature %s"),
6252 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6253 hex_string (header.signature));
78d4d2c5
JK
6254 }
6255 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6256
78d4d2c5 6257 if (dwarf_read_debug > 1)
9d8780f0
SM
6258 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6259 sect_offset_str (sect_off),
43988095 6260 hex_string (header.signature));
3019eac3 6261
78d4d2c5
JK
6262 info_ptr += length;
6263 }
6264}
3019eac3 6265
78d4d2c5
JK
6266/* Create the hash table of all entries in the .debug_types
6267 (or .debug_types.dwo) section(s).
6268 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6269 otherwise it is NULL.
b3c8eb43 6270
78d4d2c5 6271 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6272
78d4d2c5 6273 Note: This function processes DWO files only, not DWP files. */
348e048f 6274
78d4d2c5 6275static void
ed2dc618
SM
6276create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6277 struct dwo_file *dwo_file,
fd5866f6 6278 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6279 htab_up &types_htab)
78d4d2c5 6280{
fd5866f6
SM
6281 for (dwarf2_section_info &section : type_sections)
6282 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6283 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6284}
6285
6286/* Create the hash table of all entries in the .debug_types section,
6287 and initialize all_type_units.
6288 The result is zero if there is an error (e.g. missing .debug_types section),
6289 otherwise non-zero. */
6290
6291static int
ed2dc618 6292create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6293{
b0b6a987 6294 htab_up types_htab;
3019eac3 6295
ed2dc618
SM
6296 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6297 &dwarf2_per_objfile->info, types_htab,
43988095 6298 rcuh_kind::COMPILE);
ed2dc618
SM
6299 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6300 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6301 if (types_htab == NULL)
6302 {
6303 dwarf2_per_objfile->signatured_types = NULL;
6304 return 0;
6305 }
6306
b0b6a987 6307 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6308
b2bdb8cf 6309 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6310 dwarf2_per_objfile->all_type_units.reserve
6311 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6312
b0b6a987
TT
6313 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6314 add_signatured_type_cu_to_table,
b2bdb8cf 6315 &dwarf2_per_objfile->all_type_units);
1fd400ff 6316
348e048f
DE
6317 return 1;
6318}
6319
6aa5f3a6
DE
6320/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6321 If SLOT is non-NULL, it is the entry to use in the hash table.
6322 Otherwise we find one. */
6323
6324static struct signatured_type *
ed2dc618
SM
6325add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6326 void **slot)
6aa5f3a6
DE
6327{
6328 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6329
b2bdb8cf
SM
6330 if (dwarf2_per_objfile->all_type_units.size ()
6331 == dwarf2_per_objfile->all_type_units.capacity ())
6332 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6333
b2bdb8cf
SM
6334 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6335 struct signatured_type);
6336
6337 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6338 sig_type->signature = sig;
6339 sig_type->per_cu.is_debug_types = 1;
6340 if (dwarf2_per_objfile->using_index)
6341 {
6342 sig_type->per_cu.v.quick =
6343 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6344 struct dwarf2_per_cu_quick_data);
6345 }
6346
6347 if (slot == NULL)
6348 {
b0b6a987 6349 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6350 sig_type, INSERT);
6351 }
6352 gdb_assert (*slot == NULL);
6353 *slot = sig_type;
6354 /* The rest of sig_type must be filled in by the caller. */
6355 return sig_type;
6356}
6357
a2ce51a0
DE
6358/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6359 Fill in SIG_ENTRY with DWO_ENTRY. */
6360
6361static void
ed2dc618 6362fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6363 struct signatured_type *sig_entry,
6364 struct dwo_unit *dwo_entry)
6365{
7ee85ab1 6366 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6367 gdb_assert (! sig_entry->per_cu.queued);
6368 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6369 if (dwarf2_per_objfile->using_index)
6370 {
6371 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6372 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6373 }
6374 else
6375 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6376 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6377 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6378 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6379 gdb_assert (sig_entry->dwo_unit == NULL);
6380
6381 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6382 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6383 sig_entry->per_cu.length = dwo_entry->length;
6384 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6385 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6386 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6387 sig_entry->dwo_unit = dwo_entry;
6388}
6389
6390/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6391 If we haven't read the TU yet, create the signatured_type data structure
6392 for a TU to be read in directly from a DWO file, bypassing the stub.
6393 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6394 using .gdb_index, then when reading a CU we want to stay in the DWO file
6395 containing that CU. Otherwise we could end up reading several other DWO
6396 files (due to comdat folding) to process the transitive closure of all the
6397 mentioned TUs, and that can be slow. The current DWO file will have every
6398 type signature that it needs.
a2ce51a0
DE
6399 We only do this for .gdb_index because in the psymtab case we already have
6400 to read all the DWOs to build the type unit groups. */
6401
6402static struct signatured_type *
6403lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6404{
518817b3
SM
6405 struct dwarf2_per_objfile *dwarf2_per_objfile
6406 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6407 struct dwo_file *dwo_file;
6408 struct dwo_unit find_dwo_entry, *dwo_entry;
6409 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6410 void **slot;
a2ce51a0
DE
6411
6412 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6413
6aa5f3a6
DE
6414 /* If TU skeletons have been removed then we may not have read in any
6415 TUs yet. */
6416 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6417 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6418
6419 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6420 Use the global signatured_types array to do our own comdat-folding
6421 of types. If this is the first time we're reading this TU, and
6422 the TU has an entry in .gdb_index, replace the recorded data from
6423 .gdb_index with this TU. */
a2ce51a0 6424
a2ce51a0 6425 find_sig_entry.signature = sig;
b0b6a987 6426 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6427 &find_sig_entry, INSERT);
9a3c8263 6428 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6429
6430 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6431 read. Don't reassign the global entry to point to this DWO if that's
6432 the case. Also note that if the TU is already being read, it may not
6433 have come from a DWO, the program may be a mix of Fission-compiled
6434 code and non-Fission-compiled code. */
6435
6436 /* Have we already tried to read this TU?
6437 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6438 needn't exist in the global table yet). */
6439 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6440 return sig_entry;
6441
6aa5f3a6
DE
6442 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6443 dwo_unit of the TU itself. */
6444 dwo_file = cu->dwo_unit->dwo_file;
6445
a2ce51a0
DE
6446 /* Ok, this is the first time we're reading this TU. */
6447 if (dwo_file->tus == NULL)
6448 return NULL;
6449 find_dwo_entry.signature = sig;
b0b6a987
TT
6450 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6451 &find_dwo_entry);
a2ce51a0
DE
6452 if (dwo_entry == NULL)
6453 return NULL;
6454
6aa5f3a6
DE
6455 /* If the global table doesn't have an entry for this TU, add one. */
6456 if (sig_entry == NULL)
ed2dc618 6457 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6458
ed2dc618 6459 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6460 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6461 return sig_entry;
6462}
6463
a2ce51a0
DE
6464/* Subroutine of lookup_signatured_type.
6465 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6466 then try the DWP file. If the TU stub (skeleton) has been removed then
6467 it won't be in .gdb_index. */
a2ce51a0
DE
6468
6469static struct signatured_type *
6470lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6471{
518817b3
SM
6472 struct dwarf2_per_objfile *dwarf2_per_objfile
6473 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6474 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6475 struct dwo_unit *dwo_entry;
6476 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6477 void **slot;
a2ce51a0
DE
6478
6479 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6480 gdb_assert (dwp_file != NULL);
6481
6aa5f3a6
DE
6482 /* If TU skeletons have been removed then we may not have read in any
6483 TUs yet. */
6484 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6485 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6486
6aa5f3a6 6487 find_sig_entry.signature = sig;
b0b6a987 6488 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6489 &find_sig_entry, INSERT);
9a3c8263 6490 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6491
6492 /* Have we already tried to read this TU?
6493 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6494 needn't exist in the global table yet). */
6495 if (sig_entry != NULL)
6496 return sig_entry;
6497
a2ce51a0
DE
6498 if (dwp_file->tus == NULL)
6499 return NULL;
ed2dc618 6500 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6501 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6502 if (dwo_entry == NULL)
6503 return NULL;
6504
ed2dc618
SM
6505 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6506 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6507
a2ce51a0
DE
6508 return sig_entry;
6509}
6510
380bca97 6511/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6512 Returns NULL if signature SIG is not present in the table.
6513 It is up to the caller to complain about this. */
348e048f
DE
6514
6515static struct signatured_type *
a2ce51a0 6516lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6517{
518817b3
SM
6518 struct dwarf2_per_objfile *dwarf2_per_objfile
6519 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6520
a2ce51a0
DE
6521 if (cu->dwo_unit
6522 && dwarf2_per_objfile->using_index)
6523 {
6524 /* We're in a DWO/DWP file, and we're using .gdb_index.
6525 These cases require special processing. */
ed2dc618 6526 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6527 return lookup_dwo_signatured_type (cu, sig);
6528 else
6529 return lookup_dwp_signatured_type (cu, sig);
6530 }
6531 else
6532 {
6533 struct signatured_type find_entry, *entry;
348e048f 6534
a2ce51a0
DE
6535 if (dwarf2_per_objfile->signatured_types == NULL)
6536 return NULL;
6537 find_entry.signature = sig;
9a3c8263 6538 entry = ((struct signatured_type *)
b0b6a987
TT
6539 htab_find (dwarf2_per_objfile->signatured_types.get (),
6540 &find_entry));
a2ce51a0
DE
6541 return entry;
6542 }
348e048f 6543}
18a8505e 6544
42e7ad6c 6545/* Low level DIE reading support. */
348e048f 6546
d85a05f0
DJ
6547/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6548
6549static void
6550init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6551 struct dwarf2_cu *cu,
3019eac3 6552 struct dwarf2_section_info *section,
685af9cd
TT
6553 struct dwo_file *dwo_file,
6554 struct abbrev_table *abbrev_table)
d85a05f0 6555{
fceca515 6556 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6557 reader->abfd = section->get_bfd_owner ();
d85a05f0 6558 reader->cu = cu;
3019eac3 6559 reader->dwo_file = dwo_file;
dee91e82
DE
6560 reader->die_section = section;
6561 reader->buffer = section->buffer;
f664829e 6562 reader->buffer_end = section->buffer + section->size;
685af9cd 6563 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6564}
6565
c0ab21c2 6566/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6567 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6568 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6569 already.
6570
6571 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6572 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6573 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6574 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6575 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6576 STUB_COMP_DIR may be non-NULL.
3e225074 6577 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6578 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6579 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6580 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6581 kept around for at least as long as *RESULT_READER.
6582
b0c7bfa9
DE
6583 The result is non-zero if a valid (non-dummy) DIE was found. */
6584
6585static int
6586read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6587 struct dwo_unit *dwo_unit,
b0c7bfa9 6588 struct die_info *stub_comp_unit_die,
a2ce51a0 6589 const char *stub_comp_dir,
b0c7bfa9 6590 struct die_reader_specs *result_reader,
d521ce57 6591 const gdb_byte **result_info_ptr,
b0c7bfa9 6592 struct die_info **result_comp_unit_die,
685af9cd 6593 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6594{
ed2dc618 6595 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6596 struct objfile *objfile = dwarf2_per_objfile->objfile;
6597 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6598 bfd *abfd;
d521ce57 6599 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6600 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6601 int i,num_extra_attrs;
6602 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6603 struct die_info *comp_unit_die;
6604
b0aeadb3
DE
6605 /* At most one of these may be provided. */
6606 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6607
b0c7bfa9
DE
6608 /* These attributes aren't processed until later:
6609 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6610 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6611 referenced later. However, these attributes are found in the stub
6612 which we won't have later. In order to not impose this complication
6613 on the rest of the code, we read them here and copy them to the
6614 DWO CU/TU die. */
b0c7bfa9
DE
6615
6616 stmt_list = NULL;
6617 low_pc = NULL;
6618 high_pc = NULL;
6619 ranges = NULL;
6620 comp_dir = NULL;
6621
6622 if (stub_comp_unit_die != NULL)
6623 {
6624 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6625 DWO file. */
6626 if (! this_cu->is_debug_types)
6627 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6628 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6629 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6630 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6631 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6632
a39fdb41 6633 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6634
18a8505e
AT
6635 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6636 here (if needed). We need the value before we can process
6637 DW_AT_ranges. */
a39fdb41 6638 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6639 }
a2ce51a0
DE
6640 else if (stub_comp_dir != NULL)
6641 {
6642 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6643 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6644 comp_dir->name = DW_AT_comp_dir;
6645 comp_dir->form = DW_FORM_string;
6646 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6647 DW_STRING (comp_dir) = stub_comp_dir;
6648 }
b0c7bfa9
DE
6649
6650 /* Set up for reading the DWO CU/TU. */
6651 cu->dwo_unit = dwo_unit;
685af9cd 6652 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6653 section->read (objfile);
6654 abfd = section->get_bfd_owner ();
9c541725
PA
6655 begin_info_ptr = info_ptr = (section->buffer
6656 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6657 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6658
6659 if (this_cu->is_debug_types)
6660 {
b0c7bfa9
DE
6661 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6662
ed2dc618
SM
6663 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6664 &cu->header, section,
b0c7bfa9 6665 dwo_abbrev_section,
43988095 6666 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6667 /* This is not an assert because it can be caused by bad debug info. */
43988095 6668 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6669 {
6670 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6671 " TU at offset %s [in module %s]"),
a2ce51a0 6672 hex_string (sig_type->signature),
43988095 6673 hex_string (cu->header.signature),
9d8780f0 6674 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6675 bfd_get_filename (abfd));
6676 }
9c541725 6677 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6678 /* For DWOs coming from DWP files, we don't know the CU length
6679 nor the type's offset in the TU until now. */
4057dfde 6680 dwo_unit->length = cu->header.get_length ();
9c541725 6681 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6682
6683 /* Establish the type offset that can be used to lookup the type.
6684 For DWO files, we don't know it until now. */
9c541725
PA
6685 sig_type->type_offset_in_section
6686 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6687 }
6688 else
6689 {
ed2dc618
SM
6690 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6691 &cu->header, section,
b0c7bfa9 6692 dwo_abbrev_section,
43988095 6693 info_ptr, rcuh_kind::COMPILE);
9c541725 6694 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6695 /* For DWOs coming from DWP files, we don't know the CU length
6696 until now. */
4057dfde 6697 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6698 }
6699
685af9cd 6700 *result_dwo_abbrev_table
86de1d91
TT
6701 = abbrev_table::read (objfile, dwo_abbrev_section,
6702 cu->header.abbrev_sect_off);
685af9cd
TT
6703 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6704 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6705
6706 /* Read in the die, but leave space to copy over the attributes
6707 from the stub. This has the benefit of simplifying the rest of
6708 the code - all the work to maintain the illusion of a single
6709 DW_TAG_{compile,type}_unit DIE is done here. */
6710 num_extra_attrs = ((stmt_list != NULL)
6711 + (low_pc != NULL)
6712 + (high_pc != NULL)
6713 + (ranges != NULL)
6714 + (comp_dir != NULL));
6715 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6716 num_extra_attrs);
b0c7bfa9
DE
6717
6718 /* Copy over the attributes from the stub to the DIE we just read in. */
6719 comp_unit_die = *result_comp_unit_die;
6720 i = comp_unit_die->num_attrs;
6721 if (stmt_list != NULL)
6722 comp_unit_die->attrs[i++] = *stmt_list;
6723 if (low_pc != NULL)
6724 comp_unit_die->attrs[i++] = *low_pc;
6725 if (high_pc != NULL)
6726 comp_unit_die->attrs[i++] = *high_pc;
6727 if (ranges != NULL)
6728 comp_unit_die->attrs[i++] = *ranges;
6729 if (comp_dir != NULL)
6730 comp_unit_die->attrs[i++] = *comp_dir;
6731 comp_unit_die->num_attrs += num_extra_attrs;
6732
b4f54984 6733 if (dwarf_die_debug)
bf6af496
DE
6734 {
6735 fprintf_unfiltered (gdb_stdlog,
6736 "Read die from %s@0x%x of %s:\n",
96b79293 6737 section->get_name (),
bf6af496
DE
6738 (unsigned) (begin_info_ptr - section->buffer),
6739 bfd_get_filename (abfd));
b4f54984 6740 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6741 }
6742
b0c7bfa9
DE
6743 /* Skip dummy compilation units. */
6744 if (info_ptr >= begin_info_ptr + dwo_unit->length
6745 || peek_abbrev_code (abfd, info_ptr) == 0)
6746 return 0;
6747
6748 *result_info_ptr = info_ptr;
6749 return 1;
6750}
6751
a084a2a6
AT
6752/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6753 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6754 signature is part of the header. */
6755static gdb::optional<ULONGEST>
6756lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6757{
6758 if (cu->header.version >= 5)
6759 return cu->header.signature;
6760 struct attribute *attr;
6761 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6762 if (attr == nullptr)
6763 return gdb::optional<ULONGEST> ();
6764 return DW_UNSND (attr);
6765}
6766
c0ab21c2 6767/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6768 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6769 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6770
6771static struct dwo_unit *
6772lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6773 struct die_info *comp_unit_die,
6774 const char *dwo_name)
b0c7bfa9
DE
6775{
6776 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6777 struct dwo_unit *dwo_unit;
c0ab21c2 6778 const char *comp_dir;
b0c7bfa9 6779
a2ce51a0
DE
6780 gdb_assert (cu != NULL);
6781
b0c7bfa9 6782 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6783 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6784 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6785
6786 if (this_cu->is_debug_types)
6787 {
6788 struct signatured_type *sig_type;
6789
6790 /* Since this_cu is the first member of struct signatured_type,
6791 we can go from a pointer to one to a pointer to the other. */
6792 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6793 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6794 }
6795 else
6796 {
a084a2a6
AT
6797 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6798 if (!signature.has_value ())
b0c7bfa9
DE
6799 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6800 " [in module %s]"),
e3b94546 6801 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6802 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6803 *signature);
b0c7bfa9
DE
6804 }
6805
b0c7bfa9
DE
6806 return dwo_unit;
6807}
6808
c0ab21c2 6809/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6810 See it for a description of the parameters.
fcd3b13d 6811 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6812
c0ab21c2
TT
6813void
6814cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6815 int use_existing_cu)
a2ce51a0 6816{
a2ce51a0 6817 struct signatured_type *sig_type;
a2ce51a0
DE
6818
6819 /* Verify we can do the following downcast, and that we have the
6820 data we need. */
6821 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6822 sig_type = (struct signatured_type *) this_cu;
6823 gdb_assert (sig_type->dwo_unit != NULL);
6824
6aa5f3a6
DE
6825 if (use_existing_cu && this_cu->cu != NULL)
6826 {
6827 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6828 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6829 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6830 }
6831 else
6832 {
6833 /* If !use_existing_cu, this_cu->cu must be NULL. */
6834 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6835 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6836 }
6837
6838 /* A future optimization, if needed, would be to use an existing
6839 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6840 could share abbrev tables. */
a2ce51a0
DE
6841
6842 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6843 NULL /* stub_comp_unit_die */,
6844 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6845 this, &info_ptr,
3e225074 6846 &comp_unit_die,
c0ab21c2 6847 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6848 {
6849 /* Dummy die. */
c0ab21c2 6850 dummy_p = true;
a2ce51a0 6851 }
a2ce51a0
DE
6852}
6853
fd820528 6854/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6855 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6856
f4dc4d17
DE
6857 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6858 Otherwise the table specified in the comp unit header is read in and used.
6859 This is an optimization for when we already have the abbrev table.
6860
dee91e82 6861 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6862 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6863
c0ab21c2
TT
6864cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6865 struct abbrev_table *abbrev_table,
6751ebae 6866 int use_existing_cu,
c0ab21c2
TT
6867 bool skip_partial)
6868 : die_reader_specs {},
6751ebae 6869 m_this_cu (this_cu)
c906108c 6870{
ed2dc618 6871 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6872 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6873 struct dwarf2_section_info *section = this_cu->section;
96b79293 6874 bfd *abfd = section->get_bfd_owner ();
dee91e82 6875 struct dwarf2_cu *cu;
c0ab21c2 6876 const gdb_byte *begin_info_ptr;
dee91e82 6877 struct signatured_type *sig_type = NULL;
4bdcc0c1 6878 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6879 /* Non-zero if CU currently points to a DWO file and we need to
6880 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6881 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6882 int rereading_dwo_cu = 0;
c906108c 6883
b4f54984 6884 if (dwarf_die_debug)
9d8780f0 6885 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6886 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6887 sect_offset_str (this_cu->sect_off));
09406207 6888
a2ce51a0
DE
6889 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6890 file (instead of going through the stub), short-circuit all of this. */
6891 if (this_cu->reading_dwo_directly)
6892 {
6893 /* Narrow down the scope of possibilities to have to understand. */
6894 gdb_assert (this_cu->is_debug_types);
6895 gdb_assert (abbrev_table == NULL);
6751ebae 6896 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6897 return;
6898 }
6899
dee91e82 6900 /* This is cheap if the section is already read in. */
96b79293 6901 section->read (objfile);
dee91e82 6902
9c541725 6903 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6904
6905 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6906
6907 if (use_existing_cu && this_cu->cu != NULL)
6908 {
6909 cu = this_cu->cu;
42e7ad6c
DE
6910 /* If this CU is from a DWO file we need to start over, we need to
6911 refetch the attributes from the skeleton CU.
6912 This could be optimized by retrieving those attributes from when we
6913 were here the first time: the previous comp_unit_die was stored in
6914 comp_unit_obstack. But there's no data yet that we need this
6915 optimization. */
6916 if (cu->dwo_unit != NULL)
6917 rereading_dwo_cu = 1;
dee91e82
DE
6918 }
6919 else
6920 {
6921 /* If !use_existing_cu, this_cu->cu must be NULL. */
6922 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6923 m_new_cu.reset (new dwarf2_cu (this_cu));
6924 cu = m_new_cu.get ();
42e7ad6c 6925 }
dee91e82 6926
b0c7bfa9 6927 /* Get the header. */
9c541725 6928 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6929 {
6930 /* We already have the header, there's no need to read it in again. */
9c541725 6931 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6932 }
6933 else
6934 {
3019eac3 6935 if (this_cu->is_debug_types)
dee91e82 6936 {
ed2dc618
SM
6937 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6938 &cu->header, section,
4bdcc0c1 6939 abbrev_section, info_ptr,
43988095 6940 rcuh_kind::TYPE);
dee91e82 6941
42e7ad6c
DE
6942 /* Since per_cu is the first member of struct signatured_type,
6943 we can go from a pointer to one to a pointer to the other. */
6944 sig_type = (struct signatured_type *) this_cu;
43988095 6945 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6946 gdb_assert (sig_type->type_offset_in_tu
6947 == cu->header.type_cu_offset_in_tu);
6948 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6949
42e7ad6c
DE
6950 /* LENGTH has not been set yet for type units if we're
6951 using .gdb_index. */
4057dfde 6952 this_cu->length = cu->header.get_length ();
3019eac3
DE
6953
6954 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6955 sig_type->type_offset_in_section =
6956 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6957
6958 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6959 }
6960 else
6961 {
ed2dc618
SM
6962 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6963 &cu->header, section,
4bdcc0c1 6964 abbrev_section,
43988095
JK
6965 info_ptr,
6966 rcuh_kind::COMPILE);
dee91e82 6967
9c541725 6968 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6969 if (this_cu->length == 0)
6970 this_cu->length = cu->header.get_length ();
6971 else
6972 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6973 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6974 }
6975 }
10b3939b 6976
6caca83c 6977 /* Skip dummy compilation units. */
dee91e82 6978 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6979 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6980 {
6981 dummy_p = true;
6982 return;
6983 }
6caca83c 6984
433df2d4
DE
6985 /* If we don't have them yet, read the abbrevs for this compilation unit.
6986 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6987 done. */
f4dc4d17 6988 if (abbrev_table != NULL)
685af9cd
TT
6989 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6990 else
f4dc4d17 6991 {
c0ab21c2 6992 m_abbrev_table_holder
86de1d91
TT
6993 = abbrev_table::read (objfile, abbrev_section,
6994 cu->header.abbrev_sect_off);
c0ab21c2 6995 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6996 }
af703f96 6997
dee91e82 6998 /* Read the top level CU/TU die. */
c0ab21c2 6999 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7000 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7001
58f0c718 7002 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7003 {
7004 dummy_p = true;
7005 return;
7006 }
58f0c718 7007
b0c7bfa9 7008 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7009 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7010 table from the DWO file and pass the ownership over to us. It will be
7011 referenced from READER, so we must make sure to free it after we're done
7012 with READER.
7013
b0c7bfa9
DE
7014 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7015 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7016 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7017 if (dwo_name != nullptr)
3019eac3 7018 {
3019eac3 7019 struct dwo_unit *dwo_unit;
b0c7bfa9 7020 struct die_info *dwo_comp_unit_die;
3019eac3 7021
3e225074 7022 if (comp_unit_die->has_children)
6a506a2d 7023 {
b98664d3 7024 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7025 " has children (offset %s) [in module %s]"),
7026 sect_offset_str (this_cu->sect_off),
7027 bfd_get_filename (abfd));
6a506a2d 7028 }
c0ab21c2 7029 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7030 if (dwo_unit != NULL)
3019eac3 7031 {
6a506a2d 7032 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7033 comp_unit_die, NULL,
c0ab21c2 7034 this, &info_ptr,
3e225074 7035 &dwo_comp_unit_die,
c0ab21c2 7036 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7037 {
7038 /* Dummy die. */
c0ab21c2 7039 dummy_p = true;
6a506a2d
DE
7040 return;
7041 }
7042 comp_unit_die = dwo_comp_unit_die;
7043 }
7044 else
7045 {
7046 /* Yikes, we couldn't find the rest of the DIE, we only have
7047 the stub. A complaint has already been logged. There's
7048 not much more we can do except pass on the stub DIE to
7049 die_reader_func. We don't want to throw an error on bad
7050 debug info. */
3019eac3
DE
7051 }
7052 }
c0ab21c2 7053}
3019eac3 7054
6751ebae
TT
7055void
7056cutu_reader::keep ()
c0ab21c2 7057{
b0c7bfa9 7058 /* Done, clean up. */
6751ebae
TT
7059 gdb_assert (!dummy_p);
7060 if (m_new_cu != NULL)
348e048f 7061 {
c0ab21c2
TT
7062 struct dwarf2_per_objfile *dwarf2_per_objfile
7063 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7064 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7065 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7066 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7067 /* The chain owns it now. */
c0ab21c2 7068 m_new_cu.release ();
348e048f 7069 }
dee91e82
DE
7070}
7071
18a8505e
AT
7072/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7073 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7074 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7075
7076 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7077 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7078
7079 We fill in THIS_CU->length.
7080
dee91e82 7081 THIS_CU->cu is always freed when done.
3019eac3 7082 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7083 to care whether it refers to the "main" CU or the DWO CU.
7084
7085 When parent_cu is passed, it is used to provide a default value for
7086 str_offsets_base and addr_base from the parent. */
dee91e82 7087
c0ab21c2
TT
7088cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7089 struct dwarf2_cu *parent_cu,
7090 struct dwo_file *dwo_file)
7091 : die_reader_specs {},
7092 m_this_cu (this_cu)
dee91e82 7093{
ed2dc618 7094 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7095 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7096 struct dwarf2_section_info *section = this_cu->section;
96b79293 7097 bfd *abfd = section->get_bfd_owner ();
33e80786 7098 struct dwarf2_section_info *abbrev_section;
d521ce57 7099 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7100
b4f54984 7101 if (dwarf_die_debug)
9d8780f0 7102 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7103 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7104 sect_offset_str (this_cu->sect_off));
09406207 7105
dee91e82
DE
7106 gdb_assert (this_cu->cu == NULL);
7107
33e80786
DE
7108 abbrev_section = (dwo_file != NULL
7109 ? &dwo_file->sections.abbrev
7110 : get_abbrev_section_for_cu (this_cu));
7111
dee91e82 7112 /* This is cheap if the section is already read in. */
96b79293 7113 section->read (objfile);
dee91e82 7114
c0ab21c2 7115 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7116
9c541725 7117 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7118 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7119 &m_new_cu->header, section,
4bdcc0c1 7120 abbrev_section, info_ptr,
43988095
JK
7121 (this_cu->is_debug_types
7122 ? rcuh_kind::TYPE
7123 : rcuh_kind::COMPILE));
dee91e82 7124
18a8505e
AT
7125 if (parent_cu != nullptr)
7126 {
c0ab21c2
TT
7127 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7128 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7129 }
4057dfde 7130 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7131
7132 /* Skip dummy compilation units. */
7133 if (info_ptr >= begin_info_ptr + this_cu->length
7134 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7135 {
7136 dummy_p = true;
7137 return;
7138 }
72bf9492 7139
c0ab21c2 7140 m_abbrev_table_holder
86de1d91
TT
7141 = abbrev_table::read (objfile, abbrev_section,
7142 m_new_cu->header.abbrev_sect_off);
dee91e82 7143
c0ab21c2
TT
7144 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7145 m_abbrev_table_holder.get ());
3e225074 7146 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7147}
7148
0018ea6f
DE
7149\f
7150/* Type Unit Groups.
dee91e82 7151
0018ea6f
DE
7152 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7153 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7154 so that all types coming from the same compilation (.o file) are grouped
7155 together. A future step could be to put the types in the same symtab as
7156 the CU the types ultimately came from. */
ff013f42 7157
f4dc4d17
DE
7158static hashval_t
7159hash_type_unit_group (const void *item)
7160{
9a3c8263
SM
7161 const struct type_unit_group *tu_group
7162 = (const struct type_unit_group *) item;
f4dc4d17 7163
094b34ac 7164 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7165}
348e048f
DE
7166
7167static int
f4dc4d17 7168eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7169{
9a3c8263
SM
7170 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7171 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7172
094b34ac 7173 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7174}
348e048f 7175
f4dc4d17
DE
7176/* Allocate a hash table for type unit groups. */
7177
eaa5fa8b 7178static htab_up
298e9637 7179allocate_type_unit_groups_table ()
f4dc4d17 7180{
eaa5fa8b
TT
7181 return htab_up (htab_create_alloc (3,
7182 hash_type_unit_group,
7183 eq_type_unit_group,
7184 NULL, xcalloc, xfree));
f4dc4d17 7185}
dee91e82 7186
f4dc4d17
DE
7187/* Type units that don't have DW_AT_stmt_list are grouped into their own
7188 partial symtabs. We combine several TUs per psymtab to not let the size
7189 of any one psymtab grow too big. */
7190#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7191#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7192
094b34ac 7193/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7194 Create the type_unit_group object used to hold one or more TUs. */
7195
7196static struct type_unit_group *
094b34ac 7197create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7198{
518817b3
SM
7199 struct dwarf2_per_objfile *dwarf2_per_objfile
7200 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7201 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7202 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7203 struct type_unit_group *tu_group;
f4dc4d17
DE
7204
7205 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7206 struct type_unit_group);
094b34ac 7207 per_cu = &tu_group->per_cu;
518817b3 7208 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7209
094b34ac
DE
7210 if (dwarf2_per_objfile->using_index)
7211 {
7212 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7213 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7214 }
7215 else
7216 {
9c541725 7217 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7218 dwarf2_psymtab *pst;
528e1572 7219 std::string name;
094b34ac
DE
7220
7221 /* Give the symtab a useful name for debug purposes. */
7222 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7223 name = string_printf ("<type_units_%d>",
7224 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7225 else
528e1572 7226 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7227
528e1572 7228 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7229 pst->anonymous = true;
094b34ac 7230 }
f4dc4d17 7231
094b34ac 7232 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7233 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7234
7235 return tu_group;
7236}
7237
094b34ac
DE
7238/* Look up the type_unit_group for type unit CU, and create it if necessary.
7239 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7240
7241static struct type_unit_group *
ff39bb5e 7242get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7243{
518817b3
SM
7244 struct dwarf2_per_objfile *dwarf2_per_objfile
7245 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7246 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7247 struct type_unit_group *tu_group;
7248 void **slot;
7249 unsigned int line_offset;
7250 struct type_unit_group type_unit_group_for_lookup;
7251
7252 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7253 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7254
7255 /* Do we need to create a new group, or can we use an existing one? */
7256
7257 if (stmt_list)
7258 {
7259 line_offset = DW_UNSND (stmt_list);
7260 ++tu_stats->nr_symtab_sharers;
7261 }
7262 else
7263 {
7264 /* Ugh, no stmt_list. Rare, but we have to handle it.
7265 We can do various things here like create one group per TU or
7266 spread them over multiple groups to split up the expansion work.
7267 To avoid worst case scenarios (too many groups or too large groups)
7268 we, umm, group them in bunches. */
7269 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7270 | (tu_stats->nr_stmt_less_type_units
7271 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7272 ++tu_stats->nr_stmt_less_type_units;
7273 }
7274
094b34ac 7275 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7276 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7277 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7278 &type_unit_group_for_lookup, INSERT);
7279 if (*slot != NULL)
7280 {
9a3c8263 7281 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7282 gdb_assert (tu_group != NULL);
7283 }
7284 else
7285 {
9c541725 7286 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7287 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7288 *slot = tu_group;
7289 ++tu_stats->nr_symtabs;
7290 }
7291
7292 return tu_group;
7293}
0018ea6f
DE
7294\f
7295/* Partial symbol tables. */
7296
7297/* Create a psymtab named NAME and assign it to PER_CU.
7298
7299 The caller must fill in the following details:
7300 dirname, textlow, texthigh. */
7301
891813be 7302static dwarf2_psymtab *
0018ea6f
DE
7303create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7304{
e3b94546 7305 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7306 dwarf2_psymtab *pst;
0018ea6f 7307
9f4e76a4 7308 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7309
6d94535f 7310 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7311
7312 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7313 per_cu->v.psymtab = pst;
7314
7315 return pst;
7316}
7317
c0ab21c2 7318/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7319
7320static void
7321process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7322 const gdb_byte *info_ptr,
0018ea6f 7323 struct die_info *comp_unit_die,
c0ab21c2 7324 enum language pretend_language)
0018ea6f
DE
7325{
7326 struct dwarf2_cu *cu = reader->cu;
518817b3 7327 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 7328 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7329 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7330 CORE_ADDR baseaddr;
7331 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7332 dwarf2_psymtab *pst;
3a2b436a 7333 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7334 const char *filename;
0018ea6f 7335
0018ea6f
DE
7336 gdb_assert (! per_cu->is_debug_types);
7337
c0ab21c2 7338 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7339
0018ea6f 7340 /* Allocate a new partial symbol table structure. */
2e927613
TV
7341 gdb::unique_xmalloc_ptr<char> debug_filename;
7342 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7343 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7344 if (filename == NULL)
0018ea6f 7345 filename = "";
2e927613
TV
7346 else if (strcmp (filename, artificial) == 0)
7347 {
7348 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7349 sect_offset_str (per_cu->sect_off),
7350 (char *) NULL));
2e927613
TV
7351 filename = debug_filename.get ();
7352 }
0018ea6f
DE
7353
7354 pst = create_partial_symtab (per_cu, filename);
7355
7356 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7357 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7358
b3b3bada 7359 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7360
7361 dwarf2_find_base_address (comp_unit_die, cu);
7362
7363 /* Possibly set the default values of LOWPC and HIGHPC from
7364 `DW_AT_ranges'. */
3a2b436a
JK
7365 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7366 &best_highpc, cu, pst);
7367 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7368 {
7369 CORE_ADDR low
7370 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7371 - baseaddr);
7372 CORE_ADDR high
7373 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7374 - baseaddr - 1);
7375 /* Store the contiguous range if it is not empty; it can be
7376 empty for CUs with no code. */
d320c2b5
TT
7377 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7378 low, high, pst);
79748972 7379 }
0018ea6f
DE
7380
7381 /* Check if comp unit has_children.
7382 If so, read the rest of the partial symbols from this comp unit.
7383 If not, there's no more debug_info for this comp unit. */
3e225074 7384 if (comp_unit_die->has_children)
0018ea6f
DE
7385 {
7386 struct partial_die_info *first_die;
7387 CORE_ADDR lowpc, highpc;
7388
7389 lowpc = ((CORE_ADDR) -1);
7390 highpc = ((CORE_ADDR) 0);
7391
7392 first_die = load_partial_dies (reader, info_ptr, 1);
7393
7394 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7395 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7396
7397 /* If we didn't find a lowpc, set it to highpc to avoid
7398 complaints from `maint check'. */
7399 if (lowpc == ((CORE_ADDR) -1))
7400 lowpc = highpc;
7401
7402 /* If the compilation unit didn't have an explicit address range,
7403 then use the information extracted from its child dies. */
e385593e 7404 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7405 {
7406 best_lowpc = lowpc;
7407 best_highpc = highpc;
7408 }
7409 }
4ae976d1 7410 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7411 best_lowpc + baseaddr)
7412 - baseaddr);
4ae976d1 7413 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7414 best_highpc + baseaddr)
7415 - baseaddr);
0018ea6f 7416
8763cede 7417 end_psymtab_common (objfile, pst);
0018ea6f 7418
ae640021 7419 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7420 {
7421 int i;
ae640021 7422 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7423
7424 /* Fill in 'dependencies' here; we fill in 'users' in a
7425 post-pass. */
7426 pst->number_of_dependencies = len;
a9342b62
TT
7427 pst->dependencies
7428 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7429 for (i = 0; i < len; ++i)
7430 {
7431 pst->dependencies[i]
7432 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7433 }
0018ea6f 7434
ae640021 7435 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7436 }
7437
7438 /* Get the list of files included in the current compilation unit,
7439 and build a psymtab for each of them. */
7440 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7441
b4f54984 7442 if (dwarf_read_debug)
b926417a
TT
7443 fprintf_unfiltered (gdb_stdlog,
7444 "Psymtab for %s unit @%s: %s - %s"
7445 ", %d global, %d static syms\n",
7446 per_cu->is_debug_types ? "type" : "comp",
7447 sect_offset_str (per_cu->sect_off),
7448 paddress (gdbarch, pst->text_low (objfile)),
7449 paddress (gdbarch, pst->text_high (objfile)),
7450 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7451}
7452
7453/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7454 Process compilation unit THIS_CU for a psymtab. */
7455
7456static void
7457process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7458 bool want_partial_unit,
b93601f3 7459 enum language pretend_language)
0018ea6f
DE
7460{
7461 /* If this compilation unit was already read in, free the
7462 cached copy in order to read it in again. This is
7463 necessary because we skipped some symbols when we first
7464 read in the compilation unit (see load_partial_dies).
7465 This problem could be avoided, but the benefit is unclear. */
7466 if (this_cu->cu != NULL)
7467 free_one_cached_comp_unit (this_cu);
7468
6751ebae 7469 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7470
58990295
TV
7471 switch (reader.comp_unit_die->tag)
7472 {
7473 case DW_TAG_compile_unit:
7474 this_cu->unit_type = DW_UT_compile;
7475 break;
7476 case DW_TAG_partial_unit:
7477 this_cu->unit_type = DW_UT_partial;
7478 break;
7479 default:
7480 abort ();
7481 }
7482
c0ab21c2 7483 if (reader.dummy_p)
f1902523 7484 {
c0ab21c2 7485 /* Nothing. */
f1902523 7486 }
c0ab21c2 7487 else if (this_cu->is_debug_types)
3e225074
TT
7488 build_type_psymtabs_reader (&reader, reader.info_ptr,
7489 reader.comp_unit_die);
135f5437
TT
7490 else if (want_partial_unit
7491 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7492 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7493 reader.comp_unit_die,
c0ab21c2 7494 pretend_language);
0018ea6f 7495
58990295
TV
7496 this_cu->lang = this_cu->cu->language;
7497
0018ea6f 7498 /* Age out any secondary CUs. */
ed2dc618 7499 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7500}
f4dc4d17
DE
7501
7502/* Reader function for build_type_psymtabs. */
7503
7504static void
7505build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7506 const gdb_byte *info_ptr,
3e225074 7507 struct die_info *type_unit_die)
f4dc4d17 7508{
ed2dc618 7509 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7510 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7511 struct objfile *objfile = dwarf2_per_objfile->objfile;
7512 struct dwarf2_cu *cu = reader->cu;
7513 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7514 struct signatured_type *sig_type;
f4dc4d17
DE
7515 struct type_unit_group *tu_group;
7516 struct attribute *attr;
7517 struct partial_die_info *first_die;
7518 CORE_ADDR lowpc, highpc;
891813be 7519 dwarf2_psymtab *pst;
f4dc4d17 7520
0186c6a7
DE
7521 gdb_assert (per_cu->is_debug_types);
7522 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7523
3e225074 7524 if (! type_unit_die->has_children)
f4dc4d17
DE
7525 return;
7526
052c8bb8 7527 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7528 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7529
df07e2c7 7530 if (tu_group->tus == nullptr)
a8b3b8e9 7531 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7532 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7533
7534 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7535 pst = create_partial_symtab (per_cu, "");
6d94535f 7536 pst->anonymous = true;
f4dc4d17
DE
7537
7538 first_die = load_partial_dies (reader, info_ptr, 1);
7539
7540 lowpc = (CORE_ADDR) -1;
7541 highpc = (CORE_ADDR) 0;
7542 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7543
8763cede 7544 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7545}
7546
73051182
DE
7547/* Struct used to sort TUs by their abbreviation table offset. */
7548
7549struct tu_abbrev_offset
7550{
b2bdb8cf
SM
7551 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7552 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7553 {}
7554
7555 signatured_type *sig_type;
73051182
DE
7556 sect_offset abbrev_offset;
7557};
7558
484cf504 7559/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7560
484cf504
TT
7561static bool
7562sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7563 const struct tu_abbrev_offset &b)
73051182 7564{
484cf504 7565 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7566}
7567
7568/* Efficiently read all the type units.
7569 This does the bulk of the work for build_type_psymtabs.
7570
7571 The efficiency is because we sort TUs by the abbrev table they use and
7572 only read each abbrev table once. In one program there are 200K TUs
7573 sharing 8K abbrev tables.
7574
7575 The main purpose of this function is to support building the
7576 dwarf2_per_objfile->type_unit_groups table.
7577 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7578 can collapse the search space by grouping them by stmt_list.
7579 The savings can be significant, in the same program from above the 200K TUs
7580 share 8K stmt_list tables.
7581
7582 FUNC is expected to call get_type_unit_group, which will create the
7583 struct type_unit_group if necessary and add it to
7584 dwarf2_per_objfile->type_unit_groups. */
7585
7586static void
ed2dc618 7587build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7588{
73051182 7589 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7590 abbrev_table_up abbrev_table;
73051182 7591 sect_offset abbrev_offset;
73051182
DE
7592
7593 /* It's up to the caller to not call us multiple times. */
7594 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7595
b2bdb8cf 7596 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7597 return;
7598
7599 /* TUs typically share abbrev tables, and there can be way more TUs than
7600 abbrev tables. Sort by abbrev table to reduce the number of times we
7601 read each abbrev table in.
7602 Alternatives are to punt or to maintain a cache of abbrev tables.
7603 This is simpler and efficient enough for now.
7604
7605 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7606 symtab to use). Typically TUs with the same abbrev offset have the same
7607 stmt_list value too so in practice this should work well.
7608
7609 The basic algorithm here is:
7610
7611 sort TUs by abbrev table
7612 for each TU with same abbrev table:
7613 read abbrev table if first user
7614 read TU top level DIE
7615 [IWBN if DWO skeletons had DW_AT_stmt_list]
7616 call FUNC */
7617
b4f54984 7618 if (dwarf_read_debug)
73051182
DE
7619 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7620
7621 /* Sort in a separate table to maintain the order of all_type_units
7622 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7623 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7624 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7625
7626 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7627 sorted_by_abbrev.emplace_back
7628 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7629 sig_type->per_cu.section,
7630 sig_type->per_cu.sect_off));
73051182 7631
484cf504
TT
7632 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7633 sort_tu_by_abbrev_offset);
73051182 7634
9c541725 7635 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7636
b2bdb8cf 7637 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7638 {
73051182
DE
7639 /* Switch to the next abbrev table if necessary. */
7640 if (abbrev_table == NULL
b2bdb8cf 7641 || tu.abbrev_offset != abbrev_offset)
73051182 7642 {
b2bdb8cf 7643 abbrev_offset = tu.abbrev_offset;
73051182 7644 abbrev_table =
86de1d91
TT
7645 abbrev_table::read (dwarf2_per_objfile->objfile,
7646 &dwarf2_per_objfile->abbrev,
7647 abbrev_offset);
73051182
DE
7648 ++tu_stats->nr_uniq_abbrev_tables;
7649 }
7650
c0ab21c2 7651 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7652 0, false);
c0ab21c2
TT
7653 if (!reader.dummy_p)
7654 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7655 reader.comp_unit_die);
73051182 7656 }
6aa5f3a6 7657}
73051182 7658
6aa5f3a6
DE
7659/* Print collected type unit statistics. */
7660
7661static void
ed2dc618 7662print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7663{
7664 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7665
7666 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7667 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7668 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7669 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7670 tu_stats->nr_uniq_abbrev_tables);
7671 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7672 tu_stats->nr_symtabs);
7673 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7674 tu_stats->nr_symtab_sharers);
7675 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7676 tu_stats->nr_stmt_less_type_units);
7677 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7678 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7679}
7680
f4dc4d17
DE
7681/* Traversal function for build_type_psymtabs. */
7682
7683static int
7684build_type_psymtab_dependencies (void **slot, void *info)
7685{
ed2dc618
SM
7686 struct dwarf2_per_objfile *dwarf2_per_objfile
7687 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7688 struct objfile *objfile = dwarf2_per_objfile->objfile;
7689 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7690 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7691 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7692 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7693 int i;
7694
7695 gdb_assert (len > 0);
197400e8 7696 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7697
7698 pst->number_of_dependencies = len;
a9342b62 7699 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7700 for (i = 0; i < len; ++i)
f4dc4d17 7701 {
df07e2c7 7702 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7703 gdb_assert (iter->per_cu.is_debug_types);
7704 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7705 iter->type_unit_group = tu_group;
f4dc4d17
DE
7706 }
7707
df07e2c7
AB
7708 delete tu_group->tus;
7709 tu_group->tus = nullptr;
348e048f
DE
7710
7711 return 1;
7712}
7713
7714/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7715 Build partial symbol tables for the .debug_types comp-units. */
7716
7717static void
ed2dc618 7718build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7719{
ed2dc618 7720 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7721 return;
7722
ed2dc618 7723 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7724}
f4dc4d17 7725
6aa5f3a6
DE
7726/* Traversal function for process_skeletonless_type_unit.
7727 Read a TU in a DWO file and build partial symbols for it. */
7728
7729static int
7730process_skeletonless_type_unit (void **slot, void *info)
7731{
7732 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7733 struct dwarf2_per_objfile *dwarf2_per_objfile
7734 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7735 struct signatured_type find_entry, *entry;
7736
7737 /* If this TU doesn't exist in the global table, add it and read it in. */
7738
7739 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7740 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7741
7742 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7743 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7744 &find_entry, INSERT);
6aa5f3a6
DE
7745 /* If we've already seen this type there's nothing to do. What's happening
7746 is we're doing our own version of comdat-folding here. */
7747 if (*slot != NULL)
7748 return 1;
7749
7750 /* This does the job that create_all_type_units would have done for
7751 this TU. */
ed2dc618
SM
7752 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7753 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7754 *slot = entry;
7755
7756 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7757 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7758 if (!reader.dummy_p)
7759 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7760 reader.comp_unit_die);
6aa5f3a6
DE
7761
7762 return 1;
7763}
7764
7765/* Traversal function for process_skeletonless_type_units. */
7766
7767static int
7768process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7769{
7770 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7771
7772 if (dwo_file->tus != NULL)
b0b6a987
TT
7773 htab_traverse_noresize (dwo_file->tus.get (),
7774 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7775
7776 return 1;
7777}
7778
7779/* Scan all TUs of DWO files, verifying we've processed them.
7780 This is needed in case a TU was emitted without its skeleton.
7781 Note: This can't be done until we know what all the DWO files are. */
7782
7783static void
ed2dc618 7784process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7785{
7786 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7787 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7788 && dwarf2_per_objfile->dwo_files != NULL)
7789 {
51ac9db5 7790 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7791 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7792 dwarf2_per_objfile);
6aa5f3a6 7793 }
348e048f
DE
7794}
7795
ed2dc618 7796/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7797
7798static void
ed2dc618 7799set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7800{
b76e467d 7801 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7802 {
891813be 7803 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7804
36586728
TT
7805 if (pst == NULL)
7806 continue;
7807
b76e467d 7808 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7809 {
7810 /* Set the 'user' field only if it is not already set. */
7811 if (pst->dependencies[j]->user == NULL)
7812 pst->dependencies[j]->user = pst;
7813 }
7814 }
7815}
7816
93311388
DE
7817/* Build the partial symbol table by doing a quick pass through the
7818 .debug_info and .debug_abbrev sections. */
72bf9492 7819
93311388 7820static void
ed2dc618 7821dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7822{
ed2dc618 7823 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7824
b4f54984 7825 if (dwarf_read_debug)
45cfd468
DE
7826 {
7827 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7828 objfile_name (objfile));
45cfd468
DE
7829 }
7830
76935768
TT
7831 scoped_restore restore_reading_psyms
7832 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7833 true);
98bfdba5 7834
96b79293 7835 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7836
93311388
DE
7837 /* Any cached compilation units will be linked by the per-objfile
7838 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7839 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7840
ed2dc618 7841 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7842
ed2dc618 7843 create_all_comp_units (dwarf2_per_objfile);
c906108c 7844
60606b2c
TT
7845 /* Create a temporary address map on a temporary obstack. We later
7846 copy this to the final obstack. */
8268c778 7847 auto_obstack temp_obstack;
791afaa2
TT
7848
7849 scoped_restore save_psymtabs_addrmap
d320c2b5 7850 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7851 addrmap_create_mutable (&temp_obstack));
72bf9492 7852
b76e467d 7853 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3d5afab3
TV
7854 {
7855 if (per_cu->v.psymtab != NULL)
7856 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7857 continue;
7858 process_psymtab_comp_unit (per_cu, false, language_minimal);
7859 }
ff013f42 7860
6aa5f3a6 7861 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7862 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7863
7864 /* Now that all TUs have been processed we can fill in the dependencies. */
7865 if (dwarf2_per_objfile->type_unit_groups != NULL)
7866 {
eaa5fa8b 7867 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7868 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7869 }
7870
b4f54984 7871 if (dwarf_read_debug)
ed2dc618 7872 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7873
ed2dc618 7874 set_partial_user (dwarf2_per_objfile);
95554aad 7875
d320c2b5
TT
7876 objfile->partial_symtabs->psymtabs_addrmap
7877 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7878 objfile->partial_symtabs->obstack ());
791afaa2
TT
7879 /* At this point we want to keep the address map. */
7880 save_psymtabs_addrmap.release ();
ff013f42 7881
b4f54984 7882 if (dwarf_read_debug)
45cfd468 7883 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7884 objfile_name (objfile));
ae038cb0
DJ
7885}
7886
dee91e82
DE
7887/* Load the partial DIEs for a secondary CU into memory.
7888 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7889
dee91e82
DE
7890static void
7891load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7892{
6751ebae 7893 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7894
7895 if (!reader.dummy_p)
7896 {
7897 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7898 language_minimal);
7899
7900 /* Check if comp unit has_children.
7901 If so, read the rest of the partial symbols from this comp unit.
7902 If not, there's no more debug_info for this comp unit. */
3e225074 7903 if (reader.comp_unit_die->has_children)
c0ab21c2 7904 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7905
7906 reader.keep ();
c0ab21c2 7907 }
ae038cb0
DJ
7908}
7909
ae038cb0 7910static void
ed2dc618 7911read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7912 struct dwarf2_section_info *section,
f1902523 7913 struct dwarf2_section_info *abbrev_section,
b76e467d 7914 unsigned int is_dwz)
ae038cb0 7915{
d521ce57 7916 const gdb_byte *info_ptr;
ed2dc618 7917 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7918
b4f54984 7919 if (dwarf_read_debug)
bf6af496 7920 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7921 section->get_name (),
7922 section->get_file_name ());
bf6af496 7923
96b79293 7924 section->read (objfile);
ae038cb0 7925
36586728 7926 info_ptr = section->buffer;
6e70227d 7927
36586728 7928 while (info_ptr < section->buffer + section->size)
ae038cb0 7929 {
ae038cb0 7930 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7931
9c541725 7932 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7933
f1902523 7934 comp_unit_head cu_header;
ed2dc618
SM
7935 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7936 abbrev_section, info_ptr,
7937 rcuh_kind::COMPILE);
ae038cb0
DJ
7938
7939 /* Save the compilation unit for later lookup. */
f1902523
JK
7940 if (cu_header.unit_type != DW_UT_type)
7941 {
7942 this_cu = XOBNEW (&objfile->objfile_obstack,
7943 struct dwarf2_per_cu_data);
7944 memset (this_cu, 0, sizeof (*this_cu));
7945 }
7946 else
7947 {
7948 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7949 struct signatured_type);
7950 memset (sig_type, 0, sizeof (*sig_type));
7951 sig_type->signature = cu_header.signature;
7952 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7953 this_cu = &sig_type->per_cu;
7954 }
7955 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7956 this_cu->sect_off = sect_off;
f1902523 7957 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7958 this_cu->is_dwz = is_dwz;
e3b94546 7959 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7960 this_cu->section = section;
ae038cb0 7961
b76e467d 7962 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7963
7964 info_ptr = info_ptr + this_cu->length;
7965 }
36586728
TT
7966}
7967
7968/* Create a list of all compilation units in OBJFILE.
7969 This is only done for -readnow and building partial symtabs. */
7970
7971static void
ed2dc618 7972create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7973{
b76e467d 7974 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7975 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7976 &dwarf2_per_objfile->abbrev, 0);
36586728 7977
b76e467d 7978 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7979 if (dwz != NULL)
ed2dc618 7980 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7981 1);
c906108c
SS
7982}
7983
5734ee8b 7984/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7985 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7986 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7987 DW_AT_ranges). See the comments of add_partial_subprogram on how
7988 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7989
72bf9492
DJ
7990static void
7991scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7992 CORE_ADDR *highpc, int set_addrmap,
7993 struct dwarf2_cu *cu)
c906108c 7994{
72bf9492 7995 struct partial_die_info *pdi;
c906108c 7996
91c24f0a
DC
7997 /* Now, march along the PDI's, descending into ones which have
7998 interesting children but skipping the children of the other ones,
7999 until we reach the end of the compilation unit. */
c906108c 8000
72bf9492 8001 pdi = first_die;
91c24f0a 8002
72bf9492
DJ
8003 while (pdi != NULL)
8004 {
52356b79 8005 pdi->fixup (cu);
c906108c 8006
f55ee35c 8007 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8008 children, so we need to look at them. Ditto for anonymous
8009 enums. */
933c6fe4 8010
72bf9492 8011 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8012 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8013 || pdi->tag == DW_TAG_imported_unit
8014 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8015 {
72bf9492 8016 switch (pdi->tag)
c906108c
SS
8017 {
8018 case DW_TAG_subprogram:
b1dc1806 8019 case DW_TAG_inlined_subroutine:
cdc07690 8020 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8021 break;
72929c62 8022 case DW_TAG_constant:
c906108c
SS
8023 case DW_TAG_variable:
8024 case DW_TAG_typedef:
91c24f0a 8025 case DW_TAG_union_type:
317d2668
TV
8026 if (!pdi->is_declaration
8027 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8028 {
72bf9492 8029 add_partial_symbol (pdi, cu);
63d06c5c
DC
8030 }
8031 break;
c906108c 8032 case DW_TAG_class_type:
680b30c7 8033 case DW_TAG_interface_type:
c906108c 8034 case DW_TAG_structure_type:
72bf9492 8035 if (!pdi->is_declaration)
c906108c 8036 {
72bf9492 8037 add_partial_symbol (pdi, cu);
c906108c 8038 }
b7fee5a3
KS
8039 if ((cu->language == language_rust
8040 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8041 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8042 set_addrmap, cu);
c906108c 8043 break;
91c24f0a 8044 case DW_TAG_enumeration_type:
72bf9492
DJ
8045 if (!pdi->is_declaration)
8046 add_partial_enumeration (pdi, cu);
c906108c
SS
8047 break;
8048 case DW_TAG_base_type:
a02abb62 8049 case DW_TAG_subrange_type:
c906108c 8050 /* File scope base type definitions are added to the partial
c5aa993b 8051 symbol table. */
72bf9492 8052 add_partial_symbol (pdi, cu);
c906108c 8053 break;
d9fa45fe 8054 case DW_TAG_namespace:
cdc07690 8055 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8056 break;
5d7cb8df 8057 case DW_TAG_module:
59c35742
AB
8058 if (!pdi->is_declaration)
8059 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8060 break;
95554aad
TT
8061 case DW_TAG_imported_unit:
8062 {
8063 struct dwarf2_per_cu_data *per_cu;
8064
f4dc4d17
DE
8065 /* For now we don't handle imported units in type units. */
8066 if (cu->per_cu->is_debug_types)
8067 {
8068 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8069 " supported in type units [in module %s]"),
518817b3 8070 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8071 }
8072
e3b94546
SM
8073 per_cu = dwarf2_find_containing_comp_unit
8074 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8075 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8076
8077 /* Go read the partial unit, if needed. */
8078 if (per_cu->v.psymtab == NULL)
135f5437 8079 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8080
ae640021 8081 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8082 }
8083 break;
74921315
KS
8084 case DW_TAG_imported_declaration:
8085 add_partial_symbol (pdi, cu);
8086 break;
c906108c
SS
8087 default:
8088 break;
8089 }
8090 }
8091
72bf9492
DJ
8092 /* If the die has a sibling, skip to the sibling. */
8093
8094 pdi = pdi->die_sibling;
8095 }
8096}
8097
8098/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8099
72bf9492 8100 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8101 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8102 Enumerators are an exception; they use the scope of their parent
8103 enumeration type, i.e. the name of the enumeration type is not
8104 prepended to the enumerator.
91c24f0a 8105
72bf9492
DJ
8106 There are two complexities. One is DW_AT_specification; in this
8107 case "parent" means the parent of the target of the specification,
8108 instead of the direct parent of the DIE. The other is compilers
8109 which do not emit DW_TAG_namespace; in this case we try to guess
8110 the fully qualified name of structure types from their members'
8111 linkage names. This must be done using the DIE's children rather
8112 than the children of any DW_AT_specification target. We only need
8113 to do this for structures at the top level, i.e. if the target of
8114 any DW_AT_specification (if any; otherwise the DIE itself) does not
8115 have a parent. */
8116
8117/* Compute the scope prefix associated with PDI's parent, in
8118 compilation unit CU. The result will be allocated on CU's
8119 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8120 field. NULL is returned if no prefix is necessary. */
15d034d0 8121static const char *
72bf9492
DJ
8122partial_die_parent_scope (struct partial_die_info *pdi,
8123 struct dwarf2_cu *cu)
8124{
15d034d0 8125 const char *grandparent_scope;
72bf9492 8126 struct partial_die_info *parent, *real_pdi;
91c24f0a 8127
72bf9492
DJ
8128 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8129 then this means the parent of the specification DIE. */
8130
8131 real_pdi = pdi;
72bf9492 8132 while (real_pdi->has_specification)
fb816e8b 8133 {
122cf0f2
AB
8134 auto res = find_partial_die (real_pdi->spec_offset,
8135 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8136 real_pdi = res.pdi;
8137 cu = res.cu;
8138 }
72bf9492
DJ
8139
8140 parent = real_pdi->die_parent;
8141 if (parent == NULL)
8142 return NULL;
8143
8144 if (parent->scope_set)
8145 return parent->scope;
8146
52356b79 8147 parent->fixup (cu);
72bf9492 8148
10b3939b 8149 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8150
acebe513
UW
8151 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8152 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8153 Work around this problem here. */
8154 if (cu->language == language_cplus
6e70227d 8155 && parent->tag == DW_TAG_namespace
acebe513
UW
8156 && strcmp (parent->name, "::") == 0
8157 && grandparent_scope == NULL)
8158 {
8159 parent->scope = NULL;
8160 parent->scope_set = 1;
8161 return NULL;
8162 }
8163
0a4b0913 8164 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8165 if (pdi->tag == DW_TAG_enumerator)
8166 /* Enumerators should not get the name of the enumeration as a prefix. */
8167 parent->scope = grandparent_scope;
8168 else if (parent->tag == DW_TAG_namespace
f55ee35c 8169 || parent->tag == DW_TAG_module
72bf9492
DJ
8170 || parent->tag == DW_TAG_structure_type
8171 || parent->tag == DW_TAG_class_type
680b30c7 8172 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8173 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8174 || parent->tag == DW_TAG_enumeration_type
8175 || (cu->language == language_fortran
8176 && parent->tag == DW_TAG_subprogram
8177 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8178 {
8179 if (grandparent_scope == NULL)
8180 parent->scope = parent->name;
8181 else
3e43a32a
MS
8182 parent->scope = typename_concat (&cu->comp_unit_obstack,
8183 grandparent_scope,
f55ee35c 8184 parent->name, 0, cu);
72bf9492 8185 }
72bf9492
DJ
8186 else
8187 {
8188 /* FIXME drow/2004-04-01: What should we be doing with
8189 function-local names? For partial symbols, we should probably be
8190 ignoring them. */
fa9c3fa0
TT
8191 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8192 dwarf_tag_name (parent->tag),
8193 sect_offset_str (pdi->sect_off));
72bf9492 8194 parent->scope = grandparent_scope;
c906108c
SS
8195 }
8196
72bf9492
DJ
8197 parent->scope_set = 1;
8198 return parent->scope;
8199}
8200
8201/* Return the fully scoped name associated with PDI, from compilation unit
8202 CU. The result will be allocated with malloc. */
4568ecf9 8203
43816ebc 8204static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8205partial_die_full_name (struct partial_die_info *pdi,
8206 struct dwarf2_cu *cu)
8207{
15d034d0 8208 const char *parent_scope;
72bf9492 8209
98bfdba5
PA
8210 /* If this is a template instantiation, we can not work out the
8211 template arguments from partial DIEs. So, unfortunately, we have
8212 to go through the full DIEs. At least any work we do building
8213 types here will be reused if full symbols are loaded later. */
8214 if (pdi->has_template_arguments)
8215 {
52356b79 8216 pdi->fixup (cu);
98bfdba5
PA
8217
8218 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8219 {
8220 struct die_info *die;
8221 struct attribute attr;
8222 struct dwarf2_cu *ref_cu = cu;
8223
b64f50a1 8224 /* DW_FORM_ref_addr is using section offset. */
b4069958 8225 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8226 attr.form = DW_FORM_ref_addr;
9c541725 8227 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8228 die = follow_die_ref (NULL, &attr, &ref_cu);
8229
43816ebc 8230 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8231 }
8232 }
8233
72bf9492
DJ
8234 parent_scope = partial_die_parent_scope (pdi, cu);
8235 if (parent_scope == NULL)
8236 return NULL;
8237 else
43816ebc
TT
8238 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8239 pdi->name, 0, cu));
c906108c
SS
8240}
8241
8242static void
72bf9492 8243add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8244{
518817b3
SM
8245 struct dwarf2_per_objfile *dwarf2_per_objfile
8246 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8247 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8248 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8249 CORE_ADDR addr = 0;
15d034d0 8250 const char *actual_name = NULL;
e142c38c
DJ
8251 CORE_ADDR baseaddr;
8252
b3b3bada 8253 baseaddr = objfile->text_section_offset ();
c906108c 8254
43816ebc
TT
8255 gdb::unique_xmalloc_ptr<char> built_actual_name
8256 = partial_die_full_name (pdi, cu);
15d034d0 8257 if (built_actual_name != NULL)
43816ebc 8258 actual_name = built_actual_name.get ();
63d06c5c 8259
72bf9492
DJ
8260 if (actual_name == NULL)
8261 actual_name = pdi->name;
8262
76e288d1
TT
8263 partial_symbol psymbol;
8264 memset (&psymbol, 0, sizeof (psymbol));
8265 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8266 psymbol.ginfo.section = -1;
8267
8268 /* The code below indicates that the psymbol should be installed by
8269 setting this. */
8270 gdb::optional<psymbol_placement> where;
8271
c906108c
SS
8272 switch (pdi->tag)
8273 {
b1dc1806 8274 case DW_TAG_inlined_subroutine:
c906108c 8275 case DW_TAG_subprogram:
79748972
TT
8276 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8277 - baseaddr);
0a4b0913
AB
8278 if (pdi->is_external
8279 || cu->language == language_ada
8280 || (cu->language == language_fortran
8281 && pdi->die_parent != NULL
8282 && pdi->die_parent->tag == DW_TAG_subprogram))
8283 {
8284 /* Normally, only "external" DIEs are part of the global scope.
8285 But in Ada and Fortran, we want to be able to access nested
8286 procedures globally. So all Ada and Fortran subprograms are
8287 stored in the global scope. */
76e288d1 8288 where = psymbol_placement::GLOBAL;
c906108c
SS
8289 }
8290 else
76e288d1
TT
8291 where = psymbol_placement::STATIC;
8292
8293 psymbol.domain = VAR_DOMAIN;
8294 psymbol.aclass = LOC_BLOCK;
8295 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8296 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8297
8298 if (pdi->main_subprogram && actual_name != NULL)
8299 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8300 break;
72929c62 8301 case DW_TAG_constant:
76e288d1
TT
8302 psymbol.domain = VAR_DOMAIN;
8303 psymbol.aclass = LOC_STATIC;
8304 where = (pdi->is_external
8305 ? psymbol_placement::GLOBAL
8306 : psymbol_placement::STATIC);
72929c62 8307 break;
c906108c 8308 case DW_TAG_variable:
95554aad
TT
8309 if (pdi->d.locdesc)
8310 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8311
95554aad 8312 if (pdi->d.locdesc
caac4577
JG
8313 && addr == 0
8314 && !dwarf2_per_objfile->has_section_at_zero)
8315 {
8316 /* A global or static variable may also have been stripped
8317 out by the linker if unused, in which case its address
8318 will be nullified; do not add such variables into partial
8319 symbol table then. */
8320 }
8321 else if (pdi->is_external)
c906108c
SS
8322 {
8323 /* Global Variable.
8324 Don't enter into the minimal symbol tables as there is
8325 a minimal symbol table entry from the ELF symbols already.
8326 Enter into partial symbol table if it has a location
8327 descriptor or a type.
8328 If the location descriptor is missing, new_symbol will create
8329 a LOC_UNRESOLVED symbol, the address of the variable will then
8330 be determined from the minimal symbol table whenever the variable
8331 is referenced.
8332 The address for the partial symbol table entry is not
8333 used by GDB, but it comes in handy for debugging partial symbol
8334 table building. */
8335
95554aad 8336 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8337 {
8338 psymbol.domain = VAR_DOMAIN;
8339 psymbol.aclass = LOC_STATIC;
8340 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8341 psymbol.ginfo.value.address = addr;
8342 where = psymbol_placement::GLOBAL;
8343 }
c906108c
SS
8344 }
8345 else
8346 {
ff908ebf
AW
8347 int has_loc = pdi->d.locdesc != NULL;
8348
8349 /* Static Variable. Skip symbols whose value we cannot know (those
8350 without location descriptors or constant values). */
8351 if (!has_loc && !pdi->has_const_value)
43816ebc 8352 return;
ff908ebf 8353
76e288d1
TT
8354 psymbol.domain = VAR_DOMAIN;
8355 psymbol.aclass = LOC_STATIC;
8356 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8357 if (has_loc)
8358 psymbol.ginfo.value.address = addr;
8359 where = psymbol_placement::STATIC;
c906108c
SS
8360 }
8361 break;
8362 case DW_TAG_typedef:
8363 case DW_TAG_base_type:
a02abb62 8364 case DW_TAG_subrange_type:
76e288d1
TT
8365 psymbol.domain = VAR_DOMAIN;
8366 psymbol.aclass = LOC_TYPEDEF;
8367 where = psymbol_placement::STATIC;
c906108c 8368 break;
74921315 8369 case DW_TAG_imported_declaration:
72bf9492 8370 case DW_TAG_namespace:
76e288d1
TT
8371 psymbol.domain = VAR_DOMAIN;
8372 psymbol.aclass = LOC_TYPEDEF;
8373 where = psymbol_placement::GLOBAL;
72bf9492 8374 break;
530e8392 8375 case DW_TAG_module:
a5fd13a9
BH
8376 /* With Fortran 77 there might be a "BLOCK DATA" module
8377 available without any name. If so, we skip the module as it
8378 doesn't bring any value. */
8379 if (actual_name != nullptr)
76e288d1
TT
8380 {
8381 psymbol.domain = MODULE_DOMAIN;
8382 psymbol.aclass = LOC_TYPEDEF;
8383 where = psymbol_placement::GLOBAL;
8384 }
530e8392 8385 break;
c906108c 8386 case DW_TAG_class_type:
680b30c7 8387 case DW_TAG_interface_type:
c906108c
SS
8388 case DW_TAG_structure_type:
8389 case DW_TAG_union_type:
8390 case DW_TAG_enumeration_type:
fa4028e9
JB
8391 /* Skip external references. The DWARF standard says in the section
8392 about "Structure, Union, and Class Type Entries": "An incomplete
8393 structure, union or class type is represented by a structure,
8394 union or class entry that does not have a byte size attribute
8395 and that has a DW_AT_declaration attribute." */
8396 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8397 return;
fa4028e9 8398
63d06c5c
DC
8399 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8400 static vs. global. */
76e288d1
TT
8401 psymbol.domain = STRUCT_DOMAIN;
8402 psymbol.aclass = LOC_TYPEDEF;
8403 where = (cu->language == language_cplus
8404 ? psymbol_placement::GLOBAL
8405 : psymbol_placement::STATIC);
c906108c
SS
8406 break;
8407 case DW_TAG_enumerator:
76e288d1
TT
8408 psymbol.domain = VAR_DOMAIN;
8409 psymbol.aclass = LOC_CONST;
8410 where = (cu->language == language_cplus
8411 ? psymbol_placement::GLOBAL
8412 : psymbol_placement::STATIC);
c906108c
SS
8413 break;
8414 default:
8415 break;
8416 }
76e288d1
TT
8417
8418 if (where.has_value ())
8419 {
f049a313
TT
8420 if (built_actual_name != nullptr)
8421 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8422 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8423 psymbol.ginfo.set_linkage_name (actual_name);
8424 else
8425 {
8426 psymbol.ginfo.set_demangled_name (actual_name,
8427 &objfile->objfile_obstack);
8428 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8429 }
76e288d1
TT
8430 add_psymbol_to_list (psymbol, *where, objfile);
8431 }
c906108c
SS
8432}
8433
5c4e30ca
DC
8434/* Read a partial die corresponding to a namespace; also, add a symbol
8435 corresponding to that namespace to the symbol table. NAMESPACE is
8436 the name of the enclosing namespace. */
91c24f0a 8437
72bf9492
DJ
8438static void
8439add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8440 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8441 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8442{
72bf9492 8443 /* Add a symbol for the namespace. */
e7c27a73 8444
72bf9492 8445 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8446
8447 /* Now scan partial symbols in that namespace. */
8448
91c24f0a 8449 if (pdi->has_children)
cdc07690 8450 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8451}
8452
5d7cb8df
JK
8453/* Read a partial die corresponding to a Fortran module. */
8454
8455static void
8456add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8457 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8458{
530e8392
KB
8459 /* Add a symbol for the namespace. */
8460
8461 add_partial_symbol (pdi, cu);
8462
f55ee35c 8463 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8464
8465 if (pdi->has_children)
cdc07690 8466 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8467}
8468
b1dc1806
XR
8469/* Read a partial die corresponding to a subprogram or an inlined
8470 subprogram and create a partial symbol for that subprogram.
8471 When the CU language allows it, this routine also defines a partial
8472 symbol for each nested subprogram that this subprogram contains.
8473 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8474 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8475
cdc07690
YQ
8476 PDI may also be a lexical block, in which case we simply search
8477 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8478 Again, this is only performed when the CU language allows this
8479 type of definitions. */
8480
8481static void
8482add_partial_subprogram (struct partial_die_info *pdi,
8483 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8484 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8485{
b1dc1806 8486 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8487 {
8488 if (pdi->has_pc_info)
8489 {
8490 if (pdi->lowpc < *lowpc)
8491 *lowpc = pdi->lowpc;
8492 if (pdi->highpc > *highpc)
8493 *highpc = pdi->highpc;
cdc07690 8494 if (set_addrmap)
5734ee8b 8495 {
518817b3 8496 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 8497 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8498 CORE_ADDR baseaddr;
b926417a
TT
8499 CORE_ADDR this_highpc;
8500 CORE_ADDR this_lowpc;
5734ee8b 8501
b3b3bada 8502 baseaddr = objfile->text_section_offset ();
b926417a
TT
8503 this_lowpc
8504 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8505 pdi->lowpc + baseaddr)
8506 - baseaddr);
8507 this_highpc
8508 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8509 pdi->highpc + baseaddr)
8510 - baseaddr);
d320c2b5 8511 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8512 this_lowpc, this_highpc - 1,
9291a0cd 8513 cu->per_cu->v.psymtab);
5734ee8b 8514 }
481860b3
GB
8515 }
8516
8517 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8518 {
bc30ff58 8519 if (!pdi->is_declaration)
e8d05480
JB
8520 /* Ignore subprogram DIEs that do not have a name, they are
8521 illegal. Do not emit a complaint at this point, we will
8522 do so when we convert this psymtab into a symtab. */
8523 if (pdi->name)
8524 add_partial_symbol (pdi, cu);
bc30ff58
JB
8525 }
8526 }
6e70227d 8527
bc30ff58
JB
8528 if (! pdi->has_children)
8529 return;
8530
0a4b0913 8531 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8532 {
8533 pdi = pdi->die_child;
8534 while (pdi != NULL)
8535 {
52356b79 8536 pdi->fixup (cu);
bc30ff58 8537 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8538 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8539 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8540 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8541 pdi = pdi->die_sibling;
8542 }
8543 }
8544}
8545
91c24f0a
DC
8546/* Read a partial die corresponding to an enumeration type. */
8547
72bf9492
DJ
8548static void
8549add_partial_enumeration (struct partial_die_info *enum_pdi,
8550 struct dwarf2_cu *cu)
91c24f0a 8551{
72bf9492 8552 struct partial_die_info *pdi;
91c24f0a
DC
8553
8554 if (enum_pdi->name != NULL)
72bf9492
DJ
8555 add_partial_symbol (enum_pdi, cu);
8556
8557 pdi = enum_pdi->die_child;
8558 while (pdi)
91c24f0a 8559 {
72bf9492 8560 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8561 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8562 else
72bf9492
DJ
8563 add_partial_symbol (pdi, cu);
8564 pdi = pdi->die_sibling;
91c24f0a 8565 }
91c24f0a
DC
8566}
8567
6caca83c
CC
8568/* Return the initial uleb128 in the die at INFO_PTR. */
8569
8570static unsigned int
d521ce57 8571peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8572{
8573 unsigned int bytes_read;
8574
8575 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8576}
8577
685af9cd
TT
8578/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8579 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8580
4bb7a0a7
DJ
8581 Return the corresponding abbrev, or NULL if the number is zero (indicating
8582 an empty DIE). In either case *BYTES_READ will be set to the length of
8583 the initial number. */
8584
8585static struct abbrev_info *
685af9cd
TT
8586peek_die_abbrev (const die_reader_specs &reader,
8587 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8588{
685af9cd 8589 dwarf2_cu *cu = reader.cu;
518817b3 8590 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8591 unsigned int abbrev_number
8592 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8593
8594 if (abbrev_number == 0)
8595 return NULL;
8596
685af9cd 8597 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8598 if (!abbrev)
8599 {
422b9917 8600 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8601 " at offset %s [in module %s]"),
422b9917 8602 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8603 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8604 }
8605
8606 return abbrev;
8607}
8608
93311388
DE
8609/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8610 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8611 DIE. Any children of the skipped DIEs will also be skipped. */
8612
d521ce57
TT
8613static const gdb_byte *
8614skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8615{
4bb7a0a7
DJ
8616 while (1)
8617 {
685af9cd
TT
8618 unsigned int bytes_read;
8619 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8620
4bb7a0a7
DJ
8621 if (abbrev == NULL)
8622 return info_ptr + bytes_read;
8623 else
dee91e82 8624 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8625 }
8626}
8627
93311388
DE
8628/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8629 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8630 abbrev corresponding to that skipped uleb128 should be passed in
8631 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8632 children. */
8633
d521ce57
TT
8634static const gdb_byte *
8635skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8636 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8637{
8638 unsigned int bytes_read;
8639 struct attribute attr;
dee91e82
DE
8640 bfd *abfd = reader->abfd;
8641 struct dwarf2_cu *cu = reader->cu;
d521ce57 8642 const gdb_byte *buffer = reader->buffer;
f664829e 8643 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8644 unsigned int form, i;
8645
8646 for (i = 0; i < abbrev->num_attrs; i++)
8647 {
8648 /* The only abbrev we care about is DW_AT_sibling. */
8649 if (abbrev->attrs[i].name == DW_AT_sibling)
8650 {
18a8505e
AT
8651 bool ignored;
8652 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8653 &ignored);
4bb7a0a7 8654 if (attr.form == DW_FORM_ref_addr)
b98664d3 8655 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8656 else
b9502d3f 8657 {
0826b30a 8658 sect_offset off = attr.get_ref_die_offset ();
9c541725 8659 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8660
8661 if (sibling_ptr < info_ptr)
b98664d3 8662 complaint (_("DW_AT_sibling points backwards"));
22869d73 8663 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8664 reader->die_section->overflow_complaint ();
b9502d3f
WN
8665 else
8666 return sibling_ptr;
8667 }
4bb7a0a7
DJ
8668 }
8669
8670 /* If it isn't DW_AT_sibling, skip this attribute. */
8671 form = abbrev->attrs[i].form;
8672 skip_attribute:
8673 switch (form)
8674 {
4bb7a0a7 8675 case DW_FORM_ref_addr:
ae411497
TT
8676 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8677 and later it is offset sized. */
8678 if (cu->header.version == 2)
8679 info_ptr += cu->header.addr_size;
8680 else
8681 info_ptr += cu->header.offset_size;
8682 break;
36586728
TT
8683 case DW_FORM_GNU_ref_alt:
8684 info_ptr += cu->header.offset_size;
8685 break;
ae411497 8686 case DW_FORM_addr:
4bb7a0a7
DJ
8687 info_ptr += cu->header.addr_size;
8688 break;
8689 case DW_FORM_data1:
8690 case DW_FORM_ref1:
8691 case DW_FORM_flag:
8fe0f950 8692 case DW_FORM_strx1:
4bb7a0a7
DJ
8693 info_ptr += 1;
8694 break;
2dc7f7b3 8695 case DW_FORM_flag_present:
43988095 8696 case DW_FORM_implicit_const:
2dc7f7b3 8697 break;
4bb7a0a7
DJ
8698 case DW_FORM_data2:
8699 case DW_FORM_ref2:
8fe0f950 8700 case DW_FORM_strx2:
4bb7a0a7
DJ
8701 info_ptr += 2;
8702 break;
8fe0f950
AT
8703 case DW_FORM_strx3:
8704 info_ptr += 3;
8705 break;
4bb7a0a7
DJ
8706 case DW_FORM_data4:
8707 case DW_FORM_ref4:
8fe0f950 8708 case DW_FORM_strx4:
4bb7a0a7
DJ
8709 info_ptr += 4;
8710 break;
8711 case DW_FORM_data8:
8712 case DW_FORM_ref8:
55f1336d 8713 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8714 info_ptr += 8;
8715 break;
0224619f
JK
8716 case DW_FORM_data16:
8717 info_ptr += 16;
8718 break;
4bb7a0a7 8719 case DW_FORM_string:
9b1c24c8 8720 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8721 info_ptr += bytes_read;
8722 break;
2dc7f7b3 8723 case DW_FORM_sec_offset:
4bb7a0a7 8724 case DW_FORM_strp:
36586728 8725 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8726 info_ptr += cu->header.offset_size;
8727 break;
2dc7f7b3 8728 case DW_FORM_exprloc:
4bb7a0a7
DJ
8729 case DW_FORM_block:
8730 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8731 info_ptr += bytes_read;
8732 break;
8733 case DW_FORM_block1:
8734 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8735 break;
8736 case DW_FORM_block2:
8737 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8738 break;
8739 case DW_FORM_block4:
8740 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8741 break;
336d760d 8742 case DW_FORM_addrx:
cf532bd1 8743 case DW_FORM_strx:
4bb7a0a7
DJ
8744 case DW_FORM_sdata:
8745 case DW_FORM_udata:
8746 case DW_FORM_ref_udata:
3019eac3
DE
8747 case DW_FORM_GNU_addr_index:
8748 case DW_FORM_GNU_str_index:
18a8505e 8749 case DW_FORM_rnglistx:
41144253 8750 case DW_FORM_loclistx:
d521ce57 8751 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8752 break;
8753 case DW_FORM_indirect:
8754 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8755 info_ptr += bytes_read;
8756 /* We need to continue parsing from here, so just go back to
8757 the top. */
8758 goto skip_attribute;
8759
8760 default:
3e43a32a
MS
8761 error (_("Dwarf Error: Cannot handle %s "
8762 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8763 dwarf_form_name (form),
8764 bfd_get_filename (abfd));
8765 }
8766 }
8767
8768 if (abbrev->has_children)
dee91e82 8769 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8770 else
8771 return info_ptr;
8772}
8773
93311388 8774/* Locate ORIG_PDI's sibling.
dee91e82 8775 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8776
d521ce57 8777static const gdb_byte *
dee91e82
DE
8778locate_pdi_sibling (const struct die_reader_specs *reader,
8779 struct partial_die_info *orig_pdi,
d521ce57 8780 const gdb_byte *info_ptr)
91c24f0a
DC
8781{
8782 /* Do we know the sibling already? */
72bf9492 8783
91c24f0a
DC
8784 if (orig_pdi->sibling)
8785 return orig_pdi->sibling;
8786
8787 /* Are there any children to deal with? */
8788
8789 if (!orig_pdi->has_children)
8790 return info_ptr;
8791
4bb7a0a7 8792 /* Skip the children the long way. */
91c24f0a 8793
dee91e82 8794 return skip_children (reader, info_ptr);
91c24f0a
DC
8795}
8796
257e7a09 8797/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8798 not NULL. */
c906108c 8799
891813be
TT
8800void
8801dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8802{
ed2dc618
SM
8803 struct dwarf2_per_objfile *dwarf2_per_objfile
8804 = get_dwarf2_per_objfile (objfile);
8805
077cbab2
TT
8806 gdb_assert (!readin);
8807 /* If this psymtab is constructed from a debug-only objfile, the
8808 has_section_at_zero flag will not necessarily be correct. We
8809 can get the correct value for this flag by looking at the data
8810 associated with the (presumably stripped) associated objfile. */
8811 if (objfile->separate_debug_objfile_backlink)
c906108c 8812 {
077cbab2
TT
8813 struct dwarf2_per_objfile *dpo_backlink
8814 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8815
077cbab2
TT
8816 dwarf2_per_objfile->has_section_at_zero
8817 = dpo_backlink->has_section_at_zero;
8818 }
98bfdba5 8819
8566b89b 8820 expand_psymtab (objfile);
95554aad 8821
ed2dc618 8822 process_cu_includes (dwarf2_per_objfile);
c906108c 8823}
9cdd5dbd
DE
8824\f
8825/* Reading in full CUs. */
c906108c 8826
10b3939b
DJ
8827/* Add PER_CU to the queue. */
8828
8829static void
95554aad
TT
8830queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8831 enum language pretend_language)
10b3939b 8832{
10b3939b 8833 per_cu->queued = 1;
39856def 8834 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8835}
8836
89e63ee4
DE
8837/* If PER_CU is not yet queued, add it to the queue.
8838 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8839 dependency.
0907af0c 8840 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8841 meaning either PER_CU is already queued or it is already loaded.
8842
8843 N.B. There is an invariant here that if a CU is queued then it is loaded.
8844 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8845
8846static int
89e63ee4 8847maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8848 struct dwarf2_per_cu_data *per_cu,
8849 enum language pretend_language)
8850{
8851 /* We may arrive here during partial symbol reading, if we need full
8852 DIEs to process an unusual case (e.g. template arguments). Do
8853 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8854 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8855 {
8856 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8857 return 1;
8858 return 0;
8859 }
8860
8861 /* Mark the dependence relation so that we don't flush PER_CU
8862 too early. */
89e63ee4
DE
8863 if (dependent_cu != NULL)
8864 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8865
8866 /* If it's already on the queue, we have nothing to do. */
8867 if (per_cu->queued)
8868 return 0;
8869
8870 /* If the compilation unit is already loaded, just mark it as
8871 used. */
8872 if (per_cu->cu != NULL)
8873 {
8874 per_cu->cu->last_used = 0;
8875 return 0;
8876 }
8877
8878 /* Add it to the queue. */
8879 queue_comp_unit (per_cu, pretend_language);
8880
8881 return 1;
8882}
8883
10b3939b
DJ
8884/* Process the queue. */
8885
8886static void
ed2dc618 8887process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8888{
b4f54984 8889 if (dwarf_read_debug)
45cfd468
DE
8890 {
8891 fprintf_unfiltered (gdb_stdlog,
8892 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8893 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8894 }
8895
03dd20cc
DJ
8896 /* The queue starts out with one item, but following a DIE reference
8897 may load a new CU, adding it to the end of the queue. */
39856def 8898 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8899 {
39856def
TT
8900 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8901
cc12ce38 8902 if ((dwarf2_per_objfile->using_index
39856def
TT
8903 ? !item.per_cu->v.quick->compunit_symtab
8904 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8905 /* Skip dummy CUs. */
39856def 8906 && item.per_cu->cu != NULL)
f4dc4d17 8907 {
39856def 8908 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8909 unsigned int debug_print_threshold;
247f5c4f 8910 char buf[100];
f4dc4d17 8911
247f5c4f 8912 if (per_cu->is_debug_types)
f4dc4d17 8913 {
247f5c4f
DE
8914 struct signatured_type *sig_type =
8915 (struct signatured_type *) per_cu;
8916
9d8780f0 8917 sprintf (buf, "TU %s at offset %s",
73be47f5 8918 hex_string (sig_type->signature),
9d8780f0 8919 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8920 /* There can be 100s of TUs.
8921 Only print them in verbose mode. */
8922 debug_print_threshold = 2;
f4dc4d17 8923 }
247f5c4f 8924 else
73be47f5 8925 {
9d8780f0
SM
8926 sprintf (buf, "CU at offset %s",
8927 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8928 debug_print_threshold = 1;
8929 }
247f5c4f 8930
b4f54984 8931 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8932 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8933
8934 if (per_cu->is_debug_types)
39856def 8935 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8936 else
39856def 8937 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8938
b4f54984 8939 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8940 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8941 }
10b3939b 8942
39856def
TT
8943 item.per_cu->queued = 0;
8944 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8945 }
8946
b4f54984 8947 if (dwarf_read_debug)
45cfd468
DE
8948 {
8949 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8950 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8951 }
10b3939b
DJ
8952}
8953
10b3939b
DJ
8954/* Read in full symbols for PST, and anything it depends on. */
8955
8566b89b
TT
8956void
8957dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8958{
194d088f 8959 gdb_assert (!readin);
95554aad 8960
48993951 8961 expand_dependencies (objfile);
aaa75496 8962
b83470bf
TT
8963 dw2_do_instantiate_symtab (per_cu_data, false);
8964 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8965}
8966
dee91e82
DE
8967/* Trivial hash function for die_info: the hash value of a DIE
8968 is its offset in .debug_info for this objfile. */
10b3939b 8969
dee91e82
DE
8970static hashval_t
8971die_hash (const void *item)
10b3939b 8972{
9a3c8263 8973 const struct die_info *die = (const struct die_info *) item;
6502dd73 8974
9c541725 8975 return to_underlying (die->sect_off);
dee91e82 8976}
63d06c5c 8977
dee91e82
DE
8978/* Trivial comparison function for die_info structures: two DIEs
8979 are equal if they have the same offset. */
98bfdba5 8980
dee91e82
DE
8981static int
8982die_eq (const void *item_lhs, const void *item_rhs)
8983{
9a3c8263
SM
8984 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8985 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8986
9c541725 8987 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8988}
c906108c 8989
c0ab21c2 8990/* Load the DIEs associated with PER_CU into memory. */
c906108c 8991
dee91e82 8992static void
c0ab21c2
TT
8993load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8994 bool skip_partial,
8995 enum language pretend_language)
dee91e82 8996{
c0ab21c2
TT
8997 gdb_assert (! this_cu->is_debug_types);
8998
6751ebae 8999 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
9000 if (reader.dummy_p)
9001 return;
9002
9003 struct dwarf2_cu *cu = reader.cu;
9004 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9005
dee91e82
DE
9006 gdb_assert (cu->die_hash == NULL);
9007 cu->die_hash =
9008 htab_create_alloc_ex (cu->header.length / 12,
9009 die_hash,
9010 die_eq,
9011 NULL,
9012 &cu->comp_unit_obstack,
9013 hashtab_obstack_allocate,
9014 dummy_obstack_deallocate);
e142c38c 9015
3e225074 9016 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9017 reader.comp_unit_die->child
9018 = read_die_and_siblings (&reader, reader.info_ptr,
9019 &info_ptr, reader.comp_unit_die);
9020 cu->dies = reader.comp_unit_die;
dee91e82 9021 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9022
9023 /* We try not to read any attributes in this function, because not
9cdd5dbd 9024 all CUs needed for references have been loaded yet, and symbol
10b3939b 9025 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9026 or we won't be able to build types correctly.
9027 Similarly, if we do not read the producer, we can not apply
9028 producer-specific interpretation. */
c0ab21c2 9029 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9030
9031 reader.keep ();
10b3939b
DJ
9032}
9033
3da10d80
KS
9034/* Add a DIE to the delayed physname list. */
9035
9036static void
9037add_to_method_list (struct type *type, int fnfield_index, int index,
9038 const char *name, struct die_info *die,
9039 struct dwarf2_cu *cu)
9040{
9041 struct delayed_method_info mi;
9042 mi.type = type;
9043 mi.fnfield_index = fnfield_index;
9044 mi.index = index;
9045 mi.name = name;
9046 mi.die = die;
c89b44cd 9047 cu->method_list.push_back (mi);
3da10d80
KS
9048}
9049
3693fdb3
PA
9050/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9051 "const" / "volatile". If so, decrements LEN by the length of the
9052 modifier and return true. Otherwise return false. */
9053
9054template<size_t N>
9055static bool
9056check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9057{
9058 size_t mod_len = sizeof (mod) - 1;
9059 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9060 {
9061 len -= mod_len;
9062 return true;
9063 }
9064 return false;
9065}
9066
3da10d80
KS
9067/* Compute the physnames of any methods on the CU's method list.
9068
9069 The computation of method physnames is delayed in order to avoid the
9070 (bad) condition that one of the method's formal parameters is of an as yet
9071 incomplete type. */
9072
9073static void
9074compute_delayed_physnames (struct dwarf2_cu *cu)
9075{
3693fdb3 9076 /* Only C++ delays computing physnames. */
c89b44cd 9077 if (cu->method_list.empty ())
3693fdb3
PA
9078 return;
9079 gdb_assert (cu->language == language_cplus);
9080
52941706 9081 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9082 {
1d06ead6 9083 const char *physname;
3da10d80 9084 struct fn_fieldlist *fn_flp
c89b44cd
TT
9085 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9086 physname = dwarf2_physname (mi.name, mi.die, cu);
9087 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9088 = physname ? physname : "";
3693fdb3
PA
9089
9090 /* Since there's no tag to indicate whether a method is a
9091 const/volatile overload, extract that information out of the
9092 demangled name. */
9093 if (physname != NULL)
9094 {
9095 size_t len = strlen (physname);
9096
9097 while (1)
9098 {
9099 if (physname[len] == ')') /* shortcut */
9100 break;
9101 else if (check_modifier (physname, len, " const"))
c89b44cd 9102 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9103 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9104 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9105 else
9106 break;
9107 }
9108 }
3da10d80 9109 }
c89b44cd
TT
9110
9111 /* The list is no longer needed. */
9112 cu->method_list.clear ();
3da10d80
KS
9113}
9114
a766d390
DE
9115/* Go objects should be embedded in a DW_TAG_module DIE,
9116 and it's not clear if/how imported objects will appear.
9117 To keep Go support simple until that's worked out,
9118 go back through what we've read and create something usable.
9119 We could do this while processing each DIE, and feels kinda cleaner,
9120 but that way is more invasive.
9121 This is to, for example, allow the user to type "p var" or "b main"
9122 without having to specify the package name, and allow lookups
9123 of module.object to work in contexts that use the expression
9124 parser. */
9125
9126static void
9127fixup_go_packaging (struct dwarf2_cu *cu)
9128{
421d1616 9129 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9130 struct pending *list;
9131 int i;
9132
c24bdb02 9133 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9134 list != NULL;
9135 list = list->next)
a766d390
DE
9136 {
9137 for (i = 0; i < list->nsyms; ++i)
9138 {
9139 struct symbol *sym = list->symbol[i];
9140
c1b5c1eb 9141 if (sym->language () == language_go
a766d390
DE
9142 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9143 {
421d1616
TT
9144 gdb::unique_xmalloc_ptr<char> this_package_name
9145 (go_symbol_package_name (sym));
a766d390
DE
9146
9147 if (this_package_name == NULL)
9148 continue;
9149 if (package_name == NULL)
421d1616 9150 package_name = std::move (this_package_name);
a766d390
DE
9151 else
9152 {
518817b3
SM
9153 struct objfile *objfile
9154 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9155 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9156 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9157 (symbol_symtab (sym) != NULL
9158 ? symtab_to_filename_for_display
9159 (symbol_symtab (sym))
e3b94546 9160 : objfile_name (objfile)),
421d1616 9161 this_package_name.get (), package_name.get ());
a766d390
DE
9162 }
9163 }
9164 }
9165 }
9166
9167 if (package_name != NULL)
9168 {
518817b3 9169 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9170 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9171 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9172 saved_package_name);
a766d390
DE
9173 struct symbol *sym;
9174
8c14c3a3 9175 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9176 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9177 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9178 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9179 e.g., "main" finds the "main" module and not C's main(). */
9180 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9181 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9182 SYMBOL_TYPE (sym) = type;
9183
c24bdb02 9184 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9185 }
9186}
9187
c9317f21
TT
9188/* Allocate a fully-qualified name consisting of the two parts on the
9189 obstack. */
9190
9191static const char *
9192rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9193{
9194 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9195}
9196
9c6a1327
TT
9197/* A helper that allocates a variant part to attach to a Rust enum
9198 type. OBSTACK is where the results should be allocated. TYPE is
9199 the type we're processing. DISCRIMINANT_INDEX is the index of the
9200 discriminant. It must be the index of one of the fields of TYPE.
9201 DEFAULT_INDEX is the index of the default field; or -1 if there is
9202 no default. RANGES is indexed by "effective" field number (the
9203 field index, but omitting the discriminant and default fields) and
9204 must hold the discriminant values used by the variants. Note that
9205 RANGES must have a lifetime at least as long as OBSTACK -- either
9206 already allocated on it, or static. */
c9317f21 9207
9c6a1327
TT
9208static void
9209alloc_rust_variant (struct obstack *obstack, struct type *type,
9210 int discriminant_index, int default_index,
9211 gdb::array_view<discriminant_range> ranges)
9212{
9213 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9214 must be handled by the caller. */
9215 gdb_assert (discriminant_index >= 0
9216 && discriminant_index < TYPE_NFIELDS (type));
c9317f21 9217 gdb_assert (default_index == -1
c7b15a66 9218 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21 9219
9c6a1327
TT
9220 /* We have one variant for each non-discriminant field. */
9221 int n_variants = TYPE_NFIELDS (type) - 1;
c9317f21 9222
9c6a1327
TT
9223 variant *variants = new (obstack) variant[n_variants];
9224 int var_idx = 0;
9225 int range_idx = 0;
9226 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9227 {
9228 if (i == discriminant_index)
9229 continue;
c9317f21 9230
9c6a1327
TT
9231 variants[var_idx].first_field = i;
9232 variants[var_idx].last_field = i + 1;
9233
9234 /* The default field does not need a range, but other fields do.
9235 We skipped the discriminant above. */
9236 if (i != default_index)
9237 {
9238 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9239 ++range_idx;
9240 }
c9317f21 9241
9c6a1327
TT
9242 ++var_idx;
9243 }
9244
9245 gdb_assert (range_idx == ranges.size ());
9246 gdb_assert (var_idx == n_variants);
9247
9248 variant_part *part = new (obstack) variant_part;
9249 part->discriminant_index = discriminant_index;
9250 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9251 discriminant_index));
9252 part->variants = gdb::array_view<variant> (variants, n_variants);
9253
9254 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9255 gdb::array_view<variant_part> *prop_value
9256 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9257
9c6a1327
TT
9258 struct dynamic_prop prop;
9259 prop.kind = PROP_VARIANT_PARTS;
9260 prop.data.variant_parts = prop_value;
9261
5c54719c 9262 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9263}
9264
9265/* Some versions of rustc emitted enums in an unusual way.
9266
9267 Ordinary enums were emitted as unions. The first element of each
9268 structure in the union was named "RUST$ENUM$DISR". This element
9269 held the discriminant.
9270
9271 These versions of Rust also implemented the "non-zero"
9272 optimization. When the enum had two values, and one is empty and
9273 the other holds a pointer that cannot be zero, the pointer is used
9274 as the discriminant, with a zero value meaning the empty variant.
9275 Here, the union's first member is of the form
9276 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9277 where the fieldnos are the indices of the fields that should be
9278 traversed in order to find the field (which may be several fields deep)
9279 and the variantname is the name of the variant of the case when the
9280 field is zero.
9281
9282 This function recognizes whether TYPE is of one of these forms,
9283 and, if so, smashes it to be a variant type. */
9284
9285static void
9286quirk_rust_enum (struct type *type, struct objfile *objfile)
9287{
78134374 9288 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9289
9290 /* We don't need to deal with empty enums. */
9291 if (TYPE_NFIELDS (type) == 0)
9292 return;
9293
9294#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9295 if (TYPE_NFIELDS (type) == 1
9296 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9297 {
9298 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9299
9300 /* Decode the field name to find the offset of the
9301 discriminant. */
9302 ULONGEST bit_offset = 0;
9303 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9304 while (name[0] >= '0' && name[0] <= '9')
9305 {
9306 char *tail;
9307 unsigned long index = strtoul (name, &tail, 10);
9308 name = tail;
9309 if (*name != '$'
9310 || index >= TYPE_NFIELDS (field_type)
9311 || (TYPE_FIELD_LOC_KIND (field_type, index)
9312 != FIELD_LOC_KIND_BITPOS))
9313 {
b98664d3 9314 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9315 "[in module %s]"),
9316 TYPE_FIELD_NAME (type, 0),
9317 objfile_name (objfile));
9318 return;
9319 }
9320 ++name;
9321
9322 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9323 field_type = TYPE_FIELD_TYPE (field_type, index);
9324 }
9325
9c6a1327
TT
9326 /* Smash this type to be a structure type. We have to do this
9327 because the type has already been recorded. */
67607e24 9328 type->set_code (TYPE_CODE_STRUCT);
9c6a1327
TT
9329 TYPE_NFIELDS (type) = 3;
9330 /* Save the field we care about. */
9331 struct field saved_field = TYPE_FIELD (type, 0);
9332 TYPE_FIELDS (type)
c9317f21 9333 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
c9317f21 9334
9c6a1327
TT
9335 /* Put the discriminant at index 0. */
9336 TYPE_FIELD_TYPE (type, 0) = field_type;
9337 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9338 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9339 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), bit_offset);
c9317f21
TT
9340
9341 /* The order of fields doesn't really matter, so put the real
9342 field at index 1 and the data-less field at index 2. */
9c6a1327
TT
9343 TYPE_FIELD (type, 1) = saved_field;
9344 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9345 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9346 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9347 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9348 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9349
9350 const char *dataless_name
7d93a1e0 9351 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9352 name);
9353 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9354 dataless_name);
9c6a1327 9355 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9356 /* NAME points into the original discriminant name, which
9357 already has the correct lifetime. */
9c6a1327
TT
9358 TYPE_FIELD_NAME (type, 2) = name;
9359 SET_FIELD_BITPOS (TYPE_FIELD (type, 2), 0);
c9317f21 9360
9c6a1327
TT
9361 /* Indicate that this is a variant type. */
9362 static discriminant_range ranges[1] = { { 0, 0 } };
9363 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9364 }
77c2dba3
TT
9365 /* A union with a single anonymous field is probably an old-style
9366 univariant enum. */
9367 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9368 {
c9317f21
TT
9369 /* Smash this type to be a structure type. We have to do this
9370 because the type has already been recorded. */
67607e24 9371 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9372
9c6a1327 9373 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9374 const char *variant_name
7d93a1e0 9375 = rust_last_path_segment (field_type->name ());
9c6a1327 9376 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9377 field_type->set_name
9378 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9379 type->name (), variant_name));
c9317f21
TT
9380 }
9381 else
9382 {
9383 struct type *disr_type = nullptr;
9384 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9385 {
9386 disr_type = TYPE_FIELD_TYPE (type, i);
9387
78134374 9388 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9389 {
9390 /* All fields of a true enum will be structs. */
9391 return;
9392 }
9393 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9394 {
9395 /* Could be data-less variant, so keep going. */
a037790e 9396 disr_type = nullptr;
c9317f21
TT
9397 }
9398 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9399 "RUST$ENUM$DISR") != 0)
9400 {
9401 /* Not a Rust enum. */
9402 return;
9403 }
9404 else
9405 {
9406 /* Found one. */
9407 break;
9408 }
9409 }
9410
9411 /* If we got here without a discriminant, then it's probably
9412 just a union. */
9413 if (disr_type == nullptr)
9414 return;
9415
9416 /* Smash this type to be a structure type. We have to do this
9417 because the type has already been recorded. */
67607e24 9418 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9419
9c6a1327 9420 /* Make space for the discriminant field. */
c9317f21 9421 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9c6a1327
TT
9422 field *new_fields
9423 = (struct field *) TYPE_ZALLOC (type, (TYPE_NFIELDS (type)
9424 * sizeof (struct field)));
9425 memcpy (new_fields + 1, TYPE_FIELDS (type),
c9317f21 9426 TYPE_NFIELDS (type) * sizeof (struct field));
9c6a1327
TT
9427 TYPE_FIELDS (type) = new_fields;
9428 TYPE_NFIELDS (type) = TYPE_NFIELDS (type) + 1;
c9317f21
TT
9429
9430 /* Install the discriminant at index 0 in the union. */
9c6a1327
TT
9431 TYPE_FIELD (type, 0) = *disr_field;
9432 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9433 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9434
9435 /* We need a way to find the correct discriminant given a
9436 variant name. For convenience we build a map here. */
9437 struct type *enum_type = FIELD_TYPE (*disr_field);
9438 std::unordered_map<std::string, ULONGEST> discriminant_map;
9439 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9440 {
9441 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9442 {
9443 const char *name
9444 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9445 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9446 }
9447 }
9448
9c6a1327
TT
9449 int n_fields = TYPE_NFIELDS (type);
9450 /* We don't need a range entry for the discriminant, but we do
9451 need one for every other field, as there is no default
9452 variant. */
9453 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9454 discriminant_range,
9455 n_fields - 1);
c9317f21
TT
9456 /* Skip the discriminant here. */
9457 for (int i = 1; i < n_fields; ++i)
9458 {
9459 /* Find the final word in the name of this variant's type.
9460 That name can be used to look up the correct
9461 discriminant. */
9462 const char *variant_name
7d93a1e0 9463 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9464
9465 auto iter = discriminant_map.find (variant_name);
9466 if (iter != discriminant_map.end ())
9c6a1327
TT
9467 {
9468 ranges[i].low = iter->second;
9469 ranges[i].high = iter->second;
9470 }
c9317f21 9471
bedda9ac 9472 /* Remove the discriminant field, if it exists. */
9c6a1327 9473 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
bedda9ac
TT
9474 if (TYPE_NFIELDS (sub_type) > 0)
9475 {
9476 --TYPE_NFIELDS (sub_type);
9477 ++TYPE_FIELDS (sub_type);
9478 }
9c6a1327 9479 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9480 sub_type->set_name
9481 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9482 type->name (), variant_name));
c9317f21 9483 }
9c6a1327
TT
9484
9485 /* Indicate that this is a variant type. */
9486 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9487 gdb::array_view<discriminant_range> (ranges,
9488 n_fields - 1));
c9317f21
TT
9489 }
9490}
9491
9492/* Rewrite some Rust unions to be structures with variants parts. */
9493
9494static void
9495rust_union_quirks (struct dwarf2_cu *cu)
9496{
9497 gdb_assert (cu->language == language_rust);
52941706
SM
9498 for (type *type_ : cu->rust_unions)
9499 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9500 /* We don't need this any more. */
9501 cu->rust_unions.clear ();
c9317f21
TT
9502}
9503
95554aad
TT
9504/* Return the symtab for PER_CU. This works properly regardless of
9505 whether we're using the index or psymtabs. */
9506
43f3e411
DE
9507static struct compunit_symtab *
9508get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9509{
ed2dc618 9510 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9511 ? per_cu->v.quick->compunit_symtab
9512 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9513}
9514
9515/* A helper function for computing the list of all symbol tables
9516 included by PER_CU. */
9517
9518static void
4c39bc03 9519recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9520 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9521 struct dwarf2_per_cu_data *per_cu,
43f3e411 9522 struct compunit_symtab *immediate_parent)
95554aad
TT
9523{
9524 void **slot;
43f3e411 9525 struct compunit_symtab *cust;
95554aad
TT
9526
9527 slot = htab_find_slot (all_children, per_cu, INSERT);
9528 if (*slot != NULL)
9529 {
9530 /* This inclusion and its children have been processed. */
9531 return;
9532 }
9533
9534 *slot = per_cu;
9535 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9536 cust = get_compunit_symtab (per_cu);
9537 if (cust != NULL)
ec94af83
DE
9538 {
9539 /* If this is a type unit only add its symbol table if we haven't
9540 seen it yet (type unit per_cu's can share symtabs). */
9541 if (per_cu->is_debug_types)
9542 {
43f3e411 9543 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9544 if (*slot == NULL)
9545 {
43f3e411 9546 *slot = cust;
4c39bc03 9547 result->push_back (cust);
43f3e411
DE
9548 if (cust->user == NULL)
9549 cust->user = immediate_parent;
ec94af83
DE
9550 }
9551 }
9552 else
f9125b6c 9553 {
4c39bc03 9554 result->push_back (cust);
43f3e411
DE
9555 if (cust->user == NULL)
9556 cust->user = immediate_parent;
f9125b6c 9557 }
ec94af83 9558 }
95554aad 9559
ae640021
AB
9560 if (!per_cu->imported_symtabs_empty ())
9561 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9562 {
9563 recursively_compute_inclusions (result, all_children,
9564 all_type_symtabs, ptr, cust);
9565 }
95554aad
TT
9566}
9567
43f3e411 9568/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9569 PER_CU. */
9570
9571static void
43f3e411 9572compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9573{
f4dc4d17
DE
9574 gdb_assert (! per_cu->is_debug_types);
9575
ae640021 9576 if (!per_cu->imported_symtabs_empty ())
95554aad 9577 {
ae640021 9578 int len;
4c39bc03 9579 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9580 htab_t all_children, all_type_symtabs;
43f3e411 9581 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9582
9583 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9584 if (cust == NULL)
95554aad
TT
9585 return;
9586
9587 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9588 NULL, xcalloc, xfree);
ec94af83
DE
9589 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9590 NULL, xcalloc, xfree);
95554aad 9591
ae640021 9592 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9593 {
9594 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9595 all_type_symtabs, ptr, cust);
ec94af83 9596 }
95554aad 9597
ec94af83 9598 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9599 len = result_symtabs.size ();
43f3e411 9600 cust->includes
ed2dc618 9601 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9602 struct compunit_symtab *, len + 1);
4c39bc03
TT
9603 memcpy (cust->includes, result_symtabs.data (),
9604 len * sizeof (compunit_symtab *));
43f3e411 9605 cust->includes[len] = NULL;
95554aad 9606
95554aad 9607 htab_delete (all_children);
ec94af83 9608 htab_delete (all_type_symtabs);
95554aad
TT
9609 }
9610}
9611
9612/* Compute the 'includes' field for the symtabs of all the CUs we just
9613 read. */
9614
9615static void
ed2dc618 9616process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9617{
71b73764 9618 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9619 {
9620 if (! iter->is_debug_types)
43f3e411 9621 compute_compunit_symtab_includes (iter);
f4dc4d17 9622 }
95554aad 9623
c5d0225d 9624 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9625}
9626
9cdd5dbd 9627/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9628 already been loaded into memory. */
9629
9630static void
95554aad
TT
9631process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9632 enum language pretend_language)
10b3939b 9633{
10b3939b 9634 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9635 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9636 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9637 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9638 CORE_ADDR lowpc, highpc;
43f3e411 9639 struct compunit_symtab *cust;
10b3939b 9640 CORE_ADDR baseaddr;
4359dff1 9641 struct block *static_block;
3e29f34a 9642 CORE_ADDR addr;
10b3939b 9643
b3b3bada 9644 baseaddr = objfile->text_section_offset ();
10b3939b 9645
c89b44cd
TT
9646 /* Clear the list here in case something was left over. */
9647 cu->method_list.clear ();
10b3939b 9648
95554aad
TT
9649 cu->language = pretend_language;
9650 cu->language_defn = language_def (cu->language);
9651
c906108c 9652 /* Do line number decoding in read_file_scope () */
10b3939b 9653 process_die (cu->dies, cu);
c906108c 9654
a766d390
DE
9655 /* For now fudge the Go package. */
9656 if (cu->language == language_go)
9657 fixup_go_packaging (cu);
9658
5f48f8f3 9659 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9660 should be complete, and it should now be safe to compute all of the
9661 physnames. */
9662 compute_delayed_physnames (cu);
3da10d80 9663
c9317f21
TT
9664 if (cu->language == language_rust)
9665 rust_union_quirks (cu);
9666
fae299cd
DC
9667 /* Some compilers don't define a DW_AT_high_pc attribute for the
9668 compilation unit. If the DW_AT_high_pc is missing, synthesize
9669 it, by scanning the DIE's below the compilation unit. */
10b3939b 9670 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9671
3e29f34a 9672 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9673 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9674
9675 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9676 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9677 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9678 addrmap to help ensure it has an accurate map of pc values belonging to
9679 this comp unit. */
9680 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9681
c24bdb02 9682 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9683 SECT_OFF_TEXT (objfile),
9684 0);
c906108c 9685
43f3e411 9686 if (cust != NULL)
c906108c 9687 {
df15bd07 9688 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9689
8be455d7
JK
9690 /* Set symtab language to language from DW_AT_language. If the
9691 compilation is from a C file generated by language preprocessors, do
9692 not set the language if it was already deduced by start_subfile. */
43f3e411 9693 if (!(cu->language == language_c
40e3ad0e 9694 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9695 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9696
9697 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9698 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9699 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9700 there were bugs in prologue debug info, fixed later in GCC-4.5
9701 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9702
9703 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9704 needed, it would be wrong due to missing DW_AT_producer there.
9705
9706 Still one can confuse GDB by using non-standard GCC compilation
9707 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9708 */
ab260dad 9709 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9710 cust->locations_valid = 1;
e0d00bc7
JK
9711
9712 if (gcc_4_minor >= 5)
43f3e411 9713 cust->epilogue_unwind_valid = 1;
96408a79 9714
43f3e411 9715 cust->call_site_htab = cu->call_site_htab;
c906108c 9716 }
9291a0cd
TT
9717
9718 if (dwarf2_per_objfile->using_index)
43f3e411 9719 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9720 else
9721 {
891813be 9722 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9723 pst->compunit_symtab = cust;
6d94535f 9724 pst->readin = true;
9291a0cd 9725 }
c906108c 9726
95554aad 9727 /* Push it for inclusion processing later. */
c5d0225d 9728 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9729
9730 /* Not needed any more. */
c24bdb02 9731 cu->reset_builder ();
f4dc4d17 9732}
45cfd468 9733
f4dc4d17
DE
9734/* Generate full symbol information for type unit PER_CU, whose DIEs have
9735 already been loaded into memory. */
9736
9737static void
9738process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9739 enum language pretend_language)
9740{
9741 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9742 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9743 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9744 struct compunit_symtab *cust;
0186c6a7
DE
9745 struct signatured_type *sig_type;
9746
9747 gdb_assert (per_cu->is_debug_types);
9748 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9749
c89b44cd
TT
9750 /* Clear the list here in case something was left over. */
9751 cu->method_list.clear ();
f4dc4d17 9752
f4dc4d17
DE
9753 cu->language = pretend_language;
9754 cu->language_defn = language_def (cu->language);
9755
9756 /* The symbol tables are set up in read_type_unit_scope. */
9757 process_die (cu->dies, cu);
9758
9759 /* For now fudge the Go package. */
9760 if (cu->language == language_go)
9761 fixup_go_packaging (cu);
9762
5f48f8f3 9763 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9764 should be complete, and it should now be safe to compute all of the
9765 physnames. */
9766 compute_delayed_physnames (cu);
f4dc4d17 9767
c9317f21
TT
9768 if (cu->language == language_rust)
9769 rust_union_quirks (cu);
9770
f4dc4d17
DE
9771 /* TUs share symbol tables.
9772 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9773 of it with end_expandable_symtab. Otherwise, complete the addition of
9774 this TU's symbols to the existing symtab. */
43f3e411 9775 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9776 {
c24bdb02
KS
9777 buildsym_compunit *builder = cu->get_builder ();
9778 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9779 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9780
43f3e411 9781 if (cust != NULL)
f4dc4d17
DE
9782 {
9783 /* Set symtab language to language from DW_AT_language. If the
9784 compilation is from a C file generated by language preprocessors,
9785 do not set the language if it was already deduced by
9786 start_subfile. */
43f3e411
DE
9787 if (!(cu->language == language_c
9788 && COMPUNIT_FILETABS (cust)->language != language_c))
9789 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9790 }
9791 }
9792 else
9793 {
c24bdb02 9794 cu->get_builder ()->augment_type_symtab ();
43f3e411 9795 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9796 }
9797
9798 if (dwarf2_per_objfile->using_index)
43f3e411 9799 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9800 else
9801 {
891813be 9802 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9803 pst->compunit_symtab = cust;
6d94535f 9804 pst->readin = true;
45cfd468 9805 }
804d2729
TT
9806
9807 /* Not needed any more. */
c24bdb02 9808 cu->reset_builder ();
c906108c
SS
9809}
9810
95554aad
TT
9811/* Process an imported unit DIE. */
9812
9813static void
9814process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9815{
9816 struct attribute *attr;
9817
f4dc4d17
DE
9818 /* For now we don't handle imported units in type units. */
9819 if (cu->per_cu->is_debug_types)
9820 {
9821 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9822 " supported in type units [in module %s]"),
518817b3 9823 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9824 }
9825
95554aad
TT
9826 attr = dwarf2_attr (die, DW_AT_import, cu);
9827 if (attr != NULL)
9828 {
0826b30a 9829 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725
PA
9830 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9831 dwarf2_per_cu_data *per_cu
e3b94546 9832 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9833 cu->per_cu->dwarf2_per_objfile);
95554aad 9834
58990295
TV
9835 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9836 into another compilation unit, at root level. Regard this as a hint,
9837 and ignore it. */
9838 if (die->parent && die->parent->parent == NULL
9839 && per_cu->unit_type == DW_UT_compile
9840 && per_cu->lang == language_cplus)
9841 return;
9842
69d751e3 9843 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9844 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9845 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9846
ae640021 9847 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9848 }
9849}
9850
4c8aa72d
PA
9851/* RAII object that represents a process_die scope: i.e.,
9852 starts/finishes processing a DIE. */
9853class process_die_scope
adde2bff 9854{
4c8aa72d
PA
9855public:
9856 process_die_scope (die_info *die, dwarf2_cu *cu)
9857 : m_die (die), m_cu (cu)
9858 {
9859 /* We should only be processing DIEs not already in process. */
9860 gdb_assert (!m_die->in_process);
9861 m_die->in_process = true;
9862 }
8c3cb9fa 9863
4c8aa72d
PA
9864 ~process_die_scope ()
9865 {
9866 m_die->in_process = false;
9867
9868 /* If we're done processing the DIE for the CU that owns the line
9869 header, we don't need the line header anymore. */
9870 if (m_cu->line_header_die_owner == m_die)
9871 {
9872 delete m_cu->line_header;
9873 m_cu->line_header = NULL;
9874 m_cu->line_header_die_owner = NULL;
9875 }
9876 }
9877
9878private:
9879 die_info *m_die;
9880 dwarf2_cu *m_cu;
9881};
adde2bff 9882
c906108c
SS
9883/* Process a die and its children. */
9884
9885static void
e7c27a73 9886process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9887{
4c8aa72d 9888 process_die_scope scope (die, cu);
adde2bff 9889
c906108c
SS
9890 switch (die->tag)
9891 {
9892 case DW_TAG_padding:
9893 break;
9894 case DW_TAG_compile_unit:
95554aad 9895 case DW_TAG_partial_unit:
e7c27a73 9896 read_file_scope (die, cu);
c906108c 9897 break;
348e048f
DE
9898 case DW_TAG_type_unit:
9899 read_type_unit_scope (die, cu);
9900 break;
c906108c 9901 case DW_TAG_subprogram:
0a4b0913
AB
9902 /* Nested subprograms in Fortran get a prefix. */
9903 if (cu->language == language_fortran
9904 && die->parent != NULL
9905 && die->parent->tag == DW_TAG_subprogram)
9906 cu->processing_has_namespace_info = true;
9907 /* Fall through. */
c906108c 9908 case DW_TAG_inlined_subroutine:
edb3359d 9909 read_func_scope (die, cu);
c906108c
SS
9910 break;
9911 case DW_TAG_lexical_block:
14898363
L
9912 case DW_TAG_try_block:
9913 case DW_TAG_catch_block:
e7c27a73 9914 read_lexical_block_scope (die, cu);
c906108c 9915 break;
216f72a1 9916 case DW_TAG_call_site:
96408a79
SA
9917 case DW_TAG_GNU_call_site:
9918 read_call_site_scope (die, cu);
9919 break;
c906108c 9920 case DW_TAG_class_type:
680b30c7 9921 case DW_TAG_interface_type:
c906108c
SS
9922 case DW_TAG_structure_type:
9923 case DW_TAG_union_type:
134d01f1 9924 process_structure_scope (die, cu);
c906108c
SS
9925 break;
9926 case DW_TAG_enumeration_type:
134d01f1 9927 process_enumeration_scope (die, cu);
c906108c 9928 break;
134d01f1 9929
f792889a
DJ
9930 /* These dies have a type, but processing them does not create
9931 a symbol or recurse to process the children. Therefore we can
9932 read them on-demand through read_type_die. */
c906108c 9933 case DW_TAG_subroutine_type:
72019c9c 9934 case DW_TAG_set_type:
c906108c 9935 case DW_TAG_array_type:
c906108c 9936 case DW_TAG_pointer_type:
c906108c 9937 case DW_TAG_ptr_to_member_type:
c906108c 9938 case DW_TAG_reference_type:
4297a3f0 9939 case DW_TAG_rvalue_reference_type:
c906108c 9940 case DW_TAG_string_type:
c906108c 9941 break;
134d01f1 9942
c906108c 9943 case DW_TAG_base_type:
a02abb62 9944 case DW_TAG_subrange_type:
cb249c71 9945 case DW_TAG_typedef:
134d01f1
DJ
9946 /* Add a typedef symbol for the type definition, if it has a
9947 DW_AT_name. */
f792889a 9948 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9949 break;
c906108c 9950 case DW_TAG_common_block:
e7c27a73 9951 read_common_block (die, cu);
c906108c
SS
9952 break;
9953 case DW_TAG_common_inclusion:
9954 break;
d9fa45fe 9955 case DW_TAG_namespace:
9068261f 9956 cu->processing_has_namespace_info = true;
e7c27a73 9957 read_namespace (die, cu);
d9fa45fe 9958 break;
5d7cb8df 9959 case DW_TAG_module:
9068261f 9960 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9961 read_module (die, cu);
9962 break;
d9fa45fe 9963 case DW_TAG_imported_declaration:
9068261f 9964 cu->processing_has_namespace_info = true;
74921315
KS
9965 if (read_namespace_alias (die, cu))
9966 break;
86a73007
TT
9967 /* The declaration is not a global namespace alias. */
9968 /* Fall through. */
d9fa45fe 9969 case DW_TAG_imported_module:
9068261f 9970 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9971 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9972 || cu->language != language_fortran))
b98664d3 9973 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9974 dwarf_tag_name (die->tag));
9975 read_import_statement (die, cu);
d9fa45fe 9976 break;
95554aad
TT
9977
9978 case DW_TAG_imported_unit:
9979 process_imported_unit_die (die, cu);
9980 break;
9981
71a3c369
TT
9982 case DW_TAG_variable:
9983 read_variable (die, cu);
9984 break;
9985
c906108c 9986 default:
e7c27a73 9987 new_symbol (die, NULL, cu);
c906108c
SS
9988 break;
9989 }
9990}
ca69b9e6
DE
9991\f
9992/* DWARF name computation. */
c906108c 9993
94af9270
KS
9994/* A helper function for dwarf2_compute_name which determines whether DIE
9995 needs to have the name of the scope prepended to the name listed in the
9996 die. */
9997
9998static int
9999die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10000{
1c809c68
TT
10001 struct attribute *attr;
10002
94af9270
KS
10003 switch (die->tag)
10004 {
10005 case DW_TAG_namespace:
10006 case DW_TAG_typedef:
10007 case DW_TAG_class_type:
10008 case DW_TAG_interface_type:
10009 case DW_TAG_structure_type:
10010 case DW_TAG_union_type:
10011 case DW_TAG_enumeration_type:
10012 case DW_TAG_enumerator:
10013 case DW_TAG_subprogram:
08a76f8a 10014 case DW_TAG_inlined_subroutine:
94af9270 10015 case DW_TAG_member:
74921315 10016 case DW_TAG_imported_declaration:
94af9270
KS
10017 return 1;
10018
10019 case DW_TAG_variable:
c2b0a229 10020 case DW_TAG_constant:
94af9270
KS
10021 /* We only need to prefix "globally" visible variables. These include
10022 any variable marked with DW_AT_external or any variable that
10023 lives in a namespace. [Variables in anonymous namespaces
10024 require prefixing, but they are not DW_AT_external.] */
10025
10026 if (dwarf2_attr (die, DW_AT_specification, cu))
10027 {
10028 struct dwarf2_cu *spec_cu = cu;
9a619af0 10029
94af9270
KS
10030 return die_needs_namespace (die_specification (die, &spec_cu),
10031 spec_cu);
10032 }
10033
1c809c68 10034 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10035 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10036 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10037 return 0;
10038 /* A variable in a lexical block of some kind does not need a
10039 namespace, even though in C++ such variables may be external
10040 and have a mangled name. */
10041 if (die->parent->tag == DW_TAG_lexical_block
10042 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10043 || die->parent->tag == DW_TAG_catch_block
10044 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10045 return 0;
10046 return 1;
94af9270
KS
10047
10048 default:
10049 return 0;
10050 }
10051}
10052
73b9be8b
KS
10053/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10054 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10055 defined for the given DIE. */
10056
10057static struct attribute *
10058dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10059{
10060 struct attribute *attr;
10061
10062 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10063 if (attr == NULL)
10064 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10065
10066 return attr;
10067}
10068
10069/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10070 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10071 defined for the given DIE. */
10072
10073static const char *
10074dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10075{
10076 const char *linkage_name;
10077
10078 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10079 if (linkage_name == NULL)
10080 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10081
787de330
TT
10082 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10083 See https://github.com/rust-lang/rust/issues/32925. */
10084 if (cu->language == language_rust && linkage_name != NULL
10085 && strchr (linkage_name, '{') != NULL)
10086 linkage_name = NULL;
10087
73b9be8b
KS
10088 return linkage_name;
10089}
10090
94af9270 10091/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10092 compute the physname for the object, which include a method's:
9c37b5ae 10093 - formal parameters (C++),
a766d390 10094 - receiver type (Go),
a766d390
DE
10095
10096 The term "physname" is a bit confusing.
10097 For C++, for example, it is the demangled name.
10098 For Go, for example, it's the mangled name.
94af9270 10099
af6b7be1
JB
10100 For Ada, return the DIE's linkage name rather than the fully qualified
10101 name. PHYSNAME is ignored..
10102
94af9270
KS
10103 The result is allocated on the objfile_obstack and canonicalized. */
10104
10105static const char *
15d034d0
TT
10106dwarf2_compute_name (const char *name,
10107 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10108 int physname)
10109{
518817b3 10110 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10111
94af9270
KS
10112 if (name == NULL)
10113 name = dwarf2_name (die, cu);
10114
2ee7123e
DE
10115 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10116 but otherwise compute it by typename_concat inside GDB.
10117 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10118 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10119 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10120 will set the demangled name to the result of dwarf2_full_name, and it is
10121 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10122 if (cu->language == language_ada
10123 || (cu->language == language_fortran && physname))
10124 {
10125 /* For Ada unit, we prefer the linkage name over the name, as
10126 the former contains the exported name, which the user expects
10127 to be able to reference. Ideally, we want the user to be able
10128 to reference this entity using either natural or linkage name,
10129 but we haven't started looking at this enhancement yet. */
73b9be8b 10130 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10131
2ee7123e
DE
10132 if (linkage_name != NULL)
10133 return linkage_name;
f55ee35c
JK
10134 }
10135
94af9270
KS
10136 /* These are the only languages we know how to qualify names in. */
10137 if (name != NULL
9c37b5ae 10138 && (cu->language == language_cplus
c44af4eb
TT
10139 || cu->language == language_fortran || cu->language == language_d
10140 || cu->language == language_rust))
94af9270
KS
10141 {
10142 if (die_needs_namespace (die, cu))
10143 {
0d5cff50 10144 const char *prefix;
34a68019 10145 const char *canonical_name = NULL;
94af9270 10146
d7e74731
PA
10147 string_file buf;
10148
94af9270 10149 prefix = determine_prefix (die, cu);
94af9270
KS
10150 if (*prefix != '\0')
10151 {
43816ebc
TT
10152 gdb::unique_xmalloc_ptr<char> prefixed_name
10153 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10154
43816ebc 10155 buf.puts (prefixed_name.get ());
94af9270
KS
10156 }
10157 else
d7e74731 10158 buf.puts (name);
94af9270 10159
98bfdba5
PA
10160 /* Template parameters may be specified in the DIE's DW_AT_name, or
10161 as children with DW_TAG_template_type_param or
10162 DW_TAG_value_type_param. If the latter, add them to the name
10163 here. If the name already has template parameters, then
10164 skip this step; some versions of GCC emit both, and
10165 it is more efficient to use the pre-computed name.
10166
10167 Something to keep in mind about this process: it is very
10168 unlikely, or in some cases downright impossible, to produce
10169 something that will match the mangled name of a function.
10170 If the definition of the function has the same debug info,
10171 we should be able to match up with it anyway. But fallbacks
10172 using the minimal symbol, for instance to find a method
10173 implemented in a stripped copy of libstdc++, will not work.
10174 If we do not have debug info for the definition, we will have to
10175 match them up some other way.
10176
10177 When we do name matching there is a related problem with function
10178 templates; two instantiated function templates are allowed to
10179 differ only by their return types, which we do not add here. */
10180
10181 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10182 {
10183 struct attribute *attr;
10184 struct die_info *child;
10185 int first = 1;
10186
10187 die->building_fullname = 1;
10188
10189 for (child = die->child; child != NULL; child = child->sibling)
10190 {
10191 struct type *type;
12df843f 10192 LONGEST value;
d521ce57 10193 const gdb_byte *bytes;
98bfdba5
PA
10194 struct dwarf2_locexpr_baton *baton;
10195 struct value *v;
10196
10197 if (child->tag != DW_TAG_template_type_param
10198 && child->tag != DW_TAG_template_value_param)
10199 continue;
10200
10201 if (first)
10202 {
d7e74731 10203 buf.puts ("<");
98bfdba5
PA
10204 first = 0;
10205 }
10206 else
d7e74731 10207 buf.puts (", ");
98bfdba5
PA
10208
10209 attr = dwarf2_attr (child, DW_AT_type, cu);
10210 if (attr == NULL)
10211 {
b98664d3 10212 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10213 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10214 continue;
10215 }
10216 type = die_type (child, cu);
10217
10218 if (child->tag == DW_TAG_template_type_param)
10219 {
c1ec8cea
TT
10220 c_print_type (type, "", &buf, -1, 0, cu->language,
10221 &type_print_raw_options);
98bfdba5
PA
10222 continue;
10223 }
10224
10225 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10226 if (attr == NULL)
10227 {
b98664d3 10228 complaint (_("template parameter missing "
3e43a32a 10229 "DW_AT_const_value"));
d7e74731 10230 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10231 continue;
10232 }
10233
10234 dwarf2_const_value_attr (attr, type, name,
10235 &cu->comp_unit_obstack, cu,
10236 &value, &bytes, &baton);
10237
10238 if (TYPE_NOSIGN (type))
10239 /* GDB prints characters as NUMBER 'CHAR'. If that's
10240 changed, this can use value_print instead. */
d7e74731 10241 c_printchar (value, type, &buf);
98bfdba5
PA
10242 else
10243 {
10244 struct value_print_options opts;
10245
10246 if (baton != NULL)
10247 v = dwarf2_evaluate_loc_desc (type, NULL,
10248 baton->data,
10249 baton->size,
10250 baton->per_cu);
10251 else if (bytes != NULL)
10252 {
10253 v = allocate_value (type);
10254 memcpy (value_contents_writeable (v), bytes,
10255 TYPE_LENGTH (type));
10256 }
10257 else
10258 v = value_from_longest (type, value);
10259
3e43a32a
MS
10260 /* Specify decimal so that we do not depend on
10261 the radix. */
98bfdba5
PA
10262 get_formatted_print_options (&opts, 'd');
10263 opts.raw = 1;
d7e74731 10264 value_print (v, &buf, &opts);
98bfdba5 10265 release_value (v);
98bfdba5
PA
10266 }
10267 }
10268
10269 die->building_fullname = 0;
10270
10271 if (!first)
10272 {
10273 /* Close the argument list, with a space if necessary
10274 (nested templates). */
d7e74731
PA
10275 if (!buf.empty () && buf.string ().back () == '>')
10276 buf.puts (" >");
98bfdba5 10277 else
d7e74731 10278 buf.puts (">");
98bfdba5
PA
10279 }
10280 }
10281
9c37b5ae 10282 /* For C++ methods, append formal parameter type
94af9270 10283 information, if PHYSNAME. */
6e70227d 10284
94af9270 10285 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10286 && cu->language == language_cplus)
94af9270
KS
10287 {
10288 struct type *type = read_type_die (die, cu);
10289
d7e74731 10290 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10291 &type_print_raw_options);
94af9270 10292
9c37b5ae 10293 if (cu->language == language_cplus)
94af9270 10294 {
60430eff
DJ
10295 /* Assume that an artificial first parameter is
10296 "this", but do not crash if it is not. RealView
10297 marks unnamed (and thus unused) parameters as
10298 artificial; there is no way to differentiate
10299 the two cases. */
94af9270
KS
10300 if (TYPE_NFIELDS (type) > 0
10301 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10302 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10303 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10304 0))))
d7e74731 10305 buf.puts (" const");
94af9270
KS
10306 }
10307 }
10308
d7e74731 10309 const std::string &intermediate_name = buf.string ();
94af9270
KS
10310
10311 if (cu->language == language_cplus)
34a68019 10312 canonical_name
322a8516 10313 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10314 objfile);
34a68019
TT
10315
10316 /* If we only computed INTERMEDIATE_NAME, or if
10317 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10318 intern it. */
322a8516 10319 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10320 name = objfile->intern (intermediate_name);
34a68019
TT
10321 else
10322 name = canonical_name;
94af9270
KS
10323 }
10324 }
10325
10326 return name;
10327}
10328
0114d602
DJ
10329/* Return the fully qualified name of DIE, based on its DW_AT_name.
10330 If scope qualifiers are appropriate they will be added. The result
34a68019 10331 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10332 not have a name. NAME may either be from a previous call to
10333 dwarf2_name or NULL.
10334
9c37b5ae 10335 The output string will be canonicalized (if C++). */
0114d602
DJ
10336
10337static const char *
15d034d0 10338dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10339{
94af9270
KS
10340 return dwarf2_compute_name (name, die, cu, 0);
10341}
0114d602 10342
94af9270
KS
10343/* Construct a physname for the given DIE in CU. NAME may either be
10344 from a previous call to dwarf2_name or NULL. The result will be
10345 allocated on the objfile_objstack or NULL if the DIE does not have a
10346 name.
0114d602 10347
9c37b5ae 10348 The output string will be canonicalized (if C++). */
0114d602 10349
94af9270 10350static const char *
15d034d0 10351dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10352{
518817b3 10353 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10354 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10355 int need_copy = 1;
10356
10357 /* In this case dwarf2_compute_name is just a shortcut not building anything
10358 on its own. */
10359 if (!die_needs_namespace (die, cu))
10360 return dwarf2_compute_name (name, die, cu, 1);
10361
906bb4c5
TT
10362 if (cu->language != language_rust)
10363 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10364
10365 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10366 has computed. */
791afaa2 10367 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10368 if (mangled != NULL)
900e11f9 10369 {
900e11f9 10370
59cc4834
JB
10371 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10372 {
10373 /* Do nothing (do not demangle the symbol name). */
10374 }
10375 else if (cu->language == language_go)
a766d390 10376 {
5e2db402
TT
10377 /* This is a lie, but we already lie to the caller new_symbol.
10378 new_symbol assumes we return the mangled name.
a766d390 10379 This just undoes that lie until things are cleaned up. */
a766d390
DE
10380 }
10381 else
10382 {
0eb876f5
JB
10383 /* Use DMGL_RET_DROP for C++ template functions to suppress
10384 their return type. It is easier for GDB users to search
10385 for such functions as `name(params)' than `long name(params)'.
10386 In such case the minimal symbol names do not match the full
10387 symbol names but for template functions there is never a need
10388 to look up their definition from their declaration so
10389 the only disadvantage remains the minimal symbol variant
10390 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10391 demangled.reset (gdb_demangle (mangled,
10392 (DMGL_PARAMS | DMGL_ANSI
10393 | DMGL_RET_DROP)));
a766d390 10394 }
900e11f9 10395 if (demangled)
791afaa2 10396 canon = demangled.get ();
900e11f9
JK
10397 else
10398 {
10399 canon = mangled;
10400 need_copy = 0;
10401 }
10402 }
10403
10404 if (canon == NULL || check_physname)
10405 {
10406 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10407
10408 if (canon != NULL && strcmp (physname, canon) != 0)
10409 {
10410 /* It may not mean a bug in GDB. The compiler could also
10411 compute DW_AT_linkage_name incorrectly. But in such case
10412 GDB would need to be bug-to-bug compatible. */
10413
b98664d3 10414 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10415 "(from linkage <%s>) - DIE at %s [in module %s]"),
10416 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10417 objfile_name (objfile));
900e11f9
JK
10418
10419 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10420 is available here - over computed PHYSNAME. It is safer
10421 against both buggy GDB and buggy compilers. */
10422
10423 retval = canon;
10424 }
10425 else
10426 {
10427 retval = physname;
10428 need_copy = 0;
10429 }
10430 }
10431 else
10432 retval = canon;
10433
10434 if (need_copy)
be1e3d3e 10435 retval = objfile->intern (retval);
900e11f9 10436
900e11f9 10437 return retval;
0114d602
DJ
10438}
10439
74921315
KS
10440/* Inspect DIE in CU for a namespace alias. If one exists, record
10441 a new symbol for it.
10442
10443 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10444
10445static int
10446read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10447{
10448 struct attribute *attr;
10449
10450 /* If the die does not have a name, this is not a namespace
10451 alias. */
10452 attr = dwarf2_attr (die, DW_AT_name, cu);
10453 if (attr != NULL)
10454 {
10455 int num;
10456 struct die_info *d = die;
10457 struct dwarf2_cu *imported_cu = cu;
10458
10459 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10460 keep inspecting DIEs until we hit the underlying import. */
10461#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10462 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10463 {
10464 attr = dwarf2_attr (d, DW_AT_import, cu);
10465 if (attr == NULL)
10466 break;
10467
10468 d = follow_die_ref (d, attr, &imported_cu);
10469 if (d->tag != DW_TAG_imported_declaration)
10470 break;
10471 }
10472
10473 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10474 {
b98664d3 10475 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10476 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10477 return 0;
10478 }
10479
10480 if (attr != NULL)
10481 {
10482 struct type *type;
0826b30a 10483 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10484
9c541725 10485 type = get_die_type_at_offset (sect_off, cu->per_cu);
78134374 10486 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10487 {
10488 /* This declaration is a global namespace alias. Add
10489 a symbol for it whose type is the aliased namespace. */
10490 new_symbol (die, type, cu);
10491 return 1;
10492 }
10493 }
10494 }
10495
10496 return 0;
10497}
10498
22cee43f 10499/* Return the using directives repository (global or local?) to use in the
804d2729 10500 current context for CU.
22cee43f
PMR
10501
10502 For Ada, imported declarations can materialize renamings, which *may* be
10503 global. However it is impossible (for now?) in DWARF to distinguish
10504 "external" imported declarations and "static" ones. As all imported
10505 declarations seem to be static in all other languages, make them all CU-wide
10506 global only in Ada. */
10507
10508static struct using_direct **
804d2729 10509using_directives (struct dwarf2_cu *cu)
22cee43f 10510{
c24bdb02
KS
10511 if (cu->language == language_ada
10512 && cu->get_builder ()->outermost_context_p ())
10513 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10514 else
c24bdb02 10515 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10516}
10517
27aa8d6a
SW
10518/* Read the import statement specified by the given die and record it. */
10519
10520static void
10521read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10522{
518817b3 10523 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10524 struct attribute *import_attr;
32019081 10525 struct die_info *imported_die, *child_die;
de4affc9 10526 struct dwarf2_cu *imported_cu;
27aa8d6a 10527 const char *imported_name;
794684b6 10528 const char *imported_name_prefix;
13387711
SW
10529 const char *canonical_name;
10530 const char *import_alias;
10531 const char *imported_declaration = NULL;
794684b6 10532 const char *import_prefix;
eb1e02fd 10533 std::vector<const char *> excludes;
13387711 10534
27aa8d6a
SW
10535 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10536 if (import_attr == NULL)
10537 {
b98664d3 10538 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10539 dwarf_tag_name (die->tag));
10540 return;
10541 }
10542
de4affc9
CC
10543 imported_cu = cu;
10544 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10545 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10546 if (imported_name == NULL)
10547 {
10548 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10549
10550 The import in the following code:
10551 namespace A
10552 {
10553 typedef int B;
10554 }
10555
10556 int main ()
10557 {
10558 using A::B;
10559 B b;
10560 return b;
10561 }
10562
10563 ...
10564 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10565 <52> DW_AT_decl_file : 1
10566 <53> DW_AT_decl_line : 6
10567 <54> DW_AT_import : <0x75>
10568 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10569 <59> DW_AT_name : B
10570 <5b> DW_AT_decl_file : 1
10571 <5c> DW_AT_decl_line : 2
10572 <5d> DW_AT_type : <0x6e>
10573 ...
10574 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10575 <76> DW_AT_byte_size : 4
10576 <77> DW_AT_encoding : 5 (signed)
10577
10578 imports the wrong die ( 0x75 instead of 0x58 ).
10579 This case will be ignored until the gcc bug is fixed. */
10580 return;
10581 }
10582
82856980
SW
10583 /* Figure out the local name after import. */
10584 import_alias = dwarf2_name (die, cu);
27aa8d6a 10585
794684b6
SW
10586 /* Figure out where the statement is being imported to. */
10587 import_prefix = determine_prefix (die, cu);
10588
10589 /* Figure out what the scope of the imported die is and prepend it
10590 to the name of the imported die. */
de4affc9 10591 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10592
f55ee35c
JK
10593 if (imported_die->tag != DW_TAG_namespace
10594 && imported_die->tag != DW_TAG_module)
794684b6 10595 {
13387711
SW
10596 imported_declaration = imported_name;
10597 canonical_name = imported_name_prefix;
794684b6 10598 }
13387711 10599 else if (strlen (imported_name_prefix) > 0)
12aaed36 10600 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10601 imported_name_prefix,
10602 (cu->language == language_d ? "." : "::"),
10603 imported_name, (char *) NULL);
13387711
SW
10604 else
10605 canonical_name = imported_name;
794684b6 10606
32019081
JK
10607 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10608 for (child_die = die->child; child_die && child_die->tag;
436c571c 10609 child_die = child_die->sibling)
32019081
JK
10610 {
10611 /* DWARF-4: A Fortran use statement with a “rename list” may be
10612 represented by an imported module entry with an import attribute
10613 referring to the module and owned entries corresponding to those
10614 entities that are renamed as part of being imported. */
10615
10616 if (child_die->tag != DW_TAG_imported_declaration)
10617 {
b98664d3 10618 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10619 "- DIE at %s [in module %s]"),
10620 sect_offset_str (child_die->sect_off),
10621 objfile_name (objfile));
32019081
JK
10622 continue;
10623 }
10624
10625 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10626 if (import_attr == NULL)
10627 {
b98664d3 10628 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10629 dwarf_tag_name (child_die->tag));
10630 continue;
10631 }
10632
10633 imported_cu = cu;
10634 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10635 &imported_cu);
10636 imported_name = dwarf2_name (imported_die, imported_cu);
10637 if (imported_name == NULL)
10638 {
b98664d3 10639 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10640 "imported name - DIE at %s [in module %s]"),
10641 sect_offset_str (child_die->sect_off),
10642 objfile_name (objfile));
32019081
JK
10643 continue;
10644 }
10645
eb1e02fd 10646 excludes.push_back (imported_name);
32019081
JK
10647
10648 process_die (child_die, cu);
10649 }
10650
804d2729 10651 add_using_directive (using_directives (cu),
22cee43f
PMR
10652 import_prefix,
10653 canonical_name,
10654 import_alias,
10655 imported_declaration,
10656 excludes,
10657 0,
10658 &objfile->objfile_obstack);
27aa8d6a
SW
10659}
10660
5230b05a
WT
10661/* ICC<14 does not output the required DW_AT_declaration on incomplete
10662 types, but gives them a size of zero. Starting with version 14,
10663 ICC is compatible with GCC. */
10664
9068261f 10665static bool
5230b05a
WT
10666producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10667{
10668 if (!cu->checked_producer)
10669 check_producer (cu);
10670
10671 return cu->producer_is_icc_lt_14;
10672}
10673
eb77c9df
AB
10674/* ICC generates a DW_AT_type for C void functions. This was observed on
10675 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10676 which says that void functions should not have a DW_AT_type. */
10677
10678static bool
10679producer_is_icc (struct dwarf2_cu *cu)
10680{
10681 if (!cu->checked_producer)
10682 check_producer (cu);
10683
10684 return cu->producer_is_icc;
10685}
10686
1b80a9fa
JK
10687/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10688 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10689 this, it was first present in GCC release 4.3.0. */
10690
9068261f 10691static bool
1b80a9fa
JK
10692producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10693{
10694 if (!cu->checked_producer)
10695 check_producer (cu);
10696
10697 return cu->producer_is_gcc_lt_4_3;
10698}
10699
d721ba37
PA
10700static file_and_directory
10701find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10702{
d721ba37
PA
10703 file_and_directory res;
10704
9291a0cd
TT
10705 /* Find the filename. Do not use dwarf2_name here, since the filename
10706 is not a source language identifier. */
d721ba37
PA
10707 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10708 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10709
d721ba37
PA
10710 if (res.comp_dir == NULL
10711 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10712 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10713 {
d721ba37
PA
10714 res.comp_dir_storage = ldirname (res.name);
10715 if (!res.comp_dir_storage.empty ())
10716 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10717 }
d721ba37 10718 if (res.comp_dir != NULL)
9291a0cd
TT
10719 {
10720 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10721 directory, get rid of it. */
d721ba37 10722 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10723
d721ba37
PA
10724 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10725 res.comp_dir = cp + 1;
9291a0cd
TT
10726 }
10727
d721ba37
PA
10728 if (res.name == NULL)
10729 res.name = "<unknown>";
10730
10731 return res;
9291a0cd
TT
10732}
10733
f4dc4d17
DE
10734/* Handle DW_AT_stmt_list for a compilation unit.
10735 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10736 COMP_DIR is the compilation directory. LOWPC is passed to
10737 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10738
10739static void
10740handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10741 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10742{
518817b3
SM
10743 struct dwarf2_per_objfile *dwarf2_per_objfile
10744 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10745 struct attribute *attr;
527f3840
JK
10746 struct line_header line_header_local;
10747 hashval_t line_header_local_hash;
527f3840
JK
10748 void **slot;
10749 int decode_mapping;
2ab95328 10750
f4dc4d17
DE
10751 gdb_assert (! cu->per_cu->is_debug_types);
10752
2ab95328 10753 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10754 if (attr == NULL)
10755 return;
10756
9c541725 10757 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10758
10759 /* The line header hash table is only created if needed (it exists to
10760 prevent redundant reading of the line table for partial_units).
10761 If we're given a partial_unit, we'll need it. If we're given a
10762 compile_unit, then use the line header hash table if it's already
10763 created, but don't create one just yet. */
10764
10765 if (dwarf2_per_objfile->line_header_hash == NULL
10766 && die->tag == DW_TAG_partial_unit)
2ab95328 10767 {
527f3840 10768 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10769 .reset (htab_create_alloc (127, line_header_hash_voidp,
10770 line_header_eq_voidp,
10771 free_line_header_voidp,
10772 xcalloc, xfree));
527f3840 10773 }
2ab95328 10774
9c541725 10775 line_header_local.sect_off = line_offset;
527f3840
JK
10776 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10777 line_header_local_hash = line_header_hash (&line_header_local);
10778 if (dwarf2_per_objfile->line_header_hash != NULL)
10779 {
d15acc42 10780 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10781 &line_header_local,
10782 line_header_local_hash, NO_INSERT);
10783
10784 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10785 is not present in *SLOT (since if there is something in *SLOT then
10786 it will be for a partial_unit). */
10787 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10788 {
527f3840 10789 gdb_assert (*slot != NULL);
9a3c8263 10790 cu->line_header = (struct line_header *) *slot;
527f3840 10791 return;
dee91e82 10792 }
2ab95328 10793 }
527f3840
JK
10794
10795 /* dwarf_decode_line_header does not yet provide sufficient information.
10796 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10797 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10798 if (lh == NULL)
527f3840 10799 return;
4c8aa72d
PA
10800
10801 cu->line_header = lh.release ();
10802 cu->line_header_die_owner = die;
527f3840
JK
10803
10804 if (dwarf2_per_objfile->line_header_hash == NULL)
10805 slot = NULL;
10806 else
10807 {
d15acc42 10808 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10809 &line_header_local,
10810 line_header_local_hash, INSERT);
10811 gdb_assert (slot != NULL);
10812 }
10813 if (slot != NULL && *slot == NULL)
10814 {
10815 /* This newly decoded line number information unit will be owned
10816 by line_header_hash hash table. */
10817 *slot = cu->line_header;
4c8aa72d 10818 cu->line_header_die_owner = NULL;
527f3840
JK
10819 }
10820 else
10821 {
10822 /* We cannot free any current entry in (*slot) as that struct line_header
10823 may be already used by multiple CUs. Create only temporary decoded
10824 line_header for this CU - it may happen at most once for each line
10825 number information unit. And if we're not using line_header_hash
10826 then this is what we want as well. */
10827 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10828 }
10829 decode_mapping = (die->tag != DW_TAG_partial_unit);
10830 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10831 decode_mapping);
fff8551c 10832
2ab95328
TT
10833}
10834
95554aad 10835/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10836
c906108c 10837static void
e7c27a73 10838read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10839{
518817b3
SM
10840 struct dwarf2_per_objfile *dwarf2_per_objfile
10841 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10842 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10843 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10844 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10845 CORE_ADDR highpc = ((CORE_ADDR) 0);
10846 struct attribute *attr;
c906108c 10847 struct die_info *child_die;
e142c38c 10848 CORE_ADDR baseaddr;
6e70227d 10849
380618d6 10850 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10851 baseaddr = objfile->text_section_offset ();
c906108c 10852
fae299cd 10853 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10854
10855 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10856 from finish_block. */
2acceee2 10857 if (lowpc == ((CORE_ADDR) -1))
c906108c 10858 lowpc = highpc;
3e29f34a 10859 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10860
d721ba37 10861 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10862
f4b8a18d
KW
10863 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10864 standardised yet. As a workaround for the language detection we fall
10865 back to the DW_AT_producer string. */
10866 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10867 cu->language = language_opencl;
10868
3019eac3
DE
10869 /* Similar hack for Go. */
10870 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10871 set_cu_language (DW_LANG_Go, cu);
10872
c24bdb02 10873 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10874
10875 /* Decode line number information if present. We do this before
10876 processing child DIEs, so that the line header table is available
10877 for DW_AT_decl_file. */
d721ba37 10878 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10879
10880 /* Process all dies in compilation unit. */
10881 if (die->child != NULL)
10882 {
10883 child_die = die->child;
10884 while (child_die && child_die->tag)
10885 {
10886 process_die (child_die, cu);
436c571c 10887 child_die = child_die->sibling;
3019eac3
DE
10888 }
10889 }
10890
10891 /* Decode macro information, if present. Dwarf 2 macro information
10892 refers to information in the line number info statement program
10893 header, so we can only read it if we've read the header
10894 successfully. */
0af92d60
JK
10895 attr = dwarf2_attr (die, DW_AT_macros, cu);
10896 if (attr == NULL)
10897 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10898 if (attr && cu->line_header)
10899 {
10900 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10901 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10902
43f3e411 10903 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10904 }
10905 else
10906 {
10907 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10908 if (attr && cu->line_header)
10909 {
10910 unsigned int macro_offset = DW_UNSND (attr);
10911
43f3e411 10912 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10913 }
10914 }
3019eac3
DE
10915}
10916
c24bdb02
KS
10917void
10918dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10919{
f4dc4d17
DE
10920 struct type_unit_group *tu_group;
10921 int first_time;
3019eac3 10922 struct attribute *attr;
9c541725 10923 unsigned int i;
0186c6a7 10924 struct signatured_type *sig_type;
3019eac3 10925
f4dc4d17 10926 gdb_assert (per_cu->is_debug_types);
0186c6a7 10927 sig_type = (struct signatured_type *) per_cu;
3019eac3 10928
c24bdb02 10929 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10930
f4dc4d17 10931 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10932 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10933 if (sig_type->type_unit_group == NULL)
c24bdb02 10934 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10935 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10936
10937 /* If we've already processed this stmt_list there's no real need to
10938 do it again, we could fake it and just recreate the part we need
10939 (file name,index -> symtab mapping). If data shows this optimization
10940 is useful we can do it then. */
43f3e411 10941 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10942
10943 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10944 debug info. */
fff8551c 10945 line_header_up lh;
f4dc4d17 10946 if (attr != NULL)
3019eac3 10947 {
9c541725 10948 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10949 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10950 }
10951 if (lh == NULL)
10952 {
10953 if (first_time)
c24bdb02 10954 start_symtab ("", NULL, 0);
f4dc4d17
DE
10955 else
10956 {
10957 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10958 gdb_assert (m_builder == nullptr);
804d2729 10959 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10960 m_builder.reset (new struct buildsym_compunit
10961 (COMPUNIT_OBJFILE (cust), "",
10962 COMPUNIT_DIRNAME (cust),
10963 compunit_language (cust),
10964 0, cust));
770479f2 10965 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10966 }
f4dc4d17 10967 return;
3019eac3
DE
10968 }
10969
c24bdb02
KS
10970 line_header = lh.release ();
10971 line_header_die_owner = die;
3019eac3 10972
f4dc4d17
DE
10973 if (first_time)
10974 {
c24bdb02 10975 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10976
1fd60fc0
DE
10977 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10978 still initializing it, and our caller (a few levels up)
10979 process_full_type_unit still needs to know if this is the first
10980 time. */
10981
4ac93832
TT
10982 tu_group->symtabs
10983 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10984 struct symtab *, line_header->file_names_size ());
3019eac3 10985
7ba99d21
AT
10986 auto &file_names = line_header->file_names ();
10987 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10988 {
7ba99d21 10989 file_entry &fe = file_names[i];
c24bdb02
KS
10990 dwarf2_start_subfile (this, fe.name,
10991 fe.include_dir (line_header));
10992 buildsym_compunit *b = get_builder ();
10993 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10994 {
4c8aa72d
PA
10995 /* NOTE: start_subfile will recognize when it's been
10996 passed a file it has already seen. So we can't
10997 assume there's a simple mapping from
10998 cu->line_header->file_names to subfiles, plus
10999 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11000 b->get_current_subfile ()->symtab
11001 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11002 }
11003
c24bdb02 11004 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11005 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11006 }
11007 }
11008 else
3019eac3 11009 {
c24bdb02 11010 gdb_assert (m_builder == nullptr);
804d2729 11011 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11012 m_builder.reset (new struct buildsym_compunit
11013 (COMPUNIT_OBJFILE (cust), "",
11014 COMPUNIT_DIRNAME (cust),
11015 compunit_language (cust),
11016 0, cust));
770479f2 11017 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11018
7ba99d21
AT
11019 auto &file_names = line_header->file_names ();
11020 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11021 {
7ba99d21 11022 file_entry &fe = file_names[i];
4c8aa72d 11023 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11024 }
3019eac3
DE
11025 }
11026
f4dc4d17
DE
11027 /* The main symtab is allocated last. Type units don't have DW_AT_name
11028 so they don't have a "real" (so to speak) symtab anyway.
11029 There is later code that will assign the main symtab to all symbols
11030 that don't have one. We need to handle the case of a symbol with a
11031 missing symtab (DW_AT_decl_file) anyway. */
11032}
3019eac3 11033
f4dc4d17
DE
11034/* Process DW_TAG_type_unit.
11035 For TUs we want to skip the first top level sibling if it's not the
11036 actual type being defined by this TU. In this case the first top
11037 level sibling is there to provide context only. */
3019eac3 11038
f4dc4d17
DE
11039static void
11040read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11041{
11042 struct die_info *child_die;
3019eac3 11043
f4dc4d17
DE
11044 prepare_one_comp_unit (cu, die, language_minimal);
11045
11046 /* Initialize (or reinitialize) the machinery for building symtabs.
11047 We do this before processing child DIEs, so that the line header table
11048 is available for DW_AT_decl_file. */
c24bdb02 11049 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11050
11051 if (die->child != NULL)
11052 {
11053 child_die = die->child;
11054 while (child_die && child_die->tag)
11055 {
11056 process_die (child_die, cu);
436c571c 11057 child_die = child_die->sibling;
f4dc4d17
DE
11058 }
11059 }
3019eac3
DE
11060}
11061\f
80626a55
DE
11062/* DWO/DWP files.
11063
11064 http://gcc.gnu.org/wiki/DebugFission
11065 http://gcc.gnu.org/wiki/DebugFissionDWP
11066
11067 To simplify handling of both DWO files ("object" files with the DWARF info)
11068 and DWP files (a file with the DWOs packaged up into one file), we treat
11069 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11070
11071static hashval_t
11072hash_dwo_file (const void *item)
11073{
9a3c8263 11074 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11075 hashval_t hash;
3019eac3 11076
a2ce51a0
DE
11077 hash = htab_hash_string (dwo_file->dwo_name);
11078 if (dwo_file->comp_dir != NULL)
11079 hash += htab_hash_string (dwo_file->comp_dir);
11080 return hash;
3019eac3
DE
11081}
11082
11083static int
11084eq_dwo_file (const void *item_lhs, const void *item_rhs)
11085{
9a3c8263
SM
11086 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11087 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11088
a2ce51a0
DE
11089 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11090 return 0;
11091 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11092 return lhs->comp_dir == rhs->comp_dir;
11093 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11094}
11095
11096/* Allocate a hash table for DWO files. */
11097
51ac9db5 11098static htab_up
298e9637 11099allocate_dwo_file_hash_table ()
3019eac3 11100{
51ac9db5
SM
11101 auto delete_dwo_file = [] (void *item)
11102 {
11103 struct dwo_file *dwo_file = (struct dwo_file *) item;
11104
11105 delete dwo_file;
11106 };
11107
bc68fb19
TT
11108 return htab_up (htab_create_alloc (41,
11109 hash_dwo_file,
11110 eq_dwo_file,
11111 delete_dwo_file,
11112 xcalloc, xfree));
3019eac3
DE
11113}
11114
80626a55
DE
11115/* Lookup DWO file DWO_NAME. */
11116
11117static void **
ed2dc618
SM
11118lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11119 const char *dwo_name,
11120 const char *comp_dir)
80626a55
DE
11121{
11122 struct dwo_file find_entry;
11123 void **slot;
11124
11125 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11126 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11127
0ac5b59e
DE
11128 find_entry.dwo_name = dwo_name;
11129 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11130 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11131 INSERT);
80626a55
DE
11132
11133 return slot;
11134}
11135
3019eac3
DE
11136static hashval_t
11137hash_dwo_unit (const void *item)
11138{
9a3c8263 11139 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11140
11141 /* This drops the top 32 bits of the id, but is ok for a hash. */
11142 return dwo_unit->signature;
11143}
11144
11145static int
11146eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11147{
9a3c8263
SM
11148 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11149 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11150
11151 /* The signature is assumed to be unique within the DWO file.
11152 So while object file CU dwo_id's always have the value zero,
11153 that's OK, assuming each object file DWO file has only one CU,
11154 and that's the rule for now. */
11155 return lhs->signature == rhs->signature;
11156}
11157
11158/* Allocate a hash table for DWO CUs,TUs.
11159 There is one of these tables for each of CUs,TUs for each DWO file. */
11160
b0b6a987 11161static htab_up
298e9637 11162allocate_dwo_unit_table ()
3019eac3
DE
11163{
11164 /* Start out with a pretty small number.
11165 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11166 return htab_up (htab_create_alloc (3,
11167 hash_dwo_unit,
11168 eq_dwo_unit,
11169 NULL, xcalloc, xfree));
3019eac3
DE
11170}
11171
19c3d4c9 11172/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11173
11174static void
19c3d4c9
DE
11175create_dwo_cu_reader (const struct die_reader_specs *reader,
11176 const gdb_byte *info_ptr,
11177 struct die_info *comp_unit_die,
c0ab21c2
TT
11178 struct dwo_file *dwo_file,
11179 struct dwo_unit *dwo_unit)
3019eac3
DE
11180{
11181 struct dwarf2_cu *cu = reader->cu;
9c541725 11182 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11183 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11184
a084a2a6
AT
11185 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11186 if (!signature.has_value ())
3019eac3 11187 {
b98664d3 11188 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11189 " its dwo_id [in module %s]"),
9d8780f0 11190 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11191 return;
11192 }
11193
3019eac3 11194 dwo_unit->dwo_file = dwo_file;
a084a2a6 11195 dwo_unit->signature = *signature;
8a0459fd 11196 dwo_unit->section = section;
9c541725 11197 dwo_unit->sect_off = sect_off;
3019eac3
DE
11198 dwo_unit->length = cu->per_cu->length;
11199
b4f54984 11200 if (dwarf_read_debug)
9d8780f0
SM
11201 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11202 sect_offset_str (sect_off),
9c541725 11203 hex_string (dwo_unit->signature));
3019eac3
DE
11204}
11205
33c5cd75 11206/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11207 Note: This function processes DWO files only, not DWP files. */
3019eac3 11208
33c5cd75 11209static void
ed2dc618 11210create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11211 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11212 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11213{
11214 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11215 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11216
96b79293 11217 section.read (objfile);
33c5cd75 11218 info_ptr = section.buffer;
3019eac3
DE
11219
11220 if (info_ptr == NULL)
33c5cd75 11221 return;
3019eac3 11222
b4f54984 11223 if (dwarf_read_debug)
19c3d4c9
DE
11224 {
11225 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11226 section.get_name (),
11227 section.get_file_name ());
19c3d4c9 11228 }
3019eac3 11229
33c5cd75 11230 end_ptr = info_ptr + section.size;
3019eac3
DE
11231 while (info_ptr < end_ptr)
11232 {
11233 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11234 struct dwo_unit read_unit {};
33c5cd75
DB
11235 struct dwo_unit *dwo_unit;
11236 void **slot;
11237 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11238
11239 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11240 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11241 per_cu.is_debug_types = 0;
33c5cd75
DB
11242 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11243 per_cu.section = &section;
11244
c0ab21c2
TT
11245 cutu_reader reader (&per_cu, cu, &dwo_file);
11246 if (!reader.dummy_p)
11247 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11248 &dwo_file, &read_unit);
33c5cd75
DB
11249 info_ptr += per_cu.length;
11250
11251 // If the unit could not be parsed, skip it.
c0ab21c2 11252 if (read_unit.dwo_file == NULL)
33c5cd75 11253 continue;
3019eac3 11254
33c5cd75 11255 if (cus_htab == NULL)
298e9637 11256 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11257
33c5cd75 11258 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11259 *dwo_unit = read_unit;
b0b6a987 11260 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11261 gdb_assert (slot != NULL);
11262 if (*slot != NULL)
19c3d4c9 11263 {
33c5cd75
DB
11264 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11265 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11266
b98664d3 11267 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11268 " the entry at offset %s, signature %s"),
11269 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11270 hex_string (dwo_unit->signature));
19c3d4c9 11271 }
33c5cd75 11272 *slot = (void *)dwo_unit;
3019eac3 11273 }
3019eac3
DE
11274}
11275
80626a55
DE
11276/* DWP file .debug_{cu,tu}_index section format:
11277 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11278
d2415c6c
DE
11279 DWP Version 1:
11280
80626a55
DE
11281 Both index sections have the same format, and serve to map a 64-bit
11282 signature to a set of section numbers. Each section begins with a header,
11283 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11284 indexes, and a pool of 32-bit section numbers. The index sections will be
11285 aligned at 8-byte boundaries in the file.
11286
d2415c6c
DE
11287 The index section header consists of:
11288
11289 V, 32 bit version number
11290 -, 32 bits unused
11291 N, 32 bit number of compilation units or type units in the index
11292 M, 32 bit number of slots in the hash table
80626a55 11293
d2415c6c 11294 Numbers are recorded using the byte order of the application binary.
80626a55 11295
d2415c6c
DE
11296 The hash table begins at offset 16 in the section, and consists of an array
11297 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11298 order of the application binary). Unused slots in the hash table are 0.
11299 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11300
d2415c6c
DE
11301 The parallel table begins immediately after the hash table
11302 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11303 array of 32-bit indexes (using the byte order of the application binary),
11304 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11305 table contains a 32-bit index into the pool of section numbers. For unused
11306 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11307
73869dc2
DE
11308 The pool of section numbers begins immediately following the hash table
11309 (at offset 16 + 12 * M from the beginning of the section). The pool of
11310 section numbers consists of an array of 32-bit words (using the byte order
11311 of the application binary). Each item in the array is indexed starting
11312 from 0. The hash table entry provides the index of the first section
11313 number in the set. Additional section numbers in the set follow, and the
11314 set is terminated by a 0 entry (section number 0 is not used in ELF).
11315
11316 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11317 section must be the first entry in the set, and the .debug_abbrev.dwo must
11318 be the second entry. Other members of the set may follow in any order.
11319
11320 ---
11321
11322 DWP Version 2:
11323
11324 DWP Version 2 combines all the .debug_info, etc. sections into one,
11325 and the entries in the index tables are now offsets into these sections.
11326 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11327 section.
11328
11329 Index Section Contents:
11330 Header
11331 Hash Table of Signatures dwp_hash_table.hash_table
11332 Parallel Table of Indices dwp_hash_table.unit_table
11333 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11334 Table of Section Sizes dwp_hash_table.v2.sizes
11335
11336 The index section header consists of:
11337
11338 V, 32 bit version number
11339 L, 32 bit number of columns in the table of section offsets
11340 N, 32 bit number of compilation units or type units in the index
11341 M, 32 bit number of slots in the hash table
11342
11343 Numbers are recorded using the byte order of the application binary.
11344
11345 The hash table has the same format as version 1.
11346 The parallel table of indices has the same format as version 1,
11347 except that the entries are origin-1 indices into the table of sections
11348 offsets and the table of section sizes.
11349
11350 The table of offsets begins immediately following the parallel table
11351 (at offset 16 + 12 * M from the beginning of the section). The table is
11352 a two-dimensional array of 32-bit words (using the byte order of the
11353 application binary), with L columns and N+1 rows, in row-major order.
11354 Each row in the array is indexed starting from 0. The first row provides
11355 a key to the remaining rows: each column in this row provides an identifier
11356 for a debug section, and the offsets in the same column of subsequent rows
11357 refer to that section. The section identifiers are:
11358
11359 DW_SECT_INFO 1 .debug_info.dwo
11360 DW_SECT_TYPES 2 .debug_types.dwo
11361 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11362 DW_SECT_LINE 4 .debug_line.dwo
11363 DW_SECT_LOC 5 .debug_loc.dwo
11364 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11365 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11366 DW_SECT_MACRO 8 .debug_macro.dwo
11367
11368 The offsets provided by the CU and TU index sections are the base offsets
11369 for the contributions made by each CU or TU to the corresponding section
11370 in the package file. Each CU and TU header contains an abbrev_offset
11371 field, used to find the abbreviations table for that CU or TU within the
11372 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11373 be interpreted as relative to the base offset given in the index section.
11374 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11375 should be interpreted as relative to the base offset for .debug_line.dwo,
11376 and offsets into other debug sections obtained from DWARF attributes should
11377 also be interpreted as relative to the corresponding base offset.
11378
11379 The table of sizes begins immediately following the table of offsets.
11380 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11381 with L columns and N rows, in row-major order. Each row in the array is
11382 indexed starting from 1 (row 0 is shared by the two tables).
11383
11384 ---
11385
11386 Hash table lookup is handled the same in version 1 and 2:
11387
11388 We assume that N and M will not exceed 2^32 - 1.
11389 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11390
d2415c6c
DE
11391 Given a 64-bit compilation unit signature or a type signature S, an entry
11392 in the hash table is located as follows:
80626a55 11393
d2415c6c
DE
11394 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11395 the low-order k bits all set to 1.
80626a55 11396
d2415c6c 11397 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11398
d2415c6c
DE
11399 3) If the hash table entry at index H matches the signature, use that
11400 entry. If the hash table entry at index H is unused (all zeroes),
11401 terminate the search: the signature is not present in the table.
80626a55 11402
d2415c6c 11403 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11404
d2415c6c 11405 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11406 to stop at an unused slot or find the match. */
80626a55
DE
11407
11408/* Create a hash table to map DWO IDs to their CU/TU entry in
11409 .debug_{info,types}.dwo in DWP_FILE.
11410 Returns NULL if there isn't one.
11411 Note: This function processes DWP files only, not DWO files. */
11412
11413static struct dwp_hash_table *
ed2dc618
SM
11414create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11415 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11416{
11417 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11418 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11419 const gdb_byte *index_ptr, *index_end;
80626a55 11420 struct dwarf2_section_info *index;
73869dc2 11421 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11422 struct dwp_hash_table *htab;
11423
11424 if (is_debug_types)
11425 index = &dwp_file->sections.tu_index;
11426 else
11427 index = &dwp_file->sections.cu_index;
11428
96b79293 11429 if (index->empty ())
80626a55 11430 return NULL;
96b79293 11431 index->read (objfile);
80626a55
DE
11432
11433 index_ptr = index->buffer;
11434 index_end = index_ptr + index->size;
11435
11436 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11437 index_ptr += 4;
11438 if (version == 2)
11439 nr_columns = read_4_bytes (dbfd, index_ptr);
11440 else
11441 nr_columns = 0;
11442 index_ptr += 4;
80626a55
DE
11443 nr_units = read_4_bytes (dbfd, index_ptr);
11444 index_ptr += 4;
11445 nr_slots = read_4_bytes (dbfd, index_ptr);
11446 index_ptr += 4;
11447
73869dc2 11448 if (version != 1 && version != 2)
80626a55 11449 {
21aa081e 11450 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11451 " [in module %s]"),
21aa081e 11452 pulongest (version), dwp_file->name);
80626a55
DE
11453 }
11454 if (nr_slots != (nr_slots & -nr_slots))
11455 {
21aa081e 11456 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11457 " is not power of 2 [in module %s]"),
21aa081e 11458 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11459 }
11460
11461 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11462 htab->version = version;
11463 htab->nr_columns = nr_columns;
80626a55
DE
11464 htab->nr_units = nr_units;
11465 htab->nr_slots = nr_slots;
11466 htab->hash_table = index_ptr;
11467 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11468
11469 /* Exit early if the table is empty. */
11470 if (nr_slots == 0 || nr_units == 0
11471 || (version == 2 && nr_columns == 0))
11472 {
11473 /* All must be zero. */
11474 if (nr_slots != 0 || nr_units != 0
11475 || (version == 2 && nr_columns != 0))
11476 {
b98664d3 11477 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11478 " all zero [in modules %s]"),
11479 dwp_file->name);
11480 }
11481 return htab;
11482 }
11483
11484 if (version == 1)
11485 {
11486 htab->section_pool.v1.indices =
11487 htab->unit_table + sizeof (uint32_t) * nr_slots;
11488 /* It's harder to decide whether the section is too small in v1.
11489 V1 is deprecated anyway so we punt. */
11490 }
11491 else
11492 {
11493 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11494 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11495 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11496 /* Reverse map for error checking. */
11497 int ids_seen[DW_SECT_MAX + 1];
11498 int i;
11499
11500 if (nr_columns < 2)
11501 {
11502 error (_("Dwarf Error: bad DWP hash table, too few columns"
11503 " in section table [in module %s]"),
11504 dwp_file->name);
11505 }
11506 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11507 {
11508 error (_("Dwarf Error: bad DWP hash table, too many columns"
11509 " in section table [in module %s]"),
11510 dwp_file->name);
11511 }
04fd5eed
GB
11512 memset (ids, 255, sizeof_ids);
11513 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11514 for (i = 0; i < nr_columns; ++i)
11515 {
11516 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11517
11518 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11519 {
11520 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11521 " in section table [in module %s]"),
11522 id, dwp_file->name);
11523 }
11524 if (ids_seen[id] != -1)
11525 {
11526 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11527 " id %d in section table [in module %s]"),
11528 id, dwp_file->name);
11529 }
11530 ids_seen[id] = i;
11531 ids[i] = id;
11532 }
11533 /* Must have exactly one info or types section. */
11534 if (((ids_seen[DW_SECT_INFO] != -1)
11535 + (ids_seen[DW_SECT_TYPES] != -1))
11536 != 1)
11537 {
11538 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11539 " DWO info/types section [in module %s]"),
11540 dwp_file->name);
11541 }
11542 /* Must have an abbrev section. */
11543 if (ids_seen[DW_SECT_ABBREV] == -1)
11544 {
11545 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11546 " section [in module %s]"),
11547 dwp_file->name);
11548 }
11549 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11550 htab->section_pool.v2.sizes =
11551 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11552 * nr_units * nr_columns);
11553 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11554 * nr_units * nr_columns))
11555 > index_end)
11556 {
11557 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11558 " [in module %s]"),
11559 dwp_file->name);
11560 }
11561 }
80626a55
DE
11562
11563 return htab;
11564}
11565
11566/* Update SECTIONS with the data from SECTP.
11567
11568 This function is like the other "locate" section routines that are
11569 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11570 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11571
11572 The result is non-zero for success, or zero if an error was found. */
11573
11574static int
73869dc2
DE
11575locate_v1_virtual_dwo_sections (asection *sectp,
11576 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11577{
11578 const struct dwop_section_names *names = &dwop_section_names;
11579
11580 if (section_is_p (sectp->name, &names->abbrev_dwo))
11581 {
11582 /* There can be only one. */
049412e3 11583 if (sections->abbrev.s.section != NULL)
80626a55 11584 return 0;
049412e3 11585 sections->abbrev.s.section = sectp;
fd361982 11586 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11587 }
11588 else if (section_is_p (sectp->name, &names->info_dwo)
11589 || section_is_p (sectp->name, &names->types_dwo))
11590 {
11591 /* There can be only one. */
049412e3 11592 if (sections->info_or_types.s.section != NULL)
80626a55 11593 return 0;
049412e3 11594 sections->info_or_types.s.section = sectp;
fd361982 11595 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11596 }
11597 else if (section_is_p (sectp->name, &names->line_dwo))
11598 {
11599 /* There can be only one. */
049412e3 11600 if (sections->line.s.section != NULL)
80626a55 11601 return 0;
049412e3 11602 sections->line.s.section = sectp;
fd361982 11603 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11604 }
11605 else if (section_is_p (sectp->name, &names->loc_dwo))
11606 {
11607 /* There can be only one. */
049412e3 11608 if (sections->loc.s.section != NULL)
80626a55 11609 return 0;
049412e3 11610 sections->loc.s.section = sectp;
fd361982 11611 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11612 }
11613 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11614 {
11615 /* There can be only one. */
049412e3 11616 if (sections->macinfo.s.section != NULL)
80626a55 11617 return 0;
049412e3 11618 sections->macinfo.s.section = sectp;
fd361982 11619 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11620 }
11621 else if (section_is_p (sectp->name, &names->macro_dwo))
11622 {
11623 /* There can be only one. */
049412e3 11624 if (sections->macro.s.section != NULL)
80626a55 11625 return 0;
049412e3 11626 sections->macro.s.section = sectp;
fd361982 11627 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11628 }
11629 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11630 {
11631 /* There can be only one. */
049412e3 11632 if (sections->str_offsets.s.section != NULL)
80626a55 11633 return 0;
049412e3 11634 sections->str_offsets.s.section = sectp;
fd361982 11635 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11636 }
11637 else
11638 {
11639 /* No other kind of section is valid. */
11640 return 0;
11641 }
11642
11643 return 1;
11644}
11645
73869dc2
DE
11646/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11647 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11648 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11649 This is for DWP version 1 files. */
80626a55
DE
11650
11651static struct dwo_unit *
ed2dc618
SM
11652create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11653 struct dwp_file *dwp_file,
73869dc2
DE
11654 uint32_t unit_index,
11655 const char *comp_dir,
11656 ULONGEST signature, int is_debug_types)
80626a55
DE
11657{
11658 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11659 const struct dwp_hash_table *dwp_htab =
11660 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11661 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11662 const char *kind = is_debug_types ? "TU" : "CU";
11663 struct dwo_file *dwo_file;
11664 struct dwo_unit *dwo_unit;
73869dc2 11665 struct virtual_v1_dwo_sections sections;
80626a55 11666 void **dwo_file_slot;
80626a55
DE
11667 int i;
11668
73869dc2
DE
11669 gdb_assert (dwp_file->version == 1);
11670
b4f54984 11671 if (dwarf_read_debug)
80626a55 11672 {
73869dc2 11673 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11674 kind,
73869dc2 11675 pulongest (unit_index), hex_string (signature),
80626a55
DE
11676 dwp_file->name);
11677 }
11678
19ac8c2e 11679 /* Fetch the sections of this DWO unit.
80626a55
DE
11680 Put a limit on the number of sections we look for so that bad data
11681 doesn't cause us to loop forever. */
11682
73869dc2 11683#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11684 (1 /* .debug_info or .debug_types */ \
11685 + 1 /* .debug_abbrev */ \
11686 + 1 /* .debug_line */ \
11687 + 1 /* .debug_loc */ \
11688 + 1 /* .debug_str_offsets */ \
19ac8c2e 11689 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11690 + 1 /* trailing zero */)
11691
11692 memset (&sections, 0, sizeof (sections));
80626a55 11693
73869dc2 11694 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11695 {
11696 asection *sectp;
11697 uint32_t section_nr =
11698 read_4_bytes (dbfd,
73869dc2
DE
11699 dwp_htab->section_pool.v1.indices
11700 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11701
11702 if (section_nr == 0)
11703 break;
11704 if (section_nr >= dwp_file->num_sections)
11705 {
11706 error (_("Dwarf Error: bad DWP hash table, section number too large"
11707 " [in module %s]"),
11708 dwp_file->name);
11709 }
11710
11711 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11712 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11713 {
11714 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11715 " [in module %s]"),
11716 dwp_file->name);
11717 }
11718 }
11719
11720 if (i < 2
96b79293
TT
11721 || sections.info_or_types.empty ()
11722 || sections.abbrev.empty ())
80626a55
DE
11723 {
11724 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11725 " [in module %s]"),
11726 dwp_file->name);
11727 }
73869dc2 11728 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11729 {
11730 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11731 " [in module %s]"),
11732 dwp_file->name);
11733 }
11734
11735 /* It's easier for the rest of the code if we fake a struct dwo_file and
11736 have dwo_unit "live" in that. At least for now.
11737
11738 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11739 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11740 file, we can combine them back into a virtual DWO file to save space
11741 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11742 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11743
791afaa2
TT
11744 std::string virtual_dwo_name =
11745 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11746 sections.abbrev.get_id (),
11747 sections.line.get_id (),
11748 sections.loc.get_id (),
11749 sections.str_offsets.get_id ());
80626a55 11750 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11751 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11752 virtual_dwo_name.c_str (),
11753 comp_dir);
80626a55
DE
11754 /* Create one if necessary. */
11755 if (*dwo_file_slot == NULL)
11756 {
b4f54984 11757 if (dwarf_read_debug)
80626a55
DE
11758 {
11759 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11760 virtual_dwo_name.c_str ());
80626a55 11761 }
51ac9db5 11762 dwo_file = new struct dwo_file;
be1e3d3e 11763 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
0ac5b59e 11764 dwo_file->comp_dir = comp_dir;
80626a55
DE
11765 dwo_file->sections.abbrev = sections.abbrev;
11766 dwo_file->sections.line = sections.line;
11767 dwo_file->sections.loc = sections.loc;
11768 dwo_file->sections.macinfo = sections.macinfo;
11769 dwo_file->sections.macro = sections.macro;
11770 dwo_file->sections.str_offsets = sections.str_offsets;
11771 /* The "str" section is global to the entire DWP file. */
11772 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11773 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11774 there's no need to record it in dwo_file.
11775 Also, we can't simply record type sections in dwo_file because
11776 we record a pointer into the vector in dwo_unit. As we collect more
11777 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11778 for it, invalidating all copies of pointers into the previous
11779 contents. */
80626a55
DE
11780 *dwo_file_slot = dwo_file;
11781 }
11782 else
11783 {
b4f54984 11784 if (dwarf_read_debug)
80626a55
DE
11785 {
11786 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11787 virtual_dwo_name.c_str ());
80626a55 11788 }
9a3c8263 11789 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11790 }
80626a55
DE
11791
11792 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11793 dwo_unit->dwo_file = dwo_file;
11794 dwo_unit->signature = signature;
8d749320
SM
11795 dwo_unit->section =
11796 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11797 *dwo_unit->section = sections.info_or_types;
57d63ce2 11798 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11799
11800 return dwo_unit;
11801}
11802
73869dc2
DE
11803/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11804 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11805 piece within that section used by a TU/CU, return a virtual section
11806 of just that piece. */
11807
11808static struct dwarf2_section_info
ed2dc618
SM
11809create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11810 struct dwarf2_section_info *section,
73869dc2
DE
11811 bfd_size_type offset, bfd_size_type size)
11812{
11813 struct dwarf2_section_info result;
11814 asection *sectp;
11815
11816 gdb_assert (section != NULL);
11817 gdb_assert (!section->is_virtual);
11818
11819 memset (&result, 0, sizeof (result));
11820 result.s.containing_section = section;
dc4ccb6f 11821 result.is_virtual = true;
73869dc2
DE
11822
11823 if (size == 0)
11824 return result;
11825
96b79293 11826 sectp = section->get_bfd_section ();
73869dc2
DE
11827
11828 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11829 bounds of the real section. This is a pretty-rare event, so just
11830 flag an error (easier) instead of a warning and trying to cope. */
11831 if (sectp == NULL
fd361982 11832 || offset + size > bfd_section_size (sectp))
73869dc2 11833 {
73869dc2
DE
11834 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11835 " in section %s [in module %s]"),
fd361982 11836 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11837 objfile_name (dwarf2_per_objfile->objfile));
11838 }
11839
11840 result.virtual_offset = offset;
11841 result.size = size;
11842 return result;
11843}
11844
11845/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11846 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11847 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11848 This is for DWP version 2 files. */
11849
11850static struct dwo_unit *
ed2dc618
SM
11851create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11852 struct dwp_file *dwp_file,
73869dc2
DE
11853 uint32_t unit_index,
11854 const char *comp_dir,
11855 ULONGEST signature, int is_debug_types)
11856{
11857 struct objfile *objfile = dwarf2_per_objfile->objfile;
11858 const struct dwp_hash_table *dwp_htab =
11859 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11860 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11861 const char *kind = is_debug_types ? "TU" : "CU";
11862 struct dwo_file *dwo_file;
11863 struct dwo_unit *dwo_unit;
11864 struct virtual_v2_dwo_sections sections;
11865 void **dwo_file_slot;
73869dc2
DE
11866 int i;
11867
11868 gdb_assert (dwp_file->version == 2);
11869
b4f54984 11870 if (dwarf_read_debug)
73869dc2
DE
11871 {
11872 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11873 kind,
11874 pulongest (unit_index), hex_string (signature),
11875 dwp_file->name);
11876 }
11877
11878 /* Fetch the section offsets of this DWO unit. */
11879
11880 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11881
11882 for (i = 0; i < dwp_htab->nr_columns; ++i)
11883 {
11884 uint32_t offset = read_4_bytes (dbfd,
11885 dwp_htab->section_pool.v2.offsets
11886 + (((unit_index - 1) * dwp_htab->nr_columns
11887 + i)
11888 * sizeof (uint32_t)));
11889 uint32_t size = read_4_bytes (dbfd,
11890 dwp_htab->section_pool.v2.sizes
11891 + (((unit_index - 1) * dwp_htab->nr_columns
11892 + i)
11893 * sizeof (uint32_t)));
11894
11895 switch (dwp_htab->section_pool.v2.section_ids[i])
11896 {
11897 case DW_SECT_INFO:
11898 case DW_SECT_TYPES:
11899 sections.info_or_types_offset = offset;
11900 sections.info_or_types_size = size;
11901 break;
11902 case DW_SECT_ABBREV:
11903 sections.abbrev_offset = offset;
11904 sections.abbrev_size = size;
11905 break;
11906 case DW_SECT_LINE:
11907 sections.line_offset = offset;
11908 sections.line_size = size;
11909 break;
11910 case DW_SECT_LOC:
11911 sections.loc_offset = offset;
11912 sections.loc_size = size;
11913 break;
11914 case DW_SECT_STR_OFFSETS:
11915 sections.str_offsets_offset = offset;
11916 sections.str_offsets_size = size;
11917 break;
11918 case DW_SECT_MACINFO:
11919 sections.macinfo_offset = offset;
11920 sections.macinfo_size = size;
11921 break;
11922 case DW_SECT_MACRO:
11923 sections.macro_offset = offset;
11924 sections.macro_size = size;
11925 break;
11926 }
11927 }
11928
11929 /* It's easier for the rest of the code if we fake a struct dwo_file and
11930 have dwo_unit "live" in that. At least for now.
11931
11932 The DWP file can be made up of a random collection of CUs and TUs.
11933 However, for each CU + set of TUs that came from the same original DWO
11934 file, we can combine them back into a virtual DWO file to save space
11935 (fewer struct dwo_file objects to allocate). Remember that for really
11936 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11937
791afaa2
TT
11938 std::string virtual_dwo_name =
11939 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11940 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11941 (long) (sections.line_size ? sections.line_offset : 0),
11942 (long) (sections.loc_size ? sections.loc_offset : 0),
11943 (long) (sections.str_offsets_size
11944 ? sections.str_offsets_offset : 0));
73869dc2 11945 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11946 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11947 virtual_dwo_name.c_str (),
11948 comp_dir);
73869dc2
DE
11949 /* Create one if necessary. */
11950 if (*dwo_file_slot == NULL)
11951 {
b4f54984 11952 if (dwarf_read_debug)
73869dc2
DE
11953 {
11954 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11955 virtual_dwo_name.c_str ());
73869dc2 11956 }
51ac9db5 11957 dwo_file = new struct dwo_file;
be1e3d3e 11958 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
73869dc2
DE
11959 dwo_file->comp_dir = comp_dir;
11960 dwo_file->sections.abbrev =
ed2dc618 11961 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11962 sections.abbrev_offset, sections.abbrev_size);
11963 dwo_file->sections.line =
ed2dc618 11964 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11965 sections.line_offset, sections.line_size);
11966 dwo_file->sections.loc =
ed2dc618 11967 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11968 sections.loc_offset, sections.loc_size);
11969 dwo_file->sections.macinfo =
ed2dc618 11970 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11971 sections.macinfo_offset, sections.macinfo_size);
11972 dwo_file->sections.macro =
ed2dc618 11973 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11974 sections.macro_offset, sections.macro_size);
11975 dwo_file->sections.str_offsets =
ed2dc618
SM
11976 create_dwp_v2_section (dwarf2_per_objfile,
11977 &dwp_file->sections.str_offsets,
73869dc2
DE
11978 sections.str_offsets_offset,
11979 sections.str_offsets_size);
11980 /* The "str" section is global to the entire DWP file. */
11981 dwo_file->sections.str = dwp_file->sections.str;
11982 /* The info or types section is assigned below to dwo_unit,
11983 there's no need to record it in dwo_file.
11984 Also, we can't simply record type sections in dwo_file because
11985 we record a pointer into the vector in dwo_unit. As we collect more
11986 types we'll grow the vector and eventually have to reallocate space
11987 for it, invalidating all copies of pointers into the previous
11988 contents. */
11989 *dwo_file_slot = dwo_file;
11990 }
11991 else
11992 {
b4f54984 11993 if (dwarf_read_debug)
73869dc2
DE
11994 {
11995 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11996 virtual_dwo_name.c_str ());
73869dc2 11997 }
9a3c8263 11998 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11999 }
73869dc2
DE
12000
12001 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12002 dwo_unit->dwo_file = dwo_file;
12003 dwo_unit->signature = signature;
8d749320
SM
12004 dwo_unit->section =
12005 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12006 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12007 is_debug_types
73869dc2
DE
12008 ? &dwp_file->sections.types
12009 : &dwp_file->sections.info,
12010 sections.info_or_types_offset,
12011 sections.info_or_types_size);
12012 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12013
12014 return dwo_unit;
12015}
12016
57d63ce2
DE
12017/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12018 Returns NULL if the signature isn't found. */
80626a55
DE
12019
12020static struct dwo_unit *
ed2dc618
SM
12021lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12022 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12023 ULONGEST signature, int is_debug_types)
80626a55 12024{
57d63ce2
DE
12025 const struct dwp_hash_table *dwp_htab =
12026 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12027 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12028 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12029 uint32_t hash = signature & mask;
12030 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12031 unsigned int i;
12032 void **slot;
870f88f7 12033 struct dwo_unit find_dwo_cu;
80626a55
DE
12034
12035 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12036 find_dwo_cu.signature = signature;
19ac8c2e 12037 slot = htab_find_slot (is_debug_types
48b490f2
TT
12038 ? dwp_file->loaded_tus.get ()
12039 : dwp_file->loaded_cus.get (),
19ac8c2e 12040 &find_dwo_cu, INSERT);
80626a55
DE
12041
12042 if (*slot != NULL)
9a3c8263 12043 return (struct dwo_unit *) *slot;
80626a55
DE
12044
12045 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12046 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12047 {
12048 ULONGEST signature_in_table;
12049
12050 signature_in_table =
57d63ce2 12051 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12052 if (signature_in_table == signature)
12053 {
57d63ce2
DE
12054 uint32_t unit_index =
12055 read_4_bytes (dbfd,
12056 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12057
73869dc2
DE
12058 if (dwp_file->version == 1)
12059 {
ed2dc618
SM
12060 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12061 dwp_file, unit_index,
73869dc2
DE
12062 comp_dir, signature,
12063 is_debug_types);
12064 }
12065 else
12066 {
ed2dc618
SM
12067 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12068 dwp_file, unit_index,
73869dc2
DE
12069 comp_dir, signature,
12070 is_debug_types);
12071 }
9a3c8263 12072 return (struct dwo_unit *) *slot;
80626a55
DE
12073 }
12074 if (signature_in_table == 0)
12075 return NULL;
12076 hash = (hash + hash2) & mask;
12077 }
12078
12079 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12080 " [in module %s]"),
12081 dwp_file->name);
12082}
12083
ab5088bf 12084/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12085 Open the file specified by FILE_NAME and hand it off to BFD for
12086 preliminary analysis. Return a newly initialized bfd *, which
12087 includes a canonicalized copy of FILE_NAME.
80626a55 12088 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12089 SEARCH_CWD is true if the current directory is to be searched.
12090 It will be searched before debug-file-directory.
13aaf454
DE
12091 If successful, the file is added to the bfd include table of the
12092 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12093 If unable to find/open the file, return NULL.
3019eac3
DE
12094 NOTE: This function is derived from symfile_bfd_open. */
12095
192b62ce 12096static gdb_bfd_ref_ptr
ed2dc618
SM
12097try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12098 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12099{
24b9144d 12100 int desc;
9c02c129
DE
12101 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12102 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12103 to debug_file_directory. */
e0cc99a6 12104 const char *search_path;
9c02c129
DE
12105 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12106
e0cc99a6 12107 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12108 if (search_cwd)
12109 {
12110 if (*debug_file_directory != '\0')
e0cc99a6
TT
12111 {
12112 search_path_holder.reset (concat (".", dirname_separator_string,
12113 debug_file_directory,
12114 (char *) NULL));
12115 search_path = search_path_holder.get ();
12116 }
6ac97d4c 12117 else
e0cc99a6 12118 search_path = ".";
6ac97d4c 12119 }
9c02c129 12120 else
e0cc99a6 12121 search_path = debug_file_directory;
3019eac3 12122
24b9144d 12123 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12124 if (is_dwp)
12125 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12126
12127 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12128 desc = openp (search_path, flags, file_name,
3019eac3
DE
12129 O_RDONLY | O_BINARY, &absolute_name);
12130 if (desc < 0)
12131 return NULL;
12132
e0cc99a6
TT
12133 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12134 gnutarget, desc));
9c02c129
DE
12135 if (sym_bfd == NULL)
12136 return NULL;
192b62ce 12137 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12138
192b62ce
TT
12139 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12140 return NULL;
3019eac3 12141
13aaf454
DE
12142 /* Success. Record the bfd as having been included by the objfile's bfd.
12143 This is important because things like demangled_names_hash lives in the
12144 objfile's per_bfd space and may have references to things like symbol
12145 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12146 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12147
3019eac3
DE
12148 return sym_bfd;
12149}
12150
ab5088bf 12151/* Try to open DWO file FILE_NAME.
3019eac3
DE
12152 COMP_DIR is the DW_AT_comp_dir attribute.
12153 The result is the bfd handle of the file.
12154 If there is a problem finding or opening the file, return NULL.
12155 Upon success, the canonicalized path of the file is stored in the bfd,
12156 same as symfile_bfd_open. */
12157
192b62ce 12158static gdb_bfd_ref_ptr
ed2dc618
SM
12159open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12160 const char *file_name, const char *comp_dir)
3019eac3 12161{
80626a55 12162 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12163 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12164 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12165
12166 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12167
12168 if (comp_dir != NULL)
12169 {
43816ebc
TT
12170 gdb::unique_xmalloc_ptr<char> path_to_try
12171 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12172
12173 /* NOTE: If comp_dir is a relative path, this will also try the
12174 search path, which seems useful. */
ed2dc618 12175 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12176 path_to_try.get (),
ed2dc618 12177 0 /*is_dwp*/,
192b62ce 12178 1 /*search_cwd*/));
3019eac3
DE
12179 if (abfd != NULL)
12180 return abfd;
12181 }
12182
12183 /* That didn't work, try debug-file-directory, which, despite its name,
12184 is a list of paths. */
12185
12186 if (*debug_file_directory == '\0')
12187 return NULL;
12188
ed2dc618
SM
12189 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12190 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12191}
12192
80626a55
DE
12193/* This function is mapped across the sections and remembers the offset and
12194 size of each of the DWO debugging sections we are interested in. */
12195
12196static void
12197dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12198{
9a3c8263 12199 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12200 const struct dwop_section_names *names = &dwop_section_names;
12201
12202 if (section_is_p (sectp->name, &names->abbrev_dwo))
12203 {
049412e3 12204 dwo_sections->abbrev.s.section = sectp;
fd361982 12205 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12206 }
12207 else if (section_is_p (sectp->name, &names->info_dwo))
12208 {
049412e3 12209 dwo_sections->info.s.section = sectp;
fd361982 12210 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12211 }
12212 else if (section_is_p (sectp->name, &names->line_dwo))
12213 {
049412e3 12214 dwo_sections->line.s.section = sectp;
fd361982 12215 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12216 }
12217 else if (section_is_p (sectp->name, &names->loc_dwo))
12218 {
049412e3 12219 dwo_sections->loc.s.section = sectp;
fd361982 12220 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12221 }
41144253 12222 else if (section_is_p (sectp->name, &names->loclists_dwo))
12223 {
12224 dwo_sections->loclists.s.section = sectp;
12225 dwo_sections->loclists.size = bfd_section_size (sectp);
12226 }
80626a55
DE
12227 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12228 {
049412e3 12229 dwo_sections->macinfo.s.section = sectp;
fd361982 12230 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12231 }
12232 else if (section_is_p (sectp->name, &names->macro_dwo))
12233 {
049412e3 12234 dwo_sections->macro.s.section = sectp;
fd361982 12235 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12236 }
12237 else if (section_is_p (sectp->name, &names->str_dwo))
12238 {
049412e3 12239 dwo_sections->str.s.section = sectp;
fd361982 12240 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12241 }
12242 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12243 {
049412e3 12244 dwo_sections->str_offsets.s.section = sectp;
fd361982 12245 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12246 }
12247 else if (section_is_p (sectp->name, &names->types_dwo))
12248 {
12249 struct dwarf2_section_info type_section;
12250
12251 memset (&type_section, 0, sizeof (type_section));
049412e3 12252 type_section.s.section = sectp;
fd361982 12253 type_section.size = bfd_section_size (sectp);
fd5866f6 12254 dwo_sections->types.push_back (type_section);
80626a55
DE
12255 }
12256}
12257
ab5088bf 12258/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12259 by PER_CU. This is for the non-DWP case.
80626a55 12260 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12261
12262static struct dwo_file *
0ac5b59e
DE
12263open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12264 const char *dwo_name, const char *comp_dir)
3019eac3 12265{
ed2dc618 12266 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12267
fb1eb2f9 12268 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12269 if (dbfd == NULL)
12270 {
b4f54984 12271 if (dwarf_read_debug)
80626a55
DE
12272 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12273 return NULL;
12274 }
263db9a1 12275
51ac9db5 12276 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12277 dwo_file->dwo_name = dwo_name;
12278 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12279 dwo_file->dbfd = std::move (dbfd);
3019eac3 12280
fb1eb2f9 12281 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12282 &dwo_file->sections);
3019eac3 12283
18a8505e
AT
12284 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12285 dwo_file->sections.info, dwo_file->cus);
3019eac3 12286
263db9a1 12287 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12288 dwo_file->sections.types, dwo_file->tus);
3019eac3 12289
b4f54984 12290 if (dwarf_read_debug)
80626a55
DE
12291 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12292
263db9a1 12293 return dwo_file.release ();
3019eac3
DE
12294}
12295
80626a55 12296/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12297 size of each of the DWP debugging sections common to version 1 and 2 that
12298 we are interested in. */
3019eac3 12299
80626a55 12300static void
73869dc2
DE
12301dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12302 void *dwp_file_ptr)
3019eac3 12303{
9a3c8263 12304 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12305 const struct dwop_section_names *names = &dwop_section_names;
12306 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12307
80626a55 12308 /* Record the ELF section number for later lookup: this is what the
73869dc2 12309 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12310 gdb_assert (elf_section_nr < dwp_file->num_sections);
12311 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12312
80626a55
DE
12313 /* Look for specific sections that we need. */
12314 if (section_is_p (sectp->name, &names->str_dwo))
12315 {
049412e3 12316 dwp_file->sections.str.s.section = sectp;
fd361982 12317 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12318 }
12319 else if (section_is_p (sectp->name, &names->cu_index))
12320 {
049412e3 12321 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12322 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12323 }
12324 else if (section_is_p (sectp->name, &names->tu_index))
12325 {
049412e3 12326 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12327 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12328 }
12329}
3019eac3 12330
73869dc2
DE
12331/* This function is mapped across the sections and remembers the offset and
12332 size of each of the DWP version 2 debugging sections that we are interested
12333 in. This is split into a separate function because we don't know if we
12334 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12335
12336static void
12337dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12338{
9a3c8263 12339 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12340 const struct dwop_section_names *names = &dwop_section_names;
12341 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12342
12343 /* Record the ELF section number for later lookup: this is what the
12344 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12345 gdb_assert (elf_section_nr < dwp_file->num_sections);
12346 dwp_file->elf_sections[elf_section_nr] = sectp;
12347
12348 /* Look for specific sections that we need. */
12349 if (section_is_p (sectp->name, &names->abbrev_dwo))
12350 {
049412e3 12351 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12352 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12353 }
12354 else if (section_is_p (sectp->name, &names->info_dwo))
12355 {
049412e3 12356 dwp_file->sections.info.s.section = sectp;
fd361982 12357 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12358 }
12359 else if (section_is_p (sectp->name, &names->line_dwo))
12360 {
049412e3 12361 dwp_file->sections.line.s.section = sectp;
fd361982 12362 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12363 }
12364 else if (section_is_p (sectp->name, &names->loc_dwo))
12365 {
049412e3 12366 dwp_file->sections.loc.s.section = sectp;
fd361982 12367 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12368 }
12369 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12370 {
049412e3 12371 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12372 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12373 }
12374 else if (section_is_p (sectp->name, &names->macro_dwo))
12375 {
049412e3 12376 dwp_file->sections.macro.s.section = sectp;
fd361982 12377 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12378 }
12379 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12380 {
049412e3 12381 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12382 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12383 }
12384 else if (section_is_p (sectp->name, &names->types_dwo))
12385 {
049412e3 12386 dwp_file->sections.types.s.section = sectp;
fd361982 12387 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12388 }
12389}
12390
80626a55 12391/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12392
80626a55
DE
12393static hashval_t
12394hash_dwp_loaded_cutus (const void *item)
12395{
9a3c8263 12396 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12397
80626a55
DE
12398 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12399 return dwo_unit->signature;
3019eac3
DE
12400}
12401
80626a55 12402/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12403
80626a55
DE
12404static int
12405eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12406{
9a3c8263
SM
12407 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12408 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12409
80626a55
DE
12410 return dua->signature == dub->signature;
12411}
3019eac3 12412
80626a55 12413/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12414
48b490f2 12415static htab_up
298e9637 12416allocate_dwp_loaded_cutus_table ()
80626a55 12417{
48b490f2
TT
12418 return htab_up (htab_create_alloc (3,
12419 hash_dwp_loaded_cutus,
12420 eq_dwp_loaded_cutus,
12421 NULL, xcalloc, xfree));
80626a55 12422}
3019eac3 12423
ab5088bf
DE
12424/* Try to open DWP file FILE_NAME.
12425 The result is the bfd handle of the file.
12426 If there is a problem finding or opening the file, return NULL.
12427 Upon success, the canonicalized path of the file is stored in the bfd,
12428 same as symfile_bfd_open. */
12429
192b62ce 12430static gdb_bfd_ref_ptr
ed2dc618
SM
12431open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12432 const char *file_name)
ab5088bf 12433{
ed2dc618
SM
12434 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12435 1 /*is_dwp*/,
192b62ce 12436 1 /*search_cwd*/));
6ac97d4c
DE
12437 if (abfd != NULL)
12438 return abfd;
12439
12440 /* Work around upstream bug 15652.
12441 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12442 [Whether that's a "bug" is debatable, but it is getting in our way.]
12443 We have no real idea where the dwp file is, because gdb's realpath-ing
12444 of the executable's path may have discarded the needed info.
12445 [IWBN if the dwp file name was recorded in the executable, akin to
12446 .gnu_debuglink, but that doesn't exist yet.]
12447 Strip the directory from FILE_NAME and search again. */
12448 if (*debug_file_directory != '\0')
12449 {
12450 /* Don't implicitly search the current directory here.
12451 If the user wants to search "." to handle this case,
12452 it must be added to debug-file-directory. */
ed2dc618
SM
12453 return try_open_dwop_file (dwarf2_per_objfile,
12454 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12455 0 /*search_cwd*/);
12456 }
12457
12458 return NULL;
ab5088bf
DE
12459}
12460
80626a55
DE
12461/* Initialize the use of the DWP file for the current objfile.
12462 By convention the name of the DWP file is ${objfile}.dwp.
12463 The result is NULL if it can't be found. */
a766d390 12464
400174b1 12465static std::unique_ptr<struct dwp_file>
ed2dc618 12466open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12467{
12468 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12469
82bf32bc
JK
12470 /* Try to find first .dwp for the binary file before any symbolic links
12471 resolving. */
6c447423
DE
12472
12473 /* If the objfile is a debug file, find the name of the real binary
12474 file and get the name of dwp file from there. */
d721ba37 12475 std::string dwp_name;
6c447423
DE
12476 if (objfile->separate_debug_objfile_backlink != NULL)
12477 {
12478 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12479 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12480
d721ba37 12481 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12482 }
12483 else
d721ba37
PA
12484 dwp_name = objfile->original_name;
12485
12486 dwp_name += ".dwp";
80626a55 12487
ed2dc618 12488 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12489 if (dbfd == NULL
12490 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12491 {
12492 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12493 dwp_name = objfile_name (objfile);
12494 dwp_name += ".dwp";
ed2dc618 12495 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12496 }
12497
80626a55
DE
12498 if (dbfd == NULL)
12499 {
b4f54984 12500 if (dwarf_read_debug)
d721ba37 12501 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12502 return std::unique_ptr<dwp_file> ();
3019eac3 12503 }
400174b1
TT
12504
12505 const char *name = bfd_get_filename (dbfd.get ());
12506 std::unique_ptr<struct dwp_file> dwp_file
12507 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12508
0a0f4c01 12509 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12510 dwp_file->elf_sections =
12511 OBSTACK_CALLOC (&objfile->objfile_obstack,
12512 dwp_file->num_sections, asection *);
12513
400174b1
TT
12514 bfd_map_over_sections (dwp_file->dbfd.get (),
12515 dwarf2_locate_common_dwp_sections,
12516 dwp_file.get ());
80626a55 12517
400174b1
TT
12518 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12519 0);
80626a55 12520
400174b1
TT
12521 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12522 1);
80626a55 12523
73869dc2 12524 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12525 if (dwp_file->cus && dwp_file->tus
12526 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12527 {
12528 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12529 pretty bizarre. We use pulongest here because that's the established
4d65956b 12530 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12531 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12532 " TU version %s [in DWP file %s]"),
12533 pulongest (dwp_file->cus->version),
d721ba37 12534 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12535 }
08302ed2
DE
12536
12537 if (dwp_file->cus)
12538 dwp_file->version = dwp_file->cus->version;
12539 else if (dwp_file->tus)
12540 dwp_file->version = dwp_file->tus->version;
12541 else
12542 dwp_file->version = 2;
73869dc2
DE
12543
12544 if (dwp_file->version == 2)
400174b1
TT
12545 bfd_map_over_sections (dwp_file->dbfd.get (),
12546 dwarf2_locate_v2_dwp_sections,
12547 dwp_file.get ());
73869dc2 12548
298e9637
SM
12549 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12550 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12551
b4f54984 12552 if (dwarf_read_debug)
80626a55
DE
12553 {
12554 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12555 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12556 " %s CUs, %s TUs\n",
12557 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12558 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12559 }
12560
12561 return dwp_file;
3019eac3 12562}
c906108c 12563
ab5088bf
DE
12564/* Wrapper around open_and_init_dwp_file, only open it once. */
12565
12566static struct dwp_file *
ed2dc618 12567get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12568{
12569 if (! dwarf2_per_objfile->dwp_checked)
12570 {
ed2dc618
SM
12571 dwarf2_per_objfile->dwp_file
12572 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12573 dwarf2_per_objfile->dwp_checked = 1;
12574 }
400174b1 12575 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12576}
12577
80626a55
DE
12578/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12579 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12580 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12581 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12582 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12583
12584 This is called, for example, when wanting to read a variable with a
12585 complex location. Therefore we don't want to do file i/o for every call.
12586 Therefore we don't want to look for a DWO file on every call.
12587 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12588 then we check if we've already seen DWO_NAME, and only THEN do we check
12589 for a DWO file.
12590
1c658ad5 12591 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12592 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12593
3019eac3 12594static struct dwo_unit *
80626a55
DE
12595lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12596 const char *dwo_name, const char *comp_dir,
12597 ULONGEST signature, int is_debug_types)
3019eac3 12598{
ed2dc618 12599 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12600 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12601 const char *kind = is_debug_types ? "TU" : "CU";
12602 void **dwo_file_slot;
3019eac3 12603 struct dwo_file *dwo_file;
80626a55 12604 struct dwp_file *dwp_file;
cb1df416 12605
6a506a2d
DE
12606 /* First see if there's a DWP file.
12607 If we have a DWP file but didn't find the DWO inside it, don't
12608 look for the original DWO file. It makes gdb behave differently
12609 depending on whether one is debugging in the build tree. */
cf2c3c16 12610
ed2dc618 12611 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12612 if (dwp_file != NULL)
cf2c3c16 12613 {
80626a55
DE
12614 const struct dwp_hash_table *dwp_htab =
12615 is_debug_types ? dwp_file->tus : dwp_file->cus;
12616
12617 if (dwp_htab != NULL)
12618 {
12619 struct dwo_unit *dwo_cutu =
ed2dc618 12620 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12621 signature, is_debug_types);
80626a55
DE
12622
12623 if (dwo_cutu != NULL)
12624 {
b4f54984 12625 if (dwarf_read_debug)
80626a55
DE
12626 {
12627 fprintf_unfiltered (gdb_stdlog,
12628 "Virtual DWO %s %s found: @%s\n",
12629 kind, hex_string (signature),
12630 host_address_to_string (dwo_cutu));
12631 }
12632 return dwo_cutu;
12633 }
12634 }
12635 }
6a506a2d 12636 else
80626a55 12637 {
6a506a2d 12638 /* No DWP file, look for the DWO file. */
80626a55 12639
ed2dc618
SM
12640 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12641 dwo_name, comp_dir);
6a506a2d 12642 if (*dwo_file_slot == NULL)
80626a55 12643 {
6a506a2d
DE
12644 /* Read in the file and build a table of the CUs/TUs it contains. */
12645 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12646 }
6a506a2d 12647 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12648 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12649
6a506a2d 12650 if (dwo_file != NULL)
19c3d4c9 12651 {
6a506a2d
DE
12652 struct dwo_unit *dwo_cutu = NULL;
12653
12654 if (is_debug_types && dwo_file->tus)
12655 {
12656 struct dwo_unit find_dwo_cutu;
12657
12658 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12659 find_dwo_cutu.signature = signature;
9a3c8263 12660 dwo_cutu
b0b6a987
TT
12661 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12662 &find_dwo_cutu);
6a506a2d 12663 }
33c5cd75 12664 else if (!is_debug_types && dwo_file->cus)
80626a55 12665 {
33c5cd75
DB
12666 struct dwo_unit find_dwo_cutu;
12667
12668 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12669 find_dwo_cutu.signature = signature;
b0b6a987 12670 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12671 &find_dwo_cutu);
6a506a2d
DE
12672 }
12673
12674 if (dwo_cutu != NULL)
12675 {
b4f54984 12676 if (dwarf_read_debug)
6a506a2d
DE
12677 {
12678 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12679 kind, dwo_name, hex_string (signature),
12680 host_address_to_string (dwo_cutu));
12681 }
12682 return dwo_cutu;
80626a55
DE
12683 }
12684 }
2e276125 12685 }
9cdd5dbd 12686
80626a55
DE
12687 /* We didn't find it. This could mean a dwo_id mismatch, or
12688 someone deleted the DWO/DWP file, or the search path isn't set up
12689 correctly to find the file. */
12690
b4f54984 12691 if (dwarf_read_debug)
80626a55
DE
12692 {
12693 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12694 kind, dwo_name, hex_string (signature));
12695 }
3019eac3 12696
6656a72d
DE
12697 /* This is a warning and not a complaint because it can be caused by
12698 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12699 {
12700 /* Print the name of the DWP file if we looked there, helps the user
12701 better diagnose the problem. */
791afaa2 12702 std::string dwp_text;
43942612
DE
12703
12704 if (dwp_file != NULL)
791afaa2
TT
12705 dwp_text = string_printf (" [in DWP file %s]",
12706 lbasename (dwp_file->name));
43942612 12707
9d8780f0 12708 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12709 " [in module %s]"),
12710 kind, dwo_name, hex_string (signature),
791afaa2 12711 dwp_text.c_str (),
43942612 12712 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12713 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12714 }
3019eac3 12715 return NULL;
5fb290d7
DJ
12716}
12717
80626a55
DE
12718/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12719 See lookup_dwo_cutu_unit for details. */
12720
12721static struct dwo_unit *
12722lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12723 const char *dwo_name, const char *comp_dir,
12724 ULONGEST signature)
12725{
12726 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12727}
12728
12729/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12730 See lookup_dwo_cutu_unit for details. */
12731
12732static struct dwo_unit *
12733lookup_dwo_type_unit (struct signatured_type *this_tu,
12734 const char *dwo_name, const char *comp_dir)
12735{
12736 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12737}
12738
89e63ee4
DE
12739/* Traversal function for queue_and_load_all_dwo_tus. */
12740
12741static int
12742queue_and_load_dwo_tu (void **slot, void *info)
12743{
12744 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12745 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12746 ULONGEST signature = dwo_unit->signature;
12747 struct signatured_type *sig_type =
12748 lookup_dwo_signatured_type (per_cu->cu, signature);
12749
12750 if (sig_type != NULL)
12751 {
12752 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12753
12754 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12755 a real dependency of PER_CU on SIG_TYPE. That is detected later
12756 while processing PER_CU. */
12757 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12758 load_full_type_unit (sig_cu);
ae640021 12759 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12760 }
12761
12762 return 1;
12763}
12764
12765/* Queue all TUs contained in the DWO of PER_CU to be read in.
12766 The DWO may have the only definition of the type, though it may not be
12767 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12768 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12769
12770static void
12771queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12772{
12773 struct dwo_unit *dwo_unit;
12774 struct dwo_file *dwo_file;
12775
12776 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12777 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12778 gdb_assert (per_cu->cu != NULL);
12779
12780 dwo_unit = per_cu->cu->dwo_unit;
12781 gdb_assert (dwo_unit != NULL);
12782
12783 dwo_file = dwo_unit->dwo_file;
12784 if (dwo_file->tus != NULL)
b0b6a987
TT
12785 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12786 per_cu);
89e63ee4
DE
12787}
12788
3019eac3 12789/* Read in various DIEs. */
348e048f 12790
d389af10 12791/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12792 Inherit only the children of the DW_AT_abstract_origin DIE not being
12793 already referenced by DW_AT_abstract_origin from the children of the
12794 current DIE. */
d389af10
JK
12795
12796static void
12797inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12798{
12799 struct die_info *child_die;
791afaa2 12800 sect_offset *offsetp;
d389af10
JK
12801 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12802 struct die_info *origin_die;
12803 /* Iterator of the ORIGIN_DIE children. */
12804 struct die_info *origin_child_die;
d389af10 12805 struct attribute *attr;
cd02d79d
PA
12806 struct dwarf2_cu *origin_cu;
12807 struct pending **origin_previous_list_in_scope;
d389af10
JK
12808
12809 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12810 if (!attr)
12811 return;
12812
cd02d79d
PA
12813 /* Note that following die references may follow to a die in a
12814 different cu. */
12815
12816 origin_cu = cu;
12817 origin_die = follow_die_ref (die, attr, &origin_cu);
12818
12819 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12820 symbols in. */
12821 origin_previous_list_in_scope = origin_cu->list_in_scope;
12822 origin_cu->list_in_scope = cu->list_in_scope;
12823
edb3359d
DJ
12824 if (die->tag != origin_die->tag
12825 && !(die->tag == DW_TAG_inlined_subroutine
12826 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12827 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12828 sect_offset_str (die->sect_off),
12829 sect_offset_str (origin_die->sect_off));
d389af10 12830
791afaa2 12831 std::vector<sect_offset> offsets;
d389af10 12832
3ea89b92
PMR
12833 for (child_die = die->child;
12834 child_die && child_die->tag;
436c571c 12835 child_die = child_die->sibling)
3ea89b92
PMR
12836 {
12837 struct die_info *child_origin_die;
12838 struct dwarf2_cu *child_origin_cu;
12839
12840 /* We are trying to process concrete instance entries:
216f72a1 12841 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12842 it's not relevant to our analysis here. i.e. detecting DIEs that are
12843 present in the abstract instance but not referenced in the concrete
12844 one. */
216f72a1
JK
12845 if (child_die->tag == DW_TAG_call_site
12846 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12847 continue;
12848
c38f313d
DJ
12849 /* For each CHILD_DIE, find the corresponding child of
12850 ORIGIN_DIE. If there is more than one layer of
12851 DW_AT_abstract_origin, follow them all; there shouldn't be,
12852 but GCC versions at least through 4.4 generate this (GCC PR
12853 40573). */
3ea89b92
PMR
12854 child_origin_die = child_die;
12855 child_origin_cu = cu;
c38f313d
DJ
12856 while (1)
12857 {
cd02d79d
PA
12858 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12859 child_origin_cu);
c38f313d
DJ
12860 if (attr == NULL)
12861 break;
cd02d79d
PA
12862 child_origin_die = follow_die_ref (child_origin_die, attr,
12863 &child_origin_cu);
c38f313d
DJ
12864 }
12865
d389af10
JK
12866 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12867 counterpart may exist. */
c38f313d 12868 if (child_origin_die != child_die)
d389af10 12869 {
edb3359d
DJ
12870 if (child_die->tag != child_origin_die->tag
12871 && !(child_die->tag == DW_TAG_inlined_subroutine
12872 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12873 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12874 "different tags"),
9d8780f0
SM
12875 sect_offset_str (child_die->sect_off),
12876 sect_offset_str (child_origin_die->sect_off));
c38f313d 12877 if (child_origin_die->parent != origin_die)
b98664d3 12878 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12879 "different parents"),
9d8780f0
SM
12880 sect_offset_str (child_die->sect_off),
12881 sect_offset_str (child_origin_die->sect_off));
c38f313d 12882 else
791afaa2 12883 offsets.push_back (child_origin_die->sect_off);
d389af10 12884 }
d389af10 12885 }
791afaa2
TT
12886 std::sort (offsets.begin (), offsets.end ());
12887 sect_offset *offsets_end = offsets.data () + offsets.size ();
12888 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12889 if (offsetp[-1] == *offsetp)
b98664d3 12890 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12891 "to DIE %s as their abstract origin"),
12892 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12893
791afaa2 12894 offsetp = offsets.data ();
d389af10
JK
12895 origin_child_die = origin_die->child;
12896 while (origin_child_die && origin_child_die->tag)
12897 {
12898 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12899 while (offsetp < offsets_end
9c541725 12900 && *offsetp < origin_child_die->sect_off)
d389af10 12901 offsetp++;
b64f50a1 12902 if (offsetp >= offsets_end
9c541725 12903 || *offsetp > origin_child_die->sect_off)
d389af10 12904 {
adde2bff
DE
12905 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12906 Check whether we're already processing ORIGIN_CHILD_DIE.
12907 This can happen with mutually referenced abstract_origins.
12908 PR 16581. */
12909 if (!origin_child_die->in_process)
12910 process_die (origin_child_die, origin_cu);
d389af10 12911 }
436c571c 12912 origin_child_die = origin_child_die->sibling;
d389af10 12913 }
cd02d79d 12914 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12915
12916 if (cu != origin_cu)
12917 compute_delayed_physnames (origin_cu);
d389af10
JK
12918}
12919
c906108c 12920static void
e7c27a73 12921read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12922{
518817b3 12923 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 12924 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12925 struct context_stack *newobj;
c906108c
SS
12926 CORE_ADDR lowpc;
12927 CORE_ADDR highpc;
12928 struct die_info *child_die;
edb3359d 12929 struct attribute *attr, *call_line, *call_file;
15d034d0 12930 const char *name;
e142c38c 12931 CORE_ADDR baseaddr;
801e3a5b 12932 struct block *block;
edb3359d 12933 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12934 std::vector<struct symbol *> template_args;
34eaf542 12935 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12936
12937 if (inlined_func)
12938 {
12939 /* If we do not have call site information, we can't show the
12940 caller of this inlined function. That's too confusing, so
12941 only use the scope for local variables. */
12942 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12943 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12944 if (call_line == NULL || call_file == NULL)
12945 {
12946 read_lexical_block_scope (die, cu);
12947 return;
12948 }
12949 }
c906108c 12950
b3b3bada 12951 baseaddr = objfile->text_section_offset ();
e142c38c 12952
94af9270 12953 name = dwarf2_name (die, cu);
c906108c 12954
e8d05480
JB
12955 /* Ignore functions with missing or empty names. These are actually
12956 illegal according to the DWARF standard. */
12957 if (name == NULL)
12958 {
b98664d3 12959 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12960 sect_offset_str (die->sect_off));
e8d05480
JB
12961 return;
12962 }
12963
12964 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12965 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12966 <= PC_BOUNDS_INVALID)
e8d05480 12967 {
ae4d0c03
PM
12968 attr = dwarf2_attr (die, DW_AT_external, cu);
12969 if (!attr || !DW_UNSND (attr))
b98664d3 12970 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12971 "for subprogram DIE at %s"),
12972 sect_offset_str (die->sect_off));
e8d05480
JB
12973 return;
12974 }
c906108c 12975
3e29f34a
MR
12976 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12977 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12978
34eaf542
TT
12979 /* If we have any template arguments, then we must allocate a
12980 different sort of symbol. */
436c571c 12981 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
12982 {
12983 if (child_die->tag == DW_TAG_template_type_param
12984 || child_die->tag == DW_TAG_template_value_param)
12985 {
8c14c3a3 12986 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 12987 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12988 break;
12989 }
12990 }
12991
c24bdb02 12992 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12993 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12994 (struct symbol *) templ_func);
4c2df51b 12995
81873cc8 12996 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12997 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12998 cu->language);
12999
4cecd739
DJ
13000 /* If there is a location expression for DW_AT_frame_base, record
13001 it. */
e142c38c 13002 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13003 if (attr != nullptr)
fe978cb0 13004 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13005
63e43d3a
PMR
13006 /* If there is a location for the static link, record it. */
13007 newobj->static_link = NULL;
13008 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13009 if (attr != nullptr)
63e43d3a 13010 {
224c3ddb
SM
13011 newobj->static_link
13012 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13013 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 13014 cu->per_cu->addr_type ());
63e43d3a
PMR
13015 }
13016
c24bdb02 13017 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13018
639d11d3 13019 if (die->child != NULL)
c906108c 13020 {
639d11d3 13021 child_die = die->child;
c906108c
SS
13022 while (child_die && child_die->tag)
13023 {
34eaf542
TT
13024 if (child_die->tag == DW_TAG_template_type_param
13025 || child_die->tag == DW_TAG_template_value_param)
13026 {
13027 struct symbol *arg = new_symbol (child_die, NULL, cu);
13028
f1078f66 13029 if (arg != NULL)
2f4732b0 13030 template_args.push_back (arg);
34eaf542
TT
13031 }
13032 else
13033 process_die (child_die, cu);
436c571c 13034 child_die = child_die->sibling;
c906108c
SS
13035 }
13036 }
13037
d389af10
JK
13038 inherit_abstract_dies (die, cu);
13039
4a811a97
UW
13040 /* If we have a DW_AT_specification, we might need to import using
13041 directives from the context of the specification DIE. See the
13042 comment in determine_prefix. */
13043 if (cu->language == language_cplus
13044 && dwarf2_attr (die, DW_AT_specification, cu))
13045 {
13046 struct dwarf2_cu *spec_cu = cu;
13047 struct die_info *spec_die = die_specification (die, &spec_cu);
13048
13049 while (spec_die)
13050 {
13051 child_die = spec_die->child;
13052 while (child_die && child_die->tag)
13053 {
13054 if (child_die->tag == DW_TAG_imported_module)
13055 process_die (child_die, spec_cu);
436c571c 13056 child_die = child_die->sibling;
4a811a97
UW
13057 }
13058
13059 /* In some cases, GCC generates specification DIEs that
13060 themselves contain DW_AT_specification attributes. */
13061 spec_die = die_specification (spec_die, &spec_cu);
13062 }
13063 }
13064
c24bdb02 13065 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13066 /* Make a block for the local symbols within. */
c24bdb02 13067 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13068 cstk.static_link, lowpc, highpc);
801e3a5b 13069
df8a16a1 13070 /* For C++, set the block's scope. */
45280282
IB
13071 if ((cu->language == language_cplus
13072 || cu->language == language_fortran
c44af4eb
TT
13073 || cu->language == language_d
13074 || cu->language == language_rust)
4d4ec4e5 13075 && cu->processing_has_namespace_info)
195a3f6c
TT
13076 block_set_scope (block, determine_prefix (die, cu),
13077 &objfile->objfile_obstack);
df8a16a1 13078
801e3a5b
JB
13079 /* If we have address ranges, record them. */
13080 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13081
a60f3166 13082 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13083
34eaf542 13084 /* Attach template arguments to function. */
2f4732b0 13085 if (!template_args.empty ())
34eaf542
TT
13086 {
13087 gdb_assert (templ_func != NULL);
13088
2f4732b0 13089 templ_func->n_template_arguments = template_args.size ();
34eaf542 13090 templ_func->template_arguments
8d749320
SM
13091 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13092 templ_func->n_template_arguments);
34eaf542 13093 memcpy (templ_func->template_arguments,
2f4732b0 13094 template_args.data (),
34eaf542 13095 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13096
13097 /* Make sure that the symtab is set on the new symbols. Even
13098 though they don't appear in this symtab directly, other parts
13099 of gdb assume that symbols do, and this is reasonably
13100 true. */
8634679f 13101 for (symbol *sym : template_args)
3e1d3d8c 13102 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13103 }
13104
208d8187
JB
13105 /* In C++, we can have functions nested inside functions (e.g., when
13106 a function declares a class that has methods). This means that
13107 when we finish processing a function scope, we may need to go
13108 back to building a containing block's symbol lists. */
c24bdb02
KS
13109 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13110 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13111
921e78cf
JB
13112 /* If we've finished processing a top-level function, subsequent
13113 symbols go in the file symbol list. */
c24bdb02
KS
13114 if (cu->get_builder ()->outermost_context_p ())
13115 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13116}
13117
13118/* Process all the DIES contained within a lexical block scope. Start
13119 a new scope, process the dies, and then close the scope. */
13120
13121static void
e7c27a73 13122read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13123{
518817b3 13124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13125 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13126 CORE_ADDR lowpc, highpc;
13127 struct die_info *child_die;
e142c38c
DJ
13128 CORE_ADDR baseaddr;
13129
b3b3bada 13130 baseaddr = objfile->text_section_offset ();
c906108c
SS
13131
13132 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13133 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13134 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13135 be nasty. Might be easier to properly extend generic blocks to
af34e669 13136 describe ranges. */
e385593e
JK
13137 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13138 {
13139 case PC_BOUNDS_NOT_PRESENT:
13140 /* DW_TAG_lexical_block has no attributes, process its children as if
13141 there was no wrapping by that DW_TAG_lexical_block.
13142 GCC does no longer produces such DWARF since GCC r224161. */
13143 for (child_die = die->child;
13144 child_die != NULL && child_die->tag;
436c571c 13145 child_die = child_die->sibling)
4f7bc5ed
TT
13146 {
13147 /* We might already be processing this DIE. This can happen
13148 in an unusual circumstance -- where a subroutine A
13149 appears lexically in another subroutine B, but A actually
13150 inlines B. The recursion is broken here, rather than in
13151 inherit_abstract_dies, because it seems better to simply
13152 drop concrete children here. */
13153 if (!child_die->in_process)
13154 process_die (child_die, cu);
13155 }
e385593e
JK
13156 return;
13157 case PC_BOUNDS_INVALID:
13158 return;
13159 }
3e29f34a
MR
13160 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13161 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13162
c24bdb02 13163 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13164 if (die->child != NULL)
c906108c 13165 {
639d11d3 13166 child_die = die->child;
c906108c
SS
13167 while (child_die && child_die->tag)
13168 {
e7c27a73 13169 process_die (child_die, cu);
436c571c 13170 child_die = child_die->sibling;
c906108c
SS
13171 }
13172 }
3ea89b92 13173 inherit_abstract_dies (die, cu);
c24bdb02 13174 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13175
c24bdb02
KS
13176 if (*cu->get_builder ()->get_local_symbols () != NULL
13177 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13178 {
801e3a5b 13179 struct block *block
c24bdb02 13180 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13181 cstk.start_addr, highpc);
801e3a5b
JB
13182
13183 /* Note that recording ranges after traversing children, as we
13184 do here, means that recording a parent's ranges entails
13185 walking across all its children's ranges as they appear in
13186 the address map, which is quadratic behavior.
13187
13188 It would be nicer to record the parent's ranges before
13189 traversing its children, simply overriding whatever you find
13190 there. But since we don't even decide whether to create a
13191 block until after we've traversed its children, that's hard
13192 to do. */
13193 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13194 }
c24bdb02
KS
13195 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13196 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13197}
13198
216f72a1 13199/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13200
13201static void
13202read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13203{
518817b3 13204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13205 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13206 CORE_ADDR pc, baseaddr;
13207 struct attribute *attr;
13208 struct call_site *call_site, call_site_local;
13209 void **slot;
13210 int nparams;
13211 struct die_info *child_die;
13212
b3b3bada 13213 baseaddr = objfile->text_section_offset ();
96408a79 13214
216f72a1
JK
13215 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13216 if (attr == NULL)
13217 {
13218 /* This was a pre-DWARF-5 GNU extension alias
13219 for DW_AT_call_return_pc. */
13220 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13221 }
96408a79
SA
13222 if (!attr)
13223 {
b98664d3 13224 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13225 "DIE %s [in module %s]"),
13226 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13227 return;
13228 }
cd6c91b4 13229 pc = attr->value_as_address () + baseaddr;
3e29f34a 13230 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13231
13232 if (cu->call_site_htab == NULL)
13233 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13234 NULL, &objfile->objfile_obstack,
13235 hashtab_obstack_allocate, NULL);
13236 call_site_local.pc = pc;
13237 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13238 if (*slot != NULL)
13239 {
b98664d3 13240 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13241 "DIE %s [in module %s]"),
13242 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13243 objfile_name (objfile));
96408a79
SA
13244 return;
13245 }
13246
13247 /* Count parameters at the caller. */
13248
13249 nparams = 0;
13250 for (child_die = die->child; child_die && child_die->tag;
436c571c 13251 child_die = child_die->sibling)
96408a79 13252 {
216f72a1
JK
13253 if (child_die->tag != DW_TAG_call_site_parameter
13254 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13255 {
b98664d3 13256 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13257 "DW_TAG_call_site child DIE %s [in module %s]"),
13258 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13259 objfile_name (objfile));
96408a79
SA
13260 continue;
13261 }
13262
13263 nparams++;
13264 }
13265
224c3ddb
SM
13266 call_site
13267 = ((struct call_site *)
13268 obstack_alloc (&objfile->objfile_obstack,
13269 sizeof (*call_site)
13270 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13271 *slot = call_site;
13272 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13273 call_site->pc = pc;
13274
216f72a1
JK
13275 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13276 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13277 {
13278 struct die_info *func_die;
13279
13280 /* Skip also over DW_TAG_inlined_subroutine. */
13281 for (func_die = die->parent;
13282 func_die && func_die->tag != DW_TAG_subprogram
13283 && func_die->tag != DW_TAG_subroutine_type;
13284 func_die = func_die->parent);
13285
216f72a1
JK
13286 /* DW_AT_call_all_calls is a superset
13287 of DW_AT_call_all_tail_calls. */
96408a79 13288 if (func_die
216f72a1 13289 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13290 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13291 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13292 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13293 {
13294 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13295 not complete. But keep CALL_SITE for look ups via call_site_htab,
13296 both the initial caller containing the real return address PC and
13297 the final callee containing the current PC of a chain of tail
13298 calls do not need to have the tail call list complete. But any
13299 function candidate for a virtual tail call frame searched via
13300 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13301 determined unambiguously. */
13302 }
13303 else
13304 {
13305 struct type *func_type = NULL;
13306
13307 if (func_die)
13308 func_type = get_die_type (func_die, cu);
13309 if (func_type != NULL)
13310 {
78134374 13311 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13312
13313 /* Enlist this call site to the function. */
13314 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13315 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13316 }
13317 else
b98664d3 13318 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13319 "DIE %s [in module %s]"),
13320 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13321 }
13322 }
13323
216f72a1
JK
13324 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13325 if (attr == NULL)
13326 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13327 if (attr == NULL)
13328 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13329 if (attr == NULL)
216f72a1
JK
13330 {
13331 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13332 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13333 }
96408a79 13334 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13335 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13336 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13337 else if (attr->form_is_block ())
96408a79
SA
13338 {
13339 struct dwarf2_locexpr_baton *dlbaton;
13340
8d749320 13341 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13342 dlbaton->data = DW_BLOCK (attr)->data;
13343 dlbaton->size = DW_BLOCK (attr)->size;
13344 dlbaton->per_cu = cu->per_cu;
13345
13346 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13347 }
cd6c91b4 13348 else if (attr->form_is_ref ())
96408a79 13349 {
96408a79
SA
13350 struct dwarf2_cu *target_cu = cu;
13351 struct die_info *target_die;
13352
ac9ec31b 13353 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13354 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13355 if (die_is_declaration (target_die, target_cu))
13356 {
7d45c7c3 13357 const char *target_physname;
9112db09
JK
13358
13359 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13360 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13361 if (target_physname == NULL)
9112db09 13362 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13363 if (target_physname == NULL)
b98664d3 13364 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13365 "physname, for referencing DIE %s [in module %s]"),
13366 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13367 else
7d455152 13368 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13369 }
13370 else
13371 {
13372 CORE_ADDR lowpc;
13373
13374 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13375 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13376 <= PC_BOUNDS_INVALID)
b98664d3 13377 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13378 "low pc, for referencing DIE %s [in module %s]"),
13379 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13380 else
3e29f34a
MR
13381 {
13382 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13383 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13384 }
96408a79
SA
13385 }
13386 }
13387 else
b98664d3 13388 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13389 "block nor reference, for DIE %s [in module %s]"),
13390 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13391
13392 call_site->per_cu = cu->per_cu;
13393
13394 for (child_die = die->child;
13395 child_die && child_die->tag;
436c571c 13396 child_die = child_die->sibling)
96408a79 13397 {
96408a79 13398 struct call_site_parameter *parameter;
1788b2d3 13399 struct attribute *loc, *origin;
96408a79 13400
216f72a1
JK
13401 if (child_die->tag != DW_TAG_call_site_parameter
13402 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13403 {
13404 /* Already printed the complaint above. */
13405 continue;
13406 }
13407
13408 gdb_assert (call_site->parameter_count < nparams);
13409 parameter = &call_site->parameter[call_site->parameter_count];
13410
1788b2d3
JK
13411 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13412 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13413 register is contained in DW_AT_call_value. */
96408a79 13414
24c5c679 13415 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13416 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13417 if (origin == NULL)
13418 {
13419 /* This was a pre-DWARF-5 GNU extension alias
13420 for DW_AT_call_parameter. */
13421 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13422 }
cd6c91b4 13423 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13424 {
1788b2d3 13425 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13426
0826b30a 13427 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13428 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13429 {
13430 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13431 binding can be done only inside one CU. Such referenced DIE
13432 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13433 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13434 "DW_TAG_call_site child DIE %s [in module %s]"),
13435 sect_offset_str (child_die->sect_off),
9c541725 13436 objfile_name (objfile));
d76b7dbc
JK
13437 continue;
13438 }
9c541725
PA
13439 parameter->u.param_cu_off
13440 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13441 }
4fc6c0d5 13442 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13443 {
b98664d3 13444 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13445 "DW_TAG_call_site child DIE %s [in module %s]"),
13446 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13447 continue;
13448 }
24c5c679 13449 else
96408a79 13450 {
24c5c679
JK
13451 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13452 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13453 if (parameter->u.dwarf_reg != -1)
13454 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13455 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13456 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13457 &parameter->u.fb_offset))
13458 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13459 else
13460 {
b98664d3 13461 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13462 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13463 "DW_TAG_call_site child DIE %s "
24c5c679 13464 "[in module %s]"),
9d8780f0 13465 sect_offset_str (child_die->sect_off),
9c541725 13466 objfile_name (objfile));
24c5c679
JK
13467 continue;
13468 }
96408a79
SA
13469 }
13470
216f72a1
JK
13471 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13472 if (attr == NULL)
13473 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13474 if (attr == NULL || !attr->form_is_block ())
96408a79 13475 {
b98664d3 13476 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13477 "DW_TAG_call_site child DIE %s [in module %s]"),
13478 sect_offset_str (child_die->sect_off),
9c541725 13479 objfile_name (objfile));
96408a79
SA
13480 continue;
13481 }
13482 parameter->value = DW_BLOCK (attr)->data;
13483 parameter->value_size = DW_BLOCK (attr)->size;
13484
13485 /* Parameters are not pre-cleared by memset above. */
13486 parameter->data_value = NULL;
13487 parameter->data_value_size = 0;
13488 call_site->parameter_count++;
13489
216f72a1
JK
13490 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13491 if (attr == NULL)
13492 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13493 if (attr != nullptr)
96408a79 13494 {
4fc6c0d5 13495 if (!attr->form_is_block ())
b98664d3 13496 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13497 "DW_TAG_call_site child DIE %s [in module %s]"),
13498 sect_offset_str (child_die->sect_off),
9c541725 13499 objfile_name (objfile));
96408a79
SA
13500 else
13501 {
13502 parameter->data_value = DW_BLOCK (attr)->data;
13503 parameter->data_value_size = DW_BLOCK (attr)->size;
13504 }
13505 }
13506 }
13507}
13508
71a3c369
TT
13509/* Helper function for read_variable. If DIE represents a virtual
13510 table, then return the type of the concrete object that is
13511 associated with the virtual table. Otherwise, return NULL. */
13512
13513static struct type *
13514rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13515{
13516 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13517 if (attr == NULL)
13518 return NULL;
13519
13520 /* Find the type DIE. */
13521 struct die_info *type_die = NULL;
13522 struct dwarf2_cu *type_cu = cu;
13523
cd6c91b4 13524 if (attr->form_is_ref ())
71a3c369
TT
13525 type_die = follow_die_ref (die, attr, &type_cu);
13526 if (type_die == NULL)
13527 return NULL;
13528
13529 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13530 return NULL;
13531 return die_containing_type (type_die, type_cu);
13532}
13533
13534/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13535
13536static void
13537read_variable (struct die_info *die, struct dwarf2_cu *cu)
13538{
13539 struct rust_vtable_symbol *storage = NULL;
13540
13541 if (cu->language == language_rust)
13542 {
13543 struct type *containing_type = rust_containing_type (die, cu);
13544
13545 if (containing_type != NULL)
13546 {
518817b3 13547 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13548
8c14c3a3 13549 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13550 storage->concrete_type = containing_type;
cf724bc9 13551 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13552 }
13553 }
13554
e4a62c65
TV
13555 struct symbol *res = new_symbol (die, NULL, cu, storage);
13556 struct attribute *abstract_origin
13557 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13558 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13559 if (res == NULL && loc && abstract_origin)
13560 {
13561 /* We have a variable without a name, but with a location and an abstract
13562 origin. This may be a concrete instance of an abstract variable
13563 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13564 later. */
13565 struct dwarf2_cu *origin_cu = cu;
13566 struct die_info *origin_die
13567 = follow_die_ref (die, abstract_origin, &origin_cu);
13568 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13569 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13570 }
71a3c369
TT
13571}
13572
43988095
JK
13573/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13574 reading .debug_rnglists.
13575 Callback's type should be:
13576 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13577 Return true if the attributes are present and valid, otherwise,
13578 return false. */
13579
13580template <typename Callback>
13581static bool
13582dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13583 Callback &&callback)
13584{
ed2dc618 13585 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13586 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13587 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13588 bfd *obfd = objfile->obfd;
43988095 13589 /* Base address selection entry. */
2b24b6e4 13590 gdb::optional<CORE_ADDR> base;
43988095 13591 const gdb_byte *buffer;
43988095
JK
13592 CORE_ADDR baseaddr;
13593 bool overflow = false;
13594
43988095
JK
13595 base = cu->base_address;
13596
96b79293 13597 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13598 if (offset >= dwarf2_per_objfile->rnglists.size)
13599 {
b98664d3 13600 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13601 offset);
13602 return false;
13603 }
13604 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13605
b3b3bada 13606 baseaddr = objfile->text_section_offset ();
43988095
JK
13607
13608 while (1)
13609 {
7814882a
JK
13610 /* Initialize it due to a false compiler warning. */
13611 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13612 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13613 + dwarf2_per_objfile->rnglists.size);
13614 unsigned int bytes_read;
13615
13616 if (buffer == buf_end)
13617 {
13618 overflow = true;
13619 break;
13620 }
13621 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13622 switch (rlet)
13623 {
13624 case DW_RLE_end_of_list:
13625 break;
13626 case DW_RLE_base_address:
13627 if (buffer + cu->header.addr_size > buf_end)
13628 {
13629 overflow = true;
13630 break;
13631 }
c8a7a66f 13632 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13633 buffer += bytes_read;
13634 break;
13635 case DW_RLE_start_length:
13636 if (buffer + cu->header.addr_size > buf_end)
13637 {
13638 overflow = true;
13639 break;
13640 }
c8a7a66f
TT
13641 range_beginning = cu->header.read_address (obfd, buffer,
13642 &bytes_read);
43988095
JK
13643 buffer += bytes_read;
13644 range_end = (range_beginning
13645 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13646 buffer += bytes_read;
13647 if (buffer > buf_end)
13648 {
13649 overflow = true;
13650 break;
13651 }
13652 break;
13653 case DW_RLE_offset_pair:
13654 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13655 buffer += bytes_read;
13656 if (buffer > buf_end)
13657 {
13658 overflow = true;
13659 break;
13660 }
13661 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13662 buffer += bytes_read;
13663 if (buffer > buf_end)
13664 {
13665 overflow = true;
13666 break;
13667 }
13668 break;
13669 case DW_RLE_start_end:
13670 if (buffer + 2 * cu->header.addr_size > buf_end)
13671 {
13672 overflow = true;
13673 break;
13674 }
c8a7a66f
TT
13675 range_beginning = cu->header.read_address (obfd, buffer,
13676 &bytes_read);
43988095 13677 buffer += bytes_read;
c8a7a66f 13678 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13679 buffer += bytes_read;
13680 break;
13681 default:
b98664d3 13682 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13683 return false;
13684 }
13685 if (rlet == DW_RLE_end_of_list || overflow)
13686 break;
13687 if (rlet == DW_RLE_base_address)
13688 continue;
13689
2b24b6e4 13690 if (!base.has_value ())
43988095
JK
13691 {
13692 /* We have no valid base address for the ranges
13693 data. */
b98664d3 13694 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13695 return false;
13696 }
13697
13698 if (range_beginning > range_end)
13699 {
13700 /* Inverted range entries are invalid. */
b98664d3 13701 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13702 return false;
13703 }
13704
13705 /* Empty range entries have no effect. */
13706 if (range_beginning == range_end)
13707 continue;
13708
2b24b6e4
TT
13709 range_beginning += *base;
13710 range_end += *base;
43988095
JK
13711
13712 /* A not-uncommon case of bad debug info.
13713 Don't pollute the addrmap with bad data. */
13714 if (range_beginning + baseaddr == 0
13715 && !dwarf2_per_objfile->has_section_at_zero)
13716 {
b98664d3 13717 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13718 " [in module %s]"), objfile_name (objfile));
13719 continue;
13720 }
13721
13722 callback (range_beginning, range_end);
13723 }
13724
13725 if (overflow)
13726 {
b98664d3 13727 complaint (_("Offset %d is not terminated "
43988095
JK
13728 "for DW_AT_ranges attribute"),
13729 offset);
13730 return false;
13731 }
13732
13733 return true;
13734}
13735
13736/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13737 Callback's type should be:
13738 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13739 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13740
43988095 13741template <typename Callback>
43039443 13742static int
5f46c5a5 13743dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13744 Callback &&callback)
43039443 13745{
ed2dc618 13746 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13747 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13748 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13749 struct comp_unit_head *cu_header = &cu->header;
13750 bfd *obfd = objfile->obfd;
13751 unsigned int addr_size = cu_header->addr_size;
13752 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13753 /* Base address selection entry. */
2b24b6e4 13754 gdb::optional<CORE_ADDR> base;
43039443 13755 unsigned int dummy;
d521ce57 13756 const gdb_byte *buffer;
ff013f42 13757 CORE_ADDR baseaddr;
43039443 13758
43988095
JK
13759 if (cu_header->version >= 5)
13760 return dwarf2_rnglists_process (offset, cu, callback);
13761
d00adf39 13762 base = cu->base_address;
43039443 13763
96b79293 13764 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13765 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13766 {
b98664d3 13767 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13768 offset);
13769 return 0;
13770 }
dce234bc 13771 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13772
b3b3bada 13773 baseaddr = objfile->text_section_offset ();
ff013f42 13774
43039443
JK
13775 while (1)
13776 {
13777 CORE_ADDR range_beginning, range_end;
13778
c8a7a66f 13779 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13780 buffer += addr_size;
c8a7a66f 13781 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13782 buffer += addr_size;
13783 offset += 2 * addr_size;
13784
13785 /* An end of list marker is a pair of zero addresses. */
13786 if (range_beginning == 0 && range_end == 0)
13787 /* Found the end of list entry. */
13788 break;
13789
13790 /* Each base address selection entry is a pair of 2 values.
13791 The first is the largest possible address, the second is
13792 the base address. Check for a base address here. */
13793 if ((range_beginning & mask) == mask)
13794 {
28d2bfb9
AB
13795 /* If we found the largest possible address, then we already
13796 have the base address in range_end. */
13797 base = range_end;
43039443
JK
13798 continue;
13799 }
13800
2b24b6e4 13801 if (!base.has_value ())
43039443
JK
13802 {
13803 /* We have no valid base address for the ranges
13804 data. */
b98664d3 13805 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13806 return 0;
13807 }
13808
9277c30c
UW
13809 if (range_beginning > range_end)
13810 {
13811 /* Inverted range entries are invalid. */
b98664d3 13812 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13813 return 0;
13814 }
13815
13816 /* Empty range entries have no effect. */
13817 if (range_beginning == range_end)
13818 continue;
13819
2b24b6e4
TT
13820 range_beginning += *base;
13821 range_end += *base;
43039443 13822
01093045
DE
13823 /* A not-uncommon case of bad debug info.
13824 Don't pollute the addrmap with bad data. */
13825 if (range_beginning + baseaddr == 0
13826 && !dwarf2_per_objfile->has_section_at_zero)
13827 {
b98664d3 13828 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13829 " [in module %s]"), objfile_name (objfile));
01093045
DE
13830 continue;
13831 }
13832
5f46c5a5
JK
13833 callback (range_beginning, range_end);
13834 }
13835
13836 return 1;
13837}
13838
13839/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13840 Return 1 if the attributes are present and valid, otherwise, return 0.
13841 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13842
13843static int
13844dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13845 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13846 dwarf2_psymtab *ranges_pst)
5f46c5a5 13847{
518817b3 13848 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13849 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13850 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13851 int low_set = 0;
13852 CORE_ADDR low = 0;
13853 CORE_ADDR high = 0;
13854 int retval;
13855
13856 retval = dwarf2_ranges_process (offset, cu,
13857 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13858 {
9277c30c 13859 if (ranges_pst != NULL)
3e29f34a
MR
13860 {
13861 CORE_ADDR lowpc;
13862 CORE_ADDR highpc;
13863
79748972
TT
13864 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13865 range_beginning + baseaddr)
13866 - baseaddr);
13867 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13868 range_end + baseaddr)
13869 - baseaddr);
d320c2b5
TT
13870 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13871 lowpc, highpc - 1, ranges_pst);
3e29f34a 13872 }
ff013f42 13873
43039443
JK
13874 /* FIXME: This is recording everything as a low-high
13875 segment of consecutive addresses. We should have a
13876 data structure for discontiguous block ranges
13877 instead. */
13878 if (! low_set)
13879 {
13880 low = range_beginning;
13881 high = range_end;
13882 low_set = 1;
13883 }
13884 else
13885 {
13886 if (range_beginning < low)
13887 low = range_beginning;
13888 if (range_end > high)
13889 high = range_end;
13890 }
5f46c5a5
JK
13891 });
13892 if (!retval)
13893 return 0;
43039443
JK
13894
13895 if (! low_set)
13896 /* If the first entry is an end-of-list marker, the range
13897 describes an empty scope, i.e. no instructions. */
13898 return 0;
13899
13900 if (low_return)
13901 *low_return = low;
13902 if (high_return)
13903 *high_return = high;
13904 return 1;
13905}
13906
3a2b436a
JK
13907/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13908 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13909 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13910
3a2b436a 13911static enum pc_bounds_kind
af34e669 13912dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13913 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13914 dwarf2_psymtab *pst)
c906108c 13915{
518817b3
SM
13916 struct dwarf2_per_objfile *dwarf2_per_objfile
13917 = cu->per_cu->dwarf2_per_objfile;
c906108c 13918 struct attribute *attr;
91da1414 13919 struct attribute *attr_high;
af34e669
DJ
13920 CORE_ADDR low = 0;
13921 CORE_ADDR high = 0;
e385593e 13922 enum pc_bounds_kind ret;
c906108c 13923
91da1414
MW
13924 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13925 if (attr_high)
af34e669 13926 {
e142c38c 13927 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13928 if (attr != nullptr)
91da1414 13929 {
cd6c91b4
TT
13930 low = attr->value_as_address ();
13931 high = attr_high->value_as_address ();
13932 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13933 high += low;
91da1414 13934 }
af34e669
DJ
13935 else
13936 /* Found high w/o low attribute. */
e385593e 13937 return PC_BOUNDS_INVALID;
af34e669
DJ
13938
13939 /* Found consecutive range of addresses. */
3a2b436a 13940 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13941 }
c906108c 13942 else
af34e669 13943 {
e142c38c 13944 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13945 if (attr != NULL)
13946 {
18a8505e 13947 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13948 We take advantage of the fact that DW_AT_ranges does not appear
13949 in DW_TAG_compile_unit of DWO files. */
13950 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13951 unsigned int ranges_offset = (DW_UNSND (attr)
13952 + (need_ranges_base
13953 ? cu->ranges_base
13954 : 0));
2e3cf129 13955
af34e669 13956 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13957 .debug_ranges section. */
2e3cf129 13958 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13959 return PC_BOUNDS_INVALID;
43039443 13960 /* Found discontinuous range of addresses. */
3a2b436a 13961 ret = PC_BOUNDS_RANGES;
af34e669 13962 }
e385593e
JK
13963 else
13964 return PC_BOUNDS_NOT_PRESENT;
af34e669 13965 }
c906108c 13966
48fbe735 13967 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13968 if (high <= low)
e385593e 13969 return PC_BOUNDS_INVALID;
c906108c
SS
13970
13971 /* When using the GNU linker, .gnu.linkonce. sections are used to
13972 eliminate duplicate copies of functions and vtables and such.
13973 The linker will arbitrarily choose one and discard the others.
13974 The AT_*_pc values for such functions refer to local labels in
13975 these sections. If the section from that file was discarded, the
13976 labels are not in the output, so the relocs get a value of 0.
13977 If this is a discarded function, mark the pc bounds as invalid,
13978 so that GDB will ignore it. */
72dca2f5 13979 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13980 return PC_BOUNDS_INVALID;
c906108c
SS
13981
13982 *lowpc = low;
96408a79
SA
13983 if (highpc)
13984 *highpc = high;
af34e669 13985 return ret;
c906108c
SS
13986}
13987
b084d499
JB
13988/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13989 its low and high PC addresses. Do nothing if these addresses could not
13990 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13991 and HIGHPC to the high address if greater than HIGHPC. */
13992
13993static void
13994dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13995 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13996 struct dwarf2_cu *cu)
13997{
13998 CORE_ADDR low, high;
13999 struct die_info *child = die->child;
14000
e385593e 14001 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14002 {
325fac50
PA
14003 *lowpc = std::min (*lowpc, low);
14004 *highpc = std::max (*highpc, high);
b084d499
JB
14005 }
14006
14007 /* If the language does not allow nested subprograms (either inside
14008 subprograms or lexical blocks), we're done. */
14009 if (cu->language != language_ada)
14010 return;
6e70227d 14011
b084d499
JB
14012 /* Check all the children of the given DIE. If it contains nested
14013 subprograms, then check their pc bounds. Likewise, we need to
14014 check lexical blocks as well, as they may also contain subprogram
14015 definitions. */
14016 while (child && child->tag)
14017 {
14018 if (child->tag == DW_TAG_subprogram
14019 || child->tag == DW_TAG_lexical_block)
14020 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14021 child = child->sibling;
b084d499
JB
14022 }
14023}
14024
fae299cd
DC
14025/* Get the low and high pc's represented by the scope DIE, and store
14026 them in *LOWPC and *HIGHPC. If the correct values can't be
14027 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14028
14029static void
14030get_scope_pc_bounds (struct die_info *die,
14031 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14032 struct dwarf2_cu *cu)
14033{
14034 CORE_ADDR best_low = (CORE_ADDR) -1;
14035 CORE_ADDR best_high = (CORE_ADDR) 0;
14036 CORE_ADDR current_low, current_high;
14037
3a2b436a 14038 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14039 >= PC_BOUNDS_RANGES)
fae299cd
DC
14040 {
14041 best_low = current_low;
14042 best_high = current_high;
14043 }
14044 else
14045 {
14046 struct die_info *child = die->child;
14047
14048 while (child && child->tag)
14049 {
14050 switch (child->tag) {
14051 case DW_TAG_subprogram:
b084d499 14052 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14053 break;
14054 case DW_TAG_namespace:
f55ee35c 14055 case DW_TAG_module:
fae299cd
DC
14056 /* FIXME: carlton/2004-01-16: Should we do this for
14057 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14058 that current GCC's always emit the DIEs corresponding
14059 to definitions of methods of classes as children of a
14060 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14061 the DIEs giving the declarations, which could be
14062 anywhere). But I don't see any reason why the
14063 standards says that they have to be there. */
14064 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14065
14066 if (current_low != ((CORE_ADDR) -1))
14067 {
325fac50
PA
14068 best_low = std::min (best_low, current_low);
14069 best_high = std::max (best_high, current_high);
fae299cd
DC
14070 }
14071 break;
14072 default:
0963b4bd 14073 /* Ignore. */
fae299cd
DC
14074 break;
14075 }
14076
436c571c 14077 child = child->sibling;
fae299cd
DC
14078 }
14079 }
14080
14081 *lowpc = best_low;
14082 *highpc = best_high;
14083}
14084
801e3a5b
JB
14085/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14086 in DIE. */
380bca97 14087
801e3a5b
JB
14088static void
14089dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14090 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14091{
518817b3 14092 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14093 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14094 struct attribute *attr;
91da1414 14095 struct attribute *attr_high;
801e3a5b 14096
91da1414
MW
14097 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14098 if (attr_high)
801e3a5b 14099 {
801e3a5b 14100 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14101 if (attr != nullptr)
801e3a5b 14102 {
cd6c91b4
TT
14103 CORE_ADDR low = attr->value_as_address ();
14104 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14105
cd6c91b4 14106 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14107 high += low;
9a619af0 14108
3e29f34a
MR
14109 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14110 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14111 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14112 }
14113 }
14114
14115 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14116 if (attr != nullptr)
801e3a5b 14117 {
18a8505e 14118 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14119 We take advantage of the fact that DW_AT_ranges does not appear
14120 in DW_TAG_compile_unit of DWO files. */
14121 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14122
14123 /* The value of the DW_AT_ranges attribute is the offset of the
14124 address range list in the .debug_ranges section. */
ab435259
DE
14125 unsigned long offset = (DW_UNSND (attr)
14126 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14127
2d5f09ec 14128 std::vector<blockrange> blockvec;
5f46c5a5
JK
14129 dwarf2_ranges_process (offset, cu,
14130 [&] (CORE_ADDR start, CORE_ADDR end)
14131 {
58fdfd2c
JK
14132 start += baseaddr;
14133 end += baseaddr;
5f46c5a5
JK
14134 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14135 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14136 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14137 blockvec.emplace_back (start, end);
5f46c5a5 14138 });
2d5f09ec
KB
14139
14140 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14141 }
14142}
14143
685b1105
JK
14144/* Check whether the producer field indicates either of GCC < 4.6, or the
14145 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14146
685b1105
JK
14147static void
14148check_producer (struct dwarf2_cu *cu)
60d5a603 14149{
38360086 14150 int major, minor;
60d5a603
JK
14151
14152 if (cu->producer == NULL)
14153 {
14154 /* For unknown compilers expect their behavior is DWARF version
14155 compliant.
14156
14157 GCC started to support .debug_types sections by -gdwarf-4 since
14158 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14159 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14160 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14161 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14162 }
b1ffba5a 14163 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14164 {
38360086
MW
14165 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14166 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14167 }
5230b05a 14168 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14169 {
14170 cu->producer_is_icc = true;
14171 cu->producer_is_icc_lt_14 = major < 14;
14172 }
c258c396
JD
14173 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14174 cu->producer_is_codewarrior = true;
685b1105
JK
14175 else
14176 {
14177 /* For other non-GCC compilers, expect their behavior is DWARF version
14178 compliant. */
60d5a603
JK
14179 }
14180
9068261f 14181 cu->checked_producer = true;
685b1105 14182}
ba919b58 14183
685b1105
JK
14184/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14185 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14186 during 4.6.0 experimental. */
14187
9068261f 14188static bool
685b1105
JK
14189producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14190{
14191 if (!cu->checked_producer)
14192 check_producer (cu);
14193
14194 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14195}
14196
c258c396
JD
14197
14198/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14199 with incorrect is_stmt attributes. */
14200
14201static bool
14202producer_is_codewarrior (struct dwarf2_cu *cu)
14203{
14204 if (!cu->checked_producer)
14205 check_producer (cu);
14206
14207 return cu->producer_is_codewarrior;
14208}
14209
405feb71 14210/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14211 DW_AT_accessibility. */
14212
14213static enum dwarf_access_attribute
14214dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14215{
14216 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14217 {
14218 /* The default DWARF 2 accessibility for members is public, the default
14219 accessibility for inheritance is private. */
14220
14221 if (die->tag != DW_TAG_inheritance)
14222 return DW_ACCESS_public;
14223 else
14224 return DW_ACCESS_private;
14225 }
14226 else
14227 {
14228 /* DWARF 3+ defines the default accessibility a different way. The same
14229 rules apply now for DW_TAG_inheritance as for the members and it only
14230 depends on the container kind. */
14231
14232 if (die->parent->tag == DW_TAG_class_type)
14233 return DW_ACCESS_private;
14234 else
14235 return DW_ACCESS_public;
14236 }
14237}
14238
74ac6d43
TT
14239/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14240 offset. If the attribute was not found return 0, otherwise return
14241 1. If it was found but could not properly be handled, set *OFFSET
14242 to 0. */
14243
14244static int
14245handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14246 LONGEST *offset)
14247{
14248 struct attribute *attr;
14249
14250 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14251 if (attr != NULL)
14252 {
14253 *offset = 0;
14254
14255 /* Note that we do not check for a section offset first here.
14256 This is because DW_AT_data_member_location is new in DWARF 4,
14257 so if we see it, we can assume that a constant form is really
14258 a constant and not a section offset. */
cd6c91b4 14259 if (attr->form_is_constant ())
0826b30a 14260 *offset = attr->constant_value (0);
cd6c91b4 14261 else if (attr->form_is_section_offset ())
74ac6d43 14262 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14263 else if (attr->form_is_block ())
74ac6d43
TT
14264 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14265 else
14266 dwarf2_complex_location_expr_complaint ();
14267
14268 return 1;
14269 }
14270
14271 return 0;
14272}
14273
7d79de9a
TT
14274/* Look for DW_AT_data_member_location and store the results in FIELD. */
14275
14276static void
14277handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14278 struct field *field)
14279{
14280 struct attribute *attr;
14281
14282 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14283 if (attr != NULL)
14284 {
14285 if (attr->form_is_constant ())
14286 {
14287 LONGEST offset = attr->constant_value (0);
14288 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14289 }
14290 else if (attr->form_is_section_offset ())
14291 dwarf2_complex_location_expr_complaint ();
14292 else if (attr->form_is_block ())
14293 {
14294 bool handled;
14295 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14296 if (handled)
14297 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14298 else
14299 {
14300 struct objfile *objfile
14301 = cu->per_cu->dwarf2_per_objfile->objfile;
14302 struct dwarf2_locexpr_baton *dlbaton
14303 = XOBNEW (&objfile->objfile_obstack,
14304 struct dwarf2_locexpr_baton);
14305 dlbaton->data = DW_BLOCK (attr)->data;
14306 dlbaton->size = DW_BLOCK (attr)->size;
14307 /* When using this baton, we want to compute the address
14308 of the field, not the value. This is why
14309 is_reference is set to false here. */
14310 dlbaton->is_reference = false;
14311 dlbaton->per_cu = cu->per_cu;
14312
14313 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14314 }
14315 }
14316 else
14317 dwarf2_complex_location_expr_complaint ();
14318 }
14319}
14320
c906108c
SS
14321/* Add an aggregate field to the field list. */
14322
14323static void
107d2387 14324dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14325 struct dwarf2_cu *cu)
6e70227d 14326{
518817b3 14327 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14328 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14329 struct nextfield *new_field;
14330 struct attribute *attr;
14331 struct field *fp;
15d034d0 14332 const char *fieldname = "";
c906108c 14333
7d0ccb61
DJ
14334 if (die->tag == DW_TAG_inheritance)
14335 {
be2daae6
TT
14336 fip->baseclasses.emplace_back ();
14337 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14338 }
14339 else
14340 {
be2daae6
TT
14341 fip->fields.emplace_back ();
14342 new_field = &fip->fields.back ();
7d0ccb61 14343 }
be2daae6 14344
9c6a1327
TT
14345 new_field->offset = die->sect_off;
14346
e142c38c 14347 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14348 if (attr != nullptr)
c906108c 14349 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14350 else
14351 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14352 if (new_field->accessibility != DW_ACCESS_public)
14353 fip->non_public_fields = 1;
60d5a603 14354
e142c38c 14355 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14356 if (attr != nullptr)
c906108c 14357 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14358 else
14359 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14360
14361 fp = &new_field->field;
a9a9bd0f 14362
e142c38c 14363 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14364 {
a9a9bd0f 14365 /* Data member other than a C++ static data member. */
6e70227d 14366
c906108c 14367 /* Get type of field. */
e7c27a73 14368 fp->type = die_type (die, cu);
c906108c 14369
d6a843b5 14370 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14371
c906108c 14372 /* Get bit size of field (zero if none). */
e142c38c 14373 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14374 if (attr != nullptr)
c906108c
SS
14375 {
14376 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14377 }
14378 else
14379 {
14380 FIELD_BITSIZE (*fp) = 0;
14381 }
14382
14383 /* Get bit offset of field. */
7d79de9a 14384 handle_data_member_location (die, cu, fp);
e142c38c 14385 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14386 if (attr != nullptr)
c906108c 14387 {
d5a22e77 14388 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14389 {
14390 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14391 additional bit offset from the MSB of the containing
14392 anonymous object to the MSB of the field. We don't
14393 have to do anything special since we don't need to
14394 know the size of the anonymous object. */
f41f5e61 14395 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14396 }
14397 else
14398 {
14399 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14400 MSB of the anonymous object, subtract off the number of
14401 bits from the MSB of the field to the MSB of the
14402 object, and then subtract off the number of bits of
14403 the field itself. The result is the bit offset of
14404 the LSB of the field. */
c906108c
SS
14405 int anonymous_size;
14406 int bit_offset = DW_UNSND (attr);
14407
e142c38c 14408 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14409 if (attr != nullptr)
c906108c
SS
14410 {
14411 /* The size of the anonymous object containing
14412 the bit field is explicit, so use the
14413 indicated size (in bytes). */
14414 anonymous_size = DW_UNSND (attr);
14415 }
14416 else
14417 {
14418 /* The size of the anonymous object containing
14419 the bit field must be inferred from the type
14420 attribute of the data member containing the
14421 bit field. */
14422 anonymous_size = TYPE_LENGTH (fp->type);
14423 }
f41f5e61
PA
14424 SET_FIELD_BITPOS (*fp,
14425 (FIELD_BITPOS (*fp)
14426 + anonymous_size * bits_per_byte
14427 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14428 }
14429 }
da5b30da
AA
14430 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14431 if (attr != NULL)
14432 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14433 + attr->constant_value (0)));
c906108c
SS
14434
14435 /* Get name of field. */
39cbfefa
DJ
14436 fieldname = dwarf2_name (die, cu);
14437 if (fieldname == NULL)
14438 fieldname = "";
d8151005
DJ
14439
14440 /* The name is already allocated along with this objfile, so we don't
14441 need to duplicate it for the type. */
14442 fp->name = fieldname;
c906108c
SS
14443
14444 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14445 pointer or virtual base class pointer) to private. */
e142c38c 14446 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14447 {
d48cc9dd 14448 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14449 new_field->accessibility = DW_ACCESS_private;
14450 fip->non_public_fields = 1;
14451 }
14452 }
a9a9bd0f 14453 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14454 {
a9a9bd0f
DC
14455 /* C++ static member. */
14456
14457 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14458 is a declaration, but all versions of G++ as of this writing
14459 (so through at least 3.2.1) incorrectly generate
14460 DW_TAG_variable tags. */
6e70227d 14461
ff355380 14462 const char *physname;
c906108c 14463
a9a9bd0f 14464 /* Get name of field. */
39cbfefa
DJ
14465 fieldname = dwarf2_name (die, cu);
14466 if (fieldname == NULL)
c906108c
SS
14467 return;
14468
254e6b9e 14469 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14470 if (attr
14471 /* Only create a symbol if this is an external value.
14472 new_symbol checks this and puts the value in the global symbol
14473 table, which we want. If it is not external, new_symbol
14474 will try to put the value in cu->list_in_scope which is wrong. */
14475 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14476 {
14477 /* A static const member, not much different than an enum as far as
14478 we're concerned, except that we can support more types. */
14479 new_symbol (die, NULL, cu);
14480 }
14481
2df3850c 14482 /* Get physical name. */
ff355380 14483 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14484
d8151005
DJ
14485 /* The name is already allocated along with this objfile, so we don't
14486 need to duplicate it for the type. */
14487 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14488 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14489 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14490 }
14491 else if (die->tag == DW_TAG_inheritance)
14492 {
74ac6d43 14493 /* C++ base class field. */
7d79de9a 14494 handle_data_member_location (die, cu, fp);
c906108c 14495 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14496 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14497 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14498 }
2ddeaf8a
TT
14499 else
14500 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14501}
14502
883fd55a
KS
14503/* Can the type given by DIE define another type? */
14504
14505static bool
14506type_can_define_types (const struct die_info *die)
14507{
14508 switch (die->tag)
14509 {
14510 case DW_TAG_typedef:
14511 case DW_TAG_class_type:
14512 case DW_TAG_structure_type:
14513 case DW_TAG_union_type:
14514 case DW_TAG_enumeration_type:
14515 return true;
14516
14517 default:
14518 return false;
14519 }
14520}
14521
14522/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14523
14524static void
883fd55a
KS
14525dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14526 struct dwarf2_cu *cu)
6e70227d 14527{
be2daae6
TT
14528 struct decl_field fp;
14529 memset (&fp, 0, sizeof (fp));
98751a41 14530
883fd55a 14531 gdb_assert (type_can_define_types (die));
98751a41 14532
883fd55a 14533 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14534 fp.name = dwarf2_name (die, cu);
14535 fp.type = read_type_die (die, cu);
98751a41 14536
c191a687
KS
14537 /* Save accessibility. */
14538 enum dwarf_access_attribute accessibility;
14539 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14540 if (attr != NULL)
14541 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14542 else
14543 accessibility = dwarf2_default_access_attribute (die, cu);
14544 switch (accessibility)
14545 {
14546 case DW_ACCESS_public:
14547 /* The assumed value if neither private nor protected. */
14548 break;
14549 case DW_ACCESS_private:
be2daae6 14550 fp.is_private = 1;
c191a687
KS
14551 break;
14552 case DW_ACCESS_protected:
be2daae6 14553 fp.is_protected = 1;
c191a687
KS
14554 break;
14555 default:
b98664d3 14556 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14557 }
14558
883fd55a 14559 if (die->tag == DW_TAG_typedef)
be2daae6 14560 fip->typedef_field_list.push_back (fp);
883fd55a 14561 else
be2daae6 14562 fip->nested_types_list.push_back (fp);
98751a41
JK
14563}
14564
9c6a1327
TT
14565/* A convenience typedef that's used when finding the discriminant
14566 field for a variant part. */
1b95cdb7
SM
14567typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14568 offset_map_type;
9c6a1327
TT
14569
14570/* Compute the discriminant range for a given variant. OBSTACK is
14571 where the results will be stored. VARIANT is the variant to
14572 process. IS_UNSIGNED indicates whether the discriminant is signed
14573 or unsigned. */
14574
14575static const gdb::array_view<discriminant_range>
14576convert_variant_range (struct obstack *obstack, const variant_field &variant,
14577 bool is_unsigned)
14578{
14579 std::vector<discriminant_range> ranges;
14580
14581 if (variant.default_branch)
14582 return {};
14583
14584 if (variant.discr_list_data == nullptr)
14585 {
14586 discriminant_range r
14587 = {variant.discriminant_value, variant.discriminant_value};
14588 ranges.push_back (r);
14589 }
14590 else
14591 {
14592 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14593 variant.discr_list_data->size);
14594 while (!data.empty ())
14595 {
14596 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14597 {
14598 complaint (_("invalid discriminant marker: %d"), data[0]);
14599 break;
14600 }
14601 bool is_range = data[0] == DW_DSC_range;
14602 data = data.slice (1);
14603
14604 ULONGEST low, high;
14605 unsigned int bytes_read;
14606
14607 if (data.empty ())
14608 {
14609 complaint (_("DW_AT_discr_list missing low value"));
14610 break;
14611 }
14612 if (is_unsigned)
14613 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14614 else
14615 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14616 &bytes_read);
14617 data = data.slice (bytes_read);
14618
14619 if (is_range)
14620 {
14621 if (data.empty ())
14622 {
14623 complaint (_("DW_AT_discr_list missing high value"));
14624 break;
14625 }
14626 if (is_unsigned)
14627 high = read_unsigned_leb128 (nullptr, data.data (),
14628 &bytes_read);
14629 else
14630 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14631 &bytes_read);
14632 data = data.slice (bytes_read);
14633 }
14634 else
14635 high = low;
14636
14637 ranges.push_back ({ low, high });
14638 }
14639 }
14640
14641 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14642 ranges.size ());
14643 std::copy (ranges.begin (), ranges.end (), result);
14644 return gdb::array_view<discriminant_range> (result, ranges.size ());
14645}
14646
14647static const gdb::array_view<variant_part> create_variant_parts
14648 (struct obstack *obstack,
14649 const offset_map_type &offset_map,
14650 struct field_info *fi,
14651 const std::vector<variant_part_builder> &variant_parts);
14652
14653/* Fill in a "struct variant" for a given variant field. RESULT is
14654 the variant to fill in. OBSTACK is where any needed allocations
14655 will be done. OFFSET_MAP holds the mapping from section offsets to
14656 fields for the type. FI describes the fields of the type we're
14657 processing. FIELD is the variant field we're converting. */
14658
14659static void
14660create_one_variant (variant &result, struct obstack *obstack,
14661 const offset_map_type &offset_map,
14662 struct field_info *fi, const variant_field &field)
14663{
14664 result.discriminants = convert_variant_range (obstack, field, false);
14665 result.first_field = field.first_field + fi->baseclasses.size ();
14666 result.last_field = field.last_field + fi->baseclasses.size ();
14667 result.parts = create_variant_parts (obstack, offset_map, fi,
14668 field.variant_parts);
14669}
14670
14671/* Fill in a "struct variant_part" for a given variant part. RESULT
14672 is the variant part to fill in. OBSTACK is where any needed
14673 allocations will be done. OFFSET_MAP holds the mapping from
14674 section offsets to fields for the type. FI describes the fields of
14675 the type we're processing. BUILDER is the variant part to be
14676 converted. */
14677
14678static void
14679create_one_variant_part (variant_part &result,
14680 struct obstack *obstack,
14681 const offset_map_type &offset_map,
14682 struct field_info *fi,
14683 const variant_part_builder &builder)
14684{
14685 auto iter = offset_map.find (builder.discriminant_offset);
14686 if (iter == offset_map.end ())
14687 {
14688 result.discriminant_index = -1;
14689 /* Doesn't matter. */
14690 result.is_unsigned = false;
14691 }
14692 else
14693 {
14694 result.discriminant_index = iter->second;
14695 result.is_unsigned
14696 = TYPE_UNSIGNED (FIELD_TYPE
14697 (fi->fields[result.discriminant_index].field));
14698 }
14699
14700 size_t n = builder.variants.size ();
14701 variant *output = new (obstack) variant[n];
14702 for (size_t i = 0; i < n; ++i)
14703 create_one_variant (output[i], obstack, offset_map, fi,
14704 builder.variants[i]);
14705
14706 result.variants = gdb::array_view<variant> (output, n);
14707}
14708
14709/* Create a vector of variant parts that can be attached to a type.
14710 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14711 holds the mapping from section offsets to fields for the type. FI
14712 describes the fields of the type we're processing. VARIANT_PARTS
14713 is the vector to convert. */
14714
14715static const gdb::array_view<variant_part>
14716create_variant_parts (struct obstack *obstack,
14717 const offset_map_type &offset_map,
14718 struct field_info *fi,
14719 const std::vector<variant_part_builder> &variant_parts)
14720{
14721 if (variant_parts.empty ())
14722 return {};
14723
14724 size_t n = variant_parts.size ();
14725 variant_part *result = new (obstack) variant_part[n];
14726 for (size_t i = 0; i < n; ++i)
14727 create_one_variant_part (result[i], obstack, offset_map, fi,
14728 variant_parts[i]);
14729
14730 return gdb::array_view<variant_part> (result, n);
14731}
14732
14733/* Compute the variant part vector for FIP, attaching it to TYPE when
14734 done. */
14735
14736static void
14737add_variant_property (struct field_info *fip, struct type *type,
14738 struct dwarf2_cu *cu)
14739{
14740 /* Map section offsets of fields to their field index. Note the
14741 field index here does not take the number of baseclasses into
14742 account. */
14743 offset_map_type offset_map;
14744 for (int i = 0; i < fip->fields.size (); ++i)
14745 offset_map[fip->fields[i].offset] = i;
14746
14747 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14748 gdb::array_view<variant_part> parts
14749 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14750 fip->variant_parts);
14751
14752 struct dynamic_prop prop;
14753 prop.kind = PROP_VARIANT_PARTS;
14754 prop.data.variant_parts
14755 = ((gdb::array_view<variant_part> *)
14756 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14757
5c54719c 14758 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14759}
14760
c906108c
SS
14761/* Create the vector of fields, and attach it to the type. */
14762
14763static void
fba45db2 14764dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14765 struct dwarf2_cu *cu)
c906108c 14766{
317f7127 14767 int nfields = fip->nfields ();
c906108c
SS
14768
14769 /* Record the field count, allocate space for the array of fields,
14770 and create blank accessibility bitfields if necessary. */
14771 TYPE_NFIELDS (type) = nfields;
14772 TYPE_FIELDS (type) = (struct field *)
be2daae6 14773 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14774
b4ba55a1 14775 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14776 {
14777 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14778
14779 TYPE_FIELD_PRIVATE_BITS (type) =
14780 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14781 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14782
14783 TYPE_FIELD_PROTECTED_BITS (type) =
14784 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14785 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14786
774b6a14
TT
14787 TYPE_FIELD_IGNORE_BITS (type) =
14788 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14789 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14790 }
14791
14792 /* If the type has baseclasses, allocate and clear a bit vector for
14793 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14794 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14795 {
be2daae6 14796 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14797 unsigned char *pointer;
c906108c
SS
14798
14799 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14800 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14801 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14802 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14803 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14804 }
14805
9c6a1327
TT
14806 if (!fip->variant_parts.empty ())
14807 add_variant_property (fip, type, cu);
2ddeaf8a 14808
be2daae6
TT
14809 /* Copy the saved-up fields into the field vector. */
14810 for (int i = 0; i < nfields; ++i)
c906108c 14811 {
be2daae6
TT
14812 struct nextfield &field
14813 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14814 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14815
be2daae6
TT
14816 TYPE_FIELD (type, i) = field.field;
14817 switch (field.accessibility)
c906108c 14818 {
c5aa993b 14819 case DW_ACCESS_private:
b4ba55a1 14820 if (cu->language != language_ada)
be2daae6 14821 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14822 break;
c906108c 14823
c5aa993b 14824 case DW_ACCESS_protected:
b4ba55a1 14825 if (cu->language != language_ada)
be2daae6 14826 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14827 break;
c906108c 14828
c5aa993b
JM
14829 case DW_ACCESS_public:
14830 break;
c906108c 14831
c5aa993b
JM
14832 default:
14833 /* Unknown accessibility. Complain and treat it as public. */
14834 {
b98664d3 14835 complaint (_("unsupported accessibility %d"),
be2daae6 14836 field.accessibility);
c5aa993b
JM
14837 }
14838 break;
c906108c 14839 }
be2daae6 14840 if (i < fip->baseclasses.size ())
c906108c 14841 {
be2daae6 14842 switch (field.virtuality)
c906108c 14843 {
c5aa993b
JM
14844 case DW_VIRTUALITY_virtual:
14845 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14846 if (cu->language == language_ada)
a73c6dcd 14847 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14848 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14849 break;
c906108c
SS
14850 }
14851 }
c906108c
SS
14852 }
14853}
14854
7d27a96d
TT
14855/* Return true if this member function is a constructor, false
14856 otherwise. */
14857
14858static int
14859dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14860{
14861 const char *fieldname;
fe978cb0 14862 const char *type_name;
7d27a96d
TT
14863 int len;
14864
14865 if (die->parent == NULL)
14866 return 0;
14867
14868 if (die->parent->tag != DW_TAG_structure_type
14869 && die->parent->tag != DW_TAG_union_type
14870 && die->parent->tag != DW_TAG_class_type)
14871 return 0;
14872
14873 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14874 type_name = dwarf2_name (die->parent, cu);
14875 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14876 return 0;
14877
14878 len = strlen (fieldname);
fe978cb0
PA
14879 return (strncmp (fieldname, type_name, len) == 0
14880 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14881}
14882
e35000a7
TBA
14883/* Check if the given VALUE is a recognized enum
14884 dwarf_defaulted_attribute constant according to DWARF5 spec,
14885 Table 7.24. */
14886
14887static bool
14888is_valid_DW_AT_defaulted (ULONGEST value)
14889{
14890 switch (value)
14891 {
14892 case DW_DEFAULTED_no:
14893 case DW_DEFAULTED_in_class:
14894 case DW_DEFAULTED_out_of_class:
14895 return true;
14896 }
14897
3142e908 14898 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14899 return false;
14900}
14901
c906108c
SS
14902/* Add a member function to the proper fieldlist. */
14903
14904static void
107d2387 14905dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14906 struct type *type, struct dwarf2_cu *cu)
c906108c 14907{
518817b3 14908 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14909 struct attribute *attr;
c906108c 14910 int i;
be2daae6 14911 struct fnfieldlist *flp = nullptr;
c906108c 14912 struct fn_field *fnp;
15d034d0 14913 const char *fieldname;
f792889a 14914 struct type *this_type;
60d5a603 14915 enum dwarf_access_attribute accessibility;
c906108c 14916
b4ba55a1 14917 if (cu->language == language_ada)
a73c6dcd 14918 error (_("unexpected member function in Ada type"));
b4ba55a1 14919
2df3850c 14920 /* Get name of member function. */
39cbfefa
DJ
14921 fieldname = dwarf2_name (die, cu);
14922 if (fieldname == NULL)
2df3850c 14923 return;
c906108c 14924
c906108c 14925 /* Look up member function name in fieldlist. */
be2daae6 14926 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14927 {
27bfe10e 14928 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14929 {
14930 flp = &fip->fnfieldlists[i];
14931 break;
14932 }
c906108c
SS
14933 }
14934
be2daae6
TT
14935 /* Create a new fnfieldlist if necessary. */
14936 if (flp == nullptr)
c906108c 14937 {
be2daae6
TT
14938 fip->fnfieldlists.emplace_back ();
14939 flp = &fip->fnfieldlists.back ();
c906108c 14940 flp->name = fieldname;
be2daae6 14941 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14942 }
14943
be2daae6
TT
14944 /* Create a new member function field and add it to the vector of
14945 fnfieldlists. */
14946 flp->fnfields.emplace_back ();
14947 fnp = &flp->fnfields.back ();
3da10d80
KS
14948
14949 /* Delay processing of the physname until later. */
9c37b5ae 14950 if (cu->language == language_cplus)
be2daae6
TT
14951 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14952 die, cu);
3da10d80
KS
14953 else
14954 {
1d06ead6 14955 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14956 fnp->physname = physname ? physname : "";
14957 }
14958
c906108c 14959 fnp->type = alloc_type (objfile);
f792889a 14960 this_type = read_type_die (die, cu);
78134374 14961 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 14962 {
f792889a 14963 int nparams = TYPE_NFIELDS (this_type);
c906108c 14964
f792889a 14965 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14966 of the method itself (TYPE_CODE_METHOD). */
14967 smash_to_method_type (fnp->type, type,
f792889a
DJ
14968 TYPE_TARGET_TYPE (this_type),
14969 TYPE_FIELDS (this_type),
14970 TYPE_NFIELDS (this_type),
14971 TYPE_VARARGS (this_type));
c906108c
SS
14972
14973 /* Handle static member functions.
c5aa993b 14974 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14975 member functions. G++ helps GDB by marking the first
14976 parameter for non-static member functions (which is the this
14977 pointer) as artificial. We obtain this information from
14978 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14979 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14980 fnp->voffset = VOFFSET_STATIC;
14981 }
14982 else
b98664d3 14983 complaint (_("member function type missing for '%s'"),
3da10d80 14984 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14985
14986 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14987 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14988 fnp->fcontext = die_containing_type (die, cu);
c906108c 14989
3e43a32a
MS
14990 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14991 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14992
14993 /* Get accessibility. */
e142c38c 14994 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14995 if (attr != nullptr)
aead7601 14996 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14997 else
14998 accessibility = dwarf2_default_access_attribute (die, cu);
14999 switch (accessibility)
c906108c 15000 {
60d5a603
JK
15001 case DW_ACCESS_private:
15002 fnp->is_private = 1;
15003 break;
15004 case DW_ACCESS_protected:
15005 fnp->is_protected = 1;
15006 break;
c906108c
SS
15007 }
15008
b02dede2 15009 /* Check for artificial methods. */
e142c38c 15010 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15011 if (attr && DW_UNSND (attr) != 0)
15012 fnp->is_artificial = 1;
15013
e35000a7
TBA
15014 /* Check for defaulted methods. */
15015 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15016 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15017 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15018
15019 /* Check for deleted methods. */
15020 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15021 if (attr != nullptr && DW_UNSND (attr) != 0)
15022 fnp->is_deleted = 1;
15023
7d27a96d
TT
15024 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15025
0d564a31 15026 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15027 function. For older versions of GCC, this is an offset in the
15028 appropriate virtual table, as specified by DW_AT_containing_type.
15029 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15030 to the object address. */
15031
e142c38c 15032 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15033 if (attr != nullptr)
8e19ed76 15034 {
4fc6c0d5 15035 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15036 {
aec5aa8b
TT
15037 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15038 {
15039 /* Old-style GCC. */
15040 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15041 }
15042 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15043 || (DW_BLOCK (attr)->size > 1
15044 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15045 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15046 {
aec5aa8b
TT
15047 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15048 if ((fnp->voffset % cu->header.addr_size) != 0)
15049 dwarf2_complex_location_expr_complaint ();
15050 else
15051 fnp->voffset /= cu->header.addr_size;
15052 fnp->voffset += 2;
15053 }
15054 else
15055 dwarf2_complex_location_expr_complaint ();
15056
15057 if (!fnp->fcontext)
7e993ebf
KS
15058 {
15059 /* If there is no `this' field and no DW_AT_containing_type,
15060 we cannot actually find a base class context for the
15061 vtable! */
15062 if (TYPE_NFIELDS (this_type) == 0
15063 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15064 {
b98664d3 15065 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15066 "function \"%s\" (offset %s)"),
15067 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15068 }
15069 else
15070 {
15071 fnp->fcontext
15072 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15073 }
15074 }
aec5aa8b 15075 }
cd6c91b4 15076 else if (attr->form_is_section_offset ())
8e19ed76 15077 {
4d3c2250 15078 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15079 }
15080 else
15081 {
4d3c2250
KB
15082 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15083 fieldname);
8e19ed76 15084 }
0d564a31 15085 }
d48cc9dd
DJ
15086 else
15087 {
15088 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15089 if (attr && DW_UNSND (attr))
15090 {
15091 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15092 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15093 "but the vtable offset is not specified"),
9d8780f0 15094 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15095 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15096 TYPE_CPLUS_DYNAMIC (type) = 1;
15097 }
15098 }
c906108c
SS
15099}
15100
15101/* Create the vector of member function fields, and attach it to the type. */
15102
15103static void
fba45db2 15104dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15105 struct dwarf2_cu *cu)
c906108c 15106{
b4ba55a1 15107 if (cu->language == language_ada)
a73c6dcd 15108 error (_("unexpected member functions in Ada type"));
b4ba55a1 15109
c906108c
SS
15110 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15111 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15112 TYPE_ALLOC (type,
15113 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15114
be2daae6 15115 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15116 {
be2daae6 15117 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15118 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15119
be2daae6
TT
15120 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15121 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15122 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15123 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15124
15125 for (int k = 0; k < nf.fnfields.size (); ++k)
15126 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15127 }
15128
be2daae6 15129 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15130}
15131
1168df01
JB
15132/* Returns non-zero if NAME is the name of a vtable member in CU's
15133 language, zero otherwise. */
15134static int
15135is_vtable_name (const char *name, struct dwarf2_cu *cu)
15136{
15137 static const char vptr[] = "_vptr";
15138
9c37b5ae
TT
15139 /* Look for the C++ form of the vtable. */
15140 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15141 return 1;
15142
15143 return 0;
15144}
15145
c0dd20ea 15146/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15147 functions, with the ABI-specified layout. If TYPE describes
15148 such a structure, smash it into a member function type.
61049d3b
DJ
15149
15150 GCC shouldn't do this; it should just output pointer to member DIEs.
15151 This is GCC PR debug/28767. */
c0dd20ea 15152
0b92b5bb
TT
15153static void
15154quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15155{
09e2d7c7 15156 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15157
15158 /* Check for a structure with no name and two children. */
78134374 15159 if (type->code () != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
0b92b5bb 15160 return;
c0dd20ea
DJ
15161
15162 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15163 if (TYPE_FIELD_NAME (type, 0) == NULL
15164 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15165 || TYPE_FIELD_NAME (type, 1) == NULL
15166 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15167 return;
c0dd20ea
DJ
15168
15169 /* Find the type of the method. */
0b92b5bb 15170 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15171 if (pfn_type == NULL
78134374
SM
15172 || pfn_type->code () != TYPE_CODE_PTR
15173 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15174 return;
c0dd20ea
DJ
15175
15176 /* Look for the "this" argument. */
15177 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15178 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15179 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15180 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15181 return;
c0dd20ea 15182
09e2d7c7 15183 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15184 new_type = alloc_type (objfile);
09e2d7c7 15185 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15186 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15187 TYPE_VARARGS (pfn_type));
0b92b5bb 15188 smash_to_methodptr_type (type, new_type);
c0dd20ea 15189}
1168df01 15190
2b4424c3
TT
15191/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15192 appropriate error checking and issuing complaints if there is a
15193 problem. */
15194
15195static ULONGEST
15196get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15197{
15198 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15199
15200 if (attr == nullptr)
15201 return 0;
15202
cd6c91b4 15203 if (!attr->form_is_constant ())
2b4424c3 15204 {
b98664d3 15205 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15206 " - DIE at %s [in module %s]"),
15207 sect_offset_str (die->sect_off),
15208 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15209 return 0;
15210 }
15211
15212 ULONGEST align;
15213 if (attr->form == DW_FORM_sdata)
15214 {
15215 LONGEST val = DW_SND (attr);
15216 if (val < 0)
15217 {
b98664d3 15218 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15219 " - DIE at %s [in module %s]"),
15220 sect_offset_str (die->sect_off),
15221 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15222 return 0;
15223 }
15224 align = val;
15225 }
15226 else
15227 align = DW_UNSND (attr);
15228
15229 if (align == 0)
15230 {
b98664d3 15231 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15232 " - DIE at %s [in module %s]"),
15233 sect_offset_str (die->sect_off),
15234 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15235 return 0;
15236 }
15237 if ((align & (align - 1)) != 0)
15238 {
b98664d3 15239 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15240 " - DIE at %s [in module %s]"),
15241 sect_offset_str (die->sect_off),
15242 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15243 return 0;
15244 }
15245
15246 return align;
15247}
15248
15249/* If the DIE has a DW_AT_alignment attribute, use its value to set
15250 the alignment for TYPE. */
15251
15252static void
15253maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15254 struct type *type)
15255{
15256 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15257 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15258 " - DIE at %s [in module %s]"),
15259 sect_offset_str (die->sect_off),
15260 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15261}
685b1105 15262
e35000a7
TBA
15263/* Check if the given VALUE is a valid enum dwarf_calling_convention
15264 constant for a type, according to DWARF5 spec, Table 5.5. */
15265
15266static bool
15267is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15268{
15269 switch (value)
15270 {
15271 case DW_CC_normal:
15272 case DW_CC_pass_by_reference:
15273 case DW_CC_pass_by_value:
15274 return true;
15275
15276 default:
15277 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15278 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15279 return false;
15280 }
15281}
15282
d0922fcf
TBA
15283/* Check if the given VALUE is a valid enum dwarf_calling_convention
15284 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15285 also according to GNU-specific values (see include/dwarf2.h). */
15286
15287static bool
15288is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15289{
15290 switch (value)
15291 {
15292 case DW_CC_normal:
15293 case DW_CC_program:
15294 case DW_CC_nocall:
15295 return true;
15296
15297 case DW_CC_GNU_renesas_sh:
15298 case DW_CC_GNU_borland_fastcall_i386:
15299 case DW_CC_GDB_IBM_OpenCL:
15300 return true;
15301
15302 default:
15303 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15304 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15305 return false;
15306 }
15307}
15308
c906108c 15309/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15310 (definition) to create a type for the structure or union. Fill in
15311 the type's name and general properties; the members will not be
83655187
DE
15312 processed until process_structure_scope. A symbol table entry for
15313 the type will also not be done until process_structure_scope (assuming
15314 the type has a name).
c906108c 15315
c767944b
DJ
15316 NOTE: we need to call these functions regardless of whether or not the
15317 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15318 structure or union. This gets the type entered into our set of
83655187 15319 user defined types. */
c906108c 15320
f792889a 15321static struct type *
134d01f1 15322read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15323{
518817b3 15324 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15325 struct type *type;
15326 struct attribute *attr;
15d034d0 15327 const char *name;
c906108c 15328
348e048f
DE
15329 /* If the definition of this type lives in .debug_types, read that type.
15330 Don't follow DW_AT_specification though, that will take us back up
15331 the chain and we want to go down. */
052c8bb8 15332 attr = die->attr (DW_AT_signature);
435d3d88 15333 if (attr != nullptr)
348e048f 15334 {
ac9ec31b 15335 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15336
ac9ec31b 15337 /* The type's CU may not be the same as CU.
02142a6c 15338 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15339 return set_die_type (die, type, cu);
15340 }
15341
c0dd20ea 15342 type = alloc_type (objfile);
c906108c 15343 INIT_CPLUS_SPECIFIC (type);
93311388 15344
39cbfefa
DJ
15345 name = dwarf2_name (die, cu);
15346 if (name != NULL)
c906108c 15347 {
987504bb 15348 if (cu->language == language_cplus
c44af4eb
TT
15349 || cu->language == language_d
15350 || cu->language == language_rust)
63d06c5c 15351 {
15d034d0 15352 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15353
15354 /* dwarf2_full_name might have already finished building the DIE's
15355 type. If so, there is no need to continue. */
15356 if (get_die_type (die, cu) != NULL)
15357 return get_die_type (die, cu);
15358
d0e39ea2 15359 type->set_name (full_name);
63d06c5c
DC
15360 }
15361 else
15362 {
d8151005
DJ
15363 /* The name is already allocated along with this objfile, so
15364 we don't need to duplicate it for the type. */
d0e39ea2 15365 type->set_name (name);
63d06c5c 15366 }
c906108c
SS
15367 }
15368
15369 if (die->tag == DW_TAG_structure_type)
15370 {
67607e24 15371 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15372 }
15373 else if (die->tag == DW_TAG_union_type)
15374 {
67607e24 15375 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15376 }
15377 else
15378 {
67607e24 15379 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15380 }
15381
0cc2414c
TT
15382 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15383 TYPE_DECLARED_CLASS (type) = 1;
15384
e35000a7
TBA
15385 /* Store the calling convention in the type if it's available in
15386 the die. Otherwise the calling convention remains set to
15387 the default value DW_CC_normal. */
15388 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15389 if (attr != nullptr
15390 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15391 {
15392 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15393 TYPE_CPLUS_CALLING_CONVENTION (type)
15394 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15395 }
15396
e142c38c 15397 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15398 if (attr != nullptr)
c906108c 15399 {
cd6c91b4 15400 if (attr->form_is_constant ())
155bfbd3
JB
15401 TYPE_LENGTH (type) = DW_UNSND (attr);
15402 else
15403 {
f8e89861
TT
15404 struct dynamic_prop prop;
15405 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15406 cu->per_cu->addr_type ()))
5c54719c 15407 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15408 TYPE_LENGTH (type) = 0;
15409 }
c906108c
SS
15410 }
15411 else
15412 {
15413 TYPE_LENGTH (type) = 0;
15414 }
15415
2b4424c3
TT
15416 maybe_set_alignment (cu, die, type);
15417
5230b05a 15418 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15419 {
5230b05a
WT
15420 /* ICC<14 does not output the required DW_AT_declaration on
15421 incomplete types, but gives them a size of zero. */
422b1cb0 15422 TYPE_STUB (type) = 1;
685b1105
JK
15423 }
15424 else
15425 TYPE_STUB_SUPPORTED (type) = 1;
15426
dc718098 15427 if (die_is_declaration (die, cu))
876cecd0 15428 TYPE_STUB (type) = 1;
a6c727b2
DJ
15429 else if (attr == NULL && die->child == NULL
15430 && producer_is_realview (cu->producer))
15431 /* RealView does not output the required DW_AT_declaration
15432 on incomplete types. */
15433 TYPE_STUB (type) = 1;
dc718098 15434
c906108c
SS
15435 /* We need to add the type field to the die immediately so we don't
15436 infinitely recurse when dealing with pointers to the structure
0963b4bd 15437 type within the structure itself. */
1c379e20 15438 set_die_type (die, type, cu);
c906108c 15439
7e314c57
JK
15440 /* set_die_type should be already done. */
15441 set_descriptive_type (type, die, cu);
15442
c767944b
DJ
15443 return type;
15444}
15445
9c6a1327
TT
15446static void handle_struct_member_die
15447 (struct die_info *child_die,
15448 struct type *type,
15449 struct field_info *fi,
15450 std::vector<struct symbol *> *template_args,
15451 struct dwarf2_cu *cu);
15452
15453/* A helper for handle_struct_member_die that handles
15454 DW_TAG_variant_part. */
15455
15456static void
15457handle_variant_part (struct die_info *die, struct type *type,
15458 struct field_info *fi,
15459 std::vector<struct symbol *> *template_args,
15460 struct dwarf2_cu *cu)
15461{
15462 variant_part_builder *new_part;
15463 if (fi->current_variant_part == nullptr)
15464 {
15465 fi->variant_parts.emplace_back ();
15466 new_part = &fi->variant_parts.back ();
15467 }
15468 else if (!fi->current_variant_part->processing_variant)
15469 {
15470 complaint (_("nested DW_TAG_variant_part seen "
15471 "- DIE at %s [in module %s]"),
15472 sect_offset_str (die->sect_off),
15473 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15474 return;
15475 }
15476 else
15477 {
15478 variant_field &current = fi->current_variant_part->variants.back ();
15479 current.variant_parts.emplace_back ();
15480 new_part = &current.variant_parts.back ();
15481 }
15482
15483 /* When we recurse, we want callees to add to this new variant
15484 part. */
15485 scoped_restore save_current_variant_part
15486 = make_scoped_restore (&fi->current_variant_part, new_part);
15487
15488 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15489 if (discr == NULL)
15490 {
15491 /* It's a univariant form, an extension we support. */
15492 }
15493 else if (discr->form_is_ref ())
15494 {
15495 struct dwarf2_cu *target_cu = cu;
15496 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15497
15498 new_part->discriminant_offset = target_die->sect_off;
15499 }
15500 else
15501 {
15502 complaint (_("DW_AT_discr does not have DIE reference form"
15503 " - DIE at %s [in module %s]"),
15504 sect_offset_str (die->sect_off),
15505 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15506 }
15507
15508 for (die_info *child_die = die->child;
15509 child_die != NULL;
15510 child_die = child_die->sibling)
15511 handle_struct_member_die (child_die, type, fi, template_args, cu);
15512}
15513
15514/* A helper for handle_struct_member_die that handles
15515 DW_TAG_variant. */
15516
15517static void
15518handle_variant (struct die_info *die, struct type *type,
15519 struct field_info *fi,
15520 std::vector<struct symbol *> *template_args,
15521 struct dwarf2_cu *cu)
15522{
15523 if (fi->current_variant_part == nullptr)
15524 {
15525 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15526 "- DIE at %s [in module %s]"),
15527 sect_offset_str (die->sect_off),
15528 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15529 return;
15530 }
15531 if (fi->current_variant_part->processing_variant)
15532 {
15533 complaint (_("nested DW_TAG_variant seen "
15534 "- DIE at %s [in module %s]"),
15535 sect_offset_str (die->sect_off),
15536 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15537 return;
15538 }
15539
15540 scoped_restore save_processing_variant
15541 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15542 true);
15543
15544 fi->current_variant_part->variants.emplace_back ();
15545 variant_field &variant = fi->current_variant_part->variants.back ();
15546 variant.first_field = fi->fields.size ();
15547
15548 /* In a variant we want to get the discriminant and also add a
15549 field for our sole member child. */
15550 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15551 if (discr == nullptr)
15552 {
15553 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15554 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15555 variant.default_branch = true;
15556 else
15557 variant.discr_list_data = DW_BLOCK (discr);
15558 }
15559 else
15560 variant.discriminant_value = DW_UNSND (discr);
15561
15562 for (die_info *variant_child = die->child;
15563 variant_child != NULL;
15564 variant_child = variant_child->sibling)
15565 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15566
15567 variant.last_field = fi->fields.size ();
15568}
15569
2ddeaf8a
TT
15570/* A helper for process_structure_scope that handles a single member
15571 DIE. */
15572
15573static void
15574handle_struct_member_die (struct die_info *child_die, struct type *type,
15575 struct field_info *fi,
15576 std::vector<struct symbol *> *template_args,
15577 struct dwarf2_cu *cu)
15578{
15579 if (child_die->tag == DW_TAG_member
9c6a1327 15580 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15581 {
15582 /* NOTE: carlton/2002-11-05: A C++ static data member
15583 should be a DW_TAG_member that is a declaration, but
15584 all versions of G++ as of this writing (so through at
15585 least 3.2.1) incorrectly generate DW_TAG_variable
15586 tags for them instead. */
15587 dwarf2_add_field (fi, child_die, cu);
15588 }
15589 else if (child_die->tag == DW_TAG_subprogram)
15590 {
15591 /* Rust doesn't have member functions in the C++ sense.
15592 However, it does emit ordinary functions as children
15593 of a struct DIE. */
15594 if (cu->language == language_rust)
15595 read_func_scope (child_die, cu);
15596 else
15597 {
15598 /* C++ member function. */
15599 dwarf2_add_member_fn (fi, child_die, type, cu);
15600 }
15601 }
15602 else if (child_die->tag == DW_TAG_inheritance)
15603 {
15604 /* C++ base class field. */
15605 dwarf2_add_field (fi, child_die, cu);
15606 }
15607 else if (type_can_define_types (child_die))
15608 dwarf2_add_type_defn (fi, child_die, cu);
15609 else if (child_die->tag == DW_TAG_template_type_param
15610 || child_die->tag == DW_TAG_template_value_param)
15611 {
15612 struct symbol *arg = new_symbol (child_die, NULL, cu);
15613
15614 if (arg != NULL)
15615 template_args->push_back (arg);
15616 }
9c6a1327
TT
15617 else if (child_die->tag == DW_TAG_variant_part)
15618 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15619 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15620 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15621}
15622
c767944b
DJ
15623/* Finish creating a structure or union type, including filling in
15624 its members and creating a symbol for it. */
15625
15626static void
15627process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15628{
518817b3 15629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15630 struct die_info *child_die;
c767944b
DJ
15631 struct type *type;
15632
15633 type = get_die_type (die, cu);
15634 if (type == NULL)
15635 type = read_structure_type (die, cu);
15636
3e1d3d8c 15637 bool has_template_parameters = false;
e142c38c 15638 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15639 {
15640 struct field_info fi;
2f4732b0 15641 std::vector<struct symbol *> template_args;
c906108c 15642
639d11d3 15643 child_die = die->child;
c906108c
SS
15644
15645 while (child_die && child_die->tag)
15646 {
2ddeaf8a 15647 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15648 child_die = child_die->sibling;
c906108c
SS
15649 }
15650
34eaf542 15651 /* Attach template arguments to type. */
2f4732b0 15652 if (!template_args.empty ())
34eaf542 15653 {
3e1d3d8c 15654 has_template_parameters = true;
34eaf542 15655 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15656 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15657 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15658 = XOBNEWVEC (&objfile->objfile_obstack,
15659 struct symbol *,
15660 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15661 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15662 template_args.data (),
34eaf542
TT
15663 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15664 * sizeof (struct symbol *)));
34eaf542
TT
15665 }
15666
c906108c 15667 /* Attach fields and member functions to the type. */
317f7127 15668 if (fi.nfields () > 0)
e7c27a73 15669 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15670 if (!fi.fnfieldlists.empty ())
c906108c 15671 {
e7c27a73 15672 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15673
c5aa993b 15674 /* Get the type which refers to the base class (possibly this
c906108c 15675 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15676 class from the DW_AT_containing_type attribute. This use of
15677 DW_AT_containing_type is a GNU extension. */
c906108c 15678
e142c38c 15679 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15680 {
e7c27a73 15681 struct type *t = die_containing_type (die, cu);
c906108c 15682
ae6ae975 15683 set_type_vptr_basetype (type, t);
c906108c
SS
15684 if (type == t)
15685 {
c906108c
SS
15686 int i;
15687
15688 /* Our own class provides vtbl ptr. */
15689 for (i = TYPE_NFIELDS (t) - 1;
15690 i >= TYPE_N_BASECLASSES (t);
15691 --i)
15692 {
0d5cff50 15693 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15694
1168df01 15695 if (is_vtable_name (fieldname, cu))
c906108c 15696 {
ae6ae975 15697 set_type_vptr_fieldno (type, i);
c906108c
SS
15698 break;
15699 }
15700 }
15701
15702 /* Complain if virtual function table field not found. */
15703 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15704 complaint (_("virtual function table pointer "
3e43a32a 15705 "not found when defining class '%s'"),
7d93a1e0 15706 type->name () ? type->name () : "");
c906108c
SS
15707 }
15708 else
15709 {
ae6ae975 15710 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15711 }
15712 }
f6235d4c 15713 else if (cu->producer
61012eef 15714 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15715 {
15716 /* The IBM XLC compiler does not provide direct indication
15717 of the containing type, but the vtable pointer is
15718 always named __vfp. */
15719
15720 int i;
15721
15722 for (i = TYPE_NFIELDS (type) - 1;
15723 i >= TYPE_N_BASECLASSES (type);
15724 --i)
15725 {
15726 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15727 {
ae6ae975
DE
15728 set_type_vptr_fieldno (type, i);
15729 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15730 break;
15731 }
15732 }
15733 }
c906108c 15734 }
98751a41
JK
15735
15736 /* Copy fi.typedef_field_list linked list elements content into the
15737 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15738 if (!fi.typedef_field_list.empty ())
98751a41 15739 {
be2daae6 15740 int count = fi.typedef_field_list.size ();
98751a41 15741
a0d7a4ff 15742 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15743 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15744 = ((struct decl_field *)
be2daae6
TT
15745 TYPE_ALLOC (type,
15746 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15747 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15748
be2daae6
TT
15749 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15750 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15751 }
c767944b 15752
883fd55a
KS
15753 /* Copy fi.nested_types_list linked list elements content into the
15754 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15755 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15756 {
be2daae6 15757 int count = fi.nested_types_list.size ();
883fd55a
KS
15758
15759 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15760 TYPE_NESTED_TYPES_ARRAY (type)
15761 = ((struct decl_field *)
be2daae6
TT
15762 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15763 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15764
be2daae6
TT
15765 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15766 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15767 }
c906108c 15768 }
63d06c5c 15769
bb5ed363 15770 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15771 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15772 cu->rust_unions.push_back (type);
0b92b5bb 15773
90aeadfc
DC
15774 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15775 snapshots) has been known to create a die giving a declaration
15776 for a class that has, as a child, a die giving a definition for a
15777 nested class. So we have to process our children even if the
15778 current die is a declaration. Normally, of course, a declaration
15779 won't have any children at all. */
134d01f1 15780
ca040673
DE
15781 child_die = die->child;
15782
90aeadfc
DC
15783 while (child_die != NULL && child_die->tag)
15784 {
15785 if (child_die->tag == DW_TAG_member
15786 || child_die->tag == DW_TAG_variable
34eaf542
TT
15787 || child_die->tag == DW_TAG_inheritance
15788 || child_die->tag == DW_TAG_template_value_param
15789 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15790 {
90aeadfc 15791 /* Do nothing. */
134d01f1 15792 }
90aeadfc
DC
15793 else
15794 process_die (child_die, cu);
134d01f1 15795
436c571c 15796 child_die = child_die->sibling;
134d01f1
DJ
15797 }
15798
fa4028e9
JB
15799 /* Do not consider external references. According to the DWARF standard,
15800 these DIEs are identified by the fact that they have no byte_size
15801 attribute, and a declaration attribute. */
15802 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15803 || !die_is_declaration (die, cu)
15804 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15805 {
15806 struct symbol *sym = new_symbol (die, type, cu);
15807
15808 if (has_template_parameters)
15809 {
a776957c
TT
15810 struct symtab *symtab;
15811 if (sym != nullptr)
15812 symtab = symbol_symtab (sym);
15813 else if (cu->line_header != nullptr)
15814 {
15815 /* Any related symtab will do. */
15816 symtab
7ba99d21 15817 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15818 }
15819 else
15820 {
15821 symtab = nullptr;
15822 complaint (_("could not find suitable "
15823 "symtab for template parameter"
15824 " - DIE at %s [in module %s]"),
15825 sect_offset_str (die->sect_off),
15826 objfile_name (objfile));
15827 }
15828
15829 if (symtab != nullptr)
15830 {
15831 /* Make sure that the symtab is set on the new symbols.
15832 Even though they don't appear in this symtab directly,
15833 other parts of gdb assume that symbols do, and this is
15834 reasonably true. */
15835 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15836 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15837 }
3e1d3d8c
TT
15838 }
15839 }
134d01f1
DJ
15840}
15841
ed6acedd
TT
15842/* Assuming DIE is an enumeration type, and TYPE is its associated
15843 type, update TYPE using some information only available in DIE's
15844 children. In particular, the fields are computed. */
55426c9d
JB
15845
15846static void
15847update_enumeration_type_from_children (struct die_info *die,
15848 struct type *type,
15849 struct dwarf2_cu *cu)
15850{
60f7655a 15851 struct die_info *child_die;
55426c9d
JB
15852 int unsigned_enum = 1;
15853 int flag_enum = 1;
55426c9d 15854
8268c778 15855 auto_obstack obstack;
ed6acedd 15856 std::vector<struct field> fields;
55426c9d 15857
60f7655a
DE
15858 for (child_die = die->child;
15859 child_die != NULL && child_die->tag;
436c571c 15860 child_die = child_die->sibling)
55426c9d
JB
15861 {
15862 struct attribute *attr;
15863 LONGEST value;
15864 const gdb_byte *bytes;
15865 struct dwarf2_locexpr_baton *baton;
15866 const char *name;
60f7655a 15867
55426c9d
JB
15868 if (child_die->tag != DW_TAG_enumerator)
15869 continue;
15870
15871 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15872 if (attr == NULL)
15873 continue;
15874
15875 name = dwarf2_name (child_die, cu);
15876 if (name == NULL)
15877 name = "<anonymous enumerator>";
15878
15879 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15880 &value, &bytes, &baton);
15881 if (value < 0)
15882 {
15883 unsigned_enum = 0;
15884 flag_enum = 0;
15885 }
55426c9d 15886 else
edd45eb0
SM
15887 {
15888 if (count_one_bits_ll (value) >= 2)
15889 flag_enum = 0;
edd45eb0 15890 }
55426c9d 15891
ed6acedd
TT
15892 fields.emplace_back ();
15893 struct field &field = fields.back ();
15894 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15895 SET_FIELD_ENUMVAL (field, value);
15896 }
15897
15898 if (!fields.empty ())
15899 {
15900 TYPE_NFIELDS (type) = fields.size ();
15901 TYPE_FIELDS (type) = (struct field *)
15902 TYPE_ALLOC (type, sizeof (struct field) * fields.size ());
15903 memcpy (TYPE_FIELDS (type), fields.data (),
15904 sizeof (struct field) * fields.size ());
55426c9d
JB
15905 }
15906
15907 if (unsigned_enum)
15908 TYPE_UNSIGNED (type) = 1;
15909 if (flag_enum)
15910 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15911}
15912
134d01f1
DJ
15913/* Given a DW_AT_enumeration_type die, set its type. We do not
15914 complete the type's fields yet, or create any symbols. */
c906108c 15915
f792889a 15916static struct type *
134d01f1 15917read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15918{
518817b3 15919 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15920 struct type *type;
c906108c 15921 struct attribute *attr;
0114d602 15922 const char *name;
134d01f1 15923
348e048f
DE
15924 /* If the definition of this type lives in .debug_types, read that type.
15925 Don't follow DW_AT_specification though, that will take us back up
15926 the chain and we want to go down. */
052c8bb8 15927 attr = die->attr (DW_AT_signature);
435d3d88 15928 if (attr != nullptr)
348e048f 15929 {
ac9ec31b 15930 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15931
ac9ec31b 15932 /* The type's CU may not be the same as CU.
02142a6c 15933 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15934 return set_die_type (die, type, cu);
15935 }
15936
c906108c
SS
15937 type = alloc_type (objfile);
15938
67607e24 15939 type->set_code (TYPE_CODE_ENUM);
94af9270 15940 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15941 if (name != NULL)
d0e39ea2 15942 type->set_name (name);
c906108c 15943
0626fc76
TT
15944 attr = dwarf2_attr (die, DW_AT_type, cu);
15945 if (attr != NULL)
15946 {
15947 struct type *underlying_type = die_type (die, cu);
15948
15949 TYPE_TARGET_TYPE (type) = underlying_type;
15950 }
15951
e142c38c 15952 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15953 if (attr != nullptr)
c906108c
SS
15954 {
15955 TYPE_LENGTH (type) = DW_UNSND (attr);
15956 }
15957 else
15958 {
15959 TYPE_LENGTH (type) = 0;
15960 }
15961
2b4424c3
TT
15962 maybe_set_alignment (cu, die, type);
15963
137033e9
JB
15964 /* The enumeration DIE can be incomplete. In Ada, any type can be
15965 declared as private in the package spec, and then defined only
15966 inside the package body. Such types are known as Taft Amendment
15967 Types. When another package uses such a type, an incomplete DIE
15968 may be generated by the compiler. */
02eb380e 15969 if (die_is_declaration (die, cu))
876cecd0 15970 TYPE_STUB (type) = 1;
02eb380e 15971
0626fc76
TT
15972 /* If this type has an underlying type that is not a stub, then we
15973 may use its attributes. We always use the "unsigned" attribute
15974 in this situation, because ordinarily we guess whether the type
15975 is unsigned -- but the guess can be wrong and the underlying type
15976 can tell us the reality. However, we defer to a local size
15977 attribute if one exists, because this lets the compiler override
15978 the underlying type if needed. */
15979 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15980 {
9e7c9a03
HD
15981 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15982 underlying_type = check_typedef (underlying_type);
15983 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 15984 if (TYPE_LENGTH (type) == 0)
9e7c9a03 15985 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 15986 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
15987 && TYPE_RAW_ALIGN (underlying_type) != 0)
15988 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
15989 }
15990
3d567982
TT
15991 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15992
ed6acedd
TT
15993 set_die_type (die, type, cu);
15994
15995 /* Finish the creation of this type by using the enum's children.
15996 Note that, as usual, this must come after set_die_type to avoid
15997 infinite recursion when trying to compute the names of the
15998 enumerators. */
15999 update_enumeration_type_from_children (die, type, cu);
16000
16001 return type;
134d01f1
DJ
16002}
16003
16004/* Given a pointer to a die which begins an enumeration, process all
16005 the dies that define the members of the enumeration, and create the
16006 symbol for the enumeration type.
16007
16008 NOTE: We reverse the order of the element list. */
16009
16010static void
16011process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16012{
f792889a 16013 struct type *this_type;
134d01f1 16014
f792889a
DJ
16015 this_type = get_die_type (die, cu);
16016 if (this_type == NULL)
16017 this_type = read_enumeration_type (die, cu);
9dc481d3 16018
639d11d3 16019 if (die->child != NULL)
c906108c 16020 {
9dc481d3 16021 struct die_info *child_die;
15d034d0 16022 const char *name;
9dc481d3 16023
639d11d3 16024 child_die = die->child;
c906108c
SS
16025 while (child_die && child_die->tag)
16026 {
16027 if (child_die->tag != DW_TAG_enumerator)
16028 {
e7c27a73 16029 process_die (child_die, cu);
c906108c
SS
16030 }
16031 else
16032 {
39cbfefa
DJ
16033 name = dwarf2_name (child_die, cu);
16034 if (name)
ed6acedd 16035 new_symbol (child_die, this_type, cu);
c906108c
SS
16036 }
16037
436c571c 16038 child_die = child_die->sibling;
c906108c 16039 }
c906108c 16040 }
134d01f1 16041
6c83ed52
TT
16042 /* If we are reading an enum from a .debug_types unit, and the enum
16043 is a declaration, and the enum is not the signatured type in the
16044 unit, then we do not want to add a symbol for it. Adding a
16045 symbol would in some cases obscure the true definition of the
16046 enum, giving users an incomplete type when the definition is
16047 actually available. Note that we do not want to do this for all
16048 enums which are just declarations, because C++0x allows forward
16049 enum declarations. */
3019eac3 16050 if (cu->per_cu->is_debug_types
6c83ed52
TT
16051 && die_is_declaration (die, cu))
16052 {
52dc124a 16053 struct signatured_type *sig_type;
6c83ed52 16054
c0f78cd4 16055 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16056 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16057 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16058 return;
16059 }
16060
f792889a 16061 new_symbol (die, this_type, cu);
c906108c
SS
16062}
16063
16064/* Extract all information from a DW_TAG_array_type DIE and put it in
16065 the DIE's type field. For now, this only handles one dimensional
16066 arrays. */
16067
f792889a 16068static struct type *
e7c27a73 16069read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16070{
518817b3 16071 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16072 struct die_info *child_die;
7e314c57 16073 struct type *type;
c906108c 16074 struct type *element_type, *range_type, *index_type;
c906108c 16075 struct attribute *attr;
15d034d0 16076 const char *name;
a405673c 16077 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16078 unsigned int bit_stride = 0;
c906108c 16079
e7c27a73 16080 element_type = die_type (die, cu);
c906108c 16081
7e314c57
JK
16082 /* The die_type call above may have already set the type for this DIE. */
16083 type = get_die_type (die, cu);
16084 if (type)
16085 return type;
16086
dc53a7ad
JB
16087 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16088 if (attr != NULL)
a405673c
JB
16089 {
16090 int stride_ok;
09ba997f 16091 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
16092
16093 byte_stride_prop
16094 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16095 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16096 prop_type);
a405673c
JB
16097 if (!stride_ok)
16098 {
b98664d3 16099 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16100 " - DIE at %s [in module %s]"),
16101 sect_offset_str (die->sect_off),
518817b3 16102 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16103 /* Ignore this attribute. We will likely not be able to print
16104 arrays of this type correctly, but there is little we can do
16105 to help if we cannot read the attribute's value. */
16106 byte_stride_prop = NULL;
16107 }
16108 }
dc53a7ad
JB
16109
16110 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16111 if (attr != NULL)
16112 bit_stride = DW_UNSND (attr);
16113
c906108c
SS
16114 /* Irix 6.2 native cc creates array types without children for
16115 arrays with unspecified length. */
639d11d3 16116 if (die->child == NULL)
c906108c 16117 {
46bf5051 16118 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16119 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16120 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16121 byte_stride_prop, bit_stride);
f792889a 16122 return set_die_type (die, type, cu);
c906108c
SS
16123 }
16124
791afaa2 16125 std::vector<struct type *> range_types;
639d11d3 16126 child_die = die->child;
c906108c
SS
16127 while (child_die && child_die->tag)
16128 {
16129 if (child_die->tag == DW_TAG_subrange_type)
16130 {
f792889a 16131 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16132
f792889a 16133 if (child_type != NULL)
a02abb62 16134 {
0963b4bd
MS
16135 /* The range type was succesfully read. Save it for the
16136 array type creation. */
791afaa2 16137 range_types.push_back (child_type);
a02abb62 16138 }
c906108c 16139 }
436c571c 16140 child_die = child_die->sibling;
c906108c
SS
16141 }
16142
16143 /* Dwarf2 dimensions are output from left to right, create the
16144 necessary array types in backwards order. */
7ca2d3a3 16145
c906108c 16146 type = element_type;
7ca2d3a3
DL
16147
16148 if (read_array_order (die, cu) == DW_ORD_col_major)
16149 {
16150 int i = 0;
9a619af0 16151
791afaa2 16152 while (i < range_types.size ())
dc53a7ad 16153 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16154 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16155 }
16156 else
16157 {
791afaa2 16158 size_t ndim = range_types.size ();
7ca2d3a3 16159 while (ndim-- > 0)
dc53a7ad 16160 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16161 byte_stride_prop, bit_stride);
7ca2d3a3 16162 }
c906108c 16163
f5f8a009
EZ
16164 /* Understand Dwarf2 support for vector types (like they occur on
16165 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16166 array type. This is not part of the Dwarf2/3 standard yet, but a
16167 custom vendor extension. The main difference between a regular
16168 array and the vector variant is that vectors are passed by value
16169 to functions. */
e142c38c 16170 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16171 if (attr != nullptr)
ea37ba09 16172 make_vector_type (type);
f5f8a009 16173
dbc98a8b
KW
16174 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16175 implementation may choose to implement triple vectors using this
16176 attribute. */
16177 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16178 if (attr != nullptr)
dbc98a8b
KW
16179 {
16180 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16181 TYPE_LENGTH (type) = DW_UNSND (attr);
16182 else
b98664d3 16183 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16184 "than the total size of elements"));
dbc98a8b
KW
16185 }
16186
39cbfefa
DJ
16187 name = dwarf2_name (die, cu);
16188 if (name)
d0e39ea2 16189 type->set_name (name);
6e70227d 16190
2b4424c3
TT
16191 maybe_set_alignment (cu, die, type);
16192
0963b4bd 16193 /* Install the type in the die. */
7e314c57
JK
16194 set_die_type (die, type, cu);
16195
16196 /* set_die_type should be already done. */
b4ba55a1
JB
16197 set_descriptive_type (type, die, cu);
16198
7e314c57 16199 return type;
c906108c
SS
16200}
16201
7ca2d3a3 16202static enum dwarf_array_dim_ordering
6e70227d 16203read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16204{
16205 struct attribute *attr;
16206
16207 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16208
435d3d88 16209 if (attr != nullptr)
aead7601 16210 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16211
0963b4bd
MS
16212 /* GNU F77 is a special case, as at 08/2004 array type info is the
16213 opposite order to the dwarf2 specification, but data is still
16214 laid out as per normal fortran.
7ca2d3a3 16215
0963b4bd
MS
16216 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16217 version checking. */
7ca2d3a3 16218
905e0470
PM
16219 if (cu->language == language_fortran
16220 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16221 {
16222 return DW_ORD_row_major;
16223 }
16224
6e70227d 16225 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16226 {
16227 case array_column_major:
16228 return DW_ORD_col_major;
16229 case array_row_major:
16230 default:
16231 return DW_ORD_row_major;
16232 };
16233}
16234
72019c9c 16235/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16236 the DIE's type field. */
72019c9c 16237
f792889a 16238static struct type *
72019c9c
GM
16239read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16240{
7e314c57
JK
16241 struct type *domain_type, *set_type;
16242 struct attribute *attr;
f792889a 16243
7e314c57
JK
16244 domain_type = die_type (die, cu);
16245
16246 /* The die_type call above may have already set the type for this DIE. */
16247 set_type = get_die_type (die, cu);
16248 if (set_type)
16249 return set_type;
16250
16251 set_type = create_set_type (NULL, domain_type);
16252
16253 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16254 if (attr != nullptr)
d09039dd 16255 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16256
2b4424c3
TT
16257 maybe_set_alignment (cu, die, set_type);
16258
f792889a 16259 return set_die_type (die, set_type, cu);
72019c9c 16260}
7ca2d3a3 16261
0971de02
TT
16262/* A helper for read_common_block that creates a locexpr baton.
16263 SYM is the symbol which we are marking as computed.
16264 COMMON_DIE is the DIE for the common block.
16265 COMMON_LOC is the location expression attribute for the common
16266 block itself.
16267 MEMBER_LOC is the location expression attribute for the particular
16268 member of the common block that we are processing.
16269 CU is the CU from which the above come. */
16270
16271static void
16272mark_common_block_symbol_computed (struct symbol *sym,
16273 struct die_info *common_die,
16274 struct attribute *common_loc,
16275 struct attribute *member_loc,
16276 struct dwarf2_cu *cu)
16277{
518817b3
SM
16278 struct dwarf2_per_objfile *dwarf2_per_objfile
16279 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16280 struct objfile *objfile = dwarf2_per_objfile->objfile;
16281 struct dwarf2_locexpr_baton *baton;
16282 gdb_byte *ptr;
16283 unsigned int cu_off;
08feed99 16284 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16285 LONGEST offset = 0;
16286
16287 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16288 gdb_assert (common_loc->form_is_block ());
16289 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16290 || member_loc->form_is_constant ());
0971de02 16291
8d749320 16292 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16293 baton->per_cu = cu->per_cu;
16294 gdb_assert (baton->per_cu);
16295
16296 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16297
cd6c91b4 16298 if (member_loc->form_is_constant ())
0971de02 16299 {
0826b30a 16300 offset = member_loc->constant_value (0);
0971de02
TT
16301 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16302 }
16303 else
16304 baton->size += DW_BLOCK (member_loc)->size;
16305
224c3ddb 16306 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16307 baton->data = ptr;
16308
16309 *ptr++ = DW_OP_call4;
9c541725 16310 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16311 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16312 ptr += 4;
16313
cd6c91b4 16314 if (member_loc->form_is_constant ())
0971de02
TT
16315 {
16316 *ptr++ = DW_OP_addr;
16317 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16318 ptr += cu->header.addr_size;
16319 }
16320 else
16321 {
16322 /* We have to copy the data here, because DW_OP_call4 will only
16323 use a DW_AT_location attribute. */
16324 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16325 ptr += DW_BLOCK (member_loc)->size;
16326 }
16327
16328 *ptr++ = DW_OP_plus;
16329 gdb_assert (ptr - baton->data == baton->size);
16330
0971de02 16331 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16332 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16333}
16334
4357ac6c
TT
16335/* Create appropriate locally-scoped variables for all the
16336 DW_TAG_common_block entries. Also create a struct common_block
16337 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16338 is used to separate the common blocks name namespace from regular
4357ac6c 16339 variable names. */
c906108c
SS
16340
16341static void
e7c27a73 16342read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16343{
0971de02
TT
16344 struct attribute *attr;
16345
16346 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16347 if (attr != nullptr)
0971de02
TT
16348 {
16349 /* Support the .debug_loc offsets. */
4fc6c0d5 16350 if (attr->form_is_block ())
0971de02
TT
16351 {
16352 /* Ok. */
16353 }
cd6c91b4 16354 else if (attr->form_is_section_offset ())
0971de02
TT
16355 {
16356 dwarf2_complex_location_expr_complaint ();
16357 attr = NULL;
16358 }
16359 else
16360 {
16361 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16362 "common block member");
16363 attr = NULL;
16364 }
16365 }
16366
639d11d3 16367 if (die->child != NULL)
c906108c 16368 {
518817b3 16369 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16370 struct die_info *child_die;
16371 size_t n_entries = 0, size;
16372 struct common_block *common_block;
16373 struct symbol *sym;
74ac6d43 16374
4357ac6c
TT
16375 for (child_die = die->child;
16376 child_die && child_die->tag;
436c571c 16377 child_die = child_die->sibling)
4357ac6c
TT
16378 ++n_entries;
16379
16380 size = (sizeof (struct common_block)
16381 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16382 common_block
16383 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16384 size);
4357ac6c
TT
16385 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16386 common_block->n_entries = 0;
16387
16388 for (child_die = die->child;
16389 child_die && child_die->tag;
436c571c 16390 child_die = child_die->sibling)
4357ac6c
TT
16391 {
16392 /* Create the symbol in the DW_TAG_common_block block in the current
16393 symbol scope. */
e7c27a73 16394 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16395 if (sym != NULL)
16396 {
16397 struct attribute *member_loc;
16398
16399 common_block->contents[common_block->n_entries++] = sym;
16400
16401 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16402 cu);
16403 if (member_loc)
16404 {
16405 /* GDB has handled this for a long time, but it is
16406 not specified by DWARF. It seems to have been
16407 emitted by gfortran at least as recently as:
16408 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16409 complaint (_("Variable in common block has "
0971de02 16410 "DW_AT_data_member_location "
9d8780f0
SM
16411 "- DIE at %s [in module %s]"),
16412 sect_offset_str (child_die->sect_off),
518817b3 16413 objfile_name (objfile));
0971de02 16414
cd6c91b4 16415 if (member_loc->form_is_section_offset ())
0971de02 16416 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16417 else if (member_loc->form_is_constant ()
4fc6c0d5 16418 || member_loc->form_is_block ())
0971de02 16419 {
435d3d88 16420 if (attr != nullptr)
0971de02
TT
16421 mark_common_block_symbol_computed (sym, die, attr,
16422 member_loc, cu);
16423 }
16424 else
16425 dwarf2_complex_location_expr_complaint ();
16426 }
16427 }
c906108c 16428 }
4357ac6c
TT
16429
16430 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16431 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16432 }
16433}
16434
0114d602 16435/* Create a type for a C++ namespace. */
d9fa45fe 16436
0114d602
DJ
16437static struct type *
16438read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16439{
518817b3 16440 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16441 const char *previous_prefix, *name;
9219021c 16442 int is_anonymous;
0114d602
DJ
16443 struct type *type;
16444
16445 /* For extensions, reuse the type of the original namespace. */
16446 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16447 {
16448 struct die_info *ext_die;
16449 struct dwarf2_cu *ext_cu = cu;
9a619af0 16450
0114d602
DJ
16451 ext_die = dwarf2_extension (die, &ext_cu);
16452 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16453
16454 /* EXT_CU may not be the same as CU.
02142a6c 16455 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16456 return set_die_type (die, type, cu);
16457 }
9219021c 16458
e142c38c 16459 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16460
16461 /* Now build the name of the current namespace. */
16462
0114d602
DJ
16463 previous_prefix = determine_prefix (die, cu);
16464 if (previous_prefix[0] != '\0')
16465 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16466 previous_prefix, name, 0, cu);
0114d602
DJ
16467
16468 /* Create the type. */
19f392bc 16469 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16470
60531b24 16471 return set_die_type (die, type, cu);
0114d602
DJ
16472}
16473
22cee43f 16474/* Read a namespace scope. */
0114d602
DJ
16475
16476static void
16477read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16478{
518817b3 16479 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16480 int is_anonymous;
9219021c 16481
5c4e30ca
DC
16482 /* Add a symbol associated to this if we haven't seen the namespace
16483 before. Also, add a using directive if it's an anonymous
16484 namespace. */
9219021c 16485
f2f0e013 16486 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16487 {
16488 struct type *type;
16489
0114d602 16490 type = read_type_die (die, cu);
e7c27a73 16491 new_symbol (die, type, cu);
5c4e30ca 16492
e8e80198 16493 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16494 if (is_anonymous)
0114d602
DJ
16495 {
16496 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16497
eb1e02fd 16498 std::vector<const char *> excludes;
804d2729 16499 add_using_directive (using_directives (cu),
7d93a1e0 16500 previous_prefix, type->name (), NULL,
eb1e02fd 16501 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16502 }
5c4e30ca 16503 }
9219021c 16504
639d11d3 16505 if (die->child != NULL)
d9fa45fe 16506 {
639d11d3 16507 struct die_info *child_die = die->child;
6e70227d 16508
d9fa45fe
DC
16509 while (child_die && child_die->tag)
16510 {
e7c27a73 16511 process_die (child_die, cu);
436c571c 16512 child_die = child_die->sibling;
d9fa45fe
DC
16513 }
16514 }
38d518c9
EZ
16515}
16516
f55ee35c
JK
16517/* Read a Fortran module as type. This DIE can be only a declaration used for
16518 imported module. Still we need that type as local Fortran "use ... only"
16519 declaration imports depend on the created type in determine_prefix. */
16520
16521static struct type *
16522read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16523{
518817b3 16524 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16525 const char *module_name;
f55ee35c
JK
16526 struct type *type;
16527
16528 module_name = dwarf2_name (die, cu);
19f392bc 16529 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16530
f55ee35c
JK
16531 return set_die_type (die, type, cu);
16532}
16533
5d7cb8df
JK
16534/* Read a Fortran module. */
16535
16536static void
16537read_module (struct die_info *die, struct dwarf2_cu *cu)
16538{
16539 struct die_info *child_die = die->child;
530e8392
KB
16540 struct type *type;
16541
16542 type = read_type_die (die, cu);
16543 new_symbol (die, type, cu);
5d7cb8df 16544
5d7cb8df
JK
16545 while (child_die && child_die->tag)
16546 {
16547 process_die (child_die, cu);
436c571c 16548 child_die = child_die->sibling;
5d7cb8df
JK
16549 }
16550}
16551
38d518c9
EZ
16552/* Return the name of the namespace represented by DIE. Set
16553 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16554 namespace. */
16555
16556static const char *
e142c38c 16557namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16558{
16559 struct die_info *current_die;
16560 const char *name = NULL;
16561
16562 /* Loop through the extensions until we find a name. */
16563
16564 for (current_die = die;
16565 current_die != NULL;
f2f0e013 16566 current_die = dwarf2_extension (die, &cu))
38d518c9 16567 {
96553a0c
DE
16568 /* We don't use dwarf2_name here so that we can detect the absence
16569 of a name -> anonymous namespace. */
7d45c7c3 16570 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16571
38d518c9
EZ
16572 if (name != NULL)
16573 break;
16574 }
16575
16576 /* Is it an anonymous namespace? */
16577
16578 *is_anonymous = (name == NULL);
16579 if (*is_anonymous)
2b1dbab0 16580 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16581
16582 return name;
d9fa45fe
DC
16583}
16584
c906108c
SS
16585/* Extract all information from a DW_TAG_pointer_type DIE and add to
16586 the user defined type vector. */
16587
f792889a 16588static struct type *
e7c27a73 16589read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16590{
518817b3 16591 struct gdbarch *gdbarch
08feed99 16592 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
e7c27a73 16593 struct comp_unit_head *cu_header = &cu->header;
c906108c 16594 struct type *type;
8b2dbe47
KB
16595 struct attribute *attr_byte_size;
16596 struct attribute *attr_address_class;
16597 int byte_size, addr_class;
7e314c57
JK
16598 struct type *target_type;
16599
16600 target_type = die_type (die, cu);
c906108c 16601
7e314c57
JK
16602 /* The die_type call above may have already set the type for this DIE. */
16603 type = get_die_type (die, cu);
16604 if (type)
16605 return type;
16606
16607 type = lookup_pointer_type (target_type);
8b2dbe47 16608
e142c38c 16609 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16610 if (attr_byte_size)
16611 byte_size = DW_UNSND (attr_byte_size);
c906108c 16612 else
8b2dbe47
KB
16613 byte_size = cu_header->addr_size;
16614
e142c38c 16615 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16616 if (attr_address_class)
16617 addr_class = DW_UNSND (attr_address_class);
16618 else
16619 addr_class = DW_ADDR_none;
16620
2b4424c3
TT
16621 ULONGEST alignment = get_alignment (cu, die);
16622
16623 /* If the pointer size, alignment, or address class is different
16624 than the default, create a type variant marked as such and set
16625 the length accordingly. */
16626 if (TYPE_LENGTH (type) != byte_size
16627 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16628 && alignment != TYPE_RAW_ALIGN (type))
16629 || addr_class != DW_ADDR_none)
c906108c 16630 {
5e2b427d 16631 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16632 {
16633 int type_flags;
16634
849957d9 16635 type_flags = gdbarch_address_class_type_flags
5e2b427d 16636 (gdbarch, byte_size, addr_class);
876cecd0
TT
16637 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16638 == 0);
8b2dbe47
KB
16639 type = make_type_with_address_space (type, type_flags);
16640 }
16641 else if (TYPE_LENGTH (type) != byte_size)
16642 {
b98664d3 16643 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16644 }
2b4424c3
TT
16645 else if (TYPE_RAW_ALIGN (type) != alignment)
16646 {
b98664d3 16647 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16648 " - DIE at %s [in module %s]"),
16649 sect_offset_str (die->sect_off),
16650 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16651 }
6e70227d 16652 else
9a619af0
MS
16653 {
16654 /* Should we also complain about unhandled address classes? */
16655 }
c906108c 16656 }
8b2dbe47
KB
16657
16658 TYPE_LENGTH (type) = byte_size;
2b4424c3 16659 set_type_align (type, alignment);
f792889a 16660 return set_die_type (die, type, cu);
c906108c
SS
16661}
16662
16663/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16664 the user defined type vector. */
16665
f792889a 16666static struct type *
e7c27a73 16667read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16668{
16669 struct type *type;
16670 struct type *to_type;
16671 struct type *domain;
16672
e7c27a73
DJ
16673 to_type = die_type (die, cu);
16674 domain = die_containing_type (die, cu);
0d5de010 16675
7e314c57
JK
16676 /* The calls above may have already set the type for this DIE. */
16677 type = get_die_type (die, cu);
16678 if (type)
16679 return type;
16680
78134374 16681 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16682 type = lookup_methodptr_type (to_type);
78134374 16683 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16684 {
518817b3
SM
16685 struct type *new_type
16686 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16687
16688 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16689 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16690 TYPE_VARARGS (to_type));
16691 type = lookup_methodptr_type (new_type);
16692 }
0d5de010
DJ
16693 else
16694 type = lookup_memberptr_type (to_type, domain);
c906108c 16695
f792889a 16696 return set_die_type (die, type, cu);
c906108c
SS
16697}
16698
4297a3f0 16699/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16700 the user defined type vector. */
16701
f792889a 16702static struct type *
4297a3f0
AV
16703read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16704 enum type_code refcode)
c906108c 16705{
e7c27a73 16706 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16707 struct type *type, *target_type;
c906108c
SS
16708 struct attribute *attr;
16709
4297a3f0
AV
16710 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16711
7e314c57
JK
16712 target_type = die_type (die, cu);
16713
16714 /* The die_type call above may have already set the type for this DIE. */
16715 type = get_die_type (die, cu);
16716 if (type)
16717 return type;
16718
4297a3f0 16719 type = lookup_reference_type (target_type, refcode);
e142c38c 16720 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16721 if (attr != nullptr)
c906108c
SS
16722 {
16723 TYPE_LENGTH (type) = DW_UNSND (attr);
16724 }
16725 else
16726 {
107d2387 16727 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16728 }
2b4424c3 16729 maybe_set_alignment (cu, die, type);
f792889a 16730 return set_die_type (die, type, cu);
c906108c
SS
16731}
16732
cf363f18
MW
16733/* Add the given cv-qualifiers to the element type of the array. GCC
16734 outputs DWARF type qualifiers that apply to an array, not the
16735 element type. But GDB relies on the array element type to carry
16736 the cv-qualifiers. This mimics section 6.7.3 of the C99
16737 specification. */
16738
16739static struct type *
16740add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16741 struct type *base_type, int cnst, int voltl)
16742{
16743 struct type *el_type, *inner_array;
16744
16745 base_type = copy_type (base_type);
16746 inner_array = base_type;
16747
78134374 16748 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16749 {
16750 TYPE_TARGET_TYPE (inner_array) =
16751 copy_type (TYPE_TARGET_TYPE (inner_array));
16752 inner_array = TYPE_TARGET_TYPE (inner_array);
16753 }
16754
16755 el_type = TYPE_TARGET_TYPE (inner_array);
16756 cnst |= TYPE_CONST (el_type);
16757 voltl |= TYPE_VOLATILE (el_type);
16758 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16759
16760 return set_die_type (die, base_type, cu);
16761}
16762
f792889a 16763static struct type *
e7c27a73 16764read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16765{
f792889a 16766 struct type *base_type, *cv_type;
c906108c 16767
e7c27a73 16768 base_type = die_type (die, cu);
7e314c57
JK
16769
16770 /* The die_type call above may have already set the type for this DIE. */
16771 cv_type = get_die_type (die, cu);
16772 if (cv_type)
16773 return cv_type;
16774
2f608a3a
KW
16775 /* In case the const qualifier is applied to an array type, the element type
16776 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16777 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16778 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16779
f792889a
DJ
16780 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16781 return set_die_type (die, cv_type, cu);
c906108c
SS
16782}
16783
f792889a 16784static struct type *
e7c27a73 16785read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16786{
f792889a 16787 struct type *base_type, *cv_type;
c906108c 16788
e7c27a73 16789 base_type = die_type (die, cu);
7e314c57
JK
16790
16791 /* The die_type call above may have already set the type for this DIE. */
16792 cv_type = get_die_type (die, cu);
16793 if (cv_type)
16794 return cv_type;
16795
cf363f18
MW
16796 /* In case the volatile qualifier is applied to an array type, the
16797 element type is so qualified, not the array type (section 6.7.3
16798 of C99). */
78134374 16799 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16800 return add_array_cv_type (die, cu, base_type, 0, 1);
16801
f792889a
DJ
16802 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16803 return set_die_type (die, cv_type, cu);
c906108c
SS
16804}
16805
06d66ee9
TT
16806/* Handle DW_TAG_restrict_type. */
16807
16808static struct type *
16809read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16810{
16811 struct type *base_type, *cv_type;
16812
16813 base_type = die_type (die, cu);
16814
16815 /* The die_type call above may have already set the type for this DIE. */
16816 cv_type = get_die_type (die, cu);
16817 if (cv_type)
16818 return cv_type;
16819
16820 cv_type = make_restrict_type (base_type);
16821 return set_die_type (die, cv_type, cu);
16822}
16823
a2c2acaf
MW
16824/* Handle DW_TAG_atomic_type. */
16825
16826static struct type *
16827read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16828{
16829 struct type *base_type, *cv_type;
16830
16831 base_type = die_type (die, cu);
16832
16833 /* The die_type call above may have already set the type for this DIE. */
16834 cv_type = get_die_type (die, cu);
16835 if (cv_type)
16836 return cv_type;
16837
16838 cv_type = make_atomic_type (base_type);
16839 return set_die_type (die, cv_type, cu);
16840}
16841
c906108c
SS
16842/* Extract all information from a DW_TAG_string_type DIE and add to
16843 the user defined type vector. It isn't really a user defined type,
16844 but it behaves like one, with other DIE's using an AT_user_def_type
16845 attribute to reference it. */
16846
f792889a 16847static struct type *
e7c27a73 16848read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16849{
518817b3 16850 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 16851 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16852 struct type *type, *range_type, *index_type, *char_type;
16853 struct attribute *attr;
216a7e6b
AB
16854 struct dynamic_prop prop;
16855 bool length_is_constant = true;
16856 LONGEST length;
16857
16858 /* There are a couple of places where bit sizes might be made use of
16859 when parsing a DW_TAG_string_type, however, no producer that we know
16860 of make use of these. Handling bit sizes that are a multiple of the
16861 byte size is easy enough, but what about other bit sizes? Lets deal
16862 with that problem when we have to. Warn about these attributes being
16863 unsupported, then parse the type and ignore them like we always
16864 have. */
16865 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16866 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16867 {
16868 static bool warning_printed = false;
16869 if (!warning_printed)
16870 {
16871 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16872 "currently supported on DW_TAG_string_type."));
16873 warning_printed = true;
16874 }
16875 }
c906108c 16876
e142c38c 16877 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16878 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16879 {
16880 /* The string length describes the location at which the length of
16881 the string can be found. The size of the length field can be
16882 specified with one of the attributes below. */
16883 struct type *prop_type;
16884 struct attribute *len
16885 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16886 if (len == nullptr)
16887 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16888 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16889 {
16890 /* Pass 0 as the default as we know this attribute is constant
16891 and the default value will not be returned. */
0826b30a 16892 LONGEST sz = len->constant_value (0);
09ba997f 16893 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16894 }
16895 else
16896 {
16897 /* If the size is not specified then we assume it is the size of
16898 an address on this target. */
09ba997f 16899 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16900 }
16901
16902 /* Convert the attribute into a dynamic property. */
16903 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16904 length = 1;
16905 else
16906 length_is_constant = false;
16907 }
16908 else if (attr != nullptr)
16909 {
16910 /* This DW_AT_string_length just contains the length with no
16911 indirection. There's no need to create a dynamic property in this
16912 case. Pass 0 for the default value as we know it will not be
16913 returned in this case. */
0826b30a 16914 length = attr->constant_value (0);
216a7e6b
AB
16915 }
16916 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16917 {
216a7e6b 16918 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 16919 length = attr->constant_value (1);
c906108c
SS
16920 }
16921 else
16922 {
216a7e6b
AB
16923 /* Use 1 as a fallback length if we have nothing else. */
16924 length = 1;
c906108c 16925 }
6ccb9162 16926
46bf5051 16927 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16928 if (length_is_constant)
16929 range_type = create_static_range_type (NULL, index_type, 1, length);
16930 else
16931 {
16932 struct dynamic_prop low_bound;
16933
16934 low_bound.kind = PROP_CONST;
16935 low_bound.data.const_val = 1;
16936 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16937 }
3b7538c0
UW
16938 char_type = language_string_char_type (cu->language_defn, gdbarch);
16939 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16940
f792889a 16941 return set_die_type (die, type, cu);
c906108c
SS
16942}
16943
4d804846
JB
16944/* Assuming that DIE corresponds to a function, returns nonzero
16945 if the function is prototyped. */
16946
16947static int
16948prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16949{
16950 struct attribute *attr;
16951
16952 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16953 if (attr && (DW_UNSND (attr) != 0))
16954 return 1;
16955
16956 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16957 is only meaningful for C, but the concept also extends to other
4d804846
JB
16958 languages that allow unprototyped functions (Eg: Objective C).
16959 For all other languages, assume that functions are always
16960 prototyped. */
16961 if (cu->language != language_c
16962 && cu->language != language_objc
16963 && cu->language != language_opencl)
16964 return 1;
16965
16966 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16967 prototyped and unprototyped functions; default to prototyped,
16968 since that is more common in modern code (and RealView warns
16969 about unprototyped functions). */
16970 if (producer_is_realview (cu->producer))
16971 return 1;
16972
16973 return 0;
16974}
16975
c906108c
SS
16976/* Handle DIES due to C code like:
16977
16978 struct foo
c5aa993b
JM
16979 {
16980 int (*funcp)(int a, long l);
16981 int b;
16982 };
c906108c 16983
0963b4bd 16984 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16985
f792889a 16986static struct type *
e7c27a73 16987read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16988{
518817b3 16989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16990 struct type *type; /* Type that this function returns. */
16991 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16992 struct attribute *attr;
16993
e7c27a73 16994 type = die_type (die, cu);
7e314c57
JK
16995
16996 /* The die_type call above may have already set the type for this DIE. */
16997 ftype = get_die_type (die, cu);
16998 if (ftype)
16999 return ftype;
17000
0c8b41f1 17001 ftype = lookup_function_type (type);
c906108c 17002
4d804846 17003 if (prototyped_function_p (die, cu))
a6c727b2 17004 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17005
c055b101
CV
17006 /* Store the calling convention in the type if it's available in
17007 the subroutine die. Otherwise set the calling convention to
17008 the default value DW_CC_normal. */
17009 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17010 if (attr != nullptr
17011 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17012 TYPE_CALLING_CONVENTION (ftype)
17013 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17014 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17015 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17016 else
17017 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17018
743649fd
MW
17019 /* Record whether the function returns normally to its caller or not
17020 if the DWARF producer set that information. */
17021 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17022 if (attr && (DW_UNSND (attr) != 0))
17023 TYPE_NO_RETURN (ftype) = 1;
17024
76c10ea2
GM
17025 /* We need to add the subroutine type to the die immediately so
17026 we don't infinitely recurse when dealing with parameters
0963b4bd 17027 declared as the same subroutine type. */
76c10ea2 17028 set_die_type (die, ftype, cu);
6e70227d 17029
639d11d3 17030 if (die->child != NULL)
c906108c 17031 {
bb5ed363 17032 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17033 struct die_info *child_die;
8072405b 17034 int nparams, iparams;
c906108c
SS
17035
17036 /* Count the number of parameters.
17037 FIXME: GDB currently ignores vararg functions, but knows about
17038 vararg member functions. */
8072405b 17039 nparams = 0;
639d11d3 17040 child_die = die->child;
c906108c
SS
17041 while (child_die && child_die->tag)
17042 {
17043 if (child_die->tag == DW_TAG_formal_parameter)
17044 nparams++;
17045 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17046 TYPE_VARARGS (ftype) = 1;
436c571c 17047 child_die = child_die->sibling;
c906108c
SS
17048 }
17049
17050 /* Allocate storage for parameters and fill them in. */
17051 TYPE_NFIELDS (ftype) = nparams;
17052 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17053 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17054
8072405b
JK
17055 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17056 even if we error out during the parameters reading below. */
17057 for (iparams = 0; iparams < nparams; iparams++)
17058 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17059
17060 iparams = 0;
639d11d3 17061 child_die = die->child;
c906108c
SS
17062 while (child_die && child_die->tag)
17063 {
17064 if (child_die->tag == DW_TAG_formal_parameter)
17065 {
3ce3b1ba
PA
17066 struct type *arg_type;
17067
17068 /* DWARF version 2 has no clean way to discern C++
17069 static and non-static member functions. G++ helps
17070 GDB by marking the first parameter for non-static
17071 member functions (which is the this pointer) as
17072 artificial. We pass this information to
17073 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17074
17075 DWARF version 3 added DW_AT_object_pointer, which GCC
17076 4.5 does not yet generate. */
e142c38c 17077 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17078 if (attr != nullptr)
c906108c
SS
17079 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17080 else
9c37b5ae 17081 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17082 arg_type = die_type (child_die, cu);
17083
17084 /* RealView does not mark THIS as const, which the testsuite
17085 expects. GCC marks THIS as const in method definitions,
17086 but not in the class specifications (GCC PR 43053). */
17087 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17088 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17089 {
17090 int is_this = 0;
17091 struct dwarf2_cu *arg_cu = cu;
17092 const char *name = dwarf2_name (child_die, cu);
17093
17094 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17095 if (attr != nullptr)
3ce3b1ba
PA
17096 {
17097 /* If the compiler emits this, use it. */
17098 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17099 is_this = 1;
17100 }
17101 else if (name && strcmp (name, "this") == 0)
17102 /* Function definitions will have the argument names. */
17103 is_this = 1;
17104 else if (name == NULL && iparams == 0)
17105 /* Declarations may not have the names, so like
17106 elsewhere in GDB, assume an artificial first
17107 argument is "this". */
17108 is_this = 1;
17109
17110 if (is_this)
17111 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17112 arg_type, 0);
17113 }
17114
17115 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17116 iparams++;
17117 }
436c571c 17118 child_die = child_die->sibling;
c906108c
SS
17119 }
17120 }
17121
76c10ea2 17122 return ftype;
c906108c
SS
17123}
17124
f792889a 17125static struct type *
e7c27a73 17126read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17127{
518817b3 17128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17129 const char *name = NULL;
3c8e0968 17130 struct type *this_type, *target_type;
c906108c 17131
94af9270 17132 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17133 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17134 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17135 set_die_type (die, this_type, cu);
3c8e0968
DE
17136 target_type = die_type (die, cu);
17137 if (target_type != this_type)
17138 TYPE_TARGET_TYPE (this_type) = target_type;
17139 else
17140 {
17141 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17142 spec and cause infinite loops in GDB. */
b98664d3 17143 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17144 "- DIE at %s [in module %s]"),
17145 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17146 TYPE_TARGET_TYPE (this_type) = NULL;
17147 }
e4003a34
TV
17148 if (name == NULL)
17149 {
17150 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17151 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17152 Handle these by just returning the target type, rather than
17153 constructing an anonymous typedef type and trying to handle this
17154 elsewhere. */
17155 set_die_type (die, target_type, cu);
17156 return target_type;
17157 }
f792889a 17158 return this_type;
c906108c
SS
17159}
17160
9b790ce7
UW
17161/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17162 (which may be different from NAME) to the architecture back-end to allow
17163 it to guess the correct format if necessary. */
17164
17165static struct type *
17166dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17167 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17168{
08feed99 17169 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17170 const struct floatformat **format;
17171 struct type *type;
17172
17173 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17174 if (format)
103a685e 17175 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17176 else
77b7c781 17177 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17178
17179 return type;
17180}
17181
eb77c9df
AB
17182/* Allocate an integer type of size BITS and name NAME. */
17183
17184static struct type *
17185dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17186 int bits, int unsigned_p, const char *name)
17187{
17188 struct type *type;
17189
17190 /* Versions of Intel's C Compiler generate an integer type called "void"
17191 instead of using DW_TAG_unspecified_type. This has been seen on
17192 at least versions 14, 17, and 18. */
35ee2dc2
AB
17193 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17194 && strcmp (name, "void") == 0)
eb77c9df
AB
17195 type = objfile_type (objfile)->builtin_void;
17196 else
17197 type = init_integer_type (objfile, bits, unsigned_p, name);
17198
17199 return type;
17200}
17201
8bdc1658
AB
17202/* Initialise and return a floating point type of size BITS suitable for
17203 use as a component of a complex number. The NAME_HINT is passed through
17204 when initialising the floating point type and is the name of the complex
17205 type.
17206
17207 As DWARF doesn't currently provide an explicit name for the components
17208 of a complex number, but it can be helpful to have these components
17209 named, we try to select a suitable name based on the size of the
17210 component. */
17211static struct type *
17212dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17213 struct objfile *objfile,
103a685e
TT
17214 int bits, const char *name_hint,
17215 enum bfd_endian byte_order)
8bdc1658 17216{
08feed99 17217 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17218 struct type *tt = nullptr;
17219
35add35e
AB
17220 /* Try to find a suitable floating point builtin type of size BITS.
17221 We're going to use the name of this type as the name for the complex
17222 target type that we are about to create. */
1db455a7 17223 switch (cu->language)
8bdc1658 17224 {
1db455a7
AB
17225 case language_fortran:
17226 switch (bits)
17227 {
17228 case 32:
17229 tt = builtin_f_type (gdbarch)->builtin_real;
17230 break;
17231 case 64:
17232 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17233 break;
17234 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17235 case 128:
17236 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17237 break;
17238 }
8bdc1658 17239 break;
1db455a7
AB
17240 default:
17241 switch (bits)
17242 {
17243 case 32:
17244 tt = builtin_type (gdbarch)->builtin_float;
17245 break;
17246 case 64:
17247 tt = builtin_type (gdbarch)->builtin_double;
17248 break;
17249 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17250 case 128:
17251 tt = builtin_type (gdbarch)->builtin_long_double;
17252 break;
17253 }
8bdc1658
AB
17254 break;
17255 }
17256
35add35e
AB
17257 /* If the type we found doesn't match the size we were looking for, then
17258 pretend we didn't find a type at all, the complex target type we
17259 create will then be nameless. */
a12e5744 17260 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17261 tt = nullptr;
17262
7d93a1e0 17263 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17264 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17265}
17266
c906108c
SS
17267/* Find a representation of a given base type and install
17268 it in the TYPE field of the die. */
17269
f792889a 17270static struct type *
e7c27a73 17271read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17272{
518817b3 17273 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17274 struct type *type;
17275 struct attribute *attr;
19f392bc 17276 int encoding = 0, bits = 0;
15d034d0 17277 const char *name;
34877895 17278 gdbarch *arch;
c906108c 17279
e142c38c 17280 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17281 if (attr != nullptr)
34877895 17282 encoding = DW_UNSND (attr);
e142c38c 17283 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17284 if (attr != nullptr)
34877895 17285 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17286 name = dwarf2_name (die, cu);
6ccb9162 17287 if (!name)
34877895 17288 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17289
08feed99 17290 arch = objfile->arch ();
103a685e
TT
17291 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17292
34877895
PJ
17293 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17294 if (attr)
103a685e
TT
17295 {
17296 int endianity = DW_UNSND (attr);
17297
17298 switch (endianity)
17299 {
17300 case DW_END_big:
17301 byte_order = BFD_ENDIAN_BIG;
17302 break;
17303 case DW_END_little:
17304 byte_order = BFD_ENDIAN_LITTLE;
17305 break;
17306 default:
17307 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17308 break;
17309 }
17310 }
6ccb9162
UW
17311
17312 switch (encoding)
c906108c 17313 {
6ccb9162
UW
17314 case DW_ATE_address:
17315 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17316 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17317 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17318 break;
17319 case DW_ATE_boolean:
19f392bc 17320 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17321 break;
17322 case DW_ATE_complex_float:
103a685e
TT
17323 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17324 byte_order);
78134374 17325 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17326 {
17327 if (name == nullptr)
17328 {
17329 struct obstack *obstack
17330 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
7d93a1e0 17331 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17332 nullptr);
17333 }
17334 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17335 }
17336 else
17337 type = init_complex_type (name, type);
6ccb9162
UW
17338 break;
17339 case DW_ATE_decimal_float:
19f392bc 17340 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17341 break;
17342 case DW_ATE_float:
103a685e 17343 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17344 break;
17345 case DW_ATE_signed:
eb77c9df 17346 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17347 break;
17348 case DW_ATE_unsigned:
3b2b8fea
TT
17349 if (cu->language == language_fortran
17350 && name
61012eef 17351 && startswith (name, "character("))
19f392bc
UW
17352 type = init_character_type (objfile, bits, 1, name);
17353 else
eb77c9df 17354 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17355 break;
17356 case DW_ATE_signed_char:
6e70227d 17357 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17358 || cu->language == language_pascal
17359 || cu->language == language_fortran)
19f392bc
UW
17360 type = init_character_type (objfile, bits, 0, name);
17361 else
eb77c9df 17362 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17363 break;
17364 case DW_ATE_unsigned_char:
868a0084 17365 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17366 || cu->language == language_pascal
c44af4eb
TT
17367 || cu->language == language_fortran
17368 || cu->language == language_rust)
19f392bc
UW
17369 type = init_character_type (objfile, bits, 1, name);
17370 else
eb77c9df 17371 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17372 break;
75079b2b 17373 case DW_ATE_UTF:
53e710ac 17374 {
53e710ac
PA
17375 if (bits == 16)
17376 type = builtin_type (arch)->builtin_char16;
17377 else if (bits == 32)
17378 type = builtin_type (arch)->builtin_char32;
17379 else
17380 {
b98664d3 17381 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17382 bits);
eb77c9df 17383 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17384 }
17385 return set_die_type (die, type, cu);
17386 }
75079b2b
TT
17387 break;
17388
6ccb9162 17389 default:
b98664d3 17390 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17391 dwarf_type_encoding_name (encoding));
77b7c781 17392 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17393 break;
c906108c 17394 }
6ccb9162 17395
0114d602 17396 if (name && strcmp (name, "char") == 0)
876cecd0 17397 TYPE_NOSIGN (type) = 1;
0114d602 17398
2b4424c3
TT
17399 maybe_set_alignment (cu, die, type);
17400
103a685e 17401 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17402
f792889a 17403 return set_die_type (die, type, cu);
c906108c
SS
17404}
17405
80180f79
SA
17406/* Parse dwarf attribute if it's a block, reference or constant and put the
17407 resulting value of the attribute into struct bound_prop.
17408 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17409
17410static int
17411attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17412 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17413 struct type *default_type)
80180f79
SA
17414{
17415 struct dwarf2_property_baton *baton;
518817b3
SM
17416 struct obstack *obstack
17417 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17418
9a49df9d
AB
17419 gdb_assert (default_type != NULL);
17420
80180f79
SA
17421 if (attr == NULL || prop == NULL)
17422 return 0;
17423
4fc6c0d5 17424 if (attr->form_is_block ())
80180f79 17425 {
8d749320 17426 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17427 baton->property_type = default_type;
80180f79
SA
17428 baton->locexpr.per_cu = cu->per_cu;
17429 baton->locexpr.size = DW_BLOCK (attr)->size;
17430 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17431 switch (attr->name)
17432 {
17433 case DW_AT_string_length:
17434 baton->locexpr.is_reference = true;
17435 break;
17436 default:
17437 baton->locexpr.is_reference = false;
17438 break;
17439 }
80180f79
SA
17440 prop->data.baton = baton;
17441 prop->kind = PROP_LOCEXPR;
17442 gdb_assert (prop->data.baton != NULL);
17443 }
cd6c91b4 17444 else if (attr->form_is_ref ())
80180f79
SA
17445 {
17446 struct dwarf2_cu *target_cu = cu;
17447 struct die_info *target_die;
17448 struct attribute *target_attr;
17449
17450 target_die = follow_die_ref (die, attr, &target_cu);
17451 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17452 if (target_attr == NULL)
17453 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17454 target_cu);
80180f79
SA
17455 if (target_attr == NULL)
17456 return 0;
17457
df25ebbd 17458 switch (target_attr->name)
80180f79 17459 {
df25ebbd 17460 case DW_AT_location:
cd6c91b4 17461 if (target_attr->form_is_section_offset ())
df25ebbd 17462 {
8d749320 17463 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17464 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17465 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17466 prop->data.baton = baton;
17467 prop->kind = PROP_LOCLIST;
17468 gdb_assert (prop->data.baton != NULL);
17469 }
4fc6c0d5 17470 else if (target_attr->form_is_block ())
df25ebbd 17471 {
8d749320 17472 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17473 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17474 baton->locexpr.per_cu = cu->per_cu;
17475 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17476 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17477 baton->locexpr.is_reference = true;
df25ebbd
JB
17478 prop->data.baton = baton;
17479 prop->kind = PROP_LOCEXPR;
17480 gdb_assert (prop->data.baton != NULL);
17481 }
17482 else
17483 {
17484 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17485 "dynamic property");
17486 return 0;
17487 }
17488 break;
17489 case DW_AT_data_member_location:
17490 {
17491 LONGEST offset;
17492
17493 if (!handle_data_member_location (target_die, target_cu,
17494 &offset))
17495 return 0;
17496
8d749320 17497 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17498 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17499 target_cu);
df25ebbd
JB
17500 baton->offset_info.offset = offset;
17501 baton->offset_info.type = die_type (target_die, target_cu);
17502 prop->data.baton = baton;
17503 prop->kind = PROP_ADDR_OFFSET;
17504 break;
17505 }
80180f79
SA
17506 }
17507 }
cd6c91b4 17508 else if (attr->form_is_constant ())
80180f79 17509 {
0826b30a 17510 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17511 prop->kind = PROP_CONST;
17512 }
17513 else
17514 {
17515 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17516 dwarf2_name (die, cu));
17517 return 0;
17518 }
17519
17520 return 1;
17521}
17522
09ba997f 17523/* See read.h. */
9a49df9d 17524
09ba997f
TT
17525struct type *
17526dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17527{
09ba997f 17528 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17529 struct type *int_type;
17530
17531 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17532#define TRY_TYPE(F) \
17533 int_type = (unsigned_p \
17534 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17535 : objfile_type (objfile)->builtin_ ## F); \
17536 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17537 return int_type
17538
17539 TRY_TYPE (char);
17540 TRY_TYPE (short);
17541 TRY_TYPE (int);
17542 TRY_TYPE (long);
17543 TRY_TYPE (long_long);
17544
17545#undef TRY_TYPE
17546
17547 gdb_assert_not_reached ("unable to find suitable integer type");
17548}
17549
09ba997f 17550/* See read.h. */
11a8b164 17551
09ba997f
TT
17552struct type *
17553dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17554{
09ba997f
TT
17555 int addr_size = this->addr_size ();
17556 return int_type (addr_size, unsigned_p);
11a8b164
AB
17557}
17558
b86352cf
AB
17559/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17560 present (which is valid) then compute the default type based on the
17561 compilation units address size. */
17562
17563static struct type *
17564read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17565{
17566 struct type *index_type = die_type (die, cu);
17567
17568 /* Dwarf-2 specifications explicitly allows to create subrange types
17569 without specifying a base type.
17570 In that case, the base type must be set to the type of
17571 the lower bound, upper bound or count, in that order, if any of these
17572 three attributes references an object that has a type.
17573 If no base type is found, the Dwarf-2 specifications say that
17574 a signed integer type of size equal to the size of an address should
17575 be used.
17576 For the following C code: `extern char gdb_int [];'
17577 GCC produces an empty range DIE.
17578 FIXME: muller/2010-05-28: Possible references to object for low bound,
17579 high bound or count are not yet handled by this code. */
78134374 17580 if (index_type->code () == TYPE_CODE_VOID)
09ba997f 17581 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17582
17583 return index_type;
17584}
17585
a02abb62
JB
17586/* Read the given DW_AT_subrange DIE. */
17587
f792889a 17588static struct type *
a02abb62
JB
17589read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17590{
4c9ad8c2 17591 struct type *base_type, *orig_base_type;
a02abb62
JB
17592 struct type *range_type;
17593 struct attribute *attr;
729efb13 17594 struct dynamic_prop low, high;
4fae6e18 17595 int low_default_is_valid;
c451ebe5 17596 int high_bound_is_count = 0;
15d034d0 17597 const char *name;
d359392f 17598 ULONGEST negative_mask;
e77813c8 17599
b86352cf
AB
17600 orig_base_type = read_subrange_index_type (die, cu);
17601
4c9ad8c2
TT
17602 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17603 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17604 creating the range type, but we use the result of check_typedef
17605 when examining properties of the type. */
17606 base_type = check_typedef (orig_base_type);
a02abb62 17607
7e314c57
JK
17608 /* The die_type call above may have already set the type for this DIE. */
17609 range_type = get_die_type (die, cu);
17610 if (range_type)
17611 return range_type;
17612
729efb13
SA
17613 low.kind = PROP_CONST;
17614 high.kind = PROP_CONST;
17615 high.data.const_val = 0;
17616
4fae6e18
JK
17617 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17618 omitting DW_AT_lower_bound. */
17619 switch (cu->language)
6e70227d 17620 {
4fae6e18
JK
17621 case language_c:
17622 case language_cplus:
729efb13 17623 low.data.const_val = 0;
4fae6e18
JK
17624 low_default_is_valid = 1;
17625 break;
17626 case language_fortran:
729efb13 17627 low.data.const_val = 1;
4fae6e18
JK
17628 low_default_is_valid = 1;
17629 break;
17630 case language_d:
4fae6e18 17631 case language_objc:
c44af4eb 17632 case language_rust:
729efb13 17633 low.data.const_val = 0;
4fae6e18
JK
17634 low_default_is_valid = (cu->header.version >= 4);
17635 break;
17636 case language_ada:
17637 case language_m2:
17638 case language_pascal:
729efb13 17639 low.data.const_val = 1;
4fae6e18
JK
17640 low_default_is_valid = (cu->header.version >= 4);
17641 break;
17642 default:
729efb13 17643 low.data.const_val = 0;
4fae6e18
JK
17644 low_default_is_valid = 0;
17645 break;
a02abb62
JB
17646 }
17647
e142c38c 17648 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17649 if (attr != nullptr)
9a49df9d 17650 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17651 else if (!low_default_is_valid)
b98664d3 17652 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17653 "- DIE at %s [in module %s]"),
17654 sect_offset_str (die->sect_off),
518817b3 17655 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17656
506f5c41
TV
17657 struct attribute *attr_ub, *attr_count;
17658 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17659 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17660 {
506f5c41 17661 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17662 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17663 {
c451ebe5
SA
17664 /* If bounds are constant do the final calculation here. */
17665 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17666 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17667 else
17668 high_bound_is_count = 1;
c2ff108b 17669 }
506f5c41
TV
17670 else
17671 {
17672 if (attr_ub != NULL)
17673 complaint (_("Unresolved DW_AT_upper_bound "
17674 "- DIE at %s [in module %s]"),
17675 sect_offset_str (die->sect_off),
17676 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17677 if (attr_count != NULL)
17678 complaint (_("Unresolved DW_AT_count "
17679 "- DIE at %s [in module %s]"),
17680 sect_offset_str (die->sect_off),
17681 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17682 }
e77813c8 17683 }
a02abb62 17684
4e962e74
TT
17685 LONGEST bias = 0;
17686 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17687 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17688 bias = bias_attr->constant_value (0);
4e962e74 17689
dbb9c2b1
JB
17690 /* Normally, the DWARF producers are expected to use a signed
17691 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17692 But this is unfortunately not always the case, as witnessed
17693 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17694 is used instead. To work around that ambiguity, we treat
17695 the bounds as signed, and thus sign-extend their values, when
17696 the base type is signed. */
6e70227d 17697 negative_mask =
d359392f 17698 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17699 if (low.kind == PROP_CONST
17700 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17701 low.data.const_val |= negative_mask;
17702 if (high.kind == PROP_CONST
17703 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17704 high.data.const_val |= negative_mask;
43bbcdc2 17705
5bbd8269
AB
17706 /* Check for bit and byte strides. */
17707 struct dynamic_prop byte_stride_prop;
17708 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17709 if (attr_byte_stride != nullptr)
17710 {
09ba997f 17711 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17712 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17713 prop_type);
17714 }
17715
17716 struct dynamic_prop bit_stride_prop;
17717 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17718 if (attr_bit_stride != nullptr)
17719 {
17720 /* It only makes sense to have either a bit or byte stride. */
17721 if (attr_byte_stride != nullptr)
17722 {
17723 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17724 "- DIE at %s [in module %s]"),
17725 sect_offset_str (die->sect_off),
17726 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17727 attr_bit_stride = nullptr;
17728 }
17729 else
17730 {
09ba997f 17731 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17732 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17733 prop_type);
17734 }
17735 }
17736
17737 if (attr_byte_stride != nullptr
17738 || attr_bit_stride != nullptr)
17739 {
17740 bool byte_stride_p = (attr_byte_stride != nullptr);
17741 struct dynamic_prop *stride
17742 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17743
17744 range_type
17745 = create_range_type_with_stride (NULL, orig_base_type, &low,
17746 &high, bias, stride, byte_stride_p);
17747 }
17748 else
17749 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17750
c451ebe5
SA
17751 if (high_bound_is_count)
17752 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17753
c2ff108b
JK
17754 /* Ada expects an empty array on no boundary attributes. */
17755 if (attr == NULL && cu->language != language_ada)
729efb13 17756 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17757
39cbfefa
DJ
17758 name = dwarf2_name (die, cu);
17759 if (name)
d0e39ea2 17760 range_type->set_name (name);
6e70227d 17761
e142c38c 17762 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17763 if (attr != nullptr)
a02abb62
JB
17764 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17765
2b4424c3
TT
17766 maybe_set_alignment (cu, die, range_type);
17767
7e314c57
JK
17768 set_die_type (die, range_type, cu);
17769
17770 /* set_die_type should be already done. */
b4ba55a1
JB
17771 set_descriptive_type (range_type, die, cu);
17772
7e314c57 17773 return range_type;
a02abb62 17774}
6e70227d 17775
f792889a 17776static struct type *
81a17f79
JB
17777read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17778{
17779 struct type *type;
81a17f79 17780
518817b3
SM
17781 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17782 NULL);
d0e39ea2 17783 type->set_name (dwarf2_name (die, cu));
81a17f79 17784
74a2f8ff 17785 /* In Ada, an unspecified type is typically used when the description
85102364 17786 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17787 such a type, we treat it as a stub, and try to resolve it later on,
17788 when needed. */
17789 if (cu->language == language_ada)
17790 TYPE_STUB (type) = 1;
17791
f792889a 17792 return set_die_type (die, type, cu);
81a17f79 17793}
a02abb62 17794
639d11d3
DC
17795/* Read a single die and all its descendents. Set the die's sibling
17796 field to NULL; set other fields in the die correctly, and set all
17797 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17798 location of the info_ptr after reading all of those dies. PARENT
17799 is the parent of the die in question. */
17800
17801static struct die_info *
dee91e82 17802read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17803 const gdb_byte *info_ptr,
17804 const gdb_byte **new_info_ptr,
dee91e82 17805 struct die_info *parent)
639d11d3
DC
17806{
17807 struct die_info *die;
d521ce57 17808 const gdb_byte *cur_ptr;
639d11d3 17809
3e225074 17810 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17811 if (die == NULL)
17812 {
17813 *new_info_ptr = cur_ptr;
17814 return NULL;
17815 }
93311388 17816 store_in_ref_table (die, reader->cu);
639d11d3 17817
3e225074 17818 if (die->has_children)
bf6af496 17819 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17820 else
17821 {
17822 die->child = NULL;
17823 *new_info_ptr = cur_ptr;
17824 }
17825
17826 die->sibling = NULL;
17827 die->parent = parent;
17828 return die;
17829}
17830
17831/* Read a die, all of its descendents, and all of its siblings; set
17832 all of the fields of all of the dies correctly. Arguments are as
17833 in read_die_and_children. */
17834
17835static struct die_info *
bf6af496 17836read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17837 const gdb_byte *info_ptr,
17838 const gdb_byte **new_info_ptr,
bf6af496 17839 struct die_info *parent)
639d11d3
DC
17840{
17841 struct die_info *first_die, *last_sibling;
d521ce57 17842 const gdb_byte *cur_ptr;
639d11d3 17843
c906108c 17844 cur_ptr = info_ptr;
639d11d3
DC
17845 first_die = last_sibling = NULL;
17846
17847 while (1)
c906108c 17848 {
639d11d3 17849 struct die_info *die
dee91e82 17850 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17851
1d325ec1 17852 if (die == NULL)
c906108c 17853 {
639d11d3
DC
17854 *new_info_ptr = cur_ptr;
17855 return first_die;
c906108c 17856 }
1d325ec1
DJ
17857
17858 if (!first_die)
17859 first_die = die;
c906108c 17860 else
1d325ec1
DJ
17861 last_sibling->sibling = die;
17862
17863 last_sibling = die;
c906108c 17864 }
c906108c
SS
17865}
17866
bf6af496
DE
17867/* Read a die, all of its descendents, and all of its siblings; set
17868 all of the fields of all of the dies correctly. Arguments are as
17869 in read_die_and_children.
17870 This the main entry point for reading a DIE and all its children. */
17871
17872static struct die_info *
17873read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17874 const gdb_byte *info_ptr,
17875 const gdb_byte **new_info_ptr,
bf6af496
DE
17876 struct die_info *parent)
17877{
17878 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17879 new_info_ptr, parent);
17880
b4f54984 17881 if (dwarf_die_debug)
bf6af496
DE
17882 {
17883 fprintf_unfiltered (gdb_stdlog,
17884 "Read die from %s@0x%x of %s:\n",
96b79293 17885 reader->die_section->get_name (),
bf6af496
DE
17886 (unsigned) (info_ptr - reader->die_section->buffer),
17887 bfd_get_filename (reader->abfd));
b4f54984 17888 dump_die (die, dwarf_die_debug);
bf6af496
DE
17889 }
17890
17891 return die;
17892}
17893
3019eac3
DE
17894/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17895 attributes.
17896 The caller is responsible for filling in the extra attributes
17897 and updating (*DIEP)->num_attrs.
17898 Set DIEP to point to a newly allocated die with its information,
3e225074 17899 except for its child, sibling, and parent fields. */
93311388 17900
d521ce57 17901static const gdb_byte *
3019eac3 17902read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17903 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17904 int num_extra_attrs)
93311388 17905{
b64f50a1 17906 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17907 struct abbrev_info *abbrev;
17908 struct die_info *die;
17909 struct dwarf2_cu *cu = reader->cu;
17910 bfd *abfd = reader->abfd;
17911
9c541725 17912 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17913 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17914 info_ptr += bytes_read;
17915 if (!abbrev_number)
17916 {
17917 *diep = NULL;
93311388
DE
17918 return info_ptr;
17919 }
17920
685af9cd 17921 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17922 if (!abbrev)
348e048f
DE
17923 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17924 abbrev_number,
17925 bfd_get_filename (abfd));
17926
3019eac3 17927 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17928 die->sect_off = sect_off;
93311388
DE
17929 die->tag = abbrev->tag;
17930 die->abbrev = abbrev_number;
3e225074 17931 die->has_children = abbrev->has_children;
93311388 17932
3019eac3
DE
17933 /* Make the result usable.
17934 The caller needs to update num_attrs after adding the extra
17935 attributes. */
93311388
DE
17936 die->num_attrs = abbrev->num_attrs;
17937
18a8505e 17938 std::vector<int> indexes_that_need_reprocess;
93311388 17939 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17940 {
17941 bool need_reprocess;
17942 info_ptr =
17943 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17944 info_ptr, &need_reprocess);
17945 if (need_reprocess)
17946 indexes_that_need_reprocess.push_back (i);
17947 }
17948
052c8bb8 17949 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
17950 if (attr != nullptr)
17951 cu->str_offsets_base = DW_UNSND (attr);
93311388 17952
41144253 17953 attr = die->attr (DW_AT_loclists_base);
17954 if (attr != nullptr)
17955 cu->loclist_base = DW_UNSND (attr);
17956
a39fdb41 17957 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
17958 if (maybe_addr_base.has_value ())
17959 cu->addr_base = *maybe_addr_base;
17960 for (int index : indexes_that_need_reprocess)
17961 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17962 *diep = die;
93311388
DE
17963 return info_ptr;
17964}
17965
3019eac3
DE
17966/* Read a die and all its attributes.
17967 Set DIEP to point to a newly allocated die with its information,
3e225074 17968 except for its child, sibling, and parent fields. */
3019eac3 17969
d521ce57 17970static const gdb_byte *
3019eac3 17971read_full_die (const struct die_reader_specs *reader,
3e225074 17972 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17973{
d521ce57 17974 const gdb_byte *result;
bf6af496 17975
3e225074 17976 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17977
b4f54984 17978 if (dwarf_die_debug)
bf6af496
DE
17979 {
17980 fprintf_unfiltered (gdb_stdlog,
17981 "Read die from %s@0x%x of %s:\n",
96b79293 17982 reader->die_section->get_name (),
bf6af496
DE
17983 (unsigned) (info_ptr - reader->die_section->buffer),
17984 bfd_get_filename (reader->abfd));
b4f54984 17985 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17986 }
17987
17988 return result;
3019eac3 17989}
433df2d4 17990\f
c906108c 17991
72bf9492
DJ
17992/* Returns nonzero if TAG represents a type that we might generate a partial
17993 symbol for. */
17994
17995static int
17996is_type_tag_for_partial (int tag)
17997{
17998 switch (tag)
17999 {
18000#if 0
18001 /* Some types that would be reasonable to generate partial symbols for,
18002 that we don't at present. */
18003 case DW_TAG_array_type:
18004 case DW_TAG_file_type:
18005 case DW_TAG_ptr_to_member_type:
18006 case DW_TAG_set_type:
18007 case DW_TAG_string_type:
18008 case DW_TAG_subroutine_type:
18009#endif
18010 case DW_TAG_base_type:
18011 case DW_TAG_class_type:
680b30c7 18012 case DW_TAG_interface_type:
72bf9492
DJ
18013 case DW_TAG_enumeration_type:
18014 case DW_TAG_structure_type:
18015 case DW_TAG_subrange_type:
18016 case DW_TAG_typedef:
18017 case DW_TAG_union_type:
18018 return 1;
18019 default:
18020 return 0;
18021 }
18022}
18023
18024/* Load all DIEs that are interesting for partial symbols into memory. */
18025
18026static struct partial_die_info *
dee91e82 18027load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18028 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18029{
dee91e82 18030 struct dwarf2_cu *cu = reader->cu;
518817b3 18031 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18032 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18033 unsigned int bytes_read;
5afb4e99 18034 unsigned int load_all = 0;
72bf9492
DJ
18035 int nesting_level = 1;
18036
18037 parent_die = NULL;
18038 last_die = NULL;
18039
7adf1e79
DE
18040 gdb_assert (cu->per_cu != NULL);
18041 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18042 load_all = 1;
18043
72bf9492
DJ
18044 cu->partial_dies
18045 = htab_create_alloc_ex (cu->header.length / 12,
18046 partial_die_hash,
18047 partial_die_eq,
18048 NULL,
18049 &cu->comp_unit_obstack,
18050 hashtab_obstack_allocate,
18051 dummy_obstack_deallocate);
18052
72bf9492
DJ
18053 while (1)
18054 {
685af9cd 18055 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18056
18057 /* A NULL abbrev means the end of a series of children. */
18058 if (abbrev == NULL)
18059 {
18060 if (--nesting_level == 0)
cd9983dd
YQ
18061 return first_die;
18062
72bf9492
DJ
18063 info_ptr += bytes_read;
18064 last_die = parent_die;
18065 parent_die = parent_die->die_parent;
18066 continue;
18067 }
18068
98bfdba5
PA
18069 /* Check for template arguments. We never save these; if
18070 they're seen, we just mark the parent, and go on our way. */
18071 if (parent_die != NULL
18072 && cu->language == language_cplus
18073 && (abbrev->tag == DW_TAG_template_type_param
18074 || abbrev->tag == DW_TAG_template_value_param))
18075 {
18076 parent_die->has_template_arguments = 1;
18077
18078 if (!load_all)
18079 {
18080 /* We don't need a partial DIE for the template argument. */
dee91e82 18081 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18082 continue;
18083 }
18084 }
18085
0d99eb77 18086 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18087 Skip their other children. */
18088 if (!load_all
18089 && cu->language == language_cplus
18090 && parent_die != NULL
18091 && parent_die->tag == DW_TAG_subprogram)
18092 {
dee91e82 18093 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18094 continue;
18095 }
18096
5afb4e99
DJ
18097 /* Check whether this DIE is interesting enough to save. Normally
18098 we would not be interested in members here, but there may be
18099 later variables referencing them via DW_AT_specification (for
18100 static members). */
18101 if (!load_all
18102 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18103 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18104 && abbrev->tag != DW_TAG_enumerator
18105 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18106 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18107 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18108 && abbrev->tag != DW_TAG_variable
5afb4e99 18109 && abbrev->tag != DW_TAG_namespace
f55ee35c 18110 && abbrev->tag != DW_TAG_module
95554aad 18111 && abbrev->tag != DW_TAG_member
74921315
KS
18112 && abbrev->tag != DW_TAG_imported_unit
18113 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18114 {
18115 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18116 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18117 continue;
18118 }
18119
6f06d47b
YQ
18120 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18121 abbrev);
cd9983dd 18122
48fbe735 18123 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18124
18125 /* This two-pass algorithm for processing partial symbols has a
18126 high cost in cache pressure. Thus, handle some simple cases
18127 here which cover the majority of C partial symbols. DIEs
18128 which neither have specification tags in them, nor could have
18129 specification tags elsewhere pointing at them, can simply be
18130 processed and discarded.
18131
18132 This segment is also optional; scan_partial_symbols and
18133 add_partial_symbol will handle these DIEs if we chain
18134 them in normally. When compilers which do not emit large
18135 quantities of duplicate debug information are more common,
18136 this code can probably be removed. */
18137
18138 /* Any complete simple types at the top level (pretty much all
18139 of them, for a language without namespaces), can be processed
18140 directly. */
18141 if (parent_die == NULL
cd9983dd
YQ
18142 && pdi.has_specification == 0
18143 && pdi.is_declaration == 0
18144 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18145 || pdi.tag == DW_TAG_base_type
18146 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18147 {
cd9983dd 18148 if (building_psymtab && pdi.name != NULL)
31edb802 18149 add_psymbol_to_list (pdi.name, false,
79748972 18150 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18151 psymbol_placement::STATIC,
1762568f 18152 0, cu->language, objfile);
cd9983dd 18153 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18154 continue;
18155 }
18156
d8228535
JK
18157 /* The exception for DW_TAG_typedef with has_children above is
18158 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18159 type_name_or_error will error on such types later.
d8228535
JK
18160
18161 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18162 it could not find the child DIEs referenced later, this is checked
18163 above. In correct DWARF DW_TAG_typedef should have no children. */
18164
cd9983dd 18165 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18166 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18167 "- DIE at %s [in module %s]"),
cd9983dd 18168 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18169
72bf9492
DJ
18170 /* If we're at the second level, and we're an enumerator, and
18171 our parent has no specification (meaning possibly lives in a
18172 namespace elsewhere), then we can add the partial symbol now
18173 instead of queueing it. */
cd9983dd 18174 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18175 && parent_die != NULL
18176 && parent_die->die_parent == NULL
18177 && parent_die->tag == DW_TAG_enumeration_type
18178 && parent_die->has_specification == 0)
18179 {
cd9983dd 18180 if (pdi.name == NULL)
b98664d3 18181 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18182 else if (building_psymtab)
31edb802 18183 add_psymbol_to_list (pdi.name, false,
79748972 18184 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18185 cu->language == language_cplus
75aedd27
TT
18186 ? psymbol_placement::GLOBAL
18187 : psymbol_placement::STATIC,
1762568f 18188 0, cu->language, objfile);
72bf9492 18189
cd9983dd 18190 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18191 continue;
18192 }
18193
cd9983dd 18194 struct partial_die_info *part_die
6f06d47b 18195 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18196
72bf9492
DJ
18197 /* We'll save this DIE so link it in. */
18198 part_die->die_parent = parent_die;
18199 part_die->die_sibling = NULL;
18200 part_die->die_child = NULL;
18201
18202 if (last_die && last_die == parent_die)
18203 last_die->die_child = part_die;
18204 else if (last_die)
18205 last_die->die_sibling = part_die;
18206
18207 last_die = part_die;
18208
18209 if (first_die == NULL)
18210 first_die = part_die;
18211
18212 /* Maybe add the DIE to the hash table. Not all DIEs that we
18213 find interesting need to be in the hash table, because we
18214 also have the parent/sibling/child chains; only those that we
18215 might refer to by offset later during partial symbol reading.
18216
18217 For now this means things that might have be the target of a
18218 DW_AT_specification, DW_AT_abstract_origin, or
18219 DW_AT_extension. DW_AT_extension will refer only to
18220 namespaces; DW_AT_abstract_origin refers to functions (and
18221 many things under the function DIE, but we do not recurse
18222 into function DIEs during partial symbol reading) and
18223 possibly variables as well; DW_AT_specification refers to
18224 declarations. Declarations ought to have the DW_AT_declaration
18225 flag. It happens that GCC forgets to put it in sometimes, but
18226 only for functions, not for types.
18227
18228 Adding more things than necessary to the hash table is harmless
18229 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18230 wasted time in find_partial_die, when we reread the compilation
18231 unit with load_all_dies set. */
72bf9492 18232
5afb4e99 18233 if (load_all
72929c62 18234 || abbrev->tag == DW_TAG_constant
5afb4e99 18235 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18236 || abbrev->tag == DW_TAG_variable
18237 || abbrev->tag == DW_TAG_namespace
18238 || part_die->is_declaration)
18239 {
18240 void **slot;
18241
18242 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18243 to_underlying (part_die->sect_off),
18244 INSERT);
72bf9492
DJ
18245 *slot = part_die;
18246 }
18247
72bf9492 18248 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18249 we have no reason to follow the children of structures; for other
98bfdba5
PA
18250 languages we have to, so that we can get at method physnames
18251 to infer fully qualified class names, for DW_AT_specification,
18252 and for C++ template arguments. For C++, we also look one level
18253 inside functions to find template arguments (if the name of the
18254 function does not already contain the template arguments).
bc30ff58 18255
0a4b0913
AB
18256 For Ada and Fortran, we need to scan the children of subprograms
18257 and lexical blocks as well because these languages allow the
18258 definition of nested entities that could be interesting for the
18259 debugger, such as nested subprograms for instance. */
72bf9492 18260 if (last_die->has_children
5afb4e99
DJ
18261 && (load_all
18262 || last_die->tag == DW_TAG_namespace
f55ee35c 18263 || last_die->tag == DW_TAG_module
72bf9492 18264 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18265 || (cu->language == language_cplus
18266 && last_die->tag == DW_TAG_subprogram
18267 && (last_die->name == NULL
18268 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18269 || (cu->language != language_c
18270 && (last_die->tag == DW_TAG_class_type
680b30c7 18271 || last_die->tag == DW_TAG_interface_type
72bf9492 18272 || last_die->tag == DW_TAG_structure_type
bc30ff58 18273 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18274 || ((cu->language == language_ada
18275 || cu->language == language_fortran)
bc30ff58
JB
18276 && (last_die->tag == DW_TAG_subprogram
18277 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18278 {
18279 nesting_level++;
18280 parent_die = last_die;
18281 continue;
18282 }
18283
18284 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18285 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18286
18287 /* Back to the top, do it again. */
18288 }
18289}
18290
6f06d47b
YQ
18291partial_die_info::partial_die_info (sect_offset sect_off_,
18292 struct abbrev_info *abbrev)
18293 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18294{
18295}
18296
35cc7ed7
YQ
18297/* Read a minimal amount of information into the minimal die structure.
18298 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18299
48fbe735
YQ
18300const gdb_byte *
18301partial_die_info::read (const struct die_reader_specs *reader,
18302 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18303{
dee91e82 18304 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18305 struct dwarf2_per_objfile *dwarf2_per_objfile
18306 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18307 unsigned int i;
c5aa993b 18308 int has_low_pc_attr = 0;
c906108c 18309 int has_high_pc_attr = 0;
91da1414 18310 int high_pc_relative = 0;
c906108c 18311
fd0a254f 18312 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18313 {
e7da7f8f 18314 attribute attr;
18a8505e 18315 bool need_reprocess;
e7da7f8f 18316 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18317 info_ptr, &need_reprocess);
18318 /* String and address offsets that need to do the reprocessing have
18319 already been read at this point, so there is no need to wait until
18320 the loop terminates to do the reprocessing. */
18321 if (need_reprocess)
e7da7f8f 18322 read_attribute_reprocess (reader, &attr);
c906108c 18323 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18324 partial symbol table. */
c906108c
SS
18325 switch (attr.name)
18326 {
18327 case DW_AT_name:
48fbe735 18328 switch (tag)
71c25dea
TT
18329 {
18330 case DW_TAG_compile_unit:
95554aad 18331 case DW_TAG_partial_unit:
348e048f 18332 case DW_TAG_type_unit:
71c25dea
TT
18333 /* Compilation units have a DW_AT_name that is a filename, not
18334 a source language identifier. */
18335 case DW_TAG_enumeration_type:
18336 case DW_TAG_enumerator:
18337 /* These tags always have simple identifiers already; no need
18338 to canonicalize them. */
48fbe735 18339 name = DW_STRING (&attr);
71c25dea
TT
18340 break;
18341 default:
48fbe735
YQ
18342 {
18343 struct objfile *objfile = dwarf2_per_objfile->objfile;
18344
18345 name
be1e3d3e 18346 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18347 }
71c25dea
TT
18348 break;
18349 }
c906108c 18350 break;
31ef98ae 18351 case DW_AT_linkage_name:
c906108c 18352 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18353 /* Note that both forms of linkage name might appear. We
18354 assume they will be the same, and we only store the last
18355 one we see. */
e61108c9 18356 linkage_name = attr.value_as_string ();
787de330
TT
18357 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18358 See https://github.com/rust-lang/rust/issues/32925. */
18359 if (cu->language == language_rust && linkage_name != NULL
18360 && strchr (linkage_name, '{') != NULL)
18361 linkage_name = NULL;
c906108c
SS
18362 break;
18363 case DW_AT_low_pc:
18364 has_low_pc_attr = 1;
cd6c91b4 18365 lowpc = attr.value_as_address ();
c906108c
SS
18366 break;
18367 case DW_AT_high_pc:
18368 has_high_pc_attr = 1;
cd6c91b4
TT
18369 highpc = attr.value_as_address ();
18370 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18371 high_pc_relative = 1;
c906108c
SS
18372 break;
18373 case DW_AT_location:
0963b4bd 18374 /* Support the .debug_loc offsets. */
4fc6c0d5 18375 if (attr.form_is_block ())
8e19ed76 18376 {
48fbe735 18377 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18378 }
cd6c91b4 18379 else if (attr.form_is_section_offset ())
8e19ed76 18380 {
4d3c2250 18381 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18382 }
18383 else
18384 {
4d3c2250
KB
18385 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18386 "partial symbol information");
8e19ed76 18387 }
c906108c 18388 break;
c906108c 18389 case DW_AT_external:
48fbe735 18390 is_external = DW_UNSND (&attr);
c906108c
SS
18391 break;
18392 case DW_AT_declaration:
48fbe735 18393 is_declaration = DW_UNSND (&attr);
c906108c
SS
18394 break;
18395 case DW_AT_type:
48fbe735 18396 has_type = 1;
c906108c
SS
18397 break;
18398 case DW_AT_abstract_origin:
18399 case DW_AT_specification:
72bf9492 18400 case DW_AT_extension:
48fbe735 18401 has_specification = 1;
0826b30a 18402 spec_offset = attr.get_ref_die_offset ();
48fbe735 18403 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18404 || cu->per_cu->is_dwz);
c906108c
SS
18405 break;
18406 case DW_AT_sibling:
18407 /* Ignore absolute siblings, they might point outside of
18408 the current compile unit. */
18409 if (attr.form == DW_FORM_ref_addr)
b98664d3 18410 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18411 else
b9502d3f 18412 {
48fbe735 18413 const gdb_byte *buffer = reader->buffer;
0826b30a 18414 sect_offset off = attr.get_ref_die_offset ();
9c541725 18415 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18416
18417 if (sibling_ptr < info_ptr)
b98664d3 18418 complaint (_("DW_AT_sibling points backwards"));
22869d73 18419 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18420 reader->die_section->overflow_complaint ();
b9502d3f 18421 else
48fbe735 18422 sibling = sibling_ptr;
b9502d3f 18423 }
c906108c 18424 break;
fa4028e9 18425 case DW_AT_byte_size:
48fbe735 18426 has_byte_size = 1;
fa4028e9 18427 break;
ff908ebf 18428 case DW_AT_const_value:
48fbe735 18429 has_const_value = 1;
ff908ebf 18430 break;
68511cec
CES
18431 case DW_AT_calling_convention:
18432 /* DWARF doesn't provide a way to identify a program's source-level
18433 entry point. DW_AT_calling_convention attributes are only meant
18434 to describe functions' calling conventions.
18435
18436 However, because it's a necessary piece of information in
0c1b455e
TT
18437 Fortran, and before DWARF 4 DW_CC_program was the only
18438 piece of debugging information whose definition refers to
18439 a 'main program' at all, several compilers marked Fortran
18440 main programs with DW_CC_program --- even when those
18441 functions use the standard calling conventions.
18442
18443 Although DWARF now specifies a way to provide this
18444 information, we support this practice for backward
18445 compatibility. */
68511cec 18446 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18447 && cu->language == language_fortran)
48fbe735 18448 main_subprogram = 1;
68511cec 18449 break;
481860b3
GB
18450 case DW_AT_inline:
18451 if (DW_UNSND (&attr) == DW_INL_inlined
18452 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18453 may_be_inlined = 1;
481860b3 18454 break;
95554aad
TT
18455
18456 case DW_AT_import:
48fbe735 18457 if (tag == DW_TAG_imported_unit)
36586728 18458 {
0826b30a 18459 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18460 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18461 || cu->per_cu->is_dwz);
18462 }
95554aad
TT
18463 break;
18464
0c1b455e 18465 case DW_AT_main_subprogram:
48fbe735 18466 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18467 break;
18468
05caa1d2
TT
18469 case DW_AT_ranges:
18470 {
18471 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18472 but that requires a full DIE, so instead we just
18473 reimplement it. */
18474 int need_ranges_base = tag != DW_TAG_compile_unit;
18475 unsigned int ranges_offset = (DW_UNSND (&attr)
18476 + (need_ranges_base
18477 ? cu->ranges_base
18478 : 0));
18479
18480 /* Value of the DW_AT_ranges attribute is the offset in the
18481 .debug_ranges section. */
18482 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18483 nullptr))
18484 has_pc_info = 1;
18485 }
18486 break;
18487
c906108c
SS
18488 default:
18489 break;
18490 }
18491 }
18492
10d06d82
TT
18493 /* For Ada, if both the name and the linkage name appear, we prefer
18494 the latter. This lets "catch exception" work better, regardless
18495 of the order in which the name and linkage name were emitted.
18496 Really, though, this is just a workaround for the fact that gdb
18497 doesn't store both the name and the linkage name. */
18498 if (cu->language == language_ada && linkage_name != nullptr)
18499 name = linkage_name;
18500
91da1414 18501 if (high_pc_relative)
48fbe735 18502 highpc += lowpc;
91da1414 18503
9373cf26
JK
18504 if (has_low_pc_attr && has_high_pc_attr)
18505 {
18506 /* When using the GNU linker, .gnu.linkonce. sections are used to
18507 eliminate duplicate copies of functions and vtables and such.
18508 The linker will arbitrarily choose one and discard the others.
18509 The AT_*_pc values for such functions refer to local labels in
18510 these sections. If the section from that file was discarded, the
18511 labels are not in the output, so the relocs get a value of 0.
18512 If this is a discarded function, mark the pc bounds as invalid,
18513 so that GDB will ignore it. */
48fbe735 18514 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18515 {
48fbe735 18516 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18517 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18518
b98664d3 18519 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18520 "for DIE at %s [in module %s]"),
48fbe735
YQ
18521 paddress (gdbarch, lowpc),
18522 sect_offset_str (sect_off),
9d8780f0 18523 objfile_name (objfile));
9373cf26
JK
18524 }
18525 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18526 else if (lowpc >= highpc)
9373cf26 18527 {
48fbe735 18528 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18529 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18530
b98664d3 18531 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18532 "for DIE at %s [in module %s]"),
48fbe735
YQ
18533 paddress (gdbarch, lowpc),
18534 paddress (gdbarch, highpc),
18535 sect_offset_str (sect_off),
9c541725 18536 objfile_name (objfile));
9373cf26
JK
18537 }
18538 else
48fbe735 18539 has_pc_info = 1;
9373cf26 18540 }
85cbf3d3 18541
c906108c
SS
18542 return info_ptr;
18543}
18544
72bf9492
DJ
18545/* Find a cached partial DIE at OFFSET in CU. */
18546
d590ff25
YQ
18547struct partial_die_info *
18548dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18549{
18550 struct partial_die_info *lookup_die = NULL;
6f06d47b 18551 struct partial_die_info part_die (sect_off);
72bf9492 18552
9a3c8263 18553 lookup_die = ((struct partial_die_info *)
d590ff25 18554 htab_find_with_hash (partial_dies, &part_die,
9c541725 18555 to_underlying (sect_off)));
72bf9492 18556
72bf9492
DJ
18557 return lookup_die;
18558}
18559
348e048f
DE
18560/* Find a partial DIE at OFFSET, which may or may not be in CU,
18561 except in the case of .debug_types DIEs which do not reference
18562 outside their CU (they do however referencing other types via
55f1336d 18563 DW_FORM_ref_sig8). */
72bf9492 18564
122cf0f2 18565static const struct cu_partial_die_info
9c541725 18566find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18567{
518817b3
SM
18568 struct dwarf2_per_objfile *dwarf2_per_objfile
18569 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18570 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18571 struct dwarf2_per_cu_data *per_cu = NULL;
18572 struct partial_die_info *pd = NULL;
72bf9492 18573
36586728 18574 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18575 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18576 {
d590ff25 18577 pd = cu->find_partial_die (sect_off);
5afb4e99 18578 if (pd != NULL)
fb816e8b 18579 return { cu, pd };
0d99eb77
DE
18580 /* We missed recording what we needed.
18581 Load all dies and try again. */
18582 per_cu = cu->per_cu;
5afb4e99 18583 }
0d99eb77
DE
18584 else
18585 {
18586 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18587 if (cu->per_cu->is_debug_types)
0d99eb77 18588 {
9d8780f0
SM
18589 error (_("Dwarf Error: Type Unit at offset %s contains"
18590 " external reference to offset %s [in module %s].\n"),
18591 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18592 bfd_get_filename (objfile->obfd));
18593 }
9c541725 18594 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18595 dwarf2_per_objfile);
72bf9492 18596
0d99eb77
DE
18597 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18598 load_partial_comp_unit (per_cu);
ae038cb0 18599
0d99eb77 18600 per_cu->cu->last_used = 0;
d590ff25 18601 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18602 }
5afb4e99 18603
dee91e82
DE
18604 /* If we didn't find it, and not all dies have been loaded,
18605 load them all and try again. */
18606
5afb4e99
DJ
18607 if (pd == NULL && per_cu->load_all_dies == 0)
18608 {
5afb4e99 18609 per_cu->load_all_dies = 1;
fd820528
DE
18610
18611 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18612 THIS_CU->cu may already be in use. So we can't just free it and
18613 replace its DIEs with the ones we read in. Instead, we leave those
18614 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18615 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18616 set. */
dee91e82 18617 load_partial_comp_unit (per_cu);
5afb4e99 18618
d590ff25 18619 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18620 }
18621
18622 if (pd == NULL)
18623 internal_error (__FILE__, __LINE__,
9d8780f0 18624 _("could not find partial DIE %s "
3e43a32a 18625 "in cache [from module %s]\n"),
9d8780f0 18626 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18627 return { per_cu->cu, pd };
72bf9492
DJ
18628}
18629
abc72ce4
DE
18630/* See if we can figure out if the class lives in a namespace. We do
18631 this by looking for a member function; its demangled name will
18632 contain namespace info, if there is any. */
18633
18634static void
18635guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18636 struct dwarf2_cu *cu)
18637{
18638 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18639 what template types look like, because the demangler
18640 frequently doesn't give the same name as the debug info. We
18641 could fix this by only using the demangled name to get the
18642 prefix (but see comment in read_structure_type). */
18643
18644 struct partial_die_info *real_pdi;
18645 struct partial_die_info *child_pdi;
18646
18647 /* If this DIE (this DIE's specification, if any) has a parent, then
18648 we should not do this. We'll prepend the parent's fully qualified
18649 name when we create the partial symbol. */
18650
18651 real_pdi = struct_pdi;
18652 while (real_pdi->has_specification)
fb816e8b 18653 {
122cf0f2
AB
18654 auto res = find_partial_die (real_pdi->spec_offset,
18655 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18656 real_pdi = res.pdi;
18657 cu = res.cu;
18658 }
abc72ce4
DE
18659
18660 if (real_pdi->die_parent != NULL)
18661 return;
18662
18663 for (child_pdi = struct_pdi->die_child;
18664 child_pdi != NULL;
18665 child_pdi = child_pdi->die_sibling)
18666 {
18667 if (child_pdi->tag == DW_TAG_subprogram
18668 && child_pdi->linkage_name != NULL)
18669 {
43816ebc
TT
18670 gdb::unique_xmalloc_ptr<char> actual_class_name
18671 (language_class_name_from_physname (cu->language_defn,
18672 child_pdi->linkage_name));
abc72ce4
DE
18673 if (actual_class_name != NULL)
18674 {
518817b3 18675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18676 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18677 }
18678 break;
18679 }
18680 }
18681}
18682
25c11aca
TV
18683/* Return true if a DIE with TAG may have the DW_AT_const_value
18684 attribute. */
18685
18686static bool
18687can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18688{
18689 switch (tag)
18690 {
18691 case DW_TAG_constant:
18692 case DW_TAG_enumerator:
18693 case DW_TAG_formal_parameter:
18694 case DW_TAG_template_value_param:
18695 case DW_TAG_variable:
18696 return true;
18697 }
18698
18699 return false;
18700}
18701
52356b79
YQ
18702void
18703partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18704{
abc72ce4
DE
18705 /* Once we've fixed up a die, there's no point in doing so again.
18706 This also avoids a memory leak if we were to call
18707 guess_partial_die_structure_name multiple times. */
52356b79 18708 if (fixup_called)
abc72ce4
DE
18709 return;
18710
72bf9492
DJ
18711 /* If we found a reference attribute and the DIE has no name, try
18712 to find a name in the referred to DIE. */
18713
52356b79 18714 if (name == NULL && has_specification)
72bf9492
DJ
18715 {
18716 struct partial_die_info *spec_die;
72bf9492 18717
122cf0f2 18718 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18719 spec_die = res.pdi;
18720 cu = res.cu;
72bf9492 18721
52356b79 18722 spec_die->fixup (cu);
72bf9492
DJ
18723
18724 if (spec_die->name)
18725 {
52356b79 18726 name = spec_die->name;
72bf9492
DJ
18727
18728 /* Copy DW_AT_external attribute if it is set. */
18729 if (spec_die->is_external)
52356b79 18730 is_external = spec_die->is_external;
72bf9492
DJ
18731 }
18732 }
18733
25c11aca
TV
18734 if (!has_const_value && has_specification
18735 && can_have_DW_AT_const_value_p (tag))
18736 {
18737 struct partial_die_info *spec_die;
18738
18739 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18740 spec_die = res.pdi;
18741 cu = res.cu;
18742
18743 spec_die->fixup (cu);
18744
18745 if (spec_die->has_const_value)
18746 {
18747 /* Copy DW_AT_const_value attribute if it is set. */
18748 has_const_value = spec_die->has_const_value;
18749 }
18750 }
18751
72bf9492 18752 /* Set default names for some unnamed DIEs. */
72bf9492 18753
52356b79
YQ
18754 if (name == NULL && tag == DW_TAG_namespace)
18755 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18756
abc72ce4
DE
18757 /* If there is no parent die to provide a namespace, and there are
18758 children, see if we can determine the namespace from their linkage
122d1940 18759 name. */
abc72ce4 18760 if (cu->language == language_cplus
fd5866f6 18761 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18762 && die_parent == NULL
18763 && has_children
18764 && (tag == DW_TAG_class_type
18765 || tag == DW_TAG_structure_type
18766 || tag == DW_TAG_union_type))
18767 guess_partial_die_structure_name (this, cu);
abc72ce4 18768
53832f31
TT
18769 /* GCC might emit a nameless struct or union that has a linkage
18770 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18771 if (name == NULL
18772 && (tag == DW_TAG_class_type
18773 || tag == DW_TAG_interface_type
18774 || tag == DW_TAG_structure_type
18775 || tag == DW_TAG_union_type)
18776 && linkage_name != NULL)
53832f31 18777 {
43816ebc
TT
18778 gdb::unique_xmalloc_ptr<char> demangled
18779 (gdb_demangle (linkage_name, DMGL_TYPES));
18780 if (demangled != nullptr)
53832f31 18781 {
96408a79
SA
18782 const char *base;
18783
18784 /* Strip any leading namespaces/classes, keep only the base name.
18785 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18786 base = strrchr (demangled.get (), ':');
18787 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18788 base++;
18789 else
43816ebc 18790 base = demangled.get ();
96408a79 18791
518817b3 18792 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18793 name = objfile->intern (base);
53832f31
TT
18794 }
18795 }
18796
52356b79 18797 fixup_called = 1;
72bf9492
DJ
18798}
18799
41144253 18800/* Read the .debug_loclists header contents from the given SECTION in the
18801 HEADER. */
18802static void
18803read_loclist_header (struct loclist_header *header,
18804 struct dwarf2_section_info *section)
18805{
18806 unsigned int bytes_read;
18807 bfd *abfd = section->get_bfd_owner ();
18808 const gdb_byte *info_ptr = section->buffer;
18809 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18810 info_ptr += bytes_read;
18811 header->version = read_2_bytes (abfd, info_ptr);
18812 info_ptr += 2;
18813 header->addr_size = read_1_byte (abfd, info_ptr);
18814 info_ptr += 1;
18815 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18816 info_ptr += 1;
18817 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18818}
18819
18820/* Return the DW_AT_loclists_base value for the CU. */
18821static ULONGEST
18822lookup_loclist_base (struct dwarf2_cu *cu)
18823{
18824 /* For the .dwo unit, the loclist_base points to the first offset following
18825 the header. The header consists of the following entities-
18826 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18827 bit format)
18828 2. version (2 bytes)
18829 3. address size (1 byte)
18830 4. segment selector size (1 byte)
18831 5. offset entry count (4 bytes)
18832 These sizes are derived as per the DWARFv5 standard. */
18833 if (cu->dwo_unit != nullptr)
18834 {
18835 if (cu->header.initial_length_size == 4)
18836 return LOCLIST_HEADER_SIZE32;
18837 return LOCLIST_HEADER_SIZE64;
18838 }
18839 return cu->loclist_base;
18840}
18841
18842/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18843 array of offsets in the .debug_loclists section. */
18844static CORE_ADDR
18845read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18846{
18847 struct dwarf2_per_objfile *dwarf2_per_objfile
18848 = cu->per_cu->dwarf2_per_objfile;
18849 struct objfile *objfile = dwarf2_per_objfile->objfile;
18850 bfd *abfd = objfile->obfd;
18851 ULONGEST loclist_base = lookup_loclist_base (cu);
18852 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18853
18854 section->read (objfile);
18855 if (section->buffer == NULL)
18856 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18857 "section [in module %s]"), objfile_name (objfile));
18858 struct loclist_header header;
18859 read_loclist_header (&header, section);
18860 if (loclist_index >= header.offset_entry_count)
18861 complaint (_("DW_FORM_loclistx pointing outside of "
18862 ".debug_loclists offset array [in module %s]"),
18863 objfile_name (objfile));
18864 if (loclist_base + loclist_index * cu->header.offset_size
18865 >= section->size)
18866 complaint (_("DW_FORM_loclistx pointing outside of "
18867 ".debug_loclists section [in module %s]"),
18868 objfile_name (objfile));
18869 const gdb_byte *info_ptr
18870 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18871
18872 if (cu->header.offset_size == 4)
18873 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18874 else
18875 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18876}
18877
18a8505e
AT
18878/* Process the attributes that had to be skipped in the first round. These
18879 attributes are the ones that need str_offsets_base or addr_base attributes.
18880 They could not have been processed in the first round, because at the time
18881 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18882static void
18883read_attribute_reprocess (const struct die_reader_specs *reader,
18884 struct attribute *attr)
18a8505e
AT
18885{
18886 struct dwarf2_cu *cu = reader->cu;
18887 switch (attr->form)
18888 {
18889 case DW_FORM_addrx:
18890 case DW_FORM_GNU_addr_index:
18891 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18892 break;
41144253 18893 case DW_FORM_loclistx:
18894 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18895 break;
18a8505e
AT
18896 case DW_FORM_strx:
18897 case DW_FORM_strx1:
18898 case DW_FORM_strx2:
18899 case DW_FORM_strx3:
18900 case DW_FORM_strx4:
18901 case DW_FORM_GNU_str_index:
18902 {
18903 unsigned int str_index = DW_UNSND (attr);
18904 if (reader->dwo_file != NULL)
18905 {
18906 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18907 DW_STRING_IS_CANONICAL (attr) = 0;
18908 }
18909 else
18910 {
18911 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18912 DW_STRING_IS_CANONICAL (attr) = 0;
18913 }
18914 break;
18915 }
18916 default:
18917 gdb_assert_not_reached (_("Unexpected DWARF form."));
18918 }
18919}
18920
a8329558 18921/* Read an attribute value described by an attribute form. */
c906108c 18922
d521ce57 18923static const gdb_byte *
dee91e82
DE
18924read_attribute_value (const struct die_reader_specs *reader,
18925 struct attribute *attr, unsigned form,
18a8505e
AT
18926 LONGEST implicit_const, const gdb_byte *info_ptr,
18927 bool *need_reprocess)
c906108c 18928{
dee91e82 18929 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18930 struct dwarf2_per_objfile *dwarf2_per_objfile
18931 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18932 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 18933 bfd *abfd = reader->abfd;
e7c27a73 18934 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18935 unsigned int bytes_read;
18936 struct dwarf_block *blk;
18a8505e 18937 *need_reprocess = false;
c906108c 18938
aead7601 18939 attr->form = (enum dwarf_form) form;
a8329558 18940 switch (form)
c906108c 18941 {
c906108c 18942 case DW_FORM_ref_addr:
ae411497 18943 if (cu->header.version == 2)
c8a7a66f
TT
18944 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18945 &bytes_read);
ae411497 18946 else
8266302d
TT
18947 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18948 &bytes_read);
ae411497
TT
18949 info_ptr += bytes_read;
18950 break;
36586728 18951 case DW_FORM_GNU_ref_alt:
8266302d 18952 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18953 info_ptr += bytes_read;
18954 break;
ae411497 18955 case DW_FORM_addr:
08feed99
TT
18956 {
18957 struct gdbarch *gdbarch = objfile->arch ();
18958 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18959 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18960 info_ptr += bytes_read;
18961 }
c906108c
SS
18962 break;
18963 case DW_FORM_block2:
7b5a2f43 18964 blk = dwarf_alloc_block (cu);
c906108c
SS
18965 blk->size = read_2_bytes (abfd, info_ptr);
18966 info_ptr += 2;
18967 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18968 info_ptr += blk->size;
18969 DW_BLOCK (attr) = blk;
18970 break;
18971 case DW_FORM_block4:
7b5a2f43 18972 blk = dwarf_alloc_block (cu);
c906108c
SS
18973 blk->size = read_4_bytes (abfd, info_ptr);
18974 info_ptr += 4;
18975 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18976 info_ptr += blk->size;
18977 DW_BLOCK (attr) = blk;
18978 break;
18979 case DW_FORM_data2:
18980 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18981 info_ptr += 2;
18982 break;
18983 case DW_FORM_data4:
18984 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18985 info_ptr += 4;
18986 break;
18987 case DW_FORM_data8:
18988 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18989 info_ptr += 8;
18990 break;
0224619f
JK
18991 case DW_FORM_data16:
18992 blk = dwarf_alloc_block (cu);
18993 blk->size = 16;
18994 blk->data = read_n_bytes (abfd, info_ptr, 16);
18995 info_ptr += 16;
18996 DW_BLOCK (attr) = blk;
18997 break;
2dc7f7b3 18998 case DW_FORM_sec_offset:
8266302d 18999 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19000 info_ptr += bytes_read;
19001 break;
41144253 19002 case DW_FORM_loclistx:
19003 {
19004 *need_reprocess = true;
19005 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19006 info_ptr += bytes_read;
19007 }
19008 break;
c906108c 19009 case DW_FORM_string:
9b1c24c8 19010 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19011 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19012 info_ptr += bytes_read;
19013 break;
4bdf3d34 19014 case DW_FORM_strp:
36586728
TT
19015 if (!cu->per_cu->is_dwz)
19016 {
ed2dc618
SM
19017 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19018 abfd, info_ptr, cu_header,
36586728
TT
19019 &bytes_read);
19020 DW_STRING_IS_CANONICAL (attr) = 0;
19021 info_ptr += bytes_read;
19022 break;
19023 }
19024 /* FALLTHROUGH */
43988095
JK
19025 case DW_FORM_line_strp:
19026 if (!cu->per_cu->is_dwz)
19027 {
86c0bb4c
TT
19028 DW_STRING (attr)
19029 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19030 &bytes_read);
43988095
JK
19031 DW_STRING_IS_CANONICAL (attr) = 0;
19032 info_ptr += bytes_read;
19033 break;
19034 }
19035 /* FALLTHROUGH */
36586728
TT
19036 case DW_FORM_GNU_strp_alt:
19037 {
ed2dc618 19038 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
19039 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19040 &bytes_read);
36586728 19041
0314b390 19042 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19043 DW_STRING_IS_CANONICAL (attr) = 0;
19044 info_ptr += bytes_read;
19045 }
4bdf3d34 19046 break;
2dc7f7b3 19047 case DW_FORM_exprloc:
c906108c 19048 case DW_FORM_block:
7b5a2f43 19049 blk = dwarf_alloc_block (cu);
c906108c
SS
19050 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19051 info_ptr += bytes_read;
19052 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19053 info_ptr += blk->size;
19054 DW_BLOCK (attr) = blk;
19055 break;
19056 case DW_FORM_block1:
7b5a2f43 19057 blk = dwarf_alloc_block (cu);
c906108c
SS
19058 blk->size = read_1_byte (abfd, info_ptr);
19059 info_ptr += 1;
19060 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19061 info_ptr += blk->size;
19062 DW_BLOCK (attr) = blk;
19063 break;
19064 case DW_FORM_data1:
19065 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19066 info_ptr += 1;
19067 break;
19068 case DW_FORM_flag:
19069 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19070 info_ptr += 1;
19071 break;
2dc7f7b3
TT
19072 case DW_FORM_flag_present:
19073 DW_UNSND (attr) = 1;
19074 break;
c906108c
SS
19075 case DW_FORM_sdata:
19076 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19077 info_ptr += bytes_read;
19078 break;
19079 case DW_FORM_udata:
18a8505e 19080 case DW_FORM_rnglistx:
c906108c
SS
19081 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19082 info_ptr += bytes_read;
19083 break;
19084 case DW_FORM_ref1:
9c541725 19085 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19086 + read_1_byte (abfd, info_ptr));
c906108c
SS
19087 info_ptr += 1;
19088 break;
19089 case DW_FORM_ref2:
9c541725 19090 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19091 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19092 info_ptr += 2;
19093 break;
19094 case DW_FORM_ref4:
9c541725 19095 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19096 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19097 info_ptr += 4;
19098 break;
613e1657 19099 case DW_FORM_ref8:
9c541725 19100 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19101 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19102 info_ptr += 8;
19103 break;
55f1336d 19104 case DW_FORM_ref_sig8:
ac9ec31b 19105 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19106 info_ptr += 8;
19107 break;
c906108c 19108 case DW_FORM_ref_udata:
9c541725 19109 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19110 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19111 info_ptr += bytes_read;
19112 break;
c906108c 19113 case DW_FORM_indirect:
a8329558
KW
19114 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19115 info_ptr += bytes_read;
43988095
JK
19116 if (form == DW_FORM_implicit_const)
19117 {
19118 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19119 info_ptr += bytes_read;
19120 }
19121 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19122 info_ptr, need_reprocess);
43988095
JK
19123 break;
19124 case DW_FORM_implicit_const:
19125 DW_SND (attr) = implicit_const;
a8329558 19126 break;
336d760d 19127 case DW_FORM_addrx:
3019eac3 19128 case DW_FORM_GNU_addr_index:
18a8505e
AT
19129 *need_reprocess = true;
19130 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19131 info_ptr += bytes_read;
19132 break;
cf532bd1 19133 case DW_FORM_strx:
15f18d14
AT
19134 case DW_FORM_strx1:
19135 case DW_FORM_strx2:
19136 case DW_FORM_strx3:
19137 case DW_FORM_strx4:
3019eac3 19138 case DW_FORM_GNU_str_index:
3019eac3 19139 {
15f18d14
AT
19140 ULONGEST str_index;
19141 if (form == DW_FORM_strx1)
19142 {
19143 str_index = read_1_byte (abfd, info_ptr);
19144 info_ptr += 1;
19145 }
19146 else if (form == DW_FORM_strx2)
19147 {
19148 str_index = read_2_bytes (abfd, info_ptr);
19149 info_ptr += 2;
19150 }
19151 else if (form == DW_FORM_strx3)
19152 {
19153 str_index = read_3_bytes (abfd, info_ptr);
19154 info_ptr += 3;
19155 }
19156 else if (form == DW_FORM_strx4)
19157 {
19158 str_index = read_4_bytes (abfd, info_ptr);
19159 info_ptr += 4;
19160 }
19161 else
19162 {
19163 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19164 info_ptr += bytes_read;
19165 }
18a8505e
AT
19166 *need_reprocess = true;
19167 DW_UNSND (attr) = str_index;
19168 }
3019eac3 19169 break;
c906108c 19170 default:
8a3fe4f8 19171 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19172 dwarf_form_name (form),
19173 bfd_get_filename (abfd));
c906108c 19174 }
28e94949 19175
36586728 19176 /* Super hack. */
cd6c91b4 19177 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19178 attr->form = DW_FORM_GNU_ref_alt;
19179
28e94949
JB
19180 /* We have seen instances where the compiler tried to emit a byte
19181 size attribute of -1 which ended up being encoded as an unsigned
19182 0xffffffff. Although 0xffffffff is technically a valid size value,
19183 an object of this size seems pretty unlikely so we can relatively
19184 safely treat these cases as if the size attribute was invalid and
19185 treat them as zero by default. */
19186 if (attr->name == DW_AT_byte_size
19187 && form == DW_FORM_data4
19188 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19189 {
19190 complaint
b98664d3 19191 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19192 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19193 DW_UNSND (attr) = 0;
19194 }
28e94949 19195
c906108c
SS
19196 return info_ptr;
19197}
19198
a8329558
KW
19199/* Read an attribute described by an abbreviated attribute. */
19200
d521ce57 19201static const gdb_byte *
dee91e82
DE
19202read_attribute (const struct die_reader_specs *reader,
19203 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19204 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19205{
19206 attr->name = abbrev->name;
43988095 19207 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19208 abbrev->implicit_const, info_ptr,
19209 need_reprocess);
a8329558
KW
19210}
19211
43988095
JK
19212/* Return pointer to string at .debug_str offset STR_OFFSET. */
19213
19214static const char *
ed2dc618 19215read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19216 LONGEST str_offset)
43988095 19217{
4f44ae6c
TT
19218 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
19219 str_offset, "DW_FORM_strp");
c906108c
SS
19220}
19221
43988095
JK
19222/* Return pointer to string at .debug_str offset as read from BUF.
19223 BUF is assumed to be in a compilation unit described by CU_HEADER.
19224 Return *BYTES_READ_PTR count of bytes read from BUF. */
19225
d521ce57 19226static const char *
ed2dc618
SM
19227read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19228 const gdb_byte *buf,
cf2c3c16
TT
19229 const struct comp_unit_head *cu_header,
19230 unsigned int *bytes_read_ptr)
19231{
8266302d 19232 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19233
4f44ae6c 19234 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19235}
19236
86c0bb4c 19237/* See read.h. */
43988095 19238
86c0bb4c
TT
19239const char *
19240dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
43988095
JK
19241 const struct comp_unit_head *cu_header,
19242 unsigned int *bytes_read_ptr)
19243{
86c0bb4c 19244 bfd *abfd = objfile->obfd;
8266302d 19245 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19246
86c0bb4c 19247 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19248}
19249
3019eac3 19250/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19251 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19252 ADDR_SIZE is the size of addresses from the CU header. */
19253
19254static CORE_ADDR
ed2dc618 19255read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19256 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19257 int addr_size)
3019eac3
DE
19258{
19259 struct objfile *objfile = dwarf2_per_objfile->objfile;
19260 bfd *abfd = objfile->obfd;
19261 const gdb_byte *info_ptr;
18a8505e 19262 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19263
96b79293 19264 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
19265 if (dwarf2_per_objfile->addr.buffer == NULL)
19266 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19267 objfile_name (objfile));
18a8505e
AT
19268 if (addr_base_or_zero + addr_index * addr_size
19269 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
19270 error (_("DW_FORM_addr_index pointing outside of "
19271 ".debug_addr section [in module %s]"),
4262abfb 19272 objfile_name (objfile));
3019eac3 19273 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 19274 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19275 if (addr_size == 4)
19276 return bfd_get_32 (abfd, info_ptr);
19277 else
19278 return bfd_get_64 (abfd, info_ptr);
19279}
19280
19281/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19282
19283static CORE_ADDR
19284read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19285{
518817b3
SM
19286 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19287 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19288}
19289
19290/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19291
19292static CORE_ADDR
d521ce57 19293read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19294 unsigned int *bytes_read)
19295{
518817b3 19296 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19297 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19298
19299 return read_addr_index (cu, addr_index);
19300}
19301
450a1bfc 19302/* See read.h. */
3019eac3
DE
19303
19304CORE_ADDR
450a1bfc 19305dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 19306{
ed2dc618 19307 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19308 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19309 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19310 int addr_size;
19311
3019eac3
DE
19312 /* We need addr_base and addr_size.
19313 If we don't have PER_CU->cu, we have to get it.
19314 Nasty, but the alternative is storing the needed info in PER_CU,
19315 which at this point doesn't seem justified: it's not clear how frequently
19316 it would get used and it would increase the size of every PER_CU.
19317 Entry points like dwarf2_per_cu_addr_size do a similar thing
19318 so we're not in uncharted territory here.
19319 Alas we need to be a bit more complicated as addr_base is contained
19320 in the DIE.
19321
19322 We don't need to read the entire CU(/TU).
19323 We just need the header and top level die.
a1b64ce1 19324
3019eac3 19325 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19326 For now we skip this optimization. */
3019eac3
DE
19327
19328 if (cu != NULL)
19329 {
19330 addr_base = cu->addr_base;
19331 addr_size = cu->header.addr_size;
19332 }
19333 else
19334 {
6751ebae 19335 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
19336 addr_base = reader.cu->addr_base;
19337 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19338 }
19339
ed2dc618
SM
19340 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19341 addr_size);
3019eac3
DE
19342}
19343
18a8505e
AT
19344/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19345 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19346 DWO file. */
3019eac3 19347
d521ce57 19348static const char *
18a8505e
AT
19349read_str_index (struct dwarf2_cu *cu,
19350 struct dwarf2_section_info *str_section,
19351 struct dwarf2_section_info *str_offsets_section,
19352 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19353{
518817b3
SM
19354 struct dwarf2_per_objfile *dwarf2_per_objfile
19355 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19356 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19357 const char *objf_name = objfile_name (objfile);
3019eac3 19358 bfd *abfd = objfile->obfd;
d521ce57 19359 const gdb_byte *info_ptr;
3019eac3 19360 ULONGEST str_offset;
cf532bd1 19361 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19362
96b79293
TT
19363 str_section->read (objfile);
19364 str_offsets_section->read (objfile);
73869dc2 19365 if (str_section->buffer == NULL)
18a8505e 19366 error (_("%s used without %s section"
9d8780f0 19367 " in CU at offset %s [in module %s]"),
96b79293 19368 form_name, str_section->get_name (),
18a8505e 19369 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19370 if (str_offsets_section->buffer == NULL)
18a8505e 19371 error (_("%s used without %s section"
9d8780f0 19372 " in CU at offset %s [in module %s]"),
96b79293 19373 form_name, str_section->get_name (),
18a8505e 19374 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19375 info_ptr = (str_offsets_section->buffer
18a8505e 19376 + str_offsets_base
3019eac3
DE
19377 + str_index * cu->header.offset_size);
19378 if (cu->header.offset_size == 4)
19379 str_offset = bfd_get_32 (abfd, info_ptr);
19380 else
19381 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19382 if (str_offset >= str_section->size)
57d63ce2 19383 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19384 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19385 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19386 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19387}
19388
18a8505e
AT
19389/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19390
19391static const char *
19392read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19393{
19394 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19395 ? reader->cu->header.addr_size : 0;
19396 return read_str_index (reader->cu,
19397 &reader->dwo_file->sections.str,
19398 &reader->dwo_file->sections.str_offsets,
19399 str_offsets_base, str_index);
19400}
19401
19402/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19403
19404static const char *
19405read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19406{
19407 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19408 const char *objf_name = objfile_name (objfile);
19409 static const char form_name[] = "DW_FORM_GNU_str_index";
19410 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19411
19412 if (!cu->str_offsets_base.has_value ())
19413 error (_("%s used in Fission stub without %s"
19414 " in CU at offset 0x%lx [in module %s]"),
19415 form_name, str_offsets_attr_name,
19416 (long) cu->header.offset_size, objf_name);
19417
19418 return read_str_index (cu,
19419 &cu->per_cu->dwarf2_per_objfile->str,
19420 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19421 *cu->str_offsets_base, str_index);
19422}
19423
3019eac3
DE
19424/* Return the length of an LEB128 number in BUF. */
19425
19426static int
19427leb128_size (const gdb_byte *buf)
19428{
19429 const gdb_byte *begin = buf;
19430 gdb_byte byte;
19431
19432 while (1)
19433 {
19434 byte = *buf++;
19435 if ((byte & 128) == 0)
19436 return buf - begin;
19437 }
19438}
19439
c906108c 19440static void
e142c38c 19441set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19442{
19443 switch (lang)
19444 {
19445 case DW_LANG_C89:
76bee0cc 19446 case DW_LANG_C99:
0cfd832f 19447 case DW_LANG_C11:
c906108c 19448 case DW_LANG_C:
d1be3247 19449 case DW_LANG_UPC:
e142c38c 19450 cu->language = language_c;
c906108c 19451 break;
9c37b5ae 19452 case DW_LANG_Java:
c906108c 19453 case DW_LANG_C_plus_plus:
0cfd832f
MW
19454 case DW_LANG_C_plus_plus_11:
19455 case DW_LANG_C_plus_plus_14:
e142c38c 19456 cu->language = language_cplus;
c906108c 19457 break;
6aecb9c2
JB
19458 case DW_LANG_D:
19459 cu->language = language_d;
19460 break;
c906108c
SS
19461 case DW_LANG_Fortran77:
19462 case DW_LANG_Fortran90:
b21b22e0 19463 case DW_LANG_Fortran95:
f7de9aab
MW
19464 case DW_LANG_Fortran03:
19465 case DW_LANG_Fortran08:
e142c38c 19466 cu->language = language_fortran;
c906108c 19467 break;
a766d390
DE
19468 case DW_LANG_Go:
19469 cu->language = language_go;
19470 break;
c906108c 19471 case DW_LANG_Mips_Assembler:
e142c38c 19472 cu->language = language_asm;
c906108c
SS
19473 break;
19474 case DW_LANG_Ada83:
8aaf0b47 19475 case DW_LANG_Ada95:
bc5f45f8
JB
19476 cu->language = language_ada;
19477 break;
72019c9c
GM
19478 case DW_LANG_Modula2:
19479 cu->language = language_m2;
19480 break;
fe8e67fd
PM
19481 case DW_LANG_Pascal83:
19482 cu->language = language_pascal;
19483 break;
22566fbd
DJ
19484 case DW_LANG_ObjC:
19485 cu->language = language_objc;
19486 break;
c44af4eb
TT
19487 case DW_LANG_Rust:
19488 case DW_LANG_Rust_old:
19489 cu->language = language_rust;
19490 break;
c906108c
SS
19491 case DW_LANG_Cobol74:
19492 case DW_LANG_Cobol85:
c906108c 19493 default:
e142c38c 19494 cu->language = language_minimal;
c906108c
SS
19495 break;
19496 }
e142c38c 19497 cu->language_defn = language_def (cu->language);
c906108c
SS
19498}
19499
19500/* Return the named attribute or NULL if not there. */
19501
19502static struct attribute *
e142c38c 19503dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19504{
a48e046c 19505 for (;;)
c906108c 19506 {
a48e046c
TT
19507 unsigned int i;
19508 struct attribute *spec = NULL;
19509
19510 for (i = 0; i < die->num_attrs; ++i)
19511 {
19512 if (die->attrs[i].name == name)
19513 return &die->attrs[i];
19514 if (die->attrs[i].name == DW_AT_specification
19515 || die->attrs[i].name == DW_AT_abstract_origin)
19516 spec = &die->attrs[i];
19517 }
19518
19519 if (!spec)
19520 break;
c906108c 19521
f2f0e013 19522 die = follow_die_ref (die, spec, &cu);
f2f0e013 19523 }
c5aa993b 19524
c906108c
SS
19525 return NULL;
19526}
19527
7d45c7c3
KB
19528/* Return the string associated with a string-typed attribute, or NULL if it
19529 is either not found or is of an incorrect type. */
19530
19531static const char *
19532dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19533{
19534 struct attribute *attr;
19535 const char *str = NULL;
19536
19537 attr = dwarf2_attr (die, name, cu);
19538
19539 if (attr != NULL)
19540 {
e61108c9
TT
19541 str = attr->value_as_string ();
19542 if (str == nullptr)
b98664d3 19543 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19544 "DIE at %s in module %s"),
19545 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19546 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19547 }
19548
19549 return str;
19550}
19551
a084a2a6 19552/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19553 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19554static const char *
19555dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19556{
19557 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19558 if (dwo_name == nullptr)
19559 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19560 return dwo_name;
19561}
19562
05cf31d1
JB
19563/* Return non-zero iff the attribute NAME is defined for the given DIE,
19564 and holds a non-zero value. This function should only be used for
2dc7f7b3 19565 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19566
19567static int
19568dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19569{
19570 struct attribute *attr = dwarf2_attr (die, name, cu);
19571
19572 return (attr && DW_UNSND (attr));
19573}
19574
3ca72b44 19575static int
e142c38c 19576die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19577{
05cf31d1
JB
19578 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19579 which value is non-zero. However, we have to be careful with
19580 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19581 (via dwarf2_flag_true_p) follows this attribute. So we may
19582 end up accidently finding a declaration attribute that belongs
19583 to a different DIE referenced by the specification attribute,
19584 even though the given DIE does not have a declaration attribute. */
19585 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19586 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19587}
19588
63d06c5c 19589/* Return the die giving the specification for DIE, if there is
f2f0e013 19590 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19591 containing the return value on output. If there is no
19592 specification, but there is an abstract origin, that is
19593 returned. */
63d06c5c
DC
19594
19595static struct die_info *
f2f0e013 19596die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19597{
f2f0e013
DJ
19598 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19599 *spec_cu);
63d06c5c 19600
edb3359d
DJ
19601 if (spec_attr == NULL)
19602 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19603
63d06c5c
DC
19604 if (spec_attr == NULL)
19605 return NULL;
19606 else
f2f0e013 19607 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19608}
c906108c 19609
527f3840
JK
19610/* Stub for free_line_header to match void * callback types. */
19611
19612static void
19613free_line_header_voidp (void *arg)
19614{
9a3c8263 19615 struct line_header *lh = (struct line_header *) arg;
527f3840 19616
fff8551c 19617 delete lh;
527f3840
JK
19618}
19619
83769d0b 19620/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19621
19622static struct dwarf2_section_info *
19623get_debug_line_section (struct dwarf2_cu *cu)
19624{
19625 struct dwarf2_section_info *section;
518817b3
SM
19626 struct dwarf2_per_objfile *dwarf2_per_objfile
19627 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19628
19629 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19630 DWO file. */
19631 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19632 section = &cu->dwo_unit->dwo_file->sections.line;
19633 else if (cu->per_cu->is_dwz)
19634 {
ed2dc618 19635 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19636
19637 section = &dwz->line;
19638 }
19639 else
19640 section = &dwarf2_per_objfile->line;
19641
19642 return section;
19643}
19644
debd256d 19645/* Read the statement program header starting at OFFSET in
3019eac3 19646 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19647 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19648 Returns NULL if there is a problem reading the header, e.g., if it
19649 has a version we don't understand.
debd256d
JB
19650
19651 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19652 the returned object point into the dwarf line section buffer,
19653 and must not be freed. */
ae2de4f8 19654
fff8551c 19655static line_header_up
9c541725 19656dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19657{
3019eac3 19658 struct dwarf2_section_info *section;
518817b3
SM
19659 struct dwarf2_per_objfile *dwarf2_per_objfile
19660 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19661
36586728 19662 section = get_debug_line_section (cu);
96b79293 19663 section->read (dwarf2_per_objfile->objfile);
3019eac3 19664 if (section->buffer == NULL)
debd256d 19665 {
3019eac3 19666 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19667 complaint (_("missing .debug_line.dwo section"));
3019eac3 19668 else
b98664d3 19669 complaint (_("missing .debug_line section"));
debd256d
JB
19670 return 0;
19671 }
19672
0df7ad3a
TT
19673 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19674 dwarf2_per_objfile, section,
19675 &cu->header);
debd256d 19676}
c906108c 19677
c6da4cef 19678/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19679 Return the file name of the psymtab for the given file_entry.
c6da4cef 19680 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19681 If space for the result is malloc'd, *NAME_HOLDER will be set.
19682 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19683
d521ce57 19684static const char *
7ba99d21 19685psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19686 const dwarf2_psymtab *pst,
c89b44cd
TT
19687 const char *comp_dir,
19688 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19689{
d521ce57
TT
19690 const char *include_name = fe.name;
19691 const char *include_name_to_compare = include_name;
72b9f47f 19692 const char *pst_filename;
c6da4cef
DE
19693 int file_is_pst;
19694
8c43009f 19695 const char *dir_name = fe.include_dir (lh);
c6da4cef 19696
c89b44cd 19697 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19698 if (!IS_ABSOLUTE_PATH (include_name)
19699 && (dir_name != NULL || comp_dir != NULL))
19700 {
19701 /* Avoid creating a duplicate psymtab for PST.
19702 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19703 Before we do the comparison, however, we need to account
19704 for DIR_NAME and COMP_DIR.
19705 First prepend dir_name (if non-NULL). If we still don't
19706 have an absolute path prepend comp_dir (if non-NULL).
19707 However, the directory we record in the include-file's
19708 psymtab does not contain COMP_DIR (to match the
19709 corresponding symtab(s)).
19710
19711 Example:
19712
19713 bash$ cd /tmp
19714 bash$ gcc -g ./hello.c
19715 include_name = "hello.c"
19716 dir_name = "."
19717 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19718 DW_AT_name = "./hello.c"
19719
19720 */
c6da4cef
DE
19721
19722 if (dir_name != NULL)
19723 {
c89b44cd
TT
19724 name_holder->reset (concat (dir_name, SLASH_STRING,
19725 include_name, (char *) NULL));
19726 include_name = name_holder->get ();
c6da4cef 19727 include_name_to_compare = include_name;
c6da4cef
DE
19728 }
19729 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19730 {
c89b44cd
TT
19731 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19732 include_name, (char *) NULL));
19733 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19734 }
19735 }
19736
19737 pst_filename = pst->filename;
c89b44cd 19738 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19739 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19740 {
c89b44cd
TT
19741 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19742 pst_filename, (char *) NULL));
19743 pst_filename = copied_name.get ();
c6da4cef
DE
19744 }
19745
1e3fad37 19746 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19747
c6da4cef
DE
19748 if (file_is_pst)
19749 return NULL;
19750 return include_name;
19751}
19752
d9b3de22
DE
19753/* State machine to track the state of the line number program. */
19754
6f77053d 19755class lnp_state_machine
d9b3de22 19756{
6f77053d
PA
19757public:
19758 /* Initialize a machine state for the start of a line number
19759 program. */
804d2729
TT
19760 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19761 bool record_lines_p);
6f77053d 19762
8c43009f
PA
19763 file_entry *current_file ()
19764 {
19765 /* lh->file_names is 0-based, but the file name numbers in the
19766 statement program are 1-based. */
6f77053d
PA
19767 return m_line_header->file_name_at (m_file);
19768 }
19769
19770 /* Record the line in the state machine. END_SEQUENCE is true if
19771 we're processing the end of a sequence. */
19772 void record_line (bool end_sequence);
19773
7ab6656f
OJ
19774 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19775 nop-out rest of the lines in this sequence. */
6f77053d
PA
19776 void check_line_address (struct dwarf2_cu *cu,
19777 const gdb_byte *line_ptr,
7ab6656f 19778 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19779
19780 void handle_set_discriminator (unsigned int discriminator)
19781 {
19782 m_discriminator = discriminator;
19783 m_line_has_non_zero_discriminator |= discriminator != 0;
19784 }
19785
19786 /* Handle DW_LNE_set_address. */
19787 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19788 {
19789 m_op_index = 0;
19790 address += baseaddr;
19791 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19792 }
19793
19794 /* Handle DW_LNS_advance_pc. */
19795 void handle_advance_pc (CORE_ADDR adjust);
19796
19797 /* Handle a special opcode. */
19798 void handle_special_opcode (unsigned char op_code);
19799
19800 /* Handle DW_LNS_advance_line. */
19801 void handle_advance_line (int line_delta)
19802 {
19803 advance_line (line_delta);
19804 }
19805
19806 /* Handle DW_LNS_set_file. */
19807 void handle_set_file (file_name_index file);
19808
19809 /* Handle DW_LNS_negate_stmt. */
19810 void handle_negate_stmt ()
19811 {
19812 m_is_stmt = !m_is_stmt;
19813 }
19814
19815 /* Handle DW_LNS_const_add_pc. */
19816 void handle_const_add_pc ();
19817
19818 /* Handle DW_LNS_fixed_advance_pc. */
19819 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19820 {
19821 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19822 m_op_index = 0;
19823 }
19824
19825 /* Handle DW_LNS_copy. */
19826 void handle_copy ()
19827 {
19828 record_line (false);
19829 m_discriminator = 0;
19830 }
19831
19832 /* Handle DW_LNE_end_sequence. */
19833 void handle_end_sequence ()
19834 {
804d2729 19835 m_currently_recording_lines = true;
6f77053d
PA
19836 }
19837
19838private:
19839 /* Advance the line by LINE_DELTA. */
19840 void advance_line (int line_delta)
19841 {
19842 m_line += line_delta;
19843
19844 if (line_delta != 0)
19845 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19846 }
19847
804d2729
TT
19848 struct dwarf2_cu *m_cu;
19849
6f77053d
PA
19850 gdbarch *m_gdbarch;
19851
19852 /* True if we're recording lines.
19853 Otherwise we're building partial symtabs and are just interested in
19854 finding include files mentioned by the line number program. */
19855 bool m_record_lines_p;
19856
8c43009f 19857 /* The line number header. */
6f77053d 19858 line_header *m_line_header;
8c43009f 19859
6f77053d
PA
19860 /* These are part of the standard DWARF line number state machine,
19861 and initialized according to the DWARF spec. */
d9b3de22 19862
6f77053d 19863 unsigned char m_op_index = 0;
7ba99d21
AT
19864 /* The line table index of the current file. */
19865 file_name_index m_file = 1;
6f77053d
PA
19866 unsigned int m_line = 1;
19867
19868 /* These are initialized in the constructor. */
19869
19870 CORE_ADDR m_address;
19871 bool m_is_stmt;
19872 unsigned int m_discriminator;
d9b3de22
DE
19873
19874 /* Additional bits of state we need to track. */
19875
19876 /* The last file that we called dwarf2_start_subfile for.
19877 This is only used for TLLs. */
6f77053d 19878 unsigned int m_last_file = 0;
d9b3de22 19879 /* The last file a line number was recorded for. */
6f77053d 19880 struct subfile *m_last_subfile = NULL;
d9b3de22 19881
804d2729
TT
19882 /* When true, record the lines we decode. */
19883 bool m_currently_recording_lines = false;
d9b3de22
DE
19884
19885 /* The last line number that was recorded, used to coalesce
19886 consecutive entries for the same line. This can happen, for
19887 example, when discriminators are present. PR 17276. */
6f77053d
PA
19888 unsigned int m_last_line = 0;
19889 bool m_line_has_non_zero_discriminator = false;
8c43009f 19890};
d9b3de22 19891
6f77053d
PA
19892void
19893lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19894{
19895 CORE_ADDR addr_adj = (((m_op_index + adjust)
19896 / m_line_header->maximum_ops_per_instruction)
19897 * m_line_header->minimum_instruction_length);
19898 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19899 m_op_index = ((m_op_index + adjust)
19900 % m_line_header->maximum_ops_per_instruction);
19901}
d9b3de22 19902
6f77053d
PA
19903void
19904lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19905{
6f77053d 19906 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19907 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19908 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19909 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19910 / m_line_header->maximum_ops_per_instruction)
19911 * m_line_header->minimum_instruction_length);
19912 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19913 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19914 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19915
258bf0ee 19916 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19917 advance_line (line_delta);
19918 record_line (false);
19919 m_discriminator = 0;
19920}
d9b3de22 19921
6f77053d
PA
19922void
19923lnp_state_machine::handle_set_file (file_name_index file)
19924{
19925 m_file = file;
19926
19927 const file_entry *fe = current_file ();
19928 if (fe == NULL)
19929 dwarf2_debug_line_missing_file_complaint ();
19930 else if (m_record_lines_p)
19931 {
19932 const char *dir = fe->include_dir (m_line_header);
19933
c24bdb02 19934 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19935 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19936 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19937 }
19938}
19939
19940void
19941lnp_state_machine::handle_const_add_pc ()
19942{
19943 CORE_ADDR adjust
19944 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19945
19946 CORE_ADDR addr_adj
19947 = (((m_op_index + adjust)
19948 / m_line_header->maximum_ops_per_instruction)
19949 * m_line_header->minimum_instruction_length);
19950
19951 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19952 m_op_index = ((m_op_index + adjust)
19953 % m_line_header->maximum_ops_per_instruction);
19954}
d9b3de22 19955
a05a36a5
DE
19956/* Return non-zero if we should add LINE to the line number table.
19957 LINE is the line to add, LAST_LINE is the last line that was added,
19958 LAST_SUBFILE is the subfile for LAST_LINE.
19959 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19960 had a non-zero discriminator.
19961
19962 We have to be careful in the presence of discriminators.
19963 E.g., for this line:
19964
19965 for (i = 0; i < 100000; i++);
19966
19967 clang can emit four line number entries for that one line,
19968 each with a different discriminator.
19969 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19970
19971 However, we want gdb to coalesce all four entries into one.
19972 Otherwise the user could stepi into the middle of the line and
19973 gdb would get confused about whether the pc really was in the
19974 middle of the line.
19975
19976 Things are further complicated by the fact that two consecutive
19977 line number entries for the same line is a heuristic used by gcc
19978 to denote the end of the prologue. So we can't just discard duplicate
19979 entries, we have to be selective about it. The heuristic we use is
19980 that we only collapse consecutive entries for the same line if at least
19981 one of those entries has a non-zero discriminator. PR 17276.
19982
19983 Note: Addresses in the line number state machine can never go backwards
19984 within one sequence, thus this coalescing is ok. */
19985
19986static int
804d2729
TT
19987dwarf_record_line_p (struct dwarf2_cu *cu,
19988 unsigned int line, unsigned int last_line,
a05a36a5
DE
19989 int line_has_non_zero_discriminator,
19990 struct subfile *last_subfile)
19991{
c24bdb02 19992 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19993 return 1;
19994 if (line != last_line)
19995 return 1;
19996 /* Same line for the same file that we've seen already.
19997 As a last check, for pr 17276, only record the line if the line
19998 has never had a non-zero discriminator. */
19999 if (!line_has_non_zero_discriminator)
20000 return 1;
20001 return 0;
20002}
20003
804d2729
TT
20004/* Use the CU's builder to record line number LINE beginning at
20005 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20006
20007static void
d9b3de22 20008dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20009 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20010 struct dwarf2_cu *cu)
252a6764
DE
20011{
20012 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20013
27e0867f
DE
20014 if (dwarf_line_debug)
20015 {
20016 fprintf_unfiltered (gdb_stdlog,
20017 "Recording line %u, file %s, address %s\n",
20018 line, lbasename (subfile->name),
20019 paddress (gdbarch, address));
20020 }
20021
804d2729 20022 if (cu != nullptr)
8c95582d 20023 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20024}
20025
20026/* Subroutine of dwarf_decode_lines_1 to simplify it.
20027 Mark the end of a set of line number records.
d9b3de22 20028 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20029 If SUBFILE is NULL the request is ignored. */
20030
20031static void
20032dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20033 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20034{
27e0867f
DE
20035 if (subfile == NULL)
20036 return;
20037
20038 if (dwarf_line_debug)
20039 {
20040 fprintf_unfiltered (gdb_stdlog,
20041 "Finishing current line, file %s, address %s\n",
20042 lbasename (subfile->name),
20043 paddress (gdbarch, address));
20044 }
20045
8c95582d 20046 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20047}
20048
6f77053d
PA
20049void
20050lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20051{
d9b3de22
DE
20052 if (dwarf_line_debug)
20053 {
20054 fprintf_unfiltered (gdb_stdlog,
20055 "Processing actual line %u: file %u,"
94a72be7 20056 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20057 m_line, m_file,
6f77053d 20058 paddress (m_gdbarch, m_address),
94a72be7
AB
20059 m_is_stmt, m_discriminator,
20060 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20061 }
20062
6f77053d 20063 file_entry *fe = current_file ();
8c43009f
PA
20064
20065 if (fe == NULL)
d9b3de22
DE
20066 dwarf2_debug_line_missing_file_complaint ();
20067 /* For now we ignore lines not starting on an instruction boundary.
20068 But not when processing end_sequence for compatibility with the
20069 previous version of the code. */
6f77053d 20070 else if (m_op_index == 0 || end_sequence)
d9b3de22 20071 {
8c43009f 20072 fe->included_p = 1;
8c95582d 20073 if (m_record_lines_p)
d9b3de22 20074 {
c24bdb02 20075 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20076 || end_sequence)
d9b3de22 20077 {
804d2729
TT
20078 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20079 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20080 }
20081
20082 if (!end_sequence)
20083 {
8c95582d
AB
20084 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20085
804d2729 20086 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20087 m_line_has_non_zero_discriminator,
20088 m_last_subfile))
d9b3de22 20089 {
c24bdb02 20090 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20091 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20092 builder->get_current_subfile (),
8c95582d 20093 m_line, m_address, is_stmt,
804d2729 20094 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20095 }
c24bdb02 20096 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20097 m_last_line = m_line;
d9b3de22
DE
20098 }
20099 }
20100 }
20101}
20102
804d2729
TT
20103lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20104 line_header *lh, bool record_lines_p)
d9b3de22 20105{
804d2729 20106 m_cu = cu;
6f77053d
PA
20107 m_gdbarch = arch;
20108 m_record_lines_p = record_lines_p;
20109 m_line_header = lh;
d9b3de22 20110
804d2729 20111 m_currently_recording_lines = true;
d9b3de22 20112
d9b3de22
DE
20113 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20114 was a line entry for it so that the backend has a chance to adjust it
20115 and also record it in case it needs it. This is currently used by MIPS
20116 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20117 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20118 m_is_stmt = lh->default_is_stmt;
20119 m_discriminator = 0;
252a6764
DE
20120}
20121
6f77053d
PA
20122void
20123lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20124 const gdb_byte *line_ptr,
7ab6656f 20125 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20126{
7ab6656f
OJ
20127 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20128 the pc range of the CU. However, we restrict the test to only ADDRESS
20129 values of zero to preserve GDB's previous behaviour which is to handle
20130 the specific case of a function being GC'd by the linker. */
924c2928 20131
7ab6656f 20132 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20133 {
20134 /* This line table is for a function which has been
20135 GCd by the linker. Ignore it. PR gdb/12528 */
20136
518817b3 20137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20138 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20139
b98664d3 20140 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20141 line_offset, objfile_name (objfile));
804d2729
TT
20142 m_currently_recording_lines = false;
20143 /* Note: m_currently_recording_lines is left as false until we see
20144 DW_LNE_end_sequence. */
924c2928
DE
20145 }
20146}
20147
f3f5162e 20148/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20149 Process the line number information in LH.
20150 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20151 program in order to set included_p for every referenced header. */
debd256d 20152
c906108c 20153static void
43f3e411
DE
20154dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20155 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20156{
d521ce57
TT
20157 const gdb_byte *line_ptr, *extended_end;
20158 const gdb_byte *line_end;
a8c50c1f 20159 unsigned int bytes_read, extended_len;
699ca60a 20160 unsigned char op_code, extended_op;
e142c38c 20161 CORE_ADDR baseaddr;
518817b3 20162 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20163 bfd *abfd = objfile->obfd;
08feed99 20164 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20165 /* True if we're recording line info (as opposed to building partial
20166 symtabs and just interested in finding include files mentioned by
20167 the line number program). */
20168 bool record_lines_p = !decode_for_pst_p;
e142c38c 20169
b3b3bada 20170 baseaddr = objfile->text_section_offset ();
c906108c 20171
debd256d
JB
20172 line_ptr = lh->statement_program_start;
20173 line_end = lh->statement_program_end;
c906108c
SS
20174
20175 /* Read the statement sequences until there's nothing left. */
20176 while (line_ptr < line_end)
20177 {
6f77053d
PA
20178 /* The DWARF line number program state machine. Reset the state
20179 machine at the start of each sequence. */
804d2729 20180 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20181 bool end_sequence = false;
d9b3de22 20182
8c43009f 20183 if (record_lines_p)
c906108c 20184 {
8c43009f
PA
20185 /* Start a subfile for the current file of the state
20186 machine. */
20187 const file_entry *fe = state_machine.current_file ();
20188
20189 if (fe != NULL)
804d2729 20190 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20191 }
20192
a738430d 20193 /* Decode the table. */
d9b3de22 20194 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20195 {
20196 op_code = read_1_byte (abfd, line_ptr);
20197 line_ptr += 1;
9aa1fe7e 20198
debd256d 20199 if (op_code >= lh->opcode_base)
6e70227d 20200 {
8e07a239 20201 /* Special opcode. */
6f77053d 20202 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20203 }
20204 else switch (op_code)
c906108c
SS
20205 {
20206 case DW_LNS_extended_op:
3e43a32a
MS
20207 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20208 &bytes_read);
473b7be6 20209 line_ptr += bytes_read;
a8c50c1f 20210 extended_end = line_ptr + extended_len;
c906108c
SS
20211 extended_op = read_1_byte (abfd, line_ptr);
20212 line_ptr += 1;
20213 switch (extended_op)
20214 {
20215 case DW_LNE_end_sequence:
6f77053d
PA
20216 state_machine.handle_end_sequence ();
20217 end_sequence = true;
c906108c
SS
20218 break;
20219 case DW_LNE_set_address:
d9b3de22
DE
20220 {
20221 CORE_ADDR address
c8a7a66f 20222 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20223 line_ptr += bytes_read;
6f77053d
PA
20224
20225 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20226 lowpc - baseaddr, address);
6f77053d 20227 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20228 }
c906108c
SS
20229 break;
20230 case DW_LNE_define_file:
debd256d 20231 {
d521ce57 20232 const char *cur_file;
ecfb656c
PA
20233 unsigned int mod_time, length;
20234 dir_index dindex;
6e70227d 20235
3e43a32a
MS
20236 cur_file = read_direct_string (abfd, line_ptr,
20237 &bytes_read);
debd256d 20238 line_ptr += bytes_read;
ecfb656c 20239 dindex = (dir_index)
debd256d
JB
20240 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20241 line_ptr += bytes_read;
20242 mod_time =
20243 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20244 line_ptr += bytes_read;
20245 length =
20246 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20247 line_ptr += bytes_read;
ecfb656c 20248 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20249 }
c906108c 20250 break;
d0c6ba3d 20251 case DW_LNE_set_discriminator:
6f77053d
PA
20252 {
20253 /* The discriminator is not interesting to the
20254 debugger; just ignore it. We still need to
20255 check its value though:
20256 if there are consecutive entries for the same
20257 (non-prologue) line we want to coalesce them.
20258 PR 17276. */
20259 unsigned int discr
20260 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20261 line_ptr += bytes_read;
20262
20263 state_machine.handle_set_discriminator (discr);
20264 }
d0c6ba3d 20265 break;
c906108c 20266 default:
b98664d3 20267 complaint (_("mangled .debug_line section"));
debd256d 20268 return;
c906108c 20269 }
a8c50c1f
DJ
20270 /* Make sure that we parsed the extended op correctly. If e.g.
20271 we expected a different address size than the producer used,
20272 we may have read the wrong number of bytes. */
20273 if (line_ptr != extended_end)
20274 {
b98664d3 20275 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20276 return;
20277 }
c906108c
SS
20278 break;
20279 case DW_LNS_copy:
6f77053d 20280 state_machine.handle_copy ();
c906108c
SS
20281 break;
20282 case DW_LNS_advance_pc:
2dc7f7b3
TT
20283 {
20284 CORE_ADDR adjust
20285 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20286 line_ptr += bytes_read;
6f77053d
PA
20287
20288 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20289 }
c906108c
SS
20290 break;
20291 case DW_LNS_advance_line:
a05a36a5
DE
20292 {
20293 int line_delta
20294 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20295 line_ptr += bytes_read;
6f77053d
PA
20296
20297 state_machine.handle_advance_line (line_delta);
a05a36a5 20298 }
c906108c
SS
20299 break;
20300 case DW_LNS_set_file:
d9b3de22 20301 {
6f77053d 20302 file_name_index file
ecfb656c
PA
20303 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20304 &bytes_read);
d9b3de22 20305 line_ptr += bytes_read;
8c43009f 20306
6f77053d 20307 state_machine.handle_set_file (file);
d9b3de22 20308 }
c906108c
SS
20309 break;
20310 case DW_LNS_set_column:
0ad93d4f 20311 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20312 line_ptr += bytes_read;
20313 break;
20314 case DW_LNS_negate_stmt:
6f77053d 20315 state_machine.handle_negate_stmt ();
c906108c
SS
20316 break;
20317 case DW_LNS_set_basic_block:
c906108c 20318 break;
c2c6d25f
JM
20319 /* Add to the address register of the state machine the
20320 address increment value corresponding to special opcode
a738430d
MK
20321 255. I.e., this value is scaled by the minimum
20322 instruction length since special opcode 255 would have
b021a221 20323 scaled the increment. */
c906108c 20324 case DW_LNS_const_add_pc:
6f77053d 20325 state_machine.handle_const_add_pc ();
c906108c
SS
20326 break;
20327 case DW_LNS_fixed_advance_pc:
3e29f34a 20328 {
6f77053d 20329 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20330 line_ptr += 2;
6f77053d
PA
20331
20332 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20333 }
c906108c 20334 break;
9aa1fe7e 20335 default:
a738430d
MK
20336 {
20337 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20338 int i;
a738430d 20339
debd256d 20340 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20341 {
20342 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20343 line_ptr += bytes_read;
20344 }
20345 }
c906108c
SS
20346 }
20347 }
d9b3de22
DE
20348
20349 if (!end_sequence)
20350 dwarf2_debug_line_missing_end_sequence_complaint ();
20351
20352 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20353 in which case we still finish recording the last line). */
6f77053d 20354 state_machine.record_line (true);
c906108c 20355 }
f3f5162e
DE
20356}
20357
20358/* Decode the Line Number Program (LNP) for the given line_header
20359 structure and CU. The actual information extracted and the type
20360 of structures created from the LNP depends on the value of PST.
20361
20362 1. If PST is NULL, then this procedure uses the data from the program
20363 to create all necessary symbol tables, and their linetables.
20364
20365 2. If PST is not NULL, this procedure reads the program to determine
20366 the list of files included by the unit represented by PST, and
20367 builds all the associated partial symbol tables.
20368
20369 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20370 It is used for relative paths in the line table.
20371 NOTE: When processing partial symtabs (pst != NULL),
20372 comp_dir == pst->dirname.
20373
20374 NOTE: It is important that psymtabs have the same file name (via strcmp)
20375 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20376 symtab we don't use it in the name of the psymtabs we create.
20377 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20378 A good testcase for this is mb-inline.exp.
20379
527f3840
JK
20380 LOWPC is the lowest address in CU (or 0 if not known).
20381
20382 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20383 for its PC<->lines mapping information. Otherwise only the filename
20384 table is read in. */
f3f5162e
DE
20385
20386static void
20387dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20388 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20389 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20390{
518817b3 20391 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20392 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20393
527f3840
JK
20394 if (decode_mapping)
20395 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20396
20397 if (decode_for_pst_p)
20398 {
aaa75496
JB
20399 /* Now that we're done scanning the Line Header Program, we can
20400 create the psymtab of each included file. */
7ba99d21
AT
20401 for (auto &file_entry : lh->file_names ())
20402 if (file_entry.included_p == 1)
aaa75496 20403 {
c89b44cd 20404 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20405 const char *include_name =
7ba99d21
AT
20406 psymtab_include_file_name (lh, file_entry, pst,
20407 comp_dir, &name_holder);
c6da4cef 20408 if (include_name != NULL)
aaa75496
JB
20409 dwarf2_create_include_psymtab (include_name, pst, objfile);
20410 }
20411 }
cb1df416
DJ
20412 else
20413 {
20414 /* Make sure a symtab is created for every file, even files
20415 which contain only variables (i.e. no code with associated
20416 line numbers). */
c24bdb02
KS
20417 buildsym_compunit *builder = cu->get_builder ();
20418 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20419
7ba99d21 20420 for (auto &fe : lh->file_names ())
cb1df416 20421 {
804d2729 20422 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20423 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20424 {
c24bdb02 20425 builder->get_current_subfile ()->symtab
804d2729 20426 = allocate_symtab (cust,
c24bdb02 20427 builder->get_current_subfile ()->name);
43f3e411 20428 }
c24bdb02 20429 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20430 }
20431 }
c906108c
SS
20432}
20433
20434/* Start a subfile for DWARF. FILENAME is the name of the file and
20435 DIRNAME the name of the source directory which contains FILENAME
4d663531 20436 or NULL if not known.
c906108c
SS
20437 This routine tries to keep line numbers from identical absolute and
20438 relative file names in a common subfile.
20439
20440 Using the `list' example from the GDB testsuite, which resides in
20441 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20442 of /srcdir/list0.c yields the following debugging information for list0.c:
20443
c5aa993b 20444 DW_AT_name: /srcdir/list0.c
4d663531 20445 DW_AT_comp_dir: /compdir
357e46e7 20446 files.files[0].name: list0.h
c5aa993b 20447 files.files[0].dir: /srcdir
357e46e7 20448 files.files[1].name: list0.c
c5aa993b 20449 files.files[1].dir: /srcdir
c906108c
SS
20450
20451 The line number information for list0.c has to end up in a single
4f1520fb
FR
20452 subfile, so that `break /srcdir/list0.c:1' works as expected.
20453 start_subfile will ensure that this happens provided that we pass the
20454 concatenation of files.files[1].dir and files.files[1].name as the
20455 subfile's name. */
c906108c
SS
20456
20457static void
804d2729
TT
20458dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20459 const char *dirname)
c906108c 20460{
43816ebc 20461 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20462
4d663531 20463 /* In order not to lose the line information directory,
4f1520fb
FR
20464 we concatenate it to the filename when it makes sense.
20465 Note that the Dwarf3 standard says (speaking of filenames in line
20466 information): ``The directory index is ignored for file names
20467 that represent full path names''. Thus ignoring dirname in the
20468 `else' branch below isn't an issue. */
c906108c 20469
d5166ae1 20470 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20471 {
43816ebc
TT
20472 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20473 filename = copy.get ();
d521ce57 20474 }
c906108c 20475
c24bdb02 20476 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20477}
20478
804d2729
TT
20479/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20480 buildsym_compunit constructor. */
f4dc4d17 20481
c24bdb02
KS
20482struct compunit_symtab *
20483dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20484 CORE_ADDR low_pc)
f4dc4d17 20485{
c24bdb02 20486 gdb_assert (m_builder == nullptr);
43f3e411 20487
c24bdb02
KS
20488 m_builder.reset (new struct buildsym_compunit
20489 (per_cu->dwarf2_per_objfile->objfile,
20490 name, comp_dir, language, low_pc));
93b8bea4 20491
c24bdb02 20492 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20493
c24bdb02
KS
20494 get_builder ()->record_debugformat ("DWARF 2");
20495 get_builder ()->record_producer (producer);
f4dc4d17 20496
c24bdb02 20497 processing_has_namespace_info = false;
43f3e411 20498
c24bdb02 20499 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20500}
20501
4c2df51b
DJ
20502static void
20503var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20504 struct dwarf2_cu *cu)
4c2df51b 20505{
518817b3 20506 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20507 struct comp_unit_head *cu_header = &cu->header;
20508
4c2df51b
DJ
20509 /* NOTE drow/2003-01-30: There used to be a comment and some special
20510 code here to turn a symbol with DW_AT_external and a
20511 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20512 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20513 with some versions of binutils) where shared libraries could have
20514 relocations against symbols in their debug information - the
20515 minimal symbol would have the right address, but the debug info
20516 would not. It's no longer necessary, because we will explicitly
20517 apply relocations when we read in the debug information now. */
20518
20519 /* A DW_AT_location attribute with no contents indicates that a
20520 variable has been optimized away. */
4fc6c0d5 20521 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20522 {
f1e6e072 20523 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20524 return;
20525 }
20526
20527 /* Handle one degenerate form of location expression specially, to
20528 preserve GDB's previous behavior when section offsets are
336d760d
AT
20529 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20530 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20531
4fc6c0d5 20532 if (attr->form_is_block ()
3019eac3
DE
20533 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20534 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20535 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20536 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20537 && (DW_BLOCK (attr)->size
20538 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20539 {
891d2f0b 20540 unsigned int dummy;
4c2df51b 20541
3019eac3 20542 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20543 SET_SYMBOL_VALUE_ADDRESS
20544 (sym, cu->header.read_address (objfile->obfd,
20545 DW_BLOCK (attr)->data + 1,
20546 &dummy));
3019eac3 20547 else
38583298
TT
20548 SET_SYMBOL_VALUE_ADDRESS
20549 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20550 &dummy));
f1e6e072 20551 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20552 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20553 SET_SYMBOL_VALUE_ADDRESS
20554 (sym,
20555 SYMBOL_VALUE_ADDRESS (sym)
20556 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20557 return;
20558 }
20559
20560 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20561 expression evaluator, and use LOC_COMPUTED only when necessary
20562 (i.e. when the value of a register or memory location is
20563 referenced, or a thread-local block, etc.). Then again, it might
20564 not be worthwhile. I'm assuming that it isn't unless performance
20565 or memory numbers show me otherwise. */
20566
f1e6e072 20567 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20568
f1e6e072 20569 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20570 cu->has_loclist = true;
4c2df51b
DJ
20571}
20572
c906108c
SS
20573/* Given a pointer to a DWARF information entry, figure out if we need
20574 to make a symbol table entry for it, and if so, create a new entry
20575 and return a pointer to it.
20576 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20577 used the passed type.
20578 If SPACE is not NULL, use it to hold the new symbol. If it is
20579 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20580
20581static struct symbol *
5e2db402
TT
20582new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20583 struct symbol *space)
c906108c 20584{
518817b3
SM
20585 struct dwarf2_per_objfile *dwarf2_per_objfile
20586 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20587 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20588 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20589 struct symbol *sym = NULL;
15d034d0 20590 const char *name;
c906108c
SS
20591 struct attribute *attr = NULL;
20592 struct attribute *attr2 = NULL;
e142c38c 20593 CORE_ADDR baseaddr;
e37fd15a
SW
20594 struct pending **list_to_add = NULL;
20595
edb3359d 20596 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20597
b3b3bada 20598 baseaddr = objfile->text_section_offset ();
c906108c 20599
94af9270 20600 name = dwarf2_name (die, cu);
c906108c
SS
20601 if (name)
20602 {
34eaf542 20603 int suppress_add = 0;
94af9270 20604
34eaf542
TT
20605 if (space)
20606 sym = space;
20607 else
8c14c3a3 20608 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20609 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20610
20611 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20612 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20613 /* Fortran does not have mangling standard and the mangling does differ
20614 between gfortran, iFort etc. */
bcfe6157
TT
20615 const char *physname
20616 = (cu->language == language_fortran
20617 ? dwarf2_full_name (name, die, cu)
20618 : dwarf2_physname (name, die, cu));
20619 const char *linkagename = dw2_linkage_name (die, cu);
20620
20621 if (linkagename == nullptr || cu->language == language_ada)
20622 sym->set_linkage_name (physname);
20623 else
20624 {
20625 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20626 sym->set_linkage_name (linkagename);
20627 }
f55ee35c 20628
c906108c 20629 /* Default assumptions.
c5aa993b 20630 Use the passed type or decode it from the die. */
176620f1 20631 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20632 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20633 if (type != NULL)
20634 SYMBOL_TYPE (sym) = type;
20635 else
e7c27a73 20636 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20637 attr = dwarf2_attr (die,
20638 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20639 cu);
435d3d88 20640 if (attr != nullptr)
c906108c
SS
20641 {
20642 SYMBOL_LINE (sym) = DW_UNSND (attr);
20643 }
cb1df416 20644
edb3359d
DJ
20645 attr = dwarf2_attr (die,
20646 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20647 cu);
435d3d88 20648 if (attr != nullptr)
cb1df416 20649 {
ecfb656c 20650 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20651 struct file_entry *fe;
9a619af0 20652
ecfb656c
PA
20653 if (cu->line_header != NULL)
20654 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20655 else
20656 fe = NULL;
20657
20658 if (fe == NULL)
b98664d3 20659 complaint (_("file index out of range"));
8c43009f
PA
20660 else
20661 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20662 }
20663
c906108c
SS
20664 switch (die->tag)
20665 {
20666 case DW_TAG_label:
e142c38c 20667 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20668 if (attr != nullptr)
3e29f34a
MR
20669 {
20670 CORE_ADDR addr;
20671
cd6c91b4 20672 addr = attr->value_as_address ();
3e29f34a 20673 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20674 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20675 }
0f5238ed
TT
20676 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20677 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20678 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20679 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20680 break;
20681 case DW_TAG_subprogram:
20682 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20683 finish_block. */
f1e6e072 20684 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20685 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20686 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20687 || cu->language == language_ada
20688 || cu->language == language_fortran)
c906108c 20689 {
2cfa0c8d 20690 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20691 Ada and Fortran subprograms, whether marked external or
20692 not, are always stored as a global symbol, because we want
20693 to be able to access them globally. For instance, we want
20694 to be able to break on a nested subprogram without having
20695 to specify the context. */
c24bdb02 20696 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20697 }
20698 else
20699 {
e37fd15a 20700 list_to_add = cu->list_in_scope;
c906108c
SS
20701 }
20702 break;
edb3359d
DJ
20703 case DW_TAG_inlined_subroutine:
20704 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20705 finish_block. */
f1e6e072 20706 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20707 SYMBOL_INLINED (sym) = 1;
481860b3 20708 list_to_add = cu->list_in_scope;
edb3359d 20709 break;
34eaf542
TT
20710 case DW_TAG_template_value_param:
20711 suppress_add = 1;
20712 /* Fall through. */
72929c62 20713 case DW_TAG_constant:
c906108c 20714 case DW_TAG_variable:
254e6b9e 20715 case DW_TAG_member:
0963b4bd
MS
20716 /* Compilation with minimal debug info may result in
20717 variables with missing type entries. Change the
20718 misleading `void' type to something sensible. */
78134374 20719 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20720 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20721
e142c38c 20722 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20723 /* In the case of DW_TAG_member, we should only be called for
20724 static const members. */
20725 if (die->tag == DW_TAG_member)
20726 {
3863f96c
DE
20727 /* dwarf2_add_field uses die_is_declaration,
20728 so we do the same. */
254e6b9e
DE
20729 gdb_assert (die_is_declaration (die, cu));
20730 gdb_assert (attr);
20731 }
435d3d88 20732 if (attr != nullptr)
c906108c 20733 {
e7c27a73 20734 dwarf2_const_value (attr, sym, cu);
e142c38c 20735 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20736 if (!suppress_add)
34eaf542
TT
20737 {
20738 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20739 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20740 else
e37fd15a 20741 list_to_add = cu->list_in_scope;
34eaf542 20742 }
c906108c
SS
20743 break;
20744 }
e142c38c 20745 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20746 if (attr != nullptr)
c906108c 20747 {
e7c27a73 20748 var_decode_location (attr, sym, cu);
e142c38c 20749 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20750
20751 /* Fortran explicitly imports any global symbols to the local
20752 scope by DW_TAG_common_block. */
20753 if (cu->language == language_fortran && die->parent
20754 && die->parent->tag == DW_TAG_common_block)
20755 attr2 = NULL;
20756
caac4577
JG
20757 if (SYMBOL_CLASS (sym) == LOC_STATIC
20758 && SYMBOL_VALUE_ADDRESS (sym) == 0
20759 && !dwarf2_per_objfile->has_section_at_zero)
20760 {
20761 /* When a static variable is eliminated by the linker,
20762 the corresponding debug information is not stripped
20763 out, but the variable address is set to null;
20764 do not add such variables into symbol table. */
20765 }
20766 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20767 {
4b610737
TT
20768 if (SYMBOL_CLASS (sym) == LOC_STATIC
20769 && (objfile->flags & OBJF_MAINLINE) == 0
20770 && dwarf2_per_objfile->can_copy)
20771 {
20772 /* A global static variable might be subject to
20773 copy relocation. We first check for a local
20774 minsym, though, because maybe the symbol was
20775 marked hidden, in which case this would not
20776 apply. */
20777 bound_minimal_symbol found
20778 = (lookup_minimal_symbol_linkage
987012b8 20779 (sym->linkage_name (), objfile));
4b610737
TT
20780 if (found.minsym != nullptr)
20781 sym->maybe_copied = 1;
20782 }
f55ee35c 20783
1c809c68
TT
20784 /* A variable with DW_AT_external is never static,
20785 but it may be block-scoped. */
804d2729 20786 list_to_add
c24bdb02
KS
20787 = ((cu->list_in_scope
20788 == cu->get_builder ()->get_file_symbols ())
20789 ? cu->get_builder ()->get_global_symbols ()
804d2729 20790 : cu->list_in_scope);
1c809c68 20791 }
c906108c 20792 else
e37fd15a 20793 list_to_add = cu->list_in_scope;
c906108c
SS
20794 }
20795 else
20796 {
20797 /* We do not know the address of this symbol.
c5aa993b
JM
20798 If it is an external symbol and we have type information
20799 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20800 The address of the variable will then be determined from
20801 the minimal symbol table whenever the variable is
20802 referenced. */
e142c38c 20803 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20804
20805 /* Fortran explicitly imports any global symbols to the local
20806 scope by DW_TAG_common_block. */
20807 if (cu->language == language_fortran && die->parent
20808 && die->parent->tag == DW_TAG_common_block)
20809 {
20810 /* SYMBOL_CLASS doesn't matter here because
20811 read_common_block is going to reset it. */
20812 if (!suppress_add)
20813 list_to_add = cu->list_in_scope;
20814 }
20815 else if (attr2 && (DW_UNSND (attr2) != 0)
20816 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20817 {
0fe7935b
DJ
20818 /* A variable with DW_AT_external is never static, but it
20819 may be block-scoped. */
804d2729 20820 list_to_add
c24bdb02
KS
20821 = ((cu->list_in_scope
20822 == cu->get_builder ()->get_file_symbols ())
20823 ? cu->get_builder ()->get_global_symbols ()
804d2729 20824 : cu->list_in_scope);
0fe7935b 20825
f1e6e072 20826 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20827 }
442ddf59
JK
20828 else if (!die_is_declaration (die, cu))
20829 {
20830 /* Use the default LOC_OPTIMIZED_OUT class. */
20831 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20832 if (!suppress_add)
20833 list_to_add = cu->list_in_scope;
442ddf59 20834 }
c906108c
SS
20835 }
20836 break;
20837 case DW_TAG_formal_parameter:
a60f3166
TT
20838 {
20839 /* If we are inside a function, mark this as an argument. If
20840 not, we might be looking at an argument to an inlined function
20841 when we do not have enough information to show inlined frames;
20842 pretend it's a local variable in that case so that the user can
20843 still see it. */
804d2729 20844 struct context_stack *curr
c24bdb02 20845 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20846 if (curr != nullptr && curr->name != nullptr)
20847 SYMBOL_IS_ARGUMENT (sym) = 1;
20848 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20849 if (attr != nullptr)
a60f3166
TT
20850 {
20851 var_decode_location (attr, sym, cu);
20852 }
20853 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20854 if (attr != nullptr)
a60f3166
TT
20855 {
20856 dwarf2_const_value (attr, sym, cu);
20857 }
f346a30d 20858
a60f3166
TT
20859 list_to_add = cu->list_in_scope;
20860 }
c906108c
SS
20861 break;
20862 case DW_TAG_unspecified_parameters:
20863 /* From varargs functions; gdb doesn't seem to have any
20864 interest in this information, so just ignore it for now.
20865 (FIXME?) */
20866 break;
34eaf542
TT
20867 case DW_TAG_template_type_param:
20868 suppress_add = 1;
20869 /* Fall through. */
c906108c 20870 case DW_TAG_class_type:
680b30c7 20871 case DW_TAG_interface_type:
c906108c
SS
20872 case DW_TAG_structure_type:
20873 case DW_TAG_union_type:
72019c9c 20874 case DW_TAG_set_type:
c906108c 20875 case DW_TAG_enumeration_type:
f1e6e072 20876 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20877 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20878
63d06c5c 20879 {
9c37b5ae 20880 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20881 really ever be static objects: otherwise, if you try
20882 to, say, break of a class's method and you're in a file
20883 which doesn't mention that class, it won't work unless
20884 the check for all static symbols in lookup_symbol_aux
20885 saves you. See the OtherFileClass tests in
20886 gdb.c++/namespace.exp. */
20887
e37fd15a 20888 if (!suppress_add)
34eaf542 20889 {
c24bdb02 20890 buildsym_compunit *builder = cu->get_builder ();
804d2729 20891 list_to_add
c24bdb02 20892 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20893 && cu->language == language_cplus
c24bdb02 20894 ? builder->get_global_symbols ()
804d2729 20895 : cu->list_in_scope);
63d06c5c 20896
64382290 20897 /* The semantics of C++ state that "struct foo {
9c37b5ae 20898 ... }" also defines a typedef for "foo". */
64382290 20899 if (cu->language == language_cplus
45280282 20900 || cu->language == language_ada
c44af4eb
TT
20901 || cu->language == language_d
20902 || cu->language == language_rust)
64382290
TT
20903 {
20904 /* The symbol's name is already allocated along
20905 with this objfile, so we don't need to
20906 duplicate it for the type. */
7d93a1e0 20907 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 20908 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 20909 }
63d06c5c
DC
20910 }
20911 }
c906108c
SS
20912 break;
20913 case DW_TAG_typedef:
f1e6e072 20914 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20915 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20916 list_to_add = cu->list_in_scope;
63d06c5c 20917 break;
c906108c 20918 case DW_TAG_base_type:
a02abb62 20919 case DW_TAG_subrange_type:
f1e6e072 20920 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20921 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20922 list_to_add = cu->list_in_scope;
c906108c
SS
20923 break;
20924 case DW_TAG_enumerator:
e142c38c 20925 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20926 if (attr != nullptr)
c906108c 20927 {
e7c27a73 20928 dwarf2_const_value (attr, sym, cu);
c906108c 20929 }
63d06c5c
DC
20930 {
20931 /* NOTE: carlton/2003-11-10: See comment above in the
20932 DW_TAG_class_type, etc. block. */
20933
804d2729 20934 list_to_add
c24bdb02 20935 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20936 && cu->language == language_cplus
c24bdb02 20937 ? cu->get_builder ()->get_global_symbols ()
804d2729 20938 : cu->list_in_scope);
63d06c5c 20939 }
c906108c 20940 break;
74921315 20941 case DW_TAG_imported_declaration:
5c4e30ca 20942 case DW_TAG_namespace:
f1e6e072 20943 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20944 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20945 break;
530e8392
KB
20946 case DW_TAG_module:
20947 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20948 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20949 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20950 break;
4357ac6c 20951 case DW_TAG_common_block:
f1e6e072 20952 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20953 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20954 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20955 break;
c906108c
SS
20956 default:
20957 /* Not a tag we recognize. Hopefully we aren't processing
20958 trash data, but since we must specifically ignore things
20959 we don't recognize, there is nothing else we should do at
0963b4bd 20960 this point. */
b98664d3 20961 complaint (_("unsupported tag: '%s'"),
4d3c2250 20962 dwarf_tag_name (die->tag));
c906108c
SS
20963 break;
20964 }
df8a16a1 20965
e37fd15a
SW
20966 if (suppress_add)
20967 {
20968 sym->hash_next = objfile->template_symbols;
20969 objfile->template_symbols = sym;
20970 list_to_add = NULL;
20971 }
20972
20973 if (list_to_add != NULL)
d3cb6808 20974 add_symbol_to_list (sym, list_to_add);
e37fd15a 20975
df8a16a1
DJ
20976 /* For the benefit of old versions of GCC, check for anonymous
20977 namespaces based on the demangled name. */
4d4ec4e5 20978 if (!cu->processing_has_namespace_info
94af9270 20979 && cu->language == language_cplus)
c24bdb02 20980 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20981 }
20982 return (sym);
20983}
20984
98bfdba5
PA
20985/* Given an attr with a DW_FORM_dataN value in host byte order,
20986 zero-extend it as appropriate for the symbol's type. The DWARF
20987 standard (v4) is not entirely clear about the meaning of using
20988 DW_FORM_dataN for a constant with a signed type, where the type is
20989 wider than the data. The conclusion of a discussion on the DWARF
20990 list was that this is unspecified. We choose to always zero-extend
20991 because that is the interpretation long in use by GCC. */
c906108c 20992
98bfdba5 20993static gdb_byte *
ff39bb5e 20994dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20995 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20996{
518817b3 20997 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20998 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20999 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21000 LONGEST l = DW_UNSND (attr);
21001
21002 if (bits < sizeof (*value) * 8)
21003 {
21004 l &= ((LONGEST) 1 << bits) - 1;
21005 *value = l;
21006 }
21007 else if (bits == sizeof (*value) * 8)
21008 *value = l;
21009 else
21010 {
224c3ddb 21011 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21012 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21013 return bytes;
21014 }
21015
21016 return NULL;
21017}
21018
21019/* Read a constant value from an attribute. Either set *VALUE, or if
21020 the value does not fit in *VALUE, set *BYTES - either already
21021 allocated on the objfile obstack, or newly allocated on OBSTACK,
21022 or, set *BATON, if we translated the constant to a location
21023 expression. */
21024
21025static void
ff39bb5e 21026dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21027 const char *name, struct obstack *obstack,
21028 struct dwarf2_cu *cu,
d521ce57 21029 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21030 struct dwarf2_locexpr_baton **baton)
21031{
518817b3 21032 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21033 struct comp_unit_head *cu_header = &cu->header;
c906108c 21034 struct dwarf_block *blk;
98bfdba5
PA
21035 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21036 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21037
21038 *value = 0;
21039 *bytes = NULL;
21040 *baton = NULL;
c906108c
SS
21041
21042 switch (attr->form)
21043 {
21044 case DW_FORM_addr:
336d760d 21045 case DW_FORM_addrx:
3019eac3 21046 case DW_FORM_GNU_addr_index:
ac56253d 21047 {
ac56253d
TT
21048 gdb_byte *data;
21049
98bfdba5
PA
21050 if (TYPE_LENGTH (type) != cu_header->addr_size)
21051 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21052 cu_header->addr_size,
98bfdba5 21053 TYPE_LENGTH (type));
ac56253d
TT
21054 /* Symbols of this form are reasonably rare, so we just
21055 piggyback on the existing location code rather than writing
21056 a new implementation of symbol_computed_ops. */
8d749320 21057 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21058 (*baton)->per_cu = cu->per_cu;
21059 gdb_assert ((*baton)->per_cu);
ac56253d 21060
98bfdba5 21061 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21062 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21063 (*baton)->data = data;
ac56253d
TT
21064
21065 data[0] = DW_OP_addr;
21066 store_unsigned_integer (&data[1], cu_header->addr_size,
21067 byte_order, DW_ADDR (attr));
21068 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21069 }
c906108c 21070 break;
4ac36638 21071 case DW_FORM_string:
93b5768b 21072 case DW_FORM_strp:
cf532bd1 21073 case DW_FORM_strx:
3019eac3 21074 case DW_FORM_GNU_str_index:
36586728 21075 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21076 /* DW_STRING is already allocated on the objfile obstack, point
21077 directly to it. */
d521ce57 21078 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21079 break;
c906108c
SS
21080 case DW_FORM_block1:
21081 case DW_FORM_block2:
21082 case DW_FORM_block4:
21083 case DW_FORM_block:
2dc7f7b3 21084 case DW_FORM_exprloc:
0224619f 21085 case DW_FORM_data16:
c906108c 21086 blk = DW_BLOCK (attr);
98bfdba5
PA
21087 if (TYPE_LENGTH (type) != blk->size)
21088 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21089 TYPE_LENGTH (type));
21090 *bytes = blk->data;
c906108c 21091 break;
2df3850c
JM
21092
21093 /* The DW_AT_const_value attributes are supposed to carry the
21094 symbol's value "represented as it would be on the target
21095 architecture." By the time we get here, it's already been
21096 converted to host endianness, so we just need to sign- or
21097 zero-extend it as appropriate. */
21098 case DW_FORM_data1:
3aef2284 21099 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21100 break;
c906108c 21101 case DW_FORM_data2:
3aef2284 21102 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21103 break;
c906108c 21104 case DW_FORM_data4:
3aef2284 21105 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21106 break;
c906108c 21107 case DW_FORM_data8:
3aef2284 21108 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21109 break;
21110
c906108c 21111 case DW_FORM_sdata:
663c44ac 21112 case DW_FORM_implicit_const:
98bfdba5 21113 *value = DW_SND (attr);
2df3850c
JM
21114 break;
21115
c906108c 21116 case DW_FORM_udata:
98bfdba5 21117 *value = DW_UNSND (attr);
c906108c 21118 break;
2df3850c 21119
c906108c 21120 default:
b98664d3 21121 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21122 dwarf_form_name (attr->form));
98bfdba5 21123 *value = 0;
c906108c
SS
21124 break;
21125 }
21126}
21127
2df3850c 21128
98bfdba5
PA
21129/* Copy constant value from an attribute to a symbol. */
21130
2df3850c 21131static void
ff39bb5e 21132dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21133 struct dwarf2_cu *cu)
2df3850c 21134{
518817b3 21135 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21136 LONGEST value;
d521ce57 21137 const gdb_byte *bytes;
98bfdba5 21138 struct dwarf2_locexpr_baton *baton;
2df3850c 21139
98bfdba5 21140 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21141 sym->print_name (),
98bfdba5
PA
21142 &objfile->objfile_obstack, cu,
21143 &value, &bytes, &baton);
2df3850c 21144
98bfdba5
PA
21145 if (baton != NULL)
21146 {
98bfdba5 21147 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21148 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21149 }
21150 else if (bytes != NULL)
21151 {
21152 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21153 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21154 }
21155 else
21156 {
21157 SYMBOL_VALUE (sym) = value;
f1e6e072 21158 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21159 }
2df3850c
JM
21160}
21161
c906108c
SS
21162/* Return the type of the die in question using its DW_AT_type attribute. */
21163
21164static struct type *
e7c27a73 21165die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21166{
c906108c 21167 struct attribute *type_attr;
c906108c 21168
e142c38c 21169 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21170 if (!type_attr)
21171 {
518817b3 21172 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21173 /* A missing DW_AT_type represents a void type. */
518817b3 21174 return objfile_type (objfile)->builtin_void;
c906108c 21175 }
348e048f 21176
673bfd45 21177 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21178}
21179
b4ba55a1
JB
21180/* True iff CU's producer generates GNAT Ada auxiliary information
21181 that allows to find parallel types through that information instead
21182 of having to do expensive parallel lookups by type name. */
21183
21184static int
21185need_gnat_info (struct dwarf2_cu *cu)
21186{
de4cb04a
JB
21187 /* Assume that the Ada compiler was GNAT, which always produces
21188 the auxiliary information. */
21189 return (cu->language == language_ada);
b4ba55a1
JB
21190}
21191
b4ba55a1
JB
21192/* Return the auxiliary type of the die in question using its
21193 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21194 attribute is not present. */
21195
21196static struct type *
21197die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21198{
b4ba55a1 21199 struct attribute *type_attr;
b4ba55a1
JB
21200
21201 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21202 if (!type_attr)
21203 return NULL;
21204
673bfd45 21205 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21206}
21207
21208/* If DIE has a descriptive_type attribute, then set the TYPE's
21209 descriptive type accordingly. */
21210
21211static void
21212set_descriptive_type (struct type *type, struct die_info *die,
21213 struct dwarf2_cu *cu)
21214{
21215 struct type *descriptive_type = die_descriptive_type (die, cu);
21216
21217 if (descriptive_type)
21218 {
21219 ALLOCATE_GNAT_AUX_TYPE (type);
21220 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21221 }
21222}
21223
c906108c
SS
21224/* Return the containing type of the die in question using its
21225 DW_AT_containing_type attribute. */
21226
21227static struct type *
e7c27a73 21228die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21229{
c906108c 21230 struct attribute *type_attr;
518817b3 21231 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21232
e142c38c 21233 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21234 if (!type_attr)
21235 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21236 "[in module %s]"), objfile_name (objfile));
33ac96f0 21237
673bfd45 21238 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21239}
21240
ac9ec31b
DE
21241/* Return an error marker type to use for the ill formed type in DIE/CU. */
21242
21243static struct type *
21244build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21245{
518817b3
SM
21246 struct dwarf2_per_objfile *dwarf2_per_objfile
21247 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21248 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21249 char *saved;
ac9ec31b 21250
528e1572
SM
21251 std::string message
21252 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21253 objfile_name (objfile),
21254 sect_offset_str (cu->header.sect_off),
21255 sect_offset_str (die->sect_off));
efba19b0 21256 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21257
19f392bc 21258 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21259}
21260
673bfd45 21261/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21262 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21263 DW_AT_containing_type.
673bfd45
DE
21264 If there is no type substitute an error marker. */
21265
c906108c 21266static struct type *
ff39bb5e 21267lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21268 struct dwarf2_cu *cu)
c906108c 21269{
518817b3
SM
21270 struct dwarf2_per_objfile *dwarf2_per_objfile
21271 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21272 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21273 struct type *this_type;
21274
ac9ec31b
DE
21275 gdb_assert (attr->name == DW_AT_type
21276 || attr->name == DW_AT_GNAT_descriptive_type
21277 || attr->name == DW_AT_containing_type);
21278
673bfd45
DE
21279 /* First see if we have it cached. */
21280
36586728
TT
21281 if (attr->form == DW_FORM_GNU_ref_alt)
21282 {
21283 struct dwarf2_per_cu_data *per_cu;
0826b30a 21284 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21285
ed2dc618
SM
21286 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21287 dwarf2_per_objfile);
9c541725 21288 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21289 }
cd6c91b4 21290 else if (attr->form_is_ref ())
673bfd45 21291 {
0826b30a 21292 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21293
9c541725 21294 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21295 }
55f1336d 21296 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21297 {
ac9ec31b 21298 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21299
ac9ec31b 21300 return get_signatured_type (die, signature, cu);
673bfd45
DE
21301 }
21302 else
21303 {
b98664d3 21304 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21305 " at %s [in module %s]"),
21306 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21307 objfile_name (objfile));
ac9ec31b 21308 return build_error_marker_type (cu, die);
673bfd45
DE
21309 }
21310
21311 /* If not cached we need to read it in. */
21312
21313 if (this_type == NULL)
21314 {
ac9ec31b 21315 struct die_info *type_die = NULL;
673bfd45
DE
21316 struct dwarf2_cu *type_cu = cu;
21317
cd6c91b4 21318 if (attr->form_is_ref ())
ac9ec31b
DE
21319 type_die = follow_die_ref (die, attr, &type_cu);
21320 if (type_die == NULL)
21321 return build_error_marker_type (cu, die);
21322 /* If we find the type now, it's probably because the type came
3019eac3
DE
21323 from an inter-CU reference and the type's CU got expanded before
21324 ours. */
ac9ec31b 21325 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21326 }
21327
21328 /* If we still don't have a type use an error marker. */
21329
21330 if (this_type == NULL)
ac9ec31b 21331 return build_error_marker_type (cu, die);
673bfd45 21332
f792889a 21333 return this_type;
c906108c
SS
21334}
21335
673bfd45
DE
21336/* Return the type in DIE, CU.
21337 Returns NULL for invalid types.
21338
02142a6c 21339 This first does a lookup in die_type_hash,
673bfd45
DE
21340 and only reads the die in if necessary.
21341
21342 NOTE: This can be called when reading in partial or full symbols. */
21343
f792889a 21344static struct type *
e7c27a73 21345read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21346{
f792889a
DJ
21347 struct type *this_type;
21348
21349 this_type = get_die_type (die, cu);
21350 if (this_type)
21351 return this_type;
21352
673bfd45
DE
21353 return read_type_die_1 (die, cu);
21354}
21355
21356/* Read the type in DIE, CU.
21357 Returns NULL for invalid types. */
21358
21359static struct type *
21360read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21361{
21362 struct type *this_type = NULL;
21363
c906108c
SS
21364 switch (die->tag)
21365 {
21366 case DW_TAG_class_type:
680b30c7 21367 case DW_TAG_interface_type:
c906108c
SS
21368 case DW_TAG_structure_type:
21369 case DW_TAG_union_type:
f792889a 21370 this_type = read_structure_type (die, cu);
c906108c
SS
21371 break;
21372 case DW_TAG_enumeration_type:
f792889a 21373 this_type = read_enumeration_type (die, cu);
c906108c
SS
21374 break;
21375 case DW_TAG_subprogram:
21376 case DW_TAG_subroutine_type:
edb3359d 21377 case DW_TAG_inlined_subroutine:
f792889a 21378 this_type = read_subroutine_type (die, cu);
c906108c
SS
21379 break;
21380 case DW_TAG_array_type:
f792889a 21381 this_type = read_array_type (die, cu);
c906108c 21382 break;
72019c9c 21383 case DW_TAG_set_type:
f792889a 21384 this_type = read_set_type (die, cu);
72019c9c 21385 break;
c906108c 21386 case DW_TAG_pointer_type:
f792889a 21387 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21388 break;
21389 case DW_TAG_ptr_to_member_type:
f792889a 21390 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21391 break;
21392 case DW_TAG_reference_type:
4297a3f0
AV
21393 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21394 break;
21395 case DW_TAG_rvalue_reference_type:
21396 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21397 break;
21398 case DW_TAG_const_type:
f792889a 21399 this_type = read_tag_const_type (die, cu);
c906108c
SS
21400 break;
21401 case DW_TAG_volatile_type:
f792889a 21402 this_type = read_tag_volatile_type (die, cu);
c906108c 21403 break;
06d66ee9
TT
21404 case DW_TAG_restrict_type:
21405 this_type = read_tag_restrict_type (die, cu);
21406 break;
c906108c 21407 case DW_TAG_string_type:
f792889a 21408 this_type = read_tag_string_type (die, cu);
c906108c
SS
21409 break;
21410 case DW_TAG_typedef:
f792889a 21411 this_type = read_typedef (die, cu);
c906108c 21412 break;
a02abb62 21413 case DW_TAG_subrange_type:
f792889a 21414 this_type = read_subrange_type (die, cu);
a02abb62 21415 break;
c906108c 21416 case DW_TAG_base_type:
f792889a 21417 this_type = read_base_type (die, cu);
c906108c 21418 break;
81a17f79 21419 case DW_TAG_unspecified_type:
f792889a 21420 this_type = read_unspecified_type (die, cu);
81a17f79 21421 break;
0114d602
DJ
21422 case DW_TAG_namespace:
21423 this_type = read_namespace_type (die, cu);
21424 break;
f55ee35c
JK
21425 case DW_TAG_module:
21426 this_type = read_module_type (die, cu);
21427 break;
a2c2acaf
MW
21428 case DW_TAG_atomic_type:
21429 this_type = read_tag_atomic_type (die, cu);
21430 break;
c906108c 21431 default:
b98664d3 21432 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21433 dwarf_tag_name (die->tag));
c906108c
SS
21434 break;
21435 }
63d06c5c 21436
f792889a 21437 return this_type;
63d06c5c
DC
21438}
21439
abc72ce4
DE
21440/* See if we can figure out if the class lives in a namespace. We do
21441 this by looking for a member function; its demangled name will
21442 contain namespace info, if there is any.
21443 Return the computed name or NULL.
21444 Space for the result is allocated on the objfile's obstack.
21445 This is the full-die version of guess_partial_die_structure_name.
21446 In this case we know DIE has no useful parent. */
21447
43816ebc 21448static const char *
abc72ce4
DE
21449guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21450{
21451 struct die_info *spec_die;
21452 struct dwarf2_cu *spec_cu;
21453 struct die_info *child;
518817b3 21454 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21455
21456 spec_cu = cu;
21457 spec_die = die_specification (die, &spec_cu);
21458 if (spec_die != NULL)
21459 {
21460 die = spec_die;
21461 cu = spec_cu;
21462 }
21463
21464 for (child = die->child;
21465 child != NULL;
21466 child = child->sibling)
21467 {
21468 if (child->tag == DW_TAG_subprogram)
21469 {
73b9be8b 21470 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21471
7d45c7c3 21472 if (linkage_name != NULL)
abc72ce4 21473 {
43816ebc
TT
21474 gdb::unique_xmalloc_ptr<char> actual_name
21475 (language_class_name_from_physname (cu->language_defn,
21476 linkage_name));
21477 const char *name = NULL;
abc72ce4
DE
21478
21479 if (actual_name != NULL)
21480 {
15d034d0 21481 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21482
21483 if (die_name != NULL
43816ebc 21484 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21485 {
21486 /* Strip off the class name from the full name.
21487 We want the prefix. */
21488 int die_name_len = strlen (die_name);
43816ebc
TT
21489 int actual_name_len = strlen (actual_name.get ());
21490 const char *ptr = actual_name.get ();
abc72ce4
DE
21491
21492 /* Test for '::' as a sanity check. */
21493 if (actual_name_len > die_name_len + 2
43816ebc 21494 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21495 name = obstack_strndup (
e3b94546 21496 &objfile->per_bfd->storage_obstack,
43816ebc 21497 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21498 }
21499 }
abc72ce4
DE
21500 return name;
21501 }
21502 }
21503 }
21504
21505 return NULL;
21506}
21507
96408a79
SA
21508/* GCC might emit a nameless typedef that has a linkage name. Determine the
21509 prefix part in such case. See
21510 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21511
a121b7c1 21512static const char *
96408a79
SA
21513anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21514{
21515 struct attribute *attr;
e6a959d6 21516 const char *base;
96408a79
SA
21517
21518 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21519 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21520 return NULL;
21521
7d45c7c3 21522 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21523 return NULL;
21524
73b9be8b 21525 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21526 if (attr == NULL || DW_STRING (attr) == NULL)
21527 return NULL;
21528
21529 /* dwarf2_name had to be already called. */
21530 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21531
21532 /* Strip the base name, keep any leading namespaces/classes. */
21533 base = strrchr (DW_STRING (attr), ':');
21534 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21535 return "";
21536
518817b3 21537 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21538 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21539 DW_STRING (attr),
21540 &base[-1] - DW_STRING (attr));
96408a79
SA
21541}
21542
fdde2d81 21543/* Return the name of the namespace/class that DIE is defined within,
0114d602 21544 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21545
0114d602
DJ
21546 For example, if we're within the method foo() in the following
21547 code:
21548
21549 namespace N {
21550 class C {
21551 void foo () {
21552 }
21553 };
21554 }
21555
21556 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21557
0d5cff50 21558static const char *
e142c38c 21559determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21560{
518817b3
SM
21561 struct dwarf2_per_objfile *dwarf2_per_objfile
21562 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21563 struct die_info *parent, *spec_die;
21564 struct dwarf2_cu *spec_cu;
21565 struct type *parent_type;
a121b7c1 21566 const char *retval;
63d06c5c 21567
9c37b5ae 21568 if (cu->language != language_cplus
c44af4eb
TT
21569 && cu->language != language_fortran && cu->language != language_d
21570 && cu->language != language_rust)
0114d602
DJ
21571 return "";
21572
96408a79
SA
21573 retval = anonymous_struct_prefix (die, cu);
21574 if (retval)
21575 return retval;
21576
0114d602
DJ
21577 /* We have to be careful in the presence of DW_AT_specification.
21578 For example, with GCC 3.4, given the code
21579
21580 namespace N {
21581 void foo() {
21582 // Definition of N::foo.
21583 }
21584 }
21585
21586 then we'll have a tree of DIEs like this:
21587
21588 1: DW_TAG_compile_unit
21589 2: DW_TAG_namespace // N
21590 3: DW_TAG_subprogram // declaration of N::foo
21591 4: DW_TAG_subprogram // definition of N::foo
21592 DW_AT_specification // refers to die #3
21593
21594 Thus, when processing die #4, we have to pretend that we're in
21595 the context of its DW_AT_specification, namely the contex of die
21596 #3. */
21597 spec_cu = cu;
21598 spec_die = die_specification (die, &spec_cu);
21599 if (spec_die == NULL)
21600 parent = die->parent;
21601 else
63d06c5c 21602 {
0114d602
DJ
21603 parent = spec_die->parent;
21604 cu = spec_cu;
63d06c5c 21605 }
0114d602
DJ
21606
21607 if (parent == NULL)
21608 return "";
98bfdba5
PA
21609 else if (parent->building_fullname)
21610 {
21611 const char *name;
21612 const char *parent_name;
21613
21614 /* It has been seen on RealView 2.2 built binaries,
21615 DW_TAG_template_type_param types actually _defined_ as
21616 children of the parent class:
21617
21618 enum E {};
21619 template class <class Enum> Class{};
21620 Class<enum E> class_e;
21621
21622 1: DW_TAG_class_type (Class)
21623 2: DW_TAG_enumeration_type (E)
21624 3: DW_TAG_enumerator (enum1:0)
21625 3: DW_TAG_enumerator (enum2:1)
21626 ...
21627 2: DW_TAG_template_type_param
21628 DW_AT_type DW_FORM_ref_udata (E)
21629
21630 Besides being broken debug info, it can put GDB into an
21631 infinite loop. Consider:
21632
21633 When we're building the full name for Class<E>, we'll start
21634 at Class, and go look over its template type parameters,
21635 finding E. We'll then try to build the full name of E, and
21636 reach here. We're now trying to build the full name of E,
21637 and look over the parent DIE for containing scope. In the
21638 broken case, if we followed the parent DIE of E, we'd again
21639 find Class, and once again go look at its template type
21640 arguments, etc., etc. Simply don't consider such parent die
21641 as source-level parent of this die (it can't be, the language
21642 doesn't allow it), and break the loop here. */
21643 name = dwarf2_name (die, cu);
21644 parent_name = dwarf2_name (parent, cu);
b98664d3 21645 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21646 name ? name : "<unknown>",
21647 parent_name ? parent_name : "<unknown>");
21648 return "";
21649 }
63d06c5c 21650 else
0114d602
DJ
21651 switch (parent->tag)
21652 {
63d06c5c 21653 case DW_TAG_namespace:
0114d602 21654 parent_type = read_type_die (parent, cu);
acebe513
UW
21655 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21656 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21657 Work around this problem here. */
21658 if (cu->language == language_cplus
7d93a1e0 21659 && strcmp (parent_type->name (), "::") == 0)
acebe513 21660 return "";
0114d602 21661 /* We give a name to even anonymous namespaces. */
7d93a1e0 21662 return parent_type->name ();
63d06c5c 21663 case DW_TAG_class_type:
680b30c7 21664 case DW_TAG_interface_type:
63d06c5c 21665 case DW_TAG_structure_type:
0114d602 21666 case DW_TAG_union_type:
f55ee35c 21667 case DW_TAG_module:
0114d602 21668 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21669 if (parent_type->name () != NULL)
21670 return parent_type->name ();
0114d602
DJ
21671 else
21672 /* An anonymous structure is only allowed non-static data
21673 members; no typedefs, no member functions, et cetera.
21674 So it does not need a prefix. */
21675 return "";
abc72ce4 21676 case DW_TAG_compile_unit:
95554aad 21677 case DW_TAG_partial_unit:
abc72ce4
DE
21678 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21679 if (cu->language == language_cplus
fd5866f6 21680 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21681 && die->child != NULL
21682 && (die->tag == DW_TAG_class_type
21683 || die->tag == DW_TAG_structure_type
21684 || die->tag == DW_TAG_union_type))
21685 {
43816ebc 21686 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21687 if (name != NULL)
21688 return name;
21689 }
21690 return "";
0a4b0913
AB
21691 case DW_TAG_subprogram:
21692 /* Nested subroutines in Fortran get a prefix with the name
21693 of the parent's subroutine. */
21694 if (cu->language == language_fortran)
21695 {
21696 if ((die->tag == DW_TAG_subprogram)
21697 && (dwarf2_name (parent, cu) != NULL))
21698 return dwarf2_name (parent, cu);
21699 }
21700 return determine_prefix (parent, cu);
3d567982
TT
21701 case DW_TAG_enumeration_type:
21702 parent_type = read_type_die (parent, cu);
21703 if (TYPE_DECLARED_CLASS (parent_type))
21704 {
7d93a1e0
SM
21705 if (parent_type->name () != NULL)
21706 return parent_type->name ();
3d567982
TT
21707 return "";
21708 }
21709 /* Fall through. */
63d06c5c 21710 default:
8176b9b8 21711 return determine_prefix (parent, cu);
63d06c5c 21712 }
63d06c5c
DC
21713}
21714
3e43a32a
MS
21715/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21716 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21717 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21718 an obconcat, otherwise allocate storage for the result. The CU argument is
21719 used to determine the language and hence, the appropriate separator. */
987504bb 21720
f55ee35c 21721#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21722
21723static char *
f55ee35c
JK
21724typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21725 int physname, struct dwarf2_cu *cu)
63d06c5c 21726{
f55ee35c 21727 const char *lead = "";
5c315b68 21728 const char *sep;
63d06c5c 21729
3e43a32a
MS
21730 if (suffix == NULL || suffix[0] == '\0'
21731 || prefix == NULL || prefix[0] == '\0')
987504bb 21732 sep = "";
45280282
IB
21733 else if (cu->language == language_d)
21734 {
21735 /* For D, the 'main' function could be defined in any module, but it
21736 should never be prefixed. */
21737 if (strcmp (suffix, "D main") == 0)
21738 {
21739 prefix = "";
21740 sep = "";
21741 }
21742 else
21743 sep = ".";
21744 }
f55ee35c
JK
21745 else if (cu->language == language_fortran && physname)
21746 {
21747 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21748 DW_AT_MIPS_linkage_name is preferred and used instead. */
21749
21750 lead = "__";
21751 sep = "_MOD_";
21752 }
987504bb
JJ
21753 else
21754 sep = "::";
63d06c5c 21755
6dd47d34
DE
21756 if (prefix == NULL)
21757 prefix = "";
21758 if (suffix == NULL)
21759 suffix = "";
21760
987504bb
JJ
21761 if (obs == NULL)
21762 {
3e43a32a 21763 char *retval
224c3ddb
SM
21764 = ((char *)
21765 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21766
f55ee35c
JK
21767 strcpy (retval, lead);
21768 strcat (retval, prefix);
6dd47d34
DE
21769 strcat (retval, sep);
21770 strcat (retval, suffix);
63d06c5c
DC
21771 return retval;
21772 }
987504bb
JJ
21773 else
21774 {
21775 /* We have an obstack. */
f55ee35c 21776 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21777 }
63d06c5c
DC
21778}
21779
71c25dea
TT
21780/* Get name of a die, return NULL if not found. */
21781
15d034d0
TT
21782static const char *
21783dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21784 struct objfile *objfile)
71c25dea
TT
21785{
21786 if (name && cu->language == language_cplus)
21787 {
596dc4ad
TT
21788 gdb::unique_xmalloc_ptr<char> canon_name
21789 = cp_canonicalize_string (name);
71c25dea 21790
596dc4ad
TT
21791 if (canon_name != nullptr)
21792 name = objfile->intern (canon_name.get ());
71c25dea
TT
21793 }
21794
21795 return name;
c906108c
SS
21796}
21797
96553a0c
DE
21798/* Get name of a die, return NULL if not found.
21799 Anonymous namespaces are converted to their magic string. */
9219021c 21800
15d034d0 21801static const char *
e142c38c 21802dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21803{
21804 struct attribute *attr;
518817b3 21805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21806
e142c38c 21807 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21808 if ((!attr || !DW_STRING (attr))
96553a0c 21809 && die->tag != DW_TAG_namespace
53832f31
TT
21810 && die->tag != DW_TAG_class_type
21811 && die->tag != DW_TAG_interface_type
21812 && die->tag != DW_TAG_structure_type
21813 && die->tag != DW_TAG_union_type)
71c25dea
TT
21814 return NULL;
21815
21816 switch (die->tag)
21817 {
21818 case DW_TAG_compile_unit:
95554aad 21819 case DW_TAG_partial_unit:
71c25dea
TT
21820 /* Compilation units have a DW_AT_name that is a filename, not
21821 a source language identifier. */
21822 case DW_TAG_enumeration_type:
21823 case DW_TAG_enumerator:
21824 /* These tags always have simple identifiers already; no need
21825 to canonicalize them. */
21826 return DW_STRING (attr);
907af001 21827
96553a0c
DE
21828 case DW_TAG_namespace:
21829 if (attr != NULL && DW_STRING (attr) != NULL)
21830 return DW_STRING (attr);
21831 return CP_ANONYMOUS_NAMESPACE_STR;
21832
907af001
UW
21833 case DW_TAG_class_type:
21834 case DW_TAG_interface_type:
21835 case DW_TAG_structure_type:
21836 case DW_TAG_union_type:
21837 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21838 structures or unions. These were of the form "._%d" in GCC 4.1,
21839 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21840 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21841 if (attr && DW_STRING (attr)
61012eef
GB
21842 && (startswith (DW_STRING (attr), "._")
21843 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21844 return NULL;
53832f31
TT
21845
21846 /* GCC might emit a nameless typedef that has a linkage name. See
21847 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21848 if (!attr || DW_STRING (attr) == NULL)
21849 {
73b9be8b 21850 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21851 if (attr == NULL || DW_STRING (attr) == NULL)
21852 return NULL;
21853
df5c6c50
JK
21854 /* Avoid demangling DW_STRING (attr) the second time on a second
21855 call for the same DIE. */
21856 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21857 {
43816ebc
TT
21858 gdb::unique_xmalloc_ptr<char> demangled
21859 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21860 if (demangled == nullptr)
21861 return nullptr;
43816ebc 21862
be1e3d3e 21863 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21864 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21865 }
67430cd0
TT
21866
21867 /* Strip any leading namespaces/classes, keep only the base name.
21868 DW_AT_name for named DIEs does not contain the prefixes. */
21869 const char *base = strrchr (DW_STRING (attr), ':');
21870 if (base && base > DW_STRING (attr) && base[-1] == ':')
21871 return &base[1];
21872 else
21873 return DW_STRING (attr);
53832f31 21874 }
907af001
UW
21875 break;
21876
71c25dea 21877 default:
907af001
UW
21878 break;
21879 }
21880
21881 if (!DW_STRING_IS_CANONICAL (attr))
21882 {
be1e3d3e
TT
21883 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21884 objfile);
907af001 21885 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21886 }
907af001 21887 return DW_STRING (attr);
9219021c
DC
21888}
21889
21890/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21891 is none. *EXT_CU is the CU containing DIE on input, and the CU
21892 containing the return value on output. */
9219021c
DC
21893
21894static struct die_info *
f2f0e013 21895dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21896{
21897 struct attribute *attr;
9219021c 21898
f2f0e013 21899 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21900 if (attr == NULL)
21901 return NULL;
21902
f2f0e013 21903 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21904}
21905
f9aca02d 21906static void
d97bc12b 21907dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21908{
21909 unsigned int i;
21910
d97bc12b 21911 print_spaces (indent, f);
9d8780f0 21912 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21913 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21914 sect_offset_str (die->sect_off));
d97bc12b
DE
21915
21916 if (die->parent != NULL)
21917 {
21918 print_spaces (indent, f);
9d8780f0
SM
21919 fprintf_unfiltered (f, " parent at offset: %s\n",
21920 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21921 }
21922
21923 print_spaces (indent, f);
21924 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21925 dwarf_bool_name (die->child != NULL));
c906108c 21926
d97bc12b
DE
21927 print_spaces (indent, f);
21928 fprintf_unfiltered (f, " attributes:\n");
21929
c906108c
SS
21930 for (i = 0; i < die->num_attrs; ++i)
21931 {
d97bc12b
DE
21932 print_spaces (indent, f);
21933 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21934 dwarf_attr_name (die->attrs[i].name),
21935 dwarf_form_name (die->attrs[i].form));
d97bc12b 21936
c906108c
SS
21937 switch (die->attrs[i].form)
21938 {
c906108c 21939 case DW_FORM_addr:
336d760d 21940 case DW_FORM_addrx:
3019eac3 21941 case DW_FORM_GNU_addr_index:
d97bc12b 21942 fprintf_unfiltered (f, "address: ");
5af949e3 21943 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21944 break;
21945 case DW_FORM_block2:
21946 case DW_FORM_block4:
21947 case DW_FORM_block:
21948 case DW_FORM_block1:
56eb65bd
SP
21949 fprintf_unfiltered (f, "block: size %s",
21950 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21951 break;
2dc7f7b3 21952 case DW_FORM_exprloc:
56eb65bd
SP
21953 fprintf_unfiltered (f, "expression: size %s",
21954 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21955 break;
0224619f
JK
21956 case DW_FORM_data16:
21957 fprintf_unfiltered (f, "constant of 16 bytes");
21958 break;
4568ecf9
DE
21959 case DW_FORM_ref_addr:
21960 fprintf_unfiltered (f, "ref address: ");
21961 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21962 break;
36586728
TT
21963 case DW_FORM_GNU_ref_alt:
21964 fprintf_unfiltered (f, "alt ref address: ");
21965 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21966 break;
10b3939b
DJ
21967 case DW_FORM_ref1:
21968 case DW_FORM_ref2:
21969 case DW_FORM_ref4:
4568ecf9
DE
21970 case DW_FORM_ref8:
21971 case DW_FORM_ref_udata:
d97bc12b 21972 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21973 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21974 break;
c906108c
SS
21975 case DW_FORM_data1:
21976 case DW_FORM_data2:
21977 case DW_FORM_data4:
ce5d95e1 21978 case DW_FORM_data8:
c906108c
SS
21979 case DW_FORM_udata:
21980 case DW_FORM_sdata:
43bbcdc2
PH
21981 fprintf_unfiltered (f, "constant: %s",
21982 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21983 break;
2dc7f7b3
TT
21984 case DW_FORM_sec_offset:
21985 fprintf_unfiltered (f, "section offset: %s",
21986 pulongest (DW_UNSND (&die->attrs[i])));
21987 break;
55f1336d 21988 case DW_FORM_ref_sig8:
ac9ec31b
DE
21989 fprintf_unfiltered (f, "signature: %s",
21990 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21991 break;
c906108c 21992 case DW_FORM_string:
4bdf3d34 21993 case DW_FORM_strp:
43988095 21994 case DW_FORM_line_strp:
cf532bd1 21995 case DW_FORM_strx:
3019eac3 21996 case DW_FORM_GNU_str_index:
36586728 21997 case DW_FORM_GNU_strp_alt:
8285870a 21998 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21999 DW_STRING (&die->attrs[i])
8285870a
JK
22000 ? DW_STRING (&die->attrs[i]) : "",
22001 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22002 break;
22003 case DW_FORM_flag:
22004 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22005 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22006 else
d97bc12b 22007 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22008 break;
2dc7f7b3
TT
22009 case DW_FORM_flag_present:
22010 fprintf_unfiltered (f, "flag: TRUE");
22011 break;
a8329558 22012 case DW_FORM_indirect:
0963b4bd
MS
22013 /* The reader will have reduced the indirect form to
22014 the "base form" so this form should not occur. */
5f48f8f3 22015 fprintf_unfiltered (f,
3e43a32a 22016 "unexpected attribute form: DW_FORM_indirect");
a8329558 22017 break;
663c44ac
JK
22018 case DW_FORM_implicit_const:
22019 fprintf_unfiltered (f, "constant: %s",
22020 plongest (DW_SND (&die->attrs[i])));
22021 break;
c906108c 22022 default:
d97bc12b 22023 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22024 die->attrs[i].form);
d97bc12b 22025 break;
c906108c 22026 }
d97bc12b 22027 fprintf_unfiltered (f, "\n");
c906108c
SS
22028 }
22029}
22030
f9aca02d 22031static void
d97bc12b 22032dump_die_for_error (struct die_info *die)
c906108c 22033{
d97bc12b
DE
22034 dump_die_shallow (gdb_stderr, 0, die);
22035}
22036
22037static void
22038dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22039{
22040 int indent = level * 4;
22041
22042 gdb_assert (die != NULL);
22043
22044 if (level >= max_level)
22045 return;
22046
22047 dump_die_shallow (f, indent, die);
22048
22049 if (die->child != NULL)
c906108c 22050 {
d97bc12b
DE
22051 print_spaces (indent, f);
22052 fprintf_unfiltered (f, " Children:");
22053 if (level + 1 < max_level)
22054 {
22055 fprintf_unfiltered (f, "\n");
22056 dump_die_1 (f, level + 1, max_level, die->child);
22057 }
22058 else
22059 {
3e43a32a
MS
22060 fprintf_unfiltered (f,
22061 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22062 }
22063 }
22064
22065 if (die->sibling != NULL && level > 0)
22066 {
22067 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22068 }
22069}
22070
d97bc12b
DE
22071/* This is called from the pdie macro in gdbinit.in.
22072 It's not static so gcc will keep a copy callable from gdb. */
22073
22074void
22075dump_die (struct die_info *die, int max_level)
22076{
22077 dump_die_1 (gdb_stdlog, 0, max_level, die);
22078}
22079
f9aca02d 22080static void
51545339 22081store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22082{
51545339 22083 void **slot;
c906108c 22084
9c541725
PA
22085 slot = htab_find_slot_with_hash (cu->die_hash, die,
22086 to_underlying (die->sect_off),
b64f50a1 22087 INSERT);
51545339
DJ
22088
22089 *slot = die;
c906108c
SS
22090}
22091
348e048f
DE
22092/* Follow reference or signature attribute ATTR of SRC_DIE.
22093 On entry *REF_CU is the CU of SRC_DIE.
22094 On exit *REF_CU is the CU of the result. */
22095
22096static struct die_info *
ff39bb5e 22097follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22098 struct dwarf2_cu **ref_cu)
22099{
22100 struct die_info *die;
22101
cd6c91b4 22102 if (attr->form_is_ref ())
348e048f 22103 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22104 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22105 die = follow_die_sig (src_die, attr, ref_cu);
22106 else
22107 {
22108 dump_die_for_error (src_die);
22109 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22110 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22111 }
22112
22113 return die;
03dd20cc
DJ
22114}
22115
5c631832 22116/* Follow reference OFFSET.
673bfd45
DE
22117 On entry *REF_CU is the CU of the source die referencing OFFSET.
22118 On exit *REF_CU is the CU of the result.
22119 Returns NULL if OFFSET is invalid. */
f504f079 22120
f9aca02d 22121static struct die_info *
9c541725 22122follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22123 struct dwarf2_cu **ref_cu)
c906108c 22124{
10b3939b 22125 struct die_info temp_die;
f2f0e013 22126 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22127 struct dwarf2_per_objfile *dwarf2_per_objfile
22128 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22129
348e048f
DE
22130 gdb_assert (cu->per_cu != NULL);
22131
98bfdba5
PA
22132 target_cu = cu;
22133
3019eac3 22134 if (cu->per_cu->is_debug_types)
348e048f
DE
22135 {
22136 /* .debug_types CUs cannot reference anything outside their CU.
22137 If they need to, they have to reference a signatured type via
55f1336d 22138 DW_FORM_ref_sig8. */
4057dfde 22139 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22140 return NULL;
348e048f 22141 }
36586728 22142 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22143 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22144 {
22145 struct dwarf2_per_cu_data *per_cu;
9a619af0 22146
9c541725 22147 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22148 dwarf2_per_objfile);
03dd20cc
DJ
22149
22150 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22151 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22152 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22153
10b3939b
DJ
22154 target_cu = per_cu->cu;
22155 }
98bfdba5
PA
22156 else if (cu->dies == NULL)
22157 {
22158 /* We're loading full DIEs during partial symbol reading. */
22159 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22160 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22161 }
c906108c 22162
f2f0e013 22163 *ref_cu = target_cu;
9c541725 22164 temp_die.sect_off = sect_off;
c24bdb02
KS
22165
22166 if (target_cu != cu)
22167 target_cu->ancestor = cu;
22168
9a3c8263 22169 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22170 &temp_die,
22171 to_underlying (sect_off));
5c631832 22172}
10b3939b 22173
5c631832
JK
22174/* Follow reference attribute ATTR of SRC_DIE.
22175 On entry *REF_CU is the CU of SRC_DIE.
22176 On exit *REF_CU is the CU of the result. */
22177
22178static struct die_info *
ff39bb5e 22179follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22180 struct dwarf2_cu **ref_cu)
22181{
0826b30a 22182 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22183 struct dwarf2_cu *cu = *ref_cu;
22184 struct die_info *die;
22185
9c541725 22186 die = follow_die_offset (sect_off,
36586728
TT
22187 (attr->form == DW_FORM_GNU_ref_alt
22188 || cu->per_cu->is_dwz),
22189 ref_cu);
5c631832 22190 if (!die)
9d8780f0
SM
22191 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22192 "at %s [in module %s]"),
22193 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22194 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22195
5c631832
JK
22196 return die;
22197}
22198
d4c9a4f8 22199/* See read.h. */
5c631832
JK
22200
22201struct dwarf2_locexpr_baton
9c541725 22202dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22203 dwarf2_per_cu_data *per_cu,
8b9737bf 22204 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22205 void *baton, bool resolve_abstract_p)
5c631832 22206{
918dd910 22207 struct dwarf2_cu *cu;
5c631832
JK
22208 struct die_info *die;
22209 struct attribute *attr;
22210 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22211 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22212 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22213
918dd910 22214 if (per_cu->cu == NULL)
58f0c718 22215 load_cu (per_cu, false);
918dd910 22216 cu = per_cu->cu;
cc12ce38
DE
22217 if (cu == NULL)
22218 {
22219 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22220 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22221 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22222 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22223 }
918dd910 22224
9c541725 22225 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22226 if (!die)
9d8780f0
SM
22227 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22228 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22229
22230 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22231 if (!attr && resolve_abstract_p
3360b6e7 22232 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22233 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22234 {
22235 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22236 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22237 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22238
3360b6e7
TV
22239 for (const auto &cand_off
22240 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22241 {
3360b6e7
TV
22242 struct dwarf2_cu *cand_cu = cu;
22243 struct die_info *cand
22244 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22245 if (!cand
22246 || !cand->parent
e4a62c65
TV
22247 || cand->parent->tag != DW_TAG_subprogram)
22248 continue;
22249
22250 CORE_ADDR pc_low, pc_high;
22251 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22252 if (pc_low == ((CORE_ADDR) -1))
22253 continue;
22254 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22255 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22256 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22257 continue;
22258
22259 die = cand;
22260 attr = dwarf2_attr (die, DW_AT_location, cu);
22261 break;
22262 }
22263 }
22264
5c631832
JK
22265 if (!attr)
22266 {
e103e986
JK
22267 /* DWARF: "If there is no such attribute, then there is no effect.".
22268 DATA is ignored if SIZE is 0. */
5c631832 22269
e103e986 22270 retval.data = NULL;
5c631832
JK
22271 retval.size = 0;
22272 }
cd6c91b4 22273 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22274 {
22275 struct dwarf2_loclist_baton loclist_baton;
22276 CORE_ADDR pc = (*get_frame_pc) (baton);
22277 size_t size;
22278
22279 fill_in_loclist_baton (cu, &loclist_baton, attr);
22280
22281 retval.data = dwarf2_find_location_expression (&loclist_baton,
22282 &size, pc);
22283 retval.size = size;
22284 }
5c631832
JK
22285 else
22286 {
4fc6c0d5 22287 if (!attr->form_is_block ())
9d8780f0 22288 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22289 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22290 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22291
22292 retval.data = DW_BLOCK (attr)->data;
22293 retval.size = DW_BLOCK (attr)->size;
22294 }
22295 retval.per_cu = cu->per_cu;
918dd910 22296
ed2dc618 22297 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22298
5c631832 22299 return retval;
348e048f
DE
22300}
22301
d4c9a4f8 22302/* See read.h. */
8b9737bf
TT
22303
22304struct dwarf2_locexpr_baton
22305dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22306 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22307 CORE_ADDR (*get_frame_pc) (void *baton),
22308 void *baton)
22309{
9c541725 22310 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22311
9c541725 22312 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22313}
22314
b6807d98
TT
22315/* Write a constant of a given type as target-ordered bytes into
22316 OBSTACK. */
22317
22318static const gdb_byte *
22319write_constant_as_bytes (struct obstack *obstack,
22320 enum bfd_endian byte_order,
22321 struct type *type,
22322 ULONGEST value,
22323 LONGEST *len)
22324{
22325 gdb_byte *result;
22326
22327 *len = TYPE_LENGTH (type);
224c3ddb 22328 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22329 store_unsigned_integer (result, *len, byte_order, value);
22330
22331 return result;
22332}
22333
d4c9a4f8 22334/* See read.h. */
b6807d98
TT
22335
22336const gdb_byte *
9c541725 22337dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22338 dwarf2_per_cu_data *per_cu,
22339 obstack *obstack,
b6807d98
TT
22340 LONGEST *len)
22341{
22342 struct dwarf2_cu *cu;
22343 struct die_info *die;
22344 struct attribute *attr;
22345 const gdb_byte *result = NULL;
22346 struct type *type;
22347 LONGEST value;
22348 enum bfd_endian byte_order;
e3b94546 22349 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22350
b6807d98 22351 if (per_cu->cu == NULL)
58f0c718 22352 load_cu (per_cu, false);
b6807d98 22353 cu = per_cu->cu;
cc12ce38
DE
22354 if (cu == NULL)
22355 {
22356 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22357 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22358 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22359 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22360 }
b6807d98 22361
9c541725 22362 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22363 if (!die)
9d8780f0
SM
22364 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22365 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22366
22367 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22368 if (attr == NULL)
22369 return NULL;
22370
e3b94546 22371 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22372 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22373
22374 switch (attr->form)
22375 {
22376 case DW_FORM_addr:
336d760d 22377 case DW_FORM_addrx:
b6807d98
TT
22378 case DW_FORM_GNU_addr_index:
22379 {
22380 gdb_byte *tem;
22381
22382 *len = cu->header.addr_size;
224c3ddb 22383 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22384 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22385 result = tem;
22386 }
22387 break;
22388 case DW_FORM_string:
22389 case DW_FORM_strp:
cf532bd1 22390 case DW_FORM_strx:
b6807d98
TT
22391 case DW_FORM_GNU_str_index:
22392 case DW_FORM_GNU_strp_alt:
22393 /* DW_STRING is already allocated on the objfile obstack, point
22394 directly to it. */
22395 result = (const gdb_byte *) DW_STRING (attr);
22396 *len = strlen (DW_STRING (attr));
22397 break;
22398 case DW_FORM_block1:
22399 case DW_FORM_block2:
22400 case DW_FORM_block4:
22401 case DW_FORM_block:
22402 case DW_FORM_exprloc:
0224619f 22403 case DW_FORM_data16:
b6807d98
TT
22404 result = DW_BLOCK (attr)->data;
22405 *len = DW_BLOCK (attr)->size;
22406 break;
22407
22408 /* The DW_AT_const_value attributes are supposed to carry the
22409 symbol's value "represented as it would be on the target
22410 architecture." By the time we get here, it's already been
22411 converted to host endianness, so we just need to sign- or
22412 zero-extend it as appropriate. */
22413 case DW_FORM_data1:
22414 type = die_type (die, cu);
22415 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22416 if (result == NULL)
22417 result = write_constant_as_bytes (obstack, byte_order,
22418 type, value, len);
22419 break;
22420 case DW_FORM_data2:
22421 type = die_type (die, cu);
22422 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22423 if (result == NULL)
22424 result = write_constant_as_bytes (obstack, byte_order,
22425 type, value, len);
22426 break;
22427 case DW_FORM_data4:
22428 type = die_type (die, cu);
22429 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22430 if (result == NULL)
22431 result = write_constant_as_bytes (obstack, byte_order,
22432 type, value, len);
22433 break;
22434 case DW_FORM_data8:
22435 type = die_type (die, cu);
22436 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22437 if (result == NULL)
22438 result = write_constant_as_bytes (obstack, byte_order,
22439 type, value, len);
22440 break;
22441
22442 case DW_FORM_sdata:
663c44ac 22443 case DW_FORM_implicit_const:
b6807d98
TT
22444 type = die_type (die, cu);
22445 result = write_constant_as_bytes (obstack, byte_order,
22446 type, DW_SND (attr), len);
22447 break;
22448
22449 case DW_FORM_udata:
22450 type = die_type (die, cu);
22451 result = write_constant_as_bytes (obstack, byte_order,
22452 type, DW_UNSND (attr), len);
22453 break;
22454
22455 default:
b98664d3 22456 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22457 dwarf_form_name (attr->form));
22458 break;
22459 }
22460
22461 return result;
22462}
22463
d4c9a4f8 22464/* See read.h. */
7942e96e
AA
22465
22466struct type *
9c541725 22467dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22468 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22469{
22470 struct dwarf2_cu *cu;
22471 struct die_info *die;
22472
7942e96e 22473 if (per_cu->cu == NULL)
58f0c718 22474 load_cu (per_cu, false);
7942e96e
AA
22475 cu = per_cu->cu;
22476 if (!cu)
22477 return NULL;
22478
9c541725 22479 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22480 if (!die)
22481 return NULL;
22482
22483 return die_type (die, cu);
22484}
22485
8cb5117c 22486/* See read.h. */
8a9b8146
TT
22487
22488struct type *
b64f50a1 22489dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22490 struct dwarf2_per_cu_data *per_cu)
22491{
9c541725 22492 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22493 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22494}
22495
ac9ec31b 22496/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22497 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22498 On exit *REF_CU is the CU of the result.
22499 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22500
22501static struct die_info *
ac9ec31b
DE
22502follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22503 struct dwarf2_cu **ref_cu)
348e048f 22504{
348e048f 22505 struct die_info temp_die;
c24bdb02 22506 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22507 struct die_info *die;
22508
ac9ec31b
DE
22509 /* While it might be nice to assert sig_type->type == NULL here,
22510 we can get here for DW_AT_imported_declaration where we need
22511 the DIE not the type. */
348e048f
DE
22512
22513 /* If necessary, add it to the queue and load its DIEs. */
22514
95554aad 22515 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22516 read_signatured_type (sig_type);
348e048f 22517
348e048f 22518 sig_cu = sig_type->per_cu.cu;
69d751e3 22519 gdb_assert (sig_cu != NULL);
9c541725
PA
22520 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22521 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22522 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22523 to_underlying (temp_die.sect_off));
348e048f
DE
22524 if (die)
22525 {
ed2dc618 22526 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22527 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22528
796a7ff8
DE
22529 /* For .gdb_index version 7 keep track of included TUs.
22530 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22531 if (dwarf2_per_objfile->index_table != NULL
22532 && dwarf2_per_objfile->index_table->version <= 7)
22533 {
ae640021 22534 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22535 }
22536
348e048f 22537 *ref_cu = sig_cu;
c24bdb02
KS
22538 if (sig_cu != cu)
22539 sig_cu->ancestor = cu;
22540
348e048f
DE
22541 return die;
22542 }
22543
ac9ec31b
DE
22544 return NULL;
22545}
22546
22547/* Follow signatured type referenced by ATTR in SRC_DIE.
22548 On entry *REF_CU is the CU of SRC_DIE.
22549 On exit *REF_CU is the CU of the result.
22550 The result is the DIE of the type.
22551 If the referenced type cannot be found an error is thrown. */
22552
22553static struct die_info *
ff39bb5e 22554follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22555 struct dwarf2_cu **ref_cu)
22556{
22557 ULONGEST signature = DW_SIGNATURE (attr);
22558 struct signatured_type *sig_type;
22559 struct die_info *die;
22560
22561 gdb_assert (attr->form == DW_FORM_ref_sig8);
22562
a2ce51a0 22563 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22564 /* sig_type will be NULL if the signatured type is missing from
22565 the debug info. */
22566 if (sig_type == NULL)
22567 {
22568 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22569 " from DIE at %s [in module %s]"),
22570 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22571 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22572 }
22573
22574 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22575 if (die == NULL)
22576 {
22577 dump_die_for_error (src_die);
22578 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22579 " from DIE at %s [in module %s]"),
22580 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22581 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22582 }
22583
22584 return die;
22585}
22586
22587/* Get the type specified by SIGNATURE referenced in DIE/CU,
22588 reading in and processing the type unit if necessary. */
22589
22590static struct type *
22591get_signatured_type (struct die_info *die, ULONGEST signature,
22592 struct dwarf2_cu *cu)
22593{
518817b3
SM
22594 struct dwarf2_per_objfile *dwarf2_per_objfile
22595 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22596 struct signatured_type *sig_type;
22597 struct dwarf2_cu *type_cu;
22598 struct die_info *type_die;
22599 struct type *type;
22600
a2ce51a0 22601 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22602 /* sig_type will be NULL if the signatured type is missing from
22603 the debug info. */
22604 if (sig_type == NULL)
22605 {
b98664d3 22606 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22607 " from DIE at %s [in module %s]"),
22608 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22609 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22610 return build_error_marker_type (cu, die);
22611 }
22612
22613 /* If we already know the type we're done. */
22614 if (sig_type->type != NULL)
22615 return sig_type->type;
22616
22617 type_cu = cu;
22618 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22619 if (type_die != NULL)
22620 {
22621 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22622 is created. This is important, for example, because for c++ classes
22623 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22624 type = read_type_die (type_die, type_cu);
22625 if (type == NULL)
22626 {
b98664d3 22627 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22628 " referenced from DIE at %s [in module %s]"),
22629 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22630 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22631 type = build_error_marker_type (cu, die);
22632 }
22633 }
22634 else
22635 {
b98664d3 22636 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22637 " from DIE at %s [in module %s]"),
22638 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22639 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22640 type = build_error_marker_type (cu, die);
22641 }
22642 sig_type->type = type;
22643
22644 return type;
22645}
22646
22647/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22648 reading in and processing the type unit if necessary. */
22649
22650static struct type *
ff39bb5e 22651get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22652 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22653{
22654 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22655 if (attr->form_is_ref ())
ac9ec31b
DE
22656 {
22657 struct dwarf2_cu *type_cu = cu;
22658 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22659
22660 return read_type_die (type_die, type_cu);
22661 }
22662 else if (attr->form == DW_FORM_ref_sig8)
22663 {
22664 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22665 }
22666 else
22667 {
518817b3
SM
22668 struct dwarf2_per_objfile *dwarf2_per_objfile
22669 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22670
b98664d3 22671 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22672 " at %s [in module %s]"),
22673 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22674 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22675 return build_error_marker_type (cu, die);
22676 }
348e048f
DE
22677}
22678
e5fe5e75 22679/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22680
22681static void
e5fe5e75 22682load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22683{
52dc124a 22684 struct signatured_type *sig_type;
348e048f 22685
f4dc4d17 22686 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22687 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22688
6721b2ec
DE
22689 /* We have the per_cu, but we need the signatured_type.
22690 Fortunately this is an easy translation. */
22691 gdb_assert (per_cu->is_debug_types);
22692 sig_type = (struct signatured_type *) per_cu;
348e048f 22693
6721b2ec 22694 gdb_assert (per_cu->cu == NULL);
348e048f 22695
52dc124a 22696 read_signatured_type (sig_type);
348e048f 22697
6721b2ec 22698 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22699}
22700
3019eac3
DE
22701/* Read in a signatured type and build its CU and DIEs.
22702 If the type is a stub for the real type in a DWO file,
22703 read in the real type from the DWO file as well. */
dee91e82
DE
22704
22705static void
22706read_signatured_type (struct signatured_type *sig_type)
22707{
22708 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22709
3019eac3 22710 gdb_assert (per_cu->is_debug_types);
dee91e82 22711 gdb_assert (per_cu->cu == NULL);
348e048f 22712
6751ebae 22713 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22714
22715 if (!reader.dummy_p)
22716 {
22717 struct dwarf2_cu *cu = reader.cu;
22718 const gdb_byte *info_ptr = reader.info_ptr;
22719
22720 gdb_assert (cu->die_hash == NULL);
22721 cu->die_hash =
22722 htab_create_alloc_ex (cu->header.length / 12,
22723 die_hash,
22724 die_eq,
22725 NULL,
22726 &cu->comp_unit_obstack,
22727 hashtab_obstack_allocate,
22728 dummy_obstack_deallocate);
22729
3e225074 22730 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22731 reader.comp_unit_die->child
22732 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22733 reader.comp_unit_die);
22734 cu->dies = reader.comp_unit_die;
22735 /* comp_unit_die is not stored in die_hash, no need. */
22736
22737 /* We try not to read any attributes in this function, because
22738 not all CUs needed for references have been loaded yet, and
22739 symbol table processing isn't initialized. But we have to
22740 set the CU language, or we won't be able to build types
22741 correctly. Similarly, if we do not read the producer, we can
22742 not apply producer-specific interpretation. */
22743 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22744
22745 reader.keep ();
c0ab21c2
TT
22746 }
22747
7ee85ab1 22748 sig_type->per_cu.tu_read = 1;
c906108c
SS
22749}
22750
c906108c
SS
22751/* Decode simple location descriptions.
22752 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22753 the location and return the value. If COMPUTED is non-null, it is
22754 set to true to indicate that decoding was successful, and false
22755 otherwise. If COMPUTED is null, then this function may emit a
22756 complaint. */
c906108c
SS
22757
22758static CORE_ADDR
7d79de9a 22759decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22760{
518817b3 22761 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22762 size_t i;
22763 size_t size = blk->size;
d521ce57 22764 const gdb_byte *data = blk->data;
21ae7a4d
JK
22765 CORE_ADDR stack[64];
22766 int stacki;
22767 unsigned int bytes_read, unsnd;
22768 gdb_byte op;
c906108c 22769
7d79de9a
TT
22770 if (computed != nullptr)
22771 *computed = false;
22772
21ae7a4d
JK
22773 i = 0;
22774 stacki = 0;
22775 stack[stacki] = 0;
22776 stack[++stacki] = 0;
22777
22778 while (i < size)
22779 {
22780 op = data[i++];
22781 switch (op)
22782 {
22783 case DW_OP_lit0:
22784 case DW_OP_lit1:
22785 case DW_OP_lit2:
22786 case DW_OP_lit3:
22787 case DW_OP_lit4:
22788 case DW_OP_lit5:
22789 case DW_OP_lit6:
22790 case DW_OP_lit7:
22791 case DW_OP_lit8:
22792 case DW_OP_lit9:
22793 case DW_OP_lit10:
22794 case DW_OP_lit11:
22795 case DW_OP_lit12:
22796 case DW_OP_lit13:
22797 case DW_OP_lit14:
22798 case DW_OP_lit15:
22799 case DW_OP_lit16:
22800 case DW_OP_lit17:
22801 case DW_OP_lit18:
22802 case DW_OP_lit19:
22803 case DW_OP_lit20:
22804 case DW_OP_lit21:
22805 case DW_OP_lit22:
22806 case DW_OP_lit23:
22807 case DW_OP_lit24:
22808 case DW_OP_lit25:
22809 case DW_OP_lit26:
22810 case DW_OP_lit27:
22811 case DW_OP_lit28:
22812 case DW_OP_lit29:
22813 case DW_OP_lit30:
22814 case DW_OP_lit31:
22815 stack[++stacki] = op - DW_OP_lit0;
22816 break;
f1bea926 22817
21ae7a4d
JK
22818 case DW_OP_reg0:
22819 case DW_OP_reg1:
22820 case DW_OP_reg2:
22821 case DW_OP_reg3:
22822 case DW_OP_reg4:
22823 case DW_OP_reg5:
22824 case DW_OP_reg6:
22825 case DW_OP_reg7:
22826 case DW_OP_reg8:
22827 case DW_OP_reg9:
22828 case DW_OP_reg10:
22829 case DW_OP_reg11:
22830 case DW_OP_reg12:
22831 case DW_OP_reg13:
22832 case DW_OP_reg14:
22833 case DW_OP_reg15:
22834 case DW_OP_reg16:
22835 case DW_OP_reg17:
22836 case DW_OP_reg18:
22837 case DW_OP_reg19:
22838 case DW_OP_reg20:
22839 case DW_OP_reg21:
22840 case DW_OP_reg22:
22841 case DW_OP_reg23:
22842 case DW_OP_reg24:
22843 case DW_OP_reg25:
22844 case DW_OP_reg26:
22845 case DW_OP_reg27:
22846 case DW_OP_reg28:
22847 case DW_OP_reg29:
22848 case DW_OP_reg30:
22849 case DW_OP_reg31:
22850 stack[++stacki] = op - DW_OP_reg0;
22851 if (i < size)
7d79de9a
TT
22852 {
22853 if (computed == nullptr)
22854 dwarf2_complex_location_expr_complaint ();
22855 else
22856 return 0;
22857 }
21ae7a4d 22858 break;
c906108c 22859
21ae7a4d
JK
22860 case DW_OP_regx:
22861 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22862 i += bytes_read;
22863 stack[++stacki] = unsnd;
22864 if (i < size)
7d79de9a
TT
22865 {
22866 if (computed == nullptr)
22867 dwarf2_complex_location_expr_complaint ();
22868 else
22869 return 0;
22870 }
21ae7a4d 22871 break;
c906108c 22872
21ae7a4d 22873 case DW_OP_addr:
c8a7a66f
TT
22874 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22875 &bytes_read);
21ae7a4d
JK
22876 i += bytes_read;
22877 break;
d53d4ac5 22878
21ae7a4d
JK
22879 case DW_OP_const1u:
22880 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22881 i += 1;
22882 break;
22883
22884 case DW_OP_const1s:
22885 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22886 i += 1;
22887 break;
22888
22889 case DW_OP_const2u:
22890 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22891 i += 2;
22892 break;
22893
22894 case DW_OP_const2s:
22895 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22896 i += 2;
22897 break;
d53d4ac5 22898
21ae7a4d
JK
22899 case DW_OP_const4u:
22900 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22901 i += 4;
22902 break;
22903
22904 case DW_OP_const4s:
22905 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22906 i += 4;
22907 break;
22908
585861ea
JK
22909 case DW_OP_const8u:
22910 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22911 i += 8;
22912 break;
22913
21ae7a4d
JK
22914 case DW_OP_constu:
22915 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22916 &bytes_read);
22917 i += bytes_read;
22918 break;
22919
22920 case DW_OP_consts:
22921 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22922 i += bytes_read;
22923 break;
22924
22925 case DW_OP_dup:
22926 stack[stacki + 1] = stack[stacki];
22927 stacki++;
22928 break;
22929
22930 case DW_OP_plus:
22931 stack[stacki - 1] += stack[stacki];
22932 stacki--;
22933 break;
22934
22935 case DW_OP_plus_uconst:
22936 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22937 &bytes_read);
22938 i += bytes_read;
22939 break;
22940
22941 case DW_OP_minus:
22942 stack[stacki - 1] -= stack[stacki];
22943 stacki--;
22944 break;
22945
22946 case DW_OP_deref:
22947 /* If we're not the last op, then we definitely can't encode
22948 this using GDB's address_class enum. This is valid for partial
22949 global symbols, although the variable's address will be bogus
22950 in the psymtab. */
22951 if (i < size)
7d79de9a
TT
22952 {
22953 if (computed == nullptr)
22954 dwarf2_complex_location_expr_complaint ();
22955 else
22956 return 0;
22957 }
21ae7a4d
JK
22958 break;
22959
22960 case DW_OP_GNU_push_tls_address:
4aa4e28b 22961 case DW_OP_form_tls_address:
21ae7a4d
JK
22962 /* The top of the stack has the offset from the beginning
22963 of the thread control block at which the variable is located. */
22964 /* Nothing should follow this operator, so the top of stack would
22965 be returned. */
22966 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22967 address will be bogus in the psymtab. Make it always at least
22968 non-zero to not look as a variable garbage collected by linker
22969 which have DW_OP_addr 0. */
21ae7a4d 22970 if (i < size)
7d79de9a
TT
22971 {
22972 if (computed == nullptr)
22973 dwarf2_complex_location_expr_complaint ();
22974 else
22975 return 0;
22976 }
585861ea 22977 stack[stacki]++;
21ae7a4d
JK
22978 break;
22979
22980 case DW_OP_GNU_uninit:
7d79de9a
TT
22981 if (computed != nullptr)
22982 return 0;
21ae7a4d
JK
22983 break;
22984
336d760d 22985 case DW_OP_addrx:
3019eac3 22986 case DW_OP_GNU_addr_index:
49f6c839 22987 case DW_OP_GNU_const_index:
3019eac3
DE
22988 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22989 &bytes_read);
22990 i += bytes_read;
22991 break;
22992
21ae7a4d 22993 default:
7d79de9a
TT
22994 if (computed == nullptr)
22995 {
22996 const char *name = get_DW_OP_name (op);
21ae7a4d 22997
7d79de9a
TT
22998 if (name)
22999 complaint (_("unsupported stack op: '%s'"),
23000 name);
23001 else
23002 complaint (_("unsupported stack op: '%02x'"),
23003 op);
23004 }
21ae7a4d
JK
23005
23006 return (stack[stacki]);
d53d4ac5 23007 }
3c6e0cb3 23008
21ae7a4d
JK
23009 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23010 outside of the allocated space. Also enforce minimum>0. */
23011 if (stacki >= ARRAY_SIZE (stack) - 1)
23012 {
7d79de9a
TT
23013 if (computed == nullptr)
23014 complaint (_("location description stack overflow"));
21ae7a4d
JK
23015 return 0;
23016 }
23017
23018 if (stacki <= 0)
23019 {
7d79de9a
TT
23020 if (computed == nullptr)
23021 complaint (_("location description stack underflow"));
21ae7a4d
JK
23022 return 0;
23023 }
23024 }
7d79de9a
TT
23025
23026 if (computed != nullptr)
23027 *computed = true;
21ae7a4d 23028 return (stack[stacki]);
c906108c
SS
23029}
23030
23031/* memory allocation interface */
23032
c906108c 23033static struct dwarf_block *
7b5a2f43 23034dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23035{
8d749320 23036 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23037}
23038
c906108c 23039static struct die_info *
b60c80d6 23040dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23041{
23042 struct die_info *die;
b60c80d6
DJ
23043 size_t size = sizeof (struct die_info);
23044
23045 if (num_attrs > 1)
23046 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23047
b60c80d6 23048 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23049 memset (die, 0, sizeof (struct die_info));
23050 return (die);
23051}
2e276125
JB
23052
23053\f
a036ba48 23054
c90ec28a 23055/* Macro support. */
cf2c3c16 23056
9eac9650
TT
23057/* An overload of dwarf_decode_macros that finds the correct section
23058 and ensures it is read in before calling the other overload. */
23059
23060static void
23061dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23062 int section_is_gnu)
23063{
23064 struct dwarf2_per_objfile *dwarf2_per_objfile
23065 = cu->per_cu->dwarf2_per_objfile;
23066 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23067 const struct line_header *lh = cu->line_header;
9eac9650
TT
23068 unsigned int offset_size = cu->header.offset_size;
23069 struct dwarf2_section_info *section;
23070 const char *section_name;
23071
23072 if (cu->dwo_unit != nullptr)
23073 {
23074 if (section_is_gnu)
23075 {
23076 section = &cu->dwo_unit->dwo_file->sections.macro;
23077 section_name = ".debug_macro.dwo";
23078 }
23079 else
23080 {
23081 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23082 section_name = ".debug_macinfo.dwo";
23083 }
23084 }
23085 else
23086 {
23087 if (section_is_gnu)
23088 {
23089 section = &dwarf2_per_objfile->macro;
23090 section_name = ".debug_macro";
23091 }
23092 else
23093 {
23094 section = &dwarf2_per_objfile->macinfo;
23095 section_name = ".debug_macinfo";
23096 }
23097 }
23098
23099 section->read (objfile);
23100 if (section->buffer == nullptr)
23101 {
23102 complaint (_("missing %s section"), section_name);
23103 return;
23104 }
23105
23106 buildsym_compunit *builder = cu->get_builder ();
23107
23108 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23109 offset_size, offset, section_is_gnu);
23110}
23111
3019eac3
DE
23112/* Return the .debug_loc section to use for CU.
23113 For DWO files use .debug_loc.dwo. */
23114
23115static struct dwarf2_section_info *
23116cu_debug_loc_section (struct dwarf2_cu *cu)
23117{
518817b3
SM
23118 struct dwarf2_per_objfile *dwarf2_per_objfile
23119 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23120
3019eac3 23121 if (cu->dwo_unit)
43988095
JK
23122 {
23123 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23124
43988095
JK
23125 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23126 }
23127 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23128 : &dwarf2_per_objfile->loc);
3019eac3
DE
23129}
23130
8cf6f0b1
TT
23131/* A helper function that fills in a dwarf2_loclist_baton. */
23132
23133static void
23134fill_in_loclist_baton (struct dwarf2_cu *cu,
23135 struct dwarf2_loclist_baton *baton,
ff39bb5e 23136 const struct attribute *attr)
8cf6f0b1 23137{
518817b3
SM
23138 struct dwarf2_per_objfile *dwarf2_per_objfile
23139 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23140 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23141
96b79293 23142 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23143
23144 baton->per_cu = cu->per_cu;
23145 gdb_assert (baton->per_cu);
23146 /* We don't know how long the location list is, but make sure we
23147 don't run off the edge of the section. */
3019eac3
DE
23148 baton->size = section->size - DW_UNSND (attr);
23149 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23150 if (cu->base_address.has_value ())
23151 baton->base_address = *cu->base_address;
23152 else
23153 baton->base_address = 0;
f664829e 23154 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23155}
23156
4c2df51b 23157static void
ff39bb5e 23158dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23159 struct dwarf2_cu *cu, int is_block)
4c2df51b 23160{
518817b3
SM
23161 struct dwarf2_per_objfile *dwarf2_per_objfile
23162 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23163 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23164 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23165
cd6c91b4 23166 if (attr->form_is_section_offset ()
3019eac3 23167 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23168 the section. If so, fall through to the complaint in the
23169 other branch. */
2c7d5afc 23170 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23171 {
0d53c4c4 23172 struct dwarf2_loclist_baton *baton;
4c2df51b 23173
8d749320 23174 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23175
8cf6f0b1 23176 fill_in_loclist_baton (cu, baton, attr);
be391dca 23177
2b24b6e4 23178 if (!cu->base_address.has_value ())
b98664d3 23179 complaint (_("Location list used without "
3e43a32a 23180 "specifying the CU base address."));
4c2df51b 23181
f1e6e072
TT
23182 SYMBOL_ACLASS_INDEX (sym) = (is_block
23183 ? dwarf2_loclist_block_index
23184 : dwarf2_loclist_index);
0d53c4c4
DJ
23185 SYMBOL_LOCATION_BATON (sym) = baton;
23186 }
23187 else
23188 {
23189 struct dwarf2_locexpr_baton *baton;
23190
8d749320 23191 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23192 baton->per_cu = cu->per_cu;
23193 gdb_assert (baton->per_cu);
0d53c4c4 23194
4fc6c0d5 23195 if (attr->form_is_block ())
0d53c4c4
DJ
23196 {
23197 /* Note that we're just copying the block's data pointer
23198 here, not the actual data. We're still pointing into the
6502dd73
DJ
23199 info_buffer for SYM's objfile; right now we never release
23200 that buffer, but when we do clean up properly this may
23201 need to change. */
0d53c4c4
DJ
23202 baton->size = DW_BLOCK (attr)->size;
23203 baton->data = DW_BLOCK (attr)->data;
23204 }
23205 else
23206 {
23207 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23208 sym->natural_name ());
0d53c4c4 23209 baton->size = 0;
0d53c4c4 23210 }
6e70227d 23211
f1e6e072
TT
23212 SYMBOL_ACLASS_INDEX (sym) = (is_block
23213 ? dwarf2_locexpr_block_index
23214 : dwarf2_locexpr_index);
0d53c4c4
DJ
23215 SYMBOL_LOCATION_BATON (sym) = baton;
23216 }
4c2df51b 23217}
6502dd73 23218
09ba997f 23219/* See read.h. */
ae0d2f24
UW
23220
23221struct objfile *
09ba997f 23222dwarf2_per_cu_data::objfile () const
ae0d2f24 23223{
09ba997f 23224 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23225
23226 /* Return the master objfile, so that we can report and look up the
23227 correct file containing this variable. */
23228 if (objfile->separate_debug_objfile_backlink)
23229 objfile = objfile->separate_debug_objfile_backlink;
23230
23231 return objfile;
23232}
23233
96408a79
SA
23234/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23235 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23236 CU_HEADERP first. */
23237
23238static const struct comp_unit_head *
23239per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23240 const struct dwarf2_per_cu_data *per_cu)
96408a79 23241{
d521ce57 23242 const gdb_byte *info_ptr;
96408a79
SA
23243
23244 if (per_cu->cu)
23245 return &per_cu->cu->header;
23246
9c541725 23247 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23248
23249 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23250 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23251 rcuh_kind::COMPILE);
96408a79
SA
23252
23253 return cu_headerp;
23254}
23255
09ba997f 23256/* See read.h. */
ae0d2f24 23257
98714339 23258int
09ba997f 23259dwarf2_per_cu_data::addr_size () const
ae0d2f24 23260{
96408a79
SA
23261 struct comp_unit_head cu_header_local;
23262 const struct comp_unit_head *cu_headerp;
c471e790 23263
09ba997f 23264 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23265
23266 return cu_headerp->addr_size;
ae0d2f24
UW
23267}
23268
09ba997f 23269/* See read.h. */
9eae7c52
TT
23270
23271int
09ba997f 23272dwarf2_per_cu_data::offset_size () const
9eae7c52 23273{
96408a79
SA
23274 struct comp_unit_head cu_header_local;
23275 const struct comp_unit_head *cu_headerp;
9c6c53f7 23276
09ba997f 23277 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23278
23279 return cu_headerp->offset_size;
23280}
23281
09ba997f 23282/* See read.h. */
96408a79
SA
23283
23284int
09ba997f 23285dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23286{
23287 struct comp_unit_head cu_header_local;
23288 const struct comp_unit_head *cu_headerp;
23289
09ba997f 23290 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23291
23292 if (cu_headerp->version == 2)
23293 return cu_headerp->addr_size;
23294 else
23295 return cu_headerp->offset_size;
181cebd4
JK
23296}
23297
09ba997f 23298/* See read.h. */
9aa1f1e3
TT
23299
23300CORE_ADDR
09ba997f 23301dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23302{
09ba997f
TT
23303 struct objfile *objfile = dwarf2_per_objfile->objfile;
23304
23305 return objfile->text_section_offset ();
9aa1f1e3
TT
23306}
23307
09ba997f
TT
23308/* See read.h. */
23309
23310struct type *
23311dwarf2_per_cu_data::addr_type () const
9a49df9d 23312{
09ba997f 23313 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
23314 struct type *void_type = objfile_type (objfile)->builtin_void;
23315 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 23316 int addr_size = this->addr_size ();
9a49df9d
AB
23317
23318 if (TYPE_LENGTH (addr_type) == addr_size)
23319 return addr_type;
23320
09ba997f 23321 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23322 return addr_type;
23323}
23324
22b6cd70
TT
23325/* A helper function for dwarf2_find_containing_comp_unit that returns
23326 the index of the result, and that searches a vector. It will
23327 return a result even if the offset in question does not actually
23328 occur in any CU. This is separate so that it can be unit
23329 tested. */
ae038cb0 23330
22b6cd70
TT
23331static int
23332dwarf2_find_containing_comp_unit
23333 (sect_offset sect_off,
23334 unsigned int offset_in_dwz,
23335 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23336{
ae038cb0
DJ
23337 int low, high;
23338
ae038cb0 23339 low = 0;
22b6cd70 23340 high = all_comp_units.size () - 1;
ae038cb0
DJ
23341 while (high > low)
23342 {
36586728 23343 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23344 int mid = low + (high - low) / 2;
9a619af0 23345
22b6cd70 23346 mid_cu = all_comp_units[mid];
36586728 23347 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23348 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23349 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23350 high = mid;
23351 else
23352 low = mid + 1;
23353 }
23354 gdb_assert (low == high);
22b6cd70
TT
23355 return low;
23356}
23357
23358/* Locate the .debug_info compilation unit from CU's objfile which contains
23359 the DIE at OFFSET. Raises an error on failure. */
23360
23361static struct dwarf2_per_cu_data *
23362dwarf2_find_containing_comp_unit (sect_offset sect_off,
23363 unsigned int offset_in_dwz,
23364 struct dwarf2_per_objfile *dwarf2_per_objfile)
23365{
23366 int low
23367 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23368 dwarf2_per_objfile->all_comp_units);
23369 struct dwarf2_per_cu_data *this_cu
23370 = dwarf2_per_objfile->all_comp_units[low];
23371
45b8ae0c 23372 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23373 {
36586728 23374 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23375 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23376 "offset %s [in module %s]"),
23377 sect_offset_str (sect_off),
ed2dc618 23378 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23379
9c541725
PA
23380 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23381 <= sect_off);
ae038cb0
DJ
23382 return dwarf2_per_objfile->all_comp_units[low-1];
23383 }
23384 else
23385 {
b76e467d 23386 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 23387 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23388 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23389 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23390 return this_cu;
23391 }
23392}
23393
22b6cd70
TT
23394#if GDB_SELF_TEST
23395
23396namespace selftests {
23397namespace find_containing_comp_unit {
23398
23399static void
23400run_test ()
23401{
23402 struct dwarf2_per_cu_data one {};
23403 struct dwarf2_per_cu_data two {};
23404 struct dwarf2_per_cu_data three {};
23405 struct dwarf2_per_cu_data four {};
23406
23407 one.length = 5;
23408 two.sect_off = sect_offset (one.length);
23409 two.length = 7;
23410
23411 three.length = 5;
23412 three.is_dwz = 1;
23413 four.sect_off = sect_offset (three.length);
23414 four.length = 7;
23415 four.is_dwz = 1;
23416
23417 std::vector<dwarf2_per_cu_data *> units;
23418 units.push_back (&one);
23419 units.push_back (&two);
23420 units.push_back (&three);
23421 units.push_back (&four);
23422
23423 int result;
23424
23425 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23426 SELF_CHECK (units[result] == &one);
23427 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23428 SELF_CHECK (units[result] == &one);
23429 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23430 SELF_CHECK (units[result] == &two);
23431
23432 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23433 SELF_CHECK (units[result] == &three);
23434 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23435 SELF_CHECK (units[result] == &three);
23436 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23437 SELF_CHECK (units[result] == &four);
23438}
23439
23440}
23441}
23442
23443#endif /* GDB_SELF_TEST */
23444
23745b47 23445/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23446
fcd3b13d
SM
23447dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23448 : per_cu (per_cu_),
9068261f
AB
23449 mark (false),
23450 has_loclist (false),
23451 checked_producer (false),
23452 producer_is_gxx_lt_4_6 (false),
23453 producer_is_gcc_lt_4_3 (false),
eb77c9df 23454 producer_is_icc (false),
9068261f 23455 producer_is_icc_lt_14 (false),
c258c396 23456 producer_is_codewarrior (false),
9068261f 23457 processing_has_namespace_info (false)
93311388 23458{
fcd3b13d
SM
23459 per_cu->cu = this;
23460}
23461
23462/* Destroy a dwarf2_cu. */
23463
23464dwarf2_cu::~dwarf2_cu ()
23465{
23466 per_cu->cu = NULL;
9816fde3
JK
23467}
23468
23469/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23470
23471static void
95554aad
TT
23472prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23473 enum language pretend_language)
9816fde3
JK
23474{
23475 struct attribute *attr;
23476
23477 /* Set the language we're debugging. */
23478 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23479 if (attr != nullptr)
9816fde3
JK
23480 set_cu_language (DW_UNSND (attr), cu);
23481 else
9cded63f 23482 {
95554aad 23483 cu->language = pretend_language;
9cded63f
TT
23484 cu->language_defn = language_def (cu->language);
23485 }
dee91e82 23486
7d45c7c3 23487 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23488}
23489
ae038cb0
DJ
23490/* Increase the age counter on each cached compilation unit, and free
23491 any that are too old. */
23492
23493static void
ed2dc618 23494age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23495{
23496 struct dwarf2_per_cu_data *per_cu, **last_chain;
23497
23498 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23499 per_cu = dwarf2_per_objfile->read_in_chain;
23500 while (per_cu != NULL)
23501 {
23502 per_cu->cu->last_used ++;
b4f54984 23503 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23504 dwarf2_mark (per_cu->cu);
23505 per_cu = per_cu->cu->read_in_chain;
23506 }
23507
23508 per_cu = dwarf2_per_objfile->read_in_chain;
23509 last_chain = &dwarf2_per_objfile->read_in_chain;
23510 while (per_cu != NULL)
23511 {
23512 struct dwarf2_per_cu_data *next_cu;
23513
23514 next_cu = per_cu->cu->read_in_chain;
23515
23516 if (!per_cu->cu->mark)
23517 {
fcd3b13d 23518 delete per_cu->cu;
ae038cb0
DJ
23519 *last_chain = next_cu;
23520 }
23521 else
23522 last_chain = &per_cu->cu->read_in_chain;
23523
23524 per_cu = next_cu;
23525 }
23526}
23527
23528/* Remove a single compilation unit from the cache. */
23529
23530static void
dee91e82 23531free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23532{
23533 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23534 struct dwarf2_per_objfile *dwarf2_per_objfile
23535 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
23536
23537 per_cu = dwarf2_per_objfile->read_in_chain;
23538 last_chain = &dwarf2_per_objfile->read_in_chain;
23539 while (per_cu != NULL)
23540 {
23541 struct dwarf2_per_cu_data *next_cu;
23542
23543 next_cu = per_cu->cu->read_in_chain;
23544
dee91e82 23545 if (per_cu == target_per_cu)
ae038cb0 23546 {
fcd3b13d 23547 delete per_cu->cu;
dee91e82 23548 per_cu->cu = NULL;
ae038cb0
DJ
23549 *last_chain = next_cu;
23550 break;
23551 }
23552 else
23553 last_chain = &per_cu->cu->read_in_chain;
23554
23555 per_cu = next_cu;
23556 }
23557}
23558
dee91e82
DE
23559/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23560 We store these in a hash table separate from the DIEs, and preserve them
23561 when the DIEs are flushed out of cache.
23562
23563 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23564 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23565 or the type may come from a DWO file. Furthermore, while it's more logical
23566 to use per_cu->section+offset, with Fission the section with the data is in
23567 the DWO file but we don't know that section at the point we need it.
23568 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23569 because we can enter the lookup routine, get_die_type_at_offset, from
23570 outside this file, and thus won't necessarily have PER_CU->cu.
23571 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23572
dee91e82 23573struct dwarf2_per_cu_offset_and_type
1c379e20 23574{
dee91e82 23575 const struct dwarf2_per_cu_data *per_cu;
9c541725 23576 sect_offset sect_off;
1c379e20
DJ
23577 struct type *type;
23578};
23579
dee91e82 23580/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23581
23582static hashval_t
dee91e82 23583per_cu_offset_and_type_hash (const void *item)
1c379e20 23584{
9a3c8263
SM
23585 const struct dwarf2_per_cu_offset_and_type *ofs
23586 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23587
9c541725 23588 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23589}
23590
dee91e82 23591/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23592
23593static int
dee91e82 23594per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23595{
9a3c8263
SM
23596 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23597 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23598 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23599 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23600
dee91e82 23601 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23602 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23603}
23604
23605/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23606 table if necessary. For convenience, return TYPE.
23607
23608 The DIEs reading must have careful ordering to:
85102364 23609 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23610 reading current DIE.
23611 * Not trying to dereference contents of still incompletely read in types
23612 while reading in other DIEs.
23613 * Enable referencing still incompletely read in types just by a pointer to
23614 the type without accessing its fields.
23615
23616 Therefore caller should follow these rules:
23617 * Try to fetch any prerequisite types we may need to build this DIE type
23618 before building the type and calling set_die_type.
e71ec853 23619 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23620 possible before fetching more types to complete the current type.
23621 * Make the type as complete as possible before fetching more types. */
1c379e20 23622
f792889a 23623static struct type *
1c379e20
DJ
23624set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23625{
518817b3
SM
23626 struct dwarf2_per_objfile *dwarf2_per_objfile
23627 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23628 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23629 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23630 struct attribute *attr;
23631 struct dynamic_prop prop;
1c379e20 23632
b4ba55a1
JB
23633 /* For Ada types, make sure that the gnat-specific data is always
23634 initialized (if not already set). There are a few types where
23635 we should not be doing so, because the type-specific area is
23636 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23637 where the type-specific area is used to store the floatformat).
23638 But this is not a problem, because the gnat-specific information
23639 is actually not needed for these types. */
23640 if (need_gnat_info (cu)
78134374
SM
23641 && type->code () != TYPE_CODE_FUNC
23642 && type->code () != TYPE_CODE_FLT
23643 && type->code () != TYPE_CODE_METHODPTR
23644 && type->code () != TYPE_CODE_MEMBERPTR
23645 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23646 && !HAVE_GNAT_AUX_INFO (type))
23647 INIT_GNAT_SPECIFIC (type);
23648
3f2f83dd
KB
23649 /* Read DW_AT_allocated and set in type. */
23650 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23651 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23652 {
09ba997f 23653 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23654 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23655 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23656 }
23657 else if (attr != NULL)
23658 {
b98664d3 23659 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23660 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23661 sect_offset_str (die->sect_off));
3f2f83dd
KB
23662 }
23663
23664 /* Read DW_AT_associated and set in type. */
23665 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23666 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23667 {
09ba997f 23668 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23669 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23670 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23671 }
23672 else if (attr != NULL)
23673 {
b98664d3 23674 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23675 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23676 sect_offset_str (die->sect_off));
3f2f83dd
KB
23677 }
23678
3cdcd0ce
JB
23679 /* Read DW_AT_data_location and set in type. */
23680 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23681 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23682 cu->per_cu->addr_type ()))
5c54719c 23683 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23684
dee91e82 23685 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
23686 dwarf2_per_objfile->die_type_hash
23687 = htab_up (htab_create_alloc (127,
23688 per_cu_offset_and_type_hash,
23689 per_cu_offset_and_type_eq,
23690 NULL, xcalloc, xfree));
1c379e20 23691
dee91e82 23692 ofs.per_cu = cu->per_cu;
9c541725 23693 ofs.sect_off = die->sect_off;
1c379e20 23694 ofs.type = type;
dee91e82 23695 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 23696 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23697 if (*slot)
b98664d3 23698 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23699 sect_offset_str (die->sect_off));
8d749320
SM
23700 *slot = XOBNEW (&objfile->objfile_obstack,
23701 struct dwarf2_per_cu_offset_and_type);
1c379e20 23702 **slot = ofs;
f792889a 23703 return type;
1c379e20
DJ
23704}
23705
9c541725 23706/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23707 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23708
23709static struct type *
9c541725 23710get_die_type_at_offset (sect_offset sect_off,
673bfd45 23711 struct dwarf2_per_cu_data *per_cu)
1c379e20 23712{
dee91e82 23713 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23714 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23715
dee91e82 23716 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23717 return NULL;
1c379e20 23718
dee91e82 23719 ofs.per_cu = per_cu;
9c541725 23720 ofs.sect_off = sect_off;
9a3c8263 23721 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 23722 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23723 if (slot)
23724 return slot->type;
23725 else
23726 return NULL;
23727}
23728
02142a6c 23729/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23730 or return NULL if DIE does not have a saved type. */
23731
23732static struct type *
23733get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23734{
9c541725 23735 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23736}
23737
10b3939b
DJ
23738/* Add a dependence relationship from CU to REF_PER_CU. */
23739
23740static void
23741dwarf2_add_dependence (struct dwarf2_cu *cu,
23742 struct dwarf2_per_cu_data *ref_per_cu)
23743{
23744 void **slot;
23745
23746 if (cu->dependencies == NULL)
23747 cu->dependencies
23748 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23749 NULL, &cu->comp_unit_obstack,
23750 hashtab_obstack_allocate,
23751 dummy_obstack_deallocate);
23752
23753 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23754 if (*slot == NULL)
23755 *slot = ref_per_cu;
23756}
1c379e20 23757
f504f079
DE
23758/* Subroutine of dwarf2_mark to pass to htab_traverse.
23759 Set the mark field in every compilation unit in the
ae038cb0
DJ
23760 cache that we must keep because we are keeping CU. */
23761
10b3939b
DJ
23762static int
23763dwarf2_mark_helper (void **slot, void *data)
23764{
23765 struct dwarf2_per_cu_data *per_cu;
23766
23767 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23768
23769 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23770 reading of the chain. As such dependencies remain valid it is not much
23771 useful to track and undo them during QUIT cleanups. */
23772 if (per_cu->cu == NULL)
23773 return 1;
23774
10b3939b
DJ
23775 if (per_cu->cu->mark)
23776 return 1;
9068261f 23777 per_cu->cu->mark = true;
10b3939b
DJ
23778
23779 if (per_cu->cu->dependencies != NULL)
23780 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23781
23782 return 1;
23783}
23784
f504f079
DE
23785/* Set the mark field in CU and in every other compilation unit in the
23786 cache that we must keep because we are keeping CU. */
23787
ae038cb0
DJ
23788static void
23789dwarf2_mark (struct dwarf2_cu *cu)
23790{
23791 if (cu->mark)
23792 return;
9068261f 23793 cu->mark = true;
10b3939b
DJ
23794 if (cu->dependencies != NULL)
23795 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23796}
23797
23798static void
23799dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23800{
23801 while (per_cu)
23802 {
9068261f 23803 per_cu->cu->mark = false;
ae038cb0
DJ
23804 per_cu = per_cu->cu->read_in_chain;
23805 }
72bf9492
DJ
23806}
23807
72bf9492
DJ
23808/* Trivial hash function for partial_die_info: the hash value of a DIE
23809 is its offset in .debug_info for this objfile. */
23810
23811static hashval_t
23812partial_die_hash (const void *item)
23813{
9a3c8263
SM
23814 const struct partial_die_info *part_die
23815 = (const struct partial_die_info *) item;
9a619af0 23816
9c541725 23817 return to_underlying (part_die->sect_off);
72bf9492
DJ
23818}
23819
23820/* Trivial comparison function for partial_die_info structures: two DIEs
23821 are equal if they have the same offset. */
23822
23823static int
23824partial_die_eq (const void *item_lhs, const void *item_rhs)
23825{
9a3c8263
SM
23826 const struct partial_die_info *part_die_lhs
23827 = (const struct partial_die_info *) item_lhs;
23828 const struct partial_die_info *part_die_rhs
23829 = (const struct partial_die_info *) item_rhs;
9a619af0 23830
9c541725 23831 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23832}
23833
3c3bb058
AB
23834struct cmd_list_element *set_dwarf_cmdlist;
23835struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23836
9291a0cd 23837static void
cd4fb1b2
SM
23838show_check_physname (struct ui_file *file, int from_tty,
23839 struct cmd_list_element *c, const char *value)
9291a0cd 23840{
cd4fb1b2
SM
23841 fprintf_filtered (file,
23842 _("Whether to check \"physname\" is %s.\n"),
23843 value);
9291a0cd
TT
23844}
23845
6c265988 23846void _initialize_dwarf2_read ();
cd4fb1b2 23847void
6c265988 23848_initialize_dwarf2_read ()
9291a0cd 23849{
0743fc83 23850 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23851Set DWARF specific variables.\n\
590042fc 23852Configure DWARF variables such as the cache size."),
0743fc83
TT
23853 &set_dwarf_cmdlist, "maintenance set dwarf ",
23854 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23855
0743fc83 23856 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23857Show DWARF specific variables.\n\
23858Show DWARF variables such as the cache size."),
0743fc83
TT
23859 &show_dwarf_cmdlist, "maintenance show dwarf ",
23860 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23861
cd4fb1b2
SM
23862 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23863 &dwarf_max_cache_age, _("\
23864Set the upper bound on the age of cached DWARF compilation units."), _("\
23865Show the upper bound on the age of cached DWARF compilation units."), _("\
23866A higher limit means that cached compilation units will be stored\n\
23867in memory longer, and more total memory will be used. Zero disables\n\
23868caching, which can slow down startup."),
23869 NULL,
23870 show_dwarf_max_cache_age,
23871 &set_dwarf_cmdlist,
23872 &show_dwarf_cmdlist);
156942c7 23873
cd4fb1b2
SM
23874 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23875Set debugging of the DWARF reader."), _("\
23876Show debugging of the DWARF reader."), _("\
23877When enabled (non-zero), debugging messages are printed during DWARF\n\
23878reading and symtab expansion. A value of 1 (one) provides basic\n\
23879information. A value greater than 1 provides more verbose information."),
23880 NULL,
23881 NULL,
23882 &setdebuglist, &showdebuglist);
9291a0cd 23883
cd4fb1b2
SM
23884 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23885Set debugging of the DWARF DIE reader."), _("\
23886Show debugging of the DWARF DIE reader."), _("\
23887When enabled (non-zero), DIEs are dumped after they are read in.\n\
23888The value is the maximum depth to print."),
23889 NULL,
23890 NULL,
23891 &setdebuglist, &showdebuglist);
9291a0cd 23892
cd4fb1b2
SM
23893 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23894Set debugging of the dwarf line reader."), _("\
23895Show debugging of the dwarf line reader."), _("\
23896When enabled (non-zero), line number entries are dumped as they are read in.\n\
23897A value of 1 (one) provides basic information.\n\
23898A value greater than 1 provides more verbose information."),
23899 NULL,
23900 NULL,
23901 &setdebuglist, &showdebuglist);
437afbb8 23902
cd4fb1b2
SM
23903 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23904Set cross-checking of \"physname\" code against demangler."), _("\
23905Show cross-checking of \"physname\" code against demangler."), _("\
23906When enabled, GDB's internal \"physname\" code is checked against\n\
23907the demangler."),
23908 NULL, show_check_physname,
23909 &setdebuglist, &showdebuglist);
900e11f9 23910
e615022a
DE
23911 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23912 no_class, &use_deprecated_index_sections, _("\
23913Set whether to use deprecated gdb_index sections."), _("\
23914Show whether to use deprecated gdb_index sections."), _("\
23915When enabled, deprecated .gdb_index sections are used anyway.\n\
23916Normally they are ignored either because of a missing feature or\n\
23917performance issue.\n\
23918Warning: This option must be enabled before gdb reads the file."),
23919 NULL,
23920 NULL,
23921 &setlist, &showlist);
23922
f1e6e072
TT
23923 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23924 &dwarf2_locexpr_funcs);
23925 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23926 &dwarf2_loclist_funcs);
23927
23928 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23929 &dwarf2_block_frame_base_locexpr_funcs);
23930 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23931 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23932
23933#if GDB_SELF_TEST
23934 selftests::register_test ("dw2_expand_symtabs_matching",
23935 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23936 selftests::register_test ("dwarf2_find_containing_comp_unit",
23937 selftests::find_containing_comp_unit::run_test);
c62446b1 23938#endif
6502dd73 23939}
This page took 5.137922 seconds and 4 git commands to generate.