Move int type methods out of dwarf2_per_cu_data
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
41144253 117/* Size of .debug_loclists section header for 32-bit DWARF format. */
118#define LOCLIST_HEADER_SIZE32 12
119
120/* Size of .debug_loclists section header for 64-bit DWARF format. */
121#define LOCLIST_HEADER_SIZE64 20
122
3f563c84
PA
123/* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136struct name_component
137{
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146};
147
44ed8f3e
PA
148/* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151struct mapped_index_base
152{
22ca247e
TT
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
44ed8f3e
PA
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
3b00ef10
TT
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
44ed8f3e
PA
187
188 /* Prevent deleting/destroying via a base class pointer. */
189protected:
190 ~mapped_index_base() = default;
191};
192
9291a0cd
TT
193/* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
fc898b42 195struct mapped_index final : public mapped_index_base
9291a0cd 196{
f00a2de2
PA
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
559a7a62 204 /* Index data format version. */
3063847f 205 int version = 0;
559a7a62 206
f00a2de2
PA
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
b11b1f88 209
3876f04e 210 /* The symbol table, implemented as a hash table. */
f00a2de2 211 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 212
9291a0cd 213 /* A pointer to the constant pool. */
3063847f 214 const char *constant_pool = nullptr;
3f563c84 215
44ed8f3e
PA
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
9ab08412 219 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 220 }
5c58de74 221
3f563c84
PA
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
44ed8f3e 224 const char *symbol_name_at (offset_type idx) const override
f00a2de2 225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 226
44ed8f3e
PA
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
9291a0cd
TT
229};
230
927aa2e7
JK
231/* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
fc898b42 233struct mapped_debug_names final : public mapped_index_base
927aa2e7 234{
ed2dc618
SM
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
927aa2e7
JK
281};
282
cd4fb1b2 283/* See dwarf2read.h. */
ed2dc618 284
cd4fb1b2 285dwarf2_per_objfile *
ed2dc618
SM
286get_dwarf2_per_objfile (struct objfile *objfile)
287{
5bfd760d 288 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 289}
c906108c 290
251d32d9 291/* Default names of the debugging sections. */
c906108c 292
233a11ab
CS
293/* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
9cdd5dbd
DE
296static const struct dwarf2_debug_sections dwarf2_elf_names =
297{
251d32d9
TG
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
43988095 302 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 303 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 304 { ".debug_macro", ".zdebug_macro" },
251d32d9 305 { ".debug_str", ".zdebug_str" },
18a8505e 306 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 307 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 308 { ".debug_ranges", ".zdebug_ranges" },
43988095 309 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 310 { ".debug_types", ".zdebug_types" },
3019eac3 311 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
24d3216f 314 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 317 23
251d32d9 318};
c906108c 319
80626a55 320/* List of DWO/DWP sections. */
3019eac3 321
80626a55 322static const struct dwop_section_names
3019eac3
DE
323{
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
43988095 328 struct dwarf2_section_names loclists_dwo;
09262596
DE
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
3019eac3
DE
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
80626a55
DE
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
3019eac3 336}
80626a55 337dwop_section_names =
3019eac3
DE
338{
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
351};
352
c906108c
SS
353/* local data types */
354
41144253 355/* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357struct loclist_header
358{
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377};
378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
9e021579
SM
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
c24bdb02
KS
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
293e7e51
SM
424 /* Return a type that is a generic pointer type, the size of which
425 matches the address size given in the compilation unit header for
426 this CU. */
427 struct type *addr_type () const;
428
429 /* Find an integer type the same size as the address size given in
430 the compilation unit header for this CU. UNSIGNED_P controls if
431 the integer is unsigned or not. */
432 struct type *addr_sized_int_type (bool unsigned_p) const;
433
d00adf39 434 /* The header of the compilation unit. */
fcd3b13d 435 struct comp_unit_head header {};
e142c38c 436
d00adf39 437 /* Base address of this compilation unit. */
2b24b6e4 438 gdb::optional<CORE_ADDR> base_address;
d00adf39 439
e142c38c 440 /* The language we are debugging. */
fcd3b13d
SM
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
e142c38c 443
fcd3b13d 444 const char *producer = nullptr;
b0f35d58 445
c24bdb02 446private:
804d2729
TT
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
c24bdb02 449 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 450
c24bdb02 451public:
e142c38c
DJ
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
456
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
fcd3b13d 461 struct pending **list_in_scope = nullptr;
e142c38c 462
b64f50a1
JK
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 465 htab_t partial_dies = nullptr;
72bf9492
DJ
466
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
fcd3b13d 469 auto_obstack comp_unit_obstack;
72bf9492 470
ae038cb0
DJ
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
fcd3b13d 475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 476
69d751e3 477 /* Backlink to our per_cu entry. */
ae038cb0
DJ
478 struct dwarf2_per_cu_data *per_cu;
479
9e021579
SM
480 /* The dwarf2_per_objfile that owns this. */
481 struct dwarf2_per_objfile *per_objfile;
482
ae038cb0 483 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 484 int last_used = 0;
ae038cb0 485
b64f50a1
JK
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
fcd3b13d 488 htab_t die_hash = nullptr;
10b3939b
DJ
489
490 /* Full DIEs if read in. */
fcd3b13d 491 struct die_info *dies = nullptr;
10b3939b
DJ
492
493 /* A set of pointers to dwarf2_per_cu_data objects for compilation
494 units referenced by this one. Only set during full symbol processing;
495 partial symbol tables do not have dependencies. */
fcd3b13d 496 htab_t dependencies = nullptr;
10b3939b 497
cb1df416 498 /* Header data from the line table, during full symbol processing. */
fcd3b13d 499 struct line_header *line_header = nullptr;
4c8aa72d 500 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 501 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
502 this is the DW_TAG_compile_unit die for this CU. We'll hold on
503 to the line header as long as this DIE is being processed. See
504 process_die_scope. */
fcd3b13d 505 die_info *line_header_die_owner = nullptr;
cb1df416 506
3da10d80
KS
507 /* A list of methods which need to have physnames computed
508 after all type information has been read. */
c89b44cd 509 std::vector<delayed_method_info> method_list;
3da10d80 510
96408a79 511 /* To be copied to symtab->call_site_htab. */
fcd3b13d 512 htab_t call_site_htab = nullptr;
96408a79 513
034e5797
DE
514 /* Non-NULL if this CU came from a DWO file.
515 There is an invariant here that is important to remember:
516 Except for attributes copied from the top level DIE in the "main"
517 (or "stub") file in preparation for reading the DWO file
18a8505e 518 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
519 Either there isn't a DWO file (in which case this is NULL and the point
520 is moot), or there is and either we're not going to read it (in which
521 case this is NULL) or there is and we are reading it (in which case this
522 is non-NULL). */
fcd3b13d 523 struct dwo_unit *dwo_unit = nullptr;
3019eac3 524
18a8505e 525 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 526 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 527 gdb::optional<ULONGEST> addr_base;
3019eac3 528
18a8505e 529 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 530 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 531 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 536 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 538 ULONGEST ranges_base = 0;
2e3cf129 539
41144253 540 /* The DW_AT_loclists_base attribute if present. */
541 ULONGEST loclist_base = 0;
542
c9317f21
TT
543 /* When reading debug info generated by older versions of rustc, we
544 have to rewrite some union types to be struct types with a
545 variant part. This rewriting must be done after the CU is fully
546 read in, because otherwise at the point of rewriting some struct
547 type might not have been fully processed. So, we keep a list of
548 all such types here and process them after expansion. */
549 std::vector<struct type *> rust_unions;
550
18a8505e
AT
551 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
552 files, the value is implicitly zero. For DWARF 5 version DWO files, the
553 value is often implicit and is the size of the header of
554 .debug_str_offsets section (8 or 4, depending on the address size). */
555 gdb::optional<ULONGEST> str_offsets_base;
556
ae038cb0 557 /* Mark used when releasing cached dies. */
9068261f 558 bool mark : 1;
ae038cb0 559
8be455d7
JK
560 /* This CU references .debug_loc. See the symtab->locations_valid field.
561 This test is imperfect as there may exist optimized debug code not using
562 any location list and still facing inlining issues if handled as
563 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 564 bool has_loclist : 1;
ba919b58 565
9068261f 566 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
567 if all the producer_is_* fields are valid. This information is cached
568 because profiling CU expansion showed excessive time spent in
569 producer_is_gxx_lt_4_6. */
9068261f
AB
570 bool checked_producer : 1;
571 bool producer_is_gxx_lt_4_6 : 1;
572 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 573 bool producer_is_icc : 1;
9068261f 574 bool producer_is_icc_lt_14 : 1;
c258c396 575 bool producer_is_codewarrior : 1;
4d4ec4e5 576
9068261f 577 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
578 debugging info for C++ namespaces. GCC 3.3.x did not produce
579 this information, but later versions do. */
580
9068261f 581 bool processing_has_namespace_info : 1;
d590ff25
YQ
582
583 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
584
585 /* If this CU was inherited by another CU (via specification,
586 abstract_origin, etc), this is the ancestor CU. */
587 dwarf2_cu *ancestor;
588
589 /* Get the buildsym_compunit for this CU. */
590 buildsym_compunit *get_builder ()
591 {
592 /* If this CU has a builder associated with it, use that. */
593 if (m_builder != nullptr)
594 return m_builder.get ();
595
596 /* Otherwise, search ancestors for a valid builder. */
597 if (ancestor != nullptr)
598 return ancestor->get_builder ();
599
600 return nullptr;
601 }
e7c27a73
DJ
602};
603
094b34ac
DE
604/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
605 This includes type_unit_group and quick_file_names. */
606
607struct stmt_list_hash
608{
609 /* The DWO unit this table is from or NULL if there is none. */
610 struct dwo_unit *dwo_unit;
611
612 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 613 sect_offset line_sect_off;
094b34ac
DE
614};
615
5989a64e 616/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
f4dc4d17
DE
617 an object of this type. */
618
619struct type_unit_group
620{
0186c6a7 621 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
622 To simplify things we create an artificial CU that "includes" all the
623 type units using this stmt_list so that the rest of the code still has
197400e8 624 a "per_cu" handle on the symtab. */
094b34ac
DE
625 struct dwarf2_per_cu_data per_cu;
626
0186c6a7
DE
627 /* The TUs that share this DW_AT_stmt_list entry.
628 This is added to while parsing type units to build partial symtabs,
629 and is deleted afterwards and not used again. */
a8b3b8e9 630 std::vector<signatured_type *> *tus;
f4dc4d17 631
43f3e411 632 /* The compunit symtab.
094b34ac 633 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
634 so we create an essentially anonymous symtab as the compunit symtab. */
635 struct compunit_symtab *compunit_symtab;
f4dc4d17 636
094b34ac
DE
637 /* The data used to construct the hash key. */
638 struct stmt_list_hash hash;
f4dc4d17 639
f4dc4d17
DE
640 /* The symbol tables for this TU (obtained from the files listed in
641 DW_AT_stmt_list).
642 WARNING: The order of entries here must match the order of entries
643 in the line header. After the first TU using this type_unit_group, the
644 line header for the subsequent TUs is recreated from this. This is done
645 because we need to use the same symtabs for each TU using the same
646 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
647 there's no guarantee the line header doesn't have duplicate entries. */
648 struct symtab **symtabs;
649};
650
73869dc2 651/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
652
653struct dwo_sections
654{
655 struct dwarf2_section_info abbrev;
3019eac3
DE
656 struct dwarf2_section_info line;
657 struct dwarf2_section_info loc;
43988095 658 struct dwarf2_section_info loclists;
09262596
DE
659 struct dwarf2_section_info macinfo;
660 struct dwarf2_section_info macro;
3019eac3
DE
661 struct dwarf2_section_info str;
662 struct dwarf2_section_info str_offsets;
80626a55
DE
663 /* In the case of a virtual DWO file, these two are unused. */
664 struct dwarf2_section_info info;
fd5866f6 665 std::vector<dwarf2_section_info> types;
3019eac3
DE
666};
667
c88ee1f0 668/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
669
670struct dwo_unit
671{
672 /* Backlink to the containing struct dwo_file. */
673 struct dwo_file *dwo_file;
674
675 /* The "id" that distinguishes this CU/TU.
676 .debug_info calls this "dwo_id", .debug_types calls this "signature".
677 Since signatures came first, we stick with it for consistency. */
678 ULONGEST signature;
679
680 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 681 struct dwarf2_section_info *section;
3019eac3 682
9c541725
PA
683 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
684 sect_offset sect_off;
3019eac3
DE
685 unsigned int length;
686
687 /* For types, offset in the type's DIE of the type defined by this TU. */
688 cu_offset type_offset_in_tu;
689};
690
73869dc2
DE
691/* include/dwarf2.h defines the DWP section codes.
692 It defines a max value but it doesn't define a min value, which we
693 use for error checking, so provide one. */
694
695enum dwp_v2_section_ids
696{
697 DW_SECT_MIN = 1
698};
699
80626a55 700/* Data for one DWO file.
57d63ce2
DE
701
702 This includes virtual DWO files (a virtual DWO file is a DWO file as it
703 appears in a DWP file). DWP files don't really have DWO files per se -
704 comdat folding of types "loses" the DWO file they came from, and from
705 a high level view DWP files appear to contain a mass of random types.
706 However, to maintain consistency with the non-DWP case we pretend DWP
707 files contain virtual DWO files, and we assign each TU with one virtual
708 DWO file (generally based on the line and abbrev section offsets -
709 a heuristic that seems to work in practice). */
3019eac3
DE
710
711struct dwo_file
712{
51ac9db5
SM
713 dwo_file () = default;
714 DISABLE_COPY_AND_ASSIGN (dwo_file);
715
18a8505e 716 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
717 For virtual DWO files the name is constructed from the section offsets
718 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
719 from related CU+TUs. */
51ac9db5 720 const char *dwo_name = nullptr;
0ac5b59e
DE
721
722 /* The DW_AT_comp_dir attribute. */
51ac9db5 723 const char *comp_dir = nullptr;
3019eac3 724
80626a55
DE
725 /* The bfd, when the file is open. Otherwise this is NULL.
726 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 727 gdb_bfd_ref_ptr dbfd;
3019eac3 728
73869dc2
DE
729 /* The sections that make up this DWO file.
730 Remember that for virtual DWO files in DWP V2, these are virtual
731 sections (for lack of a better name). */
51ac9db5 732 struct dwo_sections sections {};
3019eac3 733
33c5cd75
DB
734 /* The CUs in the file.
735 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
736 an extension to handle LLVM's Link Time Optimization output (where
737 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 738 htab_up cus;
3019eac3
DE
739
740 /* Table of TUs in the file.
741 Each element is a struct dwo_unit. */
b0b6a987 742 htab_up tus;
3019eac3
DE
743};
744
80626a55
DE
745/* These sections are what may appear in a DWP file. */
746
747struct dwp_sections
748{
73869dc2 749 /* These are used by both DWP version 1 and 2. */
80626a55
DE
750 struct dwarf2_section_info str;
751 struct dwarf2_section_info cu_index;
752 struct dwarf2_section_info tu_index;
73869dc2
DE
753
754 /* These are only used by DWP version 2 files.
755 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
756 sections are referenced by section number, and are not recorded here.
757 In DWP version 2 there is at most one copy of all these sections, each
758 section being (effectively) comprised of the concatenation of all of the
759 individual sections that exist in the version 1 format.
760 To keep the code simple we treat each of these concatenated pieces as a
761 section itself (a virtual section?). */
762 struct dwarf2_section_info abbrev;
763 struct dwarf2_section_info info;
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 struct dwarf2_section_info types;
80626a55
DE
770};
771
73869dc2
DE
772/* These sections are what may appear in a virtual DWO file in DWP version 1.
773 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 774
73869dc2 775struct virtual_v1_dwo_sections
80626a55
DE
776{
777 struct dwarf2_section_info abbrev;
778 struct dwarf2_section_info line;
779 struct dwarf2_section_info loc;
780 struct dwarf2_section_info macinfo;
781 struct dwarf2_section_info macro;
782 struct dwarf2_section_info str_offsets;
783 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 784 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
785 struct dwarf2_section_info info_or_types;
786};
787
73869dc2
DE
788/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
789 In version 2, the sections of the DWO files are concatenated together
790 and stored in one section of that name. Thus each ELF section contains
791 several "virtual" sections. */
792
793struct virtual_v2_dwo_sections
794{
795 bfd_size_type abbrev_offset;
796 bfd_size_type abbrev_size;
797
798 bfd_size_type line_offset;
799 bfd_size_type line_size;
800
801 bfd_size_type loc_offset;
802 bfd_size_type loc_size;
803
804 bfd_size_type macinfo_offset;
805 bfd_size_type macinfo_size;
806
807 bfd_size_type macro_offset;
808 bfd_size_type macro_size;
809
810 bfd_size_type str_offsets_offset;
811 bfd_size_type str_offsets_size;
812
813 /* Each DWP hash table entry records one CU or one TU.
814 That is recorded here, and copied to dwo_unit.section. */
815 bfd_size_type info_or_types_offset;
816 bfd_size_type info_or_types_size;
817};
818
80626a55
DE
819/* Contents of DWP hash tables. */
820
821struct dwp_hash_table
822{
73869dc2 823 uint32_t version, nr_columns;
80626a55 824 uint32_t nr_units, nr_slots;
73869dc2
DE
825 const gdb_byte *hash_table, *unit_table;
826 union
827 {
828 struct
829 {
830 const gdb_byte *indices;
831 } v1;
832 struct
833 {
834 /* This is indexed by column number and gives the id of the section
835 in that column. */
836#define MAX_NR_V2_DWO_SECTIONS \
837 (1 /* .debug_info or .debug_types */ \
838 + 1 /* .debug_abbrev */ \
839 + 1 /* .debug_line */ \
840 + 1 /* .debug_loc */ \
841 + 1 /* .debug_str_offsets */ \
842 + 1 /* .debug_macro or .debug_macinfo */)
843 int section_ids[MAX_NR_V2_DWO_SECTIONS];
844 const gdb_byte *offsets;
845 const gdb_byte *sizes;
846 } v2;
847 } section_pool;
80626a55
DE
848};
849
850/* Data for one DWP file. */
851
852struct dwp_file
853{
400174b1
TT
854 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
855 : name (name_),
856 dbfd (std::move (abfd))
857 {
858 }
859
80626a55
DE
860 /* Name of the file. */
861 const char *name;
862
73869dc2 863 /* File format version. */
400174b1 864 int version = 0;
73869dc2 865
93417882 866 /* The bfd. */
400174b1 867 gdb_bfd_ref_ptr dbfd;
80626a55
DE
868
869 /* Section info for this file. */
400174b1 870 struct dwp_sections sections {};
80626a55 871
57d63ce2 872 /* Table of CUs in the file. */
400174b1 873 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
874
875 /* Table of TUs in the file. */
400174b1 876 const struct dwp_hash_table *tus = nullptr;
80626a55 877
19ac8c2e 878 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
879 htab_up loaded_cus;
880 htab_up loaded_tus;
80626a55 881
73869dc2
DE
882 /* Table to map ELF section numbers to their sections.
883 This is only needed for the DWP V1 file format. */
400174b1
TT
884 unsigned int num_sections = 0;
885 asection **elf_sections = nullptr;
80626a55
DE
886};
887
0963b4bd
MS
888/* Struct used to pass misc. parameters to read_die_and_children, et
889 al. which are used for both .debug_info and .debug_types dies.
890 All parameters here are unchanging for the life of the call. This
dee91e82 891 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
892
893struct die_reader_specs
894{
a32a8923 895 /* The bfd of die_section. */
93311388
DE
896 bfd* abfd;
897
898 /* The CU of the DIE we are parsing. */
899 struct dwarf2_cu *cu;
900
80626a55 901 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
902 struct dwo_file *dwo_file;
903
dee91e82 904 /* The section the die comes from.
3019eac3 905 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
906 struct dwarf2_section_info *die_section;
907
908 /* die_section->buffer. */
d521ce57 909 const gdb_byte *buffer;
f664829e
DE
910
911 /* The end of the buffer. */
912 const gdb_byte *buffer_end;
a2ce51a0 913
685af9cd
TT
914 /* The abbreviation table to use when reading the DIEs. */
915 struct abbrev_table *abbrev_table;
93311388
DE
916};
917
c0ab21c2
TT
918/* A subclass of die_reader_specs that holds storage and has complex
919 constructor and destructor behavior. */
920
921class cutu_reader : public die_reader_specs
922{
923public:
924
ab432490
SM
925 cutu_reader (dwarf2_per_cu_data *this_cu,
926 dwarf2_per_objfile *per_objfile,
c0ab21c2 927 struct abbrev_table *abbrev_table,
6751ebae 928 int use_existing_cu,
c0ab21c2
TT
929 bool skip_partial);
930
931 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 932 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
933 struct dwarf2_cu *parent_cu = nullptr,
934 struct dwo_file *dwo_file = nullptr);
935
c0ab21c2
TT
936 DISABLE_COPY_AND_ASSIGN (cutu_reader);
937
938 const gdb_byte *info_ptr = nullptr;
939 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
940 bool dummy_p = false;
941
6751ebae
TT
942 /* Release the new CU, putting it on the chain. This cannot be done
943 for dummy CUs. */
944 void keep ();
945
c0ab21c2 946private:
9e021579
SM
947 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
948 dwarf2_per_objfile *per_objfile,
6751ebae 949 int use_existing_cu);
c0ab21c2
TT
950
951 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
952 std::unique_ptr<dwarf2_cu> m_new_cu;
953
954 /* The ordinary abbreviation table. */
955 abbrev_table_up m_abbrev_table_holder;
956
957 /* The DWO abbreviation table. */
958 abbrev_table_up m_dwo_abbrev_table;
959};
dee91e82 960
c906108c 961/* When we construct a partial symbol table entry we only
0963b4bd 962 need this much information. */
6f06d47b 963struct partial_die_info : public allocate_on_obstack
c906108c 964 {
6f06d47b
YQ
965 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
966
967 /* Disable assign but still keep copy ctor, which is needed
968 load_partial_dies. */
969 partial_die_info& operator=(const partial_die_info& rhs) = delete;
970
52356b79
YQ
971 /* Adjust the partial die before generating a symbol for it. This
972 function may set the is_external flag or change the DIE's
973 name. */
974 void fixup (struct dwarf2_cu *cu);
975
48fbe735
YQ
976 /* Read a minimal amount of information into the minimal die
977 structure. */
978 const gdb_byte *read (const struct die_reader_specs *reader,
979 const struct abbrev_info &abbrev,
980 const gdb_byte *info_ptr);
981
72bf9492 982 /* Offset of this DIE. */
6f06d47b 983 const sect_offset sect_off;
72bf9492
DJ
984
985 /* DWARF-2 tag for this DIE. */
6f06d47b 986 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 987
72bf9492 988 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
989 const unsigned int has_children : 1;
990
72bf9492
DJ
991 unsigned int is_external : 1;
992 unsigned int is_declaration : 1;
993 unsigned int has_type : 1;
994 unsigned int has_specification : 1;
995 unsigned int has_pc_info : 1;
481860b3 996 unsigned int may_be_inlined : 1;
72bf9492 997
0c1b455e
TT
998 /* This DIE has been marked DW_AT_main_subprogram. */
999 unsigned int main_subprogram : 1;
1000
72bf9492
DJ
1001 /* Flag set if the SCOPE field of this structure has been
1002 computed. */
1003 unsigned int scope_set : 1;
1004
fa4028e9
JB
1005 /* Flag set if the DIE has a byte_size attribute. */
1006 unsigned int has_byte_size : 1;
1007
ff908ebf
AW
1008 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1009 unsigned int has_const_value : 1;
1010
98bfdba5
PA
1011 /* Flag set if any of the DIE's children are template arguments. */
1012 unsigned int has_template_arguments : 1;
1013
52356b79 1014 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1015 unsigned int fixup_called : 1;
1016
36586728
TT
1017 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1018 unsigned int is_dwz : 1;
1019
1020 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1021 unsigned int spec_is_dwz : 1;
1022
72bf9492 1023 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1024 sometimes a default name for unnamed DIEs. */
6f06d47b 1025 const char *name = nullptr;
72bf9492 1026
abc72ce4 1027 /* The linkage name, if present. */
6f06d47b 1028 const char *linkage_name = nullptr;
abc72ce4 1029
72bf9492
DJ
1030 /* The scope to prepend to our children. This is generally
1031 allocated on the comp_unit_obstack, so will disappear
1032 when this compilation unit leaves the cache. */
6f06d47b 1033 const char *scope = nullptr;
72bf9492 1034
95554aad
TT
1035 /* Some data associated with the partial DIE. The tag determines
1036 which field is live. */
1037 union
1038 {
1039 /* The location description associated with this DIE, if any. */
1040 struct dwarf_block *locdesc;
1041 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1042 sect_offset sect_off;
6f06d47b 1043 } d {};
72bf9492
DJ
1044
1045 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1046 CORE_ADDR lowpc = 0;
1047 CORE_ADDR highpc = 0;
72bf9492 1048
93311388 1049 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1050 DW_AT_sibling, if any. */
48fbe735
YQ
1051 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1052 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1053 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1054
1055 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1056 DW_AT_specification (or DW_AT_abstract_origin or
1057 DW_AT_extension). */
6f06d47b 1058 sect_offset spec_offset {};
72bf9492
DJ
1059
1060 /* Pointers to this DIE's parent, first child, and next sibling,
1061 if any. */
6f06d47b
YQ
1062 struct partial_die_info *die_parent = nullptr;
1063 struct partial_die_info *die_child = nullptr;
1064 struct partial_die_info *die_sibling = nullptr;
1065
1066 friend struct partial_die_info *
1067 dwarf2_cu::find_partial_die (sect_offset sect_off);
1068
1069 private:
1070 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1071 partial_die_info (sect_offset sect_off)
1072 : partial_die_info (sect_off, DW_TAG_padding, 0)
1073 {
1074 }
1075
1076 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1077 int has_children_)
1078 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1079 {
1080 is_external = 0;
1081 is_declaration = 0;
1082 has_type = 0;
1083 has_specification = 0;
1084 has_pc_info = 0;
1085 may_be_inlined = 0;
1086 main_subprogram = 0;
1087 scope_set = 0;
1088 has_byte_size = 0;
1089 has_const_value = 0;
1090 has_template_arguments = 0;
1091 fixup_called = 0;
1092 is_dwz = 0;
1093 spec_is_dwz = 0;
1094 }
c906108c
SS
1095 };
1096
c906108c
SS
1097/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1098 but this would require a corresponding change in unpack_field_as_long
1099 and friends. */
1100static int bits_per_byte = 8;
1101
9c6a1327
TT
1102struct variant_part_builder;
1103
1104/* When reading a variant, we track a bit more information about the
1105 field, and store it in an object of this type. */
2ddeaf8a
TT
1106
1107struct variant_field
1108{
9c6a1327
TT
1109 int first_field = -1;
1110 int last_field = -1;
1111
1112 /* A variant can contain other variant parts. */
1113 std::vector<variant_part_builder> variant_parts;
1114
2ddeaf8a
TT
1115 /* If we see a DW_TAG_variant, then this will be set if this is the
1116 default branch. */
9c6a1327
TT
1117 bool default_branch = false;
1118 /* If we see a DW_AT_discr_value, then this will be the discriminant
1119 value. */
1120 ULONGEST discriminant_value = 0;
1121 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1122 data. */
1123 struct dwarf_block *discr_list_data = nullptr;
1124};
1125
1126/* This represents a DW_TAG_variant_part. */
1127
1128struct variant_part_builder
1129{
1130 /* The offset of the discriminant field. */
1131 sect_offset discriminant_offset {};
1132
1133 /* Variants that are direct children of this variant part. */
1134 std::vector<variant_field> variants;
1135
1136 /* True if we're currently reading a variant. */
1137 bool processing_variant = false;
2ddeaf8a
TT
1138};
1139
52059ffd
TT
1140struct nextfield
1141{
be2daae6
TT
1142 int accessibility = 0;
1143 int virtuality = 0;
9c6a1327
TT
1144 /* Variant parts need to find the discriminant, which is a DIE
1145 reference. We track the section offset of each field to make
1146 this link. */
1147 sect_offset offset;
be2daae6 1148 struct field field {};
52059ffd
TT
1149};
1150
1151struct fnfieldlist
1152{
be2daae6
TT
1153 const char *name = nullptr;
1154 std::vector<struct fn_field> fnfields;
52059ffd
TT
1155};
1156
c906108c
SS
1157/* The routines that read and process dies for a C struct or C++ class
1158 pass lists of data member fields and lists of member function fields
1159 in an instance of a field_info structure, as defined below. */
1160struct field_info
c5aa993b 1161 {
0963b4bd 1162 /* List of data member and baseclasses fields. */
be2daae6
TT
1163 std::vector<struct nextfield> fields;
1164 std::vector<struct nextfield> baseclasses;
c906108c 1165
85102364 1166 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1167 int non_public_fields = 0;
c906108c 1168
c5aa993b
JM
1169 /* Member function fieldlist array, contains name of possibly overloaded
1170 member function, number of overloaded member functions and a pointer
1171 to the head of the member function field chain. */
be2daae6 1172 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1173
1174 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1175 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1176 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1177
1178 /* Nested types defined by this class and the number of elements in this
1179 list. */
be2daae6 1180 std::vector<struct decl_field> nested_types_list;
317f7127 1181
9c6a1327
TT
1182 /* If non-null, this is the variant part we are currently
1183 reading. */
1184 variant_part_builder *current_variant_part = nullptr;
1185 /* This holds all the top-level variant parts attached to the type
1186 we're reading. */
1187 std::vector<variant_part_builder> variant_parts;
1188
317f7127
TT
1189 /* Return the total number of fields (including baseclasses). */
1190 int nfields () const
1191 {
1192 return fields.size () + baseclasses.size ();
1193 }
c5aa993b 1194 };
c906108c 1195
ae038cb0
DJ
1196/* Loaded secondary compilation units are kept in memory until they
1197 have not been referenced for the processing of this many
1198 compilation units. Set this to zero to disable caching. Cache
1199 sizes of up to at least twenty will improve startup time for
1200 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1201static int dwarf_max_cache_age = 5;
920d2a44 1202static void
b4f54984
DE
1203show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1204 struct cmd_list_element *c, const char *value)
920d2a44 1205{
3e43a32a 1206 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1207 "DWARF compilation units is %s.\n"),
920d2a44
AC
1208 value);
1209}
4390d890 1210\f
c906108c
SS
1211/* local function prototypes */
1212
918dd910
JK
1213static void dwarf2_find_base_address (struct die_info *die,
1214 struct dwarf2_cu *cu);
1215
891813be 1216static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1217 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1218 const char *name);
0018ea6f 1219
f1902523
JK
1220static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1221 const gdb_byte *info_ptr,
3e225074 1222 struct die_info *type_unit_die);
f1902523 1223
ed2dc618
SM
1224static void dwarf2_build_psymtabs_hard
1225 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1226
72bf9492
DJ
1227static void scan_partial_symbols (struct partial_die_info *,
1228 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1229 int, struct dwarf2_cu *);
c906108c 1230
72bf9492
DJ
1231static void add_partial_symbol (struct partial_die_info *,
1232 struct dwarf2_cu *);
63d06c5c 1233
72bf9492
DJ
1234static void add_partial_namespace (struct partial_die_info *pdi,
1235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1236 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1237
5d7cb8df 1238static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1239 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1240 struct dwarf2_cu *cu);
1241
72bf9492
DJ
1242static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1243 struct dwarf2_cu *cu);
91c24f0a 1244
bc30ff58
JB
1245static void add_partial_subprogram (struct partial_die_info *pdi,
1246 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1247 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1248
d521ce57 1249static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1250
dee91e82 1251static struct partial_die_info *load_partial_dies
d521ce57 1252 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1253
fb816e8b
TV
1254/* A pair of partial_die_info and compilation unit. */
1255struct cu_partial_die_info
1256{
1257 /* The compilation unit of the partial_die_info. */
1258 struct dwarf2_cu *cu;
1259 /* A partial_die_info. */
1260 struct partial_die_info *pdi;
122cf0f2
AB
1261
1262 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1263 : cu (cu),
1264 pdi (pdi)
405feb71 1265 { /* Nothing. */ }
122cf0f2
AB
1266
1267private:
1268 cu_partial_die_info () = delete;
fb816e8b
TV
1269};
1270
122cf0f2
AB
1271static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1272 struct dwarf2_cu *);
72bf9492 1273
d521ce57
TT
1274static const gdb_byte *read_attribute (const struct die_reader_specs *,
1275 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1276 const gdb_byte *, bool *need_reprocess);
1277
1278static void read_attribute_reprocess (const struct die_reader_specs *reader,
1279 struct attribute *attr);
1280
1281static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1282
ed2dc618
SM
1283static sect_offset read_abbrev_offset
1284 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1285 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1286
ed2dc618
SM
1287static const char *read_indirect_string
1288 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1289 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1290
ed2dc618 1291static const char *read_indirect_string_at_offset
4f44ae6c 1292 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1293
d521ce57
TT
1294static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1295 const gdb_byte *,
3019eac3
DE
1296 unsigned int *);
1297
18a8505e
AT
1298static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1299 ULONGEST str_index);
1300
1301static const char *read_stub_str_index (struct dwarf2_cu *cu,
1302 ULONGEST str_index);
3019eac3 1303
e142c38c 1304static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1305
e142c38c
DJ
1306static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1307 struct dwarf2_cu *);
c906108c 1308
7d45c7c3
KB
1309static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1310 struct dwarf2_cu *cu);
1311
a084a2a6
AT
1312static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1313
05cf31d1
JB
1314static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1315 struct dwarf2_cu *cu);
1316
e142c38c 1317static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1318
e142c38c 1319static struct die_info *die_specification (struct die_info *die,
f2f0e013 1320 struct dwarf2_cu **);
63d06c5c 1321
9c541725 1322static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1323 struct dwarf2_cu *cu);
debd256d 1324
f3f5162e 1325static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1326 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1327 CORE_ADDR, int decode_mapping);
c906108c 1328
804d2729
TT
1329static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1330 const char *);
c906108c 1331
a14ed312 1332static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1333 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1334
ff39bb5e 1335static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1336 struct dwarf2_cu *);
c906108c 1337
ff39bb5e 1338static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1339 struct type *type,
1340 const char *name,
1341 struct obstack *obstack,
12df843f 1342 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1343 const gdb_byte **bytes,
98bfdba5 1344 struct dwarf2_locexpr_baton **baton);
2df3850c 1345
e7c27a73 1346static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1347
b4ba55a1
JB
1348static int need_gnat_info (struct dwarf2_cu *);
1349
3e43a32a
MS
1350static struct type *die_descriptive_type (struct die_info *,
1351 struct dwarf2_cu *);
b4ba55a1
JB
1352
1353static void set_descriptive_type (struct type *, struct die_info *,
1354 struct dwarf2_cu *);
1355
e7c27a73
DJ
1356static struct type *die_containing_type (struct die_info *,
1357 struct dwarf2_cu *);
c906108c 1358
ff39bb5e 1359static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1360 struct dwarf2_cu *);
c906108c 1361
f792889a 1362static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1363
673bfd45
DE
1364static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1365
0d5cff50 1366static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1367
6e70227d 1368static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1369 const char *suffix, int physname,
1370 struct dwarf2_cu *cu);
63d06c5c 1371
e7c27a73 1372static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1373
348e048f
DE
1374static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1375
e7c27a73 1376static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1377
e7c27a73 1378static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1379
96408a79
SA
1380static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1381
71a3c369
TT
1382static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1383
ff013f42 1384static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1385 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1386
41144253 1387/* Return the .debug_loclists section to use for cu. */
1388static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1389
3a2b436a 1390/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1391 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1392enum pc_bounds_kind
1393{
e385593e 1394 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1395 PC_BOUNDS_NOT_PRESENT,
1396
e385593e
JK
1397 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1398 were present but they do not form a valid range of PC addresses. */
1399 PC_BOUNDS_INVALID,
1400
3a2b436a
JK
1401 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1402 PC_BOUNDS_RANGES,
1403
1404 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1405 PC_BOUNDS_HIGH_LOW,
1406};
1407
1408static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1409 CORE_ADDR *, CORE_ADDR *,
1410 struct dwarf2_cu *,
891813be 1411 dwarf2_psymtab *);
c906108c 1412
fae299cd
DC
1413static void get_scope_pc_bounds (struct die_info *,
1414 CORE_ADDR *, CORE_ADDR *,
1415 struct dwarf2_cu *);
1416
801e3a5b
JB
1417static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1418 CORE_ADDR, struct dwarf2_cu *);
1419
a14ed312 1420static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1421 struct dwarf2_cu *);
c906108c 1422
a14ed312 1423static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1424 struct type *, struct dwarf2_cu *);
c906108c 1425
a14ed312 1426static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1427 struct die_info *, struct type *,
e7c27a73 1428 struct dwarf2_cu *);
c906108c 1429
a14ed312 1430static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1431 struct type *,
1432 struct dwarf2_cu *);
c906108c 1433
134d01f1 1434static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1435
e7c27a73 1436static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1437
e7c27a73 1438static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1439
5d7cb8df
JK
1440static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1441
804d2729 1442static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1443
27aa8d6a
SW
1444static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1445
74921315
KS
1446static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1447
f55ee35c
JK
1448static struct type *read_module_type (struct die_info *die,
1449 struct dwarf2_cu *cu);
1450
38d518c9 1451static const char *namespace_name (struct die_info *die,
e142c38c 1452 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1453
134d01f1 1454static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1455
7d79de9a
TT
1456static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1457 bool * = nullptr);
c906108c 1458
6e70227d 1459static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1460 struct dwarf2_cu *);
1461
bf6af496 1462static struct die_info *read_die_and_siblings_1
d521ce57 1463 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1464 struct die_info *);
639d11d3 1465
dee91e82 1466static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1467 const gdb_byte *info_ptr,
1468 const gdb_byte **new_info_ptr,
639d11d3
DC
1469 struct die_info *parent);
1470
d521ce57
TT
1471static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1472 struct die_info **, const gdb_byte *,
3e225074 1473 int);
3019eac3 1474
d521ce57 1475static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1476 struct die_info **, const gdb_byte *);
93311388 1477
e7c27a73 1478static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1479
15d034d0 1480static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1481 struct objfile *);
71c25dea 1482
15d034d0 1483static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1484
15d034d0 1485static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1486 struct die_info *die,
1487 struct dwarf2_cu *cu);
1488
ca69b9e6
DE
1489static const char *dwarf2_physname (const char *name, struct die_info *die,
1490 struct dwarf2_cu *cu);
1491
e142c38c 1492static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1493 struct dwarf2_cu **);
9219021c 1494
d97bc12b
DE
1495static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1496
1497static void dump_die_for_error (struct die_info *);
1498
1499static void dump_die_1 (struct ui_file *, int level, int max_level,
1500 struct die_info *);
c906108c 1501
d97bc12b 1502/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1503
51545339 1504static void store_in_ref_table (struct die_info *,
10b3939b 1505 struct dwarf2_cu *);
c906108c 1506
348e048f 1507static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1508 const struct attribute *,
348e048f
DE
1509 struct dwarf2_cu **);
1510
10b3939b 1511static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1512 const struct attribute *,
f2f0e013 1513 struct dwarf2_cu **);
c906108c 1514
348e048f 1515static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1516 const struct attribute *,
348e048f
DE
1517 struct dwarf2_cu **);
1518
ac9ec31b
DE
1519static struct type *get_signatured_type (struct die_info *, ULONGEST,
1520 struct dwarf2_cu *);
1521
1522static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1523 const struct attribute *,
ac9ec31b
DE
1524 struct dwarf2_cu *);
1525
ab432490
SM
1526static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1527 dwarf2_per_objfile *per_objfile);
348e048f 1528
ab432490
SM
1529static void read_signatured_type (signatured_type *sig_type,
1530 dwarf2_per_objfile *per_objfile);
348e048f 1531
63e43d3a
PMR
1532static int attr_to_dynamic_prop (const struct attribute *attr,
1533 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1534 struct dynamic_prop *prop, struct type *type);
63e43d3a 1535
c906108c
SS
1536/* memory allocation interface */
1537
7b5a2f43 1538static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1539
b60c80d6 1540static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1541
43f3e411 1542static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1543
8cf6f0b1
TT
1544static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1545 struct dwarf2_loclist_baton *baton,
ff39bb5e 1546 const struct attribute *attr);
8cf6f0b1 1547
ff39bb5e 1548static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1549 struct symbol *sym,
f1e6e072
TT
1550 struct dwarf2_cu *cu,
1551 int is_block);
4c2df51b 1552
d521ce57
TT
1553static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1554 const gdb_byte *info_ptr,
1555 struct abbrev_info *abbrev);
4bb7a0a7 1556
72bf9492
DJ
1557static hashval_t partial_die_hash (const void *item);
1558
1559static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1560
ae038cb0 1561static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1562 (sect_offset sect_off, unsigned int offset_in_dwz,
1563 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1564
9816fde3 1565static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1566 struct die_info *comp_unit_die,
1567 enum language pretend_language);
93311388 1568
ed2dc618 1569static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1570
dee91e82 1571static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1572
f792889a
DJ
1573static struct type *set_die_type (struct die_info *, struct type *,
1574 struct dwarf2_cu *);
1c379e20 1575
ed2dc618 1576static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1577
ed2dc618 1578static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1579
ab432490
SM
1580static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1581 dwarf2_per_objfile *per_objfile,
1582 bool skip_partial,
1583 enum language pretend_language);
10b3939b 1584
47b14e86
SM
1585static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1586 dwarf2_per_objfile *per_objfile,
1587 enum language pretend_language);
10b3939b 1588
47b14e86
SM
1589static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1590 dwarf2_per_objfile *per_objfile,
1591 enum language pretend_language);
f4dc4d17 1592
10b3939b
DJ
1593static void dwarf2_add_dependence (struct dwarf2_cu *,
1594 struct dwarf2_per_cu_data *);
1595
ae038cb0
DJ
1596static void dwarf2_mark (struct dwarf2_cu *);
1597
1598static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1599
b64f50a1 1600static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1601 struct dwarf2_per_cu_data *);
673bfd45 1602
f792889a 1603static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1604
95554aad
TT
1605static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1606 enum language pretend_language);
1607
ed2dc618 1608static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1609
b303c6f6
AB
1610/* Class, the destructor of which frees all allocated queue entries. This
1611 will only have work to do if an error was thrown while processing the
1612 dwarf. If no error was thrown then the queue entries should have all
1613 been processed, and freed, as we went along. */
1614
1615class dwarf2_queue_guard
1616{
1617public:
39856def
TT
1618 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1619 : m_per_objfile (per_objfile)
1620 {
1621 }
b303c6f6
AB
1622
1623 /* Free any entries remaining on the queue. There should only be
1624 entries left if we hit an error while processing the dwarf. */
1625 ~dwarf2_queue_guard ()
1626 {
39856def
TT
1627 /* Ensure that no memory is allocated by the queue. */
1628 std::queue<dwarf2_queue_item> empty;
5989a64e 1629 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1630 }
b303c6f6 1631
39856def 1632 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1633
39856def
TT
1634private:
1635 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1636};
1637
39856def
TT
1638dwarf2_queue_item::~dwarf2_queue_item ()
1639{
1640 /* Anything still marked queued is likely to be in an
1641 inconsistent state, so discard it. */
1642 if (per_cu->queued)
1643 {
1644 if (per_cu->cu != NULL)
1645 free_one_cached_comp_unit (per_cu);
1646 per_cu->queued = 0;
1647 }
1648}
1649
d721ba37
PA
1650/* The return type of find_file_and_directory. Note, the enclosed
1651 string pointers are only valid while this object is valid. */
1652
1653struct file_and_directory
1654{
1655 /* The filename. This is never NULL. */
1656 const char *name;
1657
1658 /* The compilation directory. NULL if not known. If we needed to
1659 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1660 points directly to the DW_AT_comp_dir string attribute owned by
1661 the obstack that owns the DIE. */
1662 const char *comp_dir;
1663
1664 /* If we needed to build a new string for comp_dir, this is what
1665 owns the storage. */
1666 std::string comp_dir_storage;
1667};
1668
1669static file_and_directory find_file_and_directory (struct die_info *die,
1670 struct dwarf2_cu *cu);
9291a0cd 1671
298e9637 1672static htab_up allocate_signatured_type_table ();
1fd400ff 1673
298e9637 1674static htab_up allocate_dwo_unit_table ();
3019eac3 1675
57d63ce2 1676static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1677 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1678 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1679 ULONGEST signature, int is_debug_types);
a2ce51a0 1680
ed2dc618
SM
1681static struct dwp_file *get_dwp_file
1682 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1683
3019eac3 1684static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1685 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1686 ULONGEST signature);
3019eac3
DE
1687
1688static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1689 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1690
89e63ee4
DE
1691static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1692
263db9a1
TT
1693/* A unique pointer to a dwo_file. */
1694
51ac9db5 1695typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1696
ed2dc618 1697static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1698
1b80a9fa 1699static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1700
1701static void free_line_header_voidp (void *arg);
4390d890
DE
1702\f
1703/* Various complaints about symbol reading that don't abort the process. */
1704
4390d890
DE
1705static void
1706dwarf2_debug_line_missing_file_complaint (void)
1707{
b98664d3 1708 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1709}
1710
1711static void
1712dwarf2_debug_line_missing_end_sequence_complaint (void)
1713{
b98664d3 1714 complaint (_(".debug_line section has line "
4390d890
DE
1715 "program sequence without an end"));
1716}
1717
1718static void
1719dwarf2_complex_location_expr_complaint (void)
1720{
b98664d3 1721 complaint (_("location expression too complex"));
4390d890
DE
1722}
1723
1724static void
1725dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1726 int arg3)
1727{
b98664d3 1728 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1729 arg1, arg2, arg3);
1730}
1731
4390d890
DE
1732static void
1733dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1734{
b98664d3 1735 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1736 arg1, arg2);
1737}
527f3840
JK
1738
1739/* Hash function for line_header_hash. */
1740
1741static hashval_t
1742line_header_hash (const struct line_header *ofs)
1743{
9c541725 1744 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1745}
1746
1747/* Hash function for htab_create_alloc_ex for line_header_hash. */
1748
1749static hashval_t
1750line_header_hash_voidp (const void *item)
1751{
9a3c8263 1752 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1753
1754 return line_header_hash (ofs);
1755}
1756
1757/* Equality function for line_header_hash. */
1758
1759static int
1760line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1761{
9a3c8263
SM
1762 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1763 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1764
9c541725 1765 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1766 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1767}
1768
4390d890 1769\f
9291a0cd 1770
330cdd98
PA
1771/* See declaration. */
1772
5989a64e
SM
1773dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1774 bool can_copy_)
c3699833
SM
1775 : obfd (obfd),
1776 can_copy (can_copy_)
330cdd98
PA
1777{
1778 if (names == NULL)
1779 names = &dwarf2_elf_names;
1780
330cdd98
PA
1781 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1782 locate_sections (obfd, sec, *names);
1783}
1784
5989a64e 1785dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98
PA
1786{
1787 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1788 free_cached_comp_units ();
1789
b76e467d 1790 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1791 per_cu->imported_symtabs_free ();
fc8e7e75 1792
b2bdb8cf 1793 for (signatured_type *sig_type : all_type_units)
ae640021 1794 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1795
5989a64e 1796 /* Everything else should be on this->obstack. */
330cdd98
PA
1797}
1798
1799/* See declaration. */
1800
1801void
5989a64e 1802dwarf2_per_bfd::free_cached_comp_units ()
330cdd98
PA
1803{
1804 dwarf2_per_cu_data *per_cu = read_in_chain;
1805 dwarf2_per_cu_data **last_chain = &read_in_chain;
1806 while (per_cu != NULL)
1807 {
1808 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1809
fcd3b13d 1810 delete per_cu->cu;
330cdd98
PA
1811 *last_chain = next_cu;
1812 per_cu = next_cu;
1813 }
1814}
1815
11ed8cad
TT
1816/* A helper class that calls free_cached_comp_units on
1817 destruction. */
1818
1819class free_cached_comp_units
1820{
1821public:
1822
1823 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1824 : m_per_objfile (per_objfile)
1825 {
1826 }
1827
1828 ~free_cached_comp_units ()
1829 {
5989a64e 1830 m_per_objfile->per_bfd->free_cached_comp_units ();
11ed8cad
TT
1831 }
1832
1833 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1834
1835private:
1836
1837 dwarf2_per_objfile *m_per_objfile;
1838};
1839
af758d11
SM
1840/* See read.h. */
1841
1842bool
1843dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1844{
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846
1847 return this->m_symtabs[per_cu->index] != nullptr;
1848}
1849
1850/* See read.h. */
1851
1852compunit_symtab *
1853dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1854{
1855 gdb_assert (per_cu->index < this->m_symtabs.size ());
1856
1857 return this->m_symtabs[per_cu->index];
1858}
1859
1860/* See read.h. */
1861
1862void
1863dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1864 compunit_symtab *symtab)
1865{
1866 gdb_assert (per_cu->index < this->m_symtabs.size ());
1867 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1868
1869 this->m_symtabs[per_cu->index] = symtab;
1870}
1871
c906108c 1872/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1873 information and return true if we have enough to do something.
1874 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1875 ELF names are used. CAN_COPY is true for formats where symbol
1876 interposition is possible and so symbol values must follow copy
1877 relocation rules. */
c906108c
SS
1878
1879int
251d32d9 1880dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1881 const struct dwarf2_debug_sections *names,
1882 bool can_copy)
c906108c 1883{
97cbe998
SDJ
1884 if (objfile->flags & OBJF_READNEVER)
1885 return 0;
1886
ed2dc618
SM
1887 struct dwarf2_per_objfile *dwarf2_per_objfile
1888 = get_dwarf2_per_objfile (objfile);
1889
1890 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1891 {
1892 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1893 sharing yet). */
1894 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1895
1896 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1897 }
5bfd760d 1898
5989a64e
SM
1899 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1900 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1901 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1902 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1903}
1904
251d32d9
TG
1905/* When loading sections, we look either for uncompressed section or for
1906 compressed section names. */
233a11ab
CS
1907
1908static int
251d32d9
TG
1909section_is_p (const char *section_name,
1910 const struct dwarf2_section_names *names)
233a11ab 1911{
251d32d9
TG
1912 if (names->normal != NULL
1913 && strcmp (section_name, names->normal) == 0)
1914 return 1;
1915 if (names->compressed != NULL
1916 && strcmp (section_name, names->compressed) == 0)
1917 return 1;
1918 return 0;
233a11ab
CS
1919}
1920
330cdd98 1921/* See declaration. */
c906108c 1922
330cdd98 1923void
5989a64e
SM
1924dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1925 const dwarf2_debug_sections &names)
c906108c 1926{
fd361982 1927 flagword aflag = bfd_section_flags (sectp);
251d32d9 1928
dc7650b8
JK
1929 if ((aflag & SEC_HAS_CONTENTS) == 0)
1930 {
1931 }
950b7495
KS
1932 else if (elf_section_data (sectp)->this_hdr.sh_size
1933 > bfd_get_file_size (abfd))
1934 {
1935 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1936 warning (_("Discarding section %s which has a section size (%s"
1937 ") larger than the file size [in module %s]"),
1938 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1939 bfd_get_filename (abfd));
1940 }
330cdd98 1941 else if (section_is_p (sectp->name, &names.info))
c906108c 1942 {
330cdd98 1943 this->info.s.section = sectp;
fd361982 1944 this->info.size = bfd_section_size (sectp);
c906108c 1945 }
330cdd98 1946 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1947 {
330cdd98 1948 this->abbrev.s.section = sectp;
fd361982 1949 this->abbrev.size = bfd_section_size (sectp);
c906108c 1950 }
330cdd98 1951 else if (section_is_p (sectp->name, &names.line))
c906108c 1952 {
330cdd98 1953 this->line.s.section = sectp;
fd361982 1954 this->line.size = bfd_section_size (sectp);
c906108c 1955 }
330cdd98 1956 else if (section_is_p (sectp->name, &names.loc))
c906108c 1957 {
330cdd98 1958 this->loc.s.section = sectp;
fd361982 1959 this->loc.size = bfd_section_size (sectp);
c906108c 1960 }
330cdd98 1961 else if (section_is_p (sectp->name, &names.loclists))
43988095 1962 {
330cdd98 1963 this->loclists.s.section = sectp;
fd361982 1964 this->loclists.size = bfd_section_size (sectp);
43988095 1965 }
330cdd98 1966 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1967 {
330cdd98 1968 this->macinfo.s.section = sectp;
fd361982 1969 this->macinfo.size = bfd_section_size (sectp);
c906108c 1970 }
330cdd98 1971 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1972 {
330cdd98 1973 this->macro.s.section = sectp;
fd361982 1974 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1975 }
330cdd98 1976 else if (section_is_p (sectp->name, &names.str))
c906108c 1977 {
330cdd98 1978 this->str.s.section = sectp;
fd361982 1979 this->str.size = bfd_section_size (sectp);
c906108c 1980 }
18a8505e
AT
1981 else if (section_is_p (sectp->name, &names.str_offsets))
1982 {
1983 this->str_offsets.s.section = sectp;
1984 this->str_offsets.size = bfd_section_size (sectp);
1985 }
330cdd98 1986 else if (section_is_p (sectp->name, &names.line_str))
43988095 1987 {
330cdd98 1988 this->line_str.s.section = sectp;
fd361982 1989 this->line_str.size = bfd_section_size (sectp);
43988095 1990 }
330cdd98 1991 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1992 {
330cdd98 1993 this->addr.s.section = sectp;
fd361982 1994 this->addr.size = bfd_section_size (sectp);
3019eac3 1995 }
330cdd98 1996 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1997 {
330cdd98 1998 this->frame.s.section = sectp;
fd361982 1999 this->frame.size = bfd_section_size (sectp);
b6af0555 2000 }
330cdd98 2001 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2002 {
330cdd98 2003 this->eh_frame.s.section = sectp;
fd361982 2004 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2005 }
330cdd98 2006 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2007 {
330cdd98 2008 this->ranges.s.section = sectp;
fd361982 2009 this->ranges.size = bfd_section_size (sectp);
af34e669 2010 }
330cdd98 2011 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2012 {
330cdd98 2013 this->rnglists.s.section = sectp;
fd361982 2014 this->rnglists.size = bfd_section_size (sectp);
43988095 2015 }
330cdd98 2016 else if (section_is_p (sectp->name, &names.types))
348e048f 2017 {
8b70b953
TT
2018 struct dwarf2_section_info type_section;
2019
2020 memset (&type_section, 0, sizeof (type_section));
049412e3 2021 type_section.s.section = sectp;
fd361982 2022 type_section.size = bfd_section_size (sectp);
8b70b953 2023
fd5866f6 2024 this->types.push_back (type_section);
348e048f 2025 }
330cdd98 2026 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2027 {
330cdd98 2028 this->gdb_index.s.section = sectp;
fd361982 2029 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2030 }
927aa2e7
JK
2031 else if (section_is_p (sectp->name, &names.debug_names))
2032 {
2033 this->debug_names.s.section = sectp;
fd361982 2034 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2035 }
2036 else if (section_is_p (sectp->name, &names.debug_aranges))
2037 {
2038 this->debug_aranges.s.section = sectp;
fd361982 2039 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2040 }
dce234bc 2041
fd361982
AM
2042 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2043 && bfd_section_vma (sectp) == 0)
330cdd98 2044 this->has_section_at_zero = true;
c906108c
SS
2045}
2046
dce234bc 2047/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2048 SECTION_NAME. */
af34e669 2049
dce234bc 2050void
3017a003
TG
2051dwarf2_get_section_info (struct objfile *objfile,
2052 enum dwarf2_section_enum sect,
d521ce57 2053 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2054 bfd_size_type *sizep)
2055{
5bfd760d 2056 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2057 struct dwarf2_section_info *info;
a3b2a86b
TT
2058
2059 /* We may see an objfile without any DWARF, in which case we just
2060 return nothing. */
2061 if (data == NULL)
2062 {
2063 *sectp = NULL;
2064 *bufp = NULL;
2065 *sizep = 0;
2066 return;
2067 }
3017a003
TG
2068 switch (sect)
2069 {
2070 case DWARF2_DEBUG_FRAME:
5989a64e 2071 info = &data->per_bfd->frame;
3017a003
TG
2072 break;
2073 case DWARF2_EH_FRAME:
5989a64e 2074 info = &data->per_bfd->eh_frame;
3017a003
TG
2075 break;
2076 default:
2077 gdb_assert_not_reached ("unexpected section");
2078 }
dce234bc 2079
96b79293 2080 info->read (objfile);
dce234bc 2081
96b79293 2082 *sectp = info->get_bfd_section ();
dce234bc
PP
2083 *bufp = info->buffer;
2084 *sizep = info->size;
2085}
2086
36586728
TT
2087/* A helper function to find the sections for a .dwz file. */
2088
2089static void
2090locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2091{
9a3c8263 2092 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2093
2094 /* Note that we only support the standard ELF names, because .dwz
2095 is ELF-only (at the time of writing). */
2096 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2097 {
049412e3 2098 dwz_file->abbrev.s.section = sectp;
fd361982 2099 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2100 }
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2102 {
049412e3 2103 dwz_file->info.s.section = sectp;
fd361982 2104 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2105 }
2106 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2107 {
049412e3 2108 dwz_file->str.s.section = sectp;
fd361982 2109 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2110 }
2111 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2112 {
049412e3 2113 dwz_file->line.s.section = sectp;
fd361982 2114 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2115 }
2116 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2117 {
049412e3 2118 dwz_file->macro.s.section = sectp;
fd361982 2119 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2120 }
2ec9a5e0
TT
2121 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2122 {
049412e3 2123 dwz_file->gdb_index.s.section = sectp;
fd361982 2124 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2125 }
927aa2e7
JK
2126 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2127 {
2128 dwz_file->debug_names.s.section = sectp;
fd361982 2129 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2130 }
36586728
TT
2131}
2132
c4973306 2133/* See dwarf2read.h. */
36586728 2134
c4973306 2135struct dwz_file *
c3699833 2136dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2137{
36586728 2138 const char *filename;
acd13123 2139 bfd_size_type buildid_len_arg;
dc294be5
TT
2140 size_t buildid_len;
2141 bfd_byte *buildid;
36586728 2142
c3699833
SM
2143 if (per_bfd->dwz_file != NULL)
2144 return per_bfd->dwz_file.get ();
36586728 2145
4db1a1dc 2146 bfd_set_error (bfd_error_no_error);
791afaa2 2147 gdb::unique_xmalloc_ptr<char> data
c3699833 2148 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2149 &buildid_len_arg, &buildid));
4db1a1dc
TT
2150 if (data == NULL)
2151 {
2152 if (bfd_get_error () == bfd_error_no_error)
2153 return NULL;
2154 error (_("could not read '.gnu_debugaltlink' section: %s"),
2155 bfd_errmsg (bfd_get_error ()));
2156 }
791afaa2
TT
2157
2158 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2159
acd13123
TT
2160 buildid_len = (size_t) buildid_len_arg;
2161
791afaa2 2162 filename = data.get ();
d721ba37
PA
2163
2164 std::string abs_storage;
36586728
TT
2165 if (!IS_ABSOLUTE_PATH (filename))
2166 {
14278e1f 2167 gdb::unique_xmalloc_ptr<char> abs
c3699833 2168 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2169
14278e1f 2170 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2171 filename = abs_storage.c_str ();
36586728
TT
2172 }
2173
dc294be5
TT
2174 /* First try the file name given in the section. If that doesn't
2175 work, try to use the build-id instead. */
ad80db5b 2176 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2177 if (dwz_bfd != NULL)
36586728 2178 {
192b62ce 2179 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2180 dwz_bfd.reset (nullptr);
36586728
TT
2181 }
2182
dc294be5
TT
2183 if (dwz_bfd == NULL)
2184 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2185
0d79cdc4
AM
2186 if (dwz_bfd == nullptr)
2187 {
2188 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2189 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2190
2191 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2192 buildid_len,
2193 origname,
2194 &alt_filename));
2195
2196 if (fd.get () >= 0)
2197 {
2198 /* File successfully retrieved from server. */
ad80db5b 2199 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2200
2201 if (dwz_bfd == nullptr)
2202 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2203 alt_filename.get ());
2204 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2205 dwz_bfd.reset (nullptr);
2206 }
2207 }
2208
dc294be5
TT
2209 if (dwz_bfd == NULL)
2210 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2211 bfd_get_filename (per_bfd->obfd));
dc294be5 2212
7ff8cb8c
TT
2213 std::unique_ptr<struct dwz_file> result
2214 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2215
7ff8cb8c
TT
2216 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2217 result.get ());
36586728 2218
c3699833
SM
2219 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2220 per_bfd->dwz_file = std::move (result);
2221 return per_bfd->dwz_file.get ();
36586728 2222}
9291a0cd 2223\f
7b9f3c50
DE
2224/* DWARF quick_symbols_functions support. */
2225
2226/* TUs can share .debug_line entries, and there can be a lot more TUs than
2227 unique line tables, so we maintain a separate table of all .debug_line
2228 derived entries to support the sharing.
2229 All the quick functions need is the list of file names. We discard the
2230 line_header when we're done and don't need to record it here. */
2231struct quick_file_names
2232{
094b34ac
DE
2233 /* The data used to construct the hash key. */
2234 struct stmt_list_hash hash;
7b9f3c50
DE
2235
2236 /* The number of entries in file_names, real_names. */
2237 unsigned int num_file_names;
2238
2239 /* The file names from the line table, after being run through
2240 file_full_name. */
2241 const char **file_names;
2242
2243 /* The file names from the line table after being run through
2244 gdb_realpath. These are computed lazily. */
2245 const char **real_names;
2246};
2247
2248/* When using the index (and thus not using psymtabs), each CU has an
2249 object of this type. This is used to hold information needed by
2250 the various "quick" methods. */
2251struct dwarf2_per_cu_quick_data
2252{
2253 /* The file table. This can be NULL if there was no file table
2254 or it's currently not read in.
5989a64e 2255 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2256 struct quick_file_names *file_names;
2257
7b9f3c50
DE
2258 /* A temporary mark bit used when iterating over all CUs in
2259 expand_symtabs_matching. */
2260 unsigned int mark : 1;
2261
2262 /* True if we've tried to read the file table and found there isn't one.
2263 There will be no point in trying to read it again next time. */
2264 unsigned int no_file_data : 1;
2265};
2266
094b34ac
DE
2267/* Utility hash function for a stmt_list_hash. */
2268
2269static hashval_t
2270hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2271{
2272 hashval_t v = 0;
2273
2274 if (stmt_list_hash->dwo_unit != NULL)
2275 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2276 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2277 return v;
2278}
2279
2280/* Utility equality function for a stmt_list_hash. */
2281
2282static int
2283eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2284 const struct stmt_list_hash *rhs)
2285{
2286 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2287 return 0;
2288 if (lhs->dwo_unit != NULL
2289 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2290 return 0;
2291
9c541725 2292 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2293}
2294
7b9f3c50
DE
2295/* Hash function for a quick_file_names. */
2296
2297static hashval_t
2298hash_file_name_entry (const void *e)
2299{
9a3c8263
SM
2300 const struct quick_file_names *file_data
2301 = (const struct quick_file_names *) e;
7b9f3c50 2302
094b34ac 2303 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2304}
2305
2306/* Equality function for a quick_file_names. */
2307
2308static int
2309eq_file_name_entry (const void *a, const void *b)
2310{
9a3c8263
SM
2311 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2312 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2313
094b34ac 2314 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2315}
2316
2317/* Delete function for a quick_file_names. */
2318
2319static void
2320delete_file_name_entry (void *e)
2321{
9a3c8263 2322 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2323 int i;
2324
2325 for (i = 0; i < file_data->num_file_names; ++i)
2326 {
2327 xfree ((void*) file_data->file_names[i]);
2328 if (file_data->real_names)
2329 xfree ((void*) file_data->real_names[i]);
2330 }
2331
45940949
TT
2332 /* The space for the struct itself lives on the obstack, so we don't
2333 free it here. */
7b9f3c50
DE
2334}
2335
2336/* Create a quick_file_names hash table. */
2337
5895093f 2338static htab_up
7b9f3c50
DE
2339create_quick_file_names_table (unsigned int nr_initial_entries)
2340{
5895093f
TT
2341 return htab_up (htab_create_alloc (nr_initial_entries,
2342 hash_file_name_entry, eq_file_name_entry,
2343 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2344}
9291a0cd 2345
ab432490
SM
2346/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2347 function is unrelated to symtabs, symtab would have to be created afterwards.
2348 You should call age_cached_comp_units after processing the CU. */
918dd910
JK
2349
2350static void
ab432490
SM
2351load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2352 bool skip_partial)
918dd910 2353{
3019eac3 2354 if (per_cu->is_debug_types)
ab432490 2355 load_full_type_unit (per_cu, per_objfile);
918dd910 2356 else
ab432490 2357 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2358
cc12ce38
DE
2359 if (per_cu->cu == NULL)
2360 return; /* Dummy CU. */
2dc860c0
DE
2361
2362 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2363}
2364
97a1449a 2365/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2366
9291a0cd 2367static void
97a1449a
SM
2368dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2369 dwarf2_per_objfile *dwarf2_per_objfile,
2370 bool skip_partial)
9291a0cd 2371{
f4dc4d17
DE
2372 /* Skip type_unit_groups, reading the type units they contain
2373 is handled elsewhere. */
197400e8 2374 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2375 return;
2376
b303c6f6
AB
2377 /* The destructor of dwarf2_queue_guard frees any entries left on
2378 the queue. After this point we're guaranteed to leave this function
2379 with the dwarf queue empty. */
39856def 2380 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2381
af758d11 2382 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad
TT
2383 {
2384 queue_comp_unit (per_cu, language_minimal);
ab432490 2385 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
89e63ee4
DE
2386
2387 /* If we just loaded a CU from a DWO, and we're working with an index
2388 that may badly handle TUs, load all the TUs in that DWO as well.
2389 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2390 if (!per_cu->is_debug_types
cc12ce38 2391 && per_cu->cu != NULL
89e63ee4 2392 && per_cu->cu->dwo_unit != NULL
5989a64e
SM
2393 && dwarf2_per_objfile->per_bfd->index_table != NULL
2394 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2395 /* DWP files aren't supported yet. */
ed2dc618 2396 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2397 queue_and_load_all_dwo_tus (per_cu);
95554aad 2398 }
9291a0cd 2399
ed2dc618 2400 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2401
2402 /* Age the cache, releasing compilation units that have not
2403 been used recently. */
ed2dc618 2404 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2405}
2406
97a1449a
SM
2407/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2408 the per-objfile for which this symtab is instantiated.
2409
2410 Returns the resulting symbol table. */
2fdf6df6 2411
43f3e411 2412static struct compunit_symtab *
97a1449a
SM
2413dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2414 dwarf2_per_objfile *dwarf2_per_objfile,
2415 bool skip_partial)
9291a0cd 2416{
5989a64e 2417 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2418
2419 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2420 {
11ed8cad 2421 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2422 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2423 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2424 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2425 }
f194fefb 2426
af758d11 2427 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2428}
2429
ff4c9fec 2430/* See declaration. */
f4dc4d17 2431
ff4c9fec 2432dwarf2_per_cu_data *
5989a64e 2433dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2434{
b76e467d 2435 if (index >= this->all_comp_units.size ())
ff4c9fec 2436 {
b76e467d 2437 index -= this->all_comp_units.size ();
b2bdb8cf 2438 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2439 return &this->all_type_units[index]->per_cu;
2440 }
f4dc4d17 2441
ff4c9fec
SM
2442 return this->all_comp_units[index];
2443}
f4dc4d17 2444
ff4c9fec 2445/* See declaration. */
2fdf6df6 2446
ff4c9fec 2447dwarf2_per_cu_data *
5989a64e 2448dwarf2_per_bfd::get_cu (int index)
1fd400ff 2449{
b76e467d 2450 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2451
ff4c9fec 2452 return this->all_comp_units[index];
f4dc4d17
DE
2453}
2454
ff4c9fec 2455/* See declaration. */
f4dc4d17 2456
ff4c9fec 2457signatured_type *
5989a64e 2458dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2459{
b2bdb8cf 2460 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2461
ff4c9fec 2462 return this->all_type_units[index];
1fd400ff
TT
2463}
2464
d3473f0c
TT
2465/* See read.h. */
2466
2467dwarf2_per_cu_data *
5989a64e 2468dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2469{
2470 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2471 result->per_bfd = this;
d3473f0c
TT
2472 result->index = m_num_psymtabs++;
2473 return result;
2474}
2475
2476/* See read.h. */
2477
2478signatured_type *
5989a64e 2479dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2480{
2481 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2482 result->per_cu.per_bfd = this;
d3473f0c
TT
2483 result->per_cu.index = m_num_psymtabs++;
2484 return result;
2485}
2486
45940949
TT
2487/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2488 obstack, and constructed with the specified field values. */
4b514bc8
JK
2489
2490static dwarf2_per_cu_data *
ed2dc618 2491create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2492 struct dwarf2_section_info *section,
2493 int is_dwz,
2494 sect_offset sect_off, ULONGEST length)
2495{
5989a64e 2496 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
4b514bc8
JK
2497 the_cu->sect_off = sect_off;
2498 the_cu->length = length;
e3b94546 2499 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8 2500 the_cu->section = section;
5989a64e 2501 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
45940949 2502 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2503 the_cu->is_dwz = is_dwz;
2504 return the_cu;
2505}
2506
2ec9a5e0
TT
2507/* A helper for create_cus_from_index that handles a given list of
2508 CUs. */
2fdf6df6 2509
74a0d9f6 2510static void
12359b5e 2511create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2512 const gdb_byte *cu_list, offset_type n_elements,
2513 struct dwarf2_section_info *section,
b76e467d 2514 int is_dwz)
9291a0cd 2515{
12359b5e 2516 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2517 {
74a0d9f6 2518 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2519
2520 sect_offset sect_off
2521 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2522 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2523 cu_list += 2 * 8;
2524
b76e467d 2525 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2526 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2527 sect_off, length);
5989a64e 2528 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2529 }
9291a0cd
TT
2530}
2531
2ec9a5e0 2532/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2533 the CU objects for this objfile. */
2ec9a5e0 2534
74a0d9f6 2535static void
12359b5e 2536create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2537 const gdb_byte *cu_list, offset_type cu_list_elements,
2538 const gdb_byte *dwz_list, offset_type dwz_elements)
2539{
5989a64e
SM
2540 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2541 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
b76e467d 2542 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2543
12359b5e 2544 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
5989a64e 2545 &dwarf2_per_objfile->per_bfd->info, 0);
2ec9a5e0
TT
2546
2547 if (dwz_elements == 0)
74a0d9f6 2548 return;
2ec9a5e0 2549
c3699833 2550 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
12359b5e 2551 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2552 &dwz->info, 1);
2ec9a5e0
TT
2553}
2554
1fd400ff 2555/* Create the signatured type hash table from the index. */
673bfd45 2556
74a0d9f6 2557static void
12359b5e
SM
2558create_signatured_type_table_from_index
2559 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2560 struct dwarf2_section_info *section,
2561 const gdb_byte *bytes,
2562 offset_type elements)
1fd400ff 2563{
5989a64e
SM
2564 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2565 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2566
298e9637 2567 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2568
12359b5e 2569 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2570 {
52dc124a 2571 struct signatured_type *sig_type;
9c541725 2572 ULONGEST signature;
1fd400ff 2573 void **slot;
9c541725 2574 cu_offset type_offset_in_tu;
1fd400ff 2575
74a0d9f6 2576 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2577 sect_offset sect_off
2578 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2579 type_offset_in_tu
2580 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2581 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2582 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2583 bytes += 3 * 8;
2584
5989a64e 2585 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
52dc124a 2586 sig_type->signature = signature;
9c541725 2587 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2588 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2589 sig_type->per_cu.section = section;
9c541725 2590 sig_type->per_cu.sect_off = sect_off;
e3b94546 2591 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2592 sig_type->per_cu.v.quick
5989a64e 2593 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
1fd400ff
TT
2594 struct dwarf2_per_cu_quick_data);
2595
b0b6a987 2596 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2597 *slot = sig_type;
1fd400ff 2598
5989a64e 2599 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2600 }
2601
5989a64e 2602 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2603}
2604
927aa2e7
JK
2605/* Create the signatured type hash table from .debug_names. */
2606
2607static void
2608create_signatured_type_table_from_debug_names
ed2dc618 2609 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2610 const mapped_debug_names &map,
2611 struct dwarf2_section_info *section,
2612 struct dwarf2_section_info *abbrev_section)
2613{
ed2dc618
SM
2614 struct objfile *objfile = dwarf2_per_objfile->objfile;
2615
96b79293
TT
2616 section->read (objfile);
2617 abbrev_section->read (objfile);
927aa2e7 2618
5989a64e
SM
2619 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2620 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2621
298e9637 2622 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2623
2624 for (uint32_t i = 0; i < map.tu_count; ++i)
2625 {
2626 struct signatured_type *sig_type;
927aa2e7 2627 void **slot;
927aa2e7
JK
2628
2629 sect_offset sect_off
2630 = (sect_offset) (extract_unsigned_integer
2631 (map.tu_table_reordered + i * map.offset_size,
2632 map.offset_size,
2633 map.dwarf5_byte_order));
2634
2635 comp_unit_head cu_header;
ed2dc618
SM
2636 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2637 abbrev_section,
927aa2e7
JK
2638 section->buffer + to_underlying (sect_off),
2639 rcuh_kind::TYPE);
2640
5989a64e 2641 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2642 sig_type->signature = cu_header.signature;
2643 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2644 sig_type->per_cu.is_debug_types = 1;
2645 sig_type->per_cu.section = section;
2646 sig_type->per_cu.sect_off = sect_off;
e3b94546 2647 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7 2648 sig_type->per_cu.v.quick
5989a64e 2649 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2650 struct dwarf2_per_cu_quick_data);
2651
b0b6a987 2652 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2653 *slot = sig_type;
2654
5989a64e 2655 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2656 }
2657
5989a64e 2658 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2659}
2660
9291a0cd
TT
2661/* Read the address map data from the mapped index, and use it to
2662 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2663
9291a0cd 2664static void
ed2dc618
SM
2665create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2666 struct mapped_index *index)
9291a0cd 2667{
ed2dc618 2668 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2669 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2670 const gdb_byte *iter, *end;
9291a0cd 2671 struct addrmap *mutable_map;
9291a0cd
TT
2672 CORE_ADDR baseaddr;
2673
8268c778
PA
2674 auto_obstack temp_obstack;
2675
9291a0cd
TT
2676 mutable_map = addrmap_create_mutable (&temp_obstack);
2677
f00a2de2
PA
2678 iter = index->address_table.data ();
2679 end = iter + index->address_table.size ();
9291a0cd 2680
b3b3bada 2681 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2682
2683 while (iter < end)
2684 {
2685 ULONGEST hi, lo, cu_index;
2686 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2687 iter += 8;
2688 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2689 iter += 8;
2690 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2691 iter += 4;
f652bce2 2692
24a55014 2693 if (lo > hi)
f652bce2 2694 {
b98664d3 2695 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2696 hex_string (lo), hex_string (hi));
24a55014 2697 continue;
f652bce2 2698 }
24a55014 2699
5989a64e 2700 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2701 {
b98664d3 2702 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2703 (unsigned) cu_index);
24a55014 2704 continue;
f652bce2 2705 }
24a55014 2706
79748972
TT
2707 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2708 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2709 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2710 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2711 }
2712
d320c2b5 2713 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2714 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2715}
2716
927aa2e7
JK
2717/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2718 populate the objfile's psymtabs_addrmap. */
2719
2720static void
ed2dc618 2721create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2722 struct dwarf2_section_info *section)
2723{
ed2dc618 2724 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2725 bfd *abfd = objfile->obfd;
08feed99 2726 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2727 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2728
2729 auto_obstack temp_obstack;
2730 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2731
2732 std::unordered_map<sect_offset,
2733 dwarf2_per_cu_data *,
2734 gdb::hash_enum<sect_offset>>
2735 debug_info_offset_to_per_cu;
5989a64e 2736 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2737 {
927aa2e7
JK
2738 const auto insertpair
2739 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2740 if (!insertpair.second)
2741 {
2742 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2743 "debug_info_offset %s, ignoring .debug_aranges."),
2744 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2745 return;
2746 }
2747 }
2748
96b79293 2749 section->read (objfile);
927aa2e7
JK
2750
2751 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2752
2753 const gdb_byte *addr = section->buffer;
2754
2755 while (addr < section->buffer + section->size)
2756 {
2757 const gdb_byte *const entry_addr = addr;
2758 unsigned int bytes_read;
2759
2760 const LONGEST entry_length = read_initial_length (abfd, addr,
2761 &bytes_read);
2762 addr += bytes_read;
2763
2764 const gdb_byte *const entry_end = addr + entry_length;
2765 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2766 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2767 if (addr + entry_length > section->buffer + section->size)
2768 {
47e3f474 2769 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2770 "length %s exceeds section length %s, "
2771 "ignoring .debug_aranges."),
47e3f474
TV
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer),
927aa2e7
JK
2774 plongest (bytes_read + entry_length),
2775 pulongest (section->size));
2776 return;
2777 }
2778
2779 /* The version number. */
2780 const uint16_t version = read_2_bytes (abfd, addr);
2781 addr += 2;
2782 if (version != 2)
2783 {
47e3f474 2784 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2785 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2786 objfile_name (objfile),
2787 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2788 return;
2789 }
2790
2791 const uint64_t debug_info_offset
2792 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2793 addr += offset_size;
2794 const auto per_cu_it
2795 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2796 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2797 {
47e3f474 2798 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2799 "debug_info_offset %s does not exists, "
2800 "ignoring .debug_aranges."),
47e3f474
TV
2801 objfile_name (objfile),
2802 plongest (entry_addr - section->buffer),
927aa2e7
JK
2803 pulongest (debug_info_offset));
2804 return;
2805 }
2806 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2807
2808 const uint8_t address_size = *addr++;
2809 if (address_size < 1 || address_size > 8)
2810 {
47e3f474 2811 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2812 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2815 return;
2816 }
2817
2818 const uint8_t segment_selector_size = *addr++;
2819 if (segment_selector_size != 0)
2820 {
47e3f474 2821 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2822 "segment_selector_size %u is not supported, "
2823 "ignoring .debug_aranges."),
47e3f474
TV
2824 objfile_name (objfile),
2825 plongest (entry_addr - section->buffer),
927aa2e7
JK
2826 segment_selector_size);
2827 return;
2828 }
2829
2830 /* Must pad to an alignment boundary that is twice the address
2831 size. It is undocumented by the DWARF standard but GCC does
2832 use it. */
2833 for (size_t padding = ((-(addr - section->buffer))
2834 & (2 * address_size - 1));
2835 padding > 0; padding--)
2836 if (*addr++ != 0)
2837 {
47e3f474 2838 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2839 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2840 objfile_name (objfile),
2841 plongest (entry_addr - section->buffer));
927aa2e7
JK
2842 return;
2843 }
2844
2845 for (;;)
2846 {
2847 if (addr + 2 * address_size > entry_end)
2848 {
47e3f474 2849 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2850 "address list is not properly terminated, "
2851 "ignoring .debug_aranges."),
47e3f474
TV
2852 objfile_name (objfile),
2853 plongest (entry_addr - section->buffer));
927aa2e7
JK
2854 return;
2855 }
2856 ULONGEST start = extract_unsigned_integer (addr, address_size,
2857 dwarf5_byte_order);
2858 addr += address_size;
2859 ULONGEST length = extract_unsigned_integer (addr, address_size,
2860 dwarf5_byte_order);
2861 addr += address_size;
2862 if (start == 0 && length == 0)
2863 break;
5989a64e 2864 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2865 {
2866 /* Symbol was eliminated due to a COMDAT group. */
2867 continue;
2868 }
2869 ULONGEST end = start + length;
79748972
TT
2870 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2871 - baseaddr);
2872 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2873 - baseaddr);
927aa2e7
JK
2874 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2875 }
2876 }
2877
d320c2b5 2878 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2879 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2880}
2881
9291a0cd
TT
2882/* Find a slot in the mapped index INDEX for the object named NAME.
2883 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2884 constant pool and return true. If NAME cannot be found, return
2885 false. */
2fdf6df6 2886
109483d9 2887static bool
9291a0cd
TT
2888find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2889 offset_type **vec_out)
2890{
0cf03b49 2891 offset_type hash;
9291a0cd 2892 offset_type slot, step;
559a7a62 2893 int (*cmp) (const char *, const char *);
9291a0cd 2894
791afaa2 2895 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2896 if (current_language->la_language == language_cplus
45280282
IB
2897 || current_language->la_language == language_fortran
2898 || current_language->la_language == language_d)
0cf03b49
JK
2899 {
2900 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2901 not contain any. */
a8719064 2902
72998fb3 2903 if (strchr (name, '(') != NULL)
0cf03b49 2904 {
109483d9 2905 without_params = cp_remove_params (name);
0cf03b49 2906
72998fb3 2907 if (without_params != NULL)
791afaa2 2908 name = without_params.get ();
0cf03b49
JK
2909 }
2910 }
2911
559a7a62 2912 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2913 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2914 simulate our NAME being searched is also lowercased. */
2915 hash = mapped_index_string_hash ((index->version == 4
2916 && case_sensitivity == case_sensitive_off
2917 ? 5 : index->version),
2918 name);
2919
f00a2de2
PA
2920 slot = hash & (index->symbol_table.size () - 1);
2921 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2922 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2923
2924 for (;;)
2925 {
9291a0cd 2926 const char *str;
f00a2de2
PA
2927
2928 const auto &bucket = index->symbol_table[slot];
2929 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2930 return false;
9291a0cd 2931
f00a2de2 2932 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2933 if (!cmp (name, str))
9291a0cd
TT
2934 {
2935 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2936 + MAYBE_SWAP (bucket.vec));
109483d9 2937 return true;
9291a0cd
TT
2938 }
2939
f00a2de2 2940 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2941 }
2942}
2943
4485a1c1
SM
2944/* A helper function that reads the .gdb_index from BUFFER and fills
2945 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2946 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2947 ok to use deprecated sections.
2948
2949 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2950 out parameters that are filled in with information about the CU and
2951 TU lists in the section.
2952
4485a1c1 2953 Returns true if all went well, false otherwise. */
2fdf6df6 2954
d33bc52e 2955static bool
3810f182 2956read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2957 bool deprecated_ok,
2958 gdb::array_view<const gdb_byte> buffer,
2959 struct mapped_index *map,
2960 const gdb_byte **cu_list,
2961 offset_type *cu_list_elements,
2962 const gdb_byte **types_list,
2963 offset_type *types_list_elements)
2964{
2965 const gdb_byte *addr = &buffer[0];
82430852 2966
9291a0cd 2967 /* Version check. */
4485a1c1 2968 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2969 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2970 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2971 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2972 indices. */
831adc1f 2973 if (version < 4)
481860b3
GB
2974 {
2975 static int warning_printed = 0;
2976 if (!warning_printed)
2977 {
2978 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2979 filename);
481860b3
GB
2980 warning_printed = 1;
2981 }
2982 return 0;
2983 }
2984 /* Index version 4 uses a different hash function than index version
2985 5 and later.
2986
2987 Versions earlier than 6 did not emit psymbols for inlined
2988 functions. Using these files will cause GDB not to be able to
2989 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2990 indices unless the user has done
2991 "set use-deprecated-index-sections on". */
2ec9a5e0 2992 if (version < 6 && !deprecated_ok)
481860b3
GB
2993 {
2994 static int warning_printed = 0;
2995 if (!warning_printed)
2996 {
e615022a
DE
2997 warning (_("\
2998Skipping deprecated .gdb_index section in %s.\n\
2999Do \"set use-deprecated-index-sections on\" before the file is read\n\
3000to use the section anyway."),
2ec9a5e0 3001 filename);
481860b3
GB
3002 warning_printed = 1;
3003 }
3004 return 0;
3005 }
796a7ff8 3006 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3007 of the TU (for symbols coming from TUs),
3008 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3009 Plus gold-generated indices can have duplicate entries for global symbols,
3010 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3011 These are just performance bugs, and we can't distinguish gdb-generated
3012 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3013
481860b3 3014 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3015 longer backward compatible. */
796a7ff8 3016 if (version > 8)
594e8718 3017 return 0;
9291a0cd 3018
559a7a62 3019 map->version = version;
9291a0cd 3020
4485a1c1 3021 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3022
4485a1c1 3023 int i = 0;
2ec9a5e0
TT
3024 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3025 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3026 / 8);
1fd400ff
TT
3027 ++i;
3028
2ec9a5e0
TT
3029 *types_list = addr + MAYBE_SWAP (metadata[i]);
3030 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3031 - MAYBE_SWAP (metadata[i]))
3032 / 8);
987d643c 3033 ++i;
1fd400ff 3034
f00a2de2
PA
3035 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3036 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3037 map->address_table
3038 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3039 ++i;
3040
f00a2de2
PA
3041 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3042 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3043 map->symbol_table
3044 = gdb::array_view<mapped_index::symbol_table_slot>
3045 ((mapped_index::symbol_table_slot *) symbol_table,
3046 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3047
f00a2de2 3048 ++i;
f9d83a0b 3049 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3050
2ec9a5e0
TT
3051 return 1;
3052}
3053
4485a1c1
SM
3054/* Callback types for dwarf2_read_gdb_index. */
3055
3056typedef gdb::function_view
5989a64e 3057 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3058 get_gdb_index_contents_ftype;
3059typedef gdb::function_view
3060 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3061 get_gdb_index_contents_dwz_ftype;
3062
927aa2e7 3063/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3064 elements of all the CUs and return 1. Otherwise, return 0. */
3065
3066static int
4485a1c1
SM
3067dwarf2_read_gdb_index
3068 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3069 get_gdb_index_contents_ftype get_gdb_index_contents,
3070 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3071{
2ec9a5e0
TT
3072 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3073 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3074 struct dwz_file *dwz;
12359b5e 3075 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3076
4485a1c1 3077 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3078 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3079
3080 if (main_index_contents.empty ())
3081 return 0;
3082
3063847f 3083 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3084 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3085 use_deprecated_index_sections,
3086 main_index_contents, map.get (), &cu_list,
3087 &cu_list_elements, &types_list,
3088 &types_list_elements))
2ec9a5e0
TT
3089 return 0;
3090
0fefef59 3091 /* Don't use the index if it's empty. */
3063847f 3092 if (map->symbol_table.empty ())
0fefef59
DE
3093 return 0;
3094
2ec9a5e0
TT
3095 /* If there is a .dwz file, read it so we can get its CU list as
3096 well. */
c3699833 3097 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3098 if (dwz != NULL)
2ec9a5e0 3099 {
2ec9a5e0
TT
3100 struct mapped_index dwz_map;
3101 const gdb_byte *dwz_types_ignore;
3102 offset_type dwz_types_elements_ignore;
3103
4485a1c1
SM
3104 gdb::array_view<const gdb_byte> dwz_index_content
3105 = get_gdb_index_contents_dwz (objfile, dwz);
3106
3107 if (dwz_index_content.empty ())
3108 return 0;
3109
3810f182 3110 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3111 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3112 &dwz_list, &dwz_list_elements,
3113 &dwz_types_ignore,
3114 &dwz_types_elements_ignore))
2ec9a5e0
TT
3115 {
3116 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3117 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3118 return 0;
3119 }
3120 }
3121
12359b5e
SM
3122 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3123 dwz_list, dwz_list_elements);
1fd400ff 3124
8b70b953
TT
3125 if (types_list_elements)
3126 {
8b70b953
TT
3127 /* We can only handle a single .debug_types when we have an
3128 index. */
5989a64e 3129 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3130 return 0;
3131
5989a64e 3132 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3133
12359b5e
SM
3134 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3135 types_list, types_list_elements);
8b70b953 3136 }
9291a0cd 3137
3063847f 3138 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3139
5989a64e
SM
3140 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3141 dwarf2_per_objfile->per_bfd->using_index = 1;
3142 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3143 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3144
3145 return 1;
3146}
3147
dee91e82 3148/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3149
dee91e82
DE
3150static void
3151dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3152 const gdb_byte *info_ptr,
3e225074 3153 struct die_info *comp_unit_die)
9291a0cd 3154{
dee91e82 3155 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3156 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3157 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3158 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3159 struct attribute *attr;
7b9f3c50
DE
3160 void **slot;
3161 struct quick_file_names *qfn;
9291a0cd 3162
0186c6a7
DE
3163 gdb_assert (! this_cu->is_debug_types);
3164
07261596
TT
3165 /* Our callers never want to match partial units -- instead they
3166 will match the enclosing full CU. */
3167 if (comp_unit_die->tag == DW_TAG_partial_unit)
3168 {
3169 this_cu->v.quick->no_file_data = 1;
3170 return;
3171 }
3172
0186c6a7 3173 lh_cu = this_cu;
7b9f3c50 3174 slot = NULL;
dee91e82 3175
fff8551c 3176 line_header_up lh;
9c541725 3177 sect_offset line_offset {};
fff8551c 3178
dee91e82 3179 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3180 if (attr != nullptr)
9291a0cd 3181 {
7b9f3c50
DE
3182 struct quick_file_names find_entry;
3183
9c541725 3184 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3185
3186 /* We may have already read in this line header (TU line header sharing).
3187 If we have we're done. */
094b34ac 3188 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3189 find_entry.hash.line_sect_off = line_offset;
5989a64e 3190 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3191 &find_entry, INSERT);
3192 if (*slot != NULL)
3193 {
9a3c8263 3194 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3195 return;
7b9f3c50
DE
3196 }
3197
3019eac3 3198 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3199 }
3200 if (lh == NULL)
3201 {
094b34ac 3202 lh_cu->v.quick->no_file_data = 1;
dee91e82 3203 return;
9291a0cd
TT
3204 }
3205
5989a64e 3206 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3207 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3208 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3209 gdb_assert (slot != NULL);
3210 *slot = qfn;
9291a0cd 3211
d721ba37 3212 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3213
aa391654
TT
3214 int offset = 0;
3215 if (strcmp (fnd.name, "<unknown>") != 0)
3216 ++offset;
3217
7ba99d21 3218 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3219 qfn->file_names =
5989a64e 3220 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3221 qfn->num_file_names);
aa391654
TT
3222 if (offset != 0)
3223 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3224 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3225 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3226 fnd.comp_dir).release ();
7b9f3c50 3227 qfn->real_names = NULL;
9291a0cd 3228
094b34ac 3229 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3230}
3231
3232/* A helper for the "quick" functions which attempts to read the line
3233 table for THIS_CU. */
3234
3235static struct quick_file_names *
ab432490
SM
3236dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3237 dwarf2_per_objfile *per_objfile)
dee91e82 3238{
0186c6a7
DE
3239 /* This should never be called for TUs. */
3240 gdb_assert (! this_cu->is_debug_types);
3241 /* Nor type unit groups. */
197400e8 3242 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3243
dee91e82
DE
3244 if (this_cu->v.quick->file_names != NULL)
3245 return this_cu->v.quick->file_names;
3246 /* If we know there is no line data, no point in looking again. */
3247 if (this_cu->v.quick->no_file_data)
3248 return NULL;
3249
ab432490 3250 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3251 if (!reader.dummy_p)
3e225074 3252 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3253
3254 if (this_cu->v.quick->no_file_data)
3255 return NULL;
3256 return this_cu->v.quick->file_names;
9291a0cd
TT
3257}
3258
3259/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3260 real path for a given file name from the line table. */
2fdf6df6 3261
9291a0cd 3262static const char *
45940949 3263dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3264 struct quick_file_names *qfn, int index)
9291a0cd 3265{
7b9f3c50 3266 if (qfn->real_names == NULL)
5989a64e 3267 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3268 qfn->num_file_names, const char *);
9291a0cd 3269
7b9f3c50 3270 if (qfn->real_names[index] == NULL)
14278e1f 3271 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3272
7b9f3c50 3273 return qfn->real_names[index];
9291a0cd
TT
3274}
3275
3276static struct symtab *
3277dw2_find_last_source_symtab (struct objfile *objfile)
3278{
ed2dc618
SM
3279 struct dwarf2_per_objfile *dwarf2_per_objfile
3280 = get_dwarf2_per_objfile (objfile);
5989a64e 3281 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3282 compunit_symtab *cust
3283 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3284
43f3e411
DE
3285 if (cust == NULL)
3286 return NULL;
ed2dc618 3287
43f3e411 3288 return compunit_primary_filetab (cust);
9291a0cd
TT
3289}
3290
7b9f3c50
DE
3291/* Traversal function for dw2_forget_cached_source_info. */
3292
3293static int
3294dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3295{
7b9f3c50 3296 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3297
7b9f3c50 3298 if (file_data->real_names)
9291a0cd 3299 {
7b9f3c50 3300 int i;
9291a0cd 3301
7b9f3c50 3302 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3303 {
7b9f3c50
DE
3304 xfree ((void*) file_data->real_names[i]);
3305 file_data->real_names[i] = NULL;
9291a0cd
TT
3306 }
3307 }
7b9f3c50
DE
3308
3309 return 1;
3310}
3311
3312static void
3313dw2_forget_cached_source_info (struct objfile *objfile)
3314{
ed2dc618
SM
3315 struct dwarf2_per_objfile *dwarf2_per_objfile
3316 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3317
5989a64e 3318 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3319 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3320}
3321
f8eba3c6
TT
3322/* Helper function for dw2_map_symtabs_matching_filename that expands
3323 the symtabs and calls the iterator. */
3324
3325static int
3326dw2_map_expand_apply (struct objfile *objfile,
3327 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3328 const char *name, const char *real_path,
14bc53a8 3329 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3330{
43f3e411 3331 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3332
3333 /* Don't visit already-expanded CUs. */
af758d11
SM
3334 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3335 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3336 return 0;
3337
3338 /* This may expand more than one symtab, and we want to iterate over
3339 all of them. */
97a1449a 3340 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3341
14bc53a8
PA
3342 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3343 last_made, callback);
f8eba3c6
TT
3344}
3345
3346/* Implementation of the map_symtabs_matching_filename method. */
3347
14bc53a8
PA
3348static bool
3349dw2_map_symtabs_matching_filename
3350 (struct objfile *objfile, const char *name, const char *real_path,
3351 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3352{
c011a4f4 3353 const char *name_basename = lbasename (name);
ed2dc618
SM
3354 struct dwarf2_per_objfile *dwarf2_per_objfile
3355 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3356
848e3e78
DE
3357 /* The rule is CUs specify all the files, including those used by
3358 any TU, so there's no need to scan TUs here. */
f4dc4d17 3359
5989a64e 3360 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3361 {
3d7bb9d9 3362 /* We only need to look at symtabs not already expanded. */
af758d11 3363 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3364 continue;
3365
ab432490
SM
3366 quick_file_names *file_data
3367 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3368 if (file_data == NULL)
9291a0cd
TT
3369 continue;
3370
b76e467d 3371 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3372 {
7b9f3c50 3373 const char *this_name = file_data->file_names[j];
da235a7c 3374 const char *this_real_name;
9291a0cd 3375
af529f8f 3376 if (compare_filenames_for_search (this_name, name))
9291a0cd 3377 {
f5b95b50 3378 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3379 callback))
3380 return true;
288e77a7 3381 continue;
4aac40c8 3382 }
9291a0cd 3383
c011a4f4
DE
3384 /* Before we invoke realpath, which can get expensive when many
3385 files are involved, do a quick comparison of the basenames. */
3386 if (! basenames_may_differ
3387 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3388 continue;
3389
45940949
TT
3390 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3391 file_data, j);
da235a7c 3392 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3393 {
da235a7c 3394 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3395 callback))
3396 return true;
288e77a7 3397 continue;
da235a7c 3398 }
9291a0cd 3399
da235a7c
JK
3400 if (real_path != NULL)
3401 {
af529f8f
JK
3402 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3403 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3404 if (this_real_name != NULL
af529f8f 3405 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3406 {
f5b95b50 3407 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3408 callback))
3409 return true;
288e77a7 3410 continue;
9291a0cd
TT
3411 }
3412 }
3413 }
3414 }
3415
14bc53a8 3416 return false;
9291a0cd
TT
3417}
3418
da51c347
DE
3419/* Struct used to manage iterating over all CUs looking for a symbol. */
3420
3421struct dw2_symtab_iterator
9291a0cd 3422{
ed2dc618
SM
3423 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3424 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3425 /* If set, only look for symbols that match that block. Valid values are
3426 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3427 gdb::optional<block_enum> block_index;
da51c347
DE
3428 /* The kind of symbol we're looking for. */
3429 domain_enum domain;
3430 /* The list of CUs from the index entry of the symbol,
3431 or NULL if not found. */
3432 offset_type *vec;
3433 /* The next element in VEC to look at. */
3434 int next;
3435 /* The number of elements in VEC, or zero if there is no match. */
3436 int length;
8943b874
DE
3437 /* Have we seen a global version of the symbol?
3438 If so we can ignore all further global instances.
3439 This is to work around gold/15646, inefficient gold-generated
3440 indices. */
3441 int global_seen;
da51c347 3442};
9291a0cd 3443
2b79f376 3444/* Initialize the index symtab iterator ITER. */
2fdf6df6 3445
9291a0cd 3446static void
da51c347 3447dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3448 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3449 gdb::optional<block_enum> block_index,
da51c347
DE
3450 domain_enum domain,
3451 const char *name)
3452{
ed2dc618 3453 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3454 iter->block_index = block_index;
3455 iter->domain = domain;
3456 iter->next = 0;
8943b874 3457 iter->global_seen = 0;
da51c347 3458
5989a64e 3459 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3460
3461 /* index is NULL if OBJF_READNOW. */
3462 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3463 iter->length = MAYBE_SWAP (*iter->vec);
3464 else
3465 {
3466 iter->vec = NULL;
3467 iter->length = 0;
3468 }
3469}
3470
3471/* Return the next matching CU or NULL if there are no more. */
3472
3473static struct dwarf2_per_cu_data *
3474dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3475{
ed2dc618
SM
3476 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3477
da51c347
DE
3478 for ( ; iter->next < iter->length; ++iter->next)
3479 {
3480 offset_type cu_index_and_attrs =
3481 MAYBE_SWAP (iter->vec[iter->next + 1]);
3482 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3483 gdb_index_symbol_kind symbol_kind =
3484 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3485 /* Only check the symbol attributes if they're present.
3486 Indices prior to version 7 don't record them,
3487 and indices >= 7 may elide them for certain symbols
3488 (gold does this). */
3489 int attrs_valid =
5989a64e 3490 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3491 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3492
3190f0c6 3493 /* Don't crash on bad data. */
5989a64e
SM
3494 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3495 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3496 {
b98664d3 3497 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3498 " [in module %s]"),
3499 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3500 continue;
3501 }
3502
5989a64e 3503 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3504
da51c347 3505 /* Skip if already read in. */
af758d11 3506 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3507 continue;
3508
8943b874
DE
3509 /* Check static vs global. */
3510 if (attrs_valid)
3511 {
2b79f376
SM
3512 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3513
3514 if (iter->block_index.has_value ())
3515 {
3516 bool want_static = *iter->block_index == STATIC_BLOCK;
3517
3518 if (is_static != want_static)
3519 continue;
3520 }
3521
8943b874
DE
3522 /* Work around gold/15646. */
3523 if (!is_static && iter->global_seen)
3524 continue;
3525 if (!is_static)
3526 iter->global_seen = 1;
3527 }
da51c347
DE
3528
3529 /* Only check the symbol's kind if it has one. */
3530 if (attrs_valid)
3531 {
3532 switch (iter->domain)
3533 {
3534 case VAR_DOMAIN:
3535 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3536 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3537 /* Some types are also in VAR_DOMAIN. */
3538 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3539 continue;
3540 break;
3541 case STRUCT_DOMAIN:
3542 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3543 continue;
3544 break;
3545 case LABEL_DOMAIN:
3546 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3547 continue;
3548 break;
59c35742
AB
3549 case MODULE_DOMAIN:
3550 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3551 continue;
3552 break;
da51c347
DE
3553 default:
3554 break;
3555 }
3556 }
3557
3558 ++iter->next;
3559 return per_cu;
3560 }
3561
3562 return NULL;
3563}
3564
43f3e411 3565static struct compunit_symtab *
c7f839cb 3566dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3567 const char *name, domain_enum domain)
9291a0cd 3568{
43f3e411 3569 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3570 struct dwarf2_per_objfile *dwarf2_per_objfile
3571 = get_dwarf2_per_objfile (objfile);
9291a0cd 3572
b5ec771e
PA
3573 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3574
ed2dc618
SM
3575 struct dw2_symtab_iterator iter;
3576 struct dwarf2_per_cu_data *per_cu;
da51c347 3577
2b79f376 3578 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3579
ed2dc618
SM
3580 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3581 {
3582 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3583 struct compunit_symtab *stab
3584 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3585 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3586 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3587
ed2dc618
SM
3588 sym = block_find_symbol (block, name, domain,
3589 block_find_non_opaque_type_preferred,
3590 &with_opaque);
b2e2f908 3591
ed2dc618
SM
3592 /* Some caution must be observed with overloaded functions
3593 and methods, since the index will not contain any overload
3594 information (but NAME might contain it). */
da51c347 3595
ed2dc618
SM
3596 if (sym != NULL
3597 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3598 return stab;
3599 if (with_opaque != NULL
3600 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3601 stab_best = stab;
da51c347 3602
ed2dc618 3603 /* Keep looking through other CUs. */
9291a0cd 3604 }
9291a0cd 3605
da51c347 3606 return stab_best;
9291a0cd
TT
3607}
3608
3609static void
3610dw2_print_stats (struct objfile *objfile)
3611{
ed2dc618
SM
3612 struct dwarf2_per_objfile *dwarf2_per_objfile
3613 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3614 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3615 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3616 int count = 0;
9291a0cd 3617
ed2dc618 3618 for (int i = 0; i < total; ++i)
9291a0cd 3619 {
5989a64e 3620 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3621
af758d11 3622 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3623 ++count;
3624 }
e4a48d9d 3625 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3626 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3627}
3628
779bd270
DE
3629/* This dumps minimal information about the index.
3630 It is called via "mt print objfiles".
3631 One use is to verify .gdb_index has been loaded by the
3632 gdb.dwarf2/gdb-index.exp testcase. */
3633
9291a0cd
TT
3634static void
3635dw2_dump (struct objfile *objfile)
3636{
ed2dc618
SM
3637 struct dwarf2_per_objfile *dwarf2_per_objfile
3638 = get_dwarf2_per_objfile (objfile);
3639
5989a64e 3640 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3641 printf_filtered (".gdb_index:");
5989a64e 3642 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3643 {
3644 printf_filtered (" version %d\n",
5989a64e 3645 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3646 }
3647 else
3648 printf_filtered (" faked for \"readnow\"\n");
3649 printf_filtered ("\n");
9291a0cd
TT
3650}
3651
9291a0cd
TT
3652static void
3653dw2_expand_symtabs_for_function (struct objfile *objfile,
3654 const char *func_name)
3655{
ed2dc618
SM
3656 struct dwarf2_per_objfile *dwarf2_per_objfile
3657 = get_dwarf2_per_objfile (objfile);
da51c347 3658
ed2dc618
SM
3659 struct dw2_symtab_iterator iter;
3660 struct dwarf2_per_cu_data *per_cu;
da51c347 3661
2b79f376 3662 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3663
ed2dc618 3664 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3665 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3666
9291a0cd
TT
3667}
3668
3669static void
3670dw2_expand_all_symtabs (struct objfile *objfile)
3671{
ed2dc618
SM
3672 struct dwarf2_per_objfile *dwarf2_per_objfile
3673 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3674 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3675 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3676
ed2dc618 3677 for (int i = 0; i < total_units; ++i)
9291a0cd 3678 {
5989a64e 3679 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3680
58f0c718
TT
3681 /* We don't want to directly expand a partial CU, because if we
3682 read it with the wrong language, then assertion failures can
3683 be triggered later on. See PR symtab/23010. So, tell
3684 dw2_instantiate_symtab to skip partial CUs -- any important
3685 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3686 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3687 }
3688}
3689
3690static void
652a8996
JK
3691dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3692 const char *fullname)
9291a0cd 3693{
ed2dc618
SM
3694 struct dwarf2_per_objfile *dwarf2_per_objfile
3695 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3696
3697 /* We don't need to consider type units here.
3698 This is only called for examining code, e.g. expand_line_sal.
3699 There can be an order of magnitude (or more) more type units
3700 than comp units, and we avoid them if we can. */
3701
5989a64e 3702 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3703 {
3d7bb9d9 3704 /* We only need to look at symtabs not already expanded. */
af758d11 3705 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3706 continue;
3707
ab432490
SM
3708 quick_file_names *file_data
3709 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3710 if (file_data == NULL)
9291a0cd
TT
3711 continue;
3712
b76e467d 3713 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3714 {
652a8996
JK
3715 const char *this_fullname = file_data->file_names[j];
3716
3717 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3718 {
97a1449a 3719 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3720 break;
3721 }
3722 }
3723 }
3724}
3725
9a0bacfb
TV
3726static void
3727dw2_expand_symtabs_matching_symbol
3728 (mapped_index_base &index,
3729 const lookup_name_info &lookup_name_in,
3730 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3731 enum search_domain kind,
3732 gdb::function_view<bool (offset_type)> match_callback);
3733
3734static void
3735dw2_expand_symtabs_matching_one
97a1449a
SM
3736 (dwarf2_per_cu_data *per_cu,
3737 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3738 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3739 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3740
9291a0cd 3741static void
199b4314
TT
3742dw2_map_matching_symbols
3743 (struct objfile *objfile,
b054970d 3744 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3745 int global,
3746 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3747 symbol_compare_ftype *ordered_compare)
9291a0cd 3748{
1aa98955
TV
3749 /* Used for Ada. */
3750 struct dwarf2_per_objfile *dwarf2_per_objfile
3751 = get_dwarf2_per_objfile (objfile);
3752
9a0bacfb
TV
3753 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3754
5989a64e 3755 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3756 {
3757 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3758 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3759 using GNU index. */
5989a64e 3760 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3761
9a0bacfb
TV
3762 const char *match_name = name.ada ().lookup_name ().c_str ();
3763 auto matcher = [&] (const char *symname)
3764 {
3765 if (ordered_compare == nullptr)
3766 return true;
3767 return ordered_compare (symname, match_name) == 0;
3768 };
3769
3770 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3771 [&] (offset_type namei)
3772 {
3773 struct dw2_symtab_iterator iter;
3774 struct dwarf2_per_cu_data *per_cu;
3775
3776 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3777 match_name);
3778 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3779 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3780 nullptr);
9a0bacfb
TV
3781 return true;
3782 });
3783 }
3784 else
3785 {
3786 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3787 proceed assuming all symtabs have been read in. */
3788 }
1aa98955
TV
3789
3790 for (compunit_symtab *cust : objfile->compunits ())
3791 {
3792 const struct block *block;
3793
3794 if (cust == NULL)
3795 continue;
3796 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3797 if (!iterate_over_symbols_terminated (block, name,
3798 domain, callback))
3799 return;
3800 }
9291a0cd
TT
3801}
3802
e1ef7d7a
PA
3803/* Starting from a search name, return the string that finds the upper
3804 bound of all strings that start with SEARCH_NAME in a sorted name
3805 list. Returns the empty string to indicate that the upper bound is
3806 the end of the list. */
3807
3808static std::string
3809make_sort_after_prefix_name (const char *search_name)
3810{
3811 /* When looking to complete "func", we find the upper bound of all
3812 symbols that start with "func" by looking for where we'd insert
3813 the closest string that would follow "func" in lexicographical
3814 order. Usually, that's "func"-with-last-character-incremented,
3815 i.e. "fund". Mind non-ASCII characters, though. Usually those
3816 will be UTF-8 multi-byte sequences, but we can't be certain.
3817 Especially mind the 0xff character, which is a valid character in
3818 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3819 rule out compilers allowing it in identifiers. Note that
3820 conveniently, strcmp/strcasecmp are specified to compare
3821 characters interpreted as unsigned char. So what we do is treat
3822 the whole string as a base 256 number composed of a sequence of
3823 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3824 to 0, and carries 1 to the following more-significant position.
3825 If the very first character in SEARCH_NAME ends up incremented
3826 and carries/overflows, then the upper bound is the end of the
3827 list. The string after the empty string is also the empty
3828 string.
3829
3830 Some examples of this operation:
3831
3832 SEARCH_NAME => "+1" RESULT
3833
3834 "abc" => "abd"
3835 "ab\xff" => "ac"
3836 "\xff" "a" "\xff" => "\xff" "b"
3837 "\xff" => ""
3838 "\xff\xff" => ""
3839 "" => ""
3840
3841 Then, with these symbols for example:
3842
3843 func
3844 func1
3845 fund
3846
3847 completing "func" looks for symbols between "func" and
3848 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3849 which finds "func" and "func1", but not "fund".
3850
3851 And with:
3852
3853 funcÿ (Latin1 'ÿ' [0xff])
3854 funcÿ1
3855 fund
3856
3857 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3858 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3859
3860 And with:
3861
3862 ÿÿ (Latin1 'ÿ' [0xff])
3863 ÿÿ1
3864
3865 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3866 the end of the list.
3867 */
3868 std::string after = search_name;
3869 while (!after.empty () && (unsigned char) after.back () == 0xff)
3870 after.pop_back ();
3871 if (!after.empty ())
3872 after.back () = (unsigned char) after.back () + 1;
3873 return after;
3874}
3875
5c58de74 3876/* See declaration. */
61d96d7e 3877
5c58de74
PA
3878std::pair<std::vector<name_component>::const_iterator,
3879 std::vector<name_component>::const_iterator>
44ed8f3e 3880mapped_index_base::find_name_components_bounds
3b00ef10 3881 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3882{
5c58de74
PA
3883 auto *name_cmp
3884 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3885
3b00ef10 3886 const char *lang_name
e0802d59 3887 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3888
3f563c84
PA
3889 /* Comparison function object for lower_bound that matches against a
3890 given symbol name. */
3891 auto lookup_compare_lower = [&] (const name_component &elem,
3892 const char *name)
3893 {
5c58de74 3894 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3895 const char *elem_name = elem_qualified + elem.name_offset;
3896 return name_cmp (elem_name, name) < 0;
3897 };
3898
3899 /* Comparison function object for upper_bound that matches against a
3900 given symbol name. */
3901 auto lookup_compare_upper = [&] (const char *name,
3902 const name_component &elem)
3903 {
5c58de74 3904 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3905 const char *elem_name = elem_qualified + elem.name_offset;
3906 return name_cmp (name, elem_name) < 0;
3907 };
3908
5c58de74
PA
3909 auto begin = this->name_components.begin ();
3910 auto end = this->name_components.end ();
3f563c84
PA
3911
3912 /* Find the lower bound. */
3913 auto lower = [&] ()
3914 {
3b00ef10 3915 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3916 return begin;
3917 else
3b00ef10 3918 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3919 } ();
3920
3921 /* Find the upper bound. */
3922 auto upper = [&] ()
3923 {
5c58de74 3924 if (lookup_name_without_params.completion_mode ())
3f563c84 3925 {
e1ef7d7a
PA
3926 /* In completion mode, we want UPPER to point past all
3927 symbols names that have the same prefix. I.e., with
3928 these symbols, and completing "func":
3929
3930 function << lower bound
3931 function1
3932 other_function << upper bound
3933
3934 We find the upper bound by looking for the insertion
3935 point of "func"-with-last-character-incremented,
3936 i.e. "fund". */
3b00ef10 3937 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3938 if (after.empty ())
3f563c84 3939 return end;
e6b2f5ef
PA
3940 return std::lower_bound (lower, end, after.c_str (),
3941 lookup_compare_lower);
3f563c84
PA
3942 }
3943 else
3b00ef10 3944 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3945 } ();
3946
5c58de74
PA
3947 return {lower, upper};
3948}
3949
3950/* See declaration. */
3951
3952void
44ed8f3e 3953mapped_index_base::build_name_components ()
5c58de74
PA
3954{
3955 if (!this->name_components.empty ())
3956 return;
3957
3958 this->name_components_casing = case_sensitivity;
3959 auto *name_cmp
3960 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3961
3962 /* The code below only knows how to break apart components of C++
3963 symbol names (and other languages that use '::' as
3b00ef10 3964 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3965 auto count = this->symbol_name_count ();
3966 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3967 {
44ed8f3e 3968 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3969 continue;
3970
3971 const char *name = this->symbol_name_at (idx);
3972
3973 /* Add each name component to the name component table. */
3974 unsigned int previous_len = 0;
3b00ef10
TT
3975
3976 if (strstr (name, "::") != nullptr)
3977 {
3978 for (unsigned int current_len = cp_find_first_component (name);
3979 name[current_len] != '\0';
3980 current_len += cp_find_first_component (name + current_len))
3981 {
3982 gdb_assert (name[current_len] == ':');
3983 this->name_components.push_back ({previous_len, idx});
3984 /* Skip the '::'. */
3985 current_len += 2;
3986 previous_len = current_len;
3987 }
3988 }
3989 else
5c58de74 3990 {
3b00ef10
TT
3991 /* Handle the Ada encoded (aka mangled) form here. */
3992 for (const char *iter = strstr (name, "__");
3993 iter != nullptr;
3994 iter = strstr (iter, "__"))
3995 {
3996 this->name_components.push_back ({previous_len, idx});
3997 iter += 2;
3998 previous_len = iter - name;
3999 }
5c58de74 4000 }
3b00ef10 4001
5c58de74
PA
4002 this->name_components.push_back ({previous_len, idx});
4003 }
4004
4005 /* Sort name_components elements by name. */
4006 auto name_comp_compare = [&] (const name_component &left,
4007 const name_component &right)
4008 {
4009 const char *left_qualified = this->symbol_name_at (left.idx);
4010 const char *right_qualified = this->symbol_name_at (right.idx);
4011
4012 const char *left_name = left_qualified + left.name_offset;
4013 const char *right_name = right_qualified + right.name_offset;
4014
4015 return name_cmp (left_name, right_name) < 0;
4016 };
4017
4018 std::sort (this->name_components.begin (),
4019 this->name_components.end (),
4020 name_comp_compare);
4021}
4022
4023/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4024 mapped_index_base instead of the containing objfile. This is split
4025 to a separate function in order to be able to unit test the
4026 name_components matching using a mock mapped_index_base. For each
5c58de74 4027 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4028 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4029
4030static void
4031dw2_expand_symtabs_matching_symbol
44ed8f3e 4032 (mapped_index_base &index,
5c58de74
PA
4033 const lookup_name_info &lookup_name_in,
4034 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4035 enum search_domain kind,
3b00ef10 4036 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4037{
4038 lookup_name_info lookup_name_without_params
4039 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4040
4041 /* Build the symbol name component sorted vector, if we haven't
4042 yet. */
4043 index.build_name_components ();
4044
3f563c84
PA
4045 /* The same symbol may appear more than once in the range though.
4046 E.g., if we're looking for symbols that complete "w", and we have
4047 a symbol named "w1::w2", we'll find the two name components for
4048 that same symbol in the range. To be sure we only call the
4049 callback once per symbol, we first collect the symbol name
4050 indexes that matched in a temporary vector and ignore
4051 duplicates. */
4052 std::vector<offset_type> matches;
3f563c84 4053
3b00ef10
TT
4054 struct name_and_matcher
4055 {
4056 symbol_name_matcher_ftype *matcher;
ecc6c606 4057 const char *name;
3b00ef10
TT
4058
4059 bool operator== (const name_and_matcher &other) const
3f563c84 4060 {
ecc6c606 4061 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4062 }
4063 };
4064
4065 /* A vector holding all the different symbol name matchers, for all
4066 languages. */
4067 std::vector<name_and_matcher> matchers;
4068
4069 for (int i = 0; i < nr_languages; i++)
4070 {
4071 enum language lang_e = (enum language) i;
4072
4073 const language_defn *lang = language_def (lang_e);
4074 symbol_name_matcher_ftype *name_matcher
4075 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4076
3b00ef10
TT
4077 name_and_matcher key {
4078 name_matcher,
4079 lookup_name_without_params.language_lookup_name (lang_e)
4080 };
4081
4082 /* Don't insert the same comparison routine more than once.
4083 Note that we do this linear walk. This is not a problem in
4084 practice because the number of supported languages is
4085 low. */
4086 if (std::find (matchers.begin (), matchers.end (), key)
4087 != matchers.end ())
9291a0cd 4088 continue;
3b00ef10
TT
4089 matchers.push_back (std::move (key));
4090
4091 auto bounds
4092 = index.find_name_components_bounds (lookup_name_without_params,
4093 lang_e);
4094
4095 /* Now for each symbol name in range, check to see if we have a name
4096 match, and if so, call the MATCH_CALLBACK callback. */
4097
4098 for (; bounds.first != bounds.second; ++bounds.first)
4099 {
4100 const char *qualified = index.symbol_name_at (bounds.first->idx);
4101
4102 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4103 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4104 continue;
9291a0cd 4105
3b00ef10
TT
4106 matches.push_back (bounds.first->idx);
4107 }
3f563c84
PA
4108 }
4109
4110 std::sort (matches.begin (), matches.end ());
4111
4112 /* Finally call the callback, once per match. */
4113 ULONGEST prev = -1;
4114 for (offset_type idx : matches)
4115 {
4116 if (prev != idx)
4117 {
3b00ef10
TT
4118 if (!match_callback (idx))
4119 break;
3f563c84
PA
4120 prev = idx;
4121 }
4122 }
4123
4124 /* Above we use a type wider than idx's for 'prev', since 0 and
4125 (offset_type)-1 are both possible values. */
4126 static_assert (sizeof (prev) > sizeof (offset_type), "");
4127}
4128
c62446b1
PA
4129#if GDB_SELF_TEST
4130
4131namespace selftests { namespace dw2_expand_symtabs_matching {
4132
a3c5fafd
PA
4133/* A mock .gdb_index/.debug_names-like name index table, enough to
4134 exercise dw2_expand_symtabs_matching_symbol, which works with the
4135 mapped_index_base interface. Builds an index from the symbol list
4136 passed as parameter to the constructor. */
4137class mock_mapped_index : public mapped_index_base
c62446b1
PA
4138{
4139public:
a3c5fafd
PA
4140 mock_mapped_index (gdb::array_view<const char *> symbols)
4141 : m_symbol_table (symbols)
c62446b1
PA
4142 {}
4143
a3c5fafd 4144 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4145
a3c5fafd 4146 /* Return the number of names in the symbol table. */
632e107b 4147 size_t symbol_name_count () const override
c62446b1 4148 {
a3c5fafd 4149 return m_symbol_table.size ();
c62446b1
PA
4150 }
4151
a3c5fafd 4152 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4153 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4154 {
4155 return m_symbol_table[idx];
4156 }
c62446b1 4157
a3c5fafd
PA
4158private:
4159 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4160};
4161
4162/* Convenience function that converts a NULL pointer to a "<null>"
4163 string, to pass to print routines. */
4164
4165static const char *
4166string_or_null (const char *str)
4167{
4168 return str != NULL ? str : "<null>";
4169}
4170
4171/* Check if a lookup_name_info built from
4172 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4173 index. EXPECTED_LIST is the list of expected matches, in expected
4174 matching order. If no match expected, then an empty list is
4175 specified. Returns true on success. On failure prints a warning
4176 indicating the file:line that failed, and returns false. */
4177
4178static bool
4179check_match (const char *file, int line,
4180 mock_mapped_index &mock_index,
4181 const char *name, symbol_name_match_type match_type,
4182 bool completion_mode,
4183 std::initializer_list<const char *> expected_list)
4184{
4185 lookup_name_info lookup_name (name, match_type, completion_mode);
4186
4187 bool matched = true;
4188
4189 auto mismatch = [&] (const char *expected_str,
4190 const char *got)
4191 {
4192 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4193 "expected=\"%s\", got=\"%s\"\n"),
4194 file, line,
4195 (match_type == symbol_name_match_type::FULL
4196 ? "FULL" : "WILD"),
4197 name, string_or_null (expected_str), string_or_null (got));
4198 matched = false;
4199 };
4200
4201 auto expected_it = expected_list.begin ();
4202 auto expected_end = expected_list.end ();
4203
a3c5fafd 4204 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4205 NULL, ALL_DOMAIN,
4206 [&] (offset_type idx)
4207 {
a3c5fafd 4208 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4209 const char *expected_str
4210 = expected_it == expected_end ? NULL : *expected_it++;
4211
4212 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4213 mismatch (expected_str, matched_name);
3b00ef10 4214 return true;
c62446b1
PA
4215 });
4216
4217 const char *expected_str
4218 = expected_it == expected_end ? NULL : *expected_it++;
4219 if (expected_str != NULL)
4220 mismatch (expected_str, NULL);
4221
4222 return matched;
4223}
4224
4225/* The symbols added to the mock mapped_index for testing (in
4226 canonical form). */
4227static const char *test_symbols[] = {
4228 "function",
4229 "std::bar",
4230 "std::zfunction",
4231 "std::zfunction2",
4232 "w1::w2",
4233 "ns::foo<char*>",
4234 "ns::foo<int>",
4235 "ns::foo<long>",
a20714ff
PA
4236 "ns2::tmpl<int>::foo2",
4237 "(anonymous namespace)::A::B::C",
c62446b1 4238
e1ef7d7a
PA
4239 /* These are used to check that the increment-last-char in the
4240 matching algorithm for completion doesn't match "t1_fund" when
4241 completing "t1_func". */
4242 "t1_func",
4243 "t1_func1",
4244 "t1_fund",
4245 "t1_fund1",
4246
4247 /* A UTF-8 name with multi-byte sequences to make sure that
4248 cp-name-parser understands this as a single identifier ("função"
4249 is "function" in PT). */
4250 u8"u8função",
4251
4252 /* \377 (0xff) is Latin1 'ÿ'. */
4253 "yfunc\377",
4254
4255 /* \377 (0xff) is Latin1 'ÿ'. */
4256 "\377",
4257 "\377\377123",
4258
c62446b1
PA
4259 /* A name with all sorts of complications. Starts with "z" to make
4260 it easier for the completion tests below. */
4261#define Z_SYM_NAME \
4262 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4263 "::tuple<(anonymous namespace)::ui*, " \
4264 "std::default_delete<(anonymous namespace)::ui>, void>"
4265
4266 Z_SYM_NAME
4267};
4268
a3c5fafd
PA
4269/* Returns true if the mapped_index_base::find_name_component_bounds
4270 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4271 in completion mode. */
5c58de74
PA
4272
4273static bool
a3c5fafd 4274check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4275 const char *search_name,
4276 gdb::array_view<const char *> expected_syms)
4277{
4278 lookup_name_info lookup_name (search_name,
4279 symbol_name_match_type::FULL, true);
4280
3b00ef10
TT
4281 auto bounds = index.find_name_components_bounds (lookup_name,
4282 language_cplus);
5c58de74
PA
4283
4284 size_t distance = std::distance (bounds.first, bounds.second);
4285 if (distance != expected_syms.size ())
4286 return false;
4287
4288 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4289 {
4290 auto nc_elem = bounds.first + exp_elem;
4291 const char *qualified = index.symbol_name_at (nc_elem->idx);
4292 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4293 return false;
4294 }
4295
4296 return true;
4297}
4298
4299/* Test the lower-level mapped_index::find_name_component_bounds
4300 method. */
4301
c62446b1 4302static void
5c58de74
PA
4303test_mapped_index_find_name_component_bounds ()
4304{
4305 mock_mapped_index mock_index (test_symbols);
4306
a3c5fafd 4307 mock_index.build_name_components ();
5c58de74
PA
4308
4309 /* Test the lower-level mapped_index::find_name_component_bounds
4310 method in completion mode. */
4311 {
4312 static const char *expected_syms[] = {
4313 "t1_func",
4314 "t1_func1",
5c58de74
PA
4315 };
4316
a3c5fafd 4317 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4318 "t1_func", expected_syms));
4319 }
4320
4321 /* Check that the increment-last-char in the name matching algorithm
4322 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4323 {
4324 static const char *expected_syms1[] = {
4325 "\377",
4326 "\377\377123",
4327 };
a3c5fafd 4328 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4329 "\377", expected_syms1));
4330
4331 static const char *expected_syms2[] = {
4332 "\377\377123",
4333 };
a3c5fafd 4334 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4335 "\377\377", expected_syms2));
4336 }
4337}
4338
4339/* Test dw2_expand_symtabs_matching_symbol. */
4340
4341static void
4342test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4343{
4344 mock_mapped_index mock_index (test_symbols);
4345
4346 /* We let all tests run until the end even if some fails, for debug
4347 convenience. */
4348 bool any_mismatch = false;
4349
4350 /* Create the expected symbols list (an initializer_list). Needed
4351 because lists have commas, and we need to pass them to CHECK,
4352 which is a macro. */
4353#define EXPECT(...) { __VA_ARGS__ }
4354
4355 /* Wrapper for check_match that passes down the current
4356 __FILE__/__LINE__. */
4357#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4358 any_mismatch |= !check_match (__FILE__, __LINE__, \
4359 mock_index, \
4360 NAME, MATCH_TYPE, COMPLETION_MODE, \
4361 EXPECTED_LIST)
4362
4363 /* Identity checks. */
4364 for (const char *sym : test_symbols)
4365 {
4366 /* Should be able to match all existing symbols. */
4367 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4368 EXPECT (sym));
4369
4370 /* Should be able to match all existing symbols with
4371 parameters. */
4372 std::string with_params = std::string (sym) + "(int)";
4373 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4374 EXPECT (sym));
4375
4376 /* Should be able to match all existing symbols with
4377 parameters and qualifiers. */
4378 with_params = std::string (sym) + " ( int ) const";
4379 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4380 EXPECT (sym));
4381
4382 /* This should really find sym, but cp-name-parser.y doesn't
4383 know about lvalue/rvalue qualifiers yet. */
4384 with_params = std::string (sym) + " ( int ) &&";
4385 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4386 {});
4387 }
4388
e1ef7d7a
PA
4389 /* Check that the name matching algorithm for completion doesn't get
4390 confused with Latin1 'ÿ' / 0xff. */
4391 {
4392 static const char str[] = "\377";
4393 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4394 EXPECT ("\377", "\377\377123"));
4395 }
4396
4397 /* Check that the increment-last-char in the matching algorithm for
4398 completion doesn't match "t1_fund" when completing "t1_func". */
4399 {
4400 static const char str[] = "t1_func";
4401 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4402 EXPECT ("t1_func", "t1_func1"));
4403 }
4404
c62446b1
PA
4405 /* Check that completion mode works at each prefix of the expected
4406 symbol name. */
4407 {
4408 static const char str[] = "function(int)";
4409 size_t len = strlen (str);
4410 std::string lookup;
4411
4412 for (size_t i = 1; i < len; i++)
4413 {
4414 lookup.assign (str, i);
4415 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4416 EXPECT ("function"));
4417 }
4418 }
4419
4420 /* While "w" is a prefix of both components, the match function
4421 should still only be called once. */
4422 {
4423 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4424 EXPECT ("w1::w2"));
a20714ff
PA
4425 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4426 EXPECT ("w1::w2"));
c62446b1
PA
4427 }
4428
4429 /* Same, with a "complicated" symbol. */
4430 {
4431 static const char str[] = Z_SYM_NAME;
4432 size_t len = strlen (str);
4433 std::string lookup;
4434
4435 for (size_t i = 1; i < len; i++)
4436 {
4437 lookup.assign (str, i);
4438 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4439 EXPECT (Z_SYM_NAME));
4440 }
4441 }
4442
4443 /* In FULL mode, an incomplete symbol doesn't match. */
4444 {
4445 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4446 {});
4447 }
4448
4449 /* A complete symbol with parameters matches any overload, since the
4450 index has no overload info. */
4451 {
4452 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4453 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4454 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4455 EXPECT ("std::zfunction", "std::zfunction2"));
4456 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4457 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4458 }
4459
4460 /* Check that whitespace is ignored appropriately. A symbol with a
4461 template argument list. */
4462 {
4463 static const char expected[] = "ns::foo<int>";
4464 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4465 EXPECT (expected));
a20714ff
PA
4466 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4467 EXPECT (expected));
c62446b1
PA
4468 }
4469
4470 /* Check that whitespace is ignored appropriately. A symbol with a
4471 template argument list that includes a pointer. */
4472 {
4473 static const char expected[] = "ns::foo<char*>";
4474 /* Try both completion and non-completion modes. */
4475 static const bool completion_mode[2] = {false, true};
4476 for (size_t i = 0; i < 2; i++)
4477 {
4478 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4479 completion_mode[i], EXPECT (expected));
a20714ff
PA
4480 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4481 completion_mode[i], EXPECT (expected));
c62446b1
PA
4482
4483 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4484 completion_mode[i], EXPECT (expected));
a20714ff
PA
4485 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4486 completion_mode[i], EXPECT (expected));
c62446b1
PA
4487 }
4488 }
4489
4490 {
4491 /* Check method qualifiers are ignored. */
4492 static const char expected[] = "ns::foo<char*>";
4493 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4494 symbol_name_match_type::FULL, true, EXPECT (expected));
4495 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4496 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4497 CHECK_MATCH ("foo < char * > ( int ) const",
4498 symbol_name_match_type::WILD, true, EXPECT (expected));
4499 CHECK_MATCH ("foo < char * > ( int ) &&",
4500 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4501 }
4502
4503 /* Test lookup names that don't match anything. */
4504 {
a20714ff
PA
4505 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4506 {});
4507
c62446b1
PA
4508 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4509 {});
4510 }
4511
a20714ff
PA
4512 /* Some wild matching tests, exercising "(anonymous namespace)",
4513 which should not be confused with a parameter list. */
4514 {
4515 static const char *syms[] = {
4516 "A::B::C",
4517 "B::C",
4518 "C",
4519 "A :: B :: C ( int )",
4520 "B :: C ( int )",
4521 "C ( int )",
4522 };
4523
4524 for (const char *s : syms)
4525 {
4526 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4527 EXPECT ("(anonymous namespace)::A::B::C"));
4528 }
4529 }
4530
4531 {
4532 static const char expected[] = "ns2::tmpl<int>::foo2";
4533 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4534 EXPECT (expected));
4535 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4536 EXPECT (expected));
4537 }
4538
c62446b1
PA
4539 SELF_CHECK (!any_mismatch);
4540
4541#undef EXPECT
4542#undef CHECK_MATCH
4543}
4544
5c58de74
PA
4545static void
4546run_test ()
4547{
4548 test_mapped_index_find_name_component_bounds ();
4549 test_dw2_expand_symtabs_matching_symbol ();
4550}
4551
c62446b1
PA
4552}} // namespace selftests::dw2_expand_symtabs_matching
4553
4554#endif /* GDB_SELF_TEST */
4555
4b514bc8
JK
4556/* If FILE_MATCHER is NULL or if PER_CU has
4557 dwarf2_per_cu_quick_data::MARK set (see
4558 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4559 EXPANSION_NOTIFY on it. */
4560
4561static void
4562dw2_expand_symtabs_matching_one
97a1449a
SM
4563 (dwarf2_per_cu_data *per_cu,
4564 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4565 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4566 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4567{
4568 if (file_matcher == NULL || per_cu->v.quick->mark)
4569 {
af758d11 4570 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4571
97a1449a
SM
4572 compunit_symtab *symtab
4573 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4574 gdb_assert (symtab != nullptr);
4b514bc8 4575
af758d11
SM
4576 if (expansion_notify != NULL && symtab_was_null)
4577 expansion_notify (symtab);
4b514bc8
JK
4578 }
4579}
4580
3f563c84
PA
4581/* Helper for dw2_expand_matching symtabs. Called on each symbol
4582 matched, to expand corresponding CUs that were marked. IDX is the
4583 index of the symbol name that matched. */
4584
4585static void
4586dw2_expand_marked_cus
97a1449a 4587 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4588 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4589 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4590 search_domain kind)
4591{
3f563c84
PA
4592 offset_type *vec, vec_len, vec_idx;
4593 bool global_seen = false;
5989a64e 4594 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4595
61920122 4596 vec = (offset_type *) (index.constant_pool
f00a2de2 4597 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4598 vec_len = MAYBE_SWAP (vec[0]);
4599 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4600 {
61920122
PA
4601 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4602 /* This value is only valid for index versions >= 7. */
4603 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4604 gdb_index_symbol_kind symbol_kind =
4605 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4606 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4607 /* Only check the symbol attributes if they're present.
4608 Indices prior to version 7 don't record them,
4609 and indices >= 7 may elide them for certain symbols
4610 (gold does this). */
4611 int attrs_valid =
4612 (index.version >= 7
4613 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4614
4615 /* Work around gold/15646. */
4616 if (attrs_valid)
9291a0cd 4617 {
61920122
PA
4618 if (!is_static && global_seen)
4619 continue;
4620 if (!is_static)
4621 global_seen = true;
4622 }
3190f0c6 4623
61920122
PA
4624 /* Only check the symbol's kind if it has one. */
4625 if (attrs_valid)
4626 {
4627 switch (kind)
8943b874 4628 {
61920122
PA
4629 case VARIABLES_DOMAIN:
4630 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4631 continue;
4632 break;
4633 case FUNCTIONS_DOMAIN:
4634 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4635 continue;
61920122
PA
4636 break;
4637 case TYPES_DOMAIN:
4638 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4639 continue;
4640 break;
59c35742
AB
4641 case MODULES_DOMAIN:
4642 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4643 continue;
4644 break;
61920122
PA
4645 default:
4646 break;
8943b874 4647 }
61920122 4648 }
8943b874 4649
61920122 4650 /* Don't crash on bad data. */
5989a64e
SM
4651 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4652 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4653 {
b98664d3 4654 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4655 " [in module %s]"),
4656 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4657 continue;
4658 }
4659
5989a64e 4660 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4661 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4662 expansion_notify);
61920122
PA
4663 }
4664}
4665
4b514bc8
JK
4666/* If FILE_MATCHER is non-NULL, set all the
4667 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4668 that match FILE_MATCHER. */
4669
61920122 4670static void
4b514bc8 4671dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4672 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4673 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4674{
4b514bc8 4675 if (file_matcher == NULL)
61920122
PA
4676 return;
4677
4b514bc8
JK
4678 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4679 htab_eq_pointer,
4680 NULL, xcalloc, xfree));
4681 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4682 htab_eq_pointer,
4683 NULL, xcalloc, xfree));
61920122 4684
4b514bc8
JK
4685 /* The rule is CUs specify all the files, including those used by
4686 any TU, so there's no need to scan TUs here. */
61920122 4687
5989a64e 4688 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4689 {
927aa2e7
JK
4690 QUIT;
4691
4692 per_cu->v.quick->mark = 0;
4693
4694 /* We only need to look at symtabs not already expanded. */
af758d11 4695 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4696 continue;
4697
ab432490
SM
4698 quick_file_names *file_data
4699 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4700 if (file_data == NULL)
4701 continue;
4702
4703 if (htab_find (visited_not_found.get (), file_data) != NULL)
4704 continue;
4705 else if (htab_find (visited_found.get (), file_data) != NULL)
4706 {
4707 per_cu->v.quick->mark = 1;
4708 continue;
4709 }
4710
b76e467d 4711 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4712 {
4713 const char *this_real_name;
4714
4715 if (file_matcher (file_data->file_names[j], false))
4716 {
4717 per_cu->v.quick->mark = 1;
4718 break;
4719 }
4720
4721 /* Before we invoke realpath, which can get expensive when many
4722 files are involved, do a quick comparison of the basenames. */
4723 if (!basenames_may_differ
4724 && !file_matcher (lbasename (file_data->file_names[j]),
4725 true))
4726 continue;
4727
45940949
TT
4728 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4729 file_data, j);
927aa2e7
JK
4730 if (file_matcher (this_real_name, false))
4731 {
4732 per_cu->v.quick->mark = 1;
4733 break;
4734 }
4735 }
4736
b76e467d
SM
4737 void **slot = htab_find_slot (per_cu->v.quick->mark
4738 ? visited_found.get ()
4739 : visited_not_found.get (),
4740 file_data, INSERT);
927aa2e7
JK
4741 *slot = file_data;
4742 }
4743}
4744
4745static void
4746dw2_expand_symtabs_matching
4747 (struct objfile *objfile,
4748 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4749 const lookup_name_info *lookup_name,
927aa2e7
JK
4750 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4751 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4752 enum search_domain kind)
4753{
ed2dc618
SM
4754 struct dwarf2_per_objfile *dwarf2_per_objfile
4755 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4756
4757 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4758 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4759 return;
4760
ed2dc618 4761 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4762
c1a66c06
TV
4763 if (symbol_matcher == NULL && lookup_name == NULL)
4764 {
5989a64e 4765 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4766 {
4767 QUIT;
4768
97a1449a
SM
4769 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4770 file_matcher, expansion_notify);
c1a66c06
TV
4771 }
4772 return;
4773 }
4774
5989a64e 4775 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4776
c1a66c06 4777 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4778 symbol_matcher,
4779 kind, [&] (offset_type idx)
4780 {
ed2dc618 4781 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4782 expansion_notify, kind);
3b00ef10 4783 return true;
927aa2e7
JK
4784 });
4785}
4786
4787/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4788 symtab. */
4789
4790static struct compunit_symtab *
4791recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4792 CORE_ADDR pc)
4793{
4794 int i;
4795
4796 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4797 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4798 return cust;
4799
4800 if (cust->includes == NULL)
4801 return NULL;
4802
4803 for (i = 0; cust->includes[i]; ++i)
4804 {
4805 struct compunit_symtab *s = cust->includes[i];
4806
4807 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4808 if (s != NULL)
4809 return s;
4810 }
4811
4812 return NULL;
4813}
4814
4815static struct compunit_symtab *
4816dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4817 struct bound_minimal_symbol msymbol,
4818 CORE_ADDR pc,
4819 struct obj_section *section,
4820 int warn_if_readin)
4821{
4822 struct dwarf2_per_cu_data *data;
4823 struct compunit_symtab *result;
4824
d320c2b5 4825 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4826 return NULL;
4827
b3b3bada 4828 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4829 data = (struct dwarf2_per_cu_data *) addrmap_find
4830 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4831 if (!data)
4832 return NULL;
4833
af758d11
SM
4834 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4835 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4836 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4837 paddress (objfile->arch (), pc));
927aa2e7 4838
97a1449a
SM
4839 result = recursively_find_pc_sect_compunit_symtab
4840 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4841
927aa2e7
JK
4842 gdb_assert (result != NULL);
4843 return result;
4844}
4845
4846static void
4847dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4848 void *data, int need_fullname)
4849{
ed2dc618
SM
4850 struct dwarf2_per_objfile *dwarf2_per_objfile
4851 = get_dwarf2_per_objfile (objfile);
927aa2e7 4852
5989a64e 4853 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4854 {
5989a64e 4855 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4856
4857 htab_up visited (htab_create_alloc (10,
4858 htab_hash_pointer, htab_eq_pointer,
4859 NULL, xcalloc, xfree));
4860
4861 /* The rule is CUs specify all the files, including those used
4862 by any TU, so there's no need to scan TUs here. We can
4863 ignore file names coming from already-expanded CUs. */
4864
5989a64e 4865 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4866 {
af758d11 4867 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4868 {
4869 void **slot = htab_find_slot (visited.get (),
4870 per_cu->v.quick->file_names,
4871 INSERT);
4872
4873 *slot = per_cu->v.quick->file_names;
4874 }
4875 }
4876
5989a64e 4877 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4878 {
927aa2e7 4879 /* We only need to look at symtabs not already expanded. */
af758d11 4880 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4881 continue;
4882
ab432490
SM
4883 quick_file_names *file_data
4884 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4885 if (file_data == NULL)
4886 continue;
4887
b76e467d 4888 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4889 if (*slot)
4890 {
4891 /* Already visited. */
4892 continue;
4893 }
4894 *slot = file_data;
4895
4896 for (int j = 0; j < file_data->num_file_names; ++j)
4897 {
4898 const char *filename = file_data->file_names[j];
5989a64e 4899 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4900 }
4901 }
4902 }
4903
5989a64e 4904 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4905 {
4906 gdb::unique_xmalloc_ptr<char> this_real_name;
4907
4908 if (need_fullname)
4909 this_real_name = gdb_realpath (filename);
4910 (*fun) (filename, this_real_name.get (), data);
4911 });
4912}
4913
4914static int
4915dw2_has_symbols (struct objfile *objfile)
4916{
4917 return 1;
4918}
4919
4920const struct quick_symbol_functions dwarf2_gdb_index_functions =
4921{
4922 dw2_has_symbols,
4923 dw2_find_last_source_symtab,
4924 dw2_forget_cached_source_info,
4925 dw2_map_symtabs_matching_filename,
4926 dw2_lookup_symbol,
d3214198 4927 NULL,
927aa2e7
JK
4928 dw2_print_stats,
4929 dw2_dump,
927aa2e7
JK
4930 dw2_expand_symtabs_for_function,
4931 dw2_expand_all_symtabs,
4932 dw2_expand_symtabs_with_fullname,
4933 dw2_map_matching_symbols,
4934 dw2_expand_symtabs_matching,
4935 dw2_find_pc_sect_compunit_symtab,
4936 NULL,
4937 dw2_map_symbol_filenames
4938};
4939
4940/* DWARF-5 debug_names reader. */
4941
4942/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4943static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4944
4945/* A helper function that reads the .debug_names section in SECTION
4946 and fills in MAP. FILENAME is the name of the file containing the
4947 section; it is used for error reporting.
4948
4949 Returns true if all went well, false otherwise. */
4950
4951static bool
4952read_debug_names_from_section (struct objfile *objfile,
4953 const char *filename,
4954 struct dwarf2_section_info *section,
4955 mapped_debug_names &map)
4956{
96b79293 4957 if (section->empty ())
927aa2e7
JK
4958 return false;
4959
4960 /* Older elfutils strip versions could keep the section in the main
4961 executable while splitting it for the separate debug info file. */
96b79293 4962 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4963 return false;
4964
96b79293 4965 section->read (objfile);
927aa2e7 4966
08feed99 4967 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4968
4969 const gdb_byte *addr = section->buffer;
4970
96b79293 4971 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4972
4973 unsigned int bytes_read;
4974 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4975 addr += bytes_read;
4976
4977 map.dwarf5_is_dwarf64 = bytes_read != 4;
4978 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4979 if (bytes_read + length != section->size)
4980 {
4981 /* There may be multiple per-CU indices. */
4982 warning (_("Section .debug_names in %s length %s does not match "
4983 "section length %s, ignoring .debug_names."),
4984 filename, plongest (bytes_read + length),
4985 pulongest (section->size));
4986 return false;
4987 }
4988
4989 /* The version number. */
4990 uint16_t version = read_2_bytes (abfd, addr);
4991 addr += 2;
4992 if (version != 5)
4993 {
4994 warning (_("Section .debug_names in %s has unsupported version %d, "
4995 "ignoring .debug_names."),
4996 filename, version);
4997 return false;
4998 }
4999
5000 /* Padding. */
5001 uint16_t padding = read_2_bytes (abfd, addr);
5002 addr += 2;
5003 if (padding != 0)
5004 {
5005 warning (_("Section .debug_names in %s has unsupported padding %d, "
5006 "ignoring .debug_names."),
5007 filename, padding);
5008 return false;
5009 }
5010
5011 /* comp_unit_count - The number of CUs in the CU list. */
5012 map.cu_count = read_4_bytes (abfd, addr);
5013 addr += 4;
5014
5015 /* local_type_unit_count - The number of TUs in the local TU
5016 list. */
5017 map.tu_count = read_4_bytes (abfd, addr);
5018 addr += 4;
5019
5020 /* foreign_type_unit_count - The number of TUs in the foreign TU
5021 list. */
5022 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5023 addr += 4;
5024 if (foreign_tu_count != 0)
5025 {
5026 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5027 "ignoring .debug_names."),
5028 filename, static_cast<unsigned long> (foreign_tu_count));
5029 return false;
5030 }
5031
5032 /* bucket_count - The number of hash buckets in the hash lookup
5033 table. */
5034 map.bucket_count = read_4_bytes (abfd, addr);
5035 addr += 4;
5036
5037 /* name_count - The number of unique names in the index. */
5038 map.name_count = read_4_bytes (abfd, addr);
5039 addr += 4;
5040
5041 /* abbrev_table_size - The size in bytes of the abbreviations
5042 table. */
5043 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5044 addr += 4;
5045
5046 /* augmentation_string_size - The size in bytes of the augmentation
5047 string. This value is rounded up to a multiple of 4. */
5048 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5049 addr += 4;
5050 map.augmentation_is_gdb = ((augmentation_string_size
5051 == sizeof (dwarf5_augmentation))
5052 && memcmp (addr, dwarf5_augmentation,
5053 sizeof (dwarf5_augmentation)) == 0);
5054 augmentation_string_size += (-augmentation_string_size) & 3;
5055 addr += augmentation_string_size;
5056
5057 /* List of CUs */
5058 map.cu_table_reordered = addr;
5059 addr += map.cu_count * map.offset_size;
5060
5061 /* List of Local TUs */
5062 map.tu_table_reordered = addr;
5063 addr += map.tu_count * map.offset_size;
5064
5065 /* Hash Lookup Table */
5066 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5067 addr += map.bucket_count * 4;
5068 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5069 addr += map.name_count * 4;
5070
5071 /* Name Table */
5072 map.name_table_string_offs_reordered = addr;
5073 addr += map.name_count * map.offset_size;
5074 map.name_table_entry_offs_reordered = addr;
5075 addr += map.name_count * map.offset_size;
5076
5077 const gdb_byte *abbrev_table_start = addr;
5078 for (;;)
5079 {
927aa2e7
JK
5080 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5081 addr += bytes_read;
5082 if (index_num == 0)
5083 break;
5084
5085 const auto insertpair
5086 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5087 if (!insertpair.second)
5088 {
5089 warning (_("Section .debug_names in %s has duplicate index %s, "
5090 "ignoring .debug_names."),
5091 filename, pulongest (index_num));
5092 return false;
5093 }
5094 mapped_debug_names::index_val &indexval = insertpair.first->second;
5095 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5096 addr += bytes_read;
5097
5098 for (;;)
5099 {
5100 mapped_debug_names::index_val::attr attr;
5101 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5102 addr += bytes_read;
5103 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5104 addr += bytes_read;
5105 if (attr.form == DW_FORM_implicit_const)
5106 {
5107 attr.implicit_const = read_signed_leb128 (abfd, addr,
5108 &bytes_read);
5109 addr += bytes_read;
5110 }
5111 if (attr.dw_idx == 0 && attr.form == 0)
5112 break;
5113 indexval.attr_vec.push_back (std::move (attr));
5114 }
5115 }
5116 if (addr != abbrev_table_start + abbrev_table_size)
5117 {
5118 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5119 "of size %s vs. written as %u, ignoring .debug_names."),
5120 filename, plongest (addr - abbrev_table_start),
5121 abbrev_table_size);
927aa2e7
JK
5122 return false;
5123 }
5124 map.entry_pool = addr;
5125
5126 return true;
5127}
5128
5129/* A helper for create_cus_from_debug_names that handles the MAP's CU
5130 list. */
5131
5132static void
ed2dc618 5133create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5134 const mapped_debug_names &map,
5135 dwarf2_section_info &section,
b76e467d 5136 bool is_dwz)
927aa2e7 5137{
3ee6bb11
TV
5138 if (!map.augmentation_is_gdb)
5139 {
5140 for (uint32_t i = 0; i < map.cu_count; ++i)
5141 {
5142 sect_offset sect_off
5143 = (sect_offset) (extract_unsigned_integer
5144 (map.cu_table_reordered + i * map.offset_size,
5145 map.offset_size,
5146 map.dwarf5_byte_order));
5147 /* We don't know the length of the CU, because the CU list in a
5148 .debug_names index can be incomplete, so we can't use the start of
5149 the next CU as end of this CU. We create the CUs here with length 0,
5150 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5151 dwarf2_per_cu_data *per_cu
5152 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5153 sect_off, 0);
5989a64e 5154 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5155 }
5156 }
5157
927aa2e7
JK
5158 sect_offset sect_off_prev;
5159 for (uint32_t i = 0; i <= map.cu_count; ++i)
5160 {
5161 sect_offset sect_off_next;
5162 if (i < map.cu_count)
5163 {
5164 sect_off_next
5165 = (sect_offset) (extract_unsigned_integer
5166 (map.cu_table_reordered + i * map.offset_size,
5167 map.offset_size,
5168 map.dwarf5_byte_order));
5169 }
5170 else
5171 sect_off_next = (sect_offset) section.size;
5172 if (i >= 1)
5173 {
5174 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5175 dwarf2_per_cu_data *per_cu
ed2dc618 5176 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5177 sect_off_prev, length);
5989a64e 5178 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5179 }
5180 sect_off_prev = sect_off_next;
5181 }
5182}
5183
5184/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5185 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5186
5187static void
ed2dc618 5188create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5189 const mapped_debug_names &map,
5190 const mapped_debug_names &dwz_map)
5191{
5989a64e
SM
5192 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5193 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5194
ed2dc618 5195 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5989a64e 5196 dwarf2_per_objfile->per_bfd->info,
b76e467d 5197 false /* is_dwz */);
927aa2e7
JK
5198
5199 if (dwz_map.cu_count == 0)
5200 return;
5201
c3699833 5202 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
ed2dc618 5203 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5204 true /* is_dwz */);
927aa2e7
JK
5205}
5206
5207/* Read .debug_names. If everything went ok, initialize the "quick"
5208 elements of all the CUs and return true. Otherwise, return false. */
5209
5210static bool
ed2dc618 5211dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5212{
22ca247e
TT
5213 std::unique_ptr<mapped_debug_names> map
5214 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5215 mapped_debug_names dwz_map (dwarf2_per_objfile);
5216 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5217
5218 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5219 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5220 *map))
927aa2e7
JK
5221 return false;
5222
5223 /* Don't use the index if it's empty. */
22ca247e 5224 if (map->name_count == 0)
927aa2e7
JK
5225 return false;
5226
5227 /* If there is a .dwz file, read it so we can get its CU list as
5228 well. */
c3699833 5229 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5230 if (dwz != NULL)
5231 {
5232 if (!read_debug_names_from_section (objfile,
00f93c44 5233 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5234 &dwz->debug_names, dwz_map))
5235 {
5236 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5237 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5238 return false;
5239 }
5240 }
5241
22ca247e 5242 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5243
22ca247e 5244 if (map->tu_count != 0)
927aa2e7
JK
5245 {
5246 /* We can only handle a single .debug_types when we have an
5247 index. */
5989a64e 5248 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5249 return false;
5250
5989a64e 5251 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5252
5253 create_signatured_type_table_from_debug_names
5989a64e 5254 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5255 }
5256
ed2dc618 5257 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5258 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5259
5989a64e
SM
5260 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5261 dwarf2_per_objfile->per_bfd->using_index = 1;
5262 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5263 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5264
5265 return true;
5266}
5267
927aa2e7
JK
5268/* Type used to manage iterating over all CUs looking for a symbol for
5269 .debug_names. */
5270
5271class dw2_debug_names_iterator
5272{
5273public:
927aa2e7 5274 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5275 gdb::optional<block_enum> block_index,
5276 domain_enum domain,
927aa2e7 5277 const char *name)
2b79f376 5278 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5279 m_addr (find_vec_in_debug_names (map, name))
5280 {}
5281
5282 dw2_debug_names_iterator (const mapped_debug_names &map,
5283 search_domain search, uint32_t namei)
5284 : m_map (map),
5285 m_search (search),
5286 m_addr (find_vec_in_debug_names (map, namei))
5287 {}
5288
3b00ef10
TT
5289 dw2_debug_names_iterator (const mapped_debug_names &map,
5290 block_enum block_index, domain_enum domain,
5291 uint32_t namei)
5292 : m_map (map), m_block_index (block_index), m_domain (domain),
5293 m_addr (find_vec_in_debug_names (map, namei))
5294 {}
5295
927aa2e7
JK
5296 /* Return the next matching CU or NULL if there are no more. */
5297 dwarf2_per_cu_data *next ();
5298
5299private:
5300 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5301 const char *name);
5302 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5303 uint32_t namei);
5304
5305 /* The internalized form of .debug_names. */
5306 const mapped_debug_names &m_map;
5307
2b79f376
SM
5308 /* If set, only look for symbols that match that block. Valid values are
5309 GLOBAL_BLOCK and STATIC_BLOCK. */
5310 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5311
5312 /* The kind of symbol we're looking for. */
5313 const domain_enum m_domain = UNDEF_DOMAIN;
5314 const search_domain m_search = ALL_DOMAIN;
5315
5316 /* The list of CUs from the index entry of the symbol, or NULL if
5317 not found. */
5318 const gdb_byte *m_addr;
5319};
5320
5321const char *
5322mapped_debug_names::namei_to_name (uint32_t namei) const
5323{
5324 const ULONGEST namei_string_offs
5325 = extract_unsigned_integer ((name_table_string_offs_reordered
5326 + namei * offset_size),
5327 offset_size,
5328 dwarf5_byte_order);
4f44ae6c
TT
5329 return read_indirect_string_at_offset (dwarf2_per_objfile,
5330 namei_string_offs);
927aa2e7
JK
5331}
5332
5333/* Find a slot in .debug_names for the object named NAME. If NAME is
5334 found, return pointer to its pool data. If NAME cannot be found,
5335 return NULL. */
5336
5337const gdb_byte *
5338dw2_debug_names_iterator::find_vec_in_debug_names
5339 (const mapped_debug_names &map, const char *name)
5340{
5341 int (*cmp) (const char *, const char *);
5342
54ee4252 5343 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5344 if (current_language->la_language == language_cplus
5345 || current_language->la_language == language_fortran
5346 || current_language->la_language == language_d)
5347 {
5348 /* NAME is already canonical. Drop any qualifiers as
5349 .debug_names does not contain any. */
5350
5351 if (strchr (name, '(') != NULL)
5352 {
54ee4252 5353 without_params = cp_remove_params (name);
927aa2e7 5354 if (without_params != NULL)
54ee4252 5355 name = without_params.get ();
927aa2e7
JK
5356 }
5357 }
5358
5359 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5360
5361 const uint32_t full_hash = dwarf5_djb_hash (name);
5362 uint32_t namei
5363 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5364 (map.bucket_table_reordered
5365 + (full_hash % map.bucket_count)), 4,
5366 map.dwarf5_byte_order);
5367 if (namei == 0)
5368 return NULL;
5369 --namei;
5370 if (namei >= map.name_count)
5371 {
b98664d3 5372 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5373 "[in module %s]"),
5374 namei, map.name_count,
ed2dc618 5375 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5376 return NULL;
5377 }
5378
5379 for (;;)
5380 {
5381 const uint32_t namei_full_hash
5382 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5383 (map.hash_table_reordered + namei), 4,
5384 map.dwarf5_byte_order);
5385 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5386 return NULL;
5387
5388 if (full_hash == namei_full_hash)
5389 {
5390 const char *const namei_string = map.namei_to_name (namei);
5391
5392#if 0 /* An expensive sanity check. */
5393 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5394 {
b98664d3 5395 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5396 "[in module %s]"),
5397 namei, objfile_name (dwarf2_per_objfile->objfile));
5398 return NULL;
5399 }
5400#endif
5401
5402 if (cmp (namei_string, name) == 0)
5403 {
5404 const ULONGEST namei_entry_offs
5405 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5406 + namei * map.offset_size),
5407 map.offset_size, map.dwarf5_byte_order);
5408 return map.entry_pool + namei_entry_offs;
5409 }
5410 }
5411
5412 ++namei;
5413 if (namei >= map.name_count)
5414 return NULL;
5415 }
5416}
5417
5418const gdb_byte *
5419dw2_debug_names_iterator::find_vec_in_debug_names
5420 (const mapped_debug_names &map, uint32_t namei)
5421{
5422 if (namei >= map.name_count)
5423 {
b98664d3 5424 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5425 "[in module %s]"),
5426 namei, map.name_count,
ed2dc618 5427 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5428 return NULL;
5429 }
5430
5431 const ULONGEST namei_entry_offs
5432 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5433 + namei * map.offset_size),
5434 map.offset_size, map.dwarf5_byte_order);
5435 return map.entry_pool + namei_entry_offs;
5436}
5437
5438/* See dw2_debug_names_iterator. */
5439
5440dwarf2_per_cu_data *
5441dw2_debug_names_iterator::next ()
5442{
5443 if (m_addr == NULL)
5444 return NULL;
5445
ed2dc618
SM
5446 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5447 struct objfile *objfile = dwarf2_per_objfile->objfile;
5448 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5449
5450 again:
5451
5452 unsigned int bytes_read;
5453 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5454 m_addr += bytes_read;
5455 if (abbrev == 0)
5456 return NULL;
5457
5458 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5459 if (indexval_it == m_map.abbrev_map.cend ())
5460 {
b98664d3 5461 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5462 "[in module %s]"),
ed2dc618 5463 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5464 return NULL;
5465 }
5466 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5467 enum class symbol_linkage {
5468 unknown,
5469 static_,
5470 extern_,
23c13d42 5471 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5472 dwarf2_per_cu_data *per_cu = NULL;
5473 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5474 {
5475 ULONGEST ull;
5476 switch (attr.form)
5477 {
5478 case DW_FORM_implicit_const:
5479 ull = attr.implicit_const;
5480 break;
5481 case DW_FORM_flag_present:
5482 ull = 1;
5483 break;
5484 case DW_FORM_udata:
5485 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5486 m_addr += bytes_read;
5487 break;
6dc55ce9 5488 case DW_FORM_ref4:
5489 ull = read_4_bytes (abfd, m_addr);
5490 m_addr += 4;
5491 break;
5492 case DW_FORM_ref8:
5493 ull = read_8_bytes (abfd, m_addr);
5494 m_addr += 8;
5495 break;
5496 case DW_FORM_ref_sig8:
5497 ull = read_8_bytes (abfd, m_addr);
5498 m_addr += 8;
5499 break;
927aa2e7 5500 default:
b98664d3 5501 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5502 dwarf_form_name (attr.form),
ed2dc618 5503 objfile_name (objfile));
927aa2e7
JK
5504 return NULL;
5505 }
5506 switch (attr.dw_idx)
5507 {
5508 case DW_IDX_compile_unit:
5509 /* Don't crash on bad data. */
5989a64e 5510 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5511 {
b98664d3 5512 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5513 " [in module %s]"),
5514 pulongest (ull),
5515 objfile_name (dwarf2_per_objfile->objfile));
5516 continue;
5517 }
5989a64e 5518 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5519 break;
8af5c486
JK
5520 case DW_IDX_type_unit:
5521 /* Don't crash on bad data. */
5989a64e 5522 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5523 {
b98664d3 5524 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5525 " [in module %s]"),
5526 pulongest (ull),
5527 objfile_name (dwarf2_per_objfile->objfile));
5528 continue;
5529 }
5989a64e 5530 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5531 break;
6dc55ce9 5532 case DW_IDX_die_offset:
5533 /* In a per-CU index (as opposed to a per-module index), index
5534 entries without CU attribute implicitly refer to the single CU. */
5535 if (per_cu == NULL)
5989a64e 5536 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5537 break;
927aa2e7
JK
5538 case DW_IDX_GNU_internal:
5539 if (!m_map.augmentation_is_gdb)
5540 break;
23c13d42 5541 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5542 break;
5543 case DW_IDX_GNU_external:
5544 if (!m_map.augmentation_is_gdb)
5545 break;
23c13d42 5546 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5547 break;
5548 }
5549 }
5550
5551 /* Skip if already read in. */
af758d11 5552 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5553 goto again;
5554
5555 /* Check static vs global. */
23c13d42 5556 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5557 {
2b79f376 5558 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5559 const bool symbol_is_static =
5560 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5561 if (want_static != symbol_is_static)
2b79f376 5562 goto again;
927aa2e7
JK
5563 }
5564
5565 /* Match dw2_symtab_iter_next, symbol_kind
5566 and debug_names::psymbol_tag. */
5567 switch (m_domain)
5568 {
5569 case VAR_DOMAIN:
5570 switch (indexval.dwarf_tag)
5571 {
5572 case DW_TAG_variable:
5573 case DW_TAG_subprogram:
5574 /* Some types are also in VAR_DOMAIN. */
5575 case DW_TAG_typedef:
5576 case DW_TAG_structure_type:
5577 break;
5578 default:
5579 goto again;
5580 }
5581 break;
5582 case STRUCT_DOMAIN:
5583 switch (indexval.dwarf_tag)
5584 {
5585 case DW_TAG_typedef:
5586 case DW_TAG_structure_type:
5587 break;
5588 default:
5589 goto again;
5590 }
5591 break;
5592 case LABEL_DOMAIN:
5593 switch (indexval.dwarf_tag)
5594 {
5595 case 0:
5596 case DW_TAG_variable:
5597 break;
5598 default:
5599 goto again;
5600 }
5601 break;
59c35742
AB
5602 case MODULE_DOMAIN:
5603 switch (indexval.dwarf_tag)
5604 {
5605 case DW_TAG_module:
5606 break;
5607 default:
5608 goto again;
5609 }
5610 break;
927aa2e7
JK
5611 default:
5612 break;
5613 }
5614
5615 /* Match dw2_expand_symtabs_matching, symbol_kind and
5616 debug_names::psymbol_tag. */
5617 switch (m_search)
4b514bc8 5618 {
927aa2e7
JK
5619 case VARIABLES_DOMAIN:
5620 switch (indexval.dwarf_tag)
4b514bc8 5621 {
927aa2e7
JK
5622 case DW_TAG_variable:
5623 break;
5624 default:
5625 goto again;
4b514bc8 5626 }
927aa2e7
JK
5627 break;
5628 case FUNCTIONS_DOMAIN:
5629 switch (indexval.dwarf_tag)
4b514bc8 5630 {
927aa2e7
JK
5631 case DW_TAG_subprogram:
5632 break;
5633 default:
5634 goto again;
4b514bc8 5635 }
927aa2e7
JK
5636 break;
5637 case TYPES_DOMAIN:
5638 switch (indexval.dwarf_tag)
5639 {
5640 case DW_TAG_typedef:
5641 case DW_TAG_structure_type:
5642 break;
5643 default:
5644 goto again;
5645 }
5646 break;
59c35742
AB
5647 case MODULES_DOMAIN:
5648 switch (indexval.dwarf_tag)
5649 {
5650 case DW_TAG_module:
5651 break;
5652 default:
5653 goto again;
5654 }
927aa2e7
JK
5655 default:
5656 break;
4b514bc8 5657 }
927aa2e7
JK
5658
5659 return per_cu;
4b514bc8 5660}
61920122 5661
927aa2e7 5662static struct compunit_symtab *
c7f839cb 5663dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5664 const char *name, domain_enum domain)
4b514bc8 5665{
ed2dc618
SM
5666 struct dwarf2_per_objfile *dwarf2_per_objfile
5667 = get_dwarf2_per_objfile (objfile);
61920122 5668
5989a64e 5669 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5670 if (!mapp)
61920122 5671 {
927aa2e7
JK
5672 /* index is NULL if OBJF_READNOW. */
5673 return NULL;
5674 }
5675 const auto &map = *mapp;
9291a0cd 5676
2b79f376 5677 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5678
927aa2e7
JK
5679 struct compunit_symtab *stab_best = NULL;
5680 struct dwarf2_per_cu_data *per_cu;
5681 while ((per_cu = iter.next ()) != NULL)
5682 {
5683 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5684 compunit_symtab *stab
5685 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5686 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5687 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5688
927aa2e7
JK
5689 sym = block_find_symbol (block, name, domain,
5690 block_find_non_opaque_type_preferred,
5691 &with_opaque);
9703b513 5692
927aa2e7
JK
5693 /* Some caution must be observed with overloaded functions and
5694 methods, since the index will not contain any overload
5695 information (but NAME might contain it). */
a3ec0bb1 5696
927aa2e7 5697 if (sym != NULL
987012b8 5698 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5699 return stab;
5700 if (with_opaque != NULL
987012b8 5701 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5702 stab_best = stab;
9703b513 5703
927aa2e7 5704 /* Keep looking through other CUs. */
9703b513
TT
5705 }
5706
927aa2e7 5707 return stab_best;
9703b513
TT
5708}
5709
927aa2e7
JK
5710/* This dumps minimal information about .debug_names. It is called
5711 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5712 uses this to verify that .debug_names has been loaded. */
9291a0cd 5713
927aa2e7
JK
5714static void
5715dw2_debug_names_dump (struct objfile *objfile)
5716{
ed2dc618
SM
5717 struct dwarf2_per_objfile *dwarf2_per_objfile
5718 = get_dwarf2_per_objfile (objfile);
5719
5989a64e 5720 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5721 printf_filtered (".debug_names:");
5989a64e 5722 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5723 printf_filtered (" exists\n");
5724 else
5725 printf_filtered (" faked for \"readnow\"\n");
5726 printf_filtered ("\n");
9291a0cd
TT
5727}
5728
9291a0cd 5729static void
927aa2e7
JK
5730dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5731 const char *func_name)
9291a0cd 5732{
ed2dc618
SM
5733 struct dwarf2_per_objfile *dwarf2_per_objfile
5734 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5735
5989a64e
SM
5736 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5737 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5738 {
5989a64e 5739 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5740
2b79f376 5741 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5742
927aa2e7
JK
5743 struct dwarf2_per_cu_data *per_cu;
5744 while ((per_cu = iter.next ()) != NULL)
97a1449a 5745 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5746 }
5747}
24c79950 5748
3b00ef10
TT
5749static void
5750dw2_debug_names_map_matching_symbols
5751 (struct objfile *objfile,
5752 const lookup_name_info &name, domain_enum domain,
5753 int global,
5754 gdb::function_view<symbol_found_callback_ftype> callback,
5755 symbol_compare_ftype *ordered_compare)
5756{
5757 struct dwarf2_per_objfile *dwarf2_per_objfile
5758 = get_dwarf2_per_objfile (objfile);
5759
5760 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5761 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5762 return;
5763
5989a64e 5764 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5765 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5766
5767 const char *match_name = name.ada ().lookup_name ().c_str ();
5768 auto matcher = [&] (const char *symname)
5769 {
5770 if (ordered_compare == nullptr)
5771 return true;
5772 return ordered_compare (symname, match_name) == 0;
5773 };
5774
5775 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5776 [&] (offset_type namei)
5777 {
5778 /* The name was matched, now expand corresponding CUs that were
5779 marked. */
5780 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5781
5782 struct dwarf2_per_cu_data *per_cu;
5783 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5784 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5785 nullptr);
3b00ef10
TT
5786 return true;
5787 });
5788
5789 /* It's a shame we couldn't do this inside the
5790 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5791 that have already been expanded. Instead, this loop matches what
5792 the psymtab code does. */
5989a64e 5793 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5794 {
af758d11
SM
5795 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5796 if (symtab != nullptr)
3b00ef10
TT
5797 {
5798 const struct block *block
af758d11 5799 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5800 if (!iterate_over_symbols_terminated (block, name,
5801 domain, callback))
5802 break;
5803 }
5804 }
5805}
5806
927aa2e7
JK
5807static void
5808dw2_debug_names_expand_symtabs_matching
5809 (struct objfile *objfile,
5810 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5811 const lookup_name_info *lookup_name,
927aa2e7
JK
5812 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5813 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5814 enum search_domain kind)
5815{
ed2dc618
SM
5816 struct dwarf2_per_objfile *dwarf2_per_objfile
5817 = get_dwarf2_per_objfile (objfile);
9291a0cd 5818
927aa2e7 5819 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5820 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5821 return;
9291a0cd 5822
ed2dc618 5823 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5824
c1a66c06
TV
5825 if (symbol_matcher == NULL && lookup_name == NULL)
5826 {
5989a64e 5827 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5828 {
5829 QUIT;
5830
97a1449a
SM
5831 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5832 file_matcher, expansion_notify);
c1a66c06
TV
5833 }
5834 return;
5835 }
5836
5989a64e 5837 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5838
c1a66c06 5839 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5840 symbol_matcher,
5841 kind, [&] (offset_type namei)
927aa2e7 5842 {
927aa2e7
JK
5843 /* The name was matched, now expand corresponding CUs that were
5844 marked. */
5845 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5846
927aa2e7
JK
5847 struct dwarf2_per_cu_data *per_cu;
5848 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5849 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5850 file_matcher, expansion_notify);
3b00ef10 5851 return true;
44ed8f3e 5852 });
9291a0cd
TT
5853}
5854
927aa2e7 5855const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5856{
5857 dw2_has_symbols,
5858 dw2_find_last_source_symtab,
5859 dw2_forget_cached_source_info,
f8eba3c6 5860 dw2_map_symtabs_matching_filename,
927aa2e7 5861 dw2_debug_names_lookup_symbol,
d3214198 5862 NULL,
9291a0cd 5863 dw2_print_stats,
927aa2e7 5864 dw2_debug_names_dump,
927aa2e7 5865 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5866 dw2_expand_all_symtabs,
652a8996 5867 dw2_expand_symtabs_with_fullname,
3b00ef10 5868 dw2_debug_names_map_matching_symbols,
927aa2e7 5869 dw2_debug_names_expand_symtabs_matching,
43f3e411 5870 dw2_find_pc_sect_compunit_symtab,
71a3c369 5871 NULL,
9291a0cd
TT
5872 dw2_map_symbol_filenames
5873};
5874
4485a1c1 5875/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5876 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5877
5878template <typename T>
5879static gdb::array_view<const gdb_byte>
5880get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5881{
5882 dwarf2_section_info *section = &section_owner->gdb_index;
5883
96b79293 5884 if (section->empty ())
4485a1c1
SM
5885 return {};
5886
5887 /* Older elfutils strip versions could keep the section in the main
5888 executable while splitting it for the separate debug info file. */
96b79293 5889 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5890 return {};
5891
96b79293 5892 section->read (obj);
4485a1c1 5893
8bebfcda
PA
5894 /* dwarf2_section_info::size is a bfd_size_type, while
5895 gdb::array_view works with size_t. On 32-bit hosts, with
5896 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5897 is 32-bit. So we need an explicit narrowing conversion here.
5898 This is fine, because it's impossible to allocate or mmap an
5899 array/buffer larger than what size_t can represent. */
5900 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5901}
5902
87d6a7aa
SM
5903/* Lookup the index cache for the contents of the index associated to
5904 DWARF2_OBJ. */
5905
5906static gdb::array_view<const gdb_byte>
5989a64e 5907get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5908{
5909 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5910 if (build_id == nullptr)
5911 return {};
5912
5913 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5914 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5915}
5916
5917/* Same as the above, but for DWZ. */
5918
5919static gdb::array_view<const gdb_byte>
5920get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5921{
5922 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5923 if (build_id == nullptr)
5924 return {};
5925
5926 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5927}
5928
3c0aa29a 5929/* See symfile.h. */
9291a0cd 5930
3c0aa29a
PA
5931bool
5932dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5933{
ed2dc618
SM
5934 struct dwarf2_per_objfile *dwarf2_per_objfile
5935 = get_dwarf2_per_objfile (objfile);
5936
9291a0cd
TT
5937 /* If we're about to read full symbols, don't bother with the
5938 indices. In this case we also don't care if some other debug
5939 format is making psymtabs, because they are all about to be
5940 expanded anyway. */
5941 if ((objfile->flags & OBJF_READNOW))
5942 {
5989a64e 5943 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5944 create_all_comp_units (dwarf2_per_objfile);
5945 create_all_type_units (dwarf2_per_objfile);
5989a64e 5946 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5947 = create_quick_file_names_table
5989a64e 5948 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5949 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5950
5989a64e
SM
5951 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5952 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5953 {
5989a64e 5954 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5955
5989a64e 5956 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5957 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5958 }
5959
5960 /* Return 1 so that gdb sees the "quick" functions. However,
5961 these functions will be no-ops because we will have expanded
5962 all symtabs. */
3c0aa29a
PA
5963 *index_kind = dw_index_kind::GDB_INDEX;
5964 return true;
9291a0cd
TT
5965 }
5966
ed2dc618 5967 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5968 {
5969 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5970 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5971 return true;
5972 }
927aa2e7 5973
4485a1c1 5974 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5975 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5976 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5977 {
5978 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5979 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5980 return true;
5981 }
9291a0cd 5982
87d6a7aa
SM
5983 /* ... otherwise, try to find the index in the index cache. */
5984 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5985 get_gdb_index_contents_from_cache,
5986 get_gdb_index_contents_from_cache_dwz))
5987 {
5988 global_index_cache.hit ();
5989 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5990 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5991 return true;
5992 }
5993
5994 global_index_cache.miss ();
3c0aa29a 5995 return false;
9291a0cd
TT
5996}
5997
5998\f
5999
dce234bc
PP
6000/* Build a partial symbol table. */
6001
6002void
f29dff0a 6003dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6004{
ed2dc618
SM
6005 struct dwarf2_per_objfile *dwarf2_per_objfile
6006 = get_dwarf2_per_objfile (objfile);
c9bf0622 6007
6eee24ce 6008 init_psymbol_list (objfile, 1024);
c906108c 6009
a70b8144 6010 try
c9bf0622
TT
6011 {
6012 /* This isn't really ideal: all the data we allocate on the
6013 objfile's obstack is still uselessly kept around. However,
6014 freeing it seems unsafe. */
906768f9 6015 psymtab_discarder psymtabs (objfile);
ed2dc618 6016 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6017 psymtabs.keep ();
87d6a7aa 6018
af758d11
SM
6019 dwarf2_per_objfile->resize_symtabs ();
6020
87d6a7aa
SM
6021 /* (maybe) store an index in the cache. */
6022 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6023 }
230d2906 6024 catch (const gdb_exception_error &except)
492d29ea
PA
6025 {
6026 exception_print (gdb_stderr, except);
6027 }
c906108c 6028}
c906108c 6029
3b80fe9b
DE
6030/* Find the base address of the compilation unit for range lists and
6031 location lists. It will normally be specified by DW_AT_low_pc.
6032 In DWARF-3 draft 4, the base address could be overridden by
6033 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6034 compilation units with discontinuous ranges. */
6035
6036static void
6037dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6038{
6039 struct attribute *attr;
6040
2b24b6e4 6041 cu->base_address.reset ();
3b80fe9b
DE
6042
6043 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6044 if (attr != nullptr)
2b24b6e4 6045 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6046 else
6047 {
6048 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6049 if (attr != nullptr)
2b24b6e4 6050 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6051 }
6052}
6053
36586728
TT
6054/* Helper function that returns the proper abbrev section for
6055 THIS_CU. */
6056
6057static struct dwarf2_section_info *
6058get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6059{
6060 struct dwarf2_section_info *abbrev;
c3699833 6061 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6062
6063 if (this_cu->is_dwz)
c3699833 6064 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6065 else
c3699833 6066 abbrev = &per_bfd->abbrev;
36586728
TT
6067
6068 return abbrev;
6069}
6070
f4dc4d17
DE
6071/* Fetch the abbreviation table offset from a comp or type unit header. */
6072
6073static sect_offset
ed2dc618
SM
6074read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6075 struct dwarf2_section_info *section,
9c541725 6076 sect_offset sect_off)
f4dc4d17 6077{
96b79293 6078 bfd *abfd = section->get_bfd_owner ();
d521ce57 6079 const gdb_byte *info_ptr;
ac298888 6080 unsigned int initial_length_size, offset_size;
43988095 6081 uint16_t version;
f4dc4d17 6082
96b79293 6083 section->read (dwarf2_per_objfile->objfile);
9c541725 6084 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6085 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6086 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6087 info_ptr += initial_length_size;
6088
6089 version = read_2_bytes (abfd, info_ptr);
6090 info_ptr += 2;
6091 if (version >= 5)
6092 {
6093 /* Skip unit type and address size. */
6094 info_ptr += 2;
6095 }
6096
24aa364d 6097 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6098}
6099
b83470bf
TT
6100/* A partial symtab that is used only for include files. */
6101struct dwarf2_include_psymtab : public partial_symtab
6102{
6103 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6104 : partial_symtab (filename, objfile)
6105 {
6106 }
6107
6108 void read_symtab (struct objfile *objfile) override
6109 {
194d088f
TV
6110 /* It's an include file, no symbols to read for it.
6111 Everything is in the includer symtab. */
6112
6113 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6114 expansion of the includer psymtab. We use the dependencies[0] field to
6115 model the includer. But if we go the regular route of calling
6116 expand_psymtab here, and having expand_psymtab call expand_dependencies
6117 to expand the includer, we'll only use expand_psymtab on the includer
6118 (making it a non-toplevel psymtab), while if we expand the includer via
6119 another path, we'll use read_symtab (making it a toplevel psymtab).
6120 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6121 psymtab, and trigger read_symtab on the includer here directly. */
6122 includer ()->read_symtab (objfile);
b83470bf
TT
6123 }
6124
6125 void expand_psymtab (struct objfile *objfile) override
6126 {
194d088f
TV
6127 /* This is not called by read_symtab, and should not be called by any
6128 expand_dependencies. */
6129 gdb_assert (false);
b83470bf
TT
6130 }
6131
5717c425 6132 bool readin_p (struct objfile *objfile) const override
b83470bf 6133 {
5717c425 6134 return includer ()->readin_p (objfile);
b83470bf
TT
6135 }
6136
5717c425 6137 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6138 {
6139 return nullptr;
6140 }
6141
6142private:
194d088f
TV
6143 partial_symtab *includer () const
6144 {
6145 /* An include psymtab has exactly one dependency: the psymtab that
6146 includes it. */
6147 gdb_assert (this->number_of_dependencies == 1);
6148 return this->dependencies[0];
6149 }
b83470bf
TT
6150};
6151
aaa75496
JB
6152/* Allocate a new partial symtab for file named NAME and mark this new
6153 partial symtab as being an include of PST. */
6154
6155static void
891813be 6156dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6157 struct objfile *objfile)
6158{
b83470bf 6159 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6160
fbd9ab74 6161 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6162 subpst->dirname = pst->dirname;
fbd9ab74 6163
a9342b62 6164 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6165 subpst->dependencies[0] = pst;
6166 subpst->number_of_dependencies = 1;
aaa75496
JB
6167}
6168
6169/* Read the Line Number Program data and extract the list of files
6170 included by the source file represented by PST. Build an include
d85a05f0 6171 partial symtab for each of these included files. */
aaa75496
JB
6172
6173static void
6174dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6175 struct die_info *die,
891813be 6176 dwarf2_psymtab *pst)
aaa75496 6177{
fff8551c 6178 line_header_up lh;
d85a05f0 6179 struct attribute *attr;
aaa75496 6180
d85a05f0 6181 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6182 if (attr != nullptr)
9c541725 6183 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6184 if (lh == NULL)
6185 return; /* No linetable, so no includes. */
6186
79748972
TT
6187 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6188 that we pass in the raw text_low here; that is ok because we're
6189 only decoding the line table to make include partial symtabs, and
6190 so the addresses aren't really used. */
4ae976d1 6191 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6192 pst->raw_text_low (), 1);
aaa75496
JB
6193}
6194
348e048f 6195static hashval_t
52dc124a 6196hash_signatured_type (const void *item)
348e048f 6197{
9a3c8263
SM
6198 const struct signatured_type *sig_type
6199 = (const struct signatured_type *) item;
9a619af0 6200
348e048f 6201 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6202 return sig_type->signature;
348e048f
DE
6203}
6204
6205static int
52dc124a 6206eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6207{
9a3c8263
SM
6208 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6209 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6210
348e048f
DE
6211 return lhs->signature == rhs->signature;
6212}
6213
1fd400ff
TT
6214/* Allocate a hash table for signatured types. */
6215
b0b6a987 6216static htab_up
298e9637 6217allocate_signatured_type_table ()
1fd400ff 6218{
b0b6a987
TT
6219 return htab_up (htab_create_alloc (41,
6220 hash_signatured_type,
6221 eq_signatured_type,
6222 NULL, xcalloc, xfree));
1fd400ff
TT
6223}
6224
d467dd73 6225/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6226
6227static int
d467dd73 6228add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6229{
9a3c8263 6230 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6231 std::vector<signatured_type *> *all_type_units
6232 = (std::vector<signatured_type *> *) datum;
1fd400ff 6233
b2bdb8cf 6234 all_type_units->push_back (sigt);
1fd400ff
TT
6235
6236 return 1;
6237}
6238
78d4d2c5 6239/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6240 and fill them into TYPES_HTAB. It will process only type units,
6241 therefore DW_UT_type. */
c88ee1f0 6242
78d4d2c5 6243static void
ed2dc618
SM
6244create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6245 struct dwo_file *dwo_file,
b0b6a987 6246 dwarf2_section_info *section, htab_up &types_htab,
43988095 6247 rcuh_kind section_kind)
348e048f 6248{
3019eac3 6249 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6250 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6251 bfd *abfd;
6252 const gdb_byte *info_ptr, *end_ptr;
348e048f 6253
4bdcc0c1
DE
6254 abbrev_section = (dwo_file != NULL
6255 ? &dwo_file->sections.abbrev
5989a64e 6256 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6257
b4f54984 6258 if (dwarf_read_debug)
43988095 6259 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6260 section->get_name (),
6261 abbrev_section->get_file_name ());
09406207 6262
96b79293 6263 section->read (objfile);
78d4d2c5 6264 info_ptr = section->buffer;
348e048f 6265
78d4d2c5
JK
6266 if (info_ptr == NULL)
6267 return;
348e048f 6268
78d4d2c5
JK
6269 /* We can't set abfd until now because the section may be empty or
6270 not present, in which case the bfd is unknown. */
96b79293 6271 abfd = section->get_bfd_owner ();
348e048f 6272
c0ab21c2
TT
6273 /* We don't use cutu_reader here because we don't need to read
6274 any dies: the signature is in the header. */
3019eac3 6275
78d4d2c5
JK
6276 end_ptr = info_ptr + section->size;
6277 while (info_ptr < end_ptr)
6278 {
78d4d2c5
JK
6279 struct signatured_type *sig_type;
6280 struct dwo_unit *dwo_tu;
6281 void **slot;
6282 const gdb_byte *ptr = info_ptr;
6283 struct comp_unit_head header;
6284 unsigned int length;
8b70b953 6285
9c541725 6286 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6287
a49dd8dd
JK
6288 /* Initialize it due to a false compiler warning. */
6289 header.signature = -1;
9c541725 6290 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6291
78d4d2c5
JK
6292 /* We need to read the type's signature in order to build the hash
6293 table, but we don't need anything else just yet. */
348e048f 6294
ed2dc618 6295 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6296 abbrev_section, ptr, section_kind);
348e048f 6297
4057dfde 6298 length = header.get_length ();
6caca83c 6299
78d4d2c5
JK
6300 /* Skip dummy type units. */
6301 if (ptr >= info_ptr + length
43988095
JK
6302 || peek_abbrev_code (abfd, ptr) == 0
6303 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6304 {
6305 info_ptr += length;
6306 continue;
6307 }
dee91e82 6308
78d4d2c5
JK
6309 if (types_htab == NULL)
6310 {
6311 if (dwo_file)
298e9637 6312 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6313 else
298e9637 6314 types_htab = allocate_signatured_type_table ();
78d4d2c5 6315 }
8b70b953 6316
78d4d2c5
JK
6317 if (dwo_file)
6318 {
6319 sig_type = NULL;
5989a64e 6320 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6321 struct dwo_unit);
6322 dwo_tu->dwo_file = dwo_file;
43988095 6323 dwo_tu->signature = header.signature;
9c541725 6324 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6325 dwo_tu->section = section;
9c541725 6326 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6327 dwo_tu->length = length;
6328 }
6329 else
6330 {
6331 /* N.B.: type_offset is not usable if this type uses a DWO file.
6332 The real type_offset is in the DWO file. */
6333 dwo_tu = NULL;
5989a64e 6334 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6335 sig_type->signature = header.signature;
9c541725 6336 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6337 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6338 sig_type->per_cu.is_debug_types = 1;
6339 sig_type->per_cu.section = section;
9c541725 6340 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6341 sig_type->per_cu.length = length;
6342 }
6343
b0b6a987 6344 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6345 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6346 INSERT);
6347 gdb_assert (slot != NULL);
6348 if (*slot != NULL)
6349 {
9c541725 6350 sect_offset dup_sect_off;
0349ea22 6351
3019eac3
DE
6352 if (dwo_file)
6353 {
78d4d2c5
JK
6354 const struct dwo_unit *dup_tu
6355 = (const struct dwo_unit *) *slot;
6356
9c541725 6357 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6358 }
6359 else
6360 {
78d4d2c5
JK
6361 const struct signatured_type *dup_tu
6362 = (const struct signatured_type *) *slot;
6363
9c541725 6364 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6365 }
8b70b953 6366
b98664d3 6367 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6368 " the entry at offset %s, signature %s"),
6369 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6370 hex_string (header.signature));
78d4d2c5
JK
6371 }
6372 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6373
78d4d2c5 6374 if (dwarf_read_debug > 1)
9d8780f0
SM
6375 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6376 sect_offset_str (sect_off),
43988095 6377 hex_string (header.signature));
3019eac3 6378
78d4d2c5
JK
6379 info_ptr += length;
6380 }
6381}
3019eac3 6382
78d4d2c5
JK
6383/* Create the hash table of all entries in the .debug_types
6384 (or .debug_types.dwo) section(s).
6385 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6386 otherwise it is NULL.
b3c8eb43 6387
78d4d2c5 6388 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6389
78d4d2c5 6390 Note: This function processes DWO files only, not DWP files. */
348e048f 6391
78d4d2c5 6392static void
ed2dc618
SM
6393create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6394 struct dwo_file *dwo_file,
fd5866f6 6395 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6396 htab_up &types_htab)
78d4d2c5 6397{
fd5866f6
SM
6398 for (dwarf2_section_info &section : type_sections)
6399 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6400 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6401}
6402
6403/* Create the hash table of all entries in the .debug_types section,
6404 and initialize all_type_units.
6405 The result is zero if there is an error (e.g. missing .debug_types section),
6406 otherwise non-zero. */
6407
6408static int
ed2dc618 6409create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6410{
b0b6a987 6411 htab_up types_htab;
3019eac3 6412
ed2dc618 6413 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6414 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6415 rcuh_kind::COMPILE);
ed2dc618 6416 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6417 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6418 if (types_htab == NULL)
6419 {
5989a64e 6420 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6421 return 0;
6422 }
6423
5989a64e 6424 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6425
5989a64e
SM
6426 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6427 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6428 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6429
5989a64e 6430 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6431 add_signatured_type_cu_to_table,
5989a64e 6432 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6433
348e048f
DE
6434 return 1;
6435}
6436
5989a64e 6437/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6438 If SLOT is non-NULL, it is the entry to use in the hash table.
6439 Otherwise we find one. */
6440
6441static struct signatured_type *
ed2dc618
SM
6442add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6443 void **slot)
6aa5f3a6 6444{
5989a64e
SM
6445 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6446 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6447 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6448
5989a64e 6449 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6450
af758d11
SM
6451 dwarf2_per_objfile->resize_symtabs ();
6452
5989a64e 6453 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6454 sig_type->signature = sig;
6455 sig_type->per_cu.is_debug_types = 1;
5989a64e 6456 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6457 {
6458 sig_type->per_cu.v.quick =
5989a64e 6459 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6460 struct dwarf2_per_cu_quick_data);
6461 }
6462
6463 if (slot == NULL)
6464 {
5989a64e 6465 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6466 sig_type, INSERT);
6467 }
6468 gdb_assert (*slot == NULL);
6469 *slot = sig_type;
6470 /* The rest of sig_type must be filled in by the caller. */
6471 return sig_type;
6472}
6473
a2ce51a0
DE
6474/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6475 Fill in SIG_ENTRY with DWO_ENTRY. */
6476
6477static void
ed2dc618 6478fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6479 struct signatured_type *sig_entry,
6480 struct dwo_unit *dwo_entry)
6481{
1859c670
SM
6482 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6483
7ee85ab1 6484 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6485 gdb_assert (! sig_entry->per_cu.queued);
6486 gdb_assert (sig_entry->per_cu.cu == NULL);
1859c670 6487 if (per_bfd->using_index)
6aa5f3a6
DE
6488 {
6489 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6490 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6491 }
6492 else
6493 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6494 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6495 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6496 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6497 gdb_assert (sig_entry->dwo_unit == NULL);
6498
6499 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6500 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6501 sig_entry->per_cu.length = dwo_entry->length;
6502 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6503 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 6504 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6505 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6506 sig_entry->dwo_unit = dwo_entry;
6507}
6508
6509/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6510 If we haven't read the TU yet, create the signatured_type data structure
6511 for a TU to be read in directly from a DWO file, bypassing the stub.
6512 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6513 using .gdb_index, then when reading a CU we want to stay in the DWO file
6514 containing that CU. Otherwise we could end up reading several other DWO
6515 files (due to comdat folding) to process the transitive closure of all the
6516 mentioned TUs, and that can be slow. The current DWO file will have every
6517 type signature that it needs.
a2ce51a0
DE
6518 We only do this for .gdb_index because in the psymtab case we already have
6519 to read all the DWOs to build the type unit groups. */
6520
6521static struct signatured_type *
6522lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6523{
5e22e966 6524 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6525 struct dwo_file *dwo_file;
6526 struct dwo_unit find_dwo_entry, *dwo_entry;
6527 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6528 void **slot;
a2ce51a0 6529
5989a64e 6530 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6531
6aa5f3a6
DE
6532 /* If TU skeletons have been removed then we may not have read in any
6533 TUs yet. */
5989a64e
SM
6534 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6535 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6536
6537 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6538 Use the global signatured_types array to do our own comdat-folding
6539 of types. If this is the first time we're reading this TU, and
6540 the TU has an entry in .gdb_index, replace the recorded data from
6541 .gdb_index with this TU. */
a2ce51a0 6542
a2ce51a0 6543 find_sig_entry.signature = sig;
5989a64e 6544 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6545 &find_sig_entry, INSERT);
9a3c8263 6546 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6547
6548 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6549 read. Don't reassign the global entry to point to this DWO if that's
6550 the case. Also note that if the TU is already being read, it may not
6551 have come from a DWO, the program may be a mix of Fission-compiled
6552 code and non-Fission-compiled code. */
6553
6554 /* Have we already tried to read this TU?
6555 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6556 needn't exist in the global table yet). */
6557 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6558 return sig_entry;
6559
6aa5f3a6
DE
6560 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6561 dwo_unit of the TU itself. */
6562 dwo_file = cu->dwo_unit->dwo_file;
6563
a2ce51a0
DE
6564 /* Ok, this is the first time we're reading this TU. */
6565 if (dwo_file->tus == NULL)
6566 return NULL;
6567 find_dwo_entry.signature = sig;
b0b6a987
TT
6568 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6569 &find_dwo_entry);
a2ce51a0
DE
6570 if (dwo_entry == NULL)
6571 return NULL;
6572
6aa5f3a6
DE
6573 /* If the global table doesn't have an entry for this TU, add one. */
6574 if (sig_entry == NULL)
ed2dc618 6575 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6576
ed2dc618 6577 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6578 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6579 return sig_entry;
6580}
6581
a2ce51a0
DE
6582/* Subroutine of lookup_signatured_type.
6583 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6584 then try the DWP file. If the TU stub (skeleton) has been removed then
6585 it won't be in .gdb_index. */
a2ce51a0
DE
6586
6587static struct signatured_type *
6588lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6589{
5e22e966 6590 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6591 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6592 struct dwo_unit *dwo_entry;
6593 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6594 void **slot;
a2ce51a0 6595
5989a64e 6596 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6597 gdb_assert (dwp_file != NULL);
6598
6aa5f3a6
DE
6599 /* If TU skeletons have been removed then we may not have read in any
6600 TUs yet. */
5989a64e
SM
6601 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6602 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6603
6aa5f3a6 6604 find_sig_entry.signature = sig;
5989a64e 6605 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6606 &find_sig_entry, INSERT);
9a3c8263 6607 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6608
6609 /* Have we already tried to read this TU?
6610 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6611 needn't exist in the global table yet). */
6612 if (sig_entry != NULL)
6613 return sig_entry;
6614
a2ce51a0
DE
6615 if (dwp_file->tus == NULL)
6616 return NULL;
ed2dc618 6617 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6618 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6619 if (dwo_entry == NULL)
6620 return NULL;
6621
ed2dc618
SM
6622 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6623 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6624
a2ce51a0
DE
6625 return sig_entry;
6626}
6627
380bca97 6628/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6629 Returns NULL if signature SIG is not present in the table.
6630 It is up to the caller to complain about this. */
348e048f
DE
6631
6632static struct signatured_type *
a2ce51a0 6633lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6634{
5e22e966 6635 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6636
a2ce51a0 6637 if (cu->dwo_unit
5989a64e 6638 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6639 {
6640 /* We're in a DWO/DWP file, and we're using .gdb_index.
6641 These cases require special processing. */
ed2dc618 6642 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6643 return lookup_dwo_signatured_type (cu, sig);
6644 else
6645 return lookup_dwp_signatured_type (cu, sig);
6646 }
6647 else
6648 {
6649 struct signatured_type find_entry, *entry;
348e048f 6650
5989a64e 6651 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6652 return NULL;
6653 find_entry.signature = sig;
9a3c8263 6654 entry = ((struct signatured_type *)
5989a64e 6655 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6656 &find_entry));
a2ce51a0
DE
6657 return entry;
6658 }
348e048f 6659}
18a8505e 6660
42e7ad6c 6661/* Low level DIE reading support. */
348e048f 6662
d85a05f0
DJ
6663/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6664
6665static void
6666init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6667 struct dwarf2_cu *cu,
3019eac3 6668 struct dwarf2_section_info *section,
685af9cd
TT
6669 struct dwo_file *dwo_file,
6670 struct abbrev_table *abbrev_table)
d85a05f0 6671{
fceca515 6672 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6673 reader->abfd = section->get_bfd_owner ();
d85a05f0 6674 reader->cu = cu;
3019eac3 6675 reader->dwo_file = dwo_file;
dee91e82
DE
6676 reader->die_section = section;
6677 reader->buffer = section->buffer;
f664829e 6678 reader->buffer_end = section->buffer + section->size;
685af9cd 6679 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6680}
6681
c0ab21c2 6682/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6683 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6684 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6685 already.
6686
6687 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6688 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6689 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6690 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6691 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6692 STUB_COMP_DIR may be non-NULL.
3e225074 6693 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6694 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6695 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6696 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6697 kept around for at least as long as *RESULT_READER.
6698
b0c7bfa9
DE
6699 The result is non-zero if a valid (non-dummy) DIE was found. */
6700
6701static int
4ab09049 6702read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6703 struct dwo_unit *dwo_unit,
b0c7bfa9 6704 struct die_info *stub_comp_unit_die,
a2ce51a0 6705 const char *stub_comp_dir,
b0c7bfa9 6706 struct die_reader_specs *result_reader,
d521ce57 6707 const gdb_byte **result_info_ptr,
b0c7bfa9 6708 struct die_info **result_comp_unit_die,
685af9cd 6709 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6710{
4ab09049
SM
6711 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6712 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6713 struct objfile *objfile = dwarf2_per_objfile->objfile;
b0c7bfa9 6714 bfd *abfd;
d521ce57 6715 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6716 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6717 int i,num_extra_attrs;
6718 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6719 struct die_info *comp_unit_die;
6720
b0aeadb3
DE
6721 /* At most one of these may be provided. */
6722 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6723
b0c7bfa9
DE
6724 /* These attributes aren't processed until later:
6725 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6726 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6727 referenced later. However, these attributes are found in the stub
6728 which we won't have later. In order to not impose this complication
6729 on the rest of the code, we read them here and copy them to the
6730 DWO CU/TU die. */
b0c7bfa9
DE
6731
6732 stmt_list = NULL;
6733 low_pc = NULL;
6734 high_pc = NULL;
6735 ranges = NULL;
6736 comp_dir = NULL;
6737
6738 if (stub_comp_unit_die != NULL)
6739 {
6740 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6741 DWO file. */
4ab09049 6742 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6743 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6744 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6745 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6746 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6747 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6748
a39fdb41 6749 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6750
18a8505e
AT
6751 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6752 here (if needed). We need the value before we can process
6753 DW_AT_ranges. */
a39fdb41 6754 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6755 }
a2ce51a0
DE
6756 else if (stub_comp_dir != NULL)
6757 {
6758 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6759 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6760 comp_dir->name = DW_AT_comp_dir;
6761 comp_dir->form = DW_FORM_string;
6762 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6763 DW_STRING (comp_dir) = stub_comp_dir;
6764 }
b0c7bfa9
DE
6765
6766 /* Set up for reading the DWO CU/TU. */
6767 cu->dwo_unit = dwo_unit;
685af9cd 6768 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6769 section->read (objfile);
6770 abfd = section->get_bfd_owner ();
9c541725
PA
6771 begin_info_ptr = info_ptr = (section->buffer
6772 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6773 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6774
4ab09049 6775 if (per_cu->is_debug_types)
b0c7bfa9 6776 {
4ab09049 6777 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6778
ed2dc618
SM
6779 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6780 &cu->header, section,
b0c7bfa9 6781 dwo_abbrev_section,
43988095 6782 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6783 /* This is not an assert because it can be caused by bad debug info. */
43988095 6784 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6785 {
6786 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6787 " TU at offset %s [in module %s]"),
a2ce51a0 6788 hex_string (sig_type->signature),
43988095 6789 hex_string (cu->header.signature),
9d8780f0 6790 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6791 bfd_get_filename (abfd));
6792 }
9c541725 6793 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6794 /* For DWOs coming from DWP files, we don't know the CU length
6795 nor the type's offset in the TU until now. */
4057dfde 6796 dwo_unit->length = cu->header.get_length ();
9c541725 6797 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6798
6799 /* Establish the type offset that can be used to lookup the type.
6800 For DWO files, we don't know it until now. */
9c541725
PA
6801 sig_type->type_offset_in_section
6802 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6803 }
6804 else
6805 {
ed2dc618
SM
6806 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6807 &cu->header, section,
b0c7bfa9 6808 dwo_abbrev_section,
43988095 6809 info_ptr, rcuh_kind::COMPILE);
9c541725 6810 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6811 /* For DWOs coming from DWP files, we don't know the CU length
6812 until now. */
4057dfde 6813 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6814 }
6815
685af9cd 6816 *result_dwo_abbrev_table
86de1d91
TT
6817 = abbrev_table::read (objfile, dwo_abbrev_section,
6818 cu->header.abbrev_sect_off);
685af9cd
TT
6819 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6820 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6821
6822 /* Read in the die, but leave space to copy over the attributes
6823 from the stub. This has the benefit of simplifying the rest of
6824 the code - all the work to maintain the illusion of a single
6825 DW_TAG_{compile,type}_unit DIE is done here. */
6826 num_extra_attrs = ((stmt_list != NULL)
6827 + (low_pc != NULL)
6828 + (high_pc != NULL)
6829 + (ranges != NULL)
6830 + (comp_dir != NULL));
6831 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6832 num_extra_attrs);
b0c7bfa9
DE
6833
6834 /* Copy over the attributes from the stub to the DIE we just read in. */
6835 comp_unit_die = *result_comp_unit_die;
6836 i = comp_unit_die->num_attrs;
6837 if (stmt_list != NULL)
6838 comp_unit_die->attrs[i++] = *stmt_list;
6839 if (low_pc != NULL)
6840 comp_unit_die->attrs[i++] = *low_pc;
6841 if (high_pc != NULL)
6842 comp_unit_die->attrs[i++] = *high_pc;
6843 if (ranges != NULL)
6844 comp_unit_die->attrs[i++] = *ranges;
6845 if (comp_dir != NULL)
6846 comp_unit_die->attrs[i++] = *comp_dir;
6847 comp_unit_die->num_attrs += num_extra_attrs;
6848
b4f54984 6849 if (dwarf_die_debug)
bf6af496
DE
6850 {
6851 fprintf_unfiltered (gdb_stdlog,
6852 "Read die from %s@0x%x of %s:\n",
96b79293 6853 section->get_name (),
bf6af496
DE
6854 (unsigned) (begin_info_ptr - section->buffer),
6855 bfd_get_filename (abfd));
b4f54984 6856 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6857 }
6858
b0c7bfa9
DE
6859 /* Skip dummy compilation units. */
6860 if (info_ptr >= begin_info_ptr + dwo_unit->length
6861 || peek_abbrev_code (abfd, info_ptr) == 0)
6862 return 0;
6863
6864 *result_info_ptr = info_ptr;
6865 return 1;
6866}
6867
a084a2a6
AT
6868/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6869 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6870 signature is part of the header. */
6871static gdb::optional<ULONGEST>
6872lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6873{
6874 if (cu->header.version >= 5)
6875 return cu->header.signature;
6876 struct attribute *attr;
6877 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6878 if (attr == nullptr)
6879 return gdb::optional<ULONGEST> ();
6880 return DW_UNSND (attr);
6881}
6882
c0ab21c2 6883/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6884 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6885 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6886
6887static struct dwo_unit *
4ab09049 6888lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6889{
4ab09049 6890 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6891 struct dwo_unit *dwo_unit;
c0ab21c2 6892 const char *comp_dir;
b0c7bfa9 6893
a2ce51a0
DE
6894 gdb_assert (cu != NULL);
6895
b0c7bfa9 6896 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6897 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6898 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6899
4ab09049
SM
6900 if (per_cu->is_debug_types)
6901 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6902 else
6903 {
a084a2a6 6904 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6905
a084a2a6 6906 if (!signature.has_value ())
b0c7bfa9
DE
6907 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6908 " [in module %s]"),
4ab09049
SM
6909 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6910
6911 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6912 }
6913
b0c7bfa9
DE
6914 return dwo_unit;
6915}
6916
c0ab21c2 6917/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6918 See it for a description of the parameters.
fcd3b13d 6919 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6920
c0ab21c2 6921void
9e021579
SM
6922cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6923 dwarf2_per_objfile *per_objfile,
6751ebae 6924 int use_existing_cu)
a2ce51a0 6925{
a2ce51a0 6926 struct signatured_type *sig_type;
a2ce51a0
DE
6927
6928 /* Verify we can do the following downcast, and that we have the
6929 data we need. */
6930 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6931 sig_type = (struct signatured_type *) this_cu;
6932 gdb_assert (sig_type->dwo_unit != NULL);
6933
6aa5f3a6
DE
6934 if (use_existing_cu && this_cu->cu != NULL)
6935 {
6936 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6937 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6938 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6939 }
6940 else
6941 {
6942 /* If !use_existing_cu, this_cu->cu must be NULL. */
6943 gdb_assert (this_cu->cu == NULL);
9e021579 6944 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6aa5f3a6
DE
6945 }
6946
6947 /* A future optimization, if needed, would be to use an existing
6948 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6949 could share abbrev tables. */
a2ce51a0 6950
4ab09049 6951 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
a2ce51a0
DE
6952 NULL /* stub_comp_unit_die */,
6953 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6954 this, &info_ptr,
3e225074 6955 &comp_unit_die,
c0ab21c2 6956 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6957 {
6958 /* Dummy die. */
c0ab21c2 6959 dummy_p = true;
a2ce51a0 6960 }
a2ce51a0
DE
6961}
6962
fd820528 6963/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6964 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6965
f4dc4d17
DE
6966 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6967 Otherwise the table specified in the comp unit header is read in and used.
6968 This is an optimization for when we already have the abbrev table.
6969
dee91e82 6970 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6971 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6972
ab432490
SM
6973cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6974 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2 6975 struct abbrev_table *abbrev_table,
6751ebae 6976 int use_existing_cu,
c0ab21c2
TT
6977 bool skip_partial)
6978 : die_reader_specs {},
6751ebae 6979 m_this_cu (this_cu)
c906108c 6980{
dee91e82 6981 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6982 struct dwarf2_section_info *section = this_cu->section;
96b79293 6983 bfd *abfd = section->get_bfd_owner ();
dee91e82 6984 struct dwarf2_cu *cu;
c0ab21c2 6985 const gdb_byte *begin_info_ptr;
dee91e82 6986 struct signatured_type *sig_type = NULL;
4bdcc0c1 6987 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6988 /* Non-zero if CU currently points to a DWO file and we need to
6989 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6990 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6991 int rereading_dwo_cu = 0;
c906108c 6992
b4f54984 6993 if (dwarf_die_debug)
9d8780f0 6994 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6995 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6996 sect_offset_str (this_cu->sect_off));
09406207 6997
a2ce51a0
DE
6998 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6999 file (instead of going through the stub), short-circuit all of this. */
7000 if (this_cu->reading_dwo_directly)
7001 {
7002 /* Narrow down the scope of possibilities to have to understand. */
7003 gdb_assert (this_cu->is_debug_types);
7004 gdb_assert (abbrev_table == NULL);
9e021579 7005 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
a2ce51a0
DE
7006 return;
7007 }
7008
dee91e82 7009 /* This is cheap if the section is already read in. */
96b79293 7010 section->read (objfile);
dee91e82 7011
9c541725 7012 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7013
7014 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7015
7016 if (use_existing_cu && this_cu->cu != NULL)
7017 {
7018 cu = this_cu->cu;
42e7ad6c
DE
7019 /* If this CU is from a DWO file we need to start over, we need to
7020 refetch the attributes from the skeleton CU.
7021 This could be optimized by retrieving those attributes from when we
7022 were here the first time: the previous comp_unit_die was stored in
7023 comp_unit_obstack. But there's no data yet that we need this
7024 optimization. */
7025 if (cu->dwo_unit != NULL)
7026 rereading_dwo_cu = 1;
dee91e82
DE
7027 }
7028 else
7029 {
7030 /* If !use_existing_cu, this_cu->cu must be NULL. */
7031 gdb_assert (this_cu->cu == NULL);
9e021579 7032 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 7033 cu = m_new_cu.get ();
42e7ad6c 7034 }
dee91e82 7035
b0c7bfa9 7036 /* Get the header. */
9c541725 7037 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7038 {
7039 /* We already have the header, there's no need to read it in again. */
9c541725 7040 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7041 }
7042 else
7043 {
3019eac3 7044 if (this_cu->is_debug_types)
dee91e82 7045 {
ed2dc618
SM
7046 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7047 &cu->header, section,
4bdcc0c1 7048 abbrev_section, info_ptr,
43988095 7049 rcuh_kind::TYPE);
dee91e82 7050
42e7ad6c
DE
7051 /* Since per_cu is the first member of struct signatured_type,
7052 we can go from a pointer to one to a pointer to the other. */
7053 sig_type = (struct signatured_type *) this_cu;
43988095 7054 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7055 gdb_assert (sig_type->type_offset_in_tu
7056 == cu->header.type_cu_offset_in_tu);
7057 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7058
42e7ad6c
DE
7059 /* LENGTH has not been set yet for type units if we're
7060 using .gdb_index. */
4057dfde 7061 this_cu->length = cu->header.get_length ();
3019eac3
DE
7062
7063 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7064 sig_type->type_offset_in_section =
7065 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7066
7067 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7068 }
7069 else
7070 {
ed2dc618
SM
7071 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7072 &cu->header, section,
4bdcc0c1 7073 abbrev_section,
43988095
JK
7074 info_ptr,
7075 rcuh_kind::COMPILE);
dee91e82 7076
9c541725 7077 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7078 if (this_cu->length == 0)
7079 this_cu->length = cu->header.get_length ();
7080 else
7081 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7082 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7083 }
7084 }
10b3939b 7085
6caca83c 7086 /* Skip dummy compilation units. */
dee91e82 7087 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7088 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7089 {
7090 dummy_p = true;
7091 return;
7092 }
6caca83c 7093
433df2d4
DE
7094 /* If we don't have them yet, read the abbrevs for this compilation unit.
7095 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7096 done. */
f4dc4d17 7097 if (abbrev_table != NULL)
685af9cd
TT
7098 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7099 else
f4dc4d17 7100 {
c0ab21c2 7101 m_abbrev_table_holder
86de1d91
TT
7102 = abbrev_table::read (objfile, abbrev_section,
7103 cu->header.abbrev_sect_off);
c0ab21c2 7104 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7105 }
af703f96 7106
dee91e82 7107 /* Read the top level CU/TU die. */
c0ab21c2 7108 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7109 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7110
58f0c718 7111 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7112 {
7113 dummy_p = true;
7114 return;
7115 }
58f0c718 7116
b0c7bfa9 7117 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7118 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7119 table from the DWO file and pass the ownership over to us. It will be
7120 referenced from READER, so we must make sure to free it after we're done
7121 with READER.
7122
b0c7bfa9
DE
7123 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7124 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7125 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7126 if (dwo_name != nullptr)
3019eac3 7127 {
3019eac3 7128 struct dwo_unit *dwo_unit;
b0c7bfa9 7129 struct die_info *dwo_comp_unit_die;
3019eac3 7130
3e225074 7131 if (comp_unit_die->has_children)
6a506a2d 7132 {
b98664d3 7133 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7134 " has children (offset %s) [in module %s]"),
7135 sect_offset_str (this_cu->sect_off),
7136 bfd_get_filename (abfd));
6a506a2d 7137 }
4ab09049 7138 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7139 if (dwo_unit != NULL)
3019eac3 7140 {
4ab09049 7141 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7142 comp_unit_die, NULL,
c0ab21c2 7143 this, &info_ptr,
3e225074 7144 &dwo_comp_unit_die,
c0ab21c2 7145 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7146 {
7147 /* Dummy die. */
c0ab21c2 7148 dummy_p = true;
6a506a2d
DE
7149 return;
7150 }
7151 comp_unit_die = dwo_comp_unit_die;
7152 }
7153 else
7154 {
7155 /* Yikes, we couldn't find the rest of the DIE, we only have
7156 the stub. A complaint has already been logged. There's
7157 not much more we can do except pass on the stub DIE to
7158 die_reader_func. We don't want to throw an error on bad
7159 debug info. */
3019eac3
DE
7160 }
7161 }
c0ab21c2 7162}
3019eac3 7163
6751ebae
TT
7164void
7165cutu_reader::keep ()
c0ab21c2 7166{
b0c7bfa9 7167 /* Done, clean up. */
6751ebae
TT
7168 gdb_assert (!dummy_p);
7169 if (m_new_cu != NULL)
348e048f 7170 {
e3beb21d
SM
7171 /* We know that m_this_cu->cu is set, since we are in the process of
7172 parsing the CU. */
7173 gdb_assert (m_this_cu->cu != nullptr);
7174 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7175
fcd3b13d 7176 /* Link this CU into read_in_chain. */
5989a64e
SM
7177 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7178 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
fcd3b13d 7179 /* The chain owns it now. */
c0ab21c2 7180 m_new_cu.release ();
348e048f 7181 }
dee91e82
DE
7182}
7183
18a8505e
AT
7184/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7185 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7186 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7187
7188 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7189 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7190
7191 We fill in THIS_CU->length.
7192
dee91e82 7193 THIS_CU->cu is always freed when done.
3019eac3 7194 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7195 to care whether it refers to the "main" CU or the DWO CU.
7196
7197 When parent_cu is passed, it is used to provide a default value for
7198 str_offsets_base and addr_base from the parent. */
dee91e82 7199
ab432490
SM
7200cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7201 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2
TT
7202 struct dwarf2_cu *parent_cu,
7203 struct dwo_file *dwo_file)
7204 : die_reader_specs {},
7205 m_this_cu (this_cu)
dee91e82
DE
7206{
7207 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7208 struct dwarf2_section_info *section = this_cu->section;
96b79293 7209 bfd *abfd = section->get_bfd_owner ();
33e80786 7210 struct dwarf2_section_info *abbrev_section;
d521ce57 7211 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7212
b4f54984 7213 if (dwarf_die_debug)
9d8780f0 7214 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7215 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7216 sect_offset_str (this_cu->sect_off));
09406207 7217
dee91e82
DE
7218 gdb_assert (this_cu->cu == NULL);
7219
33e80786
DE
7220 abbrev_section = (dwo_file != NULL
7221 ? &dwo_file->sections.abbrev
7222 : get_abbrev_section_for_cu (this_cu));
7223
dee91e82 7224 /* This is cheap if the section is already read in. */
96b79293 7225 section->read (objfile);
dee91e82 7226
9e021579 7227 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7228
9c541725 7229 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7230 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7231 &m_new_cu->header, section,
4bdcc0c1 7232 abbrev_section, info_ptr,
43988095
JK
7233 (this_cu->is_debug_types
7234 ? rcuh_kind::TYPE
7235 : rcuh_kind::COMPILE));
dee91e82 7236
18a8505e
AT
7237 if (parent_cu != nullptr)
7238 {
c0ab21c2
TT
7239 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7240 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7241 }
4057dfde 7242 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7243
7244 /* Skip dummy compilation units. */
7245 if (info_ptr >= begin_info_ptr + this_cu->length
7246 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7247 {
7248 dummy_p = true;
7249 return;
7250 }
72bf9492 7251
c0ab21c2 7252 m_abbrev_table_holder
86de1d91
TT
7253 = abbrev_table::read (objfile, abbrev_section,
7254 m_new_cu->header.abbrev_sect_off);
dee91e82 7255
c0ab21c2
TT
7256 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7257 m_abbrev_table_holder.get ());
3e225074 7258 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7259}
7260
0018ea6f
DE
7261\f
7262/* Type Unit Groups.
dee91e82 7263
0018ea6f
DE
7264 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7265 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7266 so that all types coming from the same compilation (.o file) are grouped
7267 together. A future step could be to put the types in the same symtab as
7268 the CU the types ultimately came from. */
ff013f42 7269
f4dc4d17
DE
7270static hashval_t
7271hash_type_unit_group (const void *item)
7272{
9a3c8263
SM
7273 const struct type_unit_group *tu_group
7274 = (const struct type_unit_group *) item;
f4dc4d17 7275
094b34ac 7276 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7277}
348e048f
DE
7278
7279static int
f4dc4d17 7280eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7281{
9a3c8263
SM
7282 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7283 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7284
094b34ac 7285 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7286}
348e048f 7287
f4dc4d17
DE
7288/* Allocate a hash table for type unit groups. */
7289
eaa5fa8b 7290static htab_up
298e9637 7291allocate_type_unit_groups_table ()
f4dc4d17 7292{
eaa5fa8b
TT
7293 return htab_up (htab_create_alloc (3,
7294 hash_type_unit_group,
7295 eq_type_unit_group,
7296 NULL, xcalloc, xfree));
f4dc4d17 7297}
dee91e82 7298
f4dc4d17
DE
7299/* Type units that don't have DW_AT_stmt_list are grouped into their own
7300 partial symtabs. We combine several TUs per psymtab to not let the size
7301 of any one psymtab grow too big. */
7302#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7303#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7304
094b34ac 7305/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7306 Create the type_unit_group object used to hold one or more TUs. */
7307
7308static struct type_unit_group *
094b34ac 7309create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7310{
5e22e966 7311 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7312 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7313 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7314 struct type_unit_group *tu_group;
f4dc4d17 7315
5989a64e 7316 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7317 struct type_unit_group);
094b34ac 7318 per_cu = &tu_group->per_cu;
518817b3 7319 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 7320 per_cu->per_bfd = per_bfd;
f4dc4d17 7321
1859c670 7322 if (per_bfd->using_index)
094b34ac 7323 {
1859c670 7324 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7325 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7326 }
7327 else
7328 {
9c541725 7329 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7330 dwarf2_psymtab *pst;
528e1572 7331 std::string name;
094b34ac
DE
7332
7333 /* Give the symtab a useful name for debug purposes. */
7334 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7335 name = string_printf ("<type_units_%d>",
7336 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7337 else
528e1572 7338 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7339
7aa104c4 7340 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
6d94535f 7341 pst->anonymous = true;
094b34ac 7342 }
f4dc4d17 7343
094b34ac 7344 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7345 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7346
7347 return tu_group;
7348}
7349
094b34ac
DE
7350/* Look up the type_unit_group for type unit CU, and create it if necessary.
7351 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7352
7353static struct type_unit_group *
ff39bb5e 7354get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7355{
5e22e966 7356 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7357 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7358 struct type_unit_group *tu_group;
7359 void **slot;
7360 unsigned int line_offset;
7361 struct type_unit_group type_unit_group_for_lookup;
7362
5989a64e
SM
7363 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7364 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7365
7366 /* Do we need to create a new group, or can we use an existing one? */
7367
7368 if (stmt_list)
7369 {
7370 line_offset = DW_UNSND (stmt_list);
7371 ++tu_stats->nr_symtab_sharers;
7372 }
7373 else
7374 {
7375 /* Ugh, no stmt_list. Rare, but we have to handle it.
7376 We can do various things here like create one group per TU or
7377 spread them over multiple groups to split up the expansion work.
7378 To avoid worst case scenarios (too many groups or too large groups)
7379 we, umm, group them in bunches. */
7380 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7381 | (tu_stats->nr_stmt_less_type_units
7382 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7383 ++tu_stats->nr_stmt_less_type_units;
7384 }
7385
094b34ac 7386 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7387 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7388 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7389 &type_unit_group_for_lookup, INSERT);
7390 if (*slot != NULL)
7391 {
9a3c8263 7392 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7393 gdb_assert (tu_group != NULL);
7394 }
7395 else
7396 {
9c541725 7397 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7398 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7399 *slot = tu_group;
7400 ++tu_stats->nr_symtabs;
7401 }
7402
7403 return tu_group;
7404}
0018ea6f
DE
7405\f
7406/* Partial symbol tables. */
7407
7408/* Create a psymtab named NAME and assign it to PER_CU.
7409
7410 The caller must fill in the following details:
7411 dirname, textlow, texthigh. */
7412
891813be 7413static dwarf2_psymtab *
7aa104c4
SM
7414create_partial_symtab (dwarf2_per_cu_data *per_cu,
7415 dwarf2_per_objfile *per_objfile,
7416 const char *name)
0018ea6f 7417{
7aa104c4 7418 struct objfile *objfile = per_objfile->objfile;
891813be 7419 dwarf2_psymtab *pst;
0018ea6f 7420
9f4e76a4 7421 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7422
6d94535f 7423 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7424
7425 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7426 per_cu->v.psymtab = pst;
7427
7428 return pst;
7429}
7430
c0ab21c2 7431/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7432
7433static void
7434process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7435 const gdb_byte *info_ptr,
0018ea6f 7436 struct die_info *comp_unit_die,
c0ab21c2 7437 enum language pretend_language)
0018ea6f
DE
7438{
7439 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7440 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7441 struct objfile *objfile = per_objfile->objfile;
08feed99 7442 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7443 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7444 CORE_ADDR baseaddr;
7445 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7446 dwarf2_psymtab *pst;
3a2b436a 7447 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7448 const char *filename;
0018ea6f 7449
0018ea6f
DE
7450 gdb_assert (! per_cu->is_debug_types);
7451
c0ab21c2 7452 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7453
0018ea6f 7454 /* Allocate a new partial symbol table structure. */
2e927613
TV
7455 gdb::unique_xmalloc_ptr<char> debug_filename;
7456 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7457 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7458 if (filename == NULL)
0018ea6f 7459 filename = "";
2e927613
TV
7460 else if (strcmp (filename, artificial) == 0)
7461 {
7462 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7463 sect_offset_str (per_cu->sect_off),
7464 (char *) NULL));
2e927613
TV
7465 filename = debug_filename.get ();
7466 }
0018ea6f 7467
7aa104c4 7468 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7469
7470 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7471 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7472
b3b3bada 7473 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7474
7475 dwarf2_find_base_address (comp_unit_die, cu);
7476
7477 /* Possibly set the default values of LOWPC and HIGHPC from
7478 `DW_AT_ranges'. */
3a2b436a
JK
7479 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7480 &best_highpc, cu, pst);
7481 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7482 {
7483 CORE_ADDR low
7484 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7485 - baseaddr);
7486 CORE_ADDR high
7487 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7488 - baseaddr - 1);
7489 /* Store the contiguous range if it is not empty; it can be
7490 empty for CUs with no code. */
d320c2b5
TT
7491 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7492 low, high, pst);
79748972 7493 }
0018ea6f
DE
7494
7495 /* Check if comp unit has_children.
7496 If so, read the rest of the partial symbols from this comp unit.
7497 If not, there's no more debug_info for this comp unit. */
3e225074 7498 if (comp_unit_die->has_children)
0018ea6f
DE
7499 {
7500 struct partial_die_info *first_die;
7501 CORE_ADDR lowpc, highpc;
7502
7503 lowpc = ((CORE_ADDR) -1);
7504 highpc = ((CORE_ADDR) 0);
7505
7506 first_die = load_partial_dies (reader, info_ptr, 1);
7507
7508 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7509 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7510
7511 /* If we didn't find a lowpc, set it to highpc to avoid
7512 complaints from `maint check'. */
7513 if (lowpc == ((CORE_ADDR) -1))
7514 lowpc = highpc;
7515
7516 /* If the compilation unit didn't have an explicit address range,
7517 then use the information extracted from its child dies. */
e385593e 7518 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7519 {
7520 best_lowpc = lowpc;
7521 best_highpc = highpc;
7522 }
7523 }
4ae976d1 7524 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7525 best_lowpc + baseaddr)
7526 - baseaddr);
4ae976d1 7527 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7528 best_highpc + baseaddr)
7529 - baseaddr);
0018ea6f 7530
8763cede 7531 end_psymtab_common (objfile, pst);
0018ea6f 7532
ae640021 7533 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7534 {
7535 int i;
ae640021 7536 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7537
7538 /* Fill in 'dependencies' here; we fill in 'users' in a
7539 post-pass. */
7540 pst->number_of_dependencies = len;
a9342b62
TT
7541 pst->dependencies
7542 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7543 for (i = 0; i < len; ++i)
7544 {
7545 pst->dependencies[i]
7546 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7547 }
0018ea6f 7548
ae640021 7549 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7550 }
7551
7552 /* Get the list of files included in the current compilation unit,
7553 and build a psymtab for each of them. */
7554 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7555
b4f54984 7556 if (dwarf_read_debug)
b926417a
TT
7557 fprintf_unfiltered (gdb_stdlog,
7558 "Psymtab for %s unit @%s: %s - %s"
7559 ", %d global, %d static syms\n",
7560 per_cu->is_debug_types ? "type" : "comp",
7561 sect_offset_str (per_cu->sect_off),
7562 paddress (gdbarch, pst->text_low (objfile)),
7563 paddress (gdbarch, pst->text_high (objfile)),
7564 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7565}
7566
7567/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7568 Process compilation unit THIS_CU for a psymtab. */
7569
7570static void
ab432490
SM
7571process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7572 dwarf2_per_objfile *per_objfile,
135f5437 7573 bool want_partial_unit,
b93601f3 7574 enum language pretend_language)
0018ea6f
DE
7575{
7576 /* If this compilation unit was already read in, free the
7577 cached copy in order to read it in again. This is
7578 necessary because we skipped some symbols when we first
7579 read in the compilation unit (see load_partial_dies).
7580 This problem could be avoided, but the benefit is unclear. */
7581 if (this_cu->cu != NULL)
7582 free_one_cached_comp_unit (this_cu);
7583
ab432490 7584 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
c0ab21c2 7585
58990295
TV
7586 switch (reader.comp_unit_die->tag)
7587 {
7588 case DW_TAG_compile_unit:
7589 this_cu->unit_type = DW_UT_compile;
7590 break;
7591 case DW_TAG_partial_unit:
7592 this_cu->unit_type = DW_UT_partial;
7593 break;
7594 default:
7595 abort ();
7596 }
7597
c0ab21c2 7598 if (reader.dummy_p)
f1902523 7599 {
c0ab21c2 7600 /* Nothing. */
f1902523 7601 }
c0ab21c2 7602 else if (this_cu->is_debug_types)
3e225074
TT
7603 build_type_psymtabs_reader (&reader, reader.info_ptr,
7604 reader.comp_unit_die);
135f5437
TT
7605 else if (want_partial_unit
7606 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7607 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7608 reader.comp_unit_die,
c0ab21c2 7609 pretend_language);
0018ea6f 7610
58990295
TV
7611 this_cu->lang = this_cu->cu->language;
7612
0018ea6f 7613 /* Age out any secondary CUs. */
ed2dc618 7614 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7615}
f4dc4d17
DE
7616
7617/* Reader function for build_type_psymtabs. */
7618
7619static void
7620build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7621 const gdb_byte *info_ptr,
3e225074 7622 struct die_info *type_unit_die)
f4dc4d17 7623{
5e22e966 7624 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7625 struct objfile *objfile = dwarf2_per_objfile->objfile;
7626 struct dwarf2_cu *cu = reader->cu;
7627 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7628 struct signatured_type *sig_type;
f4dc4d17
DE
7629 struct type_unit_group *tu_group;
7630 struct attribute *attr;
7631 struct partial_die_info *first_die;
7632 CORE_ADDR lowpc, highpc;
891813be 7633 dwarf2_psymtab *pst;
f4dc4d17 7634
0186c6a7
DE
7635 gdb_assert (per_cu->is_debug_types);
7636 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7637
3e225074 7638 if (! type_unit_die->has_children)
f4dc4d17
DE
7639 return;
7640
052c8bb8 7641 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7642 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7643
df07e2c7 7644 if (tu_group->tus == nullptr)
a8b3b8e9 7645 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7646 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7647
7648 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7aa104c4 7649 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
6d94535f 7650 pst->anonymous = true;
f4dc4d17
DE
7651
7652 first_die = load_partial_dies (reader, info_ptr, 1);
7653
7654 lowpc = (CORE_ADDR) -1;
7655 highpc = (CORE_ADDR) 0;
7656 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7657
8763cede 7658 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7659}
7660
73051182
DE
7661/* Struct used to sort TUs by their abbreviation table offset. */
7662
7663struct tu_abbrev_offset
7664{
b2bdb8cf
SM
7665 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7666 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7667 {}
7668
7669 signatured_type *sig_type;
73051182
DE
7670 sect_offset abbrev_offset;
7671};
7672
484cf504 7673/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7674
484cf504
TT
7675static bool
7676sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7677 const struct tu_abbrev_offset &b)
73051182 7678{
484cf504 7679 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7680}
7681
7682/* Efficiently read all the type units.
7683 This does the bulk of the work for build_type_psymtabs.
7684
7685 The efficiency is because we sort TUs by the abbrev table they use and
7686 only read each abbrev table once. In one program there are 200K TUs
7687 sharing 8K abbrev tables.
7688
7689 The main purpose of this function is to support building the
5989a64e 7690 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7691 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7692 can collapse the search space by grouping them by stmt_list.
7693 The savings can be significant, in the same program from above the 200K TUs
7694 share 8K stmt_list tables.
7695
7696 FUNC is expected to call get_type_unit_group, which will create the
7697 struct type_unit_group if necessary and add it to
5989a64e 7698 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7699
7700static void
ed2dc618 7701build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7702{
5989a64e 7703 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7704 abbrev_table_up abbrev_table;
73051182 7705 sect_offset abbrev_offset;
73051182
DE
7706
7707 /* It's up to the caller to not call us multiple times. */
5989a64e 7708 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7709
5989a64e 7710 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7711 return;
7712
7713 /* TUs typically share abbrev tables, and there can be way more TUs than
7714 abbrev tables. Sort by abbrev table to reduce the number of times we
7715 read each abbrev table in.
7716 Alternatives are to punt or to maintain a cache of abbrev tables.
7717 This is simpler and efficient enough for now.
7718
7719 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7720 symtab to use). Typically TUs with the same abbrev offset have the same
7721 stmt_list value too so in practice this should work well.
7722
7723 The basic algorithm here is:
7724
7725 sort TUs by abbrev table
7726 for each TU with same abbrev table:
7727 read abbrev table if first user
7728 read TU top level DIE
7729 [IWBN if DWO skeletons had DW_AT_stmt_list]
7730 call FUNC */
7731
b4f54984 7732 if (dwarf_read_debug)
73051182
DE
7733 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7734
7735 /* Sort in a separate table to maintain the order of all_type_units
7736 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7737 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7738 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7739
5989a64e 7740 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7741 sorted_by_abbrev.emplace_back
7742 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7743 sig_type->per_cu.section,
7744 sig_type->per_cu.sect_off));
73051182 7745
484cf504
TT
7746 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7747 sort_tu_by_abbrev_offset);
73051182 7748
9c541725 7749 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7750
b2bdb8cf 7751 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7752 {
73051182
DE
7753 /* Switch to the next abbrev table if necessary. */
7754 if (abbrev_table == NULL
b2bdb8cf 7755 || tu.abbrev_offset != abbrev_offset)
73051182 7756 {
b2bdb8cf 7757 abbrev_offset = tu.abbrev_offset;
73051182 7758 abbrev_table =
86de1d91 7759 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7760 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7761 abbrev_offset);
73051182
DE
7762 ++tu_stats->nr_uniq_abbrev_tables;
7763 }
7764
ab432490
SM
7765 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7766 abbrev_table.get (), 0, false);
c0ab21c2
TT
7767 if (!reader.dummy_p)
7768 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7769 reader.comp_unit_die);
73051182 7770 }
6aa5f3a6 7771}
73051182 7772
6aa5f3a6
DE
7773/* Print collected type unit statistics. */
7774
7775static void
ed2dc618 7776print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7777{
5989a64e 7778 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7779
7780 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7781 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7782 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7783 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7784 tu_stats->nr_uniq_abbrev_tables);
7785 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7786 tu_stats->nr_symtabs);
7787 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7788 tu_stats->nr_symtab_sharers);
7789 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7790 tu_stats->nr_stmt_less_type_units);
7791 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7792 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7793}
7794
f4dc4d17
DE
7795/* Traversal function for build_type_psymtabs. */
7796
7797static int
7798build_type_psymtab_dependencies (void **slot, void *info)
7799{
ed2dc618
SM
7800 struct dwarf2_per_objfile *dwarf2_per_objfile
7801 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7802 struct objfile *objfile = dwarf2_per_objfile->objfile;
7803 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7804 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7805 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7806 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7807 int i;
7808
7809 gdb_assert (len > 0);
197400e8 7810 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7811
7812 pst->number_of_dependencies = len;
a9342b62 7813 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7814 for (i = 0; i < len; ++i)
f4dc4d17 7815 {
df07e2c7 7816 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7817 gdb_assert (iter->per_cu.is_debug_types);
7818 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7819 iter->type_unit_group = tu_group;
f4dc4d17
DE
7820 }
7821
df07e2c7
AB
7822 delete tu_group->tus;
7823 tu_group->tus = nullptr;
348e048f
DE
7824
7825 return 1;
7826}
7827
7828/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7829 Build partial symbol tables for the .debug_types comp-units. */
7830
7831static void
ed2dc618 7832build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7833{
ed2dc618 7834 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7835 return;
7836
ed2dc618 7837 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7838}
f4dc4d17 7839
6aa5f3a6
DE
7840/* Traversal function for process_skeletonless_type_unit.
7841 Read a TU in a DWO file and build partial symbols for it. */
7842
7843static int
7844process_skeletonless_type_unit (void **slot, void *info)
7845{
7846 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7847 struct dwarf2_per_objfile *dwarf2_per_objfile
7848 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7849 struct signatured_type find_entry, *entry;
7850
7851 /* If this TU doesn't exist in the global table, add it and read it in. */
7852
5989a64e
SM
7853 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7854 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7855
7856 find_entry.signature = dwo_unit->signature;
5989a64e 7857 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7858 &find_entry, INSERT);
6aa5f3a6
DE
7859 /* If we've already seen this type there's nothing to do. What's happening
7860 is we're doing our own version of comdat-folding here. */
7861 if (*slot != NULL)
7862 return 1;
7863
7864 /* This does the job that create_all_type_units would have done for
7865 this TU. */
ed2dc618
SM
7866 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7867 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7868 *slot = entry;
7869
7870 /* This does the job that build_type_psymtabs_1 would have done. */
ab432490 7871 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
7872 if (!reader.dummy_p)
7873 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7874 reader.comp_unit_die);
6aa5f3a6
DE
7875
7876 return 1;
7877}
7878
7879/* Traversal function for process_skeletonless_type_units. */
7880
7881static int
7882process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7883{
7884 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7885
7886 if (dwo_file->tus != NULL)
b0b6a987
TT
7887 htab_traverse_noresize (dwo_file->tus.get (),
7888 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7889
7890 return 1;
7891}
7892
7893/* Scan all TUs of DWO files, verifying we've processed them.
7894 This is needed in case a TU was emitted without its skeleton.
7895 Note: This can't be done until we know what all the DWO files are. */
7896
7897static void
ed2dc618 7898process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7899{
7900 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7901 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7902 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7903 {
5989a64e 7904 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7905 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7906 dwarf2_per_objfile);
6aa5f3a6 7907 }
348e048f
DE
7908}
7909
ed2dc618 7910/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7911
7912static void
ed2dc618 7913set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7914{
5989a64e 7915 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7916 {
891813be 7917 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7918
36586728
TT
7919 if (pst == NULL)
7920 continue;
7921
b76e467d 7922 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7923 {
7924 /* Set the 'user' field only if it is not already set. */
7925 if (pst->dependencies[j]->user == NULL)
7926 pst->dependencies[j]->user = pst;
7927 }
7928 }
7929}
7930
93311388
DE
7931/* Build the partial symbol table by doing a quick pass through the
7932 .debug_info and .debug_abbrev sections. */
72bf9492 7933
93311388 7934static void
ed2dc618 7935dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7936{
ed2dc618 7937 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7938
b4f54984 7939 if (dwarf_read_debug)
45cfd468
DE
7940 {
7941 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7942 objfile_name (objfile));
45cfd468
DE
7943 }
7944
76935768 7945 scoped_restore restore_reading_psyms
5989a64e 7946 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7947 true);
98bfdba5 7948
5989a64e 7949 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7950
93311388
DE
7951 /* Any cached compilation units will be linked by the per-objfile
7952 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7953 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7954
ed2dc618 7955 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7956
ed2dc618 7957 create_all_comp_units (dwarf2_per_objfile);
c906108c 7958
60606b2c
TT
7959 /* Create a temporary address map on a temporary obstack. We later
7960 copy this to the final obstack. */
8268c778 7961 auto_obstack temp_obstack;
791afaa2
TT
7962
7963 scoped_restore save_psymtabs_addrmap
d320c2b5 7964 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7965 addrmap_create_mutable (&temp_obstack));
72bf9492 7966
5989a64e 7967 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7968 {
7969 if (per_cu->v.psymtab != NULL)
7970 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7971 continue;
ab432490
SM
7972 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7973 language_minimal);
3d5afab3 7974 }
ff013f42 7975
6aa5f3a6 7976 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7977 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7978
7979 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7980 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7981 {
5989a64e 7982 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7983 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7984 }
7985
b4f54984 7986 if (dwarf_read_debug)
ed2dc618 7987 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7988
ed2dc618 7989 set_partial_user (dwarf2_per_objfile);
95554aad 7990
d320c2b5
TT
7991 objfile->partial_symtabs->psymtabs_addrmap
7992 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7993 objfile->partial_symtabs->obstack ());
791afaa2
TT
7994 /* At this point we want to keep the address map. */
7995 save_psymtabs_addrmap.release ();
ff013f42 7996
b4f54984 7997 if (dwarf_read_debug)
45cfd468 7998 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7999 objfile_name (objfile));
ae038cb0
DJ
8000}
8001
dee91e82
DE
8002/* Load the partial DIEs for a secondary CU into memory.
8003 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8004
dee91e82 8005static void
ab432490
SM
8006load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8007 dwarf2_per_objfile *per_objfile)
dee91e82 8008{
ab432490 8009 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
c0ab21c2
TT
8010
8011 if (!reader.dummy_p)
8012 {
8013 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8014 language_minimal);
8015
8016 /* Check if comp unit has_children.
8017 If so, read the rest of the partial symbols from this comp unit.
8018 If not, there's no more debug_info for this comp unit. */
3e225074 8019 if (reader.comp_unit_die->has_children)
c0ab21c2 8020 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8021
8022 reader.keep ();
c0ab21c2 8023 }
ae038cb0
DJ
8024}
8025
ae038cb0 8026static void
ed2dc618 8027read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8028 struct dwarf2_section_info *section,
f1902523 8029 struct dwarf2_section_info *abbrev_section,
b76e467d 8030 unsigned int is_dwz)
ae038cb0 8031{
d521ce57 8032 const gdb_byte *info_ptr;
ed2dc618 8033 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8034
b4f54984 8035 if (dwarf_read_debug)
bf6af496 8036 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8037 section->get_name (),
8038 section->get_file_name ());
bf6af496 8039
96b79293 8040 section->read (objfile);
ae038cb0 8041
36586728 8042 info_ptr = section->buffer;
6e70227d 8043
36586728 8044 while (info_ptr < section->buffer + section->size)
ae038cb0 8045 {
ae038cb0 8046 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8047
9c541725 8048 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8049
f1902523 8050 comp_unit_head cu_header;
ed2dc618
SM
8051 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8052 abbrev_section, info_ptr,
8053 rcuh_kind::COMPILE);
ae038cb0
DJ
8054
8055 /* Save the compilation unit for later lookup. */
f1902523 8056 if (cu_header.unit_type != DW_UT_type)
5989a64e 8057 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8058 else
8059 {
5989a64e 8060 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8061 sig_type->signature = cu_header.signature;
8062 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8063 this_cu = &sig_type->per_cu;
8064 }
8065 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8066 this_cu->sect_off = sect_off;
f1902523 8067 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8068 this_cu->is_dwz = is_dwz;
e3b94546 8069 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8070 this_cu->section = section;
ae038cb0 8071
5989a64e 8072 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8073
8074 info_ptr = info_ptr + this_cu->length;
8075 }
36586728
TT
8076}
8077
8078/* Create a list of all compilation units in OBJFILE.
8079 This is only done for -readnow and building partial symtabs. */
8080
8081static void
ed2dc618 8082create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8083{
5989a64e
SM
8084 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8085 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8086 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8087
c3699833 8088 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8089 if (dwz != NULL)
ed2dc618 8090 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8091 1);
c906108c
SS
8092}
8093
5734ee8b 8094/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8095 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8096 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8097 DW_AT_ranges). See the comments of add_partial_subprogram on how
8098 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8099
72bf9492
DJ
8100static void
8101scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8102 CORE_ADDR *highpc, int set_addrmap,
8103 struct dwarf2_cu *cu)
c906108c 8104{
72bf9492 8105 struct partial_die_info *pdi;
c906108c 8106
91c24f0a
DC
8107 /* Now, march along the PDI's, descending into ones which have
8108 interesting children but skipping the children of the other ones,
8109 until we reach the end of the compilation unit. */
c906108c 8110
72bf9492 8111 pdi = first_die;
91c24f0a 8112
72bf9492
DJ
8113 while (pdi != NULL)
8114 {
52356b79 8115 pdi->fixup (cu);
c906108c 8116
f55ee35c 8117 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8118 children, so we need to look at them. Ditto for anonymous
8119 enums. */
933c6fe4 8120
72bf9492 8121 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8122 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8123 || pdi->tag == DW_TAG_imported_unit
8124 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8125 {
72bf9492 8126 switch (pdi->tag)
c906108c
SS
8127 {
8128 case DW_TAG_subprogram:
b1dc1806 8129 case DW_TAG_inlined_subroutine:
cdc07690 8130 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8131 break;
72929c62 8132 case DW_TAG_constant:
c906108c
SS
8133 case DW_TAG_variable:
8134 case DW_TAG_typedef:
91c24f0a 8135 case DW_TAG_union_type:
317d2668
TV
8136 if (!pdi->is_declaration
8137 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8138 {
72bf9492 8139 add_partial_symbol (pdi, cu);
63d06c5c
DC
8140 }
8141 break;
c906108c 8142 case DW_TAG_class_type:
680b30c7 8143 case DW_TAG_interface_type:
c906108c 8144 case DW_TAG_structure_type:
72bf9492 8145 if (!pdi->is_declaration)
c906108c 8146 {
72bf9492 8147 add_partial_symbol (pdi, cu);
c906108c 8148 }
b7fee5a3
KS
8149 if ((cu->language == language_rust
8150 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8151 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8152 set_addrmap, cu);
c906108c 8153 break;
91c24f0a 8154 case DW_TAG_enumeration_type:
72bf9492
DJ
8155 if (!pdi->is_declaration)
8156 add_partial_enumeration (pdi, cu);
c906108c
SS
8157 break;
8158 case DW_TAG_base_type:
a02abb62 8159 case DW_TAG_subrange_type:
c906108c 8160 /* File scope base type definitions are added to the partial
c5aa993b 8161 symbol table. */
72bf9492 8162 add_partial_symbol (pdi, cu);
c906108c 8163 break;
d9fa45fe 8164 case DW_TAG_namespace:
cdc07690 8165 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8166 break;
5d7cb8df 8167 case DW_TAG_module:
59c35742
AB
8168 if (!pdi->is_declaration)
8169 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8170 break;
95554aad
TT
8171 case DW_TAG_imported_unit:
8172 {
8173 struct dwarf2_per_cu_data *per_cu;
8174
f4dc4d17
DE
8175 /* For now we don't handle imported units in type units. */
8176 if (cu->per_cu->is_debug_types)
8177 {
8178 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8179 " supported in type units [in module %s]"),
5e22e966 8180 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8181 }
8182
e3b94546 8183 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8184 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8185
8186 /* Go read the partial unit, if needed. */
8187 if (per_cu->v.psymtab == NULL)
ab432490
SM
8188 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8189 cu->language);
95554aad 8190
ae640021 8191 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8192 }
8193 break;
74921315
KS
8194 case DW_TAG_imported_declaration:
8195 add_partial_symbol (pdi, cu);
8196 break;
c906108c
SS
8197 default:
8198 break;
8199 }
8200 }
8201
72bf9492
DJ
8202 /* If the die has a sibling, skip to the sibling. */
8203
8204 pdi = pdi->die_sibling;
8205 }
8206}
8207
8208/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8209
72bf9492 8210 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8211 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8212 Enumerators are an exception; they use the scope of their parent
8213 enumeration type, i.e. the name of the enumeration type is not
8214 prepended to the enumerator.
91c24f0a 8215
72bf9492
DJ
8216 There are two complexities. One is DW_AT_specification; in this
8217 case "parent" means the parent of the target of the specification,
8218 instead of the direct parent of the DIE. The other is compilers
8219 which do not emit DW_TAG_namespace; in this case we try to guess
8220 the fully qualified name of structure types from their members'
8221 linkage names. This must be done using the DIE's children rather
8222 than the children of any DW_AT_specification target. We only need
8223 to do this for structures at the top level, i.e. if the target of
8224 any DW_AT_specification (if any; otherwise the DIE itself) does not
8225 have a parent. */
8226
8227/* Compute the scope prefix associated with PDI's parent, in
8228 compilation unit CU. The result will be allocated on CU's
8229 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8230 field. NULL is returned if no prefix is necessary. */
15d034d0 8231static const char *
72bf9492
DJ
8232partial_die_parent_scope (struct partial_die_info *pdi,
8233 struct dwarf2_cu *cu)
8234{
15d034d0 8235 const char *grandparent_scope;
72bf9492 8236 struct partial_die_info *parent, *real_pdi;
91c24f0a 8237
72bf9492
DJ
8238 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8239 then this means the parent of the specification DIE. */
8240
8241 real_pdi = pdi;
72bf9492 8242 while (real_pdi->has_specification)
fb816e8b 8243 {
122cf0f2
AB
8244 auto res = find_partial_die (real_pdi->spec_offset,
8245 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8246 real_pdi = res.pdi;
8247 cu = res.cu;
8248 }
72bf9492
DJ
8249
8250 parent = real_pdi->die_parent;
8251 if (parent == NULL)
8252 return NULL;
8253
8254 if (parent->scope_set)
8255 return parent->scope;
8256
52356b79 8257 parent->fixup (cu);
72bf9492 8258
10b3939b 8259 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8260
acebe513
UW
8261 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8262 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8263 Work around this problem here. */
8264 if (cu->language == language_cplus
6e70227d 8265 && parent->tag == DW_TAG_namespace
acebe513
UW
8266 && strcmp (parent->name, "::") == 0
8267 && grandparent_scope == NULL)
8268 {
8269 parent->scope = NULL;
8270 parent->scope_set = 1;
8271 return NULL;
8272 }
8273
0a4b0913 8274 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8275 if (pdi->tag == DW_TAG_enumerator)
8276 /* Enumerators should not get the name of the enumeration as a prefix. */
8277 parent->scope = grandparent_scope;
8278 else if (parent->tag == DW_TAG_namespace
f55ee35c 8279 || parent->tag == DW_TAG_module
72bf9492
DJ
8280 || parent->tag == DW_TAG_structure_type
8281 || parent->tag == DW_TAG_class_type
680b30c7 8282 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8283 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8284 || parent->tag == DW_TAG_enumeration_type
8285 || (cu->language == language_fortran
8286 && parent->tag == DW_TAG_subprogram
8287 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8288 {
8289 if (grandparent_scope == NULL)
8290 parent->scope = parent->name;
8291 else
3e43a32a
MS
8292 parent->scope = typename_concat (&cu->comp_unit_obstack,
8293 grandparent_scope,
f55ee35c 8294 parent->name, 0, cu);
72bf9492 8295 }
72bf9492
DJ
8296 else
8297 {
8298 /* FIXME drow/2004-04-01: What should we be doing with
8299 function-local names? For partial symbols, we should probably be
8300 ignoring them. */
fa9c3fa0
TT
8301 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8302 dwarf_tag_name (parent->tag),
8303 sect_offset_str (pdi->sect_off));
72bf9492 8304 parent->scope = grandparent_scope;
c906108c
SS
8305 }
8306
72bf9492
DJ
8307 parent->scope_set = 1;
8308 return parent->scope;
8309}
8310
8311/* Return the fully scoped name associated with PDI, from compilation unit
8312 CU. The result will be allocated with malloc. */
4568ecf9 8313
43816ebc 8314static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8315partial_die_full_name (struct partial_die_info *pdi,
8316 struct dwarf2_cu *cu)
8317{
15d034d0 8318 const char *parent_scope;
72bf9492 8319
98bfdba5
PA
8320 /* If this is a template instantiation, we can not work out the
8321 template arguments from partial DIEs. So, unfortunately, we have
8322 to go through the full DIEs. At least any work we do building
8323 types here will be reused if full symbols are loaded later. */
8324 if (pdi->has_template_arguments)
8325 {
52356b79 8326 pdi->fixup (cu);
98bfdba5
PA
8327
8328 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8329 {
8330 struct die_info *die;
8331 struct attribute attr;
8332 struct dwarf2_cu *ref_cu = cu;
8333
b64f50a1 8334 /* DW_FORM_ref_addr is using section offset. */
b4069958 8335 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8336 attr.form = DW_FORM_ref_addr;
9c541725 8337 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8338 die = follow_die_ref (NULL, &attr, &ref_cu);
8339
43816ebc 8340 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8341 }
8342 }
8343
72bf9492
DJ
8344 parent_scope = partial_die_parent_scope (pdi, cu);
8345 if (parent_scope == NULL)
8346 return NULL;
8347 else
43816ebc
TT
8348 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8349 pdi->name, 0, cu));
c906108c
SS
8350}
8351
8352static void
72bf9492 8353add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8354{
5e22e966 8355 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8356 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8357 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8358 CORE_ADDR addr = 0;
15d034d0 8359 const char *actual_name = NULL;
e142c38c
DJ
8360 CORE_ADDR baseaddr;
8361
b3b3bada 8362 baseaddr = objfile->text_section_offset ();
c906108c 8363
43816ebc
TT
8364 gdb::unique_xmalloc_ptr<char> built_actual_name
8365 = partial_die_full_name (pdi, cu);
15d034d0 8366 if (built_actual_name != NULL)
43816ebc 8367 actual_name = built_actual_name.get ();
63d06c5c 8368
72bf9492
DJ
8369 if (actual_name == NULL)
8370 actual_name = pdi->name;
8371
76e288d1
TT
8372 partial_symbol psymbol;
8373 memset (&psymbol, 0, sizeof (psymbol));
8374 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8375 psymbol.ginfo.section = -1;
8376
8377 /* The code below indicates that the psymbol should be installed by
8378 setting this. */
8379 gdb::optional<psymbol_placement> where;
8380
c906108c
SS
8381 switch (pdi->tag)
8382 {
b1dc1806 8383 case DW_TAG_inlined_subroutine:
c906108c 8384 case DW_TAG_subprogram:
79748972
TT
8385 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8386 - baseaddr);
0a4b0913
AB
8387 if (pdi->is_external
8388 || cu->language == language_ada
8389 || (cu->language == language_fortran
8390 && pdi->die_parent != NULL
8391 && pdi->die_parent->tag == DW_TAG_subprogram))
8392 {
8393 /* Normally, only "external" DIEs are part of the global scope.
8394 But in Ada and Fortran, we want to be able to access nested
8395 procedures globally. So all Ada and Fortran subprograms are
8396 stored in the global scope. */
76e288d1 8397 where = psymbol_placement::GLOBAL;
c906108c
SS
8398 }
8399 else
76e288d1
TT
8400 where = psymbol_placement::STATIC;
8401
8402 psymbol.domain = VAR_DOMAIN;
8403 psymbol.aclass = LOC_BLOCK;
8404 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8405 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8406
8407 if (pdi->main_subprogram && actual_name != NULL)
8408 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8409 break;
72929c62 8410 case DW_TAG_constant:
76e288d1
TT
8411 psymbol.domain = VAR_DOMAIN;
8412 psymbol.aclass = LOC_STATIC;
8413 where = (pdi->is_external
8414 ? psymbol_placement::GLOBAL
8415 : psymbol_placement::STATIC);
72929c62 8416 break;
c906108c 8417 case DW_TAG_variable:
95554aad
TT
8418 if (pdi->d.locdesc)
8419 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8420
95554aad 8421 if (pdi->d.locdesc
caac4577 8422 && addr == 0
5989a64e 8423 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8424 {
8425 /* A global or static variable may also have been stripped
8426 out by the linker if unused, in which case its address
8427 will be nullified; do not add such variables into partial
8428 symbol table then. */
8429 }
8430 else if (pdi->is_external)
c906108c
SS
8431 {
8432 /* Global Variable.
8433 Don't enter into the minimal symbol tables as there is
8434 a minimal symbol table entry from the ELF symbols already.
8435 Enter into partial symbol table if it has a location
8436 descriptor or a type.
8437 If the location descriptor is missing, new_symbol will create
8438 a LOC_UNRESOLVED symbol, the address of the variable will then
8439 be determined from the minimal symbol table whenever the variable
8440 is referenced.
8441 The address for the partial symbol table entry is not
8442 used by GDB, but it comes in handy for debugging partial symbol
8443 table building. */
8444
95554aad 8445 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8446 {
8447 psymbol.domain = VAR_DOMAIN;
8448 psymbol.aclass = LOC_STATIC;
8449 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8450 psymbol.ginfo.value.address = addr;
8451 where = psymbol_placement::GLOBAL;
8452 }
c906108c
SS
8453 }
8454 else
8455 {
ff908ebf
AW
8456 int has_loc = pdi->d.locdesc != NULL;
8457
8458 /* Static Variable. Skip symbols whose value we cannot know (those
8459 without location descriptors or constant values). */
8460 if (!has_loc && !pdi->has_const_value)
43816ebc 8461 return;
ff908ebf 8462
76e288d1
TT
8463 psymbol.domain = VAR_DOMAIN;
8464 psymbol.aclass = LOC_STATIC;
8465 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8466 if (has_loc)
8467 psymbol.ginfo.value.address = addr;
8468 where = psymbol_placement::STATIC;
c906108c
SS
8469 }
8470 break;
8471 case DW_TAG_typedef:
8472 case DW_TAG_base_type:
a02abb62 8473 case DW_TAG_subrange_type:
76e288d1
TT
8474 psymbol.domain = VAR_DOMAIN;
8475 psymbol.aclass = LOC_TYPEDEF;
8476 where = psymbol_placement::STATIC;
c906108c 8477 break;
74921315 8478 case DW_TAG_imported_declaration:
72bf9492 8479 case DW_TAG_namespace:
76e288d1
TT
8480 psymbol.domain = VAR_DOMAIN;
8481 psymbol.aclass = LOC_TYPEDEF;
8482 where = psymbol_placement::GLOBAL;
72bf9492 8483 break;
530e8392 8484 case DW_TAG_module:
a5fd13a9
BH
8485 /* With Fortran 77 there might be a "BLOCK DATA" module
8486 available without any name. If so, we skip the module as it
8487 doesn't bring any value. */
8488 if (actual_name != nullptr)
76e288d1
TT
8489 {
8490 psymbol.domain = MODULE_DOMAIN;
8491 psymbol.aclass = LOC_TYPEDEF;
8492 where = psymbol_placement::GLOBAL;
8493 }
530e8392 8494 break;
c906108c 8495 case DW_TAG_class_type:
680b30c7 8496 case DW_TAG_interface_type:
c906108c
SS
8497 case DW_TAG_structure_type:
8498 case DW_TAG_union_type:
8499 case DW_TAG_enumeration_type:
fa4028e9
JB
8500 /* Skip external references. The DWARF standard says in the section
8501 about "Structure, Union, and Class Type Entries": "An incomplete
8502 structure, union or class type is represented by a structure,
8503 union or class entry that does not have a byte size attribute
8504 and that has a DW_AT_declaration attribute." */
8505 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8506 return;
fa4028e9 8507
63d06c5c
DC
8508 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8509 static vs. global. */
76e288d1
TT
8510 psymbol.domain = STRUCT_DOMAIN;
8511 psymbol.aclass = LOC_TYPEDEF;
8512 where = (cu->language == language_cplus
8513 ? psymbol_placement::GLOBAL
8514 : psymbol_placement::STATIC);
c906108c
SS
8515 break;
8516 case DW_TAG_enumerator:
76e288d1
TT
8517 psymbol.domain = VAR_DOMAIN;
8518 psymbol.aclass = LOC_CONST;
8519 where = (cu->language == language_cplus
8520 ? psymbol_placement::GLOBAL
8521 : psymbol_placement::STATIC);
c906108c
SS
8522 break;
8523 default:
8524 break;
8525 }
76e288d1
TT
8526
8527 if (where.has_value ())
8528 {
f049a313
TT
8529 if (built_actual_name != nullptr)
8530 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8531 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8532 psymbol.ginfo.set_linkage_name (actual_name);
8533 else
8534 {
8535 psymbol.ginfo.set_demangled_name (actual_name,
8536 &objfile->objfile_obstack);
8537 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8538 }
76e288d1
TT
8539 add_psymbol_to_list (psymbol, *where, objfile);
8540 }
c906108c
SS
8541}
8542
5c4e30ca
DC
8543/* Read a partial die corresponding to a namespace; also, add a symbol
8544 corresponding to that namespace to the symbol table. NAMESPACE is
8545 the name of the enclosing namespace. */
91c24f0a 8546
72bf9492
DJ
8547static void
8548add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8549 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8550 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8551{
72bf9492 8552 /* Add a symbol for the namespace. */
e7c27a73 8553
72bf9492 8554 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8555
8556 /* Now scan partial symbols in that namespace. */
8557
91c24f0a 8558 if (pdi->has_children)
cdc07690 8559 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8560}
8561
5d7cb8df
JK
8562/* Read a partial die corresponding to a Fortran module. */
8563
8564static void
8565add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8566 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8567{
530e8392
KB
8568 /* Add a symbol for the namespace. */
8569
8570 add_partial_symbol (pdi, cu);
8571
f55ee35c 8572 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8573
8574 if (pdi->has_children)
cdc07690 8575 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8576}
8577
b1dc1806
XR
8578/* Read a partial die corresponding to a subprogram or an inlined
8579 subprogram and create a partial symbol for that subprogram.
8580 When the CU language allows it, this routine also defines a partial
8581 symbol for each nested subprogram that this subprogram contains.
8582 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8583 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8584
cdc07690
YQ
8585 PDI may also be a lexical block, in which case we simply search
8586 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8587 Again, this is only performed when the CU language allows this
8588 type of definitions. */
8589
8590static void
8591add_partial_subprogram (struct partial_die_info *pdi,
8592 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8593 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8594{
b1dc1806 8595 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8596 {
8597 if (pdi->has_pc_info)
8598 {
8599 if (pdi->lowpc < *lowpc)
8600 *lowpc = pdi->lowpc;
8601 if (pdi->highpc > *highpc)
8602 *highpc = pdi->highpc;
cdc07690 8603 if (set_addrmap)
5734ee8b 8604 {
5e22e966 8605 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8606 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8607 CORE_ADDR baseaddr;
b926417a
TT
8608 CORE_ADDR this_highpc;
8609 CORE_ADDR this_lowpc;
5734ee8b 8610
b3b3bada 8611 baseaddr = objfile->text_section_offset ();
b926417a
TT
8612 this_lowpc
8613 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8614 pdi->lowpc + baseaddr)
8615 - baseaddr);
8616 this_highpc
8617 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8618 pdi->highpc + baseaddr)
8619 - baseaddr);
d320c2b5 8620 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8621 this_lowpc, this_highpc - 1,
9291a0cd 8622 cu->per_cu->v.psymtab);
5734ee8b 8623 }
481860b3
GB
8624 }
8625
8626 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8627 {
bc30ff58 8628 if (!pdi->is_declaration)
e8d05480
JB
8629 /* Ignore subprogram DIEs that do not have a name, they are
8630 illegal. Do not emit a complaint at this point, we will
8631 do so when we convert this psymtab into a symtab. */
8632 if (pdi->name)
8633 add_partial_symbol (pdi, cu);
bc30ff58
JB
8634 }
8635 }
6e70227d 8636
bc30ff58
JB
8637 if (! pdi->has_children)
8638 return;
8639
0a4b0913 8640 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8641 {
8642 pdi = pdi->die_child;
8643 while (pdi != NULL)
8644 {
52356b79 8645 pdi->fixup (cu);
bc30ff58 8646 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8647 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8648 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8649 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8650 pdi = pdi->die_sibling;
8651 }
8652 }
8653}
8654
91c24f0a
DC
8655/* Read a partial die corresponding to an enumeration type. */
8656
72bf9492
DJ
8657static void
8658add_partial_enumeration (struct partial_die_info *enum_pdi,
8659 struct dwarf2_cu *cu)
91c24f0a 8660{
72bf9492 8661 struct partial_die_info *pdi;
91c24f0a
DC
8662
8663 if (enum_pdi->name != NULL)
72bf9492
DJ
8664 add_partial_symbol (enum_pdi, cu);
8665
8666 pdi = enum_pdi->die_child;
8667 while (pdi)
91c24f0a 8668 {
72bf9492 8669 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8670 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8671 else
72bf9492
DJ
8672 add_partial_symbol (pdi, cu);
8673 pdi = pdi->die_sibling;
91c24f0a 8674 }
91c24f0a
DC
8675}
8676
6caca83c
CC
8677/* Return the initial uleb128 in the die at INFO_PTR. */
8678
8679static unsigned int
d521ce57 8680peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8681{
8682 unsigned int bytes_read;
8683
8684 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8685}
8686
685af9cd
TT
8687/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8688 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8689
4bb7a0a7
DJ
8690 Return the corresponding abbrev, or NULL if the number is zero (indicating
8691 an empty DIE). In either case *BYTES_READ will be set to the length of
8692 the initial number. */
8693
8694static struct abbrev_info *
685af9cd
TT
8695peek_die_abbrev (const die_reader_specs &reader,
8696 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8697{
685af9cd 8698 dwarf2_cu *cu = reader.cu;
5e22e966 8699 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8700 unsigned int abbrev_number
8701 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8702
8703 if (abbrev_number == 0)
8704 return NULL;
8705
685af9cd 8706 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8707 if (!abbrev)
8708 {
422b9917 8709 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8710 " at offset %s [in module %s]"),
422b9917 8711 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8712 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8713 }
8714
8715 return abbrev;
8716}
8717
93311388
DE
8718/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8719 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8720 DIE. Any children of the skipped DIEs will also be skipped. */
8721
d521ce57
TT
8722static const gdb_byte *
8723skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8724{
4bb7a0a7
DJ
8725 while (1)
8726 {
685af9cd
TT
8727 unsigned int bytes_read;
8728 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8729
4bb7a0a7
DJ
8730 if (abbrev == NULL)
8731 return info_ptr + bytes_read;
8732 else
dee91e82 8733 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8734 }
8735}
8736
93311388
DE
8737/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8738 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8739 abbrev corresponding to that skipped uleb128 should be passed in
8740 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8741 children. */
8742
d521ce57
TT
8743static const gdb_byte *
8744skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8745 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8746{
8747 unsigned int bytes_read;
8748 struct attribute attr;
dee91e82
DE
8749 bfd *abfd = reader->abfd;
8750 struct dwarf2_cu *cu = reader->cu;
d521ce57 8751 const gdb_byte *buffer = reader->buffer;
f664829e 8752 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8753 unsigned int form, i;
8754
8755 for (i = 0; i < abbrev->num_attrs; i++)
8756 {
8757 /* The only abbrev we care about is DW_AT_sibling. */
8758 if (abbrev->attrs[i].name == DW_AT_sibling)
8759 {
18a8505e
AT
8760 bool ignored;
8761 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8762 &ignored);
4bb7a0a7 8763 if (attr.form == DW_FORM_ref_addr)
b98664d3 8764 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8765 else
b9502d3f 8766 {
0826b30a 8767 sect_offset off = attr.get_ref_die_offset ();
9c541725 8768 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8769
8770 if (sibling_ptr < info_ptr)
b98664d3 8771 complaint (_("DW_AT_sibling points backwards"));
22869d73 8772 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8773 reader->die_section->overflow_complaint ();
b9502d3f
WN
8774 else
8775 return sibling_ptr;
8776 }
4bb7a0a7
DJ
8777 }
8778
8779 /* If it isn't DW_AT_sibling, skip this attribute. */
8780 form = abbrev->attrs[i].form;
8781 skip_attribute:
8782 switch (form)
8783 {
4bb7a0a7 8784 case DW_FORM_ref_addr:
ae411497
TT
8785 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8786 and later it is offset sized. */
8787 if (cu->header.version == 2)
8788 info_ptr += cu->header.addr_size;
8789 else
8790 info_ptr += cu->header.offset_size;
8791 break;
36586728
TT
8792 case DW_FORM_GNU_ref_alt:
8793 info_ptr += cu->header.offset_size;
8794 break;
ae411497 8795 case DW_FORM_addr:
4bb7a0a7
DJ
8796 info_ptr += cu->header.addr_size;
8797 break;
8798 case DW_FORM_data1:
8799 case DW_FORM_ref1:
8800 case DW_FORM_flag:
8fe0f950 8801 case DW_FORM_strx1:
4bb7a0a7
DJ
8802 info_ptr += 1;
8803 break;
2dc7f7b3 8804 case DW_FORM_flag_present:
43988095 8805 case DW_FORM_implicit_const:
2dc7f7b3 8806 break;
4bb7a0a7
DJ
8807 case DW_FORM_data2:
8808 case DW_FORM_ref2:
8fe0f950 8809 case DW_FORM_strx2:
4bb7a0a7
DJ
8810 info_ptr += 2;
8811 break;
8fe0f950
AT
8812 case DW_FORM_strx3:
8813 info_ptr += 3;
8814 break;
4bb7a0a7
DJ
8815 case DW_FORM_data4:
8816 case DW_FORM_ref4:
8fe0f950 8817 case DW_FORM_strx4:
4bb7a0a7
DJ
8818 info_ptr += 4;
8819 break;
8820 case DW_FORM_data8:
8821 case DW_FORM_ref8:
55f1336d 8822 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8823 info_ptr += 8;
8824 break;
0224619f
JK
8825 case DW_FORM_data16:
8826 info_ptr += 16;
8827 break;
4bb7a0a7 8828 case DW_FORM_string:
9b1c24c8 8829 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8830 info_ptr += bytes_read;
8831 break;
2dc7f7b3 8832 case DW_FORM_sec_offset:
4bb7a0a7 8833 case DW_FORM_strp:
36586728 8834 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8835 info_ptr += cu->header.offset_size;
8836 break;
2dc7f7b3 8837 case DW_FORM_exprloc:
4bb7a0a7
DJ
8838 case DW_FORM_block:
8839 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8840 info_ptr += bytes_read;
8841 break;
8842 case DW_FORM_block1:
8843 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8844 break;
8845 case DW_FORM_block2:
8846 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8847 break;
8848 case DW_FORM_block4:
8849 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8850 break;
336d760d 8851 case DW_FORM_addrx:
cf532bd1 8852 case DW_FORM_strx:
4bb7a0a7
DJ
8853 case DW_FORM_sdata:
8854 case DW_FORM_udata:
8855 case DW_FORM_ref_udata:
3019eac3
DE
8856 case DW_FORM_GNU_addr_index:
8857 case DW_FORM_GNU_str_index:
18a8505e 8858 case DW_FORM_rnglistx:
41144253 8859 case DW_FORM_loclistx:
d521ce57 8860 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8861 break;
8862 case DW_FORM_indirect:
8863 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8864 info_ptr += bytes_read;
8865 /* We need to continue parsing from here, so just go back to
8866 the top. */
8867 goto skip_attribute;
8868
8869 default:
3e43a32a
MS
8870 error (_("Dwarf Error: Cannot handle %s "
8871 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8872 dwarf_form_name (form),
8873 bfd_get_filename (abfd));
8874 }
8875 }
8876
8877 if (abbrev->has_children)
dee91e82 8878 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8879 else
8880 return info_ptr;
8881}
8882
93311388 8883/* Locate ORIG_PDI's sibling.
dee91e82 8884 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8885
d521ce57 8886static const gdb_byte *
dee91e82
DE
8887locate_pdi_sibling (const struct die_reader_specs *reader,
8888 struct partial_die_info *orig_pdi,
d521ce57 8889 const gdb_byte *info_ptr)
91c24f0a
DC
8890{
8891 /* Do we know the sibling already? */
72bf9492 8892
91c24f0a
DC
8893 if (orig_pdi->sibling)
8894 return orig_pdi->sibling;
8895
8896 /* Are there any children to deal with? */
8897
8898 if (!orig_pdi->has_children)
8899 return info_ptr;
8900
4bb7a0a7 8901 /* Skip the children the long way. */
91c24f0a 8902
dee91e82 8903 return skip_children (reader, info_ptr);
91c24f0a
DC
8904}
8905
257e7a09 8906/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8907 not NULL. */
c906108c 8908
891813be
TT
8909void
8910dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8911{
ed2dc618
SM
8912 struct dwarf2_per_objfile *dwarf2_per_objfile
8913 = get_dwarf2_per_objfile (objfile);
8914
af758d11
SM
8915 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8916
077cbab2
TT
8917 /* If this psymtab is constructed from a debug-only objfile, the
8918 has_section_at_zero flag will not necessarily be correct. We
8919 can get the correct value for this flag by looking at the data
8920 associated with the (presumably stripped) associated objfile. */
8921 if (objfile->separate_debug_objfile_backlink)
c906108c 8922 {
077cbab2
TT
8923 struct dwarf2_per_objfile *dpo_backlink
8924 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8925
5989a64e
SM
8926 dwarf2_per_objfile->per_bfd->has_section_at_zero
8927 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8928 }
98bfdba5 8929
8566b89b 8930 expand_psymtab (objfile);
95554aad 8931
ed2dc618 8932 process_cu_includes (dwarf2_per_objfile);
c906108c 8933}
9cdd5dbd
DE
8934\f
8935/* Reading in full CUs. */
c906108c 8936
10b3939b
DJ
8937/* Add PER_CU to the queue. */
8938
8939static void
95554aad
TT
8940queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8941 enum language pretend_language)
10b3939b 8942{
10b3939b 8943 per_cu->queued = 1;
1859c670 8944 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8945}
8946
89e63ee4
DE
8947/* If PER_CU is not yet queued, add it to the queue.
8948 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8949 dependency.
0907af0c 8950 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8951 meaning either PER_CU is already queued or it is already loaded.
8952
8953 N.B. There is an invariant here that if a CU is queued then it is loaded.
8954 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8955
8956static int
89e63ee4 8957maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8958 struct dwarf2_per_cu_data *per_cu,
8959 enum language pretend_language)
8960{
8961 /* We may arrive here during partial symbol reading, if we need full
8962 DIEs to process an unusual case (e.g. template arguments). Do
8963 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8964 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c
DE
8965 {
8966 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8967 return 1;
8968 return 0;
8969 }
8970
8971 /* Mark the dependence relation so that we don't flush PER_CU
8972 too early. */
89e63ee4
DE
8973 if (dependent_cu != NULL)
8974 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8975
8976 /* If it's already on the queue, we have nothing to do. */
8977 if (per_cu->queued)
8978 return 0;
8979
8980 /* If the compilation unit is already loaded, just mark it as
8981 used. */
8982 if (per_cu->cu != NULL)
8983 {
8984 per_cu->cu->last_used = 0;
8985 return 0;
8986 }
8987
8988 /* Add it to the queue. */
8989 queue_comp_unit (per_cu, pretend_language);
8990
8991 return 1;
8992}
8993
10b3939b
DJ
8994/* Process the queue. */
8995
8996static void
ed2dc618 8997process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8998{
b4f54984 8999 if (dwarf_read_debug)
45cfd468
DE
9000 {
9001 fprintf_unfiltered (gdb_stdlog,
9002 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9003 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9004 }
9005
03dd20cc
DJ
9006 /* The queue starts out with one item, but following a DIE reference
9007 may load a new CU, adding it to the end of the queue. */
5989a64e 9008 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 9009 {
5989a64e 9010 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
39856def 9011
af758d11 9012 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
cc12ce38 9013 /* Skip dummy CUs. */
39856def 9014 && item.per_cu->cu != NULL)
f4dc4d17 9015 {
39856def 9016 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 9017 unsigned int debug_print_threshold;
247f5c4f 9018 char buf[100];
f4dc4d17 9019
247f5c4f 9020 if (per_cu->is_debug_types)
f4dc4d17 9021 {
247f5c4f
DE
9022 struct signatured_type *sig_type =
9023 (struct signatured_type *) per_cu;
9024
9d8780f0 9025 sprintf (buf, "TU %s at offset %s",
73be47f5 9026 hex_string (sig_type->signature),
9d8780f0 9027 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9028 /* There can be 100s of TUs.
9029 Only print them in verbose mode. */
9030 debug_print_threshold = 2;
f4dc4d17 9031 }
247f5c4f 9032 else
73be47f5 9033 {
9d8780f0
SM
9034 sprintf (buf, "CU at offset %s",
9035 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9036 debug_print_threshold = 1;
9037 }
247f5c4f 9038
b4f54984 9039 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9040 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9041
9042 if (per_cu->is_debug_types)
47b14e86
SM
9043 process_full_type_unit (per_cu, dwarf2_per_objfile,
9044 item.pretend_language);
f4dc4d17 9045 else
47b14e86
SM
9046 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9047 item.pretend_language);
f4dc4d17 9048
b4f54984 9049 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9050 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9051 }
10b3939b 9052
39856def 9053 item.per_cu->queued = 0;
5989a64e 9054 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9055 }
9056
b4f54984 9057 if (dwarf_read_debug)
45cfd468
DE
9058 {
9059 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9060 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9061 }
10b3939b
DJ
9062}
9063
10b3939b
DJ
9064/* Read in full symbols for PST, and anything it depends on. */
9065
8566b89b
TT
9066void
9067dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9068{
af758d11 9069 gdb_assert (!readin_p (objfile));
95554aad 9070
48993951 9071 expand_dependencies (objfile);
aaa75496 9072
97a1449a
SM
9073 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9074 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9075 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9076}
9077
af758d11
SM
9078/* See psympriv.h. */
9079
9080bool
9081dwarf2_psymtab::readin_p (struct objfile *objfile) const
9082{
9083 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9084 return per_objfile->symtab_set_p (per_cu_data);
9085}
9086
9087/* See psympriv.h. */
9088
9089compunit_symtab *
9090dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9091{
9092 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9093 return per_objfile->get_symtab (per_cu_data);
9094}
9095
dee91e82
DE
9096/* Trivial hash function for die_info: the hash value of a DIE
9097 is its offset in .debug_info for this objfile. */
10b3939b 9098
dee91e82
DE
9099static hashval_t
9100die_hash (const void *item)
10b3939b 9101{
9a3c8263 9102 const struct die_info *die = (const struct die_info *) item;
6502dd73 9103
9c541725 9104 return to_underlying (die->sect_off);
dee91e82 9105}
63d06c5c 9106
dee91e82
DE
9107/* Trivial comparison function for die_info structures: two DIEs
9108 are equal if they have the same offset. */
98bfdba5 9109
dee91e82
DE
9110static int
9111die_eq (const void *item_lhs, const void *item_rhs)
9112{
9a3c8263
SM
9113 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9114 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9115
9c541725 9116 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9117}
c906108c 9118
c0ab21c2 9119/* Load the DIEs associated with PER_CU into memory. */
c906108c 9120
dee91e82 9121static void
ab432490
SM
9122load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9123 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9124 bool skip_partial,
9125 enum language pretend_language)
dee91e82 9126{
c0ab21c2
TT
9127 gdb_assert (! this_cu->is_debug_types);
9128
ab432490 9129 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
c0ab21c2
TT
9130 if (reader.dummy_p)
9131 return;
9132
9133 struct dwarf2_cu *cu = reader.cu;
9134 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9135
dee91e82
DE
9136 gdb_assert (cu->die_hash == NULL);
9137 cu->die_hash =
9138 htab_create_alloc_ex (cu->header.length / 12,
9139 die_hash,
9140 die_eq,
9141 NULL,
9142 &cu->comp_unit_obstack,
9143 hashtab_obstack_allocate,
9144 dummy_obstack_deallocate);
e142c38c 9145
3e225074 9146 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9147 reader.comp_unit_die->child
9148 = read_die_and_siblings (&reader, reader.info_ptr,
9149 &info_ptr, reader.comp_unit_die);
9150 cu->dies = reader.comp_unit_die;
dee91e82 9151 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9152
9153 /* We try not to read any attributes in this function, because not
9cdd5dbd 9154 all CUs needed for references have been loaded yet, and symbol
10b3939b 9155 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9156 or we won't be able to build types correctly.
9157 Similarly, if we do not read the producer, we can not apply
9158 producer-specific interpretation. */
c0ab21c2 9159 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9160
9161 reader.keep ();
10b3939b
DJ
9162}
9163
3da10d80
KS
9164/* Add a DIE to the delayed physname list. */
9165
9166static void
9167add_to_method_list (struct type *type, int fnfield_index, int index,
9168 const char *name, struct die_info *die,
9169 struct dwarf2_cu *cu)
9170{
9171 struct delayed_method_info mi;
9172 mi.type = type;
9173 mi.fnfield_index = fnfield_index;
9174 mi.index = index;
9175 mi.name = name;
9176 mi.die = die;
c89b44cd 9177 cu->method_list.push_back (mi);
3da10d80
KS
9178}
9179
3693fdb3
PA
9180/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9181 "const" / "volatile". If so, decrements LEN by the length of the
9182 modifier and return true. Otherwise return false. */
9183
9184template<size_t N>
9185static bool
9186check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9187{
9188 size_t mod_len = sizeof (mod) - 1;
9189 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9190 {
9191 len -= mod_len;
9192 return true;
9193 }
9194 return false;
9195}
9196
3da10d80
KS
9197/* Compute the physnames of any methods on the CU's method list.
9198
9199 The computation of method physnames is delayed in order to avoid the
9200 (bad) condition that one of the method's formal parameters is of an as yet
9201 incomplete type. */
9202
9203static void
9204compute_delayed_physnames (struct dwarf2_cu *cu)
9205{
3693fdb3 9206 /* Only C++ delays computing physnames. */
c89b44cd 9207 if (cu->method_list.empty ())
3693fdb3
PA
9208 return;
9209 gdb_assert (cu->language == language_cplus);
9210
52941706 9211 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9212 {
1d06ead6 9213 const char *physname;
3da10d80 9214 struct fn_fieldlist *fn_flp
c89b44cd
TT
9215 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9216 physname = dwarf2_physname (mi.name, mi.die, cu);
9217 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9218 = physname ? physname : "";
3693fdb3
PA
9219
9220 /* Since there's no tag to indicate whether a method is a
9221 const/volatile overload, extract that information out of the
9222 demangled name. */
9223 if (physname != NULL)
9224 {
9225 size_t len = strlen (physname);
9226
9227 while (1)
9228 {
9229 if (physname[len] == ')') /* shortcut */
9230 break;
9231 else if (check_modifier (physname, len, " const"))
c89b44cd 9232 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9233 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9234 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9235 else
9236 break;
9237 }
9238 }
3da10d80 9239 }
c89b44cd
TT
9240
9241 /* The list is no longer needed. */
9242 cu->method_list.clear ();
3da10d80
KS
9243}
9244
a766d390
DE
9245/* Go objects should be embedded in a DW_TAG_module DIE,
9246 and it's not clear if/how imported objects will appear.
9247 To keep Go support simple until that's worked out,
9248 go back through what we've read and create something usable.
9249 We could do this while processing each DIE, and feels kinda cleaner,
9250 but that way is more invasive.
9251 This is to, for example, allow the user to type "p var" or "b main"
9252 without having to specify the package name, and allow lookups
9253 of module.object to work in contexts that use the expression
9254 parser. */
9255
9256static void
9257fixup_go_packaging (struct dwarf2_cu *cu)
9258{
421d1616 9259 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9260 struct pending *list;
9261 int i;
9262
c24bdb02 9263 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9264 list != NULL;
9265 list = list->next)
a766d390
DE
9266 {
9267 for (i = 0; i < list->nsyms; ++i)
9268 {
9269 struct symbol *sym = list->symbol[i];
9270
c1b5c1eb 9271 if (sym->language () == language_go
a766d390
DE
9272 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9273 {
421d1616
TT
9274 gdb::unique_xmalloc_ptr<char> this_package_name
9275 (go_symbol_package_name (sym));
a766d390
DE
9276
9277 if (this_package_name == NULL)
9278 continue;
9279 if (package_name == NULL)
421d1616 9280 package_name = std::move (this_package_name);
a766d390
DE
9281 else
9282 {
5e22e966 9283 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9284 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9285 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9286 (symbol_symtab (sym) != NULL
9287 ? symtab_to_filename_for_display
9288 (symbol_symtab (sym))
e3b94546 9289 : objfile_name (objfile)),
421d1616 9290 this_package_name.get (), package_name.get ());
a766d390
DE
9291 }
9292 }
9293 }
9294 }
9295
9296 if (package_name != NULL)
9297 {
5e22e966 9298 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9299 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9300 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9301 saved_package_name);
a766d390
DE
9302 struct symbol *sym;
9303
8c14c3a3 9304 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9305 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9306 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9307 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9308 e.g., "main" finds the "main" module and not C's main(). */
9309 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9310 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9311 SYMBOL_TYPE (sym) = type;
9312
c24bdb02 9313 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9314 }
9315}
9316
c9317f21
TT
9317/* Allocate a fully-qualified name consisting of the two parts on the
9318 obstack. */
9319
9320static const char *
9321rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9322{
9323 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9324}
9325
9c6a1327
TT
9326/* A helper that allocates a variant part to attach to a Rust enum
9327 type. OBSTACK is where the results should be allocated. TYPE is
9328 the type we're processing. DISCRIMINANT_INDEX is the index of the
9329 discriminant. It must be the index of one of the fields of TYPE.
9330 DEFAULT_INDEX is the index of the default field; or -1 if there is
9331 no default. RANGES is indexed by "effective" field number (the
9332 field index, but omitting the discriminant and default fields) and
9333 must hold the discriminant values used by the variants. Note that
9334 RANGES must have a lifetime at least as long as OBSTACK -- either
9335 already allocated on it, or static. */
c9317f21 9336
9c6a1327
TT
9337static void
9338alloc_rust_variant (struct obstack *obstack, struct type *type,
9339 int discriminant_index, int default_index,
9340 gdb::array_view<discriminant_range> ranges)
9341{
9342 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9343 must be handled by the caller. */
9344 gdb_assert (discriminant_index >= 0
1f704f76 9345 && discriminant_index < type->num_fields ());
c9317f21 9346 gdb_assert (default_index == -1
1f704f76 9347 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9348
9c6a1327 9349 /* We have one variant for each non-discriminant field. */
1f704f76 9350 int n_variants = type->num_fields () - 1;
c9317f21 9351
9c6a1327
TT
9352 variant *variants = new (obstack) variant[n_variants];
9353 int var_idx = 0;
9354 int range_idx = 0;
1f704f76 9355 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9356 {
9357 if (i == discriminant_index)
9358 continue;
c9317f21 9359
9c6a1327
TT
9360 variants[var_idx].first_field = i;
9361 variants[var_idx].last_field = i + 1;
9362
9363 /* The default field does not need a range, but other fields do.
9364 We skipped the discriminant above. */
9365 if (i != default_index)
9366 {
9367 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9368 ++range_idx;
9369 }
c9317f21 9370
9c6a1327
TT
9371 ++var_idx;
9372 }
9373
9374 gdb_assert (range_idx == ranges.size ());
9375 gdb_assert (var_idx == n_variants);
9376
9377 variant_part *part = new (obstack) variant_part;
9378 part->discriminant_index = discriminant_index;
9379 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9380 discriminant_index));
9381 part->variants = gdb::array_view<variant> (variants, n_variants);
9382
9383 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9384 gdb::array_view<variant_part> *prop_value
9385 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9386
9c6a1327
TT
9387 struct dynamic_prop prop;
9388 prop.kind = PROP_VARIANT_PARTS;
9389 prop.data.variant_parts = prop_value;
9390
5c54719c 9391 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9392}
9393
9394/* Some versions of rustc emitted enums in an unusual way.
9395
9396 Ordinary enums were emitted as unions. The first element of each
9397 structure in the union was named "RUST$ENUM$DISR". This element
9398 held the discriminant.
9399
9400 These versions of Rust also implemented the "non-zero"
9401 optimization. When the enum had two values, and one is empty and
9402 the other holds a pointer that cannot be zero, the pointer is used
9403 as the discriminant, with a zero value meaning the empty variant.
9404 Here, the union's first member is of the form
9405 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9406 where the fieldnos are the indices of the fields that should be
9407 traversed in order to find the field (which may be several fields deep)
9408 and the variantname is the name of the variant of the case when the
9409 field is zero.
9410
9411 This function recognizes whether TYPE is of one of these forms,
9412 and, if so, smashes it to be a variant type. */
9413
9414static void
9415quirk_rust_enum (struct type *type, struct objfile *objfile)
9416{
78134374 9417 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9418
9419 /* We don't need to deal with empty enums. */
1f704f76 9420 if (type->num_fields () == 0)
c9317f21
TT
9421 return;
9422
9423#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9424 if (type->num_fields () == 1
c9317f21
TT
9425 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9426 {
9427 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9428
9429 /* Decode the field name to find the offset of the
9430 discriminant. */
9431 ULONGEST bit_offset = 0;
9432 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9433 while (name[0] >= '0' && name[0] <= '9')
9434 {
9435 char *tail;
9436 unsigned long index = strtoul (name, &tail, 10);
9437 name = tail;
9438 if (*name != '$'
1f704f76 9439 || index >= field_type->num_fields ()
c9317f21
TT
9440 || (TYPE_FIELD_LOC_KIND (field_type, index)
9441 != FIELD_LOC_KIND_BITPOS))
9442 {
b98664d3 9443 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9444 "[in module %s]"),
9445 TYPE_FIELD_NAME (type, 0),
9446 objfile_name (objfile));
9447 return;
9448 }
9449 ++name;
9450
9451 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9452 field_type = TYPE_FIELD_TYPE (field_type, index);
9453 }
9454
9c6a1327
TT
9455 /* Smash this type to be a structure type. We have to do this
9456 because the type has already been recorded. */
67607e24 9457 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9458 type->set_num_fields (3);
9c6a1327 9459 /* Save the field we care about. */
ceacbf6e 9460 struct field saved_field = type->field (0);
3cabb6b0
SM
9461 type->set_fields
9462 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9463
9c6a1327
TT
9464 /* Put the discriminant at index 0. */
9465 TYPE_FIELD_TYPE (type, 0) = field_type;
9466 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9467 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9468 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9469
9470 /* The order of fields doesn't really matter, so put the real
9471 field at index 1 and the data-less field at index 2. */
ceacbf6e 9472 type->field (1) = saved_field;
9c6a1327 9473 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9474 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9475 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9476 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9477 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9478
9479 const char *dataless_name
7d93a1e0 9480 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9481 name);
9482 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9483 dataless_name);
9c6a1327 9484 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9485 /* NAME points into the original discriminant name, which
9486 already has the correct lifetime. */
9c6a1327 9487 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9488 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9489
9c6a1327
TT
9490 /* Indicate that this is a variant type. */
9491 static discriminant_range ranges[1] = { { 0, 0 } };
9492 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9493 }
77c2dba3
TT
9494 /* A union with a single anonymous field is probably an old-style
9495 univariant enum. */
1f704f76 9496 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9497 {
c9317f21
TT
9498 /* Smash this type to be a structure type. We have to do this
9499 because the type has already been recorded. */
67607e24 9500 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9501
9c6a1327 9502 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9503 const char *variant_name
7d93a1e0 9504 = rust_last_path_segment (field_type->name ());
9c6a1327 9505 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9506 field_type->set_name
9507 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9508 type->name (), variant_name));
c9317f21
TT
9509 }
9510 else
9511 {
9512 struct type *disr_type = nullptr;
1f704f76 9513 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9514 {
9515 disr_type = TYPE_FIELD_TYPE (type, i);
9516
78134374 9517 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9518 {
9519 /* All fields of a true enum will be structs. */
9520 return;
9521 }
1f704f76 9522 else if (disr_type->num_fields () == 0)
c9317f21
TT
9523 {
9524 /* Could be data-less variant, so keep going. */
a037790e 9525 disr_type = nullptr;
c9317f21
TT
9526 }
9527 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9528 "RUST$ENUM$DISR") != 0)
9529 {
9530 /* Not a Rust enum. */
9531 return;
9532 }
9533 else
9534 {
9535 /* Found one. */
9536 break;
9537 }
9538 }
9539
9540 /* If we got here without a discriminant, then it's probably
9541 just a union. */
9542 if (disr_type == nullptr)
9543 return;
9544
9545 /* Smash this type to be a structure type. We have to do this
9546 because the type has already been recorded. */
67607e24 9547 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9548
9c6a1327 9549 /* Make space for the discriminant field. */
ceacbf6e 9550 struct field *disr_field = &disr_type->field (0);
9c6a1327 9551 field *new_fields
1f704f76 9552 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9553 * sizeof (struct field)));
80fc5e77 9554 memcpy (new_fields + 1, type->fields (),
1f704f76 9555 type->num_fields () * sizeof (struct field));
3cabb6b0 9556 type->set_fields (new_fields);
1f704f76 9557 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9558
9559 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9560 type->field (0) = *disr_field;
9c6a1327
TT
9561 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9562 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9563
9564 /* We need a way to find the correct discriminant given a
9565 variant name. For convenience we build a map here. */
9566 struct type *enum_type = FIELD_TYPE (*disr_field);
9567 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9568 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9569 {
9570 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9571 {
9572 const char *name
9573 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9574 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9575 }
9576 }
9577
1f704f76 9578 int n_fields = type->num_fields ();
9c6a1327
TT
9579 /* We don't need a range entry for the discriminant, but we do
9580 need one for every other field, as there is no default
9581 variant. */
9582 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9583 discriminant_range,
9584 n_fields - 1);
c9317f21
TT
9585 /* Skip the discriminant here. */
9586 for (int i = 1; i < n_fields; ++i)
9587 {
9588 /* Find the final word in the name of this variant's type.
9589 That name can be used to look up the correct
9590 discriminant. */
9591 const char *variant_name
7d93a1e0 9592 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9593
9594 auto iter = discriminant_map.find (variant_name);
9595 if (iter != discriminant_map.end ())
9c6a1327
TT
9596 {
9597 ranges[i].low = iter->second;
9598 ranges[i].high = iter->second;
9599 }
c9317f21 9600
bedda9ac 9601 /* Remove the discriminant field, if it exists. */
9c6a1327 9602 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9603 if (sub_type->num_fields () > 0)
bedda9ac 9604 {
5e33d5f4 9605 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9606 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9607 }
9c6a1327 9608 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9609 sub_type->set_name
9610 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9611 type->name (), variant_name));
c9317f21 9612 }
9c6a1327
TT
9613
9614 /* Indicate that this is a variant type. */
9615 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9616 gdb::array_view<discriminant_range> (ranges,
9617 n_fields - 1));
c9317f21
TT
9618 }
9619}
9620
9621/* Rewrite some Rust unions to be structures with variants parts. */
9622
9623static void
9624rust_union_quirks (struct dwarf2_cu *cu)
9625{
9626 gdb_assert (cu->language == language_rust);
52941706 9627 for (type *type_ : cu->rust_unions)
5e22e966 9628 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9629 /* We don't need this any more. */
9630 cu->rust_unions.clear ();
c9317f21
TT
9631}
9632
95554aad
TT
9633/* A helper function for computing the list of all symbol tables
9634 included by PER_CU. */
9635
9636static void
4c39bc03 9637recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9638 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9639 dwarf2_per_cu_data *per_cu,
9640 dwarf2_per_objfile *per_objfile,
43f3e411 9641 struct compunit_symtab *immediate_parent)
95554aad 9642{
af758d11 9643 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9644 if (*slot != NULL)
9645 {
9646 /* This inclusion and its children have been processed. */
9647 return;
9648 }
9649
9650 *slot = per_cu;
af758d11 9651
95554aad 9652 /* Only add a CU if it has a symbol table. */
43182c09 9653 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9654 if (cust != NULL)
ec94af83
DE
9655 {
9656 /* If this is a type unit only add its symbol table if we haven't
9657 seen it yet (type unit per_cu's can share symtabs). */
9658 if (per_cu->is_debug_types)
9659 {
43f3e411 9660 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9661 if (*slot == NULL)
9662 {
43f3e411 9663 *slot = cust;
4c39bc03 9664 result->push_back (cust);
43f3e411
DE
9665 if (cust->user == NULL)
9666 cust->user = immediate_parent;
ec94af83
DE
9667 }
9668 }
9669 else
f9125b6c 9670 {
4c39bc03 9671 result->push_back (cust);
43f3e411
DE
9672 if (cust->user == NULL)
9673 cust->user = immediate_parent;
f9125b6c 9674 }
ec94af83 9675 }
95554aad 9676
ae640021
AB
9677 if (!per_cu->imported_symtabs_empty ())
9678 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9679 {
9680 recursively_compute_inclusions (result, all_children,
43182c09
SM
9681 all_type_symtabs, ptr, per_objfile,
9682 cust);
ae640021 9683 }
95554aad
TT
9684}
9685
43f3e411 9686/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9687 PER_CU. */
9688
9689static void
43182c09
SM
9690compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9691 dwarf2_per_objfile *per_objfile)
95554aad 9692{
f4dc4d17
DE
9693 gdb_assert (! per_cu->is_debug_types);
9694
ae640021 9695 if (!per_cu->imported_symtabs_empty ())
95554aad 9696 {
ae640021 9697 int len;
4c39bc03 9698 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9699 htab_t all_children, all_type_symtabs;
43182c09 9700 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9701
9702 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9703 if (cust == NULL)
95554aad
TT
9704 return;
9705
9706 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9707 NULL, xcalloc, xfree);
ec94af83
DE
9708 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9709 NULL, xcalloc, xfree);
95554aad 9710
ae640021 9711 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9712 {
9713 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9714 all_type_symtabs, ptr, per_objfile,
9715 cust);
ec94af83 9716 }
95554aad 9717
ec94af83 9718 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9719 len = result_symtabs.size ();
43f3e411 9720 cust->includes
ed2dc618 9721 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9722 struct compunit_symtab *, len + 1);
4c39bc03
TT
9723 memcpy (cust->includes, result_symtabs.data (),
9724 len * sizeof (compunit_symtab *));
43f3e411 9725 cust->includes[len] = NULL;
95554aad 9726
95554aad 9727 htab_delete (all_children);
ec94af83 9728 htab_delete (all_type_symtabs);
95554aad
TT
9729 }
9730}
9731
9732/* Compute the 'includes' field for the symtabs of all the CUs we just
9733 read. */
9734
9735static void
ed2dc618 9736process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9737{
5989a64e 9738 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9739 {
9740 if (! iter->is_debug_types)
43182c09 9741 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
f4dc4d17 9742 }
95554aad 9743
5989a64e 9744 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9745}
9746
9cdd5dbd 9747/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9748 already been loaded into memory. */
9749
9750static void
47b14e86
SM
9751process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9752 dwarf2_per_objfile *dwarf2_per_objfile,
95554aad 9753 enum language pretend_language)
10b3939b 9754{
10b3939b 9755 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618 9756 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9757 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9758 CORE_ADDR lowpc, highpc;
43f3e411 9759 struct compunit_symtab *cust;
10b3939b 9760 CORE_ADDR baseaddr;
4359dff1 9761 struct block *static_block;
3e29f34a 9762 CORE_ADDR addr;
10b3939b 9763
b3b3bada 9764 baseaddr = objfile->text_section_offset ();
10b3939b 9765
c89b44cd
TT
9766 /* Clear the list here in case something was left over. */
9767 cu->method_list.clear ();
10b3939b 9768
95554aad
TT
9769 cu->language = pretend_language;
9770 cu->language_defn = language_def (cu->language);
9771
c906108c 9772 /* Do line number decoding in read_file_scope () */
10b3939b 9773 process_die (cu->dies, cu);
c906108c 9774
a766d390
DE
9775 /* For now fudge the Go package. */
9776 if (cu->language == language_go)
9777 fixup_go_packaging (cu);
9778
5f48f8f3 9779 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9780 should be complete, and it should now be safe to compute all of the
9781 physnames. */
9782 compute_delayed_physnames (cu);
3da10d80 9783
c9317f21
TT
9784 if (cu->language == language_rust)
9785 rust_union_quirks (cu);
9786
fae299cd
DC
9787 /* Some compilers don't define a DW_AT_high_pc attribute for the
9788 compilation unit. If the DW_AT_high_pc is missing, synthesize
9789 it, by scanning the DIE's below the compilation unit. */
10b3939b 9790 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9791
3e29f34a 9792 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9793 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9794
9795 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9796 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9797 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9798 addrmap to help ensure it has an accurate map of pc values belonging to
9799 this comp unit. */
9800 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9801
c24bdb02 9802 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9803 SECT_OFF_TEXT (objfile),
9804 0);
c906108c 9805
43f3e411 9806 if (cust != NULL)
c906108c 9807 {
df15bd07 9808 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9809
8be455d7
JK
9810 /* Set symtab language to language from DW_AT_language. If the
9811 compilation is from a C file generated by language preprocessors, do
9812 not set the language if it was already deduced by start_subfile. */
43f3e411 9813 if (!(cu->language == language_c
40e3ad0e 9814 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9815 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9816
9817 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9818 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9819 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9820 there were bugs in prologue debug info, fixed later in GCC-4.5
9821 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9822
9823 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9824 needed, it would be wrong due to missing DW_AT_producer there.
9825
9826 Still one can confuse GDB by using non-standard GCC compilation
9827 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9828 */
ab260dad 9829 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9830 cust->locations_valid = 1;
e0d00bc7
JK
9831
9832 if (gcc_4_minor >= 5)
43f3e411 9833 cust->epilogue_unwind_valid = 1;
96408a79 9834
43f3e411 9835 cust->call_site_htab = cu->call_site_htab;
c906108c 9836 }
9291a0cd 9837
af758d11 9838 dwarf2_per_objfile->set_symtab (per_cu, cust);
c906108c 9839
95554aad 9840 /* Push it for inclusion processing later. */
5989a64e 9841 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
804d2729
TT
9842
9843 /* Not needed any more. */
c24bdb02 9844 cu->reset_builder ();
f4dc4d17 9845}
45cfd468 9846
f4dc4d17
DE
9847/* Generate full symbol information for type unit PER_CU, whose DIEs have
9848 already been loaded into memory. */
9849
9850static void
47b14e86
SM
9851process_full_type_unit (dwarf2_per_cu_data *per_cu,
9852 dwarf2_per_objfile *dwarf2_per_objfile,
f4dc4d17
DE
9853 enum language pretend_language)
9854{
9855 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618 9856 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9857 struct compunit_symtab *cust;
0186c6a7
DE
9858 struct signatured_type *sig_type;
9859
9860 gdb_assert (per_cu->is_debug_types);
9861 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9862
c89b44cd
TT
9863 /* Clear the list here in case something was left over. */
9864 cu->method_list.clear ();
f4dc4d17 9865
f4dc4d17
DE
9866 cu->language = pretend_language;
9867 cu->language_defn = language_def (cu->language);
9868
9869 /* The symbol tables are set up in read_type_unit_scope. */
9870 process_die (cu->dies, cu);
9871
9872 /* For now fudge the Go package. */
9873 if (cu->language == language_go)
9874 fixup_go_packaging (cu);
9875
5f48f8f3 9876 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9877 should be complete, and it should now be safe to compute all of the
9878 physnames. */
9879 compute_delayed_physnames (cu);
f4dc4d17 9880
c9317f21
TT
9881 if (cu->language == language_rust)
9882 rust_union_quirks (cu);
9883
f4dc4d17
DE
9884 /* TUs share symbol tables.
9885 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9886 of it with end_expandable_symtab. Otherwise, complete the addition of
9887 this TU's symbols to the existing symtab. */
43f3e411 9888 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9889 {
c24bdb02
KS
9890 buildsym_compunit *builder = cu->get_builder ();
9891 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9892 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9893
43f3e411 9894 if (cust != NULL)
f4dc4d17
DE
9895 {
9896 /* Set symtab language to language from DW_AT_language. If the
9897 compilation is from a C file generated by language preprocessors,
9898 do not set the language if it was already deduced by
9899 start_subfile. */
43f3e411
DE
9900 if (!(cu->language == language_c
9901 && COMPUNIT_FILETABS (cust)->language != language_c))
9902 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9903 }
9904 }
9905 else
9906 {
c24bdb02 9907 cu->get_builder ()->augment_type_symtab ();
43f3e411 9908 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9909 }
9910
af758d11 9911 dwarf2_per_objfile->set_symtab (per_cu, cust);
804d2729
TT
9912
9913 /* Not needed any more. */
c24bdb02 9914 cu->reset_builder ();
c906108c
SS
9915}
9916
95554aad
TT
9917/* Process an imported unit DIE. */
9918
9919static void
9920process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9921{
9922 struct attribute *attr;
9923
f4dc4d17
DE
9924 /* For now we don't handle imported units in type units. */
9925 if (cu->per_cu->is_debug_types)
9926 {
9927 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9928 " supported in type units [in module %s]"),
5e22e966 9929 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9930 }
9931
95554aad
TT
9932 attr = dwarf2_attr (die, DW_AT_import, cu);
9933 if (attr != NULL)
9934 {
0826b30a 9935 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9936 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9937 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9938 dwarf2_per_cu_data *per_cu
ab432490 9939 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9940
58990295
TV
9941 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9942 into another compilation unit, at root level. Regard this as a hint,
9943 and ignore it. */
9944 if (die->parent && die->parent->parent == NULL
9945 && per_cu->unit_type == DW_UT_compile
9946 && per_cu->lang == language_cplus)
9947 return;
9948
69d751e3 9949 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9950 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
ab432490 9951 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 9952
ae640021 9953 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9954 }
9955}
9956
4c8aa72d
PA
9957/* RAII object that represents a process_die scope: i.e.,
9958 starts/finishes processing a DIE. */
9959class process_die_scope
adde2bff 9960{
4c8aa72d
PA
9961public:
9962 process_die_scope (die_info *die, dwarf2_cu *cu)
9963 : m_die (die), m_cu (cu)
9964 {
9965 /* We should only be processing DIEs not already in process. */
9966 gdb_assert (!m_die->in_process);
9967 m_die->in_process = true;
9968 }
8c3cb9fa 9969
4c8aa72d
PA
9970 ~process_die_scope ()
9971 {
9972 m_die->in_process = false;
9973
9974 /* If we're done processing the DIE for the CU that owns the line
9975 header, we don't need the line header anymore. */
9976 if (m_cu->line_header_die_owner == m_die)
9977 {
9978 delete m_cu->line_header;
9979 m_cu->line_header = NULL;
9980 m_cu->line_header_die_owner = NULL;
9981 }
9982 }
9983
9984private:
9985 die_info *m_die;
9986 dwarf2_cu *m_cu;
9987};
adde2bff 9988
c906108c
SS
9989/* Process a die and its children. */
9990
9991static void
e7c27a73 9992process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9993{
4c8aa72d 9994 process_die_scope scope (die, cu);
adde2bff 9995
c906108c
SS
9996 switch (die->tag)
9997 {
9998 case DW_TAG_padding:
9999 break;
10000 case DW_TAG_compile_unit:
95554aad 10001 case DW_TAG_partial_unit:
e7c27a73 10002 read_file_scope (die, cu);
c906108c 10003 break;
348e048f
DE
10004 case DW_TAG_type_unit:
10005 read_type_unit_scope (die, cu);
10006 break;
c906108c 10007 case DW_TAG_subprogram:
0a4b0913
AB
10008 /* Nested subprograms in Fortran get a prefix. */
10009 if (cu->language == language_fortran
10010 && die->parent != NULL
10011 && die->parent->tag == DW_TAG_subprogram)
10012 cu->processing_has_namespace_info = true;
10013 /* Fall through. */
c906108c 10014 case DW_TAG_inlined_subroutine:
edb3359d 10015 read_func_scope (die, cu);
c906108c
SS
10016 break;
10017 case DW_TAG_lexical_block:
14898363
L
10018 case DW_TAG_try_block:
10019 case DW_TAG_catch_block:
e7c27a73 10020 read_lexical_block_scope (die, cu);
c906108c 10021 break;
216f72a1 10022 case DW_TAG_call_site:
96408a79
SA
10023 case DW_TAG_GNU_call_site:
10024 read_call_site_scope (die, cu);
10025 break;
c906108c 10026 case DW_TAG_class_type:
680b30c7 10027 case DW_TAG_interface_type:
c906108c
SS
10028 case DW_TAG_structure_type:
10029 case DW_TAG_union_type:
134d01f1 10030 process_structure_scope (die, cu);
c906108c
SS
10031 break;
10032 case DW_TAG_enumeration_type:
134d01f1 10033 process_enumeration_scope (die, cu);
c906108c 10034 break;
134d01f1 10035
f792889a
DJ
10036 /* These dies have a type, but processing them does not create
10037 a symbol or recurse to process the children. Therefore we can
10038 read them on-demand through read_type_die. */
c906108c 10039 case DW_TAG_subroutine_type:
72019c9c 10040 case DW_TAG_set_type:
c906108c 10041 case DW_TAG_array_type:
c906108c 10042 case DW_TAG_pointer_type:
c906108c 10043 case DW_TAG_ptr_to_member_type:
c906108c 10044 case DW_TAG_reference_type:
4297a3f0 10045 case DW_TAG_rvalue_reference_type:
c906108c 10046 case DW_TAG_string_type:
c906108c 10047 break;
134d01f1 10048
c906108c 10049 case DW_TAG_base_type:
a02abb62 10050 case DW_TAG_subrange_type:
cb249c71 10051 case DW_TAG_typedef:
134d01f1
DJ
10052 /* Add a typedef symbol for the type definition, if it has a
10053 DW_AT_name. */
f792889a 10054 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10055 break;
c906108c 10056 case DW_TAG_common_block:
e7c27a73 10057 read_common_block (die, cu);
c906108c
SS
10058 break;
10059 case DW_TAG_common_inclusion:
10060 break;
d9fa45fe 10061 case DW_TAG_namespace:
9068261f 10062 cu->processing_has_namespace_info = true;
e7c27a73 10063 read_namespace (die, cu);
d9fa45fe 10064 break;
5d7cb8df 10065 case DW_TAG_module:
9068261f 10066 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10067 read_module (die, cu);
10068 break;
d9fa45fe 10069 case DW_TAG_imported_declaration:
9068261f 10070 cu->processing_has_namespace_info = true;
74921315
KS
10071 if (read_namespace_alias (die, cu))
10072 break;
86a73007
TT
10073 /* The declaration is not a global namespace alias. */
10074 /* Fall through. */
d9fa45fe 10075 case DW_TAG_imported_module:
9068261f 10076 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10077 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10078 || cu->language != language_fortran))
b98664d3 10079 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10080 dwarf_tag_name (die->tag));
10081 read_import_statement (die, cu);
d9fa45fe 10082 break;
95554aad
TT
10083
10084 case DW_TAG_imported_unit:
10085 process_imported_unit_die (die, cu);
10086 break;
10087
71a3c369
TT
10088 case DW_TAG_variable:
10089 read_variable (die, cu);
10090 break;
10091
c906108c 10092 default:
e7c27a73 10093 new_symbol (die, NULL, cu);
c906108c
SS
10094 break;
10095 }
10096}
ca69b9e6
DE
10097\f
10098/* DWARF name computation. */
c906108c 10099
94af9270
KS
10100/* A helper function for dwarf2_compute_name which determines whether DIE
10101 needs to have the name of the scope prepended to the name listed in the
10102 die. */
10103
10104static int
10105die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10106{
1c809c68
TT
10107 struct attribute *attr;
10108
94af9270
KS
10109 switch (die->tag)
10110 {
10111 case DW_TAG_namespace:
10112 case DW_TAG_typedef:
10113 case DW_TAG_class_type:
10114 case DW_TAG_interface_type:
10115 case DW_TAG_structure_type:
10116 case DW_TAG_union_type:
10117 case DW_TAG_enumeration_type:
10118 case DW_TAG_enumerator:
10119 case DW_TAG_subprogram:
08a76f8a 10120 case DW_TAG_inlined_subroutine:
94af9270 10121 case DW_TAG_member:
74921315 10122 case DW_TAG_imported_declaration:
94af9270
KS
10123 return 1;
10124
10125 case DW_TAG_variable:
c2b0a229 10126 case DW_TAG_constant:
94af9270
KS
10127 /* We only need to prefix "globally" visible variables. These include
10128 any variable marked with DW_AT_external or any variable that
10129 lives in a namespace. [Variables in anonymous namespaces
10130 require prefixing, but they are not DW_AT_external.] */
10131
10132 if (dwarf2_attr (die, DW_AT_specification, cu))
10133 {
10134 struct dwarf2_cu *spec_cu = cu;
9a619af0 10135
94af9270
KS
10136 return die_needs_namespace (die_specification (die, &spec_cu),
10137 spec_cu);
10138 }
10139
1c809c68 10140 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10141 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10142 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10143 return 0;
10144 /* A variable in a lexical block of some kind does not need a
10145 namespace, even though in C++ such variables may be external
10146 and have a mangled name. */
10147 if (die->parent->tag == DW_TAG_lexical_block
10148 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10149 || die->parent->tag == DW_TAG_catch_block
10150 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10151 return 0;
10152 return 1;
94af9270
KS
10153
10154 default:
10155 return 0;
10156 }
10157}
10158
73b9be8b
KS
10159/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10160 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10161 defined for the given DIE. */
10162
10163static struct attribute *
10164dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10165{
10166 struct attribute *attr;
10167
10168 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10169 if (attr == NULL)
10170 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10171
10172 return attr;
10173}
10174
10175/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10176 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10177 defined for the given DIE. */
10178
10179static const char *
10180dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10181{
10182 const char *linkage_name;
10183
10184 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10185 if (linkage_name == NULL)
10186 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10187
787de330
TT
10188 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10189 See https://github.com/rust-lang/rust/issues/32925. */
10190 if (cu->language == language_rust && linkage_name != NULL
10191 && strchr (linkage_name, '{') != NULL)
10192 linkage_name = NULL;
10193
73b9be8b
KS
10194 return linkage_name;
10195}
10196
94af9270 10197/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10198 compute the physname for the object, which include a method's:
9c37b5ae 10199 - formal parameters (C++),
a766d390 10200 - receiver type (Go),
a766d390
DE
10201
10202 The term "physname" is a bit confusing.
10203 For C++, for example, it is the demangled name.
10204 For Go, for example, it's the mangled name.
94af9270 10205
af6b7be1
JB
10206 For Ada, return the DIE's linkage name rather than the fully qualified
10207 name. PHYSNAME is ignored..
10208
5989a64e 10209 The result is allocated on the objfile->per_bfd's obstack and
45940949 10210 canonicalized. */
94af9270
KS
10211
10212static const char *
15d034d0
TT
10213dwarf2_compute_name (const char *name,
10214 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10215 int physname)
10216{
5e22e966 10217 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10218
94af9270
KS
10219 if (name == NULL)
10220 name = dwarf2_name (die, cu);
10221
2ee7123e
DE
10222 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10223 but otherwise compute it by typename_concat inside GDB.
10224 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10225 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10226 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10227 will set the demangled name to the result of dwarf2_full_name, and it is
10228 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10229 if (cu->language == language_ada
10230 || (cu->language == language_fortran && physname))
10231 {
10232 /* For Ada unit, we prefer the linkage name over the name, as
10233 the former contains the exported name, which the user expects
10234 to be able to reference. Ideally, we want the user to be able
10235 to reference this entity using either natural or linkage name,
10236 but we haven't started looking at this enhancement yet. */
73b9be8b 10237 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10238
2ee7123e
DE
10239 if (linkage_name != NULL)
10240 return linkage_name;
f55ee35c
JK
10241 }
10242
94af9270
KS
10243 /* These are the only languages we know how to qualify names in. */
10244 if (name != NULL
9c37b5ae 10245 && (cu->language == language_cplus
c44af4eb
TT
10246 || cu->language == language_fortran || cu->language == language_d
10247 || cu->language == language_rust))
94af9270
KS
10248 {
10249 if (die_needs_namespace (die, cu))
10250 {
0d5cff50 10251 const char *prefix;
34a68019 10252 const char *canonical_name = NULL;
94af9270 10253
d7e74731
PA
10254 string_file buf;
10255
94af9270 10256 prefix = determine_prefix (die, cu);
94af9270
KS
10257 if (*prefix != '\0')
10258 {
43816ebc
TT
10259 gdb::unique_xmalloc_ptr<char> prefixed_name
10260 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10261
43816ebc 10262 buf.puts (prefixed_name.get ());
94af9270
KS
10263 }
10264 else
d7e74731 10265 buf.puts (name);
94af9270 10266
98bfdba5
PA
10267 /* Template parameters may be specified in the DIE's DW_AT_name, or
10268 as children with DW_TAG_template_type_param or
10269 DW_TAG_value_type_param. If the latter, add them to the name
10270 here. If the name already has template parameters, then
10271 skip this step; some versions of GCC emit both, and
10272 it is more efficient to use the pre-computed name.
10273
10274 Something to keep in mind about this process: it is very
10275 unlikely, or in some cases downright impossible, to produce
10276 something that will match the mangled name of a function.
10277 If the definition of the function has the same debug info,
10278 we should be able to match up with it anyway. But fallbacks
10279 using the minimal symbol, for instance to find a method
10280 implemented in a stripped copy of libstdc++, will not work.
10281 If we do not have debug info for the definition, we will have to
10282 match them up some other way.
10283
10284 When we do name matching there is a related problem with function
10285 templates; two instantiated function templates are allowed to
10286 differ only by their return types, which we do not add here. */
10287
10288 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10289 {
10290 struct attribute *attr;
10291 struct die_info *child;
10292 int first = 1;
10293
10294 die->building_fullname = 1;
10295
10296 for (child = die->child; child != NULL; child = child->sibling)
10297 {
10298 struct type *type;
12df843f 10299 LONGEST value;
d521ce57 10300 const gdb_byte *bytes;
98bfdba5
PA
10301 struct dwarf2_locexpr_baton *baton;
10302 struct value *v;
10303
10304 if (child->tag != DW_TAG_template_type_param
10305 && child->tag != DW_TAG_template_value_param)
10306 continue;
10307
10308 if (first)
10309 {
d7e74731 10310 buf.puts ("<");
98bfdba5
PA
10311 first = 0;
10312 }
10313 else
d7e74731 10314 buf.puts (", ");
98bfdba5
PA
10315
10316 attr = dwarf2_attr (child, DW_AT_type, cu);
10317 if (attr == NULL)
10318 {
b98664d3 10319 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10320 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10321 continue;
10322 }
10323 type = die_type (child, cu);
10324
10325 if (child->tag == DW_TAG_template_type_param)
10326 {
c1ec8cea
TT
10327 c_print_type (type, "", &buf, -1, 0, cu->language,
10328 &type_print_raw_options);
98bfdba5
PA
10329 continue;
10330 }
10331
10332 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10333 if (attr == NULL)
10334 {
b98664d3 10335 complaint (_("template parameter missing "
3e43a32a 10336 "DW_AT_const_value"));
d7e74731 10337 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10338 continue;
10339 }
10340
10341 dwarf2_const_value_attr (attr, type, name,
10342 &cu->comp_unit_obstack, cu,
10343 &value, &bytes, &baton);
10344
10345 if (TYPE_NOSIGN (type))
10346 /* GDB prints characters as NUMBER 'CHAR'. If that's
10347 changed, this can use value_print instead. */
d7e74731 10348 c_printchar (value, type, &buf);
98bfdba5
PA
10349 else
10350 {
10351 struct value_print_options opts;
10352
10353 if (baton != NULL)
10354 v = dwarf2_evaluate_loc_desc (type, NULL,
10355 baton->data,
10356 baton->size,
10357 baton->per_cu);
10358 else if (bytes != NULL)
10359 {
10360 v = allocate_value (type);
10361 memcpy (value_contents_writeable (v), bytes,
10362 TYPE_LENGTH (type));
10363 }
10364 else
10365 v = value_from_longest (type, value);
10366
3e43a32a
MS
10367 /* Specify decimal so that we do not depend on
10368 the radix. */
98bfdba5
PA
10369 get_formatted_print_options (&opts, 'd');
10370 opts.raw = 1;
d7e74731 10371 value_print (v, &buf, &opts);
98bfdba5 10372 release_value (v);
98bfdba5
PA
10373 }
10374 }
10375
10376 die->building_fullname = 0;
10377
10378 if (!first)
10379 {
10380 /* Close the argument list, with a space if necessary
10381 (nested templates). */
d7e74731
PA
10382 if (!buf.empty () && buf.string ().back () == '>')
10383 buf.puts (" >");
98bfdba5 10384 else
d7e74731 10385 buf.puts (">");
98bfdba5
PA
10386 }
10387 }
10388
9c37b5ae 10389 /* For C++ methods, append formal parameter type
94af9270 10390 information, if PHYSNAME. */
6e70227d 10391
94af9270 10392 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10393 && cu->language == language_cplus)
94af9270
KS
10394 {
10395 struct type *type = read_type_die (die, cu);
10396
d7e74731 10397 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10398 &type_print_raw_options);
94af9270 10399
9c37b5ae 10400 if (cu->language == language_cplus)
94af9270 10401 {
60430eff
DJ
10402 /* Assume that an artificial first parameter is
10403 "this", but do not crash if it is not. RealView
10404 marks unnamed (and thus unused) parameters as
10405 artificial; there is no way to differentiate
10406 the two cases. */
1f704f76 10407 if (type->num_fields () > 0
94af9270 10408 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10409 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10410 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10411 0))))
d7e74731 10412 buf.puts (" const");
94af9270
KS
10413 }
10414 }
10415
d7e74731 10416 const std::string &intermediate_name = buf.string ();
94af9270
KS
10417
10418 if (cu->language == language_cplus)
34a68019 10419 canonical_name
322a8516 10420 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10421 objfile);
34a68019
TT
10422
10423 /* If we only computed INTERMEDIATE_NAME, or if
10424 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10425 intern it. */
322a8516 10426 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10427 name = objfile->intern (intermediate_name);
34a68019
TT
10428 else
10429 name = canonical_name;
94af9270
KS
10430 }
10431 }
10432
10433 return name;
10434}
10435
0114d602
DJ
10436/* Return the fully qualified name of DIE, based on its DW_AT_name.
10437 If scope qualifiers are appropriate they will be added. The result
34a68019 10438 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10439 not have a name. NAME may either be from a previous call to
10440 dwarf2_name or NULL.
10441
9c37b5ae 10442 The output string will be canonicalized (if C++). */
0114d602
DJ
10443
10444static const char *
15d034d0 10445dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10446{
94af9270
KS
10447 return dwarf2_compute_name (name, die, cu, 0);
10448}
0114d602 10449
94af9270
KS
10450/* Construct a physname for the given DIE in CU. NAME may either be
10451 from a previous call to dwarf2_name or NULL. The result will be
10452 allocated on the objfile_objstack or NULL if the DIE does not have a
10453 name.
0114d602 10454
9c37b5ae 10455 The output string will be canonicalized (if C++). */
0114d602 10456
94af9270 10457static const char *
15d034d0 10458dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10459{
5e22e966 10460 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10461 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10462 int need_copy = 1;
10463
10464 /* In this case dwarf2_compute_name is just a shortcut not building anything
10465 on its own. */
10466 if (!die_needs_namespace (die, cu))
10467 return dwarf2_compute_name (name, die, cu, 1);
10468
906bb4c5
TT
10469 if (cu->language != language_rust)
10470 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10471
10472 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10473 has computed. */
791afaa2 10474 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10475 if (mangled != NULL)
900e11f9 10476 {
900e11f9 10477
59cc4834
JB
10478 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10479 {
10480 /* Do nothing (do not demangle the symbol name). */
10481 }
10482 else if (cu->language == language_go)
a766d390 10483 {
5e2db402
TT
10484 /* This is a lie, but we already lie to the caller new_symbol.
10485 new_symbol assumes we return the mangled name.
a766d390 10486 This just undoes that lie until things are cleaned up. */
a766d390
DE
10487 }
10488 else
10489 {
0eb876f5
JB
10490 /* Use DMGL_RET_DROP for C++ template functions to suppress
10491 their return type. It is easier for GDB users to search
10492 for such functions as `name(params)' than `long name(params)'.
10493 In such case the minimal symbol names do not match the full
10494 symbol names but for template functions there is never a need
10495 to look up their definition from their declaration so
10496 the only disadvantage remains the minimal symbol variant
10497 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10498 demangled.reset (gdb_demangle (mangled,
10499 (DMGL_PARAMS | DMGL_ANSI
10500 | DMGL_RET_DROP)));
a766d390 10501 }
900e11f9 10502 if (demangled)
791afaa2 10503 canon = demangled.get ();
900e11f9
JK
10504 else
10505 {
10506 canon = mangled;
10507 need_copy = 0;
10508 }
10509 }
10510
10511 if (canon == NULL || check_physname)
10512 {
10513 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10514
10515 if (canon != NULL && strcmp (physname, canon) != 0)
10516 {
10517 /* It may not mean a bug in GDB. The compiler could also
10518 compute DW_AT_linkage_name incorrectly. But in such case
10519 GDB would need to be bug-to-bug compatible. */
10520
b98664d3 10521 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10522 "(from linkage <%s>) - DIE at %s [in module %s]"),
10523 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10524 objfile_name (objfile));
900e11f9
JK
10525
10526 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10527 is available here - over computed PHYSNAME. It is safer
10528 against both buggy GDB and buggy compilers. */
10529
10530 retval = canon;
10531 }
10532 else
10533 {
10534 retval = physname;
10535 need_copy = 0;
10536 }
10537 }
10538 else
10539 retval = canon;
10540
10541 if (need_copy)
be1e3d3e 10542 retval = objfile->intern (retval);
900e11f9 10543
900e11f9 10544 return retval;
0114d602
DJ
10545}
10546
74921315
KS
10547/* Inspect DIE in CU for a namespace alias. If one exists, record
10548 a new symbol for it.
10549
10550 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10551
10552static int
10553read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10554{
10555 struct attribute *attr;
10556
10557 /* If the die does not have a name, this is not a namespace
10558 alias. */
10559 attr = dwarf2_attr (die, DW_AT_name, cu);
10560 if (attr != NULL)
10561 {
10562 int num;
10563 struct die_info *d = die;
10564 struct dwarf2_cu *imported_cu = cu;
10565
10566 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10567 keep inspecting DIEs until we hit the underlying import. */
10568#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10569 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10570 {
10571 attr = dwarf2_attr (d, DW_AT_import, cu);
10572 if (attr == NULL)
10573 break;
10574
10575 d = follow_die_ref (d, attr, &imported_cu);
10576 if (d->tag != DW_TAG_imported_declaration)
10577 break;
10578 }
10579
10580 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10581 {
b98664d3 10582 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10583 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10584 return 0;
10585 }
10586
10587 if (attr != NULL)
10588 {
10589 struct type *type;
0826b30a 10590 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10591
9c541725 10592 type = get_die_type_at_offset (sect_off, cu->per_cu);
78134374 10593 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10594 {
10595 /* This declaration is a global namespace alias. Add
10596 a symbol for it whose type is the aliased namespace. */
10597 new_symbol (die, type, cu);
10598 return 1;
10599 }
10600 }
10601 }
10602
10603 return 0;
10604}
10605
22cee43f 10606/* Return the using directives repository (global or local?) to use in the
804d2729 10607 current context for CU.
22cee43f
PMR
10608
10609 For Ada, imported declarations can materialize renamings, which *may* be
10610 global. However it is impossible (for now?) in DWARF to distinguish
10611 "external" imported declarations and "static" ones. As all imported
10612 declarations seem to be static in all other languages, make them all CU-wide
10613 global only in Ada. */
10614
10615static struct using_direct **
804d2729 10616using_directives (struct dwarf2_cu *cu)
22cee43f 10617{
c24bdb02
KS
10618 if (cu->language == language_ada
10619 && cu->get_builder ()->outermost_context_p ())
10620 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10621 else
c24bdb02 10622 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10623}
10624
27aa8d6a
SW
10625/* Read the import statement specified by the given die and record it. */
10626
10627static void
10628read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10629{
5e22e966 10630 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10631 struct attribute *import_attr;
32019081 10632 struct die_info *imported_die, *child_die;
de4affc9 10633 struct dwarf2_cu *imported_cu;
27aa8d6a 10634 const char *imported_name;
794684b6 10635 const char *imported_name_prefix;
13387711
SW
10636 const char *canonical_name;
10637 const char *import_alias;
10638 const char *imported_declaration = NULL;
794684b6 10639 const char *import_prefix;
eb1e02fd 10640 std::vector<const char *> excludes;
13387711 10641
27aa8d6a
SW
10642 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10643 if (import_attr == NULL)
10644 {
b98664d3 10645 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10646 dwarf_tag_name (die->tag));
10647 return;
10648 }
10649
de4affc9
CC
10650 imported_cu = cu;
10651 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10652 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10653 if (imported_name == NULL)
10654 {
10655 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10656
10657 The import in the following code:
10658 namespace A
10659 {
10660 typedef int B;
10661 }
10662
10663 int main ()
10664 {
10665 using A::B;
10666 B b;
10667 return b;
10668 }
10669
10670 ...
10671 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10672 <52> DW_AT_decl_file : 1
10673 <53> DW_AT_decl_line : 6
10674 <54> DW_AT_import : <0x75>
10675 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10676 <59> DW_AT_name : B
10677 <5b> DW_AT_decl_file : 1
10678 <5c> DW_AT_decl_line : 2
10679 <5d> DW_AT_type : <0x6e>
10680 ...
10681 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10682 <76> DW_AT_byte_size : 4
10683 <77> DW_AT_encoding : 5 (signed)
10684
10685 imports the wrong die ( 0x75 instead of 0x58 ).
10686 This case will be ignored until the gcc bug is fixed. */
10687 return;
10688 }
10689
82856980
SW
10690 /* Figure out the local name after import. */
10691 import_alias = dwarf2_name (die, cu);
27aa8d6a 10692
794684b6
SW
10693 /* Figure out where the statement is being imported to. */
10694 import_prefix = determine_prefix (die, cu);
10695
10696 /* Figure out what the scope of the imported die is and prepend it
10697 to the name of the imported die. */
de4affc9 10698 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10699
f55ee35c
JK
10700 if (imported_die->tag != DW_TAG_namespace
10701 && imported_die->tag != DW_TAG_module)
794684b6 10702 {
13387711
SW
10703 imported_declaration = imported_name;
10704 canonical_name = imported_name_prefix;
794684b6 10705 }
13387711 10706 else if (strlen (imported_name_prefix) > 0)
12aaed36 10707 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10708 imported_name_prefix,
10709 (cu->language == language_d ? "." : "::"),
10710 imported_name, (char *) NULL);
13387711
SW
10711 else
10712 canonical_name = imported_name;
794684b6 10713
32019081
JK
10714 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10715 for (child_die = die->child; child_die && child_die->tag;
436c571c 10716 child_die = child_die->sibling)
32019081
JK
10717 {
10718 /* DWARF-4: A Fortran use statement with a “rename list” may be
10719 represented by an imported module entry with an import attribute
10720 referring to the module and owned entries corresponding to those
10721 entities that are renamed as part of being imported. */
10722
10723 if (child_die->tag != DW_TAG_imported_declaration)
10724 {
b98664d3 10725 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10726 "- DIE at %s [in module %s]"),
10727 sect_offset_str (child_die->sect_off),
10728 objfile_name (objfile));
32019081
JK
10729 continue;
10730 }
10731
10732 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10733 if (import_attr == NULL)
10734 {
b98664d3 10735 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10736 dwarf_tag_name (child_die->tag));
10737 continue;
10738 }
10739
10740 imported_cu = cu;
10741 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10742 &imported_cu);
10743 imported_name = dwarf2_name (imported_die, imported_cu);
10744 if (imported_name == NULL)
10745 {
b98664d3 10746 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10747 "imported name - DIE at %s [in module %s]"),
10748 sect_offset_str (child_die->sect_off),
10749 objfile_name (objfile));
32019081
JK
10750 continue;
10751 }
10752
eb1e02fd 10753 excludes.push_back (imported_name);
32019081
JK
10754
10755 process_die (child_die, cu);
10756 }
10757
804d2729 10758 add_using_directive (using_directives (cu),
22cee43f
PMR
10759 import_prefix,
10760 canonical_name,
10761 import_alias,
10762 imported_declaration,
10763 excludes,
10764 0,
10765 &objfile->objfile_obstack);
27aa8d6a
SW
10766}
10767
5230b05a
WT
10768/* ICC<14 does not output the required DW_AT_declaration on incomplete
10769 types, but gives them a size of zero. Starting with version 14,
10770 ICC is compatible with GCC. */
10771
9068261f 10772static bool
5230b05a
WT
10773producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10774{
10775 if (!cu->checked_producer)
10776 check_producer (cu);
10777
10778 return cu->producer_is_icc_lt_14;
10779}
10780
eb77c9df
AB
10781/* ICC generates a DW_AT_type for C void functions. This was observed on
10782 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10783 which says that void functions should not have a DW_AT_type. */
10784
10785static bool
10786producer_is_icc (struct dwarf2_cu *cu)
10787{
10788 if (!cu->checked_producer)
10789 check_producer (cu);
10790
10791 return cu->producer_is_icc;
10792}
10793
1b80a9fa
JK
10794/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10795 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10796 this, it was first present in GCC release 4.3.0. */
10797
9068261f 10798static bool
1b80a9fa
JK
10799producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10800{
10801 if (!cu->checked_producer)
10802 check_producer (cu);
10803
10804 return cu->producer_is_gcc_lt_4_3;
10805}
10806
d721ba37
PA
10807static file_and_directory
10808find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10809{
d721ba37
PA
10810 file_and_directory res;
10811
9291a0cd
TT
10812 /* Find the filename. Do not use dwarf2_name here, since the filename
10813 is not a source language identifier. */
d721ba37
PA
10814 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10815 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10816
d721ba37
PA
10817 if (res.comp_dir == NULL
10818 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10819 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10820 {
d721ba37
PA
10821 res.comp_dir_storage = ldirname (res.name);
10822 if (!res.comp_dir_storage.empty ())
10823 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10824 }
d721ba37 10825 if (res.comp_dir != NULL)
9291a0cd
TT
10826 {
10827 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10828 directory, get rid of it. */
d721ba37 10829 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10830
d721ba37
PA
10831 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10832 res.comp_dir = cp + 1;
9291a0cd
TT
10833 }
10834
d721ba37
PA
10835 if (res.name == NULL)
10836 res.name = "<unknown>";
10837
10838 return res;
9291a0cd
TT
10839}
10840
f4dc4d17
DE
10841/* Handle DW_AT_stmt_list for a compilation unit.
10842 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10843 COMP_DIR is the compilation directory. LOWPC is passed to
10844 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10845
10846static void
10847handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10848 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10849{
5e22e966 10850 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10851 struct attribute *attr;
527f3840
JK
10852 struct line_header line_header_local;
10853 hashval_t line_header_local_hash;
527f3840
JK
10854 void **slot;
10855 int decode_mapping;
2ab95328 10856
f4dc4d17
DE
10857 gdb_assert (! cu->per_cu->is_debug_types);
10858
2ab95328 10859 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10860 if (attr == NULL)
10861 return;
10862
9c541725 10863 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10864
10865 /* The line header hash table is only created if needed (it exists to
10866 prevent redundant reading of the line table for partial_units).
10867 If we're given a partial_unit, we'll need it. If we're given a
10868 compile_unit, then use the line header hash table if it's already
10869 created, but don't create one just yet. */
10870
5989a64e 10871 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10872 && die->tag == DW_TAG_partial_unit)
2ab95328 10873 {
5989a64e 10874 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10875 .reset (htab_create_alloc (127, line_header_hash_voidp,
10876 line_header_eq_voidp,
10877 free_line_header_voidp,
10878 xcalloc, xfree));
527f3840 10879 }
2ab95328 10880
9c541725 10881 line_header_local.sect_off = line_offset;
527f3840
JK
10882 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10883 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10884 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10885 {
5989a64e 10886 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10887 &line_header_local,
10888 line_header_local_hash, NO_INSERT);
10889
10890 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10891 is not present in *SLOT (since if there is something in *SLOT then
10892 it will be for a partial_unit). */
10893 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10894 {
527f3840 10895 gdb_assert (*slot != NULL);
9a3c8263 10896 cu->line_header = (struct line_header *) *slot;
527f3840 10897 return;
dee91e82 10898 }
2ab95328 10899 }
527f3840
JK
10900
10901 /* dwarf_decode_line_header does not yet provide sufficient information.
10902 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10903 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10904 if (lh == NULL)
527f3840 10905 return;
4c8aa72d
PA
10906
10907 cu->line_header = lh.release ();
10908 cu->line_header_die_owner = die;
527f3840 10909
5989a64e 10910 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10911 slot = NULL;
10912 else
10913 {
5989a64e 10914 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10915 &line_header_local,
10916 line_header_local_hash, INSERT);
10917 gdb_assert (slot != NULL);
10918 }
10919 if (slot != NULL && *slot == NULL)
10920 {
10921 /* This newly decoded line number information unit will be owned
10922 by line_header_hash hash table. */
10923 *slot = cu->line_header;
4c8aa72d 10924 cu->line_header_die_owner = NULL;
527f3840
JK
10925 }
10926 else
10927 {
10928 /* We cannot free any current entry in (*slot) as that struct line_header
10929 may be already used by multiple CUs. Create only temporary decoded
10930 line_header for this CU - it may happen at most once for each line
10931 number information unit. And if we're not using line_header_hash
10932 then this is what we want as well. */
10933 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10934 }
10935 decode_mapping = (die->tag != DW_TAG_partial_unit);
10936 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10937 decode_mapping);
fff8551c 10938
2ab95328
TT
10939}
10940
95554aad 10941/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10942
c906108c 10943static void
e7c27a73 10944read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10945{
5e22e966 10946 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 10947 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10948 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10949 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10950 CORE_ADDR highpc = ((CORE_ADDR) 0);
10951 struct attribute *attr;
c906108c 10952 struct die_info *child_die;
e142c38c 10953 CORE_ADDR baseaddr;
6e70227d 10954
380618d6 10955 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10956 baseaddr = objfile->text_section_offset ();
c906108c 10957
fae299cd 10958 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10959
10960 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10961 from finish_block. */
2acceee2 10962 if (lowpc == ((CORE_ADDR) -1))
c906108c 10963 lowpc = highpc;
3e29f34a 10964 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10965
d721ba37 10966 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10967
f4b8a18d
KW
10968 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10969 standardised yet. As a workaround for the language detection we fall
10970 back to the DW_AT_producer string. */
10971 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10972 cu->language = language_opencl;
10973
3019eac3
DE
10974 /* Similar hack for Go. */
10975 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10976 set_cu_language (DW_LANG_Go, cu);
10977
c24bdb02 10978 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10979
10980 /* Decode line number information if present. We do this before
10981 processing child DIEs, so that the line header table is available
10982 for DW_AT_decl_file. */
d721ba37 10983 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10984
10985 /* Process all dies in compilation unit. */
10986 if (die->child != NULL)
10987 {
10988 child_die = die->child;
10989 while (child_die && child_die->tag)
10990 {
10991 process_die (child_die, cu);
436c571c 10992 child_die = child_die->sibling;
3019eac3
DE
10993 }
10994 }
10995
10996 /* Decode macro information, if present. Dwarf 2 macro information
10997 refers to information in the line number info statement program
10998 header, so we can only read it if we've read the header
10999 successfully. */
0af92d60
JK
11000 attr = dwarf2_attr (die, DW_AT_macros, cu);
11001 if (attr == NULL)
11002 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11003 if (attr && cu->line_header)
11004 {
11005 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11006 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11007
43f3e411 11008 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11009 }
11010 else
11011 {
11012 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11013 if (attr && cu->line_header)
11014 {
11015 unsigned int macro_offset = DW_UNSND (attr);
11016
43f3e411 11017 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11018 }
11019 }
3019eac3
DE
11020}
11021
c24bdb02
KS
11022void
11023dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11024{
f4dc4d17
DE
11025 struct type_unit_group *tu_group;
11026 int first_time;
3019eac3 11027 struct attribute *attr;
9c541725 11028 unsigned int i;
0186c6a7 11029 struct signatured_type *sig_type;
3019eac3 11030
f4dc4d17 11031 gdb_assert (per_cu->is_debug_types);
0186c6a7 11032 sig_type = (struct signatured_type *) per_cu;
3019eac3 11033
c24bdb02 11034 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11035
f4dc4d17 11036 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11037 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11038 if (sig_type->type_unit_group == NULL)
c24bdb02 11039 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11040 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11041
11042 /* If we've already processed this stmt_list there's no real need to
11043 do it again, we could fake it and just recreate the part we need
11044 (file name,index -> symtab mapping). If data shows this optimization
11045 is useful we can do it then. */
43f3e411 11046 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11047
11048 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11049 debug info. */
fff8551c 11050 line_header_up lh;
f4dc4d17 11051 if (attr != NULL)
3019eac3 11052 {
9c541725 11053 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11054 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11055 }
11056 if (lh == NULL)
11057 {
11058 if (first_time)
c24bdb02 11059 start_symtab ("", NULL, 0);
f4dc4d17
DE
11060 else
11061 {
11062 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11063 gdb_assert (m_builder == nullptr);
804d2729 11064 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11065 m_builder.reset (new struct buildsym_compunit
11066 (COMPUNIT_OBJFILE (cust), "",
11067 COMPUNIT_DIRNAME (cust),
11068 compunit_language (cust),
11069 0, cust));
770479f2 11070 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11071 }
f4dc4d17 11072 return;
3019eac3
DE
11073 }
11074
c24bdb02
KS
11075 line_header = lh.release ();
11076 line_header_die_owner = die;
3019eac3 11077
f4dc4d17
DE
11078 if (first_time)
11079 {
c24bdb02 11080 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11081
1fd60fc0
DE
11082 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11083 still initializing it, and our caller (a few levels up)
11084 process_full_type_unit still needs to know if this is the first
11085 time. */
11086
4ac93832
TT
11087 tu_group->symtabs
11088 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11089 struct symtab *, line_header->file_names_size ());
3019eac3 11090
7ba99d21
AT
11091 auto &file_names = line_header->file_names ();
11092 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11093 {
7ba99d21 11094 file_entry &fe = file_names[i];
c24bdb02
KS
11095 dwarf2_start_subfile (this, fe.name,
11096 fe.include_dir (line_header));
11097 buildsym_compunit *b = get_builder ();
11098 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11099 {
4c8aa72d
PA
11100 /* NOTE: start_subfile will recognize when it's been
11101 passed a file it has already seen. So we can't
11102 assume there's a simple mapping from
11103 cu->line_header->file_names to subfiles, plus
11104 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11105 b->get_current_subfile ()->symtab
11106 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11107 }
11108
c24bdb02 11109 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11110 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11111 }
11112 }
11113 else
3019eac3 11114 {
c24bdb02 11115 gdb_assert (m_builder == nullptr);
804d2729 11116 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11117 m_builder.reset (new struct buildsym_compunit
11118 (COMPUNIT_OBJFILE (cust), "",
11119 COMPUNIT_DIRNAME (cust),
11120 compunit_language (cust),
11121 0, cust));
770479f2 11122 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11123
7ba99d21
AT
11124 auto &file_names = line_header->file_names ();
11125 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11126 {
7ba99d21 11127 file_entry &fe = file_names[i];
4c8aa72d 11128 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11129 }
3019eac3
DE
11130 }
11131
f4dc4d17
DE
11132 /* The main symtab is allocated last. Type units don't have DW_AT_name
11133 so they don't have a "real" (so to speak) symtab anyway.
11134 There is later code that will assign the main symtab to all symbols
11135 that don't have one. We need to handle the case of a symbol with a
11136 missing symtab (DW_AT_decl_file) anyway. */
11137}
3019eac3 11138
f4dc4d17
DE
11139/* Process DW_TAG_type_unit.
11140 For TUs we want to skip the first top level sibling if it's not the
11141 actual type being defined by this TU. In this case the first top
11142 level sibling is there to provide context only. */
3019eac3 11143
f4dc4d17
DE
11144static void
11145read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11146{
11147 struct die_info *child_die;
3019eac3 11148
f4dc4d17
DE
11149 prepare_one_comp_unit (cu, die, language_minimal);
11150
11151 /* Initialize (or reinitialize) the machinery for building symtabs.
11152 We do this before processing child DIEs, so that the line header table
11153 is available for DW_AT_decl_file. */
c24bdb02 11154 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11155
11156 if (die->child != NULL)
11157 {
11158 child_die = die->child;
11159 while (child_die && child_die->tag)
11160 {
11161 process_die (child_die, cu);
436c571c 11162 child_die = child_die->sibling;
f4dc4d17
DE
11163 }
11164 }
3019eac3
DE
11165}
11166\f
80626a55
DE
11167/* DWO/DWP files.
11168
11169 http://gcc.gnu.org/wiki/DebugFission
11170 http://gcc.gnu.org/wiki/DebugFissionDWP
11171
11172 To simplify handling of both DWO files ("object" files with the DWARF info)
11173 and DWP files (a file with the DWOs packaged up into one file), we treat
11174 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11175
11176static hashval_t
11177hash_dwo_file (const void *item)
11178{
9a3c8263 11179 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11180 hashval_t hash;
3019eac3 11181
a2ce51a0
DE
11182 hash = htab_hash_string (dwo_file->dwo_name);
11183 if (dwo_file->comp_dir != NULL)
11184 hash += htab_hash_string (dwo_file->comp_dir);
11185 return hash;
3019eac3
DE
11186}
11187
11188static int
11189eq_dwo_file (const void *item_lhs, const void *item_rhs)
11190{
9a3c8263
SM
11191 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11192 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11193
a2ce51a0
DE
11194 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11195 return 0;
11196 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11197 return lhs->comp_dir == rhs->comp_dir;
11198 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11199}
11200
11201/* Allocate a hash table for DWO files. */
11202
51ac9db5 11203static htab_up
298e9637 11204allocate_dwo_file_hash_table ()
3019eac3 11205{
51ac9db5
SM
11206 auto delete_dwo_file = [] (void *item)
11207 {
11208 struct dwo_file *dwo_file = (struct dwo_file *) item;
11209
11210 delete dwo_file;
11211 };
11212
bc68fb19
TT
11213 return htab_up (htab_create_alloc (41,
11214 hash_dwo_file,
11215 eq_dwo_file,
11216 delete_dwo_file,
11217 xcalloc, xfree));
3019eac3
DE
11218}
11219
80626a55
DE
11220/* Lookup DWO file DWO_NAME. */
11221
11222static void **
ed2dc618
SM
11223lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11224 const char *dwo_name,
11225 const char *comp_dir)
80626a55
DE
11226{
11227 struct dwo_file find_entry;
11228 void **slot;
11229
5989a64e
SM
11230 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11231 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11232
0ac5b59e
DE
11233 find_entry.dwo_name = dwo_name;
11234 find_entry.comp_dir = comp_dir;
5989a64e 11235 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11236 INSERT);
80626a55
DE
11237
11238 return slot;
11239}
11240
3019eac3
DE
11241static hashval_t
11242hash_dwo_unit (const void *item)
11243{
9a3c8263 11244 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11245
11246 /* This drops the top 32 bits of the id, but is ok for a hash. */
11247 return dwo_unit->signature;
11248}
11249
11250static int
11251eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11252{
9a3c8263
SM
11253 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11254 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11255
11256 /* The signature is assumed to be unique within the DWO file.
11257 So while object file CU dwo_id's always have the value zero,
11258 that's OK, assuming each object file DWO file has only one CU,
11259 and that's the rule for now. */
11260 return lhs->signature == rhs->signature;
11261}
11262
11263/* Allocate a hash table for DWO CUs,TUs.
11264 There is one of these tables for each of CUs,TUs for each DWO file. */
11265
b0b6a987 11266static htab_up
298e9637 11267allocate_dwo_unit_table ()
3019eac3
DE
11268{
11269 /* Start out with a pretty small number.
11270 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11271 return htab_up (htab_create_alloc (3,
11272 hash_dwo_unit,
11273 eq_dwo_unit,
11274 NULL, xcalloc, xfree));
3019eac3
DE
11275}
11276
19c3d4c9 11277/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11278
11279static void
19c3d4c9
DE
11280create_dwo_cu_reader (const struct die_reader_specs *reader,
11281 const gdb_byte *info_ptr,
11282 struct die_info *comp_unit_die,
c0ab21c2
TT
11283 struct dwo_file *dwo_file,
11284 struct dwo_unit *dwo_unit)
3019eac3
DE
11285{
11286 struct dwarf2_cu *cu = reader->cu;
9c541725 11287 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11288 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11289
a084a2a6
AT
11290 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11291 if (!signature.has_value ())
3019eac3 11292 {
b98664d3 11293 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11294 " its dwo_id [in module %s]"),
9d8780f0 11295 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11296 return;
11297 }
11298
3019eac3 11299 dwo_unit->dwo_file = dwo_file;
a084a2a6 11300 dwo_unit->signature = *signature;
8a0459fd 11301 dwo_unit->section = section;
9c541725 11302 dwo_unit->sect_off = sect_off;
3019eac3
DE
11303 dwo_unit->length = cu->per_cu->length;
11304
b4f54984 11305 if (dwarf_read_debug)
9d8780f0
SM
11306 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11307 sect_offset_str (sect_off),
9c541725 11308 hex_string (dwo_unit->signature));
3019eac3
DE
11309}
11310
33c5cd75 11311/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11312 Note: This function processes DWO files only, not DWP files. */
3019eac3 11313
33c5cd75 11314static void
ed2dc618 11315create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11316 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11317 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11318{
11319 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11320 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11321 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11322
96b79293 11323 section.read (objfile);
33c5cd75 11324 info_ptr = section.buffer;
3019eac3
DE
11325
11326 if (info_ptr == NULL)
33c5cd75 11327 return;
3019eac3 11328
b4f54984 11329 if (dwarf_read_debug)
19c3d4c9
DE
11330 {
11331 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11332 section.get_name (),
11333 section.get_file_name ());
19c3d4c9 11334 }
3019eac3 11335
33c5cd75 11336 end_ptr = info_ptr + section.size;
3019eac3
DE
11337 while (info_ptr < end_ptr)
11338 {
11339 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11340 struct dwo_unit read_unit {};
33c5cd75
DB
11341 struct dwo_unit *dwo_unit;
11342 void **slot;
11343 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11344
11345 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11346 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 11347 per_cu.per_bfd = per_bfd;
3019eac3 11348 per_cu.is_debug_types = 0;
33c5cd75
DB
11349 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11350 per_cu.section = &section;
11351
ab432490 11352 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
c0ab21c2
TT
11353 if (!reader.dummy_p)
11354 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11355 &dwo_file, &read_unit);
33c5cd75
DB
11356 info_ptr += per_cu.length;
11357
11358 // If the unit could not be parsed, skip it.
c0ab21c2 11359 if (read_unit.dwo_file == NULL)
33c5cd75 11360 continue;
3019eac3 11361
33c5cd75 11362 if (cus_htab == NULL)
298e9637 11363 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11364
1859c670 11365 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11366 struct dwo_unit);
c0ab21c2 11367 *dwo_unit = read_unit;
b0b6a987 11368 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11369 gdb_assert (slot != NULL);
11370 if (*slot != NULL)
19c3d4c9 11371 {
33c5cd75
DB
11372 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11373 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11374
b98664d3 11375 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11376 " the entry at offset %s, signature %s"),
11377 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11378 hex_string (dwo_unit->signature));
19c3d4c9 11379 }
33c5cd75 11380 *slot = (void *)dwo_unit;
3019eac3 11381 }
3019eac3
DE
11382}
11383
80626a55
DE
11384/* DWP file .debug_{cu,tu}_index section format:
11385 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11386
d2415c6c
DE
11387 DWP Version 1:
11388
80626a55
DE
11389 Both index sections have the same format, and serve to map a 64-bit
11390 signature to a set of section numbers. Each section begins with a header,
11391 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11392 indexes, and a pool of 32-bit section numbers. The index sections will be
11393 aligned at 8-byte boundaries in the file.
11394
d2415c6c
DE
11395 The index section header consists of:
11396
11397 V, 32 bit version number
11398 -, 32 bits unused
11399 N, 32 bit number of compilation units or type units in the index
11400 M, 32 bit number of slots in the hash table
80626a55 11401
d2415c6c 11402 Numbers are recorded using the byte order of the application binary.
80626a55 11403
d2415c6c
DE
11404 The hash table begins at offset 16 in the section, and consists of an array
11405 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11406 order of the application binary). Unused slots in the hash table are 0.
11407 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11408
d2415c6c
DE
11409 The parallel table begins immediately after the hash table
11410 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11411 array of 32-bit indexes (using the byte order of the application binary),
11412 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11413 table contains a 32-bit index into the pool of section numbers. For unused
11414 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11415
73869dc2
DE
11416 The pool of section numbers begins immediately following the hash table
11417 (at offset 16 + 12 * M from the beginning of the section). The pool of
11418 section numbers consists of an array of 32-bit words (using the byte order
11419 of the application binary). Each item in the array is indexed starting
11420 from 0. The hash table entry provides the index of the first section
11421 number in the set. Additional section numbers in the set follow, and the
11422 set is terminated by a 0 entry (section number 0 is not used in ELF).
11423
11424 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11425 section must be the first entry in the set, and the .debug_abbrev.dwo must
11426 be the second entry. Other members of the set may follow in any order.
11427
11428 ---
11429
11430 DWP Version 2:
11431
11432 DWP Version 2 combines all the .debug_info, etc. sections into one,
11433 and the entries in the index tables are now offsets into these sections.
11434 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11435 section.
11436
11437 Index Section Contents:
11438 Header
11439 Hash Table of Signatures dwp_hash_table.hash_table
11440 Parallel Table of Indices dwp_hash_table.unit_table
11441 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11442 Table of Section Sizes dwp_hash_table.v2.sizes
11443
11444 The index section header consists of:
11445
11446 V, 32 bit version number
11447 L, 32 bit number of columns in the table of section offsets
11448 N, 32 bit number of compilation units or type units in the index
11449 M, 32 bit number of slots in the hash table
11450
11451 Numbers are recorded using the byte order of the application binary.
11452
11453 The hash table has the same format as version 1.
11454 The parallel table of indices has the same format as version 1,
11455 except that the entries are origin-1 indices into the table of sections
11456 offsets and the table of section sizes.
11457
11458 The table of offsets begins immediately following the parallel table
11459 (at offset 16 + 12 * M from the beginning of the section). The table is
11460 a two-dimensional array of 32-bit words (using the byte order of the
11461 application binary), with L columns and N+1 rows, in row-major order.
11462 Each row in the array is indexed starting from 0. The first row provides
11463 a key to the remaining rows: each column in this row provides an identifier
11464 for a debug section, and the offsets in the same column of subsequent rows
11465 refer to that section. The section identifiers are:
11466
11467 DW_SECT_INFO 1 .debug_info.dwo
11468 DW_SECT_TYPES 2 .debug_types.dwo
11469 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11470 DW_SECT_LINE 4 .debug_line.dwo
11471 DW_SECT_LOC 5 .debug_loc.dwo
11472 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11473 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11474 DW_SECT_MACRO 8 .debug_macro.dwo
11475
11476 The offsets provided by the CU and TU index sections are the base offsets
11477 for the contributions made by each CU or TU to the corresponding section
11478 in the package file. Each CU and TU header contains an abbrev_offset
11479 field, used to find the abbreviations table for that CU or TU within the
11480 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11481 be interpreted as relative to the base offset given in the index section.
11482 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11483 should be interpreted as relative to the base offset for .debug_line.dwo,
11484 and offsets into other debug sections obtained from DWARF attributes should
11485 also be interpreted as relative to the corresponding base offset.
11486
11487 The table of sizes begins immediately following the table of offsets.
11488 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11489 with L columns and N rows, in row-major order. Each row in the array is
11490 indexed starting from 1 (row 0 is shared by the two tables).
11491
11492 ---
11493
11494 Hash table lookup is handled the same in version 1 and 2:
11495
11496 We assume that N and M will not exceed 2^32 - 1.
11497 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11498
d2415c6c
DE
11499 Given a 64-bit compilation unit signature or a type signature S, an entry
11500 in the hash table is located as follows:
80626a55 11501
d2415c6c
DE
11502 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11503 the low-order k bits all set to 1.
80626a55 11504
d2415c6c 11505 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11506
d2415c6c
DE
11507 3) If the hash table entry at index H matches the signature, use that
11508 entry. If the hash table entry at index H is unused (all zeroes),
11509 terminate the search: the signature is not present in the table.
80626a55 11510
d2415c6c 11511 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11512
d2415c6c 11513 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11514 to stop at an unused slot or find the match. */
80626a55
DE
11515
11516/* Create a hash table to map DWO IDs to their CU/TU entry in
11517 .debug_{info,types}.dwo in DWP_FILE.
11518 Returns NULL if there isn't one.
11519 Note: This function processes DWP files only, not DWO files. */
11520
11521static struct dwp_hash_table *
ed2dc618
SM
11522create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11523 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11524{
11525 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11526 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11527 const gdb_byte *index_ptr, *index_end;
80626a55 11528 struct dwarf2_section_info *index;
73869dc2 11529 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11530 struct dwp_hash_table *htab;
11531
11532 if (is_debug_types)
11533 index = &dwp_file->sections.tu_index;
11534 else
11535 index = &dwp_file->sections.cu_index;
11536
96b79293 11537 if (index->empty ())
80626a55 11538 return NULL;
96b79293 11539 index->read (objfile);
80626a55
DE
11540
11541 index_ptr = index->buffer;
11542 index_end = index_ptr + index->size;
11543
11544 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11545 index_ptr += 4;
11546 if (version == 2)
11547 nr_columns = read_4_bytes (dbfd, index_ptr);
11548 else
11549 nr_columns = 0;
11550 index_ptr += 4;
80626a55
DE
11551 nr_units = read_4_bytes (dbfd, index_ptr);
11552 index_ptr += 4;
11553 nr_slots = read_4_bytes (dbfd, index_ptr);
11554 index_ptr += 4;
11555
73869dc2 11556 if (version != 1 && version != 2)
80626a55 11557 {
21aa081e 11558 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11559 " [in module %s]"),
21aa081e 11560 pulongest (version), dwp_file->name);
80626a55
DE
11561 }
11562 if (nr_slots != (nr_slots & -nr_slots))
11563 {
21aa081e 11564 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11565 " is not power of 2 [in module %s]"),
21aa081e 11566 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11567 }
11568
5989a64e 11569 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11570 htab->version = version;
11571 htab->nr_columns = nr_columns;
80626a55
DE
11572 htab->nr_units = nr_units;
11573 htab->nr_slots = nr_slots;
11574 htab->hash_table = index_ptr;
11575 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11576
11577 /* Exit early if the table is empty. */
11578 if (nr_slots == 0 || nr_units == 0
11579 || (version == 2 && nr_columns == 0))
11580 {
11581 /* All must be zero. */
11582 if (nr_slots != 0 || nr_units != 0
11583 || (version == 2 && nr_columns != 0))
11584 {
b98664d3 11585 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11586 " all zero [in modules %s]"),
11587 dwp_file->name);
11588 }
11589 return htab;
11590 }
11591
11592 if (version == 1)
11593 {
11594 htab->section_pool.v1.indices =
11595 htab->unit_table + sizeof (uint32_t) * nr_slots;
11596 /* It's harder to decide whether the section is too small in v1.
11597 V1 is deprecated anyway so we punt. */
11598 }
11599 else
11600 {
11601 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11602 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11603 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11604 /* Reverse map for error checking. */
11605 int ids_seen[DW_SECT_MAX + 1];
11606 int i;
11607
11608 if (nr_columns < 2)
11609 {
11610 error (_("Dwarf Error: bad DWP hash table, too few columns"
11611 " in section table [in module %s]"),
11612 dwp_file->name);
11613 }
11614 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11615 {
11616 error (_("Dwarf Error: bad DWP hash table, too many columns"
11617 " in section table [in module %s]"),
11618 dwp_file->name);
11619 }
04fd5eed
GB
11620 memset (ids, 255, sizeof_ids);
11621 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11622 for (i = 0; i < nr_columns; ++i)
11623 {
11624 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11625
11626 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11627 {
11628 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11629 " in section table [in module %s]"),
11630 id, dwp_file->name);
11631 }
11632 if (ids_seen[id] != -1)
11633 {
11634 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11635 " id %d in section table [in module %s]"),
11636 id, dwp_file->name);
11637 }
11638 ids_seen[id] = i;
11639 ids[i] = id;
11640 }
11641 /* Must have exactly one info or types section. */
11642 if (((ids_seen[DW_SECT_INFO] != -1)
11643 + (ids_seen[DW_SECT_TYPES] != -1))
11644 != 1)
11645 {
11646 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11647 " DWO info/types section [in module %s]"),
11648 dwp_file->name);
11649 }
11650 /* Must have an abbrev section. */
11651 if (ids_seen[DW_SECT_ABBREV] == -1)
11652 {
11653 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11654 " section [in module %s]"),
11655 dwp_file->name);
11656 }
11657 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11658 htab->section_pool.v2.sizes =
11659 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11660 * nr_units * nr_columns);
11661 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11662 * nr_units * nr_columns))
11663 > index_end)
11664 {
11665 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11666 " [in module %s]"),
11667 dwp_file->name);
11668 }
11669 }
80626a55
DE
11670
11671 return htab;
11672}
11673
11674/* Update SECTIONS with the data from SECTP.
11675
11676 This function is like the other "locate" section routines that are
11677 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11678 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11679
11680 The result is non-zero for success, or zero if an error was found. */
11681
11682static int
73869dc2
DE
11683locate_v1_virtual_dwo_sections (asection *sectp,
11684 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11685{
11686 const struct dwop_section_names *names = &dwop_section_names;
11687
11688 if (section_is_p (sectp->name, &names->abbrev_dwo))
11689 {
11690 /* There can be only one. */
049412e3 11691 if (sections->abbrev.s.section != NULL)
80626a55 11692 return 0;
049412e3 11693 sections->abbrev.s.section = sectp;
fd361982 11694 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11695 }
11696 else if (section_is_p (sectp->name, &names->info_dwo)
11697 || section_is_p (sectp->name, &names->types_dwo))
11698 {
11699 /* There can be only one. */
049412e3 11700 if (sections->info_or_types.s.section != NULL)
80626a55 11701 return 0;
049412e3 11702 sections->info_or_types.s.section = sectp;
fd361982 11703 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11704 }
11705 else if (section_is_p (sectp->name, &names->line_dwo))
11706 {
11707 /* There can be only one. */
049412e3 11708 if (sections->line.s.section != NULL)
80626a55 11709 return 0;
049412e3 11710 sections->line.s.section = sectp;
fd361982 11711 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11712 }
11713 else if (section_is_p (sectp->name, &names->loc_dwo))
11714 {
11715 /* There can be only one. */
049412e3 11716 if (sections->loc.s.section != NULL)
80626a55 11717 return 0;
049412e3 11718 sections->loc.s.section = sectp;
fd361982 11719 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11720 }
11721 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11722 {
11723 /* There can be only one. */
049412e3 11724 if (sections->macinfo.s.section != NULL)
80626a55 11725 return 0;
049412e3 11726 sections->macinfo.s.section = sectp;
fd361982 11727 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11728 }
11729 else if (section_is_p (sectp->name, &names->macro_dwo))
11730 {
11731 /* There can be only one. */
049412e3 11732 if (sections->macro.s.section != NULL)
80626a55 11733 return 0;
049412e3 11734 sections->macro.s.section = sectp;
fd361982 11735 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11736 }
11737 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11738 {
11739 /* There can be only one. */
049412e3 11740 if (sections->str_offsets.s.section != NULL)
80626a55 11741 return 0;
049412e3 11742 sections->str_offsets.s.section = sectp;
fd361982 11743 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11744 }
11745 else
11746 {
11747 /* No other kind of section is valid. */
11748 return 0;
11749 }
11750
11751 return 1;
11752}
11753
73869dc2
DE
11754/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11755 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11756 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11757 This is for DWP version 1 files. */
80626a55
DE
11758
11759static struct dwo_unit *
ed2dc618
SM
11760create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11761 struct dwp_file *dwp_file,
73869dc2
DE
11762 uint32_t unit_index,
11763 const char *comp_dir,
11764 ULONGEST signature, int is_debug_types)
80626a55 11765{
73869dc2
DE
11766 const struct dwp_hash_table *dwp_htab =
11767 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11768 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11769 const char *kind = is_debug_types ? "TU" : "CU";
11770 struct dwo_file *dwo_file;
11771 struct dwo_unit *dwo_unit;
73869dc2 11772 struct virtual_v1_dwo_sections sections;
80626a55 11773 void **dwo_file_slot;
80626a55
DE
11774 int i;
11775
73869dc2
DE
11776 gdb_assert (dwp_file->version == 1);
11777
b4f54984 11778 if (dwarf_read_debug)
80626a55 11779 {
73869dc2 11780 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11781 kind,
73869dc2 11782 pulongest (unit_index), hex_string (signature),
80626a55
DE
11783 dwp_file->name);
11784 }
11785
19ac8c2e 11786 /* Fetch the sections of this DWO unit.
80626a55
DE
11787 Put a limit on the number of sections we look for so that bad data
11788 doesn't cause us to loop forever. */
11789
73869dc2 11790#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11791 (1 /* .debug_info or .debug_types */ \
11792 + 1 /* .debug_abbrev */ \
11793 + 1 /* .debug_line */ \
11794 + 1 /* .debug_loc */ \
11795 + 1 /* .debug_str_offsets */ \
19ac8c2e 11796 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11797 + 1 /* trailing zero */)
11798
11799 memset (&sections, 0, sizeof (sections));
80626a55 11800
73869dc2 11801 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11802 {
11803 asection *sectp;
11804 uint32_t section_nr =
11805 read_4_bytes (dbfd,
73869dc2
DE
11806 dwp_htab->section_pool.v1.indices
11807 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11808
11809 if (section_nr == 0)
11810 break;
11811 if (section_nr >= dwp_file->num_sections)
11812 {
11813 error (_("Dwarf Error: bad DWP hash table, section number too large"
11814 " [in module %s]"),
11815 dwp_file->name);
11816 }
11817
11818 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11819 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11820 {
11821 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11822 " [in module %s]"),
11823 dwp_file->name);
11824 }
11825 }
11826
11827 if (i < 2
96b79293
TT
11828 || sections.info_or_types.empty ()
11829 || sections.abbrev.empty ())
80626a55
DE
11830 {
11831 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11832 " [in module %s]"),
11833 dwp_file->name);
11834 }
73869dc2 11835 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11836 {
11837 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11838 " [in module %s]"),
11839 dwp_file->name);
11840 }
11841
11842 /* It's easier for the rest of the code if we fake a struct dwo_file and
11843 have dwo_unit "live" in that. At least for now.
11844
11845 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11846 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11847 file, we can combine them back into a virtual DWO file to save space
11848 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11849 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11850
791afaa2
TT
11851 std::string virtual_dwo_name =
11852 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11853 sections.abbrev.get_id (),
11854 sections.line.get_id (),
11855 sections.loc.get_id (),
11856 sections.str_offsets.get_id ());
80626a55 11857 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11858 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11859 virtual_dwo_name.c_str (),
11860 comp_dir);
80626a55
DE
11861 /* Create one if necessary. */
11862 if (*dwo_file_slot == NULL)
11863 {
b4f54984 11864 if (dwarf_read_debug)
80626a55
DE
11865 {
11866 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11867 virtual_dwo_name.c_str ());
80626a55 11868 }
51ac9db5 11869 dwo_file = new struct dwo_file;
45940949 11870 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11871 dwo_file->comp_dir = comp_dir;
80626a55
DE
11872 dwo_file->sections.abbrev = sections.abbrev;
11873 dwo_file->sections.line = sections.line;
11874 dwo_file->sections.loc = sections.loc;
11875 dwo_file->sections.macinfo = sections.macinfo;
11876 dwo_file->sections.macro = sections.macro;
11877 dwo_file->sections.str_offsets = sections.str_offsets;
11878 /* The "str" section is global to the entire DWP file. */
11879 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11880 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11881 there's no need to record it in dwo_file.
11882 Also, we can't simply record type sections in dwo_file because
11883 we record a pointer into the vector in dwo_unit. As we collect more
11884 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11885 for it, invalidating all copies of pointers into the previous
11886 contents. */
80626a55
DE
11887 *dwo_file_slot = dwo_file;
11888 }
11889 else
11890 {
b4f54984 11891 if (dwarf_read_debug)
80626a55
DE
11892 {
11893 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11894 virtual_dwo_name.c_str ());
80626a55 11895 }
9a3c8263 11896 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11897 }
80626a55 11898
5989a64e 11899 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11900 dwo_unit->dwo_file = dwo_file;
11901 dwo_unit->signature = signature;
8d749320 11902 dwo_unit->section =
5989a64e 11903 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11904 *dwo_unit->section = sections.info_or_types;
57d63ce2 11905 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11906
11907 return dwo_unit;
11908}
11909
73869dc2
DE
11910/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11911 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11912 piece within that section used by a TU/CU, return a virtual section
11913 of just that piece. */
11914
11915static struct dwarf2_section_info
ed2dc618
SM
11916create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11917 struct dwarf2_section_info *section,
73869dc2
DE
11918 bfd_size_type offset, bfd_size_type size)
11919{
11920 struct dwarf2_section_info result;
11921 asection *sectp;
11922
11923 gdb_assert (section != NULL);
11924 gdb_assert (!section->is_virtual);
11925
11926 memset (&result, 0, sizeof (result));
11927 result.s.containing_section = section;
dc4ccb6f 11928 result.is_virtual = true;
73869dc2
DE
11929
11930 if (size == 0)
11931 return result;
11932
96b79293 11933 sectp = section->get_bfd_section ();
73869dc2
DE
11934
11935 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11936 bounds of the real section. This is a pretty-rare event, so just
11937 flag an error (easier) instead of a warning and trying to cope. */
11938 if (sectp == NULL
fd361982 11939 || offset + size > bfd_section_size (sectp))
73869dc2 11940 {
73869dc2
DE
11941 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11942 " in section %s [in module %s]"),
fd361982 11943 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11944 objfile_name (dwarf2_per_objfile->objfile));
11945 }
11946
11947 result.virtual_offset = offset;
11948 result.size = size;
11949 return result;
11950}
11951
11952/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11953 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11954 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11955 This is for DWP version 2 files. */
11956
11957static struct dwo_unit *
ed2dc618
SM
11958create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11959 struct dwp_file *dwp_file,
73869dc2
DE
11960 uint32_t unit_index,
11961 const char *comp_dir,
11962 ULONGEST signature, int is_debug_types)
11963{
73869dc2
DE
11964 const struct dwp_hash_table *dwp_htab =
11965 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11966 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11967 const char *kind = is_debug_types ? "TU" : "CU";
11968 struct dwo_file *dwo_file;
11969 struct dwo_unit *dwo_unit;
11970 struct virtual_v2_dwo_sections sections;
11971 void **dwo_file_slot;
73869dc2
DE
11972 int i;
11973
11974 gdb_assert (dwp_file->version == 2);
11975
b4f54984 11976 if (dwarf_read_debug)
73869dc2
DE
11977 {
11978 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11979 kind,
11980 pulongest (unit_index), hex_string (signature),
11981 dwp_file->name);
11982 }
11983
11984 /* Fetch the section offsets of this DWO unit. */
11985
11986 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11987
11988 for (i = 0; i < dwp_htab->nr_columns; ++i)
11989 {
11990 uint32_t offset = read_4_bytes (dbfd,
11991 dwp_htab->section_pool.v2.offsets
11992 + (((unit_index - 1) * dwp_htab->nr_columns
11993 + i)
11994 * sizeof (uint32_t)));
11995 uint32_t size = read_4_bytes (dbfd,
11996 dwp_htab->section_pool.v2.sizes
11997 + (((unit_index - 1) * dwp_htab->nr_columns
11998 + i)
11999 * sizeof (uint32_t)));
12000
12001 switch (dwp_htab->section_pool.v2.section_ids[i])
12002 {
12003 case DW_SECT_INFO:
12004 case DW_SECT_TYPES:
12005 sections.info_or_types_offset = offset;
12006 sections.info_or_types_size = size;
12007 break;
12008 case DW_SECT_ABBREV:
12009 sections.abbrev_offset = offset;
12010 sections.abbrev_size = size;
12011 break;
12012 case DW_SECT_LINE:
12013 sections.line_offset = offset;
12014 sections.line_size = size;
12015 break;
12016 case DW_SECT_LOC:
12017 sections.loc_offset = offset;
12018 sections.loc_size = size;
12019 break;
12020 case DW_SECT_STR_OFFSETS:
12021 sections.str_offsets_offset = offset;
12022 sections.str_offsets_size = size;
12023 break;
12024 case DW_SECT_MACINFO:
12025 sections.macinfo_offset = offset;
12026 sections.macinfo_size = size;
12027 break;
12028 case DW_SECT_MACRO:
12029 sections.macro_offset = offset;
12030 sections.macro_size = size;
12031 break;
12032 }
12033 }
12034
12035 /* It's easier for the rest of the code if we fake a struct dwo_file and
12036 have dwo_unit "live" in that. At least for now.
12037
12038 The DWP file can be made up of a random collection of CUs and TUs.
12039 However, for each CU + set of TUs that came from the same original DWO
12040 file, we can combine them back into a virtual DWO file to save space
12041 (fewer struct dwo_file objects to allocate). Remember that for really
12042 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12043
791afaa2
TT
12044 std::string virtual_dwo_name =
12045 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12046 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12047 (long) (sections.line_size ? sections.line_offset : 0),
12048 (long) (sections.loc_size ? sections.loc_offset : 0),
12049 (long) (sections.str_offsets_size
12050 ? sections.str_offsets_offset : 0));
73869dc2 12051 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12052 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12053 virtual_dwo_name.c_str (),
12054 comp_dir);
73869dc2
DE
12055 /* Create one if necessary. */
12056 if (*dwo_file_slot == NULL)
12057 {
b4f54984 12058 if (dwarf_read_debug)
73869dc2
DE
12059 {
12060 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12061 virtual_dwo_name.c_str ());
73869dc2 12062 }
51ac9db5 12063 dwo_file = new struct dwo_file;
45940949 12064 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12065 dwo_file->comp_dir = comp_dir;
12066 dwo_file->sections.abbrev =
ed2dc618 12067 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12068 sections.abbrev_offset, sections.abbrev_size);
12069 dwo_file->sections.line =
ed2dc618 12070 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12071 sections.line_offset, sections.line_size);
12072 dwo_file->sections.loc =
ed2dc618 12073 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12074 sections.loc_offset, sections.loc_size);
12075 dwo_file->sections.macinfo =
ed2dc618 12076 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12077 sections.macinfo_offset, sections.macinfo_size);
12078 dwo_file->sections.macro =
ed2dc618 12079 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12080 sections.macro_offset, sections.macro_size);
12081 dwo_file->sections.str_offsets =
ed2dc618
SM
12082 create_dwp_v2_section (dwarf2_per_objfile,
12083 &dwp_file->sections.str_offsets,
73869dc2
DE
12084 sections.str_offsets_offset,
12085 sections.str_offsets_size);
12086 /* The "str" section is global to the entire DWP file. */
12087 dwo_file->sections.str = dwp_file->sections.str;
12088 /* The info or types section is assigned below to dwo_unit,
12089 there's no need to record it in dwo_file.
12090 Also, we can't simply record type sections in dwo_file because
12091 we record a pointer into the vector in dwo_unit. As we collect more
12092 types we'll grow the vector and eventually have to reallocate space
12093 for it, invalidating all copies of pointers into the previous
12094 contents. */
12095 *dwo_file_slot = dwo_file;
12096 }
12097 else
12098 {
b4f54984 12099 if (dwarf_read_debug)
73869dc2
DE
12100 {
12101 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12102 virtual_dwo_name.c_str ());
73869dc2 12103 }
9a3c8263 12104 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12105 }
73869dc2 12106
5989a64e 12107 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12108 dwo_unit->dwo_file = dwo_file;
12109 dwo_unit->signature = signature;
8d749320 12110 dwo_unit->section =
5989a64e 12111 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12112 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12113 is_debug_types
73869dc2
DE
12114 ? &dwp_file->sections.types
12115 : &dwp_file->sections.info,
12116 sections.info_or_types_offset,
12117 sections.info_or_types_size);
12118 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12119
12120 return dwo_unit;
12121}
12122
57d63ce2
DE
12123/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12124 Returns NULL if the signature isn't found. */
80626a55
DE
12125
12126static struct dwo_unit *
ed2dc618
SM
12127lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12128 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12129 ULONGEST signature, int is_debug_types)
80626a55 12130{
57d63ce2
DE
12131 const struct dwp_hash_table *dwp_htab =
12132 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12133 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12134 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12135 uint32_t hash = signature & mask;
12136 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12137 unsigned int i;
12138 void **slot;
870f88f7 12139 struct dwo_unit find_dwo_cu;
80626a55
DE
12140
12141 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12142 find_dwo_cu.signature = signature;
19ac8c2e 12143 slot = htab_find_slot (is_debug_types
48b490f2
TT
12144 ? dwp_file->loaded_tus.get ()
12145 : dwp_file->loaded_cus.get (),
19ac8c2e 12146 &find_dwo_cu, INSERT);
80626a55
DE
12147
12148 if (*slot != NULL)
9a3c8263 12149 return (struct dwo_unit *) *slot;
80626a55
DE
12150
12151 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12152 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12153 {
12154 ULONGEST signature_in_table;
12155
12156 signature_in_table =
57d63ce2 12157 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12158 if (signature_in_table == signature)
12159 {
57d63ce2
DE
12160 uint32_t unit_index =
12161 read_4_bytes (dbfd,
12162 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12163
73869dc2
DE
12164 if (dwp_file->version == 1)
12165 {
ed2dc618
SM
12166 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12167 dwp_file, unit_index,
73869dc2
DE
12168 comp_dir, signature,
12169 is_debug_types);
12170 }
12171 else
12172 {
ed2dc618
SM
12173 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12174 dwp_file, unit_index,
73869dc2
DE
12175 comp_dir, signature,
12176 is_debug_types);
12177 }
9a3c8263 12178 return (struct dwo_unit *) *slot;
80626a55
DE
12179 }
12180 if (signature_in_table == 0)
12181 return NULL;
12182 hash = (hash + hash2) & mask;
12183 }
12184
12185 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12186 " [in module %s]"),
12187 dwp_file->name);
12188}
12189
ab5088bf 12190/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12191 Open the file specified by FILE_NAME and hand it off to BFD for
12192 preliminary analysis. Return a newly initialized bfd *, which
12193 includes a canonicalized copy of FILE_NAME.
80626a55 12194 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12195 SEARCH_CWD is true if the current directory is to be searched.
12196 It will be searched before debug-file-directory.
13aaf454
DE
12197 If successful, the file is added to the bfd include table of the
12198 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12199 If unable to find/open the file, return NULL.
3019eac3
DE
12200 NOTE: This function is derived from symfile_bfd_open. */
12201
192b62ce 12202static gdb_bfd_ref_ptr
ed2dc618
SM
12203try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12204 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12205{
24b9144d 12206 int desc;
9c02c129
DE
12207 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12208 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12209 to debug_file_directory. */
e0cc99a6 12210 const char *search_path;
9c02c129
DE
12211 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12212
e0cc99a6 12213 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12214 if (search_cwd)
12215 {
12216 if (*debug_file_directory != '\0')
e0cc99a6
TT
12217 {
12218 search_path_holder.reset (concat (".", dirname_separator_string,
12219 debug_file_directory,
12220 (char *) NULL));
12221 search_path = search_path_holder.get ();
12222 }
6ac97d4c 12223 else
e0cc99a6 12224 search_path = ".";
6ac97d4c 12225 }
9c02c129 12226 else
e0cc99a6 12227 search_path = debug_file_directory;
3019eac3 12228
24b9144d 12229 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12230 if (is_dwp)
12231 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12232
12233 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12234 desc = openp (search_path, flags, file_name,
3019eac3
DE
12235 O_RDONLY | O_BINARY, &absolute_name);
12236 if (desc < 0)
12237 return NULL;
12238
e0cc99a6
TT
12239 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12240 gnutarget, desc));
9c02c129
DE
12241 if (sym_bfd == NULL)
12242 return NULL;
192b62ce 12243 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12244
192b62ce
TT
12245 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12246 return NULL;
3019eac3 12247
13aaf454
DE
12248 /* Success. Record the bfd as having been included by the objfile's bfd.
12249 This is important because things like demangled_names_hash lives in the
12250 objfile's per_bfd space and may have references to things like symbol
12251 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12252 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12253
3019eac3
DE
12254 return sym_bfd;
12255}
12256
ab5088bf 12257/* Try to open DWO file FILE_NAME.
3019eac3
DE
12258 COMP_DIR is the DW_AT_comp_dir attribute.
12259 The result is the bfd handle of the file.
12260 If there is a problem finding or opening the file, return NULL.
12261 Upon success, the canonicalized path of the file is stored in the bfd,
12262 same as symfile_bfd_open. */
12263
192b62ce 12264static gdb_bfd_ref_ptr
ed2dc618
SM
12265open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12266 const char *file_name, const char *comp_dir)
3019eac3 12267{
80626a55 12268 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12269 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12270 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12271
12272 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12273
12274 if (comp_dir != NULL)
12275 {
43816ebc
TT
12276 gdb::unique_xmalloc_ptr<char> path_to_try
12277 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12278
12279 /* NOTE: If comp_dir is a relative path, this will also try the
12280 search path, which seems useful. */
ed2dc618 12281 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12282 path_to_try.get (),
ed2dc618 12283 0 /*is_dwp*/,
192b62ce 12284 1 /*search_cwd*/));
3019eac3
DE
12285 if (abfd != NULL)
12286 return abfd;
12287 }
12288
12289 /* That didn't work, try debug-file-directory, which, despite its name,
12290 is a list of paths. */
12291
12292 if (*debug_file_directory == '\0')
12293 return NULL;
12294
ed2dc618
SM
12295 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12296 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12297}
12298
80626a55
DE
12299/* This function is mapped across the sections and remembers the offset and
12300 size of each of the DWO debugging sections we are interested in. */
12301
12302static void
12303dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12304{
9a3c8263 12305 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12306 const struct dwop_section_names *names = &dwop_section_names;
12307
12308 if (section_is_p (sectp->name, &names->abbrev_dwo))
12309 {
049412e3 12310 dwo_sections->abbrev.s.section = sectp;
fd361982 12311 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12312 }
12313 else if (section_is_p (sectp->name, &names->info_dwo))
12314 {
049412e3 12315 dwo_sections->info.s.section = sectp;
fd361982 12316 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12317 }
12318 else if (section_is_p (sectp->name, &names->line_dwo))
12319 {
049412e3 12320 dwo_sections->line.s.section = sectp;
fd361982 12321 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12322 }
12323 else if (section_is_p (sectp->name, &names->loc_dwo))
12324 {
049412e3 12325 dwo_sections->loc.s.section = sectp;
fd361982 12326 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12327 }
41144253 12328 else if (section_is_p (sectp->name, &names->loclists_dwo))
12329 {
12330 dwo_sections->loclists.s.section = sectp;
12331 dwo_sections->loclists.size = bfd_section_size (sectp);
12332 }
80626a55
DE
12333 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12334 {
049412e3 12335 dwo_sections->macinfo.s.section = sectp;
fd361982 12336 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12337 }
12338 else if (section_is_p (sectp->name, &names->macro_dwo))
12339 {
049412e3 12340 dwo_sections->macro.s.section = sectp;
fd361982 12341 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12342 }
12343 else if (section_is_p (sectp->name, &names->str_dwo))
12344 {
049412e3 12345 dwo_sections->str.s.section = sectp;
fd361982 12346 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12347 }
12348 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12349 {
049412e3 12350 dwo_sections->str_offsets.s.section = sectp;
fd361982 12351 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12352 }
12353 else if (section_is_p (sectp->name, &names->types_dwo))
12354 {
12355 struct dwarf2_section_info type_section;
12356
12357 memset (&type_section, 0, sizeof (type_section));
049412e3 12358 type_section.s.section = sectp;
fd361982 12359 type_section.size = bfd_section_size (sectp);
fd5866f6 12360 dwo_sections->types.push_back (type_section);
80626a55
DE
12361 }
12362}
12363
ab5088bf 12364/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12365 by PER_CU. This is for the non-DWP case.
80626a55 12366 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12367
12368static struct dwo_file *
4ab09049
SM
12369open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12370 const char *comp_dir)
3019eac3 12371{
4ab09049 12372 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12373
fb1eb2f9 12374 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12375 if (dbfd == NULL)
12376 {
b4f54984 12377 if (dwarf_read_debug)
80626a55
DE
12378 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12379 return NULL;
12380 }
263db9a1 12381
51ac9db5 12382 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12383 dwo_file->dwo_name = dwo_name;
12384 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12385 dwo_file->dbfd = std::move (dbfd);
3019eac3 12386
fb1eb2f9 12387 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12388 &dwo_file->sections);
3019eac3 12389
4ab09049 12390 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
18a8505e 12391 dwo_file->sections.info, dwo_file->cus);
3019eac3 12392
263db9a1 12393 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12394 dwo_file->sections.types, dwo_file->tus);
3019eac3 12395
b4f54984 12396 if (dwarf_read_debug)
80626a55
DE
12397 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12398
263db9a1 12399 return dwo_file.release ();
3019eac3
DE
12400}
12401
80626a55 12402/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12403 size of each of the DWP debugging sections common to version 1 and 2 that
12404 we are interested in. */
3019eac3 12405
80626a55 12406static void
73869dc2
DE
12407dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12408 void *dwp_file_ptr)
3019eac3 12409{
9a3c8263 12410 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12411 const struct dwop_section_names *names = &dwop_section_names;
12412 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12413
80626a55 12414 /* Record the ELF section number for later lookup: this is what the
73869dc2 12415 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12416 gdb_assert (elf_section_nr < dwp_file->num_sections);
12417 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12418
80626a55
DE
12419 /* Look for specific sections that we need. */
12420 if (section_is_p (sectp->name, &names->str_dwo))
12421 {
049412e3 12422 dwp_file->sections.str.s.section = sectp;
fd361982 12423 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12424 }
12425 else if (section_is_p (sectp->name, &names->cu_index))
12426 {
049412e3 12427 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12428 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12429 }
12430 else if (section_is_p (sectp->name, &names->tu_index))
12431 {
049412e3 12432 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12433 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12434 }
12435}
3019eac3 12436
73869dc2
DE
12437/* This function is mapped across the sections and remembers the offset and
12438 size of each of the DWP version 2 debugging sections that we are interested
12439 in. This is split into a separate function because we don't know if we
12440 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12441
12442static void
12443dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12444{
9a3c8263 12445 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12446 const struct dwop_section_names *names = &dwop_section_names;
12447 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12448
12449 /* Record the ELF section number for later lookup: this is what the
12450 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12451 gdb_assert (elf_section_nr < dwp_file->num_sections);
12452 dwp_file->elf_sections[elf_section_nr] = sectp;
12453
12454 /* Look for specific sections that we need. */
12455 if (section_is_p (sectp->name, &names->abbrev_dwo))
12456 {
049412e3 12457 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12458 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12459 }
12460 else if (section_is_p (sectp->name, &names->info_dwo))
12461 {
049412e3 12462 dwp_file->sections.info.s.section = sectp;
fd361982 12463 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12464 }
12465 else if (section_is_p (sectp->name, &names->line_dwo))
12466 {
049412e3 12467 dwp_file->sections.line.s.section = sectp;
fd361982 12468 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12469 }
12470 else if (section_is_p (sectp->name, &names->loc_dwo))
12471 {
049412e3 12472 dwp_file->sections.loc.s.section = sectp;
fd361982 12473 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12474 }
12475 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12476 {
049412e3 12477 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12478 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12479 }
12480 else if (section_is_p (sectp->name, &names->macro_dwo))
12481 {
049412e3 12482 dwp_file->sections.macro.s.section = sectp;
fd361982 12483 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12484 }
12485 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12486 {
049412e3 12487 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12488 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12489 }
12490 else if (section_is_p (sectp->name, &names->types_dwo))
12491 {
049412e3 12492 dwp_file->sections.types.s.section = sectp;
fd361982 12493 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12494 }
12495}
12496
80626a55 12497/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12498
80626a55
DE
12499static hashval_t
12500hash_dwp_loaded_cutus (const void *item)
12501{
9a3c8263 12502 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12503
80626a55
DE
12504 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12505 return dwo_unit->signature;
3019eac3
DE
12506}
12507
80626a55 12508/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12509
80626a55
DE
12510static int
12511eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12512{
9a3c8263
SM
12513 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12514 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12515
80626a55
DE
12516 return dua->signature == dub->signature;
12517}
3019eac3 12518
80626a55 12519/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12520
48b490f2 12521static htab_up
298e9637 12522allocate_dwp_loaded_cutus_table ()
80626a55 12523{
48b490f2
TT
12524 return htab_up (htab_create_alloc (3,
12525 hash_dwp_loaded_cutus,
12526 eq_dwp_loaded_cutus,
12527 NULL, xcalloc, xfree));
80626a55 12528}
3019eac3 12529
ab5088bf
DE
12530/* Try to open DWP file FILE_NAME.
12531 The result is the bfd handle of the file.
12532 If there is a problem finding or opening the file, return NULL.
12533 Upon success, the canonicalized path of the file is stored in the bfd,
12534 same as symfile_bfd_open. */
12535
192b62ce 12536static gdb_bfd_ref_ptr
ed2dc618
SM
12537open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12538 const char *file_name)
ab5088bf 12539{
ed2dc618
SM
12540 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12541 1 /*is_dwp*/,
192b62ce 12542 1 /*search_cwd*/));
6ac97d4c
DE
12543 if (abfd != NULL)
12544 return abfd;
12545
12546 /* Work around upstream bug 15652.
12547 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12548 [Whether that's a "bug" is debatable, but it is getting in our way.]
12549 We have no real idea where the dwp file is, because gdb's realpath-ing
12550 of the executable's path may have discarded the needed info.
12551 [IWBN if the dwp file name was recorded in the executable, akin to
12552 .gnu_debuglink, but that doesn't exist yet.]
12553 Strip the directory from FILE_NAME and search again. */
12554 if (*debug_file_directory != '\0')
12555 {
12556 /* Don't implicitly search the current directory here.
12557 If the user wants to search "." to handle this case,
12558 it must be added to debug-file-directory. */
ed2dc618
SM
12559 return try_open_dwop_file (dwarf2_per_objfile,
12560 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12561 0 /*search_cwd*/);
12562 }
12563
12564 return NULL;
ab5088bf
DE
12565}
12566
80626a55
DE
12567/* Initialize the use of the DWP file for the current objfile.
12568 By convention the name of the DWP file is ${objfile}.dwp.
12569 The result is NULL if it can't be found. */
a766d390 12570
400174b1 12571static std::unique_ptr<struct dwp_file>
ed2dc618 12572open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12573{
12574 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12575
82bf32bc
JK
12576 /* Try to find first .dwp for the binary file before any symbolic links
12577 resolving. */
6c447423
DE
12578
12579 /* If the objfile is a debug file, find the name of the real binary
12580 file and get the name of dwp file from there. */
d721ba37 12581 std::string dwp_name;
6c447423
DE
12582 if (objfile->separate_debug_objfile_backlink != NULL)
12583 {
12584 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12585 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12586
d721ba37 12587 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12588 }
12589 else
d721ba37
PA
12590 dwp_name = objfile->original_name;
12591
12592 dwp_name += ".dwp";
80626a55 12593
ed2dc618 12594 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12595 if (dbfd == NULL
12596 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12597 {
12598 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12599 dwp_name = objfile_name (objfile);
12600 dwp_name += ".dwp";
ed2dc618 12601 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12602 }
12603
80626a55
DE
12604 if (dbfd == NULL)
12605 {
b4f54984 12606 if (dwarf_read_debug)
d721ba37 12607 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12608 return std::unique_ptr<dwp_file> ();
3019eac3 12609 }
400174b1
TT
12610
12611 const char *name = bfd_get_filename (dbfd.get ());
12612 std::unique_ptr<struct dwp_file> dwp_file
12613 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12614
0a0f4c01 12615 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12616 dwp_file->elf_sections =
5989a64e 12617 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12618 dwp_file->num_sections, asection *);
12619
400174b1
TT
12620 bfd_map_over_sections (dwp_file->dbfd.get (),
12621 dwarf2_locate_common_dwp_sections,
12622 dwp_file.get ());
80626a55 12623
400174b1
TT
12624 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12625 0);
80626a55 12626
400174b1
TT
12627 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12628 1);
80626a55 12629
73869dc2 12630 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12631 if (dwp_file->cus && dwp_file->tus
12632 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12633 {
12634 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12635 pretty bizarre. We use pulongest here because that's the established
4d65956b 12636 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12637 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12638 " TU version %s [in DWP file %s]"),
12639 pulongest (dwp_file->cus->version),
d721ba37 12640 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12641 }
08302ed2
DE
12642
12643 if (dwp_file->cus)
12644 dwp_file->version = dwp_file->cus->version;
12645 else if (dwp_file->tus)
12646 dwp_file->version = dwp_file->tus->version;
12647 else
12648 dwp_file->version = 2;
73869dc2
DE
12649
12650 if (dwp_file->version == 2)
400174b1
TT
12651 bfd_map_over_sections (dwp_file->dbfd.get (),
12652 dwarf2_locate_v2_dwp_sections,
12653 dwp_file.get ());
73869dc2 12654
298e9637
SM
12655 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12656 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12657
b4f54984 12658 if (dwarf_read_debug)
80626a55
DE
12659 {
12660 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12661 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12662 " %s CUs, %s TUs\n",
12663 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12664 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12665 }
12666
12667 return dwp_file;
3019eac3 12668}
c906108c 12669
ab5088bf
DE
12670/* Wrapper around open_and_init_dwp_file, only open it once. */
12671
12672static struct dwp_file *
ed2dc618 12673get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12674{
5989a64e 12675 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12676 {
5989a64e 12677 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12678 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12679 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12680 }
5989a64e 12681 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12682}
12683
80626a55
DE
12684/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12685 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12686 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12687 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12688 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12689
12690 This is called, for example, when wanting to read a variable with a
12691 complex location. Therefore we don't want to do file i/o for every call.
12692 Therefore we don't want to look for a DWO file on every call.
12693 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12694 then we check if we've already seen DWO_NAME, and only THEN do we check
12695 for a DWO file.
12696
1c658ad5 12697 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12698 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12699
3019eac3 12700static struct dwo_unit *
4ab09049 12701lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12702 ULONGEST signature, int is_debug_types)
3019eac3 12703{
4ab09049 12704 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12705 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12706 const char *kind = is_debug_types ? "TU" : "CU";
12707 void **dwo_file_slot;
3019eac3 12708 struct dwo_file *dwo_file;
80626a55 12709 struct dwp_file *dwp_file;
cb1df416 12710
6a506a2d
DE
12711 /* First see if there's a DWP file.
12712 If we have a DWP file but didn't find the DWO inside it, don't
12713 look for the original DWO file. It makes gdb behave differently
12714 depending on whether one is debugging in the build tree. */
cf2c3c16 12715
ed2dc618 12716 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12717 if (dwp_file != NULL)
cf2c3c16 12718 {
80626a55
DE
12719 const struct dwp_hash_table *dwp_htab =
12720 is_debug_types ? dwp_file->tus : dwp_file->cus;
12721
12722 if (dwp_htab != NULL)
12723 {
12724 struct dwo_unit *dwo_cutu =
ed2dc618 12725 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12726 signature, is_debug_types);
80626a55
DE
12727
12728 if (dwo_cutu != NULL)
12729 {
b4f54984 12730 if (dwarf_read_debug)
80626a55
DE
12731 {
12732 fprintf_unfiltered (gdb_stdlog,
12733 "Virtual DWO %s %s found: @%s\n",
12734 kind, hex_string (signature),
12735 host_address_to_string (dwo_cutu));
12736 }
12737 return dwo_cutu;
12738 }
12739 }
12740 }
6a506a2d 12741 else
80626a55 12742 {
6a506a2d 12743 /* No DWP file, look for the DWO file. */
80626a55 12744
ed2dc618
SM
12745 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12746 dwo_name, comp_dir);
6a506a2d 12747 if (*dwo_file_slot == NULL)
80626a55 12748 {
6a506a2d 12749 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12750 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12751 }
6a506a2d 12752 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12753 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12754
6a506a2d 12755 if (dwo_file != NULL)
19c3d4c9 12756 {
6a506a2d
DE
12757 struct dwo_unit *dwo_cutu = NULL;
12758
12759 if (is_debug_types && dwo_file->tus)
12760 {
12761 struct dwo_unit find_dwo_cutu;
12762
12763 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12764 find_dwo_cutu.signature = signature;
9a3c8263 12765 dwo_cutu
b0b6a987
TT
12766 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12767 &find_dwo_cutu);
6a506a2d 12768 }
33c5cd75 12769 else if (!is_debug_types && dwo_file->cus)
80626a55 12770 {
33c5cd75
DB
12771 struct dwo_unit find_dwo_cutu;
12772
12773 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12774 find_dwo_cutu.signature = signature;
b0b6a987 12775 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12776 &find_dwo_cutu);
6a506a2d
DE
12777 }
12778
12779 if (dwo_cutu != NULL)
12780 {
b4f54984 12781 if (dwarf_read_debug)
6a506a2d
DE
12782 {
12783 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12784 kind, dwo_name, hex_string (signature),
12785 host_address_to_string (dwo_cutu));
12786 }
12787 return dwo_cutu;
80626a55
DE
12788 }
12789 }
2e276125 12790 }
9cdd5dbd 12791
80626a55
DE
12792 /* We didn't find it. This could mean a dwo_id mismatch, or
12793 someone deleted the DWO/DWP file, or the search path isn't set up
12794 correctly to find the file. */
12795
b4f54984 12796 if (dwarf_read_debug)
80626a55
DE
12797 {
12798 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12799 kind, dwo_name, hex_string (signature));
12800 }
3019eac3 12801
6656a72d
DE
12802 /* This is a warning and not a complaint because it can be caused by
12803 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12804 {
12805 /* Print the name of the DWP file if we looked there, helps the user
12806 better diagnose the problem. */
791afaa2 12807 std::string dwp_text;
43942612
DE
12808
12809 if (dwp_file != NULL)
791afaa2
TT
12810 dwp_text = string_printf (" [in DWP file %s]",
12811 lbasename (dwp_file->name));
43942612 12812
9d8780f0 12813 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12814 " [in module %s]"),
4ab09049
SM
12815 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12816 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12817 }
3019eac3 12818 return NULL;
5fb290d7
DJ
12819}
12820
80626a55
DE
12821/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12822 See lookup_dwo_cutu_unit for details. */
12823
12824static struct dwo_unit *
4ab09049 12825lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12826 ULONGEST signature)
12827{
4ab09049
SM
12828 gdb_assert (!cu->per_cu->is_debug_types);
12829
12830 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12831}
12832
12833/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12834 See lookup_dwo_cutu_unit for details. */
12835
12836static struct dwo_unit *
4ab09049 12837lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12838{
4ab09049
SM
12839 gdb_assert (cu->per_cu->is_debug_types);
12840
12841 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12842
12843 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12844}
12845
89e63ee4
DE
12846/* Traversal function for queue_and_load_all_dwo_tus. */
12847
12848static int
12849queue_and_load_dwo_tu (void **slot, void *info)
12850{
12851 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12852 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12853 ULONGEST signature = dwo_unit->signature;
d460f660 12854 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12855
12856 if (sig_type != NULL)
12857 {
12858 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12859
12860 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12861 a real dependency of PER_CU on SIG_TYPE. That is detected later
12862 while processing PER_CU. */
d460f660
SM
12863 if (maybe_queue_comp_unit (NULL, sig_cu, cu->language))
12864 load_full_type_unit (sig_cu, cu->per_objfile);
12865 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12866 }
12867
12868 return 1;
12869}
12870
12871/* Queue all TUs contained in the DWO of PER_CU to be read in.
12872 The DWO may have the only definition of the type, though it may not be
12873 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12874 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12875
12876static void
12877queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12878{
12879 struct dwo_unit *dwo_unit;
12880 struct dwo_file *dwo_file;
12881
12882 gdb_assert (!per_cu->is_debug_types);
89e63ee4 12883 gdb_assert (per_cu->cu != NULL);
64874a40 12884 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
89e63ee4
DE
12885
12886 dwo_unit = per_cu->cu->dwo_unit;
12887 gdb_assert (dwo_unit != NULL);
12888
12889 dwo_file = dwo_unit->dwo_file;
12890 if (dwo_file->tus != NULL)
b0b6a987 12891 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
d460f660 12892 per_cu->cu);
89e63ee4
DE
12893}
12894
3019eac3 12895/* Read in various DIEs. */
348e048f 12896
d389af10 12897/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12898 Inherit only the children of the DW_AT_abstract_origin DIE not being
12899 already referenced by DW_AT_abstract_origin from the children of the
12900 current DIE. */
d389af10
JK
12901
12902static void
12903inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12904{
12905 struct die_info *child_die;
791afaa2 12906 sect_offset *offsetp;
d389af10
JK
12907 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12908 struct die_info *origin_die;
12909 /* Iterator of the ORIGIN_DIE children. */
12910 struct die_info *origin_child_die;
d389af10 12911 struct attribute *attr;
cd02d79d
PA
12912 struct dwarf2_cu *origin_cu;
12913 struct pending **origin_previous_list_in_scope;
d389af10
JK
12914
12915 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12916 if (!attr)
12917 return;
12918
cd02d79d
PA
12919 /* Note that following die references may follow to a die in a
12920 different cu. */
12921
12922 origin_cu = cu;
12923 origin_die = follow_die_ref (die, attr, &origin_cu);
12924
12925 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12926 symbols in. */
12927 origin_previous_list_in_scope = origin_cu->list_in_scope;
12928 origin_cu->list_in_scope = cu->list_in_scope;
12929
edb3359d
DJ
12930 if (die->tag != origin_die->tag
12931 && !(die->tag == DW_TAG_inlined_subroutine
12932 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12933 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12934 sect_offset_str (die->sect_off),
12935 sect_offset_str (origin_die->sect_off));
d389af10 12936
791afaa2 12937 std::vector<sect_offset> offsets;
d389af10 12938
3ea89b92
PMR
12939 for (child_die = die->child;
12940 child_die && child_die->tag;
436c571c 12941 child_die = child_die->sibling)
3ea89b92
PMR
12942 {
12943 struct die_info *child_origin_die;
12944 struct dwarf2_cu *child_origin_cu;
12945
12946 /* We are trying to process concrete instance entries:
216f72a1 12947 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12948 it's not relevant to our analysis here. i.e. detecting DIEs that are
12949 present in the abstract instance but not referenced in the concrete
12950 one. */
216f72a1
JK
12951 if (child_die->tag == DW_TAG_call_site
12952 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12953 continue;
12954
c38f313d
DJ
12955 /* For each CHILD_DIE, find the corresponding child of
12956 ORIGIN_DIE. If there is more than one layer of
12957 DW_AT_abstract_origin, follow them all; there shouldn't be,
12958 but GCC versions at least through 4.4 generate this (GCC PR
12959 40573). */
3ea89b92
PMR
12960 child_origin_die = child_die;
12961 child_origin_cu = cu;
c38f313d
DJ
12962 while (1)
12963 {
cd02d79d
PA
12964 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12965 child_origin_cu);
c38f313d
DJ
12966 if (attr == NULL)
12967 break;
cd02d79d
PA
12968 child_origin_die = follow_die_ref (child_origin_die, attr,
12969 &child_origin_cu);
c38f313d
DJ
12970 }
12971
d389af10
JK
12972 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12973 counterpart may exist. */
c38f313d 12974 if (child_origin_die != child_die)
d389af10 12975 {
edb3359d
DJ
12976 if (child_die->tag != child_origin_die->tag
12977 && !(child_die->tag == DW_TAG_inlined_subroutine
12978 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12979 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12980 "different tags"),
9d8780f0
SM
12981 sect_offset_str (child_die->sect_off),
12982 sect_offset_str (child_origin_die->sect_off));
c38f313d 12983 if (child_origin_die->parent != origin_die)
b98664d3 12984 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12985 "different parents"),
9d8780f0
SM
12986 sect_offset_str (child_die->sect_off),
12987 sect_offset_str (child_origin_die->sect_off));
c38f313d 12988 else
791afaa2 12989 offsets.push_back (child_origin_die->sect_off);
d389af10 12990 }
d389af10 12991 }
791afaa2
TT
12992 std::sort (offsets.begin (), offsets.end ());
12993 sect_offset *offsets_end = offsets.data () + offsets.size ();
12994 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12995 if (offsetp[-1] == *offsetp)
b98664d3 12996 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12997 "to DIE %s as their abstract origin"),
12998 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12999
791afaa2 13000 offsetp = offsets.data ();
d389af10
JK
13001 origin_child_die = origin_die->child;
13002 while (origin_child_die && origin_child_die->tag)
13003 {
13004 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13005 while (offsetp < offsets_end
9c541725 13006 && *offsetp < origin_child_die->sect_off)
d389af10 13007 offsetp++;
b64f50a1 13008 if (offsetp >= offsets_end
9c541725 13009 || *offsetp > origin_child_die->sect_off)
d389af10 13010 {
adde2bff
DE
13011 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13012 Check whether we're already processing ORIGIN_CHILD_DIE.
13013 This can happen with mutually referenced abstract_origins.
13014 PR 16581. */
13015 if (!origin_child_die->in_process)
13016 process_die (origin_child_die, origin_cu);
d389af10 13017 }
436c571c 13018 origin_child_die = origin_child_die->sibling;
d389af10 13019 }
cd02d79d 13020 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13021
13022 if (cu != origin_cu)
13023 compute_delayed_physnames (origin_cu);
d389af10
JK
13024}
13025
c906108c 13026static void
e7c27a73 13027read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13028{
5e22e966 13029 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13030 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13031 struct context_stack *newobj;
c906108c
SS
13032 CORE_ADDR lowpc;
13033 CORE_ADDR highpc;
13034 struct die_info *child_die;
edb3359d 13035 struct attribute *attr, *call_line, *call_file;
15d034d0 13036 const char *name;
e142c38c 13037 CORE_ADDR baseaddr;
801e3a5b 13038 struct block *block;
edb3359d 13039 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13040 std::vector<struct symbol *> template_args;
34eaf542 13041 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13042
13043 if (inlined_func)
13044 {
13045 /* If we do not have call site information, we can't show the
13046 caller of this inlined function. That's too confusing, so
13047 only use the scope for local variables. */
13048 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13049 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13050 if (call_line == NULL || call_file == NULL)
13051 {
13052 read_lexical_block_scope (die, cu);
13053 return;
13054 }
13055 }
c906108c 13056
b3b3bada 13057 baseaddr = objfile->text_section_offset ();
e142c38c 13058
94af9270 13059 name = dwarf2_name (die, cu);
c906108c 13060
e8d05480
JB
13061 /* Ignore functions with missing or empty names. These are actually
13062 illegal according to the DWARF standard. */
13063 if (name == NULL)
13064 {
b98664d3 13065 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13066 sect_offset_str (die->sect_off));
e8d05480
JB
13067 return;
13068 }
13069
13070 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13071 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13072 <= PC_BOUNDS_INVALID)
e8d05480 13073 {
ae4d0c03
PM
13074 attr = dwarf2_attr (die, DW_AT_external, cu);
13075 if (!attr || !DW_UNSND (attr))
b98664d3 13076 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13077 "for subprogram DIE at %s"),
13078 sect_offset_str (die->sect_off));
e8d05480
JB
13079 return;
13080 }
c906108c 13081
3e29f34a
MR
13082 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13083 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13084
34eaf542
TT
13085 /* If we have any template arguments, then we must allocate a
13086 different sort of symbol. */
436c571c 13087 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13088 {
13089 if (child_die->tag == DW_TAG_template_type_param
13090 || child_die->tag == DW_TAG_template_value_param)
13091 {
8c14c3a3 13092 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13093 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13094 break;
13095 }
13096 }
13097
c24bdb02 13098 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13099 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13100 (struct symbol *) templ_func);
4c2df51b 13101
81873cc8 13102 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13103 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13104 cu->language);
13105
4cecd739
DJ
13106 /* If there is a location expression for DW_AT_frame_base, record
13107 it. */
e142c38c 13108 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13109 if (attr != nullptr)
fe978cb0 13110 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13111
63e43d3a
PMR
13112 /* If there is a location for the static link, record it. */
13113 newobj->static_link = NULL;
13114 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13115 if (attr != nullptr)
63e43d3a 13116 {
224c3ddb
SM
13117 newobj->static_link
13118 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13119 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13120 cu->addr_type ());
63e43d3a
PMR
13121 }
13122
c24bdb02 13123 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13124
639d11d3 13125 if (die->child != NULL)
c906108c 13126 {
639d11d3 13127 child_die = die->child;
c906108c
SS
13128 while (child_die && child_die->tag)
13129 {
34eaf542
TT
13130 if (child_die->tag == DW_TAG_template_type_param
13131 || child_die->tag == DW_TAG_template_value_param)
13132 {
13133 struct symbol *arg = new_symbol (child_die, NULL, cu);
13134
f1078f66 13135 if (arg != NULL)
2f4732b0 13136 template_args.push_back (arg);
34eaf542
TT
13137 }
13138 else
13139 process_die (child_die, cu);
436c571c 13140 child_die = child_die->sibling;
c906108c
SS
13141 }
13142 }
13143
d389af10
JK
13144 inherit_abstract_dies (die, cu);
13145
4a811a97
UW
13146 /* If we have a DW_AT_specification, we might need to import using
13147 directives from the context of the specification DIE. See the
13148 comment in determine_prefix. */
13149 if (cu->language == language_cplus
13150 && dwarf2_attr (die, DW_AT_specification, cu))
13151 {
13152 struct dwarf2_cu *spec_cu = cu;
13153 struct die_info *spec_die = die_specification (die, &spec_cu);
13154
13155 while (spec_die)
13156 {
13157 child_die = spec_die->child;
13158 while (child_die && child_die->tag)
13159 {
13160 if (child_die->tag == DW_TAG_imported_module)
13161 process_die (child_die, spec_cu);
436c571c 13162 child_die = child_die->sibling;
4a811a97
UW
13163 }
13164
13165 /* In some cases, GCC generates specification DIEs that
13166 themselves contain DW_AT_specification attributes. */
13167 spec_die = die_specification (spec_die, &spec_cu);
13168 }
13169 }
13170
c24bdb02 13171 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13172 /* Make a block for the local symbols within. */
c24bdb02 13173 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13174 cstk.static_link, lowpc, highpc);
801e3a5b 13175
df8a16a1 13176 /* For C++, set the block's scope. */
45280282
IB
13177 if ((cu->language == language_cplus
13178 || cu->language == language_fortran
c44af4eb
TT
13179 || cu->language == language_d
13180 || cu->language == language_rust)
4d4ec4e5 13181 && cu->processing_has_namespace_info)
195a3f6c
TT
13182 block_set_scope (block, determine_prefix (die, cu),
13183 &objfile->objfile_obstack);
df8a16a1 13184
801e3a5b
JB
13185 /* If we have address ranges, record them. */
13186 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13187
a60f3166 13188 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13189
34eaf542 13190 /* Attach template arguments to function. */
2f4732b0 13191 if (!template_args.empty ())
34eaf542
TT
13192 {
13193 gdb_assert (templ_func != NULL);
13194
2f4732b0 13195 templ_func->n_template_arguments = template_args.size ();
34eaf542 13196 templ_func->template_arguments
8d749320
SM
13197 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13198 templ_func->n_template_arguments);
34eaf542 13199 memcpy (templ_func->template_arguments,
2f4732b0 13200 template_args.data (),
34eaf542 13201 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13202
13203 /* Make sure that the symtab is set on the new symbols. Even
13204 though they don't appear in this symtab directly, other parts
13205 of gdb assume that symbols do, and this is reasonably
13206 true. */
8634679f 13207 for (symbol *sym : template_args)
3e1d3d8c 13208 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13209 }
13210
208d8187
JB
13211 /* In C++, we can have functions nested inside functions (e.g., when
13212 a function declares a class that has methods). This means that
13213 when we finish processing a function scope, we may need to go
13214 back to building a containing block's symbol lists. */
c24bdb02
KS
13215 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13216 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13217
921e78cf
JB
13218 /* If we've finished processing a top-level function, subsequent
13219 symbols go in the file symbol list. */
c24bdb02
KS
13220 if (cu->get_builder ()->outermost_context_p ())
13221 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13222}
13223
13224/* Process all the DIES contained within a lexical block scope. Start
13225 a new scope, process the dies, and then close the scope. */
13226
13227static void
e7c27a73 13228read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13229{
5e22e966 13230 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13231 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13232 CORE_ADDR lowpc, highpc;
13233 struct die_info *child_die;
e142c38c
DJ
13234 CORE_ADDR baseaddr;
13235
b3b3bada 13236 baseaddr = objfile->text_section_offset ();
c906108c
SS
13237
13238 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13239 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13240 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13241 be nasty. Might be easier to properly extend generic blocks to
af34e669 13242 describe ranges. */
e385593e
JK
13243 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13244 {
13245 case PC_BOUNDS_NOT_PRESENT:
13246 /* DW_TAG_lexical_block has no attributes, process its children as if
13247 there was no wrapping by that DW_TAG_lexical_block.
13248 GCC does no longer produces such DWARF since GCC r224161. */
13249 for (child_die = die->child;
13250 child_die != NULL && child_die->tag;
436c571c 13251 child_die = child_die->sibling)
4f7bc5ed
TT
13252 {
13253 /* We might already be processing this DIE. This can happen
13254 in an unusual circumstance -- where a subroutine A
13255 appears lexically in another subroutine B, but A actually
13256 inlines B. The recursion is broken here, rather than in
13257 inherit_abstract_dies, because it seems better to simply
13258 drop concrete children here. */
13259 if (!child_die->in_process)
13260 process_die (child_die, cu);
13261 }
e385593e
JK
13262 return;
13263 case PC_BOUNDS_INVALID:
13264 return;
13265 }
3e29f34a
MR
13266 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13267 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13268
c24bdb02 13269 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13270 if (die->child != NULL)
c906108c 13271 {
639d11d3 13272 child_die = die->child;
c906108c
SS
13273 while (child_die && child_die->tag)
13274 {
e7c27a73 13275 process_die (child_die, cu);
436c571c 13276 child_die = child_die->sibling;
c906108c
SS
13277 }
13278 }
3ea89b92 13279 inherit_abstract_dies (die, cu);
c24bdb02 13280 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13281
c24bdb02
KS
13282 if (*cu->get_builder ()->get_local_symbols () != NULL
13283 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13284 {
801e3a5b 13285 struct block *block
c24bdb02 13286 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13287 cstk.start_addr, highpc);
801e3a5b
JB
13288
13289 /* Note that recording ranges after traversing children, as we
13290 do here, means that recording a parent's ranges entails
13291 walking across all its children's ranges as they appear in
13292 the address map, which is quadratic behavior.
13293
13294 It would be nicer to record the parent's ranges before
13295 traversing its children, simply overriding whatever you find
13296 there. But since we don't even decide whether to create a
13297 block until after we've traversed its children, that's hard
13298 to do. */
13299 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13300 }
c24bdb02
KS
13301 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13302 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13303}
13304
216f72a1 13305/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13306
13307static void
13308read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13309{
5e22e966 13310 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13311 struct objfile *objfile = per_objfile->objfile;
08feed99 13312 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13313 CORE_ADDR pc, baseaddr;
13314 struct attribute *attr;
13315 struct call_site *call_site, call_site_local;
13316 void **slot;
13317 int nparams;
13318 struct die_info *child_die;
13319
b3b3bada 13320 baseaddr = objfile->text_section_offset ();
96408a79 13321
216f72a1
JK
13322 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13323 if (attr == NULL)
13324 {
13325 /* This was a pre-DWARF-5 GNU extension alias
13326 for DW_AT_call_return_pc. */
13327 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13328 }
96408a79
SA
13329 if (!attr)
13330 {
b98664d3 13331 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13332 "DIE %s [in module %s]"),
13333 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13334 return;
13335 }
cd6c91b4 13336 pc = attr->value_as_address () + baseaddr;
3e29f34a 13337 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13338
13339 if (cu->call_site_htab == NULL)
13340 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13341 NULL, &objfile->objfile_obstack,
13342 hashtab_obstack_allocate, NULL);
13343 call_site_local.pc = pc;
13344 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13345 if (*slot != NULL)
13346 {
b98664d3 13347 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13348 "DIE %s [in module %s]"),
13349 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13350 objfile_name (objfile));
96408a79
SA
13351 return;
13352 }
13353
13354 /* Count parameters at the caller. */
13355
13356 nparams = 0;
13357 for (child_die = die->child; child_die && child_die->tag;
436c571c 13358 child_die = child_die->sibling)
96408a79 13359 {
216f72a1
JK
13360 if (child_die->tag != DW_TAG_call_site_parameter
13361 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13362 {
b98664d3 13363 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13364 "DW_TAG_call_site child DIE %s [in module %s]"),
13365 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13366 objfile_name (objfile));
96408a79
SA
13367 continue;
13368 }
13369
13370 nparams++;
13371 }
13372
224c3ddb
SM
13373 call_site
13374 = ((struct call_site *)
13375 obstack_alloc (&objfile->objfile_obstack,
13376 sizeof (*call_site)
13377 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13378 *slot = call_site;
13379 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13380 call_site->pc = pc;
13381
216f72a1
JK
13382 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13383 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13384 {
13385 struct die_info *func_die;
13386
13387 /* Skip also over DW_TAG_inlined_subroutine. */
13388 for (func_die = die->parent;
13389 func_die && func_die->tag != DW_TAG_subprogram
13390 && func_die->tag != DW_TAG_subroutine_type;
13391 func_die = func_die->parent);
13392
216f72a1
JK
13393 /* DW_AT_call_all_calls is a superset
13394 of DW_AT_call_all_tail_calls. */
96408a79 13395 if (func_die
216f72a1 13396 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13397 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13398 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13399 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13400 {
13401 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13402 not complete. But keep CALL_SITE for look ups via call_site_htab,
13403 both the initial caller containing the real return address PC and
13404 the final callee containing the current PC of a chain of tail
13405 calls do not need to have the tail call list complete. But any
13406 function candidate for a virtual tail call frame searched via
13407 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13408 determined unambiguously. */
13409 }
13410 else
13411 {
13412 struct type *func_type = NULL;
13413
13414 if (func_die)
13415 func_type = get_die_type (func_die, cu);
13416 if (func_type != NULL)
13417 {
78134374 13418 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13419
13420 /* Enlist this call site to the function. */
13421 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13422 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13423 }
13424 else
b98664d3 13425 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13426 "DIE %s [in module %s]"),
13427 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13428 }
13429 }
13430
216f72a1
JK
13431 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13432 if (attr == NULL)
13433 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13434 if (attr == NULL)
13435 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13436 if (attr == NULL)
216f72a1
JK
13437 {
13438 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13439 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13440 }
96408a79 13441 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13442 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13443 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13444 else if (attr->form_is_block ())
96408a79
SA
13445 {
13446 struct dwarf2_locexpr_baton *dlbaton;
13447
8d749320 13448 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13449 dlbaton->data = DW_BLOCK (attr)->data;
13450 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13451 dlbaton->per_objfile = per_objfile;
96408a79
SA
13452 dlbaton->per_cu = cu->per_cu;
13453
13454 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13455 }
cd6c91b4 13456 else if (attr->form_is_ref ())
96408a79 13457 {
96408a79
SA
13458 struct dwarf2_cu *target_cu = cu;
13459 struct die_info *target_die;
13460
ac9ec31b 13461 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13462 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13463 if (die_is_declaration (target_die, target_cu))
13464 {
7d45c7c3 13465 const char *target_physname;
9112db09
JK
13466
13467 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13468 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13469 if (target_physname == NULL)
9112db09 13470 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13471 if (target_physname == NULL)
b98664d3 13472 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13473 "physname, for referencing DIE %s [in module %s]"),
13474 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13475 else
7d455152 13476 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13477 }
13478 else
13479 {
13480 CORE_ADDR lowpc;
13481
13482 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13483 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13484 <= PC_BOUNDS_INVALID)
b98664d3 13485 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13486 "low pc, for referencing DIE %s [in module %s]"),
13487 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13488 else
3e29f34a
MR
13489 {
13490 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13491 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13492 }
96408a79
SA
13493 }
13494 }
13495 else
b98664d3 13496 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13497 "block nor reference, for DIE %s [in module %s]"),
13498 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13499
13500 call_site->per_cu = cu->per_cu;
13501
13502 for (child_die = die->child;
13503 child_die && child_die->tag;
436c571c 13504 child_die = child_die->sibling)
96408a79 13505 {
96408a79 13506 struct call_site_parameter *parameter;
1788b2d3 13507 struct attribute *loc, *origin;
96408a79 13508
216f72a1
JK
13509 if (child_die->tag != DW_TAG_call_site_parameter
13510 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13511 {
13512 /* Already printed the complaint above. */
13513 continue;
13514 }
13515
13516 gdb_assert (call_site->parameter_count < nparams);
13517 parameter = &call_site->parameter[call_site->parameter_count];
13518
1788b2d3
JK
13519 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13520 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13521 register is contained in DW_AT_call_value. */
96408a79 13522
24c5c679 13523 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13524 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13525 if (origin == NULL)
13526 {
13527 /* This was a pre-DWARF-5 GNU extension alias
13528 for DW_AT_call_parameter. */
13529 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13530 }
cd6c91b4 13531 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13532 {
1788b2d3 13533 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13534
0826b30a 13535 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13536 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13537 {
13538 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13539 binding can be done only inside one CU. Such referenced DIE
13540 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13541 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13542 "DW_TAG_call_site child DIE %s [in module %s]"),
13543 sect_offset_str (child_die->sect_off),
9c541725 13544 objfile_name (objfile));
d76b7dbc
JK
13545 continue;
13546 }
9c541725
PA
13547 parameter->u.param_cu_off
13548 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13549 }
4fc6c0d5 13550 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13551 {
b98664d3 13552 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13553 "DW_TAG_call_site child DIE %s [in module %s]"),
13554 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13555 continue;
13556 }
24c5c679 13557 else
96408a79 13558 {
24c5c679
JK
13559 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13560 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13561 if (parameter->u.dwarf_reg != -1)
13562 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13563 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13564 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13565 &parameter->u.fb_offset))
13566 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13567 else
13568 {
b98664d3 13569 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13570 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13571 "DW_TAG_call_site child DIE %s "
24c5c679 13572 "[in module %s]"),
9d8780f0 13573 sect_offset_str (child_die->sect_off),
9c541725 13574 objfile_name (objfile));
24c5c679
JK
13575 continue;
13576 }
96408a79
SA
13577 }
13578
216f72a1
JK
13579 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13580 if (attr == NULL)
13581 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13582 if (attr == NULL || !attr->form_is_block ())
96408a79 13583 {
b98664d3 13584 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13585 "DW_TAG_call_site child DIE %s [in module %s]"),
13586 sect_offset_str (child_die->sect_off),
9c541725 13587 objfile_name (objfile));
96408a79
SA
13588 continue;
13589 }
13590 parameter->value = DW_BLOCK (attr)->data;
13591 parameter->value_size = DW_BLOCK (attr)->size;
13592
13593 /* Parameters are not pre-cleared by memset above. */
13594 parameter->data_value = NULL;
13595 parameter->data_value_size = 0;
13596 call_site->parameter_count++;
13597
216f72a1
JK
13598 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13599 if (attr == NULL)
13600 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13601 if (attr != nullptr)
96408a79 13602 {
4fc6c0d5 13603 if (!attr->form_is_block ())
b98664d3 13604 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13605 "DW_TAG_call_site child DIE %s [in module %s]"),
13606 sect_offset_str (child_die->sect_off),
9c541725 13607 objfile_name (objfile));
96408a79
SA
13608 else
13609 {
13610 parameter->data_value = DW_BLOCK (attr)->data;
13611 parameter->data_value_size = DW_BLOCK (attr)->size;
13612 }
13613 }
13614 }
13615}
13616
71a3c369
TT
13617/* Helper function for read_variable. If DIE represents a virtual
13618 table, then return the type of the concrete object that is
13619 associated with the virtual table. Otherwise, return NULL. */
13620
13621static struct type *
13622rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13623{
13624 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13625 if (attr == NULL)
13626 return NULL;
13627
13628 /* Find the type DIE. */
13629 struct die_info *type_die = NULL;
13630 struct dwarf2_cu *type_cu = cu;
13631
cd6c91b4 13632 if (attr->form_is_ref ())
71a3c369
TT
13633 type_die = follow_die_ref (die, attr, &type_cu);
13634 if (type_die == NULL)
13635 return NULL;
13636
13637 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13638 return NULL;
13639 return die_containing_type (type_die, type_cu);
13640}
13641
13642/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13643
13644static void
13645read_variable (struct die_info *die, struct dwarf2_cu *cu)
13646{
13647 struct rust_vtable_symbol *storage = NULL;
13648
13649 if (cu->language == language_rust)
13650 {
13651 struct type *containing_type = rust_containing_type (die, cu);
13652
13653 if (containing_type != NULL)
13654 {
5e22e966 13655 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13656
8c14c3a3 13657 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13658 storage->concrete_type = containing_type;
cf724bc9 13659 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13660 }
13661 }
13662
e4a62c65
TV
13663 struct symbol *res = new_symbol (die, NULL, cu, storage);
13664 struct attribute *abstract_origin
13665 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13666 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13667 if (res == NULL && loc && abstract_origin)
13668 {
13669 /* We have a variable without a name, but with a location and an abstract
13670 origin. This may be a concrete instance of an abstract variable
13671 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13672 later. */
13673 struct dwarf2_cu *origin_cu = cu;
13674 struct die_info *origin_die
13675 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13676 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13677 per_objfile->per_bfd->abstract_to_concrete
13678 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13679 }
71a3c369
TT
13680}
13681
43988095
JK
13682/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13683 reading .debug_rnglists.
13684 Callback's type should be:
13685 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13686 Return true if the attributes are present and valid, otherwise,
13687 return false. */
13688
13689template <typename Callback>
13690static bool
13691dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13692 Callback &&callback)
13693{
5e22e966 13694 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13695 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13696 bfd *obfd = objfile->obfd;
43988095 13697 /* Base address selection entry. */
2b24b6e4 13698 gdb::optional<CORE_ADDR> base;
43988095 13699 const gdb_byte *buffer;
43988095
JK
13700 CORE_ADDR baseaddr;
13701 bool overflow = false;
13702
43988095
JK
13703 base = cu->base_address;
13704
5989a64e
SM
13705 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13706 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13707 {
b98664d3 13708 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13709 offset);
13710 return false;
13711 }
5989a64e 13712 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13713
b3b3bada 13714 baseaddr = objfile->text_section_offset ();
43988095
JK
13715
13716 while (1)
13717 {
7814882a
JK
13718 /* Initialize it due to a false compiler warning. */
13719 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13720 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13721 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13722 unsigned int bytes_read;
13723
13724 if (buffer == buf_end)
13725 {
13726 overflow = true;
13727 break;
13728 }
13729 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13730 switch (rlet)
13731 {
13732 case DW_RLE_end_of_list:
13733 break;
13734 case DW_RLE_base_address:
13735 if (buffer + cu->header.addr_size > buf_end)
13736 {
13737 overflow = true;
13738 break;
13739 }
c8a7a66f 13740 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13741 buffer += bytes_read;
13742 break;
13743 case DW_RLE_start_length:
13744 if (buffer + cu->header.addr_size > buf_end)
13745 {
13746 overflow = true;
13747 break;
13748 }
c8a7a66f
TT
13749 range_beginning = cu->header.read_address (obfd, buffer,
13750 &bytes_read);
43988095
JK
13751 buffer += bytes_read;
13752 range_end = (range_beginning
13753 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13754 buffer += bytes_read;
13755 if (buffer > buf_end)
13756 {
13757 overflow = true;
13758 break;
13759 }
13760 break;
13761 case DW_RLE_offset_pair:
13762 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13763 buffer += bytes_read;
13764 if (buffer > buf_end)
13765 {
13766 overflow = true;
13767 break;
13768 }
13769 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13770 buffer += bytes_read;
13771 if (buffer > buf_end)
13772 {
13773 overflow = true;
13774 break;
13775 }
13776 break;
13777 case DW_RLE_start_end:
13778 if (buffer + 2 * cu->header.addr_size > buf_end)
13779 {
13780 overflow = true;
13781 break;
13782 }
c8a7a66f
TT
13783 range_beginning = cu->header.read_address (obfd, buffer,
13784 &bytes_read);
43988095 13785 buffer += bytes_read;
c8a7a66f 13786 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13787 buffer += bytes_read;
13788 break;
13789 default:
b98664d3 13790 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13791 return false;
13792 }
13793 if (rlet == DW_RLE_end_of_list || overflow)
13794 break;
13795 if (rlet == DW_RLE_base_address)
13796 continue;
13797
2b24b6e4 13798 if (!base.has_value ())
43988095
JK
13799 {
13800 /* We have no valid base address for the ranges
13801 data. */
b98664d3 13802 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13803 return false;
13804 }
13805
13806 if (range_beginning > range_end)
13807 {
13808 /* Inverted range entries are invalid. */
b98664d3 13809 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13810 return false;
13811 }
13812
13813 /* Empty range entries have no effect. */
13814 if (range_beginning == range_end)
13815 continue;
13816
2b24b6e4
TT
13817 range_beginning += *base;
13818 range_end += *base;
43988095
JK
13819
13820 /* A not-uncommon case of bad debug info.
13821 Don't pollute the addrmap with bad data. */
13822 if (range_beginning + baseaddr == 0
5989a64e 13823 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13824 {
b98664d3 13825 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13826 " [in module %s]"), objfile_name (objfile));
13827 continue;
13828 }
13829
13830 callback (range_beginning, range_end);
13831 }
13832
13833 if (overflow)
13834 {
b98664d3 13835 complaint (_("Offset %d is not terminated "
43988095
JK
13836 "for DW_AT_ranges attribute"),
13837 offset);
13838 return false;
13839 }
13840
13841 return true;
13842}
13843
13844/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13845 Callback's type should be:
13846 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13847 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13848
43988095 13849template <typename Callback>
43039443 13850static int
5f46c5a5 13851dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13852 Callback &&callback)
43039443 13853{
5e22e966
SM
13854 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13855 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13856 struct comp_unit_head *cu_header = &cu->header;
13857 bfd *obfd = objfile->obfd;
13858 unsigned int addr_size = cu_header->addr_size;
13859 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13860 /* Base address selection entry. */
2b24b6e4 13861 gdb::optional<CORE_ADDR> base;
43039443 13862 unsigned int dummy;
d521ce57 13863 const gdb_byte *buffer;
ff013f42 13864 CORE_ADDR baseaddr;
43039443 13865
43988095
JK
13866 if (cu_header->version >= 5)
13867 return dwarf2_rnglists_process (offset, cu, callback);
13868
d00adf39 13869 base = cu->base_address;
43039443 13870
5e22e966
SM
13871 per_objfile->per_bfd->ranges.read (objfile);
13872 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13873 {
b98664d3 13874 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13875 offset);
13876 return 0;
13877 }
5e22e966 13878 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13879
b3b3bada 13880 baseaddr = objfile->text_section_offset ();
ff013f42 13881
43039443
JK
13882 while (1)
13883 {
13884 CORE_ADDR range_beginning, range_end;
13885
c8a7a66f 13886 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13887 buffer += addr_size;
c8a7a66f 13888 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13889 buffer += addr_size;
13890 offset += 2 * addr_size;
13891
13892 /* An end of list marker is a pair of zero addresses. */
13893 if (range_beginning == 0 && range_end == 0)
13894 /* Found the end of list entry. */
13895 break;
13896
13897 /* Each base address selection entry is a pair of 2 values.
13898 The first is the largest possible address, the second is
13899 the base address. Check for a base address here. */
13900 if ((range_beginning & mask) == mask)
13901 {
28d2bfb9
AB
13902 /* If we found the largest possible address, then we already
13903 have the base address in range_end. */
13904 base = range_end;
43039443
JK
13905 continue;
13906 }
13907
2b24b6e4 13908 if (!base.has_value ())
43039443
JK
13909 {
13910 /* We have no valid base address for the ranges
13911 data. */
b98664d3 13912 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13913 return 0;
13914 }
13915
9277c30c
UW
13916 if (range_beginning > range_end)
13917 {
13918 /* Inverted range entries are invalid. */
b98664d3 13919 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13920 return 0;
13921 }
13922
13923 /* Empty range entries have no effect. */
13924 if (range_beginning == range_end)
13925 continue;
13926
2b24b6e4
TT
13927 range_beginning += *base;
13928 range_end += *base;
43039443 13929
01093045
DE
13930 /* A not-uncommon case of bad debug info.
13931 Don't pollute the addrmap with bad data. */
13932 if (range_beginning + baseaddr == 0
5e22e966 13933 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13934 {
b98664d3 13935 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13936 " [in module %s]"), objfile_name (objfile));
01093045
DE
13937 continue;
13938 }
13939
5f46c5a5
JK
13940 callback (range_beginning, range_end);
13941 }
13942
13943 return 1;
13944}
13945
13946/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13947 Return 1 if the attributes are present and valid, otherwise, return 0.
13948 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13949
13950static int
13951dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13952 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13953 dwarf2_psymtab *ranges_pst)
5f46c5a5 13954{
5e22e966 13955 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13956 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13957 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13958 int low_set = 0;
13959 CORE_ADDR low = 0;
13960 CORE_ADDR high = 0;
13961 int retval;
13962
13963 retval = dwarf2_ranges_process (offset, cu,
13964 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13965 {
9277c30c 13966 if (ranges_pst != NULL)
3e29f34a
MR
13967 {
13968 CORE_ADDR lowpc;
13969 CORE_ADDR highpc;
13970
79748972
TT
13971 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13972 range_beginning + baseaddr)
13973 - baseaddr);
13974 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13975 range_end + baseaddr)
13976 - baseaddr);
d320c2b5
TT
13977 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13978 lowpc, highpc - 1, ranges_pst);
3e29f34a 13979 }
ff013f42 13980
43039443
JK
13981 /* FIXME: This is recording everything as a low-high
13982 segment of consecutive addresses. We should have a
13983 data structure for discontiguous block ranges
13984 instead. */
13985 if (! low_set)
13986 {
13987 low = range_beginning;
13988 high = range_end;
13989 low_set = 1;
13990 }
13991 else
13992 {
13993 if (range_beginning < low)
13994 low = range_beginning;
13995 if (range_end > high)
13996 high = range_end;
13997 }
5f46c5a5
JK
13998 });
13999 if (!retval)
14000 return 0;
43039443
JK
14001
14002 if (! low_set)
14003 /* If the first entry is an end-of-list marker, the range
14004 describes an empty scope, i.e. no instructions. */
14005 return 0;
14006
14007 if (low_return)
14008 *low_return = low;
14009 if (high_return)
14010 *high_return = high;
14011 return 1;
14012}
14013
3a2b436a
JK
14014/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14015 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14016 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14017
3a2b436a 14018static enum pc_bounds_kind
af34e669 14019dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14020 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14021 dwarf2_psymtab *pst)
c906108c 14022{
5e22e966 14023 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 14024 struct attribute *attr;
91da1414 14025 struct attribute *attr_high;
af34e669
DJ
14026 CORE_ADDR low = 0;
14027 CORE_ADDR high = 0;
e385593e 14028 enum pc_bounds_kind ret;
c906108c 14029
91da1414
MW
14030 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14031 if (attr_high)
af34e669 14032 {
e142c38c 14033 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14034 if (attr != nullptr)
91da1414 14035 {
cd6c91b4
TT
14036 low = attr->value_as_address ();
14037 high = attr_high->value_as_address ();
14038 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14039 high += low;
91da1414 14040 }
af34e669
DJ
14041 else
14042 /* Found high w/o low attribute. */
e385593e 14043 return PC_BOUNDS_INVALID;
af34e669
DJ
14044
14045 /* Found consecutive range of addresses. */
3a2b436a 14046 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14047 }
c906108c 14048 else
af34e669 14049 {
e142c38c 14050 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14051 if (attr != NULL)
14052 {
18a8505e 14053 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14054 We take advantage of the fact that DW_AT_ranges does not appear
14055 in DW_TAG_compile_unit of DWO files. */
14056 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14057 unsigned int ranges_offset = (DW_UNSND (attr)
14058 + (need_ranges_base
14059 ? cu->ranges_base
14060 : 0));
2e3cf129 14061
af34e669 14062 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14063 .debug_ranges section. */
2e3cf129 14064 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14065 return PC_BOUNDS_INVALID;
43039443 14066 /* Found discontinuous range of addresses. */
3a2b436a 14067 ret = PC_BOUNDS_RANGES;
af34e669 14068 }
e385593e
JK
14069 else
14070 return PC_BOUNDS_NOT_PRESENT;
af34e669 14071 }
c906108c 14072
48fbe735 14073 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14074 if (high <= low)
e385593e 14075 return PC_BOUNDS_INVALID;
c906108c
SS
14076
14077 /* When using the GNU linker, .gnu.linkonce. sections are used to
14078 eliminate duplicate copies of functions and vtables and such.
14079 The linker will arbitrarily choose one and discard the others.
14080 The AT_*_pc values for such functions refer to local labels in
14081 these sections. If the section from that file was discarded, the
14082 labels are not in the output, so the relocs get a value of 0.
14083 If this is a discarded function, mark the pc bounds as invalid,
14084 so that GDB will ignore it. */
5989a64e 14085 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14086 return PC_BOUNDS_INVALID;
c906108c
SS
14087
14088 *lowpc = low;
96408a79
SA
14089 if (highpc)
14090 *highpc = high;
af34e669 14091 return ret;
c906108c
SS
14092}
14093
b084d499
JB
14094/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14095 its low and high PC addresses. Do nothing if these addresses could not
14096 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14097 and HIGHPC to the high address if greater than HIGHPC. */
14098
14099static void
14100dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14101 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14102 struct dwarf2_cu *cu)
14103{
14104 CORE_ADDR low, high;
14105 struct die_info *child = die->child;
14106
e385593e 14107 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14108 {
325fac50
PA
14109 *lowpc = std::min (*lowpc, low);
14110 *highpc = std::max (*highpc, high);
b084d499
JB
14111 }
14112
14113 /* If the language does not allow nested subprograms (either inside
14114 subprograms or lexical blocks), we're done. */
14115 if (cu->language != language_ada)
14116 return;
6e70227d 14117
b084d499
JB
14118 /* Check all the children of the given DIE. If it contains nested
14119 subprograms, then check their pc bounds. Likewise, we need to
14120 check lexical blocks as well, as they may also contain subprogram
14121 definitions. */
14122 while (child && child->tag)
14123 {
14124 if (child->tag == DW_TAG_subprogram
14125 || child->tag == DW_TAG_lexical_block)
14126 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14127 child = child->sibling;
b084d499
JB
14128 }
14129}
14130
fae299cd
DC
14131/* Get the low and high pc's represented by the scope DIE, and store
14132 them in *LOWPC and *HIGHPC. If the correct values can't be
14133 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14134
14135static void
14136get_scope_pc_bounds (struct die_info *die,
14137 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14138 struct dwarf2_cu *cu)
14139{
14140 CORE_ADDR best_low = (CORE_ADDR) -1;
14141 CORE_ADDR best_high = (CORE_ADDR) 0;
14142 CORE_ADDR current_low, current_high;
14143
3a2b436a 14144 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14145 >= PC_BOUNDS_RANGES)
fae299cd
DC
14146 {
14147 best_low = current_low;
14148 best_high = current_high;
14149 }
14150 else
14151 {
14152 struct die_info *child = die->child;
14153
14154 while (child && child->tag)
14155 {
14156 switch (child->tag) {
14157 case DW_TAG_subprogram:
b084d499 14158 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14159 break;
14160 case DW_TAG_namespace:
f55ee35c 14161 case DW_TAG_module:
fae299cd
DC
14162 /* FIXME: carlton/2004-01-16: Should we do this for
14163 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14164 that current GCC's always emit the DIEs corresponding
14165 to definitions of methods of classes as children of a
14166 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14167 the DIEs giving the declarations, which could be
14168 anywhere). But I don't see any reason why the
14169 standards says that they have to be there. */
14170 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14171
14172 if (current_low != ((CORE_ADDR) -1))
14173 {
325fac50
PA
14174 best_low = std::min (best_low, current_low);
14175 best_high = std::max (best_high, current_high);
fae299cd
DC
14176 }
14177 break;
14178 default:
0963b4bd 14179 /* Ignore. */
fae299cd
DC
14180 break;
14181 }
14182
436c571c 14183 child = child->sibling;
fae299cd
DC
14184 }
14185 }
14186
14187 *lowpc = best_low;
14188 *highpc = best_high;
14189}
14190
801e3a5b
JB
14191/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14192 in DIE. */
380bca97 14193
801e3a5b
JB
14194static void
14195dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14196 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14197{
5e22e966 14198 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14199 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14200 struct attribute *attr;
91da1414 14201 struct attribute *attr_high;
801e3a5b 14202
91da1414
MW
14203 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14204 if (attr_high)
801e3a5b 14205 {
801e3a5b 14206 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14207 if (attr != nullptr)
801e3a5b 14208 {
cd6c91b4
TT
14209 CORE_ADDR low = attr->value_as_address ();
14210 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14211
cd6c91b4 14212 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14213 high += low;
9a619af0 14214
3e29f34a
MR
14215 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14216 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14217 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14218 }
14219 }
14220
14221 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14222 if (attr != nullptr)
801e3a5b 14223 {
18a8505e 14224 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14225 We take advantage of the fact that DW_AT_ranges does not appear
14226 in DW_TAG_compile_unit of DWO files. */
14227 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14228
14229 /* The value of the DW_AT_ranges attribute is the offset of the
14230 address range list in the .debug_ranges section. */
ab435259
DE
14231 unsigned long offset = (DW_UNSND (attr)
14232 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14233
2d5f09ec 14234 std::vector<blockrange> blockvec;
5f46c5a5
JK
14235 dwarf2_ranges_process (offset, cu,
14236 [&] (CORE_ADDR start, CORE_ADDR end)
14237 {
58fdfd2c
JK
14238 start += baseaddr;
14239 end += baseaddr;
5f46c5a5
JK
14240 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14241 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14242 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14243 blockvec.emplace_back (start, end);
5f46c5a5 14244 });
2d5f09ec
KB
14245
14246 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14247 }
14248}
14249
685b1105
JK
14250/* Check whether the producer field indicates either of GCC < 4.6, or the
14251 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14252
685b1105
JK
14253static void
14254check_producer (struct dwarf2_cu *cu)
60d5a603 14255{
38360086 14256 int major, minor;
60d5a603
JK
14257
14258 if (cu->producer == NULL)
14259 {
14260 /* For unknown compilers expect their behavior is DWARF version
14261 compliant.
14262
14263 GCC started to support .debug_types sections by -gdwarf-4 since
14264 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14265 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14266 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14267 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14268 }
b1ffba5a 14269 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14270 {
38360086
MW
14271 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14272 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14273 }
5230b05a 14274 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14275 {
14276 cu->producer_is_icc = true;
14277 cu->producer_is_icc_lt_14 = major < 14;
14278 }
c258c396
JD
14279 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14280 cu->producer_is_codewarrior = true;
685b1105
JK
14281 else
14282 {
14283 /* For other non-GCC compilers, expect their behavior is DWARF version
14284 compliant. */
60d5a603
JK
14285 }
14286
9068261f 14287 cu->checked_producer = true;
685b1105 14288}
ba919b58 14289
685b1105
JK
14290/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14291 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14292 during 4.6.0 experimental. */
14293
9068261f 14294static bool
685b1105
JK
14295producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14296{
14297 if (!cu->checked_producer)
14298 check_producer (cu);
14299
14300 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14301}
14302
c258c396
JD
14303
14304/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14305 with incorrect is_stmt attributes. */
14306
14307static bool
14308producer_is_codewarrior (struct dwarf2_cu *cu)
14309{
14310 if (!cu->checked_producer)
14311 check_producer (cu);
14312
14313 return cu->producer_is_codewarrior;
14314}
14315
405feb71 14316/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14317 DW_AT_accessibility. */
14318
14319static enum dwarf_access_attribute
14320dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14321{
14322 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14323 {
14324 /* The default DWARF 2 accessibility for members is public, the default
14325 accessibility for inheritance is private. */
14326
14327 if (die->tag != DW_TAG_inheritance)
14328 return DW_ACCESS_public;
14329 else
14330 return DW_ACCESS_private;
14331 }
14332 else
14333 {
14334 /* DWARF 3+ defines the default accessibility a different way. The same
14335 rules apply now for DW_TAG_inheritance as for the members and it only
14336 depends on the container kind. */
14337
14338 if (die->parent->tag == DW_TAG_class_type)
14339 return DW_ACCESS_private;
14340 else
14341 return DW_ACCESS_public;
14342 }
14343}
14344
74ac6d43
TT
14345/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14346 offset. If the attribute was not found return 0, otherwise return
14347 1. If it was found but could not properly be handled, set *OFFSET
14348 to 0. */
14349
14350static int
14351handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14352 LONGEST *offset)
14353{
14354 struct attribute *attr;
14355
14356 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14357 if (attr != NULL)
14358 {
14359 *offset = 0;
14360
14361 /* Note that we do not check for a section offset first here.
14362 This is because DW_AT_data_member_location is new in DWARF 4,
14363 so if we see it, we can assume that a constant form is really
14364 a constant and not a section offset. */
cd6c91b4 14365 if (attr->form_is_constant ())
0826b30a 14366 *offset = attr->constant_value (0);
cd6c91b4 14367 else if (attr->form_is_section_offset ())
74ac6d43 14368 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14369 else if (attr->form_is_block ())
74ac6d43
TT
14370 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14371 else
14372 dwarf2_complex_location_expr_complaint ();
14373
14374 return 1;
14375 }
14376
14377 return 0;
14378}
14379
7d79de9a
TT
14380/* Look for DW_AT_data_member_location and store the results in FIELD. */
14381
14382static void
14383handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14384 struct field *field)
14385{
14386 struct attribute *attr;
14387
14388 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14389 if (attr != NULL)
14390 {
14391 if (attr->form_is_constant ())
14392 {
14393 LONGEST offset = attr->constant_value (0);
14394 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14395 }
14396 else if (attr->form_is_section_offset ())
14397 dwarf2_complex_location_expr_complaint ();
14398 else if (attr->form_is_block ())
14399 {
14400 bool handled;
14401 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14402 if (handled)
14403 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14404 else
14405 {
5e22e966
SM
14406 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14407 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14408 struct dwarf2_locexpr_baton *dlbaton
14409 = XOBNEW (&objfile->objfile_obstack,
14410 struct dwarf2_locexpr_baton);
14411 dlbaton->data = DW_BLOCK (attr)->data;
14412 dlbaton->size = DW_BLOCK (attr)->size;
14413 /* When using this baton, we want to compute the address
14414 of the field, not the value. This is why
14415 is_reference is set to false here. */
14416 dlbaton->is_reference = false;
5e22e966 14417 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14418 dlbaton->per_cu = cu->per_cu;
14419
14420 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14421 }
14422 }
14423 else
14424 dwarf2_complex_location_expr_complaint ();
14425 }
14426}
14427
c906108c
SS
14428/* Add an aggregate field to the field list. */
14429
14430static void
107d2387 14431dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14432 struct dwarf2_cu *cu)
6e70227d 14433{
5e22e966 14434 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14435 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14436 struct nextfield *new_field;
14437 struct attribute *attr;
14438 struct field *fp;
15d034d0 14439 const char *fieldname = "";
c906108c 14440
7d0ccb61
DJ
14441 if (die->tag == DW_TAG_inheritance)
14442 {
be2daae6
TT
14443 fip->baseclasses.emplace_back ();
14444 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14445 }
14446 else
14447 {
be2daae6
TT
14448 fip->fields.emplace_back ();
14449 new_field = &fip->fields.back ();
7d0ccb61 14450 }
be2daae6 14451
9c6a1327
TT
14452 new_field->offset = die->sect_off;
14453
e142c38c 14454 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14455 if (attr != nullptr)
c906108c 14456 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14457 else
14458 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14459 if (new_field->accessibility != DW_ACCESS_public)
14460 fip->non_public_fields = 1;
60d5a603 14461
e142c38c 14462 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14463 if (attr != nullptr)
c906108c 14464 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14465 else
14466 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14467
14468 fp = &new_field->field;
a9a9bd0f 14469
e142c38c 14470 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14471 {
a9a9bd0f 14472 /* Data member other than a C++ static data member. */
6e70227d 14473
c906108c 14474 /* Get type of field. */
e7c27a73 14475 fp->type = die_type (die, cu);
c906108c 14476
d6a843b5 14477 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14478
c906108c 14479 /* Get bit size of field (zero if none). */
e142c38c 14480 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14481 if (attr != nullptr)
c906108c
SS
14482 {
14483 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14484 }
14485 else
14486 {
14487 FIELD_BITSIZE (*fp) = 0;
14488 }
14489
14490 /* Get bit offset of field. */
7d79de9a 14491 handle_data_member_location (die, cu, fp);
e142c38c 14492 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14493 if (attr != nullptr)
c906108c 14494 {
d5a22e77 14495 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14496 {
14497 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14498 additional bit offset from the MSB of the containing
14499 anonymous object to the MSB of the field. We don't
14500 have to do anything special since we don't need to
14501 know the size of the anonymous object. */
f41f5e61 14502 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14503 }
14504 else
14505 {
14506 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14507 MSB of the anonymous object, subtract off the number of
14508 bits from the MSB of the field to the MSB of the
14509 object, and then subtract off the number of bits of
14510 the field itself. The result is the bit offset of
14511 the LSB of the field. */
c906108c
SS
14512 int anonymous_size;
14513 int bit_offset = DW_UNSND (attr);
14514
e142c38c 14515 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14516 if (attr != nullptr)
c906108c
SS
14517 {
14518 /* The size of the anonymous object containing
14519 the bit field is explicit, so use the
14520 indicated size (in bytes). */
14521 anonymous_size = DW_UNSND (attr);
14522 }
14523 else
14524 {
14525 /* The size of the anonymous object containing
14526 the bit field must be inferred from the type
14527 attribute of the data member containing the
14528 bit field. */
14529 anonymous_size = TYPE_LENGTH (fp->type);
14530 }
f41f5e61
PA
14531 SET_FIELD_BITPOS (*fp,
14532 (FIELD_BITPOS (*fp)
14533 + anonymous_size * bits_per_byte
14534 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14535 }
14536 }
da5b30da
AA
14537 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14538 if (attr != NULL)
14539 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14540 + attr->constant_value (0)));
c906108c
SS
14541
14542 /* Get name of field. */
39cbfefa
DJ
14543 fieldname = dwarf2_name (die, cu);
14544 if (fieldname == NULL)
14545 fieldname = "";
d8151005
DJ
14546
14547 /* The name is already allocated along with this objfile, so we don't
14548 need to duplicate it for the type. */
14549 fp->name = fieldname;
c906108c
SS
14550
14551 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14552 pointer or virtual base class pointer) to private. */
e142c38c 14553 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14554 {
d48cc9dd 14555 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14556 new_field->accessibility = DW_ACCESS_private;
14557 fip->non_public_fields = 1;
14558 }
14559 }
a9a9bd0f 14560 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14561 {
a9a9bd0f
DC
14562 /* C++ static member. */
14563
14564 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14565 is a declaration, but all versions of G++ as of this writing
14566 (so through at least 3.2.1) incorrectly generate
14567 DW_TAG_variable tags. */
6e70227d 14568
ff355380 14569 const char *physname;
c906108c 14570
a9a9bd0f 14571 /* Get name of field. */
39cbfefa
DJ
14572 fieldname = dwarf2_name (die, cu);
14573 if (fieldname == NULL)
c906108c
SS
14574 return;
14575
254e6b9e 14576 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14577 if (attr
14578 /* Only create a symbol if this is an external value.
14579 new_symbol checks this and puts the value in the global symbol
14580 table, which we want. If it is not external, new_symbol
14581 will try to put the value in cu->list_in_scope which is wrong. */
14582 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14583 {
14584 /* A static const member, not much different than an enum as far as
14585 we're concerned, except that we can support more types. */
14586 new_symbol (die, NULL, cu);
14587 }
14588
2df3850c 14589 /* Get physical name. */
ff355380 14590 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14591
d8151005
DJ
14592 /* The name is already allocated along with this objfile, so we don't
14593 need to duplicate it for the type. */
14594 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14595 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14596 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14597 }
14598 else if (die->tag == DW_TAG_inheritance)
14599 {
74ac6d43 14600 /* C++ base class field. */
7d79de9a 14601 handle_data_member_location (die, cu, fp);
c906108c 14602 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14603 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14604 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14605 }
2ddeaf8a
TT
14606 else
14607 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14608}
14609
883fd55a
KS
14610/* Can the type given by DIE define another type? */
14611
14612static bool
14613type_can_define_types (const struct die_info *die)
14614{
14615 switch (die->tag)
14616 {
14617 case DW_TAG_typedef:
14618 case DW_TAG_class_type:
14619 case DW_TAG_structure_type:
14620 case DW_TAG_union_type:
14621 case DW_TAG_enumeration_type:
14622 return true;
14623
14624 default:
14625 return false;
14626 }
14627}
14628
14629/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14630
14631static void
883fd55a
KS
14632dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14633 struct dwarf2_cu *cu)
6e70227d 14634{
be2daae6
TT
14635 struct decl_field fp;
14636 memset (&fp, 0, sizeof (fp));
98751a41 14637
883fd55a 14638 gdb_assert (type_can_define_types (die));
98751a41 14639
883fd55a 14640 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14641 fp.name = dwarf2_name (die, cu);
14642 fp.type = read_type_die (die, cu);
98751a41 14643
c191a687
KS
14644 /* Save accessibility. */
14645 enum dwarf_access_attribute accessibility;
14646 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14647 if (attr != NULL)
14648 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14649 else
14650 accessibility = dwarf2_default_access_attribute (die, cu);
14651 switch (accessibility)
14652 {
14653 case DW_ACCESS_public:
14654 /* The assumed value if neither private nor protected. */
14655 break;
14656 case DW_ACCESS_private:
be2daae6 14657 fp.is_private = 1;
c191a687
KS
14658 break;
14659 case DW_ACCESS_protected:
be2daae6 14660 fp.is_protected = 1;
c191a687
KS
14661 break;
14662 default:
b98664d3 14663 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14664 }
14665
883fd55a 14666 if (die->tag == DW_TAG_typedef)
be2daae6 14667 fip->typedef_field_list.push_back (fp);
883fd55a 14668 else
be2daae6 14669 fip->nested_types_list.push_back (fp);
98751a41
JK
14670}
14671
9c6a1327
TT
14672/* A convenience typedef that's used when finding the discriminant
14673 field for a variant part. */
1b95cdb7
SM
14674typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14675 offset_map_type;
9c6a1327
TT
14676
14677/* Compute the discriminant range for a given variant. OBSTACK is
14678 where the results will be stored. VARIANT is the variant to
14679 process. IS_UNSIGNED indicates whether the discriminant is signed
14680 or unsigned. */
14681
14682static const gdb::array_view<discriminant_range>
14683convert_variant_range (struct obstack *obstack, const variant_field &variant,
14684 bool is_unsigned)
14685{
14686 std::vector<discriminant_range> ranges;
14687
14688 if (variant.default_branch)
14689 return {};
14690
14691 if (variant.discr_list_data == nullptr)
14692 {
14693 discriminant_range r
14694 = {variant.discriminant_value, variant.discriminant_value};
14695 ranges.push_back (r);
14696 }
14697 else
14698 {
14699 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14700 variant.discr_list_data->size);
14701 while (!data.empty ())
14702 {
14703 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14704 {
14705 complaint (_("invalid discriminant marker: %d"), data[0]);
14706 break;
14707 }
14708 bool is_range = data[0] == DW_DSC_range;
14709 data = data.slice (1);
14710
14711 ULONGEST low, high;
14712 unsigned int bytes_read;
14713
14714 if (data.empty ())
14715 {
14716 complaint (_("DW_AT_discr_list missing low value"));
14717 break;
14718 }
14719 if (is_unsigned)
14720 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14721 else
14722 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14723 &bytes_read);
14724 data = data.slice (bytes_read);
14725
14726 if (is_range)
14727 {
14728 if (data.empty ())
14729 {
14730 complaint (_("DW_AT_discr_list missing high value"));
14731 break;
14732 }
14733 if (is_unsigned)
14734 high = read_unsigned_leb128 (nullptr, data.data (),
14735 &bytes_read);
14736 else
14737 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14738 &bytes_read);
14739 data = data.slice (bytes_read);
14740 }
14741 else
14742 high = low;
14743
14744 ranges.push_back ({ low, high });
14745 }
14746 }
14747
14748 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14749 ranges.size ());
14750 std::copy (ranges.begin (), ranges.end (), result);
14751 return gdb::array_view<discriminant_range> (result, ranges.size ());
14752}
14753
14754static const gdb::array_view<variant_part> create_variant_parts
14755 (struct obstack *obstack,
14756 const offset_map_type &offset_map,
14757 struct field_info *fi,
14758 const std::vector<variant_part_builder> &variant_parts);
14759
14760/* Fill in a "struct variant" for a given variant field. RESULT is
14761 the variant to fill in. OBSTACK is where any needed allocations
14762 will be done. OFFSET_MAP holds the mapping from section offsets to
14763 fields for the type. FI describes the fields of the type we're
14764 processing. FIELD is the variant field we're converting. */
14765
14766static void
14767create_one_variant (variant &result, struct obstack *obstack,
14768 const offset_map_type &offset_map,
14769 struct field_info *fi, const variant_field &field)
14770{
14771 result.discriminants = convert_variant_range (obstack, field, false);
14772 result.first_field = field.first_field + fi->baseclasses.size ();
14773 result.last_field = field.last_field + fi->baseclasses.size ();
14774 result.parts = create_variant_parts (obstack, offset_map, fi,
14775 field.variant_parts);
14776}
14777
14778/* Fill in a "struct variant_part" for a given variant part. RESULT
14779 is the variant part to fill in. OBSTACK is where any needed
14780 allocations will be done. OFFSET_MAP holds the mapping from
14781 section offsets to fields for the type. FI describes the fields of
14782 the type we're processing. BUILDER is the variant part to be
14783 converted. */
14784
14785static void
14786create_one_variant_part (variant_part &result,
14787 struct obstack *obstack,
14788 const offset_map_type &offset_map,
14789 struct field_info *fi,
14790 const variant_part_builder &builder)
14791{
14792 auto iter = offset_map.find (builder.discriminant_offset);
14793 if (iter == offset_map.end ())
14794 {
14795 result.discriminant_index = -1;
14796 /* Doesn't matter. */
14797 result.is_unsigned = false;
14798 }
14799 else
14800 {
14801 result.discriminant_index = iter->second;
14802 result.is_unsigned
14803 = TYPE_UNSIGNED (FIELD_TYPE
14804 (fi->fields[result.discriminant_index].field));
14805 }
14806
14807 size_t n = builder.variants.size ();
14808 variant *output = new (obstack) variant[n];
14809 for (size_t i = 0; i < n; ++i)
14810 create_one_variant (output[i], obstack, offset_map, fi,
14811 builder.variants[i]);
14812
14813 result.variants = gdb::array_view<variant> (output, n);
14814}
14815
14816/* Create a vector of variant parts that can be attached to a type.
14817 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14818 holds the mapping from section offsets to fields for the type. FI
14819 describes the fields of the type we're processing. VARIANT_PARTS
14820 is the vector to convert. */
14821
14822static const gdb::array_view<variant_part>
14823create_variant_parts (struct obstack *obstack,
14824 const offset_map_type &offset_map,
14825 struct field_info *fi,
14826 const std::vector<variant_part_builder> &variant_parts)
14827{
14828 if (variant_parts.empty ())
14829 return {};
14830
14831 size_t n = variant_parts.size ();
14832 variant_part *result = new (obstack) variant_part[n];
14833 for (size_t i = 0; i < n; ++i)
14834 create_one_variant_part (result[i], obstack, offset_map, fi,
14835 variant_parts[i]);
14836
14837 return gdb::array_view<variant_part> (result, n);
14838}
14839
14840/* Compute the variant part vector for FIP, attaching it to TYPE when
14841 done. */
14842
14843static void
14844add_variant_property (struct field_info *fip, struct type *type,
14845 struct dwarf2_cu *cu)
14846{
14847 /* Map section offsets of fields to their field index. Note the
14848 field index here does not take the number of baseclasses into
14849 account. */
14850 offset_map_type offset_map;
14851 for (int i = 0; i < fip->fields.size (); ++i)
14852 offset_map[fip->fields[i].offset] = i;
14853
5e22e966 14854 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14855 gdb::array_view<variant_part> parts
14856 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14857 fip->variant_parts);
14858
14859 struct dynamic_prop prop;
14860 prop.kind = PROP_VARIANT_PARTS;
14861 prop.data.variant_parts
14862 = ((gdb::array_view<variant_part> *)
14863 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14864
5c54719c 14865 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14866}
14867
c906108c
SS
14868/* Create the vector of fields, and attach it to the type. */
14869
14870static void
fba45db2 14871dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14872 struct dwarf2_cu *cu)
c906108c 14873{
317f7127 14874 int nfields = fip->nfields ();
c906108c
SS
14875
14876 /* Record the field count, allocate space for the array of fields,
14877 and create blank accessibility bitfields if necessary. */
5e33d5f4 14878 type->set_num_fields (nfields);
3cabb6b0
SM
14879 type->set_fields
14880 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14881
b4ba55a1 14882 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14883 {
14884 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14885
14886 TYPE_FIELD_PRIVATE_BITS (type) =
14887 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14888 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14889
14890 TYPE_FIELD_PROTECTED_BITS (type) =
14891 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14892 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14893
774b6a14
TT
14894 TYPE_FIELD_IGNORE_BITS (type) =
14895 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14896 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14897 }
14898
14899 /* If the type has baseclasses, allocate and clear a bit vector for
14900 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14901 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14902 {
be2daae6 14903 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14904 unsigned char *pointer;
c906108c
SS
14905
14906 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14907 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14908 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14909 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14910 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14911 }
14912
9c6a1327
TT
14913 if (!fip->variant_parts.empty ())
14914 add_variant_property (fip, type, cu);
2ddeaf8a 14915
be2daae6
TT
14916 /* Copy the saved-up fields into the field vector. */
14917 for (int i = 0; i < nfields; ++i)
c906108c 14918 {
be2daae6
TT
14919 struct nextfield &field
14920 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14921 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14922
ceacbf6e 14923 type->field (i) = field.field;
be2daae6 14924 switch (field.accessibility)
c906108c 14925 {
c5aa993b 14926 case DW_ACCESS_private:
b4ba55a1 14927 if (cu->language != language_ada)
be2daae6 14928 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14929 break;
c906108c 14930
c5aa993b 14931 case DW_ACCESS_protected:
b4ba55a1 14932 if (cu->language != language_ada)
be2daae6 14933 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14934 break;
c906108c 14935
c5aa993b
JM
14936 case DW_ACCESS_public:
14937 break;
c906108c 14938
c5aa993b
JM
14939 default:
14940 /* Unknown accessibility. Complain and treat it as public. */
14941 {
b98664d3 14942 complaint (_("unsupported accessibility %d"),
be2daae6 14943 field.accessibility);
c5aa993b
JM
14944 }
14945 break;
c906108c 14946 }
be2daae6 14947 if (i < fip->baseclasses.size ())
c906108c 14948 {
be2daae6 14949 switch (field.virtuality)
c906108c 14950 {
c5aa993b
JM
14951 case DW_VIRTUALITY_virtual:
14952 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14953 if (cu->language == language_ada)
a73c6dcd 14954 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14955 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14956 break;
c906108c
SS
14957 }
14958 }
c906108c
SS
14959 }
14960}
14961
7d27a96d
TT
14962/* Return true if this member function is a constructor, false
14963 otherwise. */
14964
14965static int
14966dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14967{
14968 const char *fieldname;
fe978cb0 14969 const char *type_name;
7d27a96d
TT
14970 int len;
14971
14972 if (die->parent == NULL)
14973 return 0;
14974
14975 if (die->parent->tag != DW_TAG_structure_type
14976 && die->parent->tag != DW_TAG_union_type
14977 && die->parent->tag != DW_TAG_class_type)
14978 return 0;
14979
14980 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14981 type_name = dwarf2_name (die->parent, cu);
14982 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14983 return 0;
14984
14985 len = strlen (fieldname);
fe978cb0
PA
14986 return (strncmp (fieldname, type_name, len) == 0
14987 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14988}
14989
e35000a7
TBA
14990/* Check if the given VALUE is a recognized enum
14991 dwarf_defaulted_attribute constant according to DWARF5 spec,
14992 Table 7.24. */
14993
14994static bool
14995is_valid_DW_AT_defaulted (ULONGEST value)
14996{
14997 switch (value)
14998 {
14999 case DW_DEFAULTED_no:
15000 case DW_DEFAULTED_in_class:
15001 case DW_DEFAULTED_out_of_class:
15002 return true;
15003 }
15004
3142e908 15005 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15006 return false;
15007}
15008
c906108c
SS
15009/* Add a member function to the proper fieldlist. */
15010
15011static void
107d2387 15012dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15013 struct type *type, struct dwarf2_cu *cu)
c906108c 15014{
5e22e966 15015 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15016 struct attribute *attr;
c906108c 15017 int i;
be2daae6 15018 struct fnfieldlist *flp = nullptr;
c906108c 15019 struct fn_field *fnp;
15d034d0 15020 const char *fieldname;
f792889a 15021 struct type *this_type;
60d5a603 15022 enum dwarf_access_attribute accessibility;
c906108c 15023
b4ba55a1 15024 if (cu->language == language_ada)
a73c6dcd 15025 error (_("unexpected member function in Ada type"));
b4ba55a1 15026
2df3850c 15027 /* Get name of member function. */
39cbfefa
DJ
15028 fieldname = dwarf2_name (die, cu);
15029 if (fieldname == NULL)
2df3850c 15030 return;
c906108c 15031
c906108c 15032 /* Look up member function name in fieldlist. */
be2daae6 15033 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15034 {
27bfe10e 15035 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15036 {
15037 flp = &fip->fnfieldlists[i];
15038 break;
15039 }
c906108c
SS
15040 }
15041
be2daae6
TT
15042 /* Create a new fnfieldlist if necessary. */
15043 if (flp == nullptr)
c906108c 15044 {
be2daae6
TT
15045 fip->fnfieldlists.emplace_back ();
15046 flp = &fip->fnfieldlists.back ();
c906108c 15047 flp->name = fieldname;
be2daae6 15048 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15049 }
15050
be2daae6
TT
15051 /* Create a new member function field and add it to the vector of
15052 fnfieldlists. */
15053 flp->fnfields.emplace_back ();
15054 fnp = &flp->fnfields.back ();
3da10d80
KS
15055
15056 /* Delay processing of the physname until later. */
9c37b5ae 15057 if (cu->language == language_cplus)
be2daae6
TT
15058 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15059 die, cu);
3da10d80
KS
15060 else
15061 {
1d06ead6 15062 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15063 fnp->physname = physname ? physname : "";
15064 }
15065
c906108c 15066 fnp->type = alloc_type (objfile);
f792889a 15067 this_type = read_type_die (die, cu);
78134374 15068 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15069 {
1f704f76 15070 int nparams = this_type->num_fields ();
c906108c 15071
f792889a 15072 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15073 of the method itself (TYPE_CODE_METHOD). */
15074 smash_to_method_type (fnp->type, type,
f792889a 15075 TYPE_TARGET_TYPE (this_type),
80fc5e77 15076 this_type->fields (),
1f704f76 15077 this_type->num_fields (),
f792889a 15078 TYPE_VARARGS (this_type));
c906108c
SS
15079
15080 /* Handle static member functions.
c5aa993b 15081 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15082 member functions. G++ helps GDB by marking the first
15083 parameter for non-static member functions (which is the this
15084 pointer) as artificial. We obtain this information from
15085 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15086 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15087 fnp->voffset = VOFFSET_STATIC;
15088 }
15089 else
b98664d3 15090 complaint (_("member function type missing for '%s'"),
3da10d80 15091 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15092
15093 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15094 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15095 fnp->fcontext = die_containing_type (die, cu);
c906108c 15096
3e43a32a
MS
15097 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15098 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15099
15100 /* Get accessibility. */
e142c38c 15101 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15102 if (attr != nullptr)
aead7601 15103 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15104 else
15105 accessibility = dwarf2_default_access_attribute (die, cu);
15106 switch (accessibility)
c906108c 15107 {
60d5a603
JK
15108 case DW_ACCESS_private:
15109 fnp->is_private = 1;
15110 break;
15111 case DW_ACCESS_protected:
15112 fnp->is_protected = 1;
15113 break;
c906108c
SS
15114 }
15115
b02dede2 15116 /* Check for artificial methods. */
e142c38c 15117 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15118 if (attr && DW_UNSND (attr) != 0)
15119 fnp->is_artificial = 1;
15120
e35000a7
TBA
15121 /* Check for defaulted methods. */
15122 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15123 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15124 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15125
15126 /* Check for deleted methods. */
15127 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15128 if (attr != nullptr && DW_UNSND (attr) != 0)
15129 fnp->is_deleted = 1;
15130
7d27a96d
TT
15131 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15132
0d564a31 15133 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15134 function. For older versions of GCC, this is an offset in the
15135 appropriate virtual table, as specified by DW_AT_containing_type.
15136 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15137 to the object address. */
15138
e142c38c 15139 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15140 if (attr != nullptr)
8e19ed76 15141 {
4fc6c0d5 15142 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15143 {
aec5aa8b
TT
15144 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15145 {
15146 /* Old-style GCC. */
15147 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15148 }
15149 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15150 || (DW_BLOCK (attr)->size > 1
15151 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15152 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15153 {
aec5aa8b
TT
15154 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15155 if ((fnp->voffset % cu->header.addr_size) != 0)
15156 dwarf2_complex_location_expr_complaint ();
15157 else
15158 fnp->voffset /= cu->header.addr_size;
15159 fnp->voffset += 2;
15160 }
15161 else
15162 dwarf2_complex_location_expr_complaint ();
15163
15164 if (!fnp->fcontext)
7e993ebf
KS
15165 {
15166 /* If there is no `this' field and no DW_AT_containing_type,
15167 we cannot actually find a base class context for the
15168 vtable! */
1f704f76 15169 if (this_type->num_fields () == 0
7e993ebf
KS
15170 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15171 {
b98664d3 15172 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15173 "function \"%s\" (offset %s)"),
15174 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15175 }
15176 else
15177 {
15178 fnp->fcontext
15179 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15180 }
15181 }
aec5aa8b 15182 }
cd6c91b4 15183 else if (attr->form_is_section_offset ())
8e19ed76 15184 {
4d3c2250 15185 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15186 }
15187 else
15188 {
4d3c2250
KB
15189 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15190 fieldname);
8e19ed76 15191 }
0d564a31 15192 }
d48cc9dd
DJ
15193 else
15194 {
15195 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15196 if (attr && DW_UNSND (attr))
15197 {
15198 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15199 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15200 "but the vtable offset is not specified"),
9d8780f0 15201 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15202 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15203 TYPE_CPLUS_DYNAMIC (type) = 1;
15204 }
15205 }
c906108c
SS
15206}
15207
15208/* Create the vector of member function fields, and attach it to the type. */
15209
15210static void
fba45db2 15211dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15212 struct dwarf2_cu *cu)
c906108c 15213{
b4ba55a1 15214 if (cu->language == language_ada)
a73c6dcd 15215 error (_("unexpected member functions in Ada type"));
b4ba55a1 15216
c906108c
SS
15217 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15218 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15219 TYPE_ALLOC (type,
15220 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15221
be2daae6 15222 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15223 {
be2daae6 15224 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15225 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15226
be2daae6
TT
15227 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15228 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15229 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15230 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15231
15232 for (int k = 0; k < nf.fnfields.size (); ++k)
15233 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15234 }
15235
be2daae6 15236 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15237}
15238
1168df01
JB
15239/* Returns non-zero if NAME is the name of a vtable member in CU's
15240 language, zero otherwise. */
15241static int
15242is_vtable_name (const char *name, struct dwarf2_cu *cu)
15243{
15244 static const char vptr[] = "_vptr";
15245
9c37b5ae
TT
15246 /* Look for the C++ form of the vtable. */
15247 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15248 return 1;
15249
15250 return 0;
15251}
15252
c0dd20ea 15253/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15254 functions, with the ABI-specified layout. If TYPE describes
15255 such a structure, smash it into a member function type.
61049d3b
DJ
15256
15257 GCC shouldn't do this; it should just output pointer to member DIEs.
15258 This is GCC PR debug/28767. */
c0dd20ea 15259
0b92b5bb
TT
15260static void
15261quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15262{
09e2d7c7 15263 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15264
15265 /* Check for a structure with no name and two children. */
1f704f76 15266 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15267 return;
c0dd20ea
DJ
15268
15269 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15270 if (TYPE_FIELD_NAME (type, 0) == NULL
15271 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15272 || TYPE_FIELD_NAME (type, 1) == NULL
15273 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15274 return;
c0dd20ea
DJ
15275
15276 /* Find the type of the method. */
0b92b5bb 15277 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15278 if (pfn_type == NULL
78134374
SM
15279 || pfn_type->code () != TYPE_CODE_PTR
15280 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15281 return;
c0dd20ea
DJ
15282
15283 /* Look for the "this" argument. */
15284 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15285 if (pfn_type->num_fields () == 0
0b92b5bb 15286 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15287 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15288 return;
c0dd20ea 15289
09e2d7c7 15290 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15291 new_type = alloc_type (objfile);
09e2d7c7 15292 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15293 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15294 TYPE_VARARGS (pfn_type));
0b92b5bb 15295 smash_to_methodptr_type (type, new_type);
c0dd20ea 15296}
1168df01 15297
2b4424c3
TT
15298/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15299 appropriate error checking and issuing complaints if there is a
15300 problem. */
15301
15302static ULONGEST
15303get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15304{
15305 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15306
15307 if (attr == nullptr)
15308 return 0;
15309
cd6c91b4 15310 if (!attr->form_is_constant ())
2b4424c3 15311 {
b98664d3 15312 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15313 " - DIE at %s [in module %s]"),
15314 sect_offset_str (die->sect_off),
5e22e966 15315 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15316 return 0;
15317 }
15318
15319 ULONGEST align;
15320 if (attr->form == DW_FORM_sdata)
15321 {
15322 LONGEST val = DW_SND (attr);
15323 if (val < 0)
15324 {
b98664d3 15325 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15326 " - DIE at %s [in module %s]"),
15327 sect_offset_str (die->sect_off),
5e22e966 15328 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15329 return 0;
15330 }
15331 align = val;
15332 }
15333 else
15334 align = DW_UNSND (attr);
15335
15336 if (align == 0)
15337 {
b98664d3 15338 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15339 " - DIE at %s [in module %s]"),
15340 sect_offset_str (die->sect_off),
5e22e966 15341 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15342 return 0;
15343 }
15344 if ((align & (align - 1)) != 0)
15345 {
b98664d3 15346 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15347 " - DIE at %s [in module %s]"),
15348 sect_offset_str (die->sect_off),
5e22e966 15349 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15350 return 0;
15351 }
15352
15353 return align;
15354}
15355
15356/* If the DIE has a DW_AT_alignment attribute, use its value to set
15357 the alignment for TYPE. */
15358
15359static void
15360maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15361 struct type *type)
15362{
15363 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15364 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15365 " - DIE at %s [in module %s]"),
15366 sect_offset_str (die->sect_off),
5e22e966 15367 objfile_name (cu->per_objfile->objfile));
2b4424c3 15368}
685b1105 15369
e35000a7
TBA
15370/* Check if the given VALUE is a valid enum dwarf_calling_convention
15371 constant for a type, according to DWARF5 spec, Table 5.5. */
15372
15373static bool
15374is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15375{
15376 switch (value)
15377 {
15378 case DW_CC_normal:
15379 case DW_CC_pass_by_reference:
15380 case DW_CC_pass_by_value:
15381 return true;
15382
15383 default:
15384 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15385 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15386 return false;
15387 }
15388}
15389
d0922fcf
TBA
15390/* Check if the given VALUE is a valid enum dwarf_calling_convention
15391 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15392 also according to GNU-specific values (see include/dwarf2.h). */
15393
15394static bool
15395is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15396{
15397 switch (value)
15398 {
15399 case DW_CC_normal:
15400 case DW_CC_program:
15401 case DW_CC_nocall:
15402 return true;
15403
15404 case DW_CC_GNU_renesas_sh:
15405 case DW_CC_GNU_borland_fastcall_i386:
15406 case DW_CC_GDB_IBM_OpenCL:
15407 return true;
15408
15409 default:
15410 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15411 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15412 return false;
15413 }
15414}
15415
c906108c 15416/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15417 (definition) to create a type for the structure or union. Fill in
15418 the type's name and general properties; the members will not be
83655187
DE
15419 processed until process_structure_scope. A symbol table entry for
15420 the type will also not be done until process_structure_scope (assuming
15421 the type has a name).
c906108c 15422
c767944b
DJ
15423 NOTE: we need to call these functions regardless of whether or not the
15424 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15425 structure or union. This gets the type entered into our set of
83655187 15426 user defined types. */
c906108c 15427
f792889a 15428static struct type *
134d01f1 15429read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15430{
5e22e966 15431 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15432 struct type *type;
15433 struct attribute *attr;
15d034d0 15434 const char *name;
c906108c 15435
348e048f
DE
15436 /* If the definition of this type lives in .debug_types, read that type.
15437 Don't follow DW_AT_specification though, that will take us back up
15438 the chain and we want to go down. */
052c8bb8 15439 attr = die->attr (DW_AT_signature);
435d3d88 15440 if (attr != nullptr)
348e048f 15441 {
ac9ec31b 15442 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15443
ac9ec31b 15444 /* The type's CU may not be the same as CU.
02142a6c 15445 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15446 return set_die_type (die, type, cu);
15447 }
15448
c0dd20ea 15449 type = alloc_type (objfile);
c906108c 15450 INIT_CPLUS_SPECIFIC (type);
93311388 15451
39cbfefa
DJ
15452 name = dwarf2_name (die, cu);
15453 if (name != NULL)
c906108c 15454 {
987504bb 15455 if (cu->language == language_cplus
c44af4eb
TT
15456 || cu->language == language_d
15457 || cu->language == language_rust)
63d06c5c 15458 {
15d034d0 15459 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15460
15461 /* dwarf2_full_name might have already finished building the DIE's
15462 type. If so, there is no need to continue. */
15463 if (get_die_type (die, cu) != NULL)
15464 return get_die_type (die, cu);
15465
d0e39ea2 15466 type->set_name (full_name);
63d06c5c
DC
15467 }
15468 else
15469 {
d8151005
DJ
15470 /* The name is already allocated along with this objfile, so
15471 we don't need to duplicate it for the type. */
d0e39ea2 15472 type->set_name (name);
63d06c5c 15473 }
c906108c
SS
15474 }
15475
15476 if (die->tag == DW_TAG_structure_type)
15477 {
67607e24 15478 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15479 }
15480 else if (die->tag == DW_TAG_union_type)
15481 {
67607e24 15482 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15483 }
15484 else
15485 {
67607e24 15486 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15487 }
15488
0cc2414c
TT
15489 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15490 TYPE_DECLARED_CLASS (type) = 1;
15491
e35000a7
TBA
15492 /* Store the calling convention in the type if it's available in
15493 the die. Otherwise the calling convention remains set to
15494 the default value DW_CC_normal. */
15495 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15496 if (attr != nullptr
15497 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15498 {
15499 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15500 TYPE_CPLUS_CALLING_CONVENTION (type)
15501 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15502 }
15503
e142c38c 15504 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15505 if (attr != nullptr)
c906108c 15506 {
cd6c91b4 15507 if (attr->form_is_constant ())
155bfbd3
JB
15508 TYPE_LENGTH (type) = DW_UNSND (attr);
15509 else
15510 {
f8e89861 15511 struct dynamic_prop prop;
293e7e51 15512 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15513 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15514 TYPE_LENGTH (type) = 0;
15515 }
c906108c
SS
15516 }
15517 else
15518 {
15519 TYPE_LENGTH (type) = 0;
15520 }
15521
2b4424c3
TT
15522 maybe_set_alignment (cu, die, type);
15523
5230b05a 15524 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15525 {
5230b05a
WT
15526 /* ICC<14 does not output the required DW_AT_declaration on
15527 incomplete types, but gives them a size of zero. */
422b1cb0 15528 TYPE_STUB (type) = 1;
685b1105
JK
15529 }
15530 else
15531 TYPE_STUB_SUPPORTED (type) = 1;
15532
dc718098 15533 if (die_is_declaration (die, cu))
876cecd0 15534 TYPE_STUB (type) = 1;
a6c727b2
DJ
15535 else if (attr == NULL && die->child == NULL
15536 && producer_is_realview (cu->producer))
15537 /* RealView does not output the required DW_AT_declaration
15538 on incomplete types. */
15539 TYPE_STUB (type) = 1;
dc718098 15540
c906108c
SS
15541 /* We need to add the type field to the die immediately so we don't
15542 infinitely recurse when dealing with pointers to the structure
0963b4bd 15543 type within the structure itself. */
1c379e20 15544 set_die_type (die, type, cu);
c906108c 15545
7e314c57
JK
15546 /* set_die_type should be already done. */
15547 set_descriptive_type (type, die, cu);
15548
c767944b
DJ
15549 return type;
15550}
15551
9c6a1327
TT
15552static void handle_struct_member_die
15553 (struct die_info *child_die,
15554 struct type *type,
15555 struct field_info *fi,
15556 std::vector<struct symbol *> *template_args,
15557 struct dwarf2_cu *cu);
15558
15559/* A helper for handle_struct_member_die that handles
15560 DW_TAG_variant_part. */
15561
15562static void
15563handle_variant_part (struct die_info *die, struct type *type,
15564 struct field_info *fi,
15565 std::vector<struct symbol *> *template_args,
15566 struct dwarf2_cu *cu)
15567{
15568 variant_part_builder *new_part;
15569 if (fi->current_variant_part == nullptr)
15570 {
15571 fi->variant_parts.emplace_back ();
15572 new_part = &fi->variant_parts.back ();
15573 }
15574 else if (!fi->current_variant_part->processing_variant)
15575 {
15576 complaint (_("nested DW_TAG_variant_part seen "
15577 "- DIE at %s [in module %s]"),
15578 sect_offset_str (die->sect_off),
5e22e966 15579 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15580 return;
15581 }
15582 else
15583 {
15584 variant_field &current = fi->current_variant_part->variants.back ();
15585 current.variant_parts.emplace_back ();
15586 new_part = &current.variant_parts.back ();
15587 }
15588
15589 /* When we recurse, we want callees to add to this new variant
15590 part. */
15591 scoped_restore save_current_variant_part
15592 = make_scoped_restore (&fi->current_variant_part, new_part);
15593
15594 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15595 if (discr == NULL)
15596 {
15597 /* It's a univariant form, an extension we support. */
15598 }
15599 else if (discr->form_is_ref ())
15600 {
15601 struct dwarf2_cu *target_cu = cu;
15602 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15603
15604 new_part->discriminant_offset = target_die->sect_off;
15605 }
15606 else
15607 {
15608 complaint (_("DW_AT_discr does not have DIE reference form"
15609 " - DIE at %s [in module %s]"),
15610 sect_offset_str (die->sect_off),
5e22e966 15611 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15612 }
15613
15614 for (die_info *child_die = die->child;
15615 child_die != NULL;
15616 child_die = child_die->sibling)
15617 handle_struct_member_die (child_die, type, fi, template_args, cu);
15618}
15619
15620/* A helper for handle_struct_member_die that handles
15621 DW_TAG_variant. */
15622
15623static void
15624handle_variant (struct die_info *die, struct type *type,
15625 struct field_info *fi,
15626 std::vector<struct symbol *> *template_args,
15627 struct dwarf2_cu *cu)
15628{
15629 if (fi->current_variant_part == nullptr)
15630 {
15631 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15632 "- DIE at %s [in module %s]"),
15633 sect_offset_str (die->sect_off),
5e22e966 15634 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15635 return;
15636 }
15637 if (fi->current_variant_part->processing_variant)
15638 {
15639 complaint (_("nested DW_TAG_variant seen "
15640 "- DIE at %s [in module %s]"),
15641 sect_offset_str (die->sect_off),
5e22e966 15642 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15643 return;
15644 }
15645
15646 scoped_restore save_processing_variant
15647 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15648 true);
15649
15650 fi->current_variant_part->variants.emplace_back ();
15651 variant_field &variant = fi->current_variant_part->variants.back ();
15652 variant.first_field = fi->fields.size ();
15653
15654 /* In a variant we want to get the discriminant and also add a
15655 field for our sole member child. */
15656 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15657 if (discr == nullptr)
15658 {
15659 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15660 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15661 variant.default_branch = true;
15662 else
15663 variant.discr_list_data = DW_BLOCK (discr);
15664 }
15665 else
15666 variant.discriminant_value = DW_UNSND (discr);
15667
15668 for (die_info *variant_child = die->child;
15669 variant_child != NULL;
15670 variant_child = variant_child->sibling)
15671 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15672
15673 variant.last_field = fi->fields.size ();
15674}
15675
2ddeaf8a
TT
15676/* A helper for process_structure_scope that handles a single member
15677 DIE. */
15678
15679static void
15680handle_struct_member_die (struct die_info *child_die, struct type *type,
15681 struct field_info *fi,
15682 std::vector<struct symbol *> *template_args,
15683 struct dwarf2_cu *cu)
15684{
15685 if (child_die->tag == DW_TAG_member
9c6a1327 15686 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15687 {
15688 /* NOTE: carlton/2002-11-05: A C++ static data member
15689 should be a DW_TAG_member that is a declaration, but
15690 all versions of G++ as of this writing (so through at
15691 least 3.2.1) incorrectly generate DW_TAG_variable
15692 tags for them instead. */
15693 dwarf2_add_field (fi, child_die, cu);
15694 }
15695 else if (child_die->tag == DW_TAG_subprogram)
15696 {
15697 /* Rust doesn't have member functions in the C++ sense.
15698 However, it does emit ordinary functions as children
15699 of a struct DIE. */
15700 if (cu->language == language_rust)
15701 read_func_scope (child_die, cu);
15702 else
15703 {
15704 /* C++ member function. */
15705 dwarf2_add_member_fn (fi, child_die, type, cu);
15706 }
15707 }
15708 else if (child_die->tag == DW_TAG_inheritance)
15709 {
15710 /* C++ base class field. */
15711 dwarf2_add_field (fi, child_die, cu);
15712 }
15713 else if (type_can_define_types (child_die))
15714 dwarf2_add_type_defn (fi, child_die, cu);
15715 else if (child_die->tag == DW_TAG_template_type_param
15716 || child_die->tag == DW_TAG_template_value_param)
15717 {
15718 struct symbol *arg = new_symbol (child_die, NULL, cu);
15719
15720 if (arg != NULL)
15721 template_args->push_back (arg);
15722 }
9c6a1327
TT
15723 else if (child_die->tag == DW_TAG_variant_part)
15724 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15725 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15726 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15727}
15728
c767944b
DJ
15729/* Finish creating a structure or union type, including filling in
15730 its members and creating a symbol for it. */
15731
15732static void
15733process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15734{
5e22e966 15735 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15736 struct die_info *child_die;
c767944b
DJ
15737 struct type *type;
15738
15739 type = get_die_type (die, cu);
15740 if (type == NULL)
15741 type = read_structure_type (die, cu);
15742
3e1d3d8c 15743 bool has_template_parameters = false;
e142c38c 15744 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15745 {
15746 struct field_info fi;
2f4732b0 15747 std::vector<struct symbol *> template_args;
c906108c 15748
639d11d3 15749 child_die = die->child;
c906108c
SS
15750
15751 while (child_die && child_die->tag)
15752 {
2ddeaf8a 15753 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15754 child_die = child_die->sibling;
c906108c
SS
15755 }
15756
34eaf542 15757 /* Attach template arguments to type. */
2f4732b0 15758 if (!template_args.empty ())
34eaf542 15759 {
3e1d3d8c 15760 has_template_parameters = true;
34eaf542 15761 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15762 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15763 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15764 = XOBNEWVEC (&objfile->objfile_obstack,
15765 struct symbol *,
15766 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15767 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15768 template_args.data (),
34eaf542
TT
15769 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15770 * sizeof (struct symbol *)));
34eaf542
TT
15771 }
15772
c906108c 15773 /* Attach fields and member functions to the type. */
317f7127 15774 if (fi.nfields () > 0)
e7c27a73 15775 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15776 if (!fi.fnfieldlists.empty ())
c906108c 15777 {
e7c27a73 15778 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15779
c5aa993b 15780 /* Get the type which refers to the base class (possibly this
c906108c 15781 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15782 class from the DW_AT_containing_type attribute. This use of
15783 DW_AT_containing_type is a GNU extension. */
c906108c 15784
e142c38c 15785 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15786 {
e7c27a73 15787 struct type *t = die_containing_type (die, cu);
c906108c 15788
ae6ae975 15789 set_type_vptr_basetype (type, t);
c906108c
SS
15790 if (type == t)
15791 {
c906108c
SS
15792 int i;
15793
15794 /* Our own class provides vtbl ptr. */
1f704f76 15795 for (i = t->num_fields () - 1;
c906108c
SS
15796 i >= TYPE_N_BASECLASSES (t);
15797 --i)
15798 {
0d5cff50 15799 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15800
1168df01 15801 if (is_vtable_name (fieldname, cu))
c906108c 15802 {
ae6ae975 15803 set_type_vptr_fieldno (type, i);
c906108c
SS
15804 break;
15805 }
15806 }
15807
15808 /* Complain if virtual function table field not found. */
15809 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15810 complaint (_("virtual function table pointer "
3e43a32a 15811 "not found when defining class '%s'"),
7d93a1e0 15812 type->name () ? type->name () : "");
c906108c
SS
15813 }
15814 else
15815 {
ae6ae975 15816 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15817 }
15818 }
f6235d4c 15819 else if (cu->producer
61012eef 15820 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15821 {
15822 /* The IBM XLC compiler does not provide direct indication
15823 of the containing type, but the vtable pointer is
15824 always named __vfp. */
15825
15826 int i;
15827
1f704f76 15828 for (i = type->num_fields () - 1;
f6235d4c
EZ
15829 i >= TYPE_N_BASECLASSES (type);
15830 --i)
15831 {
15832 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15833 {
ae6ae975
DE
15834 set_type_vptr_fieldno (type, i);
15835 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15836 break;
15837 }
15838 }
15839 }
c906108c 15840 }
98751a41
JK
15841
15842 /* Copy fi.typedef_field_list linked list elements content into the
15843 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15844 if (!fi.typedef_field_list.empty ())
98751a41 15845 {
be2daae6 15846 int count = fi.typedef_field_list.size ();
98751a41 15847
a0d7a4ff 15848 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15849 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15850 = ((struct decl_field *)
be2daae6
TT
15851 TYPE_ALLOC (type,
15852 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15853 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15854
be2daae6
TT
15855 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15856 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15857 }
c767944b 15858
883fd55a
KS
15859 /* Copy fi.nested_types_list linked list elements content into the
15860 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15861 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15862 {
be2daae6 15863 int count = fi.nested_types_list.size ();
883fd55a
KS
15864
15865 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15866 TYPE_NESTED_TYPES_ARRAY (type)
15867 = ((struct decl_field *)
be2daae6
TT
15868 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15869 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15870
be2daae6
TT
15871 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15872 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15873 }
c906108c 15874 }
63d06c5c 15875
bb5ed363 15876 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15877 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15878 cu->rust_unions.push_back (type);
0b92b5bb 15879
90aeadfc
DC
15880 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15881 snapshots) has been known to create a die giving a declaration
15882 for a class that has, as a child, a die giving a definition for a
15883 nested class. So we have to process our children even if the
15884 current die is a declaration. Normally, of course, a declaration
15885 won't have any children at all. */
134d01f1 15886
ca040673
DE
15887 child_die = die->child;
15888
90aeadfc
DC
15889 while (child_die != NULL && child_die->tag)
15890 {
15891 if (child_die->tag == DW_TAG_member
15892 || child_die->tag == DW_TAG_variable
34eaf542
TT
15893 || child_die->tag == DW_TAG_inheritance
15894 || child_die->tag == DW_TAG_template_value_param
15895 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15896 {
90aeadfc 15897 /* Do nothing. */
134d01f1 15898 }
90aeadfc
DC
15899 else
15900 process_die (child_die, cu);
134d01f1 15901
436c571c 15902 child_die = child_die->sibling;
134d01f1
DJ
15903 }
15904
fa4028e9
JB
15905 /* Do not consider external references. According to the DWARF standard,
15906 these DIEs are identified by the fact that they have no byte_size
15907 attribute, and a declaration attribute. */
15908 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15909 || !die_is_declaration (die, cu)
15910 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15911 {
15912 struct symbol *sym = new_symbol (die, type, cu);
15913
15914 if (has_template_parameters)
15915 {
a776957c
TT
15916 struct symtab *symtab;
15917 if (sym != nullptr)
15918 symtab = symbol_symtab (sym);
15919 else if (cu->line_header != nullptr)
15920 {
15921 /* Any related symtab will do. */
15922 symtab
7ba99d21 15923 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15924 }
15925 else
15926 {
15927 symtab = nullptr;
15928 complaint (_("could not find suitable "
15929 "symtab for template parameter"
15930 " - DIE at %s [in module %s]"),
15931 sect_offset_str (die->sect_off),
15932 objfile_name (objfile));
15933 }
15934
15935 if (symtab != nullptr)
15936 {
15937 /* Make sure that the symtab is set on the new symbols.
15938 Even though they don't appear in this symtab directly,
15939 other parts of gdb assume that symbols do, and this is
15940 reasonably true. */
15941 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15942 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15943 }
3e1d3d8c
TT
15944 }
15945 }
134d01f1
DJ
15946}
15947
ed6acedd
TT
15948/* Assuming DIE is an enumeration type, and TYPE is its associated
15949 type, update TYPE using some information only available in DIE's
15950 children. In particular, the fields are computed. */
55426c9d
JB
15951
15952static void
15953update_enumeration_type_from_children (struct die_info *die,
15954 struct type *type,
15955 struct dwarf2_cu *cu)
15956{
60f7655a 15957 struct die_info *child_die;
55426c9d
JB
15958 int unsigned_enum = 1;
15959 int flag_enum = 1;
55426c9d 15960
8268c778 15961 auto_obstack obstack;
ed6acedd 15962 std::vector<struct field> fields;
55426c9d 15963
60f7655a
DE
15964 for (child_die = die->child;
15965 child_die != NULL && child_die->tag;
436c571c 15966 child_die = child_die->sibling)
55426c9d
JB
15967 {
15968 struct attribute *attr;
15969 LONGEST value;
15970 const gdb_byte *bytes;
15971 struct dwarf2_locexpr_baton *baton;
15972 const char *name;
60f7655a 15973
55426c9d
JB
15974 if (child_die->tag != DW_TAG_enumerator)
15975 continue;
15976
15977 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15978 if (attr == NULL)
15979 continue;
15980
15981 name = dwarf2_name (child_die, cu);
15982 if (name == NULL)
15983 name = "<anonymous enumerator>";
15984
15985 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15986 &value, &bytes, &baton);
15987 if (value < 0)
15988 {
15989 unsigned_enum = 0;
15990 flag_enum = 0;
15991 }
55426c9d 15992 else
edd45eb0
SM
15993 {
15994 if (count_one_bits_ll (value) >= 2)
15995 flag_enum = 0;
edd45eb0 15996 }
55426c9d 15997
ed6acedd
TT
15998 fields.emplace_back ();
15999 struct field &field = fields.back ();
16000 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16001 SET_FIELD_ENUMVAL (field, value);
16002 }
16003
16004 if (!fields.empty ())
16005 {
5e33d5f4 16006 type->set_num_fields (fields.size ());
3cabb6b0
SM
16007 type->set_fields
16008 ((struct field *)
16009 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16010 memcpy (type->fields (), fields.data (),
ed6acedd 16011 sizeof (struct field) * fields.size ());
55426c9d
JB
16012 }
16013
16014 if (unsigned_enum)
16015 TYPE_UNSIGNED (type) = 1;
16016 if (flag_enum)
16017 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16018}
16019
134d01f1
DJ
16020/* Given a DW_AT_enumeration_type die, set its type. We do not
16021 complete the type's fields yet, or create any symbols. */
c906108c 16022
f792889a 16023static struct type *
134d01f1 16024read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16025{
5e22e966 16026 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16027 struct type *type;
c906108c 16028 struct attribute *attr;
0114d602 16029 const char *name;
134d01f1 16030
348e048f
DE
16031 /* If the definition of this type lives in .debug_types, read that type.
16032 Don't follow DW_AT_specification though, that will take us back up
16033 the chain and we want to go down. */
052c8bb8 16034 attr = die->attr (DW_AT_signature);
435d3d88 16035 if (attr != nullptr)
348e048f 16036 {
ac9ec31b 16037 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16038
ac9ec31b 16039 /* The type's CU may not be the same as CU.
02142a6c 16040 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16041 return set_die_type (die, type, cu);
16042 }
16043
c906108c
SS
16044 type = alloc_type (objfile);
16045
67607e24 16046 type->set_code (TYPE_CODE_ENUM);
94af9270 16047 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16048 if (name != NULL)
d0e39ea2 16049 type->set_name (name);
c906108c 16050
0626fc76
TT
16051 attr = dwarf2_attr (die, DW_AT_type, cu);
16052 if (attr != NULL)
16053 {
16054 struct type *underlying_type = die_type (die, cu);
16055
16056 TYPE_TARGET_TYPE (type) = underlying_type;
16057 }
16058
e142c38c 16059 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16060 if (attr != nullptr)
c906108c
SS
16061 {
16062 TYPE_LENGTH (type) = DW_UNSND (attr);
16063 }
16064 else
16065 {
16066 TYPE_LENGTH (type) = 0;
16067 }
16068
2b4424c3
TT
16069 maybe_set_alignment (cu, die, type);
16070
137033e9
JB
16071 /* The enumeration DIE can be incomplete. In Ada, any type can be
16072 declared as private in the package spec, and then defined only
16073 inside the package body. Such types are known as Taft Amendment
16074 Types. When another package uses such a type, an incomplete DIE
16075 may be generated by the compiler. */
02eb380e 16076 if (die_is_declaration (die, cu))
876cecd0 16077 TYPE_STUB (type) = 1;
02eb380e 16078
0626fc76
TT
16079 /* If this type has an underlying type that is not a stub, then we
16080 may use its attributes. We always use the "unsigned" attribute
16081 in this situation, because ordinarily we guess whether the type
16082 is unsigned -- but the guess can be wrong and the underlying type
16083 can tell us the reality. However, we defer to a local size
16084 attribute if one exists, because this lets the compiler override
16085 the underlying type if needed. */
16086 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16087 {
9e7c9a03
HD
16088 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16089 underlying_type = check_typedef (underlying_type);
16090 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16091 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16092 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16093 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16094 && TYPE_RAW_ALIGN (underlying_type) != 0)
16095 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16096 }
16097
3d567982
TT
16098 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16099
ed6acedd
TT
16100 set_die_type (die, type, cu);
16101
16102 /* Finish the creation of this type by using the enum's children.
16103 Note that, as usual, this must come after set_die_type to avoid
16104 infinite recursion when trying to compute the names of the
16105 enumerators. */
16106 update_enumeration_type_from_children (die, type, cu);
16107
16108 return type;
134d01f1
DJ
16109}
16110
16111/* Given a pointer to a die which begins an enumeration, process all
16112 the dies that define the members of the enumeration, and create the
16113 symbol for the enumeration type.
16114
16115 NOTE: We reverse the order of the element list. */
16116
16117static void
16118process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16119{
f792889a 16120 struct type *this_type;
134d01f1 16121
f792889a
DJ
16122 this_type = get_die_type (die, cu);
16123 if (this_type == NULL)
16124 this_type = read_enumeration_type (die, cu);
9dc481d3 16125
639d11d3 16126 if (die->child != NULL)
c906108c 16127 {
9dc481d3 16128 struct die_info *child_die;
15d034d0 16129 const char *name;
9dc481d3 16130
639d11d3 16131 child_die = die->child;
c906108c
SS
16132 while (child_die && child_die->tag)
16133 {
16134 if (child_die->tag != DW_TAG_enumerator)
16135 {
e7c27a73 16136 process_die (child_die, cu);
c906108c
SS
16137 }
16138 else
16139 {
39cbfefa
DJ
16140 name = dwarf2_name (child_die, cu);
16141 if (name)
ed6acedd 16142 new_symbol (child_die, this_type, cu);
c906108c
SS
16143 }
16144
436c571c 16145 child_die = child_die->sibling;
c906108c 16146 }
c906108c 16147 }
134d01f1 16148
6c83ed52
TT
16149 /* If we are reading an enum from a .debug_types unit, and the enum
16150 is a declaration, and the enum is not the signatured type in the
16151 unit, then we do not want to add a symbol for it. Adding a
16152 symbol would in some cases obscure the true definition of the
16153 enum, giving users an incomplete type when the definition is
16154 actually available. Note that we do not want to do this for all
16155 enums which are just declarations, because C++0x allows forward
16156 enum declarations. */
3019eac3 16157 if (cu->per_cu->is_debug_types
6c83ed52
TT
16158 && die_is_declaration (die, cu))
16159 {
52dc124a 16160 struct signatured_type *sig_type;
6c83ed52 16161
c0f78cd4 16162 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16163 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16164 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16165 return;
16166 }
16167
f792889a 16168 new_symbol (die, this_type, cu);
c906108c
SS
16169}
16170
16171/* Extract all information from a DW_TAG_array_type DIE and put it in
16172 the DIE's type field. For now, this only handles one dimensional
16173 arrays. */
16174
f792889a 16175static struct type *
e7c27a73 16176read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16177{
5e22e966 16178 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16179 struct die_info *child_die;
7e314c57 16180 struct type *type;
c906108c 16181 struct type *element_type, *range_type, *index_type;
c906108c 16182 struct attribute *attr;
15d034d0 16183 const char *name;
a405673c 16184 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16185 unsigned int bit_stride = 0;
c906108c 16186
e7c27a73 16187 element_type = die_type (die, cu);
c906108c 16188
7e314c57
JK
16189 /* The die_type call above may have already set the type for this DIE. */
16190 type = get_die_type (die, cu);
16191 if (type)
16192 return type;
16193
dc53a7ad
JB
16194 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16195 if (attr != NULL)
a405673c
JB
16196 {
16197 int stride_ok;
293e7e51 16198 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16199
16200 byte_stride_prop
16201 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16202 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16203 prop_type);
a405673c
JB
16204 if (!stride_ok)
16205 {
b98664d3 16206 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16207 " - DIE at %s [in module %s]"),
16208 sect_offset_str (die->sect_off),
5e22e966 16209 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16210 /* Ignore this attribute. We will likely not be able to print
16211 arrays of this type correctly, but there is little we can do
16212 to help if we cannot read the attribute's value. */
16213 byte_stride_prop = NULL;
16214 }
16215 }
dc53a7ad
JB
16216
16217 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16218 if (attr != NULL)
16219 bit_stride = DW_UNSND (attr);
16220
c906108c
SS
16221 /* Irix 6.2 native cc creates array types without children for
16222 arrays with unspecified length. */
639d11d3 16223 if (die->child == NULL)
c906108c 16224 {
46bf5051 16225 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16226 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16227 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16228 byte_stride_prop, bit_stride);
f792889a 16229 return set_die_type (die, type, cu);
c906108c
SS
16230 }
16231
791afaa2 16232 std::vector<struct type *> range_types;
639d11d3 16233 child_die = die->child;
c906108c
SS
16234 while (child_die && child_die->tag)
16235 {
16236 if (child_die->tag == DW_TAG_subrange_type)
16237 {
f792889a 16238 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16239
f792889a 16240 if (child_type != NULL)
a02abb62 16241 {
0963b4bd
MS
16242 /* The range type was succesfully read. Save it for the
16243 array type creation. */
791afaa2 16244 range_types.push_back (child_type);
a02abb62 16245 }
c906108c 16246 }
436c571c 16247 child_die = child_die->sibling;
c906108c
SS
16248 }
16249
16250 /* Dwarf2 dimensions are output from left to right, create the
16251 necessary array types in backwards order. */
7ca2d3a3 16252
c906108c 16253 type = element_type;
7ca2d3a3
DL
16254
16255 if (read_array_order (die, cu) == DW_ORD_col_major)
16256 {
16257 int i = 0;
9a619af0 16258
791afaa2 16259 while (i < range_types.size ())
dc53a7ad 16260 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16261 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16262 }
16263 else
16264 {
791afaa2 16265 size_t ndim = range_types.size ();
7ca2d3a3 16266 while (ndim-- > 0)
dc53a7ad 16267 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16268 byte_stride_prop, bit_stride);
7ca2d3a3 16269 }
c906108c 16270
f5f8a009
EZ
16271 /* Understand Dwarf2 support for vector types (like they occur on
16272 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16273 array type. This is not part of the Dwarf2/3 standard yet, but a
16274 custom vendor extension. The main difference between a regular
16275 array and the vector variant is that vectors are passed by value
16276 to functions. */
e142c38c 16277 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16278 if (attr != nullptr)
ea37ba09 16279 make_vector_type (type);
f5f8a009 16280
dbc98a8b
KW
16281 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16282 implementation may choose to implement triple vectors using this
16283 attribute. */
16284 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16285 if (attr != nullptr)
dbc98a8b
KW
16286 {
16287 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16288 TYPE_LENGTH (type) = DW_UNSND (attr);
16289 else
b98664d3 16290 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16291 "than the total size of elements"));
dbc98a8b
KW
16292 }
16293
39cbfefa
DJ
16294 name = dwarf2_name (die, cu);
16295 if (name)
d0e39ea2 16296 type->set_name (name);
6e70227d 16297
2b4424c3
TT
16298 maybe_set_alignment (cu, die, type);
16299
0963b4bd 16300 /* Install the type in the die. */
7e314c57
JK
16301 set_die_type (die, type, cu);
16302
16303 /* set_die_type should be already done. */
b4ba55a1
JB
16304 set_descriptive_type (type, die, cu);
16305
7e314c57 16306 return type;
c906108c
SS
16307}
16308
7ca2d3a3 16309static enum dwarf_array_dim_ordering
6e70227d 16310read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16311{
16312 struct attribute *attr;
16313
16314 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16315
435d3d88 16316 if (attr != nullptr)
aead7601 16317 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16318
0963b4bd
MS
16319 /* GNU F77 is a special case, as at 08/2004 array type info is the
16320 opposite order to the dwarf2 specification, but data is still
16321 laid out as per normal fortran.
7ca2d3a3 16322
0963b4bd
MS
16323 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16324 version checking. */
7ca2d3a3 16325
905e0470
PM
16326 if (cu->language == language_fortran
16327 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16328 {
16329 return DW_ORD_row_major;
16330 }
16331
6e70227d 16332 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16333 {
16334 case array_column_major:
16335 return DW_ORD_col_major;
16336 case array_row_major:
16337 default:
16338 return DW_ORD_row_major;
16339 };
16340}
16341
72019c9c 16342/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16343 the DIE's type field. */
72019c9c 16344
f792889a 16345static struct type *
72019c9c
GM
16346read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16347{
7e314c57
JK
16348 struct type *domain_type, *set_type;
16349 struct attribute *attr;
f792889a 16350
7e314c57
JK
16351 domain_type = die_type (die, cu);
16352
16353 /* The die_type call above may have already set the type for this DIE. */
16354 set_type = get_die_type (die, cu);
16355 if (set_type)
16356 return set_type;
16357
16358 set_type = create_set_type (NULL, domain_type);
16359
16360 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16361 if (attr != nullptr)
d09039dd 16362 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16363
2b4424c3
TT
16364 maybe_set_alignment (cu, die, set_type);
16365
f792889a 16366 return set_die_type (die, set_type, cu);
72019c9c 16367}
7ca2d3a3 16368
0971de02
TT
16369/* A helper for read_common_block that creates a locexpr baton.
16370 SYM is the symbol which we are marking as computed.
16371 COMMON_DIE is the DIE for the common block.
16372 COMMON_LOC is the location expression attribute for the common
16373 block itself.
16374 MEMBER_LOC is the location expression attribute for the particular
16375 member of the common block that we are processing.
16376 CU is the CU from which the above come. */
16377
16378static void
16379mark_common_block_symbol_computed (struct symbol *sym,
16380 struct die_info *common_die,
16381 struct attribute *common_loc,
16382 struct attribute *member_loc,
16383 struct dwarf2_cu *cu)
16384{
5e22e966 16385 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16386 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16387 struct dwarf2_locexpr_baton *baton;
16388 gdb_byte *ptr;
16389 unsigned int cu_off;
08feed99 16390 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16391 LONGEST offset = 0;
16392
16393 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16394 gdb_assert (common_loc->form_is_block ());
16395 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16396 || member_loc->form_is_constant ());
0971de02 16397
8d749320 16398 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16399 baton->per_objfile = per_objfile;
0971de02
TT
16400 baton->per_cu = cu->per_cu;
16401 gdb_assert (baton->per_cu);
16402
16403 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16404
cd6c91b4 16405 if (member_loc->form_is_constant ())
0971de02 16406 {
0826b30a 16407 offset = member_loc->constant_value (0);
0971de02
TT
16408 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16409 }
16410 else
16411 baton->size += DW_BLOCK (member_loc)->size;
16412
224c3ddb 16413 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16414 baton->data = ptr;
16415
16416 *ptr++ = DW_OP_call4;
9c541725 16417 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16418 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16419 ptr += 4;
16420
cd6c91b4 16421 if (member_loc->form_is_constant ())
0971de02
TT
16422 {
16423 *ptr++ = DW_OP_addr;
16424 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16425 ptr += cu->header.addr_size;
16426 }
16427 else
16428 {
16429 /* We have to copy the data here, because DW_OP_call4 will only
16430 use a DW_AT_location attribute. */
16431 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16432 ptr += DW_BLOCK (member_loc)->size;
16433 }
16434
16435 *ptr++ = DW_OP_plus;
16436 gdb_assert (ptr - baton->data == baton->size);
16437
0971de02 16438 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16439 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16440}
16441
4357ac6c
TT
16442/* Create appropriate locally-scoped variables for all the
16443 DW_TAG_common_block entries. Also create a struct common_block
16444 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16445 is used to separate the common blocks name namespace from regular
4357ac6c 16446 variable names. */
c906108c
SS
16447
16448static void
e7c27a73 16449read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16450{
0971de02
TT
16451 struct attribute *attr;
16452
16453 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16454 if (attr != nullptr)
0971de02
TT
16455 {
16456 /* Support the .debug_loc offsets. */
4fc6c0d5 16457 if (attr->form_is_block ())
0971de02
TT
16458 {
16459 /* Ok. */
16460 }
cd6c91b4 16461 else if (attr->form_is_section_offset ())
0971de02
TT
16462 {
16463 dwarf2_complex_location_expr_complaint ();
16464 attr = NULL;
16465 }
16466 else
16467 {
16468 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16469 "common block member");
16470 attr = NULL;
16471 }
16472 }
16473
639d11d3 16474 if (die->child != NULL)
c906108c 16475 {
5e22e966 16476 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16477 struct die_info *child_die;
16478 size_t n_entries = 0, size;
16479 struct common_block *common_block;
16480 struct symbol *sym;
74ac6d43 16481
4357ac6c
TT
16482 for (child_die = die->child;
16483 child_die && child_die->tag;
436c571c 16484 child_die = child_die->sibling)
4357ac6c
TT
16485 ++n_entries;
16486
16487 size = (sizeof (struct common_block)
16488 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16489 common_block
16490 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16491 size);
4357ac6c
TT
16492 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16493 common_block->n_entries = 0;
16494
16495 for (child_die = die->child;
16496 child_die && child_die->tag;
436c571c 16497 child_die = child_die->sibling)
4357ac6c
TT
16498 {
16499 /* Create the symbol in the DW_TAG_common_block block in the current
16500 symbol scope. */
e7c27a73 16501 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16502 if (sym != NULL)
16503 {
16504 struct attribute *member_loc;
16505
16506 common_block->contents[common_block->n_entries++] = sym;
16507
16508 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16509 cu);
16510 if (member_loc)
16511 {
16512 /* GDB has handled this for a long time, but it is
16513 not specified by DWARF. It seems to have been
16514 emitted by gfortran at least as recently as:
16515 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16516 complaint (_("Variable in common block has "
0971de02 16517 "DW_AT_data_member_location "
9d8780f0
SM
16518 "- DIE at %s [in module %s]"),
16519 sect_offset_str (child_die->sect_off),
518817b3 16520 objfile_name (objfile));
0971de02 16521
cd6c91b4 16522 if (member_loc->form_is_section_offset ())
0971de02 16523 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16524 else if (member_loc->form_is_constant ()
4fc6c0d5 16525 || member_loc->form_is_block ())
0971de02 16526 {
435d3d88 16527 if (attr != nullptr)
0971de02
TT
16528 mark_common_block_symbol_computed (sym, die, attr,
16529 member_loc, cu);
16530 }
16531 else
16532 dwarf2_complex_location_expr_complaint ();
16533 }
16534 }
c906108c 16535 }
4357ac6c
TT
16536
16537 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16538 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16539 }
16540}
16541
0114d602 16542/* Create a type for a C++ namespace. */
d9fa45fe 16543
0114d602
DJ
16544static struct type *
16545read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16546{
5e22e966 16547 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16548 const char *previous_prefix, *name;
9219021c 16549 int is_anonymous;
0114d602
DJ
16550 struct type *type;
16551
16552 /* For extensions, reuse the type of the original namespace. */
16553 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16554 {
16555 struct die_info *ext_die;
16556 struct dwarf2_cu *ext_cu = cu;
9a619af0 16557
0114d602
DJ
16558 ext_die = dwarf2_extension (die, &ext_cu);
16559 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16560
16561 /* EXT_CU may not be the same as CU.
02142a6c 16562 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16563 return set_die_type (die, type, cu);
16564 }
9219021c 16565
e142c38c 16566 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16567
16568 /* Now build the name of the current namespace. */
16569
0114d602
DJ
16570 previous_prefix = determine_prefix (die, cu);
16571 if (previous_prefix[0] != '\0')
16572 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16573 previous_prefix, name, 0, cu);
0114d602
DJ
16574
16575 /* Create the type. */
19f392bc 16576 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16577
60531b24 16578 return set_die_type (die, type, cu);
0114d602
DJ
16579}
16580
22cee43f 16581/* Read a namespace scope. */
0114d602
DJ
16582
16583static void
16584read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16585{
5e22e966 16586 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16587 int is_anonymous;
9219021c 16588
5c4e30ca
DC
16589 /* Add a symbol associated to this if we haven't seen the namespace
16590 before. Also, add a using directive if it's an anonymous
16591 namespace. */
9219021c 16592
f2f0e013 16593 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16594 {
16595 struct type *type;
16596
0114d602 16597 type = read_type_die (die, cu);
e7c27a73 16598 new_symbol (die, type, cu);
5c4e30ca 16599
e8e80198 16600 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16601 if (is_anonymous)
0114d602
DJ
16602 {
16603 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16604
eb1e02fd 16605 std::vector<const char *> excludes;
804d2729 16606 add_using_directive (using_directives (cu),
7d93a1e0 16607 previous_prefix, type->name (), NULL,
eb1e02fd 16608 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16609 }
5c4e30ca 16610 }
9219021c 16611
639d11d3 16612 if (die->child != NULL)
d9fa45fe 16613 {
639d11d3 16614 struct die_info *child_die = die->child;
6e70227d 16615
d9fa45fe
DC
16616 while (child_die && child_die->tag)
16617 {
e7c27a73 16618 process_die (child_die, cu);
436c571c 16619 child_die = child_die->sibling;
d9fa45fe
DC
16620 }
16621 }
38d518c9
EZ
16622}
16623
f55ee35c
JK
16624/* Read a Fortran module as type. This DIE can be only a declaration used for
16625 imported module. Still we need that type as local Fortran "use ... only"
16626 declaration imports depend on the created type in determine_prefix. */
16627
16628static struct type *
16629read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16630{
5e22e966 16631 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16632 const char *module_name;
f55ee35c
JK
16633 struct type *type;
16634
16635 module_name = dwarf2_name (die, cu);
19f392bc 16636 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16637
f55ee35c
JK
16638 return set_die_type (die, type, cu);
16639}
16640
5d7cb8df
JK
16641/* Read a Fortran module. */
16642
16643static void
16644read_module (struct die_info *die, struct dwarf2_cu *cu)
16645{
16646 struct die_info *child_die = die->child;
530e8392
KB
16647 struct type *type;
16648
16649 type = read_type_die (die, cu);
16650 new_symbol (die, type, cu);
5d7cb8df 16651
5d7cb8df
JK
16652 while (child_die && child_die->tag)
16653 {
16654 process_die (child_die, cu);
436c571c 16655 child_die = child_die->sibling;
5d7cb8df
JK
16656 }
16657}
16658
38d518c9
EZ
16659/* Return the name of the namespace represented by DIE. Set
16660 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16661 namespace. */
16662
16663static const char *
e142c38c 16664namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16665{
16666 struct die_info *current_die;
16667 const char *name = NULL;
16668
16669 /* Loop through the extensions until we find a name. */
16670
16671 for (current_die = die;
16672 current_die != NULL;
f2f0e013 16673 current_die = dwarf2_extension (die, &cu))
38d518c9 16674 {
96553a0c
DE
16675 /* We don't use dwarf2_name here so that we can detect the absence
16676 of a name -> anonymous namespace. */
7d45c7c3 16677 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16678
38d518c9
EZ
16679 if (name != NULL)
16680 break;
16681 }
16682
16683 /* Is it an anonymous namespace? */
16684
16685 *is_anonymous = (name == NULL);
16686 if (*is_anonymous)
2b1dbab0 16687 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16688
16689 return name;
d9fa45fe
DC
16690}
16691
c906108c
SS
16692/* Extract all information from a DW_TAG_pointer_type DIE and add to
16693 the user defined type vector. */
16694
f792889a 16695static struct type *
e7c27a73 16696read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16697{
5e22e966 16698 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16699 struct comp_unit_head *cu_header = &cu->header;
c906108c 16700 struct type *type;
8b2dbe47
KB
16701 struct attribute *attr_byte_size;
16702 struct attribute *attr_address_class;
16703 int byte_size, addr_class;
7e314c57
JK
16704 struct type *target_type;
16705
16706 target_type = die_type (die, cu);
c906108c 16707
7e314c57
JK
16708 /* The die_type call above may have already set the type for this DIE. */
16709 type = get_die_type (die, cu);
16710 if (type)
16711 return type;
16712
16713 type = lookup_pointer_type (target_type);
8b2dbe47 16714
e142c38c 16715 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16716 if (attr_byte_size)
16717 byte_size = DW_UNSND (attr_byte_size);
c906108c 16718 else
8b2dbe47
KB
16719 byte_size = cu_header->addr_size;
16720
e142c38c 16721 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16722 if (attr_address_class)
16723 addr_class = DW_UNSND (attr_address_class);
16724 else
16725 addr_class = DW_ADDR_none;
16726
2b4424c3
TT
16727 ULONGEST alignment = get_alignment (cu, die);
16728
16729 /* If the pointer size, alignment, or address class is different
16730 than the default, create a type variant marked as such and set
16731 the length accordingly. */
16732 if (TYPE_LENGTH (type) != byte_size
16733 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16734 && alignment != TYPE_RAW_ALIGN (type))
16735 || addr_class != DW_ADDR_none)
c906108c 16736 {
5e2b427d 16737 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16738 {
16739 int type_flags;
16740
849957d9 16741 type_flags = gdbarch_address_class_type_flags
5e2b427d 16742 (gdbarch, byte_size, addr_class);
876cecd0
TT
16743 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16744 == 0);
8b2dbe47
KB
16745 type = make_type_with_address_space (type, type_flags);
16746 }
16747 else if (TYPE_LENGTH (type) != byte_size)
16748 {
b98664d3 16749 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16750 }
2b4424c3
TT
16751 else if (TYPE_RAW_ALIGN (type) != alignment)
16752 {
b98664d3 16753 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16754 " - DIE at %s [in module %s]"),
16755 sect_offset_str (die->sect_off),
5e22e966 16756 objfile_name (cu->per_objfile->objfile));
2b4424c3 16757 }
6e70227d 16758 else
9a619af0
MS
16759 {
16760 /* Should we also complain about unhandled address classes? */
16761 }
c906108c 16762 }
8b2dbe47
KB
16763
16764 TYPE_LENGTH (type) = byte_size;
2b4424c3 16765 set_type_align (type, alignment);
f792889a 16766 return set_die_type (die, type, cu);
c906108c
SS
16767}
16768
16769/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16770 the user defined type vector. */
16771
f792889a 16772static struct type *
e7c27a73 16773read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16774{
16775 struct type *type;
16776 struct type *to_type;
16777 struct type *domain;
16778
e7c27a73
DJ
16779 to_type = die_type (die, cu);
16780 domain = die_containing_type (die, cu);
0d5de010 16781
7e314c57
JK
16782 /* The calls above may have already set the type for this DIE. */
16783 type = get_die_type (die, cu);
16784 if (type)
16785 return type;
16786
78134374 16787 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16788 type = lookup_methodptr_type (to_type);
78134374 16789 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16790 {
5e22e966 16791 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16792
16793 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16794 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16795 TYPE_VARARGS (to_type));
16796 type = lookup_methodptr_type (new_type);
16797 }
0d5de010
DJ
16798 else
16799 type = lookup_memberptr_type (to_type, domain);
c906108c 16800
f792889a 16801 return set_die_type (die, type, cu);
c906108c
SS
16802}
16803
4297a3f0 16804/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16805 the user defined type vector. */
16806
f792889a 16807static struct type *
4297a3f0
AV
16808read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16809 enum type_code refcode)
c906108c 16810{
e7c27a73 16811 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16812 struct type *type, *target_type;
c906108c
SS
16813 struct attribute *attr;
16814
4297a3f0
AV
16815 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16816
7e314c57
JK
16817 target_type = die_type (die, cu);
16818
16819 /* The die_type call above may have already set the type for this DIE. */
16820 type = get_die_type (die, cu);
16821 if (type)
16822 return type;
16823
4297a3f0 16824 type = lookup_reference_type (target_type, refcode);
e142c38c 16825 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16826 if (attr != nullptr)
c906108c
SS
16827 {
16828 TYPE_LENGTH (type) = DW_UNSND (attr);
16829 }
16830 else
16831 {
107d2387 16832 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16833 }
2b4424c3 16834 maybe_set_alignment (cu, die, type);
f792889a 16835 return set_die_type (die, type, cu);
c906108c
SS
16836}
16837
cf363f18
MW
16838/* Add the given cv-qualifiers to the element type of the array. GCC
16839 outputs DWARF type qualifiers that apply to an array, not the
16840 element type. But GDB relies on the array element type to carry
16841 the cv-qualifiers. This mimics section 6.7.3 of the C99
16842 specification. */
16843
16844static struct type *
16845add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16846 struct type *base_type, int cnst, int voltl)
16847{
16848 struct type *el_type, *inner_array;
16849
16850 base_type = copy_type (base_type);
16851 inner_array = base_type;
16852
78134374 16853 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16854 {
16855 TYPE_TARGET_TYPE (inner_array) =
16856 copy_type (TYPE_TARGET_TYPE (inner_array));
16857 inner_array = TYPE_TARGET_TYPE (inner_array);
16858 }
16859
16860 el_type = TYPE_TARGET_TYPE (inner_array);
16861 cnst |= TYPE_CONST (el_type);
16862 voltl |= TYPE_VOLATILE (el_type);
16863 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16864
16865 return set_die_type (die, base_type, cu);
16866}
16867
f792889a 16868static struct type *
e7c27a73 16869read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16870{
f792889a 16871 struct type *base_type, *cv_type;
c906108c 16872
e7c27a73 16873 base_type = die_type (die, cu);
7e314c57
JK
16874
16875 /* The die_type call above may have already set the type for this DIE. */
16876 cv_type = get_die_type (die, cu);
16877 if (cv_type)
16878 return cv_type;
16879
2f608a3a
KW
16880 /* In case the const qualifier is applied to an array type, the element type
16881 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16882 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16883 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16884
f792889a
DJ
16885 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16886 return set_die_type (die, cv_type, cu);
c906108c
SS
16887}
16888
f792889a 16889static struct type *
e7c27a73 16890read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16891{
f792889a 16892 struct type *base_type, *cv_type;
c906108c 16893
e7c27a73 16894 base_type = die_type (die, cu);
7e314c57
JK
16895
16896 /* The die_type call above may have already set the type for this DIE. */
16897 cv_type = get_die_type (die, cu);
16898 if (cv_type)
16899 return cv_type;
16900
cf363f18
MW
16901 /* In case the volatile qualifier is applied to an array type, the
16902 element type is so qualified, not the array type (section 6.7.3
16903 of C99). */
78134374 16904 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16905 return add_array_cv_type (die, cu, base_type, 0, 1);
16906
f792889a
DJ
16907 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16908 return set_die_type (die, cv_type, cu);
c906108c
SS
16909}
16910
06d66ee9
TT
16911/* Handle DW_TAG_restrict_type. */
16912
16913static struct type *
16914read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16915{
16916 struct type *base_type, *cv_type;
16917
16918 base_type = die_type (die, cu);
16919
16920 /* The die_type call above may have already set the type for this DIE. */
16921 cv_type = get_die_type (die, cu);
16922 if (cv_type)
16923 return cv_type;
16924
16925 cv_type = make_restrict_type (base_type);
16926 return set_die_type (die, cv_type, cu);
16927}
16928
a2c2acaf
MW
16929/* Handle DW_TAG_atomic_type. */
16930
16931static struct type *
16932read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16933{
16934 struct type *base_type, *cv_type;
16935
16936 base_type = die_type (die, cu);
16937
16938 /* The die_type call above may have already set the type for this DIE. */
16939 cv_type = get_die_type (die, cu);
16940 if (cv_type)
16941 return cv_type;
16942
16943 cv_type = make_atomic_type (base_type);
16944 return set_die_type (die, cv_type, cu);
16945}
16946
c906108c
SS
16947/* Extract all information from a DW_TAG_string_type DIE and add to
16948 the user defined type vector. It isn't really a user defined type,
16949 but it behaves like one, with other DIE's using an AT_user_def_type
16950 attribute to reference it. */
16951
f792889a 16952static struct type *
e7c27a73 16953read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16954{
5e22e966 16955 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 16956 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16957 struct type *type, *range_type, *index_type, *char_type;
16958 struct attribute *attr;
216a7e6b
AB
16959 struct dynamic_prop prop;
16960 bool length_is_constant = true;
16961 LONGEST length;
16962
16963 /* There are a couple of places where bit sizes might be made use of
16964 when parsing a DW_TAG_string_type, however, no producer that we know
16965 of make use of these. Handling bit sizes that are a multiple of the
16966 byte size is easy enough, but what about other bit sizes? Lets deal
16967 with that problem when we have to. Warn about these attributes being
16968 unsupported, then parse the type and ignore them like we always
16969 have. */
16970 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16971 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16972 {
16973 static bool warning_printed = false;
16974 if (!warning_printed)
16975 {
16976 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16977 "currently supported on DW_TAG_string_type."));
16978 warning_printed = true;
16979 }
16980 }
c906108c 16981
e142c38c 16982 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16983 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16984 {
16985 /* The string length describes the location at which the length of
16986 the string can be found. The size of the length field can be
16987 specified with one of the attributes below. */
16988 struct type *prop_type;
16989 struct attribute *len
16990 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16991 if (len == nullptr)
16992 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16993 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16994 {
16995 /* Pass 0 as the default as we know this attribute is constant
16996 and the default value will not be returned. */
0826b30a 16997 LONGEST sz = len->constant_value (0);
293e7e51 16998 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
16999 }
17000 else
17001 {
17002 /* If the size is not specified then we assume it is the size of
17003 an address on this target. */
293e7e51 17004 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17005 }
17006
17007 /* Convert the attribute into a dynamic property. */
17008 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17009 length = 1;
17010 else
17011 length_is_constant = false;
17012 }
17013 else if (attr != nullptr)
17014 {
17015 /* This DW_AT_string_length just contains the length with no
17016 indirection. There's no need to create a dynamic property in this
17017 case. Pass 0 for the default value as we know it will not be
17018 returned in this case. */
0826b30a 17019 length = attr->constant_value (0);
216a7e6b
AB
17020 }
17021 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17022 {
216a7e6b 17023 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17024 length = attr->constant_value (1);
c906108c
SS
17025 }
17026 else
17027 {
216a7e6b
AB
17028 /* Use 1 as a fallback length if we have nothing else. */
17029 length = 1;
c906108c 17030 }
6ccb9162 17031
46bf5051 17032 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17033 if (length_is_constant)
17034 range_type = create_static_range_type (NULL, index_type, 1, length);
17035 else
17036 {
17037 struct dynamic_prop low_bound;
17038
17039 low_bound.kind = PROP_CONST;
17040 low_bound.data.const_val = 1;
17041 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17042 }
3b7538c0
UW
17043 char_type = language_string_char_type (cu->language_defn, gdbarch);
17044 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17045
f792889a 17046 return set_die_type (die, type, cu);
c906108c
SS
17047}
17048
4d804846
JB
17049/* Assuming that DIE corresponds to a function, returns nonzero
17050 if the function is prototyped. */
17051
17052static int
17053prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17054{
17055 struct attribute *attr;
17056
17057 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17058 if (attr && (DW_UNSND (attr) != 0))
17059 return 1;
17060
17061 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17062 is only meaningful for C, but the concept also extends to other
4d804846
JB
17063 languages that allow unprototyped functions (Eg: Objective C).
17064 For all other languages, assume that functions are always
17065 prototyped. */
17066 if (cu->language != language_c
17067 && cu->language != language_objc
17068 && cu->language != language_opencl)
17069 return 1;
17070
17071 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17072 prototyped and unprototyped functions; default to prototyped,
17073 since that is more common in modern code (and RealView warns
17074 about unprototyped functions). */
17075 if (producer_is_realview (cu->producer))
17076 return 1;
17077
17078 return 0;
17079}
17080
c906108c
SS
17081/* Handle DIES due to C code like:
17082
17083 struct foo
c5aa993b
JM
17084 {
17085 int (*funcp)(int a, long l);
17086 int b;
17087 };
c906108c 17088
0963b4bd 17089 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17090
f792889a 17091static struct type *
e7c27a73 17092read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17093{
5e22e966 17094 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17095 struct type *type; /* Type that this function returns. */
17096 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17097 struct attribute *attr;
17098
e7c27a73 17099 type = die_type (die, cu);
7e314c57
JK
17100
17101 /* The die_type call above may have already set the type for this DIE. */
17102 ftype = get_die_type (die, cu);
17103 if (ftype)
17104 return ftype;
17105
0c8b41f1 17106 ftype = lookup_function_type (type);
c906108c 17107
4d804846 17108 if (prototyped_function_p (die, cu))
a6c727b2 17109 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17110
c055b101
CV
17111 /* Store the calling convention in the type if it's available in
17112 the subroutine die. Otherwise set the calling convention to
17113 the default value DW_CC_normal. */
17114 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17115 if (attr != nullptr
17116 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17117 TYPE_CALLING_CONVENTION (ftype)
17118 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17119 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17120 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17121 else
17122 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17123
743649fd
MW
17124 /* Record whether the function returns normally to its caller or not
17125 if the DWARF producer set that information. */
17126 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17127 if (attr && (DW_UNSND (attr) != 0))
17128 TYPE_NO_RETURN (ftype) = 1;
17129
76c10ea2
GM
17130 /* We need to add the subroutine type to the die immediately so
17131 we don't infinitely recurse when dealing with parameters
0963b4bd 17132 declared as the same subroutine type. */
76c10ea2 17133 set_die_type (die, ftype, cu);
6e70227d 17134
639d11d3 17135 if (die->child != NULL)
c906108c 17136 {
bb5ed363 17137 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17138 struct die_info *child_die;
8072405b 17139 int nparams, iparams;
c906108c
SS
17140
17141 /* Count the number of parameters.
17142 FIXME: GDB currently ignores vararg functions, but knows about
17143 vararg member functions. */
8072405b 17144 nparams = 0;
639d11d3 17145 child_die = die->child;
c906108c
SS
17146 while (child_die && child_die->tag)
17147 {
17148 if (child_die->tag == DW_TAG_formal_parameter)
17149 nparams++;
17150 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17151 TYPE_VARARGS (ftype) = 1;
436c571c 17152 child_die = child_die->sibling;
c906108c
SS
17153 }
17154
17155 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17156 ftype->set_num_fields (nparams);
3cabb6b0
SM
17157 ftype->set_fields
17158 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17159
8072405b
JK
17160 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17161 even if we error out during the parameters reading below. */
17162 for (iparams = 0; iparams < nparams; iparams++)
17163 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17164
17165 iparams = 0;
639d11d3 17166 child_die = die->child;
c906108c
SS
17167 while (child_die && child_die->tag)
17168 {
17169 if (child_die->tag == DW_TAG_formal_parameter)
17170 {
3ce3b1ba
PA
17171 struct type *arg_type;
17172
17173 /* DWARF version 2 has no clean way to discern C++
17174 static and non-static member functions. G++ helps
17175 GDB by marking the first parameter for non-static
17176 member functions (which is the this pointer) as
17177 artificial. We pass this information to
17178 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17179
17180 DWARF version 3 added DW_AT_object_pointer, which GCC
17181 4.5 does not yet generate. */
e142c38c 17182 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17183 if (attr != nullptr)
c906108c
SS
17184 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17185 else
9c37b5ae 17186 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17187 arg_type = die_type (child_die, cu);
17188
17189 /* RealView does not mark THIS as const, which the testsuite
17190 expects. GCC marks THIS as const in method definitions,
17191 but not in the class specifications (GCC PR 43053). */
17192 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17193 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17194 {
17195 int is_this = 0;
17196 struct dwarf2_cu *arg_cu = cu;
17197 const char *name = dwarf2_name (child_die, cu);
17198
17199 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17200 if (attr != nullptr)
3ce3b1ba
PA
17201 {
17202 /* If the compiler emits this, use it. */
17203 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17204 is_this = 1;
17205 }
17206 else if (name && strcmp (name, "this") == 0)
17207 /* Function definitions will have the argument names. */
17208 is_this = 1;
17209 else if (name == NULL && iparams == 0)
17210 /* Declarations may not have the names, so like
17211 elsewhere in GDB, assume an artificial first
17212 argument is "this". */
17213 is_this = 1;
17214
17215 if (is_this)
17216 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17217 arg_type, 0);
17218 }
17219
17220 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17221 iparams++;
17222 }
436c571c 17223 child_die = child_die->sibling;
c906108c
SS
17224 }
17225 }
17226
76c10ea2 17227 return ftype;
c906108c
SS
17228}
17229
f792889a 17230static struct type *
e7c27a73 17231read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17232{
5e22e966 17233 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17234 const char *name = NULL;
3c8e0968 17235 struct type *this_type, *target_type;
c906108c 17236
94af9270 17237 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17238 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17239 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17240 set_die_type (die, this_type, cu);
3c8e0968
DE
17241 target_type = die_type (die, cu);
17242 if (target_type != this_type)
17243 TYPE_TARGET_TYPE (this_type) = target_type;
17244 else
17245 {
17246 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17247 spec and cause infinite loops in GDB. */
b98664d3 17248 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17249 "- DIE at %s [in module %s]"),
17250 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17251 TYPE_TARGET_TYPE (this_type) = NULL;
17252 }
e4003a34
TV
17253 if (name == NULL)
17254 {
17255 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17256 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17257 Handle these by just returning the target type, rather than
17258 constructing an anonymous typedef type and trying to handle this
17259 elsewhere. */
17260 set_die_type (die, target_type, cu);
17261 return target_type;
17262 }
f792889a 17263 return this_type;
c906108c
SS
17264}
17265
9b790ce7
UW
17266/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17267 (which may be different from NAME) to the architecture back-end to allow
17268 it to guess the correct format if necessary. */
17269
17270static struct type *
17271dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17272 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17273{
08feed99 17274 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17275 const struct floatformat **format;
17276 struct type *type;
17277
17278 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17279 if (format)
103a685e 17280 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17281 else
77b7c781 17282 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17283
17284 return type;
17285}
17286
eb77c9df
AB
17287/* Allocate an integer type of size BITS and name NAME. */
17288
17289static struct type *
17290dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17291 int bits, int unsigned_p, const char *name)
17292{
17293 struct type *type;
17294
17295 /* Versions of Intel's C Compiler generate an integer type called "void"
17296 instead of using DW_TAG_unspecified_type. This has been seen on
17297 at least versions 14, 17, and 18. */
35ee2dc2
AB
17298 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17299 && strcmp (name, "void") == 0)
eb77c9df
AB
17300 type = objfile_type (objfile)->builtin_void;
17301 else
17302 type = init_integer_type (objfile, bits, unsigned_p, name);
17303
17304 return type;
17305}
17306
8bdc1658
AB
17307/* Initialise and return a floating point type of size BITS suitable for
17308 use as a component of a complex number. The NAME_HINT is passed through
17309 when initialising the floating point type and is the name of the complex
17310 type.
17311
17312 As DWARF doesn't currently provide an explicit name for the components
17313 of a complex number, but it can be helpful to have these components
17314 named, we try to select a suitable name based on the size of the
17315 component. */
17316static struct type *
17317dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17318 struct objfile *objfile,
103a685e
TT
17319 int bits, const char *name_hint,
17320 enum bfd_endian byte_order)
8bdc1658 17321{
08feed99 17322 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17323 struct type *tt = nullptr;
17324
35add35e
AB
17325 /* Try to find a suitable floating point builtin type of size BITS.
17326 We're going to use the name of this type as the name for the complex
17327 target type that we are about to create. */
1db455a7 17328 switch (cu->language)
8bdc1658 17329 {
1db455a7
AB
17330 case language_fortran:
17331 switch (bits)
17332 {
17333 case 32:
17334 tt = builtin_f_type (gdbarch)->builtin_real;
17335 break;
17336 case 64:
17337 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17338 break;
17339 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17340 case 128:
17341 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17342 break;
17343 }
8bdc1658 17344 break;
1db455a7
AB
17345 default:
17346 switch (bits)
17347 {
17348 case 32:
17349 tt = builtin_type (gdbarch)->builtin_float;
17350 break;
17351 case 64:
17352 tt = builtin_type (gdbarch)->builtin_double;
17353 break;
17354 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17355 case 128:
17356 tt = builtin_type (gdbarch)->builtin_long_double;
17357 break;
17358 }
8bdc1658
AB
17359 break;
17360 }
17361
35add35e
AB
17362 /* If the type we found doesn't match the size we were looking for, then
17363 pretend we didn't find a type at all, the complex target type we
17364 create will then be nameless. */
a12e5744 17365 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17366 tt = nullptr;
17367
7d93a1e0 17368 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17369 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17370}
17371
c906108c
SS
17372/* Find a representation of a given base type and install
17373 it in the TYPE field of the die. */
17374
f792889a 17375static struct type *
e7c27a73 17376read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17377{
5e22e966 17378 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17379 struct type *type;
17380 struct attribute *attr;
19f392bc 17381 int encoding = 0, bits = 0;
15d034d0 17382 const char *name;
34877895 17383 gdbarch *arch;
c906108c 17384
e142c38c 17385 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17386 if (attr != nullptr)
34877895 17387 encoding = DW_UNSND (attr);
e142c38c 17388 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17389 if (attr != nullptr)
34877895 17390 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17391 name = dwarf2_name (die, cu);
6ccb9162 17392 if (!name)
34877895 17393 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17394
08feed99 17395 arch = objfile->arch ();
103a685e
TT
17396 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17397
34877895
PJ
17398 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17399 if (attr)
103a685e
TT
17400 {
17401 int endianity = DW_UNSND (attr);
17402
17403 switch (endianity)
17404 {
17405 case DW_END_big:
17406 byte_order = BFD_ENDIAN_BIG;
17407 break;
17408 case DW_END_little:
17409 byte_order = BFD_ENDIAN_LITTLE;
17410 break;
17411 default:
17412 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17413 break;
17414 }
17415 }
6ccb9162
UW
17416
17417 switch (encoding)
c906108c 17418 {
6ccb9162
UW
17419 case DW_ATE_address:
17420 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17421 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17422 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17423 break;
17424 case DW_ATE_boolean:
19f392bc 17425 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17426 break;
17427 case DW_ATE_complex_float:
103a685e
TT
17428 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17429 byte_order);
78134374 17430 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17431 {
17432 if (name == nullptr)
17433 {
17434 struct obstack *obstack
5e22e966 17435 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17436 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17437 nullptr);
17438 }
17439 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17440 }
17441 else
17442 type = init_complex_type (name, type);
6ccb9162
UW
17443 break;
17444 case DW_ATE_decimal_float:
19f392bc 17445 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17446 break;
17447 case DW_ATE_float:
103a685e 17448 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17449 break;
17450 case DW_ATE_signed:
eb77c9df 17451 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17452 break;
17453 case DW_ATE_unsigned:
3b2b8fea
TT
17454 if (cu->language == language_fortran
17455 && name
61012eef 17456 && startswith (name, "character("))
19f392bc
UW
17457 type = init_character_type (objfile, bits, 1, name);
17458 else
eb77c9df 17459 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17460 break;
17461 case DW_ATE_signed_char:
6e70227d 17462 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17463 || cu->language == language_pascal
17464 || cu->language == language_fortran)
19f392bc
UW
17465 type = init_character_type (objfile, bits, 0, name);
17466 else
eb77c9df 17467 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17468 break;
17469 case DW_ATE_unsigned_char:
868a0084 17470 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17471 || cu->language == language_pascal
c44af4eb
TT
17472 || cu->language == language_fortran
17473 || cu->language == language_rust)
19f392bc
UW
17474 type = init_character_type (objfile, bits, 1, name);
17475 else
eb77c9df 17476 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17477 break;
75079b2b 17478 case DW_ATE_UTF:
53e710ac 17479 {
53e710ac
PA
17480 if (bits == 16)
17481 type = builtin_type (arch)->builtin_char16;
17482 else if (bits == 32)
17483 type = builtin_type (arch)->builtin_char32;
17484 else
17485 {
b98664d3 17486 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17487 bits);
eb77c9df 17488 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17489 }
17490 return set_die_type (die, type, cu);
17491 }
75079b2b
TT
17492 break;
17493
6ccb9162 17494 default:
b98664d3 17495 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17496 dwarf_type_encoding_name (encoding));
77b7c781 17497 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17498 break;
c906108c 17499 }
6ccb9162 17500
0114d602 17501 if (name && strcmp (name, "char") == 0)
876cecd0 17502 TYPE_NOSIGN (type) = 1;
0114d602 17503
2b4424c3
TT
17504 maybe_set_alignment (cu, die, type);
17505
103a685e 17506 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17507
f792889a 17508 return set_die_type (die, type, cu);
c906108c
SS
17509}
17510
80180f79
SA
17511/* Parse dwarf attribute if it's a block, reference or constant and put the
17512 resulting value of the attribute into struct bound_prop.
17513 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17514
17515static int
17516attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17517 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17518 struct type *default_type)
80180f79
SA
17519{
17520 struct dwarf2_property_baton *baton;
5e22e966 17521 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17522 struct objfile *objfile = per_objfile->objfile;
17523 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17524
9a49df9d
AB
17525 gdb_assert (default_type != NULL);
17526
80180f79
SA
17527 if (attr == NULL || prop == NULL)
17528 return 0;
17529
4fc6c0d5 17530 if (attr->form_is_block ())
80180f79 17531 {
8d749320 17532 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17533 baton->property_type = default_type;
80180f79 17534 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17535 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17536 baton->locexpr.size = DW_BLOCK (attr)->size;
17537 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17538 switch (attr->name)
17539 {
17540 case DW_AT_string_length:
17541 baton->locexpr.is_reference = true;
17542 break;
17543 default:
17544 baton->locexpr.is_reference = false;
17545 break;
17546 }
80180f79
SA
17547 prop->data.baton = baton;
17548 prop->kind = PROP_LOCEXPR;
17549 gdb_assert (prop->data.baton != NULL);
17550 }
cd6c91b4 17551 else if (attr->form_is_ref ())
80180f79
SA
17552 {
17553 struct dwarf2_cu *target_cu = cu;
17554 struct die_info *target_die;
17555 struct attribute *target_attr;
17556
17557 target_die = follow_die_ref (die, attr, &target_cu);
17558 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17559 if (target_attr == NULL)
17560 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17561 target_cu);
80180f79
SA
17562 if (target_attr == NULL)
17563 return 0;
17564
df25ebbd 17565 switch (target_attr->name)
80180f79 17566 {
df25ebbd 17567 case DW_AT_location:
cd6c91b4 17568 if (target_attr->form_is_section_offset ())
df25ebbd 17569 {
8d749320 17570 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17571 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17572 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17573 prop->data.baton = baton;
17574 prop->kind = PROP_LOCLIST;
17575 gdb_assert (prop->data.baton != NULL);
17576 }
4fc6c0d5 17577 else if (target_attr->form_is_block ())
df25ebbd 17578 {
8d749320 17579 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17580 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17581 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17582 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17583 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17584 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17585 baton->locexpr.is_reference = true;
df25ebbd
JB
17586 prop->data.baton = baton;
17587 prop->kind = PROP_LOCEXPR;
17588 gdb_assert (prop->data.baton != NULL);
17589 }
17590 else
17591 {
17592 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17593 "dynamic property");
17594 return 0;
17595 }
17596 break;
17597 case DW_AT_data_member_location:
17598 {
17599 LONGEST offset;
17600
17601 if (!handle_data_member_location (target_die, target_cu,
17602 &offset))
17603 return 0;
17604
8d749320 17605 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17606 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17607 target_cu);
df25ebbd
JB
17608 baton->offset_info.offset = offset;
17609 baton->offset_info.type = die_type (target_die, target_cu);
17610 prop->data.baton = baton;
17611 prop->kind = PROP_ADDR_OFFSET;
17612 break;
17613 }
80180f79
SA
17614 }
17615 }
cd6c91b4 17616 else if (attr->form_is_constant ())
80180f79 17617 {
0826b30a 17618 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17619 prop->kind = PROP_CONST;
17620 }
17621 else
17622 {
17623 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17624 dwarf2_name (die, cu));
17625 return 0;
17626 }
17627
17628 return 1;
17629}
17630
09ba997f 17631/* See read.h. */
9a49df9d 17632
09ba997f 17633struct type *
293e7e51 17634dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17635{
9a49df9d
AB
17636 struct type *int_type;
17637
17638 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17639#define TRY_TYPE(F) \
17640 int_type = (unsigned_p \
17641 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17642 : objfile_type (objfile)->builtin_ ## F); \
17643 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17644 return int_type
17645
17646 TRY_TYPE (char);
17647 TRY_TYPE (short);
17648 TRY_TYPE (int);
17649 TRY_TYPE (long);
17650 TRY_TYPE (long_long);
17651
17652#undef TRY_TYPE
17653
17654 gdb_assert_not_reached ("unable to find suitable integer type");
17655}
17656
09ba997f 17657/* See read.h. */
11a8b164 17658
09ba997f 17659struct type *
293e7e51 17660dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17661{
293e7e51
SM
17662 int addr_size = this->per_cu->addr_size ();
17663 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17664}
17665
b86352cf
AB
17666/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17667 present (which is valid) then compute the default type based on the
17668 compilation units address size. */
17669
17670static struct type *
17671read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17672{
17673 struct type *index_type = die_type (die, cu);
17674
17675 /* Dwarf-2 specifications explicitly allows to create subrange types
17676 without specifying a base type.
17677 In that case, the base type must be set to the type of
17678 the lower bound, upper bound or count, in that order, if any of these
17679 three attributes references an object that has a type.
17680 If no base type is found, the Dwarf-2 specifications say that
17681 a signed integer type of size equal to the size of an address should
17682 be used.
17683 For the following C code: `extern char gdb_int [];'
17684 GCC produces an empty range DIE.
17685 FIXME: muller/2010-05-28: Possible references to object for low bound,
17686 high bound or count are not yet handled by this code. */
78134374 17687 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17688 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17689
17690 return index_type;
17691}
17692
a02abb62
JB
17693/* Read the given DW_AT_subrange DIE. */
17694
f792889a 17695static struct type *
a02abb62
JB
17696read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17697{
4c9ad8c2 17698 struct type *base_type, *orig_base_type;
a02abb62
JB
17699 struct type *range_type;
17700 struct attribute *attr;
729efb13 17701 struct dynamic_prop low, high;
4fae6e18 17702 int low_default_is_valid;
c451ebe5 17703 int high_bound_is_count = 0;
15d034d0 17704 const char *name;
d359392f 17705 ULONGEST negative_mask;
e77813c8 17706
b86352cf
AB
17707 orig_base_type = read_subrange_index_type (die, cu);
17708
4c9ad8c2
TT
17709 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17710 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17711 creating the range type, but we use the result of check_typedef
17712 when examining properties of the type. */
17713 base_type = check_typedef (orig_base_type);
a02abb62 17714
7e314c57
JK
17715 /* The die_type call above may have already set the type for this DIE. */
17716 range_type = get_die_type (die, cu);
17717 if (range_type)
17718 return range_type;
17719
729efb13
SA
17720 low.kind = PROP_CONST;
17721 high.kind = PROP_CONST;
17722 high.data.const_val = 0;
17723
4fae6e18
JK
17724 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17725 omitting DW_AT_lower_bound. */
17726 switch (cu->language)
6e70227d 17727 {
4fae6e18
JK
17728 case language_c:
17729 case language_cplus:
729efb13 17730 low.data.const_val = 0;
4fae6e18
JK
17731 low_default_is_valid = 1;
17732 break;
17733 case language_fortran:
729efb13 17734 low.data.const_val = 1;
4fae6e18
JK
17735 low_default_is_valid = 1;
17736 break;
17737 case language_d:
4fae6e18 17738 case language_objc:
c44af4eb 17739 case language_rust:
729efb13 17740 low.data.const_val = 0;
4fae6e18
JK
17741 low_default_is_valid = (cu->header.version >= 4);
17742 break;
17743 case language_ada:
17744 case language_m2:
17745 case language_pascal:
729efb13 17746 low.data.const_val = 1;
4fae6e18
JK
17747 low_default_is_valid = (cu->header.version >= 4);
17748 break;
17749 default:
729efb13 17750 low.data.const_val = 0;
4fae6e18
JK
17751 low_default_is_valid = 0;
17752 break;
a02abb62
JB
17753 }
17754
e142c38c 17755 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17756 if (attr != nullptr)
9a49df9d 17757 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17758 else if (!low_default_is_valid)
b98664d3 17759 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17760 "- DIE at %s [in module %s]"),
17761 sect_offset_str (die->sect_off),
5e22e966 17762 objfile_name (cu->per_objfile->objfile));
a02abb62 17763
506f5c41
TV
17764 struct attribute *attr_ub, *attr_count;
17765 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17766 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17767 {
506f5c41 17768 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17769 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17770 {
c451ebe5
SA
17771 /* If bounds are constant do the final calculation here. */
17772 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17773 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17774 else
17775 high_bound_is_count = 1;
c2ff108b 17776 }
506f5c41
TV
17777 else
17778 {
17779 if (attr_ub != NULL)
17780 complaint (_("Unresolved DW_AT_upper_bound "
17781 "- DIE at %s [in module %s]"),
17782 sect_offset_str (die->sect_off),
5e22e966 17783 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17784 if (attr_count != NULL)
17785 complaint (_("Unresolved DW_AT_count "
17786 "- DIE at %s [in module %s]"),
17787 sect_offset_str (die->sect_off),
5e22e966 17788 objfile_name (cu->per_objfile->objfile));
506f5c41 17789 }
e77813c8 17790 }
a02abb62 17791
4e962e74
TT
17792 LONGEST bias = 0;
17793 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17794 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17795 bias = bias_attr->constant_value (0);
4e962e74 17796
dbb9c2b1
JB
17797 /* Normally, the DWARF producers are expected to use a signed
17798 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17799 But this is unfortunately not always the case, as witnessed
17800 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17801 is used instead. To work around that ambiguity, we treat
17802 the bounds as signed, and thus sign-extend their values, when
17803 the base type is signed. */
6e70227d 17804 negative_mask =
d359392f 17805 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17806 if (low.kind == PROP_CONST
17807 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17808 low.data.const_val |= negative_mask;
17809 if (high.kind == PROP_CONST
17810 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17811 high.data.const_val |= negative_mask;
43bbcdc2 17812
5bbd8269
AB
17813 /* Check for bit and byte strides. */
17814 struct dynamic_prop byte_stride_prop;
17815 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17816 if (attr_byte_stride != nullptr)
17817 {
293e7e51 17818 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17819 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17820 prop_type);
17821 }
17822
17823 struct dynamic_prop bit_stride_prop;
17824 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17825 if (attr_bit_stride != nullptr)
17826 {
17827 /* It only makes sense to have either a bit or byte stride. */
17828 if (attr_byte_stride != nullptr)
17829 {
17830 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17831 "- DIE at %s [in module %s]"),
17832 sect_offset_str (die->sect_off),
5e22e966 17833 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17834 attr_bit_stride = nullptr;
17835 }
17836 else
17837 {
293e7e51 17838 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17839 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17840 prop_type);
17841 }
17842 }
17843
17844 if (attr_byte_stride != nullptr
17845 || attr_bit_stride != nullptr)
17846 {
17847 bool byte_stride_p = (attr_byte_stride != nullptr);
17848 struct dynamic_prop *stride
17849 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17850
17851 range_type
17852 = create_range_type_with_stride (NULL, orig_base_type, &low,
17853 &high, bias, stride, byte_stride_p);
17854 }
17855 else
17856 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17857
c451ebe5
SA
17858 if (high_bound_is_count)
17859 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17860
c2ff108b
JK
17861 /* Ada expects an empty array on no boundary attributes. */
17862 if (attr == NULL && cu->language != language_ada)
729efb13 17863 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17864
39cbfefa
DJ
17865 name = dwarf2_name (die, cu);
17866 if (name)
d0e39ea2 17867 range_type->set_name (name);
6e70227d 17868
e142c38c 17869 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17870 if (attr != nullptr)
a02abb62
JB
17871 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17872
2b4424c3
TT
17873 maybe_set_alignment (cu, die, range_type);
17874
7e314c57
JK
17875 set_die_type (die, range_type, cu);
17876
17877 /* set_die_type should be already done. */
b4ba55a1
JB
17878 set_descriptive_type (range_type, die, cu);
17879
7e314c57 17880 return range_type;
a02abb62 17881}
6e70227d 17882
f792889a 17883static struct type *
81a17f79
JB
17884read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17885{
17886 struct type *type;
81a17f79 17887
5e22e966 17888 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17889 type->set_name (dwarf2_name (die, cu));
81a17f79 17890
74a2f8ff 17891 /* In Ada, an unspecified type is typically used when the description
85102364 17892 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17893 such a type, we treat it as a stub, and try to resolve it later on,
17894 when needed. */
17895 if (cu->language == language_ada)
17896 TYPE_STUB (type) = 1;
17897
f792889a 17898 return set_die_type (die, type, cu);
81a17f79 17899}
a02abb62 17900
639d11d3
DC
17901/* Read a single die and all its descendents. Set the die's sibling
17902 field to NULL; set other fields in the die correctly, and set all
17903 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17904 location of the info_ptr after reading all of those dies. PARENT
17905 is the parent of the die in question. */
17906
17907static struct die_info *
dee91e82 17908read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17909 const gdb_byte *info_ptr,
17910 const gdb_byte **new_info_ptr,
dee91e82 17911 struct die_info *parent)
639d11d3
DC
17912{
17913 struct die_info *die;
d521ce57 17914 const gdb_byte *cur_ptr;
639d11d3 17915
3e225074 17916 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17917 if (die == NULL)
17918 {
17919 *new_info_ptr = cur_ptr;
17920 return NULL;
17921 }
93311388 17922 store_in_ref_table (die, reader->cu);
639d11d3 17923
3e225074 17924 if (die->has_children)
bf6af496 17925 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17926 else
17927 {
17928 die->child = NULL;
17929 *new_info_ptr = cur_ptr;
17930 }
17931
17932 die->sibling = NULL;
17933 die->parent = parent;
17934 return die;
17935}
17936
17937/* Read a die, all of its descendents, and all of its siblings; set
17938 all of the fields of all of the dies correctly. Arguments are as
17939 in read_die_and_children. */
17940
17941static struct die_info *
bf6af496 17942read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17943 const gdb_byte *info_ptr,
17944 const gdb_byte **new_info_ptr,
bf6af496 17945 struct die_info *parent)
639d11d3
DC
17946{
17947 struct die_info *first_die, *last_sibling;
d521ce57 17948 const gdb_byte *cur_ptr;
639d11d3 17949
c906108c 17950 cur_ptr = info_ptr;
639d11d3
DC
17951 first_die = last_sibling = NULL;
17952
17953 while (1)
c906108c 17954 {
639d11d3 17955 struct die_info *die
dee91e82 17956 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17957
1d325ec1 17958 if (die == NULL)
c906108c 17959 {
639d11d3
DC
17960 *new_info_ptr = cur_ptr;
17961 return first_die;
c906108c 17962 }
1d325ec1
DJ
17963
17964 if (!first_die)
17965 first_die = die;
c906108c 17966 else
1d325ec1
DJ
17967 last_sibling->sibling = die;
17968
17969 last_sibling = die;
c906108c 17970 }
c906108c
SS
17971}
17972
bf6af496
DE
17973/* Read a die, all of its descendents, and all of its siblings; set
17974 all of the fields of all of the dies correctly. Arguments are as
17975 in read_die_and_children.
17976 This the main entry point for reading a DIE and all its children. */
17977
17978static struct die_info *
17979read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17980 const gdb_byte *info_ptr,
17981 const gdb_byte **new_info_ptr,
bf6af496
DE
17982 struct die_info *parent)
17983{
17984 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17985 new_info_ptr, parent);
17986
b4f54984 17987 if (dwarf_die_debug)
bf6af496
DE
17988 {
17989 fprintf_unfiltered (gdb_stdlog,
17990 "Read die from %s@0x%x of %s:\n",
96b79293 17991 reader->die_section->get_name (),
bf6af496
DE
17992 (unsigned) (info_ptr - reader->die_section->buffer),
17993 bfd_get_filename (reader->abfd));
b4f54984 17994 dump_die (die, dwarf_die_debug);
bf6af496
DE
17995 }
17996
17997 return die;
17998}
17999
3019eac3
DE
18000/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18001 attributes.
18002 The caller is responsible for filling in the extra attributes
18003 and updating (*DIEP)->num_attrs.
18004 Set DIEP to point to a newly allocated die with its information,
3e225074 18005 except for its child, sibling, and parent fields. */
93311388 18006
d521ce57 18007static const gdb_byte *
3019eac3 18008read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18009 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18010 int num_extra_attrs)
93311388 18011{
b64f50a1 18012 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18013 struct abbrev_info *abbrev;
18014 struct die_info *die;
18015 struct dwarf2_cu *cu = reader->cu;
18016 bfd *abfd = reader->abfd;
18017
9c541725 18018 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18019 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18020 info_ptr += bytes_read;
18021 if (!abbrev_number)
18022 {
18023 *diep = NULL;
93311388
DE
18024 return info_ptr;
18025 }
18026
685af9cd 18027 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18028 if (!abbrev)
348e048f
DE
18029 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18030 abbrev_number,
18031 bfd_get_filename (abfd));
18032
3019eac3 18033 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18034 die->sect_off = sect_off;
93311388
DE
18035 die->tag = abbrev->tag;
18036 die->abbrev = abbrev_number;
3e225074 18037 die->has_children = abbrev->has_children;
93311388 18038
3019eac3
DE
18039 /* Make the result usable.
18040 The caller needs to update num_attrs after adding the extra
18041 attributes. */
93311388
DE
18042 die->num_attrs = abbrev->num_attrs;
18043
18a8505e 18044 std::vector<int> indexes_that_need_reprocess;
93311388 18045 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18046 {
18047 bool need_reprocess;
18048 info_ptr =
18049 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18050 info_ptr, &need_reprocess);
18051 if (need_reprocess)
18052 indexes_that_need_reprocess.push_back (i);
18053 }
18054
052c8bb8 18055 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18056 if (attr != nullptr)
18057 cu->str_offsets_base = DW_UNSND (attr);
93311388 18058
41144253 18059 attr = die->attr (DW_AT_loclists_base);
18060 if (attr != nullptr)
18061 cu->loclist_base = DW_UNSND (attr);
18062
a39fdb41 18063 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18064 if (maybe_addr_base.has_value ())
18065 cu->addr_base = *maybe_addr_base;
18066 for (int index : indexes_that_need_reprocess)
18067 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18068 *diep = die;
93311388
DE
18069 return info_ptr;
18070}
18071
3019eac3
DE
18072/* Read a die and all its attributes.
18073 Set DIEP to point to a newly allocated die with its information,
3e225074 18074 except for its child, sibling, and parent fields. */
3019eac3 18075
d521ce57 18076static const gdb_byte *
3019eac3 18077read_full_die (const struct die_reader_specs *reader,
3e225074 18078 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18079{
d521ce57 18080 const gdb_byte *result;
bf6af496 18081
3e225074 18082 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18083
b4f54984 18084 if (dwarf_die_debug)
bf6af496
DE
18085 {
18086 fprintf_unfiltered (gdb_stdlog,
18087 "Read die from %s@0x%x of %s:\n",
96b79293 18088 reader->die_section->get_name (),
bf6af496
DE
18089 (unsigned) (info_ptr - reader->die_section->buffer),
18090 bfd_get_filename (reader->abfd));
b4f54984 18091 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18092 }
18093
18094 return result;
3019eac3 18095}
433df2d4 18096\f
c906108c 18097
72bf9492
DJ
18098/* Returns nonzero if TAG represents a type that we might generate a partial
18099 symbol for. */
18100
18101static int
18102is_type_tag_for_partial (int tag)
18103{
18104 switch (tag)
18105 {
18106#if 0
18107 /* Some types that would be reasonable to generate partial symbols for,
18108 that we don't at present. */
18109 case DW_TAG_array_type:
18110 case DW_TAG_file_type:
18111 case DW_TAG_ptr_to_member_type:
18112 case DW_TAG_set_type:
18113 case DW_TAG_string_type:
18114 case DW_TAG_subroutine_type:
18115#endif
18116 case DW_TAG_base_type:
18117 case DW_TAG_class_type:
680b30c7 18118 case DW_TAG_interface_type:
72bf9492
DJ
18119 case DW_TAG_enumeration_type:
18120 case DW_TAG_structure_type:
18121 case DW_TAG_subrange_type:
18122 case DW_TAG_typedef:
18123 case DW_TAG_union_type:
18124 return 1;
18125 default:
18126 return 0;
18127 }
18128}
18129
18130/* Load all DIEs that are interesting for partial symbols into memory. */
18131
18132static struct partial_die_info *
dee91e82 18133load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18134 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18135{
dee91e82 18136 struct dwarf2_cu *cu = reader->cu;
5e22e966 18137 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18138 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18139 unsigned int bytes_read;
5afb4e99 18140 unsigned int load_all = 0;
72bf9492
DJ
18141 int nesting_level = 1;
18142
18143 parent_die = NULL;
18144 last_die = NULL;
18145
7adf1e79
DE
18146 gdb_assert (cu->per_cu != NULL);
18147 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18148 load_all = 1;
18149
72bf9492
DJ
18150 cu->partial_dies
18151 = htab_create_alloc_ex (cu->header.length / 12,
18152 partial_die_hash,
18153 partial_die_eq,
18154 NULL,
18155 &cu->comp_unit_obstack,
18156 hashtab_obstack_allocate,
18157 dummy_obstack_deallocate);
18158
72bf9492
DJ
18159 while (1)
18160 {
685af9cd 18161 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18162
18163 /* A NULL abbrev means the end of a series of children. */
18164 if (abbrev == NULL)
18165 {
18166 if (--nesting_level == 0)
cd9983dd
YQ
18167 return first_die;
18168
72bf9492
DJ
18169 info_ptr += bytes_read;
18170 last_die = parent_die;
18171 parent_die = parent_die->die_parent;
18172 continue;
18173 }
18174
98bfdba5
PA
18175 /* Check for template arguments. We never save these; if
18176 they're seen, we just mark the parent, and go on our way. */
18177 if (parent_die != NULL
18178 && cu->language == language_cplus
18179 && (abbrev->tag == DW_TAG_template_type_param
18180 || abbrev->tag == DW_TAG_template_value_param))
18181 {
18182 parent_die->has_template_arguments = 1;
18183
18184 if (!load_all)
18185 {
18186 /* We don't need a partial DIE for the template argument. */
dee91e82 18187 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18188 continue;
18189 }
18190 }
18191
0d99eb77 18192 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18193 Skip their other children. */
18194 if (!load_all
18195 && cu->language == language_cplus
18196 && parent_die != NULL
18197 && parent_die->tag == DW_TAG_subprogram)
18198 {
dee91e82 18199 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18200 continue;
18201 }
18202
5afb4e99
DJ
18203 /* Check whether this DIE is interesting enough to save. Normally
18204 we would not be interested in members here, but there may be
18205 later variables referencing them via DW_AT_specification (for
18206 static members). */
18207 if (!load_all
18208 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18209 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18210 && abbrev->tag != DW_TAG_enumerator
18211 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18212 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18213 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18214 && abbrev->tag != DW_TAG_variable
5afb4e99 18215 && abbrev->tag != DW_TAG_namespace
f55ee35c 18216 && abbrev->tag != DW_TAG_module
95554aad 18217 && abbrev->tag != DW_TAG_member
74921315
KS
18218 && abbrev->tag != DW_TAG_imported_unit
18219 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18220 {
18221 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18222 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18223 continue;
18224 }
18225
6f06d47b
YQ
18226 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18227 abbrev);
cd9983dd 18228
48fbe735 18229 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18230
18231 /* This two-pass algorithm for processing partial symbols has a
18232 high cost in cache pressure. Thus, handle some simple cases
18233 here which cover the majority of C partial symbols. DIEs
18234 which neither have specification tags in them, nor could have
18235 specification tags elsewhere pointing at them, can simply be
18236 processed and discarded.
18237
18238 This segment is also optional; scan_partial_symbols and
18239 add_partial_symbol will handle these DIEs if we chain
18240 them in normally. When compilers which do not emit large
18241 quantities of duplicate debug information are more common,
18242 this code can probably be removed. */
18243
18244 /* Any complete simple types at the top level (pretty much all
18245 of them, for a language without namespaces), can be processed
18246 directly. */
18247 if (parent_die == NULL
cd9983dd
YQ
18248 && pdi.has_specification == 0
18249 && pdi.is_declaration == 0
18250 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18251 || pdi.tag == DW_TAG_base_type
18252 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18253 {
cd9983dd 18254 if (building_psymtab && pdi.name != NULL)
31edb802 18255 add_psymbol_to_list (pdi.name, false,
79748972 18256 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18257 psymbol_placement::STATIC,
1762568f 18258 0, cu->language, objfile);
cd9983dd 18259 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18260 continue;
18261 }
18262
d8228535
JK
18263 /* The exception for DW_TAG_typedef with has_children above is
18264 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18265 type_name_or_error will error on such types later.
d8228535
JK
18266
18267 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18268 it could not find the child DIEs referenced later, this is checked
18269 above. In correct DWARF DW_TAG_typedef should have no children. */
18270
cd9983dd 18271 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18272 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18273 "- DIE at %s [in module %s]"),
cd9983dd 18274 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18275
72bf9492
DJ
18276 /* If we're at the second level, and we're an enumerator, and
18277 our parent has no specification (meaning possibly lives in a
18278 namespace elsewhere), then we can add the partial symbol now
18279 instead of queueing it. */
cd9983dd 18280 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18281 && parent_die != NULL
18282 && parent_die->die_parent == NULL
18283 && parent_die->tag == DW_TAG_enumeration_type
18284 && parent_die->has_specification == 0)
18285 {
cd9983dd 18286 if (pdi.name == NULL)
b98664d3 18287 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18288 else if (building_psymtab)
31edb802 18289 add_psymbol_to_list (pdi.name, false,
79748972 18290 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18291 cu->language == language_cplus
75aedd27
TT
18292 ? psymbol_placement::GLOBAL
18293 : psymbol_placement::STATIC,
1762568f 18294 0, cu->language, objfile);
72bf9492 18295
cd9983dd 18296 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18297 continue;
18298 }
18299
cd9983dd 18300 struct partial_die_info *part_die
6f06d47b 18301 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18302
72bf9492
DJ
18303 /* We'll save this DIE so link it in. */
18304 part_die->die_parent = parent_die;
18305 part_die->die_sibling = NULL;
18306 part_die->die_child = NULL;
18307
18308 if (last_die && last_die == parent_die)
18309 last_die->die_child = part_die;
18310 else if (last_die)
18311 last_die->die_sibling = part_die;
18312
18313 last_die = part_die;
18314
18315 if (first_die == NULL)
18316 first_die = part_die;
18317
18318 /* Maybe add the DIE to the hash table. Not all DIEs that we
18319 find interesting need to be in the hash table, because we
18320 also have the parent/sibling/child chains; only those that we
18321 might refer to by offset later during partial symbol reading.
18322
18323 For now this means things that might have be the target of a
18324 DW_AT_specification, DW_AT_abstract_origin, or
18325 DW_AT_extension. DW_AT_extension will refer only to
18326 namespaces; DW_AT_abstract_origin refers to functions (and
18327 many things under the function DIE, but we do not recurse
18328 into function DIEs during partial symbol reading) and
18329 possibly variables as well; DW_AT_specification refers to
18330 declarations. Declarations ought to have the DW_AT_declaration
18331 flag. It happens that GCC forgets to put it in sometimes, but
18332 only for functions, not for types.
18333
18334 Adding more things than necessary to the hash table is harmless
18335 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18336 wasted time in find_partial_die, when we reread the compilation
18337 unit with load_all_dies set. */
72bf9492 18338
5afb4e99 18339 if (load_all
72929c62 18340 || abbrev->tag == DW_TAG_constant
5afb4e99 18341 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18342 || abbrev->tag == DW_TAG_variable
18343 || abbrev->tag == DW_TAG_namespace
18344 || part_die->is_declaration)
18345 {
18346 void **slot;
18347
18348 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18349 to_underlying (part_die->sect_off),
18350 INSERT);
72bf9492
DJ
18351 *slot = part_die;
18352 }
18353
72bf9492 18354 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18355 we have no reason to follow the children of structures; for other
98bfdba5
PA
18356 languages we have to, so that we can get at method physnames
18357 to infer fully qualified class names, for DW_AT_specification,
18358 and for C++ template arguments. For C++, we also look one level
18359 inside functions to find template arguments (if the name of the
18360 function does not already contain the template arguments).
bc30ff58 18361
0a4b0913
AB
18362 For Ada and Fortran, we need to scan the children of subprograms
18363 and lexical blocks as well because these languages allow the
18364 definition of nested entities that could be interesting for the
18365 debugger, such as nested subprograms for instance. */
72bf9492 18366 if (last_die->has_children
5afb4e99
DJ
18367 && (load_all
18368 || last_die->tag == DW_TAG_namespace
f55ee35c 18369 || last_die->tag == DW_TAG_module
72bf9492 18370 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18371 || (cu->language == language_cplus
18372 && last_die->tag == DW_TAG_subprogram
18373 && (last_die->name == NULL
18374 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18375 || (cu->language != language_c
18376 && (last_die->tag == DW_TAG_class_type
680b30c7 18377 || last_die->tag == DW_TAG_interface_type
72bf9492 18378 || last_die->tag == DW_TAG_structure_type
bc30ff58 18379 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18380 || ((cu->language == language_ada
18381 || cu->language == language_fortran)
bc30ff58
JB
18382 && (last_die->tag == DW_TAG_subprogram
18383 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18384 {
18385 nesting_level++;
18386 parent_die = last_die;
18387 continue;
18388 }
18389
18390 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18391 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18392
18393 /* Back to the top, do it again. */
18394 }
18395}
18396
6f06d47b
YQ
18397partial_die_info::partial_die_info (sect_offset sect_off_,
18398 struct abbrev_info *abbrev)
18399 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18400{
18401}
18402
35cc7ed7
YQ
18403/* Read a minimal amount of information into the minimal die structure.
18404 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18405
48fbe735
YQ
18406const gdb_byte *
18407partial_die_info::read (const struct die_reader_specs *reader,
18408 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18409{
dee91e82 18410 struct dwarf2_cu *cu = reader->cu;
5e22e966 18411 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18412 unsigned int i;
c5aa993b 18413 int has_low_pc_attr = 0;
c906108c 18414 int has_high_pc_attr = 0;
91da1414 18415 int high_pc_relative = 0;
c906108c 18416
fd0a254f 18417 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18418 {
e7da7f8f 18419 attribute attr;
18a8505e 18420 bool need_reprocess;
e7da7f8f 18421 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18422 info_ptr, &need_reprocess);
18423 /* String and address offsets that need to do the reprocessing have
18424 already been read at this point, so there is no need to wait until
18425 the loop terminates to do the reprocessing. */
18426 if (need_reprocess)
e7da7f8f 18427 read_attribute_reprocess (reader, &attr);
c906108c 18428 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18429 partial symbol table. */
c906108c
SS
18430 switch (attr.name)
18431 {
18432 case DW_AT_name:
48fbe735 18433 switch (tag)
71c25dea
TT
18434 {
18435 case DW_TAG_compile_unit:
95554aad 18436 case DW_TAG_partial_unit:
348e048f 18437 case DW_TAG_type_unit:
71c25dea
TT
18438 /* Compilation units have a DW_AT_name that is a filename, not
18439 a source language identifier. */
18440 case DW_TAG_enumeration_type:
18441 case DW_TAG_enumerator:
18442 /* These tags always have simple identifiers already; no need
18443 to canonicalize them. */
48fbe735 18444 name = DW_STRING (&attr);
71c25dea
TT
18445 break;
18446 default:
48fbe735
YQ
18447 {
18448 struct objfile *objfile = dwarf2_per_objfile->objfile;
18449
18450 name
be1e3d3e 18451 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18452 }
71c25dea
TT
18453 break;
18454 }
c906108c 18455 break;
31ef98ae 18456 case DW_AT_linkage_name:
c906108c 18457 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18458 /* Note that both forms of linkage name might appear. We
18459 assume they will be the same, and we only store the last
18460 one we see. */
e61108c9 18461 linkage_name = attr.value_as_string ();
787de330
TT
18462 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18463 See https://github.com/rust-lang/rust/issues/32925. */
18464 if (cu->language == language_rust && linkage_name != NULL
18465 && strchr (linkage_name, '{') != NULL)
18466 linkage_name = NULL;
c906108c
SS
18467 break;
18468 case DW_AT_low_pc:
18469 has_low_pc_attr = 1;
cd6c91b4 18470 lowpc = attr.value_as_address ();
c906108c
SS
18471 break;
18472 case DW_AT_high_pc:
18473 has_high_pc_attr = 1;
cd6c91b4
TT
18474 highpc = attr.value_as_address ();
18475 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18476 high_pc_relative = 1;
c906108c
SS
18477 break;
18478 case DW_AT_location:
0963b4bd 18479 /* Support the .debug_loc offsets. */
4fc6c0d5 18480 if (attr.form_is_block ())
8e19ed76 18481 {
48fbe735 18482 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18483 }
cd6c91b4 18484 else if (attr.form_is_section_offset ())
8e19ed76 18485 {
4d3c2250 18486 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18487 }
18488 else
18489 {
4d3c2250
KB
18490 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18491 "partial symbol information");
8e19ed76 18492 }
c906108c 18493 break;
c906108c 18494 case DW_AT_external:
48fbe735 18495 is_external = DW_UNSND (&attr);
c906108c
SS
18496 break;
18497 case DW_AT_declaration:
48fbe735 18498 is_declaration = DW_UNSND (&attr);
c906108c
SS
18499 break;
18500 case DW_AT_type:
48fbe735 18501 has_type = 1;
c906108c
SS
18502 break;
18503 case DW_AT_abstract_origin:
18504 case DW_AT_specification:
72bf9492 18505 case DW_AT_extension:
48fbe735 18506 has_specification = 1;
0826b30a 18507 spec_offset = attr.get_ref_die_offset ();
48fbe735 18508 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18509 || cu->per_cu->is_dwz);
c906108c
SS
18510 break;
18511 case DW_AT_sibling:
18512 /* Ignore absolute siblings, they might point outside of
18513 the current compile unit. */
18514 if (attr.form == DW_FORM_ref_addr)
b98664d3 18515 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18516 else
b9502d3f 18517 {
48fbe735 18518 const gdb_byte *buffer = reader->buffer;
0826b30a 18519 sect_offset off = attr.get_ref_die_offset ();
9c541725 18520 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18521
18522 if (sibling_ptr < info_ptr)
b98664d3 18523 complaint (_("DW_AT_sibling points backwards"));
22869d73 18524 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18525 reader->die_section->overflow_complaint ();
b9502d3f 18526 else
48fbe735 18527 sibling = sibling_ptr;
b9502d3f 18528 }
c906108c 18529 break;
fa4028e9 18530 case DW_AT_byte_size:
48fbe735 18531 has_byte_size = 1;
fa4028e9 18532 break;
ff908ebf 18533 case DW_AT_const_value:
48fbe735 18534 has_const_value = 1;
ff908ebf 18535 break;
68511cec
CES
18536 case DW_AT_calling_convention:
18537 /* DWARF doesn't provide a way to identify a program's source-level
18538 entry point. DW_AT_calling_convention attributes are only meant
18539 to describe functions' calling conventions.
18540
18541 However, because it's a necessary piece of information in
0c1b455e
TT
18542 Fortran, and before DWARF 4 DW_CC_program was the only
18543 piece of debugging information whose definition refers to
18544 a 'main program' at all, several compilers marked Fortran
18545 main programs with DW_CC_program --- even when those
18546 functions use the standard calling conventions.
18547
18548 Although DWARF now specifies a way to provide this
18549 information, we support this practice for backward
18550 compatibility. */
68511cec 18551 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18552 && cu->language == language_fortran)
48fbe735 18553 main_subprogram = 1;
68511cec 18554 break;
481860b3
GB
18555 case DW_AT_inline:
18556 if (DW_UNSND (&attr) == DW_INL_inlined
18557 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18558 may_be_inlined = 1;
481860b3 18559 break;
95554aad
TT
18560
18561 case DW_AT_import:
48fbe735 18562 if (tag == DW_TAG_imported_unit)
36586728 18563 {
0826b30a 18564 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18565 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18566 || cu->per_cu->is_dwz);
18567 }
95554aad
TT
18568 break;
18569
0c1b455e 18570 case DW_AT_main_subprogram:
48fbe735 18571 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18572 break;
18573
05caa1d2
TT
18574 case DW_AT_ranges:
18575 {
18576 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18577 but that requires a full DIE, so instead we just
18578 reimplement it. */
18579 int need_ranges_base = tag != DW_TAG_compile_unit;
18580 unsigned int ranges_offset = (DW_UNSND (&attr)
18581 + (need_ranges_base
18582 ? cu->ranges_base
18583 : 0));
18584
18585 /* Value of the DW_AT_ranges attribute is the offset in the
18586 .debug_ranges section. */
18587 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18588 nullptr))
18589 has_pc_info = 1;
18590 }
18591 break;
18592
c906108c
SS
18593 default:
18594 break;
18595 }
18596 }
18597
10d06d82
TT
18598 /* For Ada, if both the name and the linkage name appear, we prefer
18599 the latter. This lets "catch exception" work better, regardless
18600 of the order in which the name and linkage name were emitted.
18601 Really, though, this is just a workaround for the fact that gdb
18602 doesn't store both the name and the linkage name. */
18603 if (cu->language == language_ada && linkage_name != nullptr)
18604 name = linkage_name;
18605
91da1414 18606 if (high_pc_relative)
48fbe735 18607 highpc += lowpc;
91da1414 18608
9373cf26
JK
18609 if (has_low_pc_attr && has_high_pc_attr)
18610 {
18611 /* When using the GNU linker, .gnu.linkonce. sections are used to
18612 eliminate duplicate copies of functions and vtables and such.
18613 The linker will arbitrarily choose one and discard the others.
18614 The AT_*_pc values for such functions refer to local labels in
18615 these sections. If the section from that file was discarded, the
18616 labels are not in the output, so the relocs get a value of 0.
18617 If this is a discarded function, mark the pc bounds as invalid,
18618 so that GDB will ignore it. */
5989a64e 18619 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18620 {
48fbe735 18621 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18622 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18623
b98664d3 18624 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18625 "for DIE at %s [in module %s]"),
48fbe735
YQ
18626 paddress (gdbarch, lowpc),
18627 sect_offset_str (sect_off),
9d8780f0 18628 objfile_name (objfile));
9373cf26
JK
18629 }
18630 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18631 else if (lowpc >= highpc)
9373cf26 18632 {
48fbe735 18633 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18634 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18635
b98664d3 18636 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18637 "for DIE at %s [in module %s]"),
48fbe735
YQ
18638 paddress (gdbarch, lowpc),
18639 paddress (gdbarch, highpc),
18640 sect_offset_str (sect_off),
9c541725 18641 objfile_name (objfile));
9373cf26
JK
18642 }
18643 else
48fbe735 18644 has_pc_info = 1;
9373cf26 18645 }
85cbf3d3 18646
c906108c
SS
18647 return info_ptr;
18648}
18649
72bf9492
DJ
18650/* Find a cached partial DIE at OFFSET in CU. */
18651
d590ff25
YQ
18652struct partial_die_info *
18653dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18654{
18655 struct partial_die_info *lookup_die = NULL;
6f06d47b 18656 struct partial_die_info part_die (sect_off);
72bf9492 18657
9a3c8263 18658 lookup_die = ((struct partial_die_info *)
d590ff25 18659 htab_find_with_hash (partial_dies, &part_die,
9c541725 18660 to_underlying (sect_off)));
72bf9492 18661
72bf9492
DJ
18662 return lookup_die;
18663}
18664
348e048f
DE
18665/* Find a partial DIE at OFFSET, which may or may not be in CU,
18666 except in the case of .debug_types DIEs which do not reference
18667 outside their CU (they do however referencing other types via
55f1336d 18668 DW_FORM_ref_sig8). */
72bf9492 18669
122cf0f2 18670static const struct cu_partial_die_info
9c541725 18671find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18672{
5e22e966 18673 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18674 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18675 struct dwarf2_per_cu_data *per_cu = NULL;
18676 struct partial_die_info *pd = NULL;
72bf9492 18677
36586728 18678 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18679 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18680 {
d590ff25 18681 pd = cu->find_partial_die (sect_off);
5afb4e99 18682 if (pd != NULL)
fb816e8b 18683 return { cu, pd };
0d99eb77
DE
18684 /* We missed recording what we needed.
18685 Load all dies and try again. */
18686 per_cu = cu->per_cu;
5afb4e99 18687 }
0d99eb77
DE
18688 else
18689 {
18690 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18691 if (cu->per_cu->is_debug_types)
0d99eb77 18692 {
9d8780f0
SM
18693 error (_("Dwarf Error: Type Unit at offset %s contains"
18694 " external reference to offset %s [in module %s].\n"),
18695 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18696 bfd_get_filename (objfile->obfd));
18697 }
9c541725 18698 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18699 dwarf2_per_objfile);
72bf9492 18700
0d99eb77 18701 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
ab432490 18702 load_partial_comp_unit (per_cu, cu->per_objfile);
ae038cb0 18703
0d99eb77 18704 per_cu->cu->last_used = 0;
d590ff25 18705 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18706 }
5afb4e99 18707
dee91e82
DE
18708 /* If we didn't find it, and not all dies have been loaded,
18709 load them all and try again. */
18710
5afb4e99
DJ
18711 if (pd == NULL && per_cu->load_all_dies == 0)
18712 {
5afb4e99 18713 per_cu->load_all_dies = 1;
fd820528
DE
18714
18715 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18716 THIS_CU->cu may already be in use. So we can't just free it and
18717 replace its DIEs with the ones we read in. Instead, we leave those
18718 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18719 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18720 set. */
ab432490 18721 load_partial_comp_unit (per_cu, cu->per_objfile);
5afb4e99 18722
d590ff25 18723 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18724 }
18725
18726 if (pd == NULL)
18727 internal_error (__FILE__, __LINE__,
9d8780f0 18728 _("could not find partial DIE %s "
3e43a32a 18729 "in cache [from module %s]\n"),
9d8780f0 18730 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18731 return { per_cu->cu, pd };
72bf9492
DJ
18732}
18733
abc72ce4
DE
18734/* See if we can figure out if the class lives in a namespace. We do
18735 this by looking for a member function; its demangled name will
18736 contain namespace info, if there is any. */
18737
18738static void
18739guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18740 struct dwarf2_cu *cu)
18741{
18742 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18743 what template types look like, because the demangler
18744 frequently doesn't give the same name as the debug info. We
18745 could fix this by only using the demangled name to get the
18746 prefix (but see comment in read_structure_type). */
18747
18748 struct partial_die_info *real_pdi;
18749 struct partial_die_info *child_pdi;
18750
18751 /* If this DIE (this DIE's specification, if any) has a parent, then
18752 we should not do this. We'll prepend the parent's fully qualified
18753 name when we create the partial symbol. */
18754
18755 real_pdi = struct_pdi;
18756 while (real_pdi->has_specification)
fb816e8b 18757 {
122cf0f2
AB
18758 auto res = find_partial_die (real_pdi->spec_offset,
18759 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18760 real_pdi = res.pdi;
18761 cu = res.cu;
18762 }
abc72ce4
DE
18763
18764 if (real_pdi->die_parent != NULL)
18765 return;
18766
18767 for (child_pdi = struct_pdi->die_child;
18768 child_pdi != NULL;
18769 child_pdi = child_pdi->die_sibling)
18770 {
18771 if (child_pdi->tag == DW_TAG_subprogram
18772 && child_pdi->linkage_name != NULL)
18773 {
43816ebc
TT
18774 gdb::unique_xmalloc_ptr<char> actual_class_name
18775 (language_class_name_from_physname (cu->language_defn,
18776 child_pdi->linkage_name));
abc72ce4
DE
18777 if (actual_class_name != NULL)
18778 {
5e22e966 18779 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18780 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18781 }
18782 break;
18783 }
18784 }
18785}
18786
25c11aca
TV
18787/* Return true if a DIE with TAG may have the DW_AT_const_value
18788 attribute. */
18789
18790static bool
18791can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18792{
18793 switch (tag)
18794 {
18795 case DW_TAG_constant:
18796 case DW_TAG_enumerator:
18797 case DW_TAG_formal_parameter:
18798 case DW_TAG_template_value_param:
18799 case DW_TAG_variable:
18800 return true;
18801 }
18802
18803 return false;
18804}
18805
52356b79
YQ
18806void
18807partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18808{
abc72ce4
DE
18809 /* Once we've fixed up a die, there's no point in doing so again.
18810 This also avoids a memory leak if we were to call
18811 guess_partial_die_structure_name multiple times. */
52356b79 18812 if (fixup_called)
abc72ce4
DE
18813 return;
18814
72bf9492
DJ
18815 /* If we found a reference attribute and the DIE has no name, try
18816 to find a name in the referred to DIE. */
18817
52356b79 18818 if (name == NULL && has_specification)
72bf9492
DJ
18819 {
18820 struct partial_die_info *spec_die;
72bf9492 18821
122cf0f2 18822 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18823 spec_die = res.pdi;
18824 cu = res.cu;
72bf9492 18825
52356b79 18826 spec_die->fixup (cu);
72bf9492
DJ
18827
18828 if (spec_die->name)
18829 {
52356b79 18830 name = spec_die->name;
72bf9492
DJ
18831
18832 /* Copy DW_AT_external attribute if it is set. */
18833 if (spec_die->is_external)
52356b79 18834 is_external = spec_die->is_external;
72bf9492
DJ
18835 }
18836 }
18837
25c11aca
TV
18838 if (!has_const_value && has_specification
18839 && can_have_DW_AT_const_value_p (tag))
18840 {
18841 struct partial_die_info *spec_die;
18842
18843 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18844 spec_die = res.pdi;
18845 cu = res.cu;
18846
18847 spec_die->fixup (cu);
18848
18849 if (spec_die->has_const_value)
18850 {
18851 /* Copy DW_AT_const_value attribute if it is set. */
18852 has_const_value = spec_die->has_const_value;
18853 }
18854 }
18855
72bf9492 18856 /* Set default names for some unnamed DIEs. */
72bf9492 18857
52356b79
YQ
18858 if (name == NULL && tag == DW_TAG_namespace)
18859 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18860
abc72ce4
DE
18861 /* If there is no parent die to provide a namespace, and there are
18862 children, see if we can determine the namespace from their linkage
122d1940 18863 name. */
abc72ce4 18864 if (cu->language == language_cplus
5e22e966 18865 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18866 && die_parent == NULL
18867 && has_children
18868 && (tag == DW_TAG_class_type
18869 || tag == DW_TAG_structure_type
18870 || tag == DW_TAG_union_type))
18871 guess_partial_die_structure_name (this, cu);
abc72ce4 18872
53832f31
TT
18873 /* GCC might emit a nameless struct or union that has a linkage
18874 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18875 if (name == NULL
18876 && (tag == DW_TAG_class_type
18877 || tag == DW_TAG_interface_type
18878 || tag == DW_TAG_structure_type
18879 || tag == DW_TAG_union_type)
18880 && linkage_name != NULL)
53832f31 18881 {
43816ebc
TT
18882 gdb::unique_xmalloc_ptr<char> demangled
18883 (gdb_demangle (linkage_name, DMGL_TYPES));
18884 if (demangled != nullptr)
53832f31 18885 {
96408a79
SA
18886 const char *base;
18887
18888 /* Strip any leading namespaces/classes, keep only the base name.
18889 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18890 base = strrchr (demangled.get (), ':');
18891 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18892 base++;
18893 else
43816ebc 18894 base = demangled.get ();
96408a79 18895
5e22e966 18896 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18897 name = objfile->intern (base);
53832f31
TT
18898 }
18899 }
18900
52356b79 18901 fixup_called = 1;
72bf9492
DJ
18902}
18903
41144253 18904/* Read the .debug_loclists header contents from the given SECTION in the
18905 HEADER. */
18906static void
18907read_loclist_header (struct loclist_header *header,
18908 struct dwarf2_section_info *section)
18909{
18910 unsigned int bytes_read;
18911 bfd *abfd = section->get_bfd_owner ();
18912 const gdb_byte *info_ptr = section->buffer;
18913 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18914 info_ptr += bytes_read;
18915 header->version = read_2_bytes (abfd, info_ptr);
18916 info_ptr += 2;
18917 header->addr_size = read_1_byte (abfd, info_ptr);
18918 info_ptr += 1;
18919 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18920 info_ptr += 1;
18921 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18922}
18923
18924/* Return the DW_AT_loclists_base value for the CU. */
18925static ULONGEST
18926lookup_loclist_base (struct dwarf2_cu *cu)
18927{
18928 /* For the .dwo unit, the loclist_base points to the first offset following
18929 the header. The header consists of the following entities-
18930 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18931 bit format)
18932 2. version (2 bytes)
18933 3. address size (1 byte)
18934 4. segment selector size (1 byte)
18935 5. offset entry count (4 bytes)
18936 These sizes are derived as per the DWARFv5 standard. */
18937 if (cu->dwo_unit != nullptr)
18938 {
18939 if (cu->header.initial_length_size == 4)
18940 return LOCLIST_HEADER_SIZE32;
18941 return LOCLIST_HEADER_SIZE64;
18942 }
18943 return cu->loclist_base;
18944}
18945
18946/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18947 array of offsets in the .debug_loclists section. */
18948static CORE_ADDR
18949read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18950{
5e22e966 18951 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 18952 struct objfile *objfile = dwarf2_per_objfile->objfile;
18953 bfd *abfd = objfile->obfd;
18954 ULONGEST loclist_base = lookup_loclist_base (cu);
18955 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18956
18957 section->read (objfile);
18958 if (section->buffer == NULL)
18959 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18960 "section [in module %s]"), objfile_name (objfile));
18961 struct loclist_header header;
18962 read_loclist_header (&header, section);
18963 if (loclist_index >= header.offset_entry_count)
18964 complaint (_("DW_FORM_loclistx pointing outside of "
18965 ".debug_loclists offset array [in module %s]"),
18966 objfile_name (objfile));
18967 if (loclist_base + loclist_index * cu->header.offset_size
18968 >= section->size)
18969 complaint (_("DW_FORM_loclistx pointing outside of "
18970 ".debug_loclists section [in module %s]"),
18971 objfile_name (objfile));
18972 const gdb_byte *info_ptr
18973 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18974
18975 if (cu->header.offset_size == 4)
18976 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18977 else
18978 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18979}
18980
18a8505e
AT
18981/* Process the attributes that had to be skipped in the first round. These
18982 attributes are the ones that need str_offsets_base or addr_base attributes.
18983 They could not have been processed in the first round, because at the time
18984 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18985static void
18986read_attribute_reprocess (const struct die_reader_specs *reader,
18987 struct attribute *attr)
18a8505e
AT
18988{
18989 struct dwarf2_cu *cu = reader->cu;
18990 switch (attr->form)
18991 {
18992 case DW_FORM_addrx:
18993 case DW_FORM_GNU_addr_index:
18994 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18995 break;
41144253 18996 case DW_FORM_loclistx:
18997 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18998 break;
18a8505e
AT
18999 case DW_FORM_strx:
19000 case DW_FORM_strx1:
19001 case DW_FORM_strx2:
19002 case DW_FORM_strx3:
19003 case DW_FORM_strx4:
19004 case DW_FORM_GNU_str_index:
19005 {
19006 unsigned int str_index = DW_UNSND (attr);
19007 if (reader->dwo_file != NULL)
19008 {
19009 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19010 DW_STRING_IS_CANONICAL (attr) = 0;
19011 }
19012 else
19013 {
19014 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19015 DW_STRING_IS_CANONICAL (attr) = 0;
19016 }
19017 break;
19018 }
19019 default:
19020 gdb_assert_not_reached (_("Unexpected DWARF form."));
19021 }
19022}
19023
a8329558 19024/* Read an attribute value described by an attribute form. */
c906108c 19025
d521ce57 19026static const gdb_byte *
dee91e82
DE
19027read_attribute_value (const struct die_reader_specs *reader,
19028 struct attribute *attr, unsigned form,
18a8505e
AT
19029 LONGEST implicit_const, const gdb_byte *info_ptr,
19030 bool *need_reprocess)
c906108c 19031{
dee91e82 19032 struct dwarf2_cu *cu = reader->cu;
5e22e966 19033 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19034 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19035 bfd *abfd = reader->abfd;
e7c27a73 19036 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19037 unsigned int bytes_read;
19038 struct dwarf_block *blk;
18a8505e 19039 *need_reprocess = false;
c906108c 19040
aead7601 19041 attr->form = (enum dwarf_form) form;
a8329558 19042 switch (form)
c906108c 19043 {
c906108c 19044 case DW_FORM_ref_addr:
ae411497 19045 if (cu->header.version == 2)
c8a7a66f
TT
19046 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19047 &bytes_read);
ae411497 19048 else
8266302d
TT
19049 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19050 &bytes_read);
ae411497
TT
19051 info_ptr += bytes_read;
19052 break;
36586728 19053 case DW_FORM_GNU_ref_alt:
8266302d 19054 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19055 info_ptr += bytes_read;
19056 break;
ae411497 19057 case DW_FORM_addr:
08feed99
TT
19058 {
19059 struct gdbarch *gdbarch = objfile->arch ();
19060 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19061 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19062 info_ptr += bytes_read;
19063 }
c906108c
SS
19064 break;
19065 case DW_FORM_block2:
7b5a2f43 19066 blk = dwarf_alloc_block (cu);
c906108c
SS
19067 blk->size = read_2_bytes (abfd, info_ptr);
19068 info_ptr += 2;
19069 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19070 info_ptr += blk->size;
19071 DW_BLOCK (attr) = blk;
19072 break;
19073 case DW_FORM_block4:
7b5a2f43 19074 blk = dwarf_alloc_block (cu);
c906108c
SS
19075 blk->size = read_4_bytes (abfd, info_ptr);
19076 info_ptr += 4;
19077 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19078 info_ptr += blk->size;
19079 DW_BLOCK (attr) = blk;
19080 break;
19081 case DW_FORM_data2:
19082 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19083 info_ptr += 2;
19084 break;
19085 case DW_FORM_data4:
19086 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19087 info_ptr += 4;
19088 break;
19089 case DW_FORM_data8:
19090 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19091 info_ptr += 8;
19092 break;
0224619f
JK
19093 case DW_FORM_data16:
19094 blk = dwarf_alloc_block (cu);
19095 blk->size = 16;
19096 blk->data = read_n_bytes (abfd, info_ptr, 16);
19097 info_ptr += 16;
19098 DW_BLOCK (attr) = blk;
19099 break;
2dc7f7b3 19100 case DW_FORM_sec_offset:
8266302d 19101 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19102 info_ptr += bytes_read;
19103 break;
41144253 19104 case DW_FORM_loclistx:
19105 {
19106 *need_reprocess = true;
19107 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19108 info_ptr += bytes_read;
19109 }
19110 break;
c906108c 19111 case DW_FORM_string:
9b1c24c8 19112 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19113 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19114 info_ptr += bytes_read;
19115 break;
4bdf3d34 19116 case DW_FORM_strp:
36586728
TT
19117 if (!cu->per_cu->is_dwz)
19118 {
ed2dc618
SM
19119 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19120 abfd, info_ptr, cu_header,
36586728
TT
19121 &bytes_read);
19122 DW_STRING_IS_CANONICAL (attr) = 0;
19123 info_ptr += bytes_read;
19124 break;
19125 }
19126 /* FALLTHROUGH */
43988095
JK
19127 case DW_FORM_line_strp:
19128 if (!cu->per_cu->is_dwz)
19129 {
86c0bb4c
TT
19130 DW_STRING (attr)
19131 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19132 &bytes_read);
43988095
JK
19133 DW_STRING_IS_CANONICAL (attr) = 0;
19134 info_ptr += bytes_read;
19135 break;
19136 }
19137 /* FALLTHROUGH */
36586728
TT
19138 case DW_FORM_GNU_strp_alt:
19139 {
c3699833 19140 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19141 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19142 &bytes_read);
36586728 19143
0314b390 19144 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19145 DW_STRING_IS_CANONICAL (attr) = 0;
19146 info_ptr += bytes_read;
19147 }
4bdf3d34 19148 break;
2dc7f7b3 19149 case DW_FORM_exprloc:
c906108c 19150 case DW_FORM_block:
7b5a2f43 19151 blk = dwarf_alloc_block (cu);
c906108c
SS
19152 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19153 info_ptr += bytes_read;
19154 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19155 info_ptr += blk->size;
19156 DW_BLOCK (attr) = blk;
19157 break;
19158 case DW_FORM_block1:
7b5a2f43 19159 blk = dwarf_alloc_block (cu);
c906108c
SS
19160 blk->size = read_1_byte (abfd, info_ptr);
19161 info_ptr += 1;
19162 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19163 info_ptr += blk->size;
19164 DW_BLOCK (attr) = blk;
19165 break;
19166 case DW_FORM_data1:
19167 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19168 info_ptr += 1;
19169 break;
19170 case DW_FORM_flag:
19171 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19172 info_ptr += 1;
19173 break;
2dc7f7b3
TT
19174 case DW_FORM_flag_present:
19175 DW_UNSND (attr) = 1;
19176 break;
c906108c
SS
19177 case DW_FORM_sdata:
19178 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19179 info_ptr += bytes_read;
19180 break;
19181 case DW_FORM_udata:
18a8505e 19182 case DW_FORM_rnglistx:
c906108c
SS
19183 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19184 info_ptr += bytes_read;
19185 break;
19186 case DW_FORM_ref1:
9c541725 19187 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19188 + read_1_byte (abfd, info_ptr));
c906108c
SS
19189 info_ptr += 1;
19190 break;
19191 case DW_FORM_ref2:
9c541725 19192 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19193 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19194 info_ptr += 2;
19195 break;
19196 case DW_FORM_ref4:
9c541725 19197 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19198 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19199 info_ptr += 4;
19200 break;
613e1657 19201 case DW_FORM_ref8:
9c541725 19202 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19203 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19204 info_ptr += 8;
19205 break;
55f1336d 19206 case DW_FORM_ref_sig8:
ac9ec31b 19207 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19208 info_ptr += 8;
19209 break;
c906108c 19210 case DW_FORM_ref_udata:
9c541725 19211 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19212 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19213 info_ptr += bytes_read;
19214 break;
c906108c 19215 case DW_FORM_indirect:
a8329558
KW
19216 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19217 info_ptr += bytes_read;
43988095
JK
19218 if (form == DW_FORM_implicit_const)
19219 {
19220 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19221 info_ptr += bytes_read;
19222 }
19223 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19224 info_ptr, need_reprocess);
43988095
JK
19225 break;
19226 case DW_FORM_implicit_const:
19227 DW_SND (attr) = implicit_const;
a8329558 19228 break;
336d760d 19229 case DW_FORM_addrx:
3019eac3 19230 case DW_FORM_GNU_addr_index:
18a8505e
AT
19231 *need_reprocess = true;
19232 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19233 info_ptr += bytes_read;
19234 break;
cf532bd1 19235 case DW_FORM_strx:
15f18d14
AT
19236 case DW_FORM_strx1:
19237 case DW_FORM_strx2:
19238 case DW_FORM_strx3:
19239 case DW_FORM_strx4:
3019eac3 19240 case DW_FORM_GNU_str_index:
3019eac3 19241 {
15f18d14
AT
19242 ULONGEST str_index;
19243 if (form == DW_FORM_strx1)
19244 {
19245 str_index = read_1_byte (abfd, info_ptr);
19246 info_ptr += 1;
19247 }
19248 else if (form == DW_FORM_strx2)
19249 {
19250 str_index = read_2_bytes (abfd, info_ptr);
19251 info_ptr += 2;
19252 }
19253 else if (form == DW_FORM_strx3)
19254 {
19255 str_index = read_3_bytes (abfd, info_ptr);
19256 info_ptr += 3;
19257 }
19258 else if (form == DW_FORM_strx4)
19259 {
19260 str_index = read_4_bytes (abfd, info_ptr);
19261 info_ptr += 4;
19262 }
19263 else
19264 {
19265 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19266 info_ptr += bytes_read;
19267 }
18a8505e
AT
19268 *need_reprocess = true;
19269 DW_UNSND (attr) = str_index;
19270 }
3019eac3 19271 break;
c906108c 19272 default:
8a3fe4f8 19273 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19274 dwarf_form_name (form),
19275 bfd_get_filename (abfd));
c906108c 19276 }
28e94949 19277
36586728 19278 /* Super hack. */
cd6c91b4 19279 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19280 attr->form = DW_FORM_GNU_ref_alt;
19281
28e94949
JB
19282 /* We have seen instances where the compiler tried to emit a byte
19283 size attribute of -1 which ended up being encoded as an unsigned
19284 0xffffffff. Although 0xffffffff is technically a valid size value,
19285 an object of this size seems pretty unlikely so we can relatively
19286 safely treat these cases as if the size attribute was invalid and
19287 treat them as zero by default. */
19288 if (attr->name == DW_AT_byte_size
19289 && form == DW_FORM_data4
19290 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19291 {
19292 complaint
b98664d3 19293 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19294 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19295 DW_UNSND (attr) = 0;
19296 }
28e94949 19297
c906108c
SS
19298 return info_ptr;
19299}
19300
a8329558
KW
19301/* Read an attribute described by an abbreviated attribute. */
19302
d521ce57 19303static const gdb_byte *
dee91e82
DE
19304read_attribute (const struct die_reader_specs *reader,
19305 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19306 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19307{
19308 attr->name = abbrev->name;
43988095 19309 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19310 abbrev->implicit_const, info_ptr,
19311 need_reprocess);
a8329558
KW
19312}
19313
43988095
JK
19314/* Return pointer to string at .debug_str offset STR_OFFSET. */
19315
19316static const char *
ed2dc618 19317read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19318 LONGEST str_offset)
43988095 19319{
5989a64e
SM
19320 return dwarf2_per_objfile->per_bfd->str.read_string
19321 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19322}
19323
43988095
JK
19324/* Return pointer to string at .debug_str offset as read from BUF.
19325 BUF is assumed to be in a compilation unit described by CU_HEADER.
19326 Return *BYTES_READ_PTR count of bytes read from BUF. */
19327
d521ce57 19328static const char *
ed2dc618
SM
19329read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19330 const gdb_byte *buf,
cf2c3c16
TT
19331 const struct comp_unit_head *cu_header,
19332 unsigned int *bytes_read_ptr)
19333{
8266302d 19334 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19335
4f44ae6c 19336 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19337}
19338
86c0bb4c 19339/* See read.h. */
43988095 19340
86c0bb4c
TT
19341const char *
19342dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19343 const struct comp_unit_head *cu_header,
19344 unsigned int *bytes_read_ptr)
43988095 19345{
86c0bb4c 19346 bfd *abfd = objfile->obfd;
8266302d 19347 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19348
5989a64e 19349 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19350}
19351
3019eac3 19352/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19353 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19354 ADDR_SIZE is the size of addresses from the CU header. */
19355
19356static CORE_ADDR
ed2dc618 19357read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19358 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19359 int addr_size)
3019eac3
DE
19360{
19361 struct objfile *objfile = dwarf2_per_objfile->objfile;
19362 bfd *abfd = objfile->obfd;
19363 const gdb_byte *info_ptr;
18a8505e 19364 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19365
5989a64e
SM
19366 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19367 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19368 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19369 objfile_name (objfile));
18a8505e 19370 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19371 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19372 error (_("DW_FORM_addr_index pointing outside of "
19373 ".debug_addr section [in module %s]"),
4262abfb 19374 objfile_name (objfile));
5989a64e 19375 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19376 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19377 if (addr_size == 4)
19378 return bfd_get_32 (abfd, info_ptr);
19379 else
19380 return bfd_get_64 (abfd, info_ptr);
19381}
19382
19383/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19384
19385static CORE_ADDR
19386read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19387{
5e22e966 19388 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19389 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19390}
19391
19392/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19393
19394static CORE_ADDR
d521ce57 19395read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19396 unsigned int *bytes_read)
19397{
5e22e966 19398 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19399 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19400
19401 return read_addr_index (cu, addr_index);
19402}
19403
450a1bfc 19404/* See read.h. */
3019eac3
DE
19405
19406CORE_ADDR
450a1bfc 19407dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 19408{
ed2dc618 19409 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19410 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19411 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19412 int addr_size;
19413
3019eac3
DE
19414 /* We need addr_base and addr_size.
19415 If we don't have PER_CU->cu, we have to get it.
19416 Nasty, but the alternative is storing the needed info in PER_CU,
19417 which at this point doesn't seem justified: it's not clear how frequently
19418 it would get used and it would increase the size of every PER_CU.
19419 Entry points like dwarf2_per_cu_addr_size do a similar thing
19420 so we're not in uncharted territory here.
19421 Alas we need to be a bit more complicated as addr_base is contained
19422 in the DIE.
19423
19424 We don't need to read the entire CU(/TU).
19425 We just need the header and top level die.
a1b64ce1 19426
3019eac3 19427 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19428 For now we skip this optimization. */
3019eac3
DE
19429
19430 if (cu != NULL)
19431 {
19432 addr_base = cu->addr_base;
19433 addr_size = cu->header.addr_size;
19434 }
19435 else
19436 {
ab432490 19437 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
19438 addr_base = reader.cu->addr_base;
19439 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19440 }
19441
ed2dc618
SM
19442 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19443 addr_size);
3019eac3
DE
19444}
19445
18a8505e
AT
19446/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19447 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19448 DWO file. */
3019eac3 19449
d521ce57 19450static const char *
18a8505e
AT
19451read_str_index (struct dwarf2_cu *cu,
19452 struct dwarf2_section_info *str_section,
19453 struct dwarf2_section_info *str_offsets_section,
19454 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19455{
5e22e966 19456 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19457 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19458 const char *objf_name = objfile_name (objfile);
3019eac3 19459 bfd *abfd = objfile->obfd;
d521ce57 19460 const gdb_byte *info_ptr;
3019eac3 19461 ULONGEST str_offset;
cf532bd1 19462 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19463
96b79293
TT
19464 str_section->read (objfile);
19465 str_offsets_section->read (objfile);
73869dc2 19466 if (str_section->buffer == NULL)
18a8505e 19467 error (_("%s used without %s section"
9d8780f0 19468 " in CU at offset %s [in module %s]"),
96b79293 19469 form_name, str_section->get_name (),
18a8505e 19470 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19471 if (str_offsets_section->buffer == NULL)
18a8505e 19472 error (_("%s used without %s section"
9d8780f0 19473 " in CU at offset %s [in module %s]"),
96b79293 19474 form_name, str_section->get_name (),
18a8505e 19475 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19476 info_ptr = (str_offsets_section->buffer
18a8505e 19477 + str_offsets_base
3019eac3
DE
19478 + str_index * cu->header.offset_size);
19479 if (cu->header.offset_size == 4)
19480 str_offset = bfd_get_32 (abfd, info_ptr);
19481 else
19482 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19483 if (str_offset >= str_section->size)
57d63ce2 19484 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19485 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19486 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19487 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19488}
19489
18a8505e
AT
19490/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19491
19492static const char *
19493read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19494{
19495 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19496 ? reader->cu->header.addr_size : 0;
19497 return read_str_index (reader->cu,
19498 &reader->dwo_file->sections.str,
19499 &reader->dwo_file->sections.str_offsets,
19500 str_offsets_base, str_index);
19501}
19502
19503/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19504
19505static const char *
19506read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19507{
5e22e966 19508 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19509 const char *objf_name = objfile_name (objfile);
19510 static const char form_name[] = "DW_FORM_GNU_str_index";
19511 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19512
19513 if (!cu->str_offsets_base.has_value ())
19514 error (_("%s used in Fission stub without %s"
19515 " in CU at offset 0x%lx [in module %s]"),
19516 form_name, str_offsets_attr_name,
19517 (long) cu->header.offset_size, objf_name);
19518
19519 return read_str_index (cu,
5e22e966
SM
19520 &cu->per_objfile->per_bfd->str,
19521 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19522 *cu->str_offsets_base, str_index);
19523}
19524
3019eac3
DE
19525/* Return the length of an LEB128 number in BUF. */
19526
19527static int
19528leb128_size (const gdb_byte *buf)
19529{
19530 const gdb_byte *begin = buf;
19531 gdb_byte byte;
19532
19533 while (1)
19534 {
19535 byte = *buf++;
19536 if ((byte & 128) == 0)
19537 return buf - begin;
19538 }
19539}
19540
c906108c 19541static void
e142c38c 19542set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19543{
19544 switch (lang)
19545 {
19546 case DW_LANG_C89:
76bee0cc 19547 case DW_LANG_C99:
0cfd832f 19548 case DW_LANG_C11:
c906108c 19549 case DW_LANG_C:
d1be3247 19550 case DW_LANG_UPC:
e142c38c 19551 cu->language = language_c;
c906108c 19552 break;
9c37b5ae 19553 case DW_LANG_Java:
c906108c 19554 case DW_LANG_C_plus_plus:
0cfd832f
MW
19555 case DW_LANG_C_plus_plus_11:
19556 case DW_LANG_C_plus_plus_14:
e142c38c 19557 cu->language = language_cplus;
c906108c 19558 break;
6aecb9c2
JB
19559 case DW_LANG_D:
19560 cu->language = language_d;
19561 break;
c906108c
SS
19562 case DW_LANG_Fortran77:
19563 case DW_LANG_Fortran90:
b21b22e0 19564 case DW_LANG_Fortran95:
f7de9aab
MW
19565 case DW_LANG_Fortran03:
19566 case DW_LANG_Fortran08:
e142c38c 19567 cu->language = language_fortran;
c906108c 19568 break;
a766d390
DE
19569 case DW_LANG_Go:
19570 cu->language = language_go;
19571 break;
c906108c 19572 case DW_LANG_Mips_Assembler:
e142c38c 19573 cu->language = language_asm;
c906108c
SS
19574 break;
19575 case DW_LANG_Ada83:
8aaf0b47 19576 case DW_LANG_Ada95:
bc5f45f8
JB
19577 cu->language = language_ada;
19578 break;
72019c9c
GM
19579 case DW_LANG_Modula2:
19580 cu->language = language_m2;
19581 break;
fe8e67fd
PM
19582 case DW_LANG_Pascal83:
19583 cu->language = language_pascal;
19584 break;
22566fbd
DJ
19585 case DW_LANG_ObjC:
19586 cu->language = language_objc;
19587 break;
c44af4eb
TT
19588 case DW_LANG_Rust:
19589 case DW_LANG_Rust_old:
19590 cu->language = language_rust;
19591 break;
c906108c
SS
19592 case DW_LANG_Cobol74:
19593 case DW_LANG_Cobol85:
c906108c 19594 default:
e142c38c 19595 cu->language = language_minimal;
c906108c
SS
19596 break;
19597 }
e142c38c 19598 cu->language_defn = language_def (cu->language);
c906108c
SS
19599}
19600
19601/* Return the named attribute or NULL if not there. */
19602
19603static struct attribute *
e142c38c 19604dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19605{
a48e046c 19606 for (;;)
c906108c 19607 {
a48e046c
TT
19608 unsigned int i;
19609 struct attribute *spec = NULL;
19610
19611 for (i = 0; i < die->num_attrs; ++i)
19612 {
19613 if (die->attrs[i].name == name)
19614 return &die->attrs[i];
19615 if (die->attrs[i].name == DW_AT_specification
19616 || die->attrs[i].name == DW_AT_abstract_origin)
19617 spec = &die->attrs[i];
19618 }
19619
19620 if (!spec)
19621 break;
c906108c 19622
f2f0e013 19623 die = follow_die_ref (die, spec, &cu);
f2f0e013 19624 }
c5aa993b 19625
c906108c
SS
19626 return NULL;
19627}
19628
7d45c7c3
KB
19629/* Return the string associated with a string-typed attribute, or NULL if it
19630 is either not found or is of an incorrect type. */
19631
19632static const char *
19633dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19634{
19635 struct attribute *attr;
19636 const char *str = NULL;
19637
19638 attr = dwarf2_attr (die, name, cu);
19639
19640 if (attr != NULL)
19641 {
e61108c9
TT
19642 str = attr->value_as_string ();
19643 if (str == nullptr)
b98664d3 19644 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19645 "DIE at %s in module %s"),
19646 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19647 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19648 }
19649
19650 return str;
19651}
19652
a084a2a6 19653/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19654 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19655static const char *
19656dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19657{
19658 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19659 if (dwo_name == nullptr)
19660 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19661 return dwo_name;
19662}
19663
05cf31d1
JB
19664/* Return non-zero iff the attribute NAME is defined for the given DIE,
19665 and holds a non-zero value. This function should only be used for
2dc7f7b3 19666 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19667
19668static int
19669dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19670{
19671 struct attribute *attr = dwarf2_attr (die, name, cu);
19672
19673 return (attr && DW_UNSND (attr));
19674}
19675
3ca72b44 19676static int
e142c38c 19677die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19678{
05cf31d1
JB
19679 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19680 which value is non-zero. However, we have to be careful with
19681 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19682 (via dwarf2_flag_true_p) follows this attribute. So we may
19683 end up accidently finding a declaration attribute that belongs
19684 to a different DIE referenced by the specification attribute,
19685 even though the given DIE does not have a declaration attribute. */
19686 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19687 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19688}
19689
63d06c5c 19690/* Return the die giving the specification for DIE, if there is
f2f0e013 19691 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19692 containing the return value on output. If there is no
19693 specification, but there is an abstract origin, that is
19694 returned. */
63d06c5c
DC
19695
19696static struct die_info *
f2f0e013 19697die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19698{
f2f0e013
DJ
19699 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19700 *spec_cu);
63d06c5c 19701
edb3359d
DJ
19702 if (spec_attr == NULL)
19703 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19704
63d06c5c
DC
19705 if (spec_attr == NULL)
19706 return NULL;
19707 else
f2f0e013 19708 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19709}
c906108c 19710
527f3840
JK
19711/* Stub for free_line_header to match void * callback types. */
19712
19713static void
19714free_line_header_voidp (void *arg)
19715{
9a3c8263 19716 struct line_header *lh = (struct line_header *) arg;
527f3840 19717
fff8551c 19718 delete lh;
527f3840
JK
19719}
19720
83769d0b 19721/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19722
19723static struct dwarf2_section_info *
19724get_debug_line_section (struct dwarf2_cu *cu)
19725{
19726 struct dwarf2_section_info *section;
5e22e966 19727 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19728
19729 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19730 DWO file. */
19731 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19732 section = &cu->dwo_unit->dwo_file->sections.line;
19733 else if (cu->per_cu->is_dwz)
19734 {
c3699833 19735 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19736
19737 section = &dwz->line;
19738 }
19739 else
5989a64e 19740 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19741
19742 return section;
19743}
19744
debd256d 19745/* Read the statement program header starting at OFFSET in
3019eac3 19746 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19747 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19748 Returns NULL if there is a problem reading the header, e.g., if it
19749 has a version we don't understand.
debd256d
JB
19750
19751 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19752 the returned object point into the dwarf line section buffer,
19753 and must not be freed. */
ae2de4f8 19754
fff8551c 19755static line_header_up
9c541725 19756dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19757{
3019eac3 19758 struct dwarf2_section_info *section;
5e22e966 19759 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19760
36586728 19761 section = get_debug_line_section (cu);
96b79293 19762 section->read (dwarf2_per_objfile->objfile);
3019eac3 19763 if (section->buffer == NULL)
debd256d 19764 {
3019eac3 19765 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19766 complaint (_("missing .debug_line.dwo section"));
3019eac3 19767 else
b98664d3 19768 complaint (_("missing .debug_line section"));
debd256d
JB
19769 return 0;
19770 }
19771
0df7ad3a
TT
19772 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19773 dwarf2_per_objfile, section,
19774 &cu->header);
debd256d 19775}
c906108c 19776
c6da4cef 19777/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19778 Return the file name of the psymtab for the given file_entry.
c6da4cef 19779 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19780 If space for the result is malloc'd, *NAME_HOLDER will be set.
19781 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19782
d521ce57 19783static const char *
7ba99d21 19784psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19785 const dwarf2_psymtab *pst,
c89b44cd
TT
19786 const char *comp_dir,
19787 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19788{
d521ce57
TT
19789 const char *include_name = fe.name;
19790 const char *include_name_to_compare = include_name;
72b9f47f 19791 const char *pst_filename;
c6da4cef
DE
19792 int file_is_pst;
19793
8c43009f 19794 const char *dir_name = fe.include_dir (lh);
c6da4cef 19795
c89b44cd 19796 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19797 if (!IS_ABSOLUTE_PATH (include_name)
19798 && (dir_name != NULL || comp_dir != NULL))
19799 {
19800 /* Avoid creating a duplicate psymtab for PST.
19801 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19802 Before we do the comparison, however, we need to account
19803 for DIR_NAME and COMP_DIR.
19804 First prepend dir_name (if non-NULL). If we still don't
19805 have an absolute path prepend comp_dir (if non-NULL).
19806 However, the directory we record in the include-file's
19807 psymtab does not contain COMP_DIR (to match the
19808 corresponding symtab(s)).
19809
19810 Example:
19811
19812 bash$ cd /tmp
19813 bash$ gcc -g ./hello.c
19814 include_name = "hello.c"
19815 dir_name = "."
19816 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19817 DW_AT_name = "./hello.c"
19818
19819 */
c6da4cef
DE
19820
19821 if (dir_name != NULL)
19822 {
c89b44cd
TT
19823 name_holder->reset (concat (dir_name, SLASH_STRING,
19824 include_name, (char *) NULL));
19825 include_name = name_holder->get ();
c6da4cef 19826 include_name_to_compare = include_name;
c6da4cef
DE
19827 }
19828 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19829 {
c89b44cd
TT
19830 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19831 include_name, (char *) NULL));
19832 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19833 }
19834 }
19835
19836 pst_filename = pst->filename;
c89b44cd 19837 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19838 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19839 {
c89b44cd
TT
19840 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19841 pst_filename, (char *) NULL));
19842 pst_filename = copied_name.get ();
c6da4cef
DE
19843 }
19844
1e3fad37 19845 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19846
c6da4cef
DE
19847 if (file_is_pst)
19848 return NULL;
19849 return include_name;
19850}
19851
d9b3de22
DE
19852/* State machine to track the state of the line number program. */
19853
6f77053d 19854class lnp_state_machine
d9b3de22 19855{
6f77053d
PA
19856public:
19857 /* Initialize a machine state for the start of a line number
19858 program. */
804d2729
TT
19859 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19860 bool record_lines_p);
6f77053d 19861
8c43009f
PA
19862 file_entry *current_file ()
19863 {
19864 /* lh->file_names is 0-based, but the file name numbers in the
19865 statement program are 1-based. */
6f77053d
PA
19866 return m_line_header->file_name_at (m_file);
19867 }
19868
19869 /* Record the line in the state machine. END_SEQUENCE is true if
19870 we're processing the end of a sequence. */
19871 void record_line (bool end_sequence);
19872
7ab6656f
OJ
19873 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19874 nop-out rest of the lines in this sequence. */
6f77053d
PA
19875 void check_line_address (struct dwarf2_cu *cu,
19876 const gdb_byte *line_ptr,
7ab6656f 19877 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19878
19879 void handle_set_discriminator (unsigned int discriminator)
19880 {
19881 m_discriminator = discriminator;
19882 m_line_has_non_zero_discriminator |= discriminator != 0;
19883 }
19884
19885 /* Handle DW_LNE_set_address. */
19886 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19887 {
19888 m_op_index = 0;
19889 address += baseaddr;
19890 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19891 }
19892
19893 /* Handle DW_LNS_advance_pc. */
19894 void handle_advance_pc (CORE_ADDR adjust);
19895
19896 /* Handle a special opcode. */
19897 void handle_special_opcode (unsigned char op_code);
19898
19899 /* Handle DW_LNS_advance_line. */
19900 void handle_advance_line (int line_delta)
19901 {
19902 advance_line (line_delta);
19903 }
19904
19905 /* Handle DW_LNS_set_file. */
19906 void handle_set_file (file_name_index file);
19907
19908 /* Handle DW_LNS_negate_stmt. */
19909 void handle_negate_stmt ()
19910 {
19911 m_is_stmt = !m_is_stmt;
19912 }
19913
19914 /* Handle DW_LNS_const_add_pc. */
19915 void handle_const_add_pc ();
19916
19917 /* Handle DW_LNS_fixed_advance_pc. */
19918 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19919 {
19920 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19921 m_op_index = 0;
19922 }
19923
19924 /* Handle DW_LNS_copy. */
19925 void handle_copy ()
19926 {
19927 record_line (false);
19928 m_discriminator = 0;
19929 }
19930
19931 /* Handle DW_LNE_end_sequence. */
19932 void handle_end_sequence ()
19933 {
804d2729 19934 m_currently_recording_lines = true;
6f77053d
PA
19935 }
19936
19937private:
19938 /* Advance the line by LINE_DELTA. */
19939 void advance_line (int line_delta)
19940 {
19941 m_line += line_delta;
19942
19943 if (line_delta != 0)
19944 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19945 }
19946
804d2729
TT
19947 struct dwarf2_cu *m_cu;
19948
6f77053d
PA
19949 gdbarch *m_gdbarch;
19950
19951 /* True if we're recording lines.
19952 Otherwise we're building partial symtabs and are just interested in
19953 finding include files mentioned by the line number program. */
19954 bool m_record_lines_p;
19955
8c43009f 19956 /* The line number header. */
6f77053d 19957 line_header *m_line_header;
8c43009f 19958
6f77053d
PA
19959 /* These are part of the standard DWARF line number state machine,
19960 and initialized according to the DWARF spec. */
d9b3de22 19961
6f77053d 19962 unsigned char m_op_index = 0;
7ba99d21
AT
19963 /* The line table index of the current file. */
19964 file_name_index m_file = 1;
6f77053d
PA
19965 unsigned int m_line = 1;
19966
19967 /* These are initialized in the constructor. */
19968
19969 CORE_ADDR m_address;
19970 bool m_is_stmt;
19971 unsigned int m_discriminator;
d9b3de22
DE
19972
19973 /* Additional bits of state we need to track. */
19974
19975 /* The last file that we called dwarf2_start_subfile for.
19976 This is only used for TLLs. */
6f77053d 19977 unsigned int m_last_file = 0;
d9b3de22 19978 /* The last file a line number was recorded for. */
6f77053d 19979 struct subfile *m_last_subfile = NULL;
d9b3de22 19980
804d2729
TT
19981 /* When true, record the lines we decode. */
19982 bool m_currently_recording_lines = false;
d9b3de22
DE
19983
19984 /* The last line number that was recorded, used to coalesce
19985 consecutive entries for the same line. This can happen, for
19986 example, when discriminators are present. PR 17276. */
6f77053d
PA
19987 unsigned int m_last_line = 0;
19988 bool m_line_has_non_zero_discriminator = false;
8c43009f 19989};
d9b3de22 19990
6f77053d
PA
19991void
19992lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19993{
19994 CORE_ADDR addr_adj = (((m_op_index + adjust)
19995 / m_line_header->maximum_ops_per_instruction)
19996 * m_line_header->minimum_instruction_length);
19997 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19998 m_op_index = ((m_op_index + adjust)
19999 % m_line_header->maximum_ops_per_instruction);
20000}
d9b3de22 20001
6f77053d
PA
20002void
20003lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20004{
6f77053d 20005 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20006 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20007 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20008 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20009 / m_line_header->maximum_ops_per_instruction)
20010 * m_line_header->minimum_instruction_length);
20011 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20012 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20013 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20014
258bf0ee 20015 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20016 advance_line (line_delta);
20017 record_line (false);
20018 m_discriminator = 0;
20019}
d9b3de22 20020
6f77053d
PA
20021void
20022lnp_state_machine::handle_set_file (file_name_index file)
20023{
20024 m_file = file;
20025
20026 const file_entry *fe = current_file ();
20027 if (fe == NULL)
20028 dwarf2_debug_line_missing_file_complaint ();
20029 else if (m_record_lines_p)
20030 {
20031 const char *dir = fe->include_dir (m_line_header);
20032
c24bdb02 20033 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20034 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20035 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20036 }
20037}
20038
20039void
20040lnp_state_machine::handle_const_add_pc ()
20041{
20042 CORE_ADDR adjust
20043 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20044
20045 CORE_ADDR addr_adj
20046 = (((m_op_index + adjust)
20047 / m_line_header->maximum_ops_per_instruction)
20048 * m_line_header->minimum_instruction_length);
20049
20050 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20051 m_op_index = ((m_op_index + adjust)
20052 % m_line_header->maximum_ops_per_instruction);
20053}
d9b3de22 20054
a05a36a5
DE
20055/* Return non-zero if we should add LINE to the line number table.
20056 LINE is the line to add, LAST_LINE is the last line that was added,
20057 LAST_SUBFILE is the subfile for LAST_LINE.
20058 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20059 had a non-zero discriminator.
20060
20061 We have to be careful in the presence of discriminators.
20062 E.g., for this line:
20063
20064 for (i = 0; i < 100000; i++);
20065
20066 clang can emit four line number entries for that one line,
20067 each with a different discriminator.
20068 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20069
20070 However, we want gdb to coalesce all four entries into one.
20071 Otherwise the user could stepi into the middle of the line and
20072 gdb would get confused about whether the pc really was in the
20073 middle of the line.
20074
20075 Things are further complicated by the fact that two consecutive
20076 line number entries for the same line is a heuristic used by gcc
20077 to denote the end of the prologue. So we can't just discard duplicate
20078 entries, we have to be selective about it. The heuristic we use is
20079 that we only collapse consecutive entries for the same line if at least
20080 one of those entries has a non-zero discriminator. PR 17276.
20081
20082 Note: Addresses in the line number state machine can never go backwards
20083 within one sequence, thus this coalescing is ok. */
20084
20085static int
804d2729
TT
20086dwarf_record_line_p (struct dwarf2_cu *cu,
20087 unsigned int line, unsigned int last_line,
a05a36a5
DE
20088 int line_has_non_zero_discriminator,
20089 struct subfile *last_subfile)
20090{
c24bdb02 20091 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20092 return 1;
20093 if (line != last_line)
20094 return 1;
20095 /* Same line for the same file that we've seen already.
20096 As a last check, for pr 17276, only record the line if the line
20097 has never had a non-zero discriminator. */
20098 if (!line_has_non_zero_discriminator)
20099 return 1;
20100 return 0;
20101}
20102
804d2729
TT
20103/* Use the CU's builder to record line number LINE beginning at
20104 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20105
20106static void
d9b3de22 20107dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20108 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20109 struct dwarf2_cu *cu)
252a6764
DE
20110{
20111 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20112
27e0867f
DE
20113 if (dwarf_line_debug)
20114 {
20115 fprintf_unfiltered (gdb_stdlog,
20116 "Recording line %u, file %s, address %s\n",
20117 line, lbasename (subfile->name),
20118 paddress (gdbarch, address));
20119 }
20120
804d2729 20121 if (cu != nullptr)
8c95582d 20122 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20123}
20124
20125/* Subroutine of dwarf_decode_lines_1 to simplify it.
20126 Mark the end of a set of line number records.
d9b3de22 20127 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20128 If SUBFILE is NULL the request is ignored. */
20129
20130static void
20131dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20132 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20133{
27e0867f
DE
20134 if (subfile == NULL)
20135 return;
20136
20137 if (dwarf_line_debug)
20138 {
20139 fprintf_unfiltered (gdb_stdlog,
20140 "Finishing current line, file %s, address %s\n",
20141 lbasename (subfile->name),
20142 paddress (gdbarch, address));
20143 }
20144
8c95582d 20145 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20146}
20147
6f77053d
PA
20148void
20149lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20150{
d9b3de22
DE
20151 if (dwarf_line_debug)
20152 {
20153 fprintf_unfiltered (gdb_stdlog,
20154 "Processing actual line %u: file %u,"
94a72be7 20155 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20156 m_line, m_file,
6f77053d 20157 paddress (m_gdbarch, m_address),
94a72be7
AB
20158 m_is_stmt, m_discriminator,
20159 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20160 }
20161
6f77053d 20162 file_entry *fe = current_file ();
8c43009f
PA
20163
20164 if (fe == NULL)
d9b3de22
DE
20165 dwarf2_debug_line_missing_file_complaint ();
20166 /* For now we ignore lines not starting on an instruction boundary.
20167 But not when processing end_sequence for compatibility with the
20168 previous version of the code. */
6f77053d 20169 else if (m_op_index == 0 || end_sequence)
d9b3de22 20170 {
8c43009f 20171 fe->included_p = 1;
8c95582d 20172 if (m_record_lines_p)
d9b3de22 20173 {
c24bdb02 20174 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20175 || end_sequence)
d9b3de22 20176 {
804d2729
TT
20177 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20178 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20179 }
20180
20181 if (!end_sequence)
20182 {
8c95582d
AB
20183 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20184
804d2729 20185 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20186 m_line_has_non_zero_discriminator,
20187 m_last_subfile))
d9b3de22 20188 {
c24bdb02 20189 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20190 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20191 builder->get_current_subfile (),
8c95582d 20192 m_line, m_address, is_stmt,
804d2729 20193 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20194 }
c24bdb02 20195 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20196 m_last_line = m_line;
d9b3de22
DE
20197 }
20198 }
20199 }
20200}
20201
804d2729
TT
20202lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20203 line_header *lh, bool record_lines_p)
d9b3de22 20204{
804d2729 20205 m_cu = cu;
6f77053d
PA
20206 m_gdbarch = arch;
20207 m_record_lines_p = record_lines_p;
20208 m_line_header = lh;
d9b3de22 20209
804d2729 20210 m_currently_recording_lines = true;
d9b3de22 20211
d9b3de22
DE
20212 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20213 was a line entry for it so that the backend has a chance to adjust it
20214 and also record it in case it needs it. This is currently used by MIPS
20215 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20216 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20217 m_is_stmt = lh->default_is_stmt;
20218 m_discriminator = 0;
252a6764
DE
20219}
20220
6f77053d
PA
20221void
20222lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20223 const gdb_byte *line_ptr,
7ab6656f 20224 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20225{
7ab6656f
OJ
20226 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20227 the pc range of the CU. However, we restrict the test to only ADDRESS
20228 values of zero to preserve GDB's previous behaviour which is to handle
20229 the specific case of a function being GC'd by the linker. */
924c2928 20230
7ab6656f 20231 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20232 {
20233 /* This line table is for a function which has been
20234 GCd by the linker. Ignore it. PR gdb/12528 */
20235
5e22e966 20236 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20237 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20238
b98664d3 20239 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20240 line_offset, objfile_name (objfile));
804d2729
TT
20241 m_currently_recording_lines = false;
20242 /* Note: m_currently_recording_lines is left as false until we see
20243 DW_LNE_end_sequence. */
924c2928
DE
20244 }
20245}
20246
f3f5162e 20247/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20248 Process the line number information in LH.
20249 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20250 program in order to set included_p for every referenced header. */
debd256d 20251
c906108c 20252static void
43f3e411
DE
20253dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20254 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20255{
d521ce57
TT
20256 const gdb_byte *line_ptr, *extended_end;
20257 const gdb_byte *line_end;
a8c50c1f 20258 unsigned int bytes_read, extended_len;
699ca60a 20259 unsigned char op_code, extended_op;
e142c38c 20260 CORE_ADDR baseaddr;
5e22e966 20261 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20262 bfd *abfd = objfile->obfd;
08feed99 20263 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20264 /* True if we're recording line info (as opposed to building partial
20265 symtabs and just interested in finding include files mentioned by
20266 the line number program). */
20267 bool record_lines_p = !decode_for_pst_p;
e142c38c 20268
b3b3bada 20269 baseaddr = objfile->text_section_offset ();
c906108c 20270
debd256d
JB
20271 line_ptr = lh->statement_program_start;
20272 line_end = lh->statement_program_end;
c906108c
SS
20273
20274 /* Read the statement sequences until there's nothing left. */
20275 while (line_ptr < line_end)
20276 {
6f77053d
PA
20277 /* The DWARF line number program state machine. Reset the state
20278 machine at the start of each sequence. */
804d2729 20279 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20280 bool end_sequence = false;
d9b3de22 20281
8c43009f 20282 if (record_lines_p)
c906108c 20283 {
8c43009f
PA
20284 /* Start a subfile for the current file of the state
20285 machine. */
20286 const file_entry *fe = state_machine.current_file ();
20287
20288 if (fe != NULL)
804d2729 20289 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20290 }
20291
a738430d 20292 /* Decode the table. */
d9b3de22 20293 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20294 {
20295 op_code = read_1_byte (abfd, line_ptr);
20296 line_ptr += 1;
9aa1fe7e 20297
debd256d 20298 if (op_code >= lh->opcode_base)
6e70227d 20299 {
8e07a239 20300 /* Special opcode. */
6f77053d 20301 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20302 }
20303 else switch (op_code)
c906108c
SS
20304 {
20305 case DW_LNS_extended_op:
3e43a32a
MS
20306 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20307 &bytes_read);
473b7be6 20308 line_ptr += bytes_read;
a8c50c1f 20309 extended_end = line_ptr + extended_len;
c906108c
SS
20310 extended_op = read_1_byte (abfd, line_ptr);
20311 line_ptr += 1;
20312 switch (extended_op)
20313 {
20314 case DW_LNE_end_sequence:
6f77053d
PA
20315 state_machine.handle_end_sequence ();
20316 end_sequence = true;
c906108c
SS
20317 break;
20318 case DW_LNE_set_address:
d9b3de22
DE
20319 {
20320 CORE_ADDR address
c8a7a66f 20321 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20322 line_ptr += bytes_read;
6f77053d
PA
20323
20324 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20325 lowpc - baseaddr, address);
6f77053d 20326 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20327 }
c906108c
SS
20328 break;
20329 case DW_LNE_define_file:
debd256d 20330 {
d521ce57 20331 const char *cur_file;
ecfb656c
PA
20332 unsigned int mod_time, length;
20333 dir_index dindex;
6e70227d 20334
3e43a32a
MS
20335 cur_file = read_direct_string (abfd, line_ptr,
20336 &bytes_read);
debd256d 20337 line_ptr += bytes_read;
ecfb656c 20338 dindex = (dir_index)
debd256d
JB
20339 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20340 line_ptr += bytes_read;
20341 mod_time =
20342 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20343 line_ptr += bytes_read;
20344 length =
20345 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20346 line_ptr += bytes_read;
ecfb656c 20347 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20348 }
c906108c 20349 break;
d0c6ba3d 20350 case DW_LNE_set_discriminator:
6f77053d
PA
20351 {
20352 /* The discriminator is not interesting to the
20353 debugger; just ignore it. We still need to
20354 check its value though:
20355 if there are consecutive entries for the same
20356 (non-prologue) line we want to coalesce them.
20357 PR 17276. */
20358 unsigned int discr
20359 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20360 line_ptr += bytes_read;
20361
20362 state_machine.handle_set_discriminator (discr);
20363 }
d0c6ba3d 20364 break;
c906108c 20365 default:
b98664d3 20366 complaint (_("mangled .debug_line section"));
debd256d 20367 return;
c906108c 20368 }
a8c50c1f
DJ
20369 /* Make sure that we parsed the extended op correctly. If e.g.
20370 we expected a different address size than the producer used,
20371 we may have read the wrong number of bytes. */
20372 if (line_ptr != extended_end)
20373 {
b98664d3 20374 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20375 return;
20376 }
c906108c
SS
20377 break;
20378 case DW_LNS_copy:
6f77053d 20379 state_machine.handle_copy ();
c906108c
SS
20380 break;
20381 case DW_LNS_advance_pc:
2dc7f7b3
TT
20382 {
20383 CORE_ADDR adjust
20384 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20385 line_ptr += bytes_read;
6f77053d
PA
20386
20387 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20388 }
c906108c
SS
20389 break;
20390 case DW_LNS_advance_line:
a05a36a5
DE
20391 {
20392 int line_delta
20393 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20394 line_ptr += bytes_read;
6f77053d
PA
20395
20396 state_machine.handle_advance_line (line_delta);
a05a36a5 20397 }
c906108c
SS
20398 break;
20399 case DW_LNS_set_file:
d9b3de22 20400 {
6f77053d 20401 file_name_index file
ecfb656c
PA
20402 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20403 &bytes_read);
d9b3de22 20404 line_ptr += bytes_read;
8c43009f 20405
6f77053d 20406 state_machine.handle_set_file (file);
d9b3de22 20407 }
c906108c
SS
20408 break;
20409 case DW_LNS_set_column:
0ad93d4f 20410 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20411 line_ptr += bytes_read;
20412 break;
20413 case DW_LNS_negate_stmt:
6f77053d 20414 state_machine.handle_negate_stmt ();
c906108c
SS
20415 break;
20416 case DW_LNS_set_basic_block:
c906108c 20417 break;
c2c6d25f
JM
20418 /* Add to the address register of the state machine the
20419 address increment value corresponding to special opcode
a738430d
MK
20420 255. I.e., this value is scaled by the minimum
20421 instruction length since special opcode 255 would have
b021a221 20422 scaled the increment. */
c906108c 20423 case DW_LNS_const_add_pc:
6f77053d 20424 state_machine.handle_const_add_pc ();
c906108c
SS
20425 break;
20426 case DW_LNS_fixed_advance_pc:
3e29f34a 20427 {
6f77053d 20428 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20429 line_ptr += 2;
6f77053d
PA
20430
20431 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20432 }
c906108c 20433 break;
9aa1fe7e 20434 default:
a738430d
MK
20435 {
20436 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20437 int i;
a738430d 20438
debd256d 20439 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20440 {
20441 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20442 line_ptr += bytes_read;
20443 }
20444 }
c906108c
SS
20445 }
20446 }
d9b3de22
DE
20447
20448 if (!end_sequence)
20449 dwarf2_debug_line_missing_end_sequence_complaint ();
20450
20451 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20452 in which case we still finish recording the last line). */
6f77053d 20453 state_machine.record_line (true);
c906108c 20454 }
f3f5162e
DE
20455}
20456
20457/* Decode the Line Number Program (LNP) for the given line_header
20458 structure and CU. The actual information extracted and the type
20459 of structures created from the LNP depends on the value of PST.
20460
20461 1. If PST is NULL, then this procedure uses the data from the program
20462 to create all necessary symbol tables, and their linetables.
20463
20464 2. If PST is not NULL, this procedure reads the program to determine
20465 the list of files included by the unit represented by PST, and
20466 builds all the associated partial symbol tables.
20467
20468 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20469 It is used for relative paths in the line table.
20470 NOTE: When processing partial symtabs (pst != NULL),
20471 comp_dir == pst->dirname.
20472
20473 NOTE: It is important that psymtabs have the same file name (via strcmp)
20474 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20475 symtab we don't use it in the name of the psymtabs we create.
20476 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20477 A good testcase for this is mb-inline.exp.
20478
527f3840
JK
20479 LOWPC is the lowest address in CU (or 0 if not known).
20480
20481 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20482 for its PC<->lines mapping information. Otherwise only the filename
20483 table is read in. */
f3f5162e
DE
20484
20485static void
20486dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20487 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20488 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20489{
5e22e966 20490 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20491 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20492
527f3840
JK
20493 if (decode_mapping)
20494 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20495
20496 if (decode_for_pst_p)
20497 {
aaa75496
JB
20498 /* Now that we're done scanning the Line Header Program, we can
20499 create the psymtab of each included file. */
7ba99d21
AT
20500 for (auto &file_entry : lh->file_names ())
20501 if (file_entry.included_p == 1)
aaa75496 20502 {
c89b44cd 20503 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20504 const char *include_name =
7ba99d21
AT
20505 psymtab_include_file_name (lh, file_entry, pst,
20506 comp_dir, &name_holder);
c6da4cef 20507 if (include_name != NULL)
aaa75496
JB
20508 dwarf2_create_include_psymtab (include_name, pst, objfile);
20509 }
20510 }
cb1df416
DJ
20511 else
20512 {
20513 /* Make sure a symtab is created for every file, even files
20514 which contain only variables (i.e. no code with associated
20515 line numbers). */
c24bdb02
KS
20516 buildsym_compunit *builder = cu->get_builder ();
20517 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20518
7ba99d21 20519 for (auto &fe : lh->file_names ())
cb1df416 20520 {
804d2729 20521 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20522 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20523 {
c24bdb02 20524 builder->get_current_subfile ()->symtab
804d2729 20525 = allocate_symtab (cust,
c24bdb02 20526 builder->get_current_subfile ()->name);
43f3e411 20527 }
c24bdb02 20528 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20529 }
20530 }
c906108c
SS
20531}
20532
20533/* Start a subfile for DWARF. FILENAME is the name of the file and
20534 DIRNAME the name of the source directory which contains FILENAME
4d663531 20535 or NULL if not known.
c906108c
SS
20536 This routine tries to keep line numbers from identical absolute and
20537 relative file names in a common subfile.
20538
20539 Using the `list' example from the GDB testsuite, which resides in
20540 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20541 of /srcdir/list0.c yields the following debugging information for list0.c:
20542
c5aa993b 20543 DW_AT_name: /srcdir/list0.c
4d663531 20544 DW_AT_comp_dir: /compdir
357e46e7 20545 files.files[0].name: list0.h
c5aa993b 20546 files.files[0].dir: /srcdir
357e46e7 20547 files.files[1].name: list0.c
c5aa993b 20548 files.files[1].dir: /srcdir
c906108c
SS
20549
20550 The line number information for list0.c has to end up in a single
4f1520fb
FR
20551 subfile, so that `break /srcdir/list0.c:1' works as expected.
20552 start_subfile will ensure that this happens provided that we pass the
20553 concatenation of files.files[1].dir and files.files[1].name as the
20554 subfile's name. */
c906108c
SS
20555
20556static void
804d2729
TT
20557dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20558 const char *dirname)
c906108c 20559{
43816ebc 20560 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20561
4d663531 20562 /* In order not to lose the line information directory,
4f1520fb
FR
20563 we concatenate it to the filename when it makes sense.
20564 Note that the Dwarf3 standard says (speaking of filenames in line
20565 information): ``The directory index is ignored for file names
20566 that represent full path names''. Thus ignoring dirname in the
20567 `else' branch below isn't an issue. */
c906108c 20568
d5166ae1 20569 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20570 {
43816ebc
TT
20571 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20572 filename = copy.get ();
d521ce57 20573 }
c906108c 20574
c24bdb02 20575 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20576}
20577
804d2729
TT
20578/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20579 buildsym_compunit constructor. */
f4dc4d17 20580
c24bdb02
KS
20581struct compunit_symtab *
20582dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20583 CORE_ADDR low_pc)
f4dc4d17 20584{
c24bdb02 20585 gdb_assert (m_builder == nullptr);
43f3e411 20586
c24bdb02
KS
20587 m_builder.reset (new struct buildsym_compunit
20588 (per_cu->dwarf2_per_objfile->objfile,
20589 name, comp_dir, language, low_pc));
93b8bea4 20590
c24bdb02 20591 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20592
c24bdb02
KS
20593 get_builder ()->record_debugformat ("DWARF 2");
20594 get_builder ()->record_producer (producer);
f4dc4d17 20595
c24bdb02 20596 processing_has_namespace_info = false;
43f3e411 20597
c24bdb02 20598 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20599}
20600
4c2df51b
DJ
20601static void
20602var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20603 struct dwarf2_cu *cu)
4c2df51b 20604{
5e22e966 20605 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20606 struct comp_unit_head *cu_header = &cu->header;
20607
4c2df51b
DJ
20608 /* NOTE drow/2003-01-30: There used to be a comment and some special
20609 code here to turn a symbol with DW_AT_external and a
20610 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20611 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20612 with some versions of binutils) where shared libraries could have
20613 relocations against symbols in their debug information - the
20614 minimal symbol would have the right address, but the debug info
20615 would not. It's no longer necessary, because we will explicitly
20616 apply relocations when we read in the debug information now. */
20617
20618 /* A DW_AT_location attribute with no contents indicates that a
20619 variable has been optimized away. */
4fc6c0d5 20620 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20621 {
f1e6e072 20622 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20623 return;
20624 }
20625
20626 /* Handle one degenerate form of location expression specially, to
20627 preserve GDB's previous behavior when section offsets are
336d760d
AT
20628 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20629 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20630
4fc6c0d5 20631 if (attr->form_is_block ()
3019eac3
DE
20632 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20633 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20634 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20635 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20636 && (DW_BLOCK (attr)->size
20637 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20638 {
891d2f0b 20639 unsigned int dummy;
4c2df51b 20640
3019eac3 20641 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20642 SET_SYMBOL_VALUE_ADDRESS
20643 (sym, cu->header.read_address (objfile->obfd,
20644 DW_BLOCK (attr)->data + 1,
20645 &dummy));
3019eac3 20646 else
38583298
TT
20647 SET_SYMBOL_VALUE_ADDRESS
20648 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20649 &dummy));
f1e6e072 20650 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20651 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20652 SET_SYMBOL_VALUE_ADDRESS
20653 (sym,
20654 SYMBOL_VALUE_ADDRESS (sym)
20655 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20656 return;
20657 }
20658
20659 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20660 expression evaluator, and use LOC_COMPUTED only when necessary
20661 (i.e. when the value of a register or memory location is
20662 referenced, or a thread-local block, etc.). Then again, it might
20663 not be worthwhile. I'm assuming that it isn't unless performance
20664 or memory numbers show me otherwise. */
20665
f1e6e072 20666 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20667
f1e6e072 20668 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20669 cu->has_loclist = true;
4c2df51b
DJ
20670}
20671
c906108c
SS
20672/* Given a pointer to a DWARF information entry, figure out if we need
20673 to make a symbol table entry for it, and if so, create a new entry
20674 and return a pointer to it.
20675 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20676 used the passed type.
20677 If SPACE is not NULL, use it to hold the new symbol. If it is
20678 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20679
20680static struct symbol *
5e2db402
TT
20681new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20682 struct symbol *space)
c906108c 20683{
5e22e966 20684 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20685 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20686 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20687 struct symbol *sym = NULL;
15d034d0 20688 const char *name;
c906108c
SS
20689 struct attribute *attr = NULL;
20690 struct attribute *attr2 = NULL;
e142c38c 20691 CORE_ADDR baseaddr;
e37fd15a
SW
20692 struct pending **list_to_add = NULL;
20693
edb3359d 20694 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20695
b3b3bada 20696 baseaddr = objfile->text_section_offset ();
c906108c 20697
94af9270 20698 name = dwarf2_name (die, cu);
c906108c
SS
20699 if (name)
20700 {
34eaf542 20701 int suppress_add = 0;
94af9270 20702
34eaf542
TT
20703 if (space)
20704 sym = space;
20705 else
8c14c3a3 20706 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20707 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20708
20709 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20710 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20711 /* Fortran does not have mangling standard and the mangling does differ
20712 between gfortran, iFort etc. */
bcfe6157
TT
20713 const char *physname
20714 = (cu->language == language_fortran
20715 ? dwarf2_full_name (name, die, cu)
20716 : dwarf2_physname (name, die, cu));
20717 const char *linkagename = dw2_linkage_name (die, cu);
20718
20719 if (linkagename == nullptr || cu->language == language_ada)
20720 sym->set_linkage_name (physname);
20721 else
20722 {
20723 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20724 sym->set_linkage_name (linkagename);
20725 }
f55ee35c 20726
c906108c 20727 /* Default assumptions.
c5aa993b 20728 Use the passed type or decode it from the die. */
176620f1 20729 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20730 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20731 if (type != NULL)
20732 SYMBOL_TYPE (sym) = type;
20733 else
e7c27a73 20734 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20735 attr = dwarf2_attr (die,
20736 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20737 cu);
435d3d88 20738 if (attr != nullptr)
c906108c
SS
20739 {
20740 SYMBOL_LINE (sym) = DW_UNSND (attr);
20741 }
cb1df416 20742
edb3359d
DJ
20743 attr = dwarf2_attr (die,
20744 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20745 cu);
435d3d88 20746 if (attr != nullptr)
cb1df416 20747 {
ecfb656c 20748 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20749 struct file_entry *fe;
9a619af0 20750
ecfb656c
PA
20751 if (cu->line_header != NULL)
20752 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20753 else
20754 fe = NULL;
20755
20756 if (fe == NULL)
b98664d3 20757 complaint (_("file index out of range"));
8c43009f
PA
20758 else
20759 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20760 }
20761
c906108c
SS
20762 switch (die->tag)
20763 {
20764 case DW_TAG_label:
e142c38c 20765 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20766 if (attr != nullptr)
3e29f34a
MR
20767 {
20768 CORE_ADDR addr;
20769
cd6c91b4 20770 addr = attr->value_as_address ();
3e29f34a 20771 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20772 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20773 }
0f5238ed
TT
20774 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20775 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20776 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20777 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20778 break;
20779 case DW_TAG_subprogram:
20780 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20781 finish_block. */
f1e6e072 20782 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20783 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20784 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20785 || cu->language == language_ada
20786 || cu->language == language_fortran)
c906108c 20787 {
2cfa0c8d 20788 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20789 Ada and Fortran subprograms, whether marked external or
20790 not, are always stored as a global symbol, because we want
20791 to be able to access them globally. For instance, we want
20792 to be able to break on a nested subprogram without having
20793 to specify the context. */
c24bdb02 20794 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20795 }
20796 else
20797 {
e37fd15a 20798 list_to_add = cu->list_in_scope;
c906108c
SS
20799 }
20800 break;
edb3359d
DJ
20801 case DW_TAG_inlined_subroutine:
20802 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20803 finish_block. */
f1e6e072 20804 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20805 SYMBOL_INLINED (sym) = 1;
481860b3 20806 list_to_add = cu->list_in_scope;
edb3359d 20807 break;
34eaf542
TT
20808 case DW_TAG_template_value_param:
20809 suppress_add = 1;
20810 /* Fall through. */
72929c62 20811 case DW_TAG_constant:
c906108c 20812 case DW_TAG_variable:
254e6b9e 20813 case DW_TAG_member:
0963b4bd
MS
20814 /* Compilation with minimal debug info may result in
20815 variables with missing type entries. Change the
20816 misleading `void' type to something sensible. */
78134374 20817 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20818 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20819
e142c38c 20820 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20821 /* In the case of DW_TAG_member, we should only be called for
20822 static const members. */
20823 if (die->tag == DW_TAG_member)
20824 {
3863f96c
DE
20825 /* dwarf2_add_field uses die_is_declaration,
20826 so we do the same. */
254e6b9e
DE
20827 gdb_assert (die_is_declaration (die, cu));
20828 gdb_assert (attr);
20829 }
435d3d88 20830 if (attr != nullptr)
c906108c 20831 {
e7c27a73 20832 dwarf2_const_value (attr, sym, cu);
e142c38c 20833 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20834 if (!suppress_add)
34eaf542
TT
20835 {
20836 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20837 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20838 else
e37fd15a 20839 list_to_add = cu->list_in_scope;
34eaf542 20840 }
c906108c
SS
20841 break;
20842 }
e142c38c 20843 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20844 if (attr != nullptr)
c906108c 20845 {
e7c27a73 20846 var_decode_location (attr, sym, cu);
e142c38c 20847 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20848
20849 /* Fortran explicitly imports any global symbols to the local
20850 scope by DW_TAG_common_block. */
20851 if (cu->language == language_fortran && die->parent
20852 && die->parent->tag == DW_TAG_common_block)
20853 attr2 = NULL;
20854
caac4577
JG
20855 if (SYMBOL_CLASS (sym) == LOC_STATIC
20856 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20857 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20858 {
20859 /* When a static variable is eliminated by the linker,
20860 the corresponding debug information is not stripped
20861 out, but the variable address is set to null;
20862 do not add such variables into symbol table. */
20863 }
20864 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20865 {
4b610737
TT
20866 if (SYMBOL_CLASS (sym) == LOC_STATIC
20867 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20868 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20869 {
20870 /* A global static variable might be subject to
20871 copy relocation. We first check for a local
20872 minsym, though, because maybe the symbol was
20873 marked hidden, in which case this would not
20874 apply. */
20875 bound_minimal_symbol found
20876 = (lookup_minimal_symbol_linkage
987012b8 20877 (sym->linkage_name (), objfile));
4b610737
TT
20878 if (found.minsym != nullptr)
20879 sym->maybe_copied = 1;
20880 }
f55ee35c 20881
1c809c68
TT
20882 /* A variable with DW_AT_external is never static,
20883 but it may be block-scoped. */
804d2729 20884 list_to_add
c24bdb02
KS
20885 = ((cu->list_in_scope
20886 == cu->get_builder ()->get_file_symbols ())
20887 ? cu->get_builder ()->get_global_symbols ()
804d2729 20888 : cu->list_in_scope);
1c809c68 20889 }
c906108c 20890 else
e37fd15a 20891 list_to_add = cu->list_in_scope;
c906108c
SS
20892 }
20893 else
20894 {
20895 /* We do not know the address of this symbol.
c5aa993b
JM
20896 If it is an external symbol and we have type information
20897 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20898 The address of the variable will then be determined from
20899 the minimal symbol table whenever the variable is
20900 referenced. */
e142c38c 20901 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20902
20903 /* Fortran explicitly imports any global symbols to the local
20904 scope by DW_TAG_common_block. */
20905 if (cu->language == language_fortran && die->parent
20906 && die->parent->tag == DW_TAG_common_block)
20907 {
20908 /* SYMBOL_CLASS doesn't matter here because
20909 read_common_block is going to reset it. */
20910 if (!suppress_add)
20911 list_to_add = cu->list_in_scope;
20912 }
20913 else if (attr2 && (DW_UNSND (attr2) != 0)
20914 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20915 {
0fe7935b
DJ
20916 /* A variable with DW_AT_external is never static, but it
20917 may be block-scoped. */
804d2729 20918 list_to_add
c24bdb02
KS
20919 = ((cu->list_in_scope
20920 == cu->get_builder ()->get_file_symbols ())
20921 ? cu->get_builder ()->get_global_symbols ()
804d2729 20922 : cu->list_in_scope);
0fe7935b 20923
f1e6e072 20924 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20925 }
442ddf59
JK
20926 else if (!die_is_declaration (die, cu))
20927 {
20928 /* Use the default LOC_OPTIMIZED_OUT class. */
20929 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20930 if (!suppress_add)
20931 list_to_add = cu->list_in_scope;
442ddf59 20932 }
c906108c
SS
20933 }
20934 break;
20935 case DW_TAG_formal_parameter:
a60f3166
TT
20936 {
20937 /* If we are inside a function, mark this as an argument. If
20938 not, we might be looking at an argument to an inlined function
20939 when we do not have enough information to show inlined frames;
20940 pretend it's a local variable in that case so that the user can
20941 still see it. */
804d2729 20942 struct context_stack *curr
c24bdb02 20943 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20944 if (curr != nullptr && curr->name != nullptr)
20945 SYMBOL_IS_ARGUMENT (sym) = 1;
20946 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20947 if (attr != nullptr)
a60f3166
TT
20948 {
20949 var_decode_location (attr, sym, cu);
20950 }
20951 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20952 if (attr != nullptr)
a60f3166
TT
20953 {
20954 dwarf2_const_value (attr, sym, cu);
20955 }
f346a30d 20956
a60f3166
TT
20957 list_to_add = cu->list_in_scope;
20958 }
c906108c
SS
20959 break;
20960 case DW_TAG_unspecified_parameters:
20961 /* From varargs functions; gdb doesn't seem to have any
20962 interest in this information, so just ignore it for now.
20963 (FIXME?) */
20964 break;
34eaf542
TT
20965 case DW_TAG_template_type_param:
20966 suppress_add = 1;
20967 /* Fall through. */
c906108c 20968 case DW_TAG_class_type:
680b30c7 20969 case DW_TAG_interface_type:
c906108c
SS
20970 case DW_TAG_structure_type:
20971 case DW_TAG_union_type:
72019c9c 20972 case DW_TAG_set_type:
c906108c 20973 case DW_TAG_enumeration_type:
f1e6e072 20974 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20975 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20976
63d06c5c 20977 {
9c37b5ae 20978 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20979 really ever be static objects: otherwise, if you try
20980 to, say, break of a class's method and you're in a file
20981 which doesn't mention that class, it won't work unless
20982 the check for all static symbols in lookup_symbol_aux
20983 saves you. See the OtherFileClass tests in
20984 gdb.c++/namespace.exp. */
20985
e37fd15a 20986 if (!suppress_add)
34eaf542 20987 {
c24bdb02 20988 buildsym_compunit *builder = cu->get_builder ();
804d2729 20989 list_to_add
c24bdb02 20990 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20991 && cu->language == language_cplus
c24bdb02 20992 ? builder->get_global_symbols ()
804d2729 20993 : cu->list_in_scope);
63d06c5c 20994
64382290 20995 /* The semantics of C++ state that "struct foo {
9c37b5ae 20996 ... }" also defines a typedef for "foo". */
64382290 20997 if (cu->language == language_cplus
45280282 20998 || cu->language == language_ada
c44af4eb
TT
20999 || cu->language == language_d
21000 || cu->language == language_rust)
64382290
TT
21001 {
21002 /* The symbol's name is already allocated along
21003 with this objfile, so we don't need to
21004 duplicate it for the type. */
7d93a1e0 21005 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21006 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21007 }
63d06c5c
DC
21008 }
21009 }
c906108c
SS
21010 break;
21011 case DW_TAG_typedef:
f1e6e072 21012 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21013 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21014 list_to_add = cu->list_in_scope;
63d06c5c 21015 break;
c906108c 21016 case DW_TAG_base_type:
a02abb62 21017 case DW_TAG_subrange_type:
f1e6e072 21018 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21019 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21020 list_to_add = cu->list_in_scope;
c906108c
SS
21021 break;
21022 case DW_TAG_enumerator:
e142c38c 21023 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21024 if (attr != nullptr)
c906108c 21025 {
e7c27a73 21026 dwarf2_const_value (attr, sym, cu);
c906108c 21027 }
63d06c5c
DC
21028 {
21029 /* NOTE: carlton/2003-11-10: See comment above in the
21030 DW_TAG_class_type, etc. block. */
21031
804d2729 21032 list_to_add
c24bdb02 21033 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21034 && cu->language == language_cplus
c24bdb02 21035 ? cu->get_builder ()->get_global_symbols ()
804d2729 21036 : cu->list_in_scope);
63d06c5c 21037 }
c906108c 21038 break;
74921315 21039 case DW_TAG_imported_declaration:
5c4e30ca 21040 case DW_TAG_namespace:
f1e6e072 21041 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21042 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21043 break;
530e8392
KB
21044 case DW_TAG_module:
21045 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21046 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21047 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21048 break;
4357ac6c 21049 case DW_TAG_common_block:
f1e6e072 21050 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21051 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21052 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21053 break;
c906108c
SS
21054 default:
21055 /* Not a tag we recognize. Hopefully we aren't processing
21056 trash data, but since we must specifically ignore things
21057 we don't recognize, there is nothing else we should do at
0963b4bd 21058 this point. */
b98664d3 21059 complaint (_("unsupported tag: '%s'"),
4d3c2250 21060 dwarf_tag_name (die->tag));
c906108c
SS
21061 break;
21062 }
df8a16a1 21063
e37fd15a
SW
21064 if (suppress_add)
21065 {
21066 sym->hash_next = objfile->template_symbols;
21067 objfile->template_symbols = sym;
21068 list_to_add = NULL;
21069 }
21070
21071 if (list_to_add != NULL)
d3cb6808 21072 add_symbol_to_list (sym, list_to_add);
e37fd15a 21073
df8a16a1
DJ
21074 /* For the benefit of old versions of GCC, check for anonymous
21075 namespaces based on the demangled name. */
4d4ec4e5 21076 if (!cu->processing_has_namespace_info
94af9270 21077 && cu->language == language_cplus)
c24bdb02 21078 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21079 }
21080 return (sym);
21081}
21082
98bfdba5
PA
21083/* Given an attr with a DW_FORM_dataN value in host byte order,
21084 zero-extend it as appropriate for the symbol's type. The DWARF
21085 standard (v4) is not entirely clear about the meaning of using
21086 DW_FORM_dataN for a constant with a signed type, where the type is
21087 wider than the data. The conclusion of a discussion on the DWARF
21088 list was that this is unspecified. We choose to always zero-extend
21089 because that is the interpretation long in use by GCC. */
c906108c 21090
98bfdba5 21091static gdb_byte *
ff39bb5e 21092dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21093 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21094{
5e22e966 21095 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21096 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21097 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21098 LONGEST l = DW_UNSND (attr);
21099
21100 if (bits < sizeof (*value) * 8)
21101 {
21102 l &= ((LONGEST) 1 << bits) - 1;
21103 *value = l;
21104 }
21105 else if (bits == sizeof (*value) * 8)
21106 *value = l;
21107 else
21108 {
224c3ddb 21109 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21110 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21111 return bytes;
21112 }
21113
21114 return NULL;
21115}
21116
21117/* Read a constant value from an attribute. Either set *VALUE, or if
21118 the value does not fit in *VALUE, set *BYTES - either already
21119 allocated on the objfile obstack, or newly allocated on OBSTACK,
21120 or, set *BATON, if we translated the constant to a location
21121 expression. */
21122
21123static void
ff39bb5e 21124dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21125 const char *name, struct obstack *obstack,
21126 struct dwarf2_cu *cu,
d521ce57 21127 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21128 struct dwarf2_locexpr_baton **baton)
21129{
5e22e966 21130 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21131 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21132 struct comp_unit_head *cu_header = &cu->header;
c906108c 21133 struct dwarf_block *blk;
98bfdba5
PA
21134 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21135 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21136
21137 *value = 0;
21138 *bytes = NULL;
21139 *baton = NULL;
c906108c
SS
21140
21141 switch (attr->form)
21142 {
21143 case DW_FORM_addr:
336d760d 21144 case DW_FORM_addrx:
3019eac3 21145 case DW_FORM_GNU_addr_index:
ac56253d 21146 {
ac56253d
TT
21147 gdb_byte *data;
21148
98bfdba5
PA
21149 if (TYPE_LENGTH (type) != cu_header->addr_size)
21150 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21151 cu_header->addr_size,
98bfdba5 21152 TYPE_LENGTH (type));
ac56253d
TT
21153 /* Symbols of this form are reasonably rare, so we just
21154 piggyback on the existing location code rather than writing
21155 a new implementation of symbol_computed_ops. */
8d749320 21156 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21157 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21158 (*baton)->per_cu = cu->per_cu;
21159 gdb_assert ((*baton)->per_cu);
ac56253d 21160
98bfdba5 21161 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21162 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21163 (*baton)->data = data;
ac56253d
TT
21164
21165 data[0] = DW_OP_addr;
21166 store_unsigned_integer (&data[1], cu_header->addr_size,
21167 byte_order, DW_ADDR (attr));
21168 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21169 }
c906108c 21170 break;
4ac36638 21171 case DW_FORM_string:
93b5768b 21172 case DW_FORM_strp:
cf532bd1 21173 case DW_FORM_strx:
3019eac3 21174 case DW_FORM_GNU_str_index:
36586728 21175 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21176 /* DW_STRING is already allocated on the objfile obstack, point
21177 directly to it. */
d521ce57 21178 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21179 break;
c906108c
SS
21180 case DW_FORM_block1:
21181 case DW_FORM_block2:
21182 case DW_FORM_block4:
21183 case DW_FORM_block:
2dc7f7b3 21184 case DW_FORM_exprloc:
0224619f 21185 case DW_FORM_data16:
c906108c 21186 blk = DW_BLOCK (attr);
98bfdba5
PA
21187 if (TYPE_LENGTH (type) != blk->size)
21188 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21189 TYPE_LENGTH (type));
21190 *bytes = blk->data;
c906108c 21191 break;
2df3850c
JM
21192
21193 /* The DW_AT_const_value attributes are supposed to carry the
21194 symbol's value "represented as it would be on the target
21195 architecture." By the time we get here, it's already been
21196 converted to host endianness, so we just need to sign- or
21197 zero-extend it as appropriate. */
21198 case DW_FORM_data1:
3aef2284 21199 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21200 break;
c906108c 21201 case DW_FORM_data2:
3aef2284 21202 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21203 break;
c906108c 21204 case DW_FORM_data4:
3aef2284 21205 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21206 break;
c906108c 21207 case DW_FORM_data8:
3aef2284 21208 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21209 break;
21210
c906108c 21211 case DW_FORM_sdata:
663c44ac 21212 case DW_FORM_implicit_const:
98bfdba5 21213 *value = DW_SND (attr);
2df3850c
JM
21214 break;
21215
c906108c 21216 case DW_FORM_udata:
98bfdba5 21217 *value = DW_UNSND (attr);
c906108c 21218 break;
2df3850c 21219
c906108c 21220 default:
b98664d3 21221 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21222 dwarf_form_name (attr->form));
98bfdba5 21223 *value = 0;
c906108c
SS
21224 break;
21225 }
21226}
21227
2df3850c 21228
98bfdba5
PA
21229/* Copy constant value from an attribute to a symbol. */
21230
2df3850c 21231static void
ff39bb5e 21232dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21233 struct dwarf2_cu *cu)
2df3850c 21234{
5e22e966 21235 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21236 LONGEST value;
d521ce57 21237 const gdb_byte *bytes;
98bfdba5 21238 struct dwarf2_locexpr_baton *baton;
2df3850c 21239
98bfdba5 21240 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21241 sym->print_name (),
98bfdba5
PA
21242 &objfile->objfile_obstack, cu,
21243 &value, &bytes, &baton);
2df3850c 21244
98bfdba5
PA
21245 if (baton != NULL)
21246 {
98bfdba5 21247 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21248 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21249 }
21250 else if (bytes != NULL)
21251 {
21252 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21253 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21254 }
21255 else
21256 {
21257 SYMBOL_VALUE (sym) = value;
f1e6e072 21258 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21259 }
2df3850c
JM
21260}
21261
c906108c
SS
21262/* Return the type of the die in question using its DW_AT_type attribute. */
21263
21264static struct type *
e7c27a73 21265die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21266{
c906108c 21267 struct attribute *type_attr;
c906108c 21268
e142c38c 21269 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21270 if (!type_attr)
21271 {
5e22e966 21272 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21273 /* A missing DW_AT_type represents a void type. */
518817b3 21274 return objfile_type (objfile)->builtin_void;
c906108c 21275 }
348e048f 21276
673bfd45 21277 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21278}
21279
b4ba55a1
JB
21280/* True iff CU's producer generates GNAT Ada auxiliary information
21281 that allows to find parallel types through that information instead
21282 of having to do expensive parallel lookups by type name. */
21283
21284static int
21285need_gnat_info (struct dwarf2_cu *cu)
21286{
de4cb04a
JB
21287 /* Assume that the Ada compiler was GNAT, which always produces
21288 the auxiliary information. */
21289 return (cu->language == language_ada);
b4ba55a1
JB
21290}
21291
b4ba55a1
JB
21292/* Return the auxiliary type of the die in question using its
21293 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21294 attribute is not present. */
21295
21296static struct type *
21297die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21298{
b4ba55a1 21299 struct attribute *type_attr;
b4ba55a1
JB
21300
21301 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21302 if (!type_attr)
21303 return NULL;
21304
673bfd45 21305 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21306}
21307
21308/* If DIE has a descriptive_type attribute, then set the TYPE's
21309 descriptive type accordingly. */
21310
21311static void
21312set_descriptive_type (struct type *type, struct die_info *die,
21313 struct dwarf2_cu *cu)
21314{
21315 struct type *descriptive_type = die_descriptive_type (die, cu);
21316
21317 if (descriptive_type)
21318 {
21319 ALLOCATE_GNAT_AUX_TYPE (type);
21320 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21321 }
21322}
21323
c906108c
SS
21324/* Return the containing type of the die in question using its
21325 DW_AT_containing_type attribute. */
21326
21327static struct type *
e7c27a73 21328die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21329{
c906108c 21330 struct attribute *type_attr;
5e22e966 21331 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21332
e142c38c 21333 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21334 if (!type_attr)
21335 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21336 "[in module %s]"), objfile_name (objfile));
33ac96f0 21337
673bfd45 21338 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21339}
21340
ac9ec31b
DE
21341/* Return an error marker type to use for the ill formed type in DIE/CU. */
21342
21343static struct type *
21344build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21345{
5e22e966 21346 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21347 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21348 char *saved;
ac9ec31b 21349
528e1572
SM
21350 std::string message
21351 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21352 objfile_name (objfile),
21353 sect_offset_str (cu->header.sect_off),
21354 sect_offset_str (die->sect_off));
efba19b0 21355 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21356
19f392bc 21357 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21358}
21359
673bfd45 21360/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21361 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21362 DW_AT_containing_type.
673bfd45
DE
21363 If there is no type substitute an error marker. */
21364
c906108c 21365static struct type *
ff39bb5e 21366lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21367 struct dwarf2_cu *cu)
c906108c 21368{
5e22e966 21369 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21370 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21371 struct type *this_type;
21372
ac9ec31b
DE
21373 gdb_assert (attr->name == DW_AT_type
21374 || attr->name == DW_AT_GNAT_descriptive_type
21375 || attr->name == DW_AT_containing_type);
21376
673bfd45
DE
21377 /* First see if we have it cached. */
21378
36586728
TT
21379 if (attr->form == DW_FORM_GNU_ref_alt)
21380 {
21381 struct dwarf2_per_cu_data *per_cu;
0826b30a 21382 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21383
ed2dc618
SM
21384 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21385 dwarf2_per_objfile);
9c541725 21386 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21387 }
cd6c91b4 21388 else if (attr->form_is_ref ())
673bfd45 21389 {
0826b30a 21390 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21391
9c541725 21392 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21393 }
55f1336d 21394 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21395 {
ac9ec31b 21396 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21397
ac9ec31b 21398 return get_signatured_type (die, signature, cu);
673bfd45
DE
21399 }
21400 else
21401 {
b98664d3 21402 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21403 " at %s [in module %s]"),
21404 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21405 objfile_name (objfile));
ac9ec31b 21406 return build_error_marker_type (cu, die);
673bfd45
DE
21407 }
21408
21409 /* If not cached we need to read it in. */
21410
21411 if (this_type == NULL)
21412 {
ac9ec31b 21413 struct die_info *type_die = NULL;
673bfd45
DE
21414 struct dwarf2_cu *type_cu = cu;
21415
cd6c91b4 21416 if (attr->form_is_ref ())
ac9ec31b
DE
21417 type_die = follow_die_ref (die, attr, &type_cu);
21418 if (type_die == NULL)
21419 return build_error_marker_type (cu, die);
21420 /* If we find the type now, it's probably because the type came
3019eac3
DE
21421 from an inter-CU reference and the type's CU got expanded before
21422 ours. */
ac9ec31b 21423 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21424 }
21425
21426 /* If we still don't have a type use an error marker. */
21427
21428 if (this_type == NULL)
ac9ec31b 21429 return build_error_marker_type (cu, die);
673bfd45 21430
f792889a 21431 return this_type;
c906108c
SS
21432}
21433
673bfd45
DE
21434/* Return the type in DIE, CU.
21435 Returns NULL for invalid types.
21436
02142a6c 21437 This first does a lookup in die_type_hash,
673bfd45
DE
21438 and only reads the die in if necessary.
21439
21440 NOTE: This can be called when reading in partial or full symbols. */
21441
f792889a 21442static struct type *
e7c27a73 21443read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21444{
f792889a
DJ
21445 struct type *this_type;
21446
21447 this_type = get_die_type (die, cu);
21448 if (this_type)
21449 return this_type;
21450
673bfd45
DE
21451 return read_type_die_1 (die, cu);
21452}
21453
21454/* Read the type in DIE, CU.
21455 Returns NULL for invalid types. */
21456
21457static struct type *
21458read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21459{
21460 struct type *this_type = NULL;
21461
c906108c
SS
21462 switch (die->tag)
21463 {
21464 case DW_TAG_class_type:
680b30c7 21465 case DW_TAG_interface_type:
c906108c
SS
21466 case DW_TAG_structure_type:
21467 case DW_TAG_union_type:
f792889a 21468 this_type = read_structure_type (die, cu);
c906108c
SS
21469 break;
21470 case DW_TAG_enumeration_type:
f792889a 21471 this_type = read_enumeration_type (die, cu);
c906108c
SS
21472 break;
21473 case DW_TAG_subprogram:
21474 case DW_TAG_subroutine_type:
edb3359d 21475 case DW_TAG_inlined_subroutine:
f792889a 21476 this_type = read_subroutine_type (die, cu);
c906108c
SS
21477 break;
21478 case DW_TAG_array_type:
f792889a 21479 this_type = read_array_type (die, cu);
c906108c 21480 break;
72019c9c 21481 case DW_TAG_set_type:
f792889a 21482 this_type = read_set_type (die, cu);
72019c9c 21483 break;
c906108c 21484 case DW_TAG_pointer_type:
f792889a 21485 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21486 break;
21487 case DW_TAG_ptr_to_member_type:
f792889a 21488 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21489 break;
21490 case DW_TAG_reference_type:
4297a3f0
AV
21491 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21492 break;
21493 case DW_TAG_rvalue_reference_type:
21494 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21495 break;
21496 case DW_TAG_const_type:
f792889a 21497 this_type = read_tag_const_type (die, cu);
c906108c
SS
21498 break;
21499 case DW_TAG_volatile_type:
f792889a 21500 this_type = read_tag_volatile_type (die, cu);
c906108c 21501 break;
06d66ee9
TT
21502 case DW_TAG_restrict_type:
21503 this_type = read_tag_restrict_type (die, cu);
21504 break;
c906108c 21505 case DW_TAG_string_type:
f792889a 21506 this_type = read_tag_string_type (die, cu);
c906108c
SS
21507 break;
21508 case DW_TAG_typedef:
f792889a 21509 this_type = read_typedef (die, cu);
c906108c 21510 break;
a02abb62 21511 case DW_TAG_subrange_type:
f792889a 21512 this_type = read_subrange_type (die, cu);
a02abb62 21513 break;
c906108c 21514 case DW_TAG_base_type:
f792889a 21515 this_type = read_base_type (die, cu);
c906108c 21516 break;
81a17f79 21517 case DW_TAG_unspecified_type:
f792889a 21518 this_type = read_unspecified_type (die, cu);
81a17f79 21519 break;
0114d602
DJ
21520 case DW_TAG_namespace:
21521 this_type = read_namespace_type (die, cu);
21522 break;
f55ee35c
JK
21523 case DW_TAG_module:
21524 this_type = read_module_type (die, cu);
21525 break;
a2c2acaf
MW
21526 case DW_TAG_atomic_type:
21527 this_type = read_tag_atomic_type (die, cu);
21528 break;
c906108c 21529 default:
b98664d3 21530 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21531 dwarf_tag_name (die->tag));
c906108c
SS
21532 break;
21533 }
63d06c5c 21534
f792889a 21535 return this_type;
63d06c5c
DC
21536}
21537
abc72ce4
DE
21538/* See if we can figure out if the class lives in a namespace. We do
21539 this by looking for a member function; its demangled name will
21540 contain namespace info, if there is any.
21541 Return the computed name or NULL.
21542 Space for the result is allocated on the objfile's obstack.
21543 This is the full-die version of guess_partial_die_structure_name.
21544 In this case we know DIE has no useful parent. */
21545
43816ebc 21546static const char *
abc72ce4
DE
21547guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21548{
21549 struct die_info *spec_die;
21550 struct dwarf2_cu *spec_cu;
21551 struct die_info *child;
5e22e966 21552 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21553
21554 spec_cu = cu;
21555 spec_die = die_specification (die, &spec_cu);
21556 if (spec_die != NULL)
21557 {
21558 die = spec_die;
21559 cu = spec_cu;
21560 }
21561
21562 for (child = die->child;
21563 child != NULL;
21564 child = child->sibling)
21565 {
21566 if (child->tag == DW_TAG_subprogram)
21567 {
73b9be8b 21568 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21569
7d45c7c3 21570 if (linkage_name != NULL)
abc72ce4 21571 {
43816ebc
TT
21572 gdb::unique_xmalloc_ptr<char> actual_name
21573 (language_class_name_from_physname (cu->language_defn,
21574 linkage_name));
21575 const char *name = NULL;
abc72ce4
DE
21576
21577 if (actual_name != NULL)
21578 {
15d034d0 21579 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21580
21581 if (die_name != NULL
43816ebc 21582 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21583 {
21584 /* Strip off the class name from the full name.
21585 We want the prefix. */
21586 int die_name_len = strlen (die_name);
43816ebc
TT
21587 int actual_name_len = strlen (actual_name.get ());
21588 const char *ptr = actual_name.get ();
abc72ce4
DE
21589
21590 /* Test for '::' as a sanity check. */
21591 if (actual_name_len > die_name_len + 2
43816ebc 21592 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21593 name = obstack_strndup (
e3b94546 21594 &objfile->per_bfd->storage_obstack,
43816ebc 21595 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21596 }
21597 }
abc72ce4
DE
21598 return name;
21599 }
21600 }
21601 }
21602
21603 return NULL;
21604}
21605
96408a79
SA
21606/* GCC might emit a nameless typedef that has a linkage name. Determine the
21607 prefix part in such case. See
21608 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21609
a121b7c1 21610static const char *
96408a79
SA
21611anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21612{
21613 struct attribute *attr;
e6a959d6 21614 const char *base;
96408a79
SA
21615
21616 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21617 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21618 return NULL;
21619
7d45c7c3 21620 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21621 return NULL;
21622
73b9be8b 21623 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21624 if (attr == NULL || DW_STRING (attr) == NULL)
21625 return NULL;
21626
21627 /* dwarf2_name had to be already called. */
21628 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21629
21630 /* Strip the base name, keep any leading namespaces/classes. */
21631 base = strrchr (DW_STRING (attr), ':');
21632 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21633 return "";
21634
5e22e966 21635 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21636 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21637 DW_STRING (attr),
21638 &base[-1] - DW_STRING (attr));
96408a79
SA
21639}
21640
fdde2d81 21641/* Return the name of the namespace/class that DIE is defined within,
0114d602 21642 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21643
0114d602
DJ
21644 For example, if we're within the method foo() in the following
21645 code:
21646
21647 namespace N {
21648 class C {
21649 void foo () {
21650 }
21651 };
21652 }
21653
21654 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21655
0d5cff50 21656static const char *
e142c38c 21657determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21658{
5e22e966 21659 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21660 struct die_info *parent, *spec_die;
21661 struct dwarf2_cu *spec_cu;
21662 struct type *parent_type;
a121b7c1 21663 const char *retval;
63d06c5c 21664
9c37b5ae 21665 if (cu->language != language_cplus
c44af4eb
TT
21666 && cu->language != language_fortran && cu->language != language_d
21667 && cu->language != language_rust)
0114d602
DJ
21668 return "";
21669
96408a79
SA
21670 retval = anonymous_struct_prefix (die, cu);
21671 if (retval)
21672 return retval;
21673
0114d602
DJ
21674 /* We have to be careful in the presence of DW_AT_specification.
21675 For example, with GCC 3.4, given the code
21676
21677 namespace N {
21678 void foo() {
21679 // Definition of N::foo.
21680 }
21681 }
21682
21683 then we'll have a tree of DIEs like this:
21684
21685 1: DW_TAG_compile_unit
21686 2: DW_TAG_namespace // N
21687 3: DW_TAG_subprogram // declaration of N::foo
21688 4: DW_TAG_subprogram // definition of N::foo
21689 DW_AT_specification // refers to die #3
21690
21691 Thus, when processing die #4, we have to pretend that we're in
21692 the context of its DW_AT_specification, namely the contex of die
21693 #3. */
21694 spec_cu = cu;
21695 spec_die = die_specification (die, &spec_cu);
21696 if (spec_die == NULL)
21697 parent = die->parent;
21698 else
63d06c5c 21699 {
0114d602
DJ
21700 parent = spec_die->parent;
21701 cu = spec_cu;
63d06c5c 21702 }
0114d602
DJ
21703
21704 if (parent == NULL)
21705 return "";
98bfdba5
PA
21706 else if (parent->building_fullname)
21707 {
21708 const char *name;
21709 const char *parent_name;
21710
21711 /* It has been seen on RealView 2.2 built binaries,
21712 DW_TAG_template_type_param types actually _defined_ as
21713 children of the parent class:
21714
21715 enum E {};
21716 template class <class Enum> Class{};
21717 Class<enum E> class_e;
21718
21719 1: DW_TAG_class_type (Class)
21720 2: DW_TAG_enumeration_type (E)
21721 3: DW_TAG_enumerator (enum1:0)
21722 3: DW_TAG_enumerator (enum2:1)
21723 ...
21724 2: DW_TAG_template_type_param
21725 DW_AT_type DW_FORM_ref_udata (E)
21726
21727 Besides being broken debug info, it can put GDB into an
21728 infinite loop. Consider:
21729
21730 When we're building the full name for Class<E>, we'll start
21731 at Class, and go look over its template type parameters,
21732 finding E. We'll then try to build the full name of E, and
21733 reach here. We're now trying to build the full name of E,
21734 and look over the parent DIE for containing scope. In the
21735 broken case, if we followed the parent DIE of E, we'd again
21736 find Class, and once again go look at its template type
21737 arguments, etc., etc. Simply don't consider such parent die
21738 as source-level parent of this die (it can't be, the language
21739 doesn't allow it), and break the loop here. */
21740 name = dwarf2_name (die, cu);
21741 parent_name = dwarf2_name (parent, cu);
b98664d3 21742 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21743 name ? name : "<unknown>",
21744 parent_name ? parent_name : "<unknown>");
21745 return "";
21746 }
63d06c5c 21747 else
0114d602
DJ
21748 switch (parent->tag)
21749 {
63d06c5c 21750 case DW_TAG_namespace:
0114d602 21751 parent_type = read_type_die (parent, cu);
acebe513
UW
21752 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21753 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21754 Work around this problem here. */
21755 if (cu->language == language_cplus
7d93a1e0 21756 && strcmp (parent_type->name (), "::") == 0)
acebe513 21757 return "";
0114d602 21758 /* We give a name to even anonymous namespaces. */
7d93a1e0 21759 return parent_type->name ();
63d06c5c 21760 case DW_TAG_class_type:
680b30c7 21761 case DW_TAG_interface_type:
63d06c5c 21762 case DW_TAG_structure_type:
0114d602 21763 case DW_TAG_union_type:
f55ee35c 21764 case DW_TAG_module:
0114d602 21765 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21766 if (parent_type->name () != NULL)
21767 return parent_type->name ();
0114d602
DJ
21768 else
21769 /* An anonymous structure is only allowed non-static data
21770 members; no typedefs, no member functions, et cetera.
21771 So it does not need a prefix. */
21772 return "";
abc72ce4 21773 case DW_TAG_compile_unit:
95554aad 21774 case DW_TAG_partial_unit:
abc72ce4
DE
21775 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21776 if (cu->language == language_cplus
5989a64e 21777 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21778 && die->child != NULL
21779 && (die->tag == DW_TAG_class_type
21780 || die->tag == DW_TAG_structure_type
21781 || die->tag == DW_TAG_union_type))
21782 {
43816ebc 21783 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21784 if (name != NULL)
21785 return name;
21786 }
21787 return "";
0a4b0913
AB
21788 case DW_TAG_subprogram:
21789 /* Nested subroutines in Fortran get a prefix with the name
21790 of the parent's subroutine. */
21791 if (cu->language == language_fortran)
21792 {
21793 if ((die->tag == DW_TAG_subprogram)
21794 && (dwarf2_name (parent, cu) != NULL))
21795 return dwarf2_name (parent, cu);
21796 }
21797 return determine_prefix (parent, cu);
3d567982
TT
21798 case DW_TAG_enumeration_type:
21799 parent_type = read_type_die (parent, cu);
21800 if (TYPE_DECLARED_CLASS (parent_type))
21801 {
7d93a1e0
SM
21802 if (parent_type->name () != NULL)
21803 return parent_type->name ();
3d567982
TT
21804 return "";
21805 }
21806 /* Fall through. */
63d06c5c 21807 default:
8176b9b8 21808 return determine_prefix (parent, cu);
63d06c5c 21809 }
63d06c5c
DC
21810}
21811
3e43a32a
MS
21812/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21813 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21814 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21815 an obconcat, otherwise allocate storage for the result. The CU argument is
21816 used to determine the language and hence, the appropriate separator. */
987504bb 21817
f55ee35c 21818#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21819
21820static char *
f55ee35c
JK
21821typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21822 int physname, struct dwarf2_cu *cu)
63d06c5c 21823{
f55ee35c 21824 const char *lead = "";
5c315b68 21825 const char *sep;
63d06c5c 21826
3e43a32a
MS
21827 if (suffix == NULL || suffix[0] == '\0'
21828 || prefix == NULL || prefix[0] == '\0')
987504bb 21829 sep = "";
45280282
IB
21830 else if (cu->language == language_d)
21831 {
21832 /* For D, the 'main' function could be defined in any module, but it
21833 should never be prefixed. */
21834 if (strcmp (suffix, "D main") == 0)
21835 {
21836 prefix = "";
21837 sep = "";
21838 }
21839 else
21840 sep = ".";
21841 }
f55ee35c
JK
21842 else if (cu->language == language_fortran && physname)
21843 {
21844 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21845 DW_AT_MIPS_linkage_name is preferred and used instead. */
21846
21847 lead = "__";
21848 sep = "_MOD_";
21849 }
987504bb
JJ
21850 else
21851 sep = "::";
63d06c5c 21852
6dd47d34
DE
21853 if (prefix == NULL)
21854 prefix = "";
21855 if (suffix == NULL)
21856 suffix = "";
21857
987504bb
JJ
21858 if (obs == NULL)
21859 {
3e43a32a 21860 char *retval
224c3ddb
SM
21861 = ((char *)
21862 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21863
f55ee35c
JK
21864 strcpy (retval, lead);
21865 strcat (retval, prefix);
6dd47d34
DE
21866 strcat (retval, sep);
21867 strcat (retval, suffix);
63d06c5c
DC
21868 return retval;
21869 }
987504bb
JJ
21870 else
21871 {
21872 /* We have an obstack. */
f55ee35c 21873 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21874 }
63d06c5c
DC
21875}
21876
71c25dea
TT
21877/* Get name of a die, return NULL if not found. */
21878
15d034d0
TT
21879static const char *
21880dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21881 struct objfile *objfile)
71c25dea
TT
21882{
21883 if (name && cu->language == language_cplus)
21884 {
596dc4ad
TT
21885 gdb::unique_xmalloc_ptr<char> canon_name
21886 = cp_canonicalize_string (name);
71c25dea 21887
596dc4ad
TT
21888 if (canon_name != nullptr)
21889 name = objfile->intern (canon_name.get ());
71c25dea
TT
21890 }
21891
21892 return name;
c906108c
SS
21893}
21894
96553a0c
DE
21895/* Get name of a die, return NULL if not found.
21896 Anonymous namespaces are converted to their magic string. */
9219021c 21897
15d034d0 21898static const char *
e142c38c 21899dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21900{
21901 struct attribute *attr;
5e22e966 21902 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21903
e142c38c 21904 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21905 if ((!attr || !DW_STRING (attr))
96553a0c 21906 && die->tag != DW_TAG_namespace
53832f31
TT
21907 && die->tag != DW_TAG_class_type
21908 && die->tag != DW_TAG_interface_type
21909 && die->tag != DW_TAG_structure_type
21910 && die->tag != DW_TAG_union_type)
71c25dea
TT
21911 return NULL;
21912
21913 switch (die->tag)
21914 {
21915 case DW_TAG_compile_unit:
95554aad 21916 case DW_TAG_partial_unit:
71c25dea
TT
21917 /* Compilation units have a DW_AT_name that is a filename, not
21918 a source language identifier. */
21919 case DW_TAG_enumeration_type:
21920 case DW_TAG_enumerator:
21921 /* These tags always have simple identifiers already; no need
21922 to canonicalize them. */
21923 return DW_STRING (attr);
907af001 21924
96553a0c
DE
21925 case DW_TAG_namespace:
21926 if (attr != NULL && DW_STRING (attr) != NULL)
21927 return DW_STRING (attr);
21928 return CP_ANONYMOUS_NAMESPACE_STR;
21929
907af001
UW
21930 case DW_TAG_class_type:
21931 case DW_TAG_interface_type:
21932 case DW_TAG_structure_type:
21933 case DW_TAG_union_type:
21934 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21935 structures or unions. These were of the form "._%d" in GCC 4.1,
21936 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21937 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21938 if (attr && DW_STRING (attr)
61012eef
GB
21939 && (startswith (DW_STRING (attr), "._")
21940 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21941 return NULL;
53832f31
TT
21942
21943 /* GCC might emit a nameless typedef that has a linkage name. See
21944 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21945 if (!attr || DW_STRING (attr) == NULL)
21946 {
73b9be8b 21947 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21948 if (attr == NULL || DW_STRING (attr) == NULL)
21949 return NULL;
21950
df5c6c50
JK
21951 /* Avoid demangling DW_STRING (attr) the second time on a second
21952 call for the same DIE. */
21953 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21954 {
43816ebc
TT
21955 gdb::unique_xmalloc_ptr<char> demangled
21956 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21957 if (demangled == nullptr)
21958 return nullptr;
43816ebc 21959
be1e3d3e 21960 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21961 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21962 }
67430cd0
TT
21963
21964 /* Strip any leading namespaces/classes, keep only the base name.
21965 DW_AT_name for named DIEs does not contain the prefixes. */
21966 const char *base = strrchr (DW_STRING (attr), ':');
21967 if (base && base > DW_STRING (attr) && base[-1] == ':')
21968 return &base[1];
21969 else
21970 return DW_STRING (attr);
53832f31 21971 }
907af001
UW
21972 break;
21973
71c25dea 21974 default:
907af001
UW
21975 break;
21976 }
21977
21978 if (!DW_STRING_IS_CANONICAL (attr))
21979 {
be1e3d3e
TT
21980 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21981 objfile);
907af001 21982 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21983 }
907af001 21984 return DW_STRING (attr);
9219021c
DC
21985}
21986
21987/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21988 is none. *EXT_CU is the CU containing DIE on input, and the CU
21989 containing the return value on output. */
9219021c
DC
21990
21991static struct die_info *
f2f0e013 21992dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21993{
21994 struct attribute *attr;
9219021c 21995
f2f0e013 21996 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21997 if (attr == NULL)
21998 return NULL;
21999
f2f0e013 22000 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22001}
22002
f9aca02d 22003static void
d97bc12b 22004dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22005{
22006 unsigned int i;
22007
d97bc12b 22008 print_spaces (indent, f);
9d8780f0 22009 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22010 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22011 sect_offset_str (die->sect_off));
d97bc12b
DE
22012
22013 if (die->parent != NULL)
22014 {
22015 print_spaces (indent, f);
9d8780f0
SM
22016 fprintf_unfiltered (f, " parent at offset: %s\n",
22017 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22018 }
22019
22020 print_spaces (indent, f);
22021 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22022 dwarf_bool_name (die->child != NULL));
c906108c 22023
d97bc12b
DE
22024 print_spaces (indent, f);
22025 fprintf_unfiltered (f, " attributes:\n");
22026
c906108c
SS
22027 for (i = 0; i < die->num_attrs; ++i)
22028 {
d97bc12b
DE
22029 print_spaces (indent, f);
22030 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22031 dwarf_attr_name (die->attrs[i].name),
22032 dwarf_form_name (die->attrs[i].form));
d97bc12b 22033
c906108c
SS
22034 switch (die->attrs[i].form)
22035 {
c906108c 22036 case DW_FORM_addr:
336d760d 22037 case DW_FORM_addrx:
3019eac3 22038 case DW_FORM_GNU_addr_index:
d97bc12b 22039 fprintf_unfiltered (f, "address: ");
5af949e3 22040 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22041 break;
22042 case DW_FORM_block2:
22043 case DW_FORM_block4:
22044 case DW_FORM_block:
22045 case DW_FORM_block1:
56eb65bd
SP
22046 fprintf_unfiltered (f, "block: size %s",
22047 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22048 break;
2dc7f7b3 22049 case DW_FORM_exprloc:
56eb65bd
SP
22050 fprintf_unfiltered (f, "expression: size %s",
22051 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22052 break;
0224619f
JK
22053 case DW_FORM_data16:
22054 fprintf_unfiltered (f, "constant of 16 bytes");
22055 break;
4568ecf9
DE
22056 case DW_FORM_ref_addr:
22057 fprintf_unfiltered (f, "ref address: ");
22058 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22059 break;
36586728
TT
22060 case DW_FORM_GNU_ref_alt:
22061 fprintf_unfiltered (f, "alt ref address: ");
22062 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22063 break;
10b3939b
DJ
22064 case DW_FORM_ref1:
22065 case DW_FORM_ref2:
22066 case DW_FORM_ref4:
4568ecf9
DE
22067 case DW_FORM_ref8:
22068 case DW_FORM_ref_udata:
d97bc12b 22069 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22070 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22071 break;
c906108c
SS
22072 case DW_FORM_data1:
22073 case DW_FORM_data2:
22074 case DW_FORM_data4:
ce5d95e1 22075 case DW_FORM_data8:
c906108c
SS
22076 case DW_FORM_udata:
22077 case DW_FORM_sdata:
43bbcdc2
PH
22078 fprintf_unfiltered (f, "constant: %s",
22079 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22080 break;
2dc7f7b3
TT
22081 case DW_FORM_sec_offset:
22082 fprintf_unfiltered (f, "section offset: %s",
22083 pulongest (DW_UNSND (&die->attrs[i])));
22084 break;
55f1336d 22085 case DW_FORM_ref_sig8:
ac9ec31b
DE
22086 fprintf_unfiltered (f, "signature: %s",
22087 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22088 break;
c906108c 22089 case DW_FORM_string:
4bdf3d34 22090 case DW_FORM_strp:
43988095 22091 case DW_FORM_line_strp:
cf532bd1 22092 case DW_FORM_strx:
3019eac3 22093 case DW_FORM_GNU_str_index:
36586728 22094 case DW_FORM_GNU_strp_alt:
8285870a 22095 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22096 DW_STRING (&die->attrs[i])
8285870a
JK
22097 ? DW_STRING (&die->attrs[i]) : "",
22098 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22099 break;
22100 case DW_FORM_flag:
22101 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22102 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22103 else
d97bc12b 22104 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22105 break;
2dc7f7b3
TT
22106 case DW_FORM_flag_present:
22107 fprintf_unfiltered (f, "flag: TRUE");
22108 break;
a8329558 22109 case DW_FORM_indirect:
0963b4bd
MS
22110 /* The reader will have reduced the indirect form to
22111 the "base form" so this form should not occur. */
5f48f8f3 22112 fprintf_unfiltered (f,
3e43a32a 22113 "unexpected attribute form: DW_FORM_indirect");
a8329558 22114 break;
663c44ac
JK
22115 case DW_FORM_implicit_const:
22116 fprintf_unfiltered (f, "constant: %s",
22117 plongest (DW_SND (&die->attrs[i])));
22118 break;
c906108c 22119 default:
d97bc12b 22120 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22121 die->attrs[i].form);
d97bc12b 22122 break;
c906108c 22123 }
d97bc12b 22124 fprintf_unfiltered (f, "\n");
c906108c
SS
22125 }
22126}
22127
f9aca02d 22128static void
d97bc12b 22129dump_die_for_error (struct die_info *die)
c906108c 22130{
d97bc12b
DE
22131 dump_die_shallow (gdb_stderr, 0, die);
22132}
22133
22134static void
22135dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22136{
22137 int indent = level * 4;
22138
22139 gdb_assert (die != NULL);
22140
22141 if (level >= max_level)
22142 return;
22143
22144 dump_die_shallow (f, indent, die);
22145
22146 if (die->child != NULL)
c906108c 22147 {
d97bc12b
DE
22148 print_spaces (indent, f);
22149 fprintf_unfiltered (f, " Children:");
22150 if (level + 1 < max_level)
22151 {
22152 fprintf_unfiltered (f, "\n");
22153 dump_die_1 (f, level + 1, max_level, die->child);
22154 }
22155 else
22156 {
3e43a32a
MS
22157 fprintf_unfiltered (f,
22158 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22159 }
22160 }
22161
22162 if (die->sibling != NULL && level > 0)
22163 {
22164 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22165 }
22166}
22167
d97bc12b
DE
22168/* This is called from the pdie macro in gdbinit.in.
22169 It's not static so gcc will keep a copy callable from gdb. */
22170
22171void
22172dump_die (struct die_info *die, int max_level)
22173{
22174 dump_die_1 (gdb_stdlog, 0, max_level, die);
22175}
22176
f9aca02d 22177static void
51545339 22178store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22179{
51545339 22180 void **slot;
c906108c 22181
9c541725
PA
22182 slot = htab_find_slot_with_hash (cu->die_hash, die,
22183 to_underlying (die->sect_off),
b64f50a1 22184 INSERT);
51545339
DJ
22185
22186 *slot = die;
c906108c
SS
22187}
22188
348e048f
DE
22189/* Follow reference or signature attribute ATTR of SRC_DIE.
22190 On entry *REF_CU is the CU of SRC_DIE.
22191 On exit *REF_CU is the CU of the result. */
22192
22193static struct die_info *
ff39bb5e 22194follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22195 struct dwarf2_cu **ref_cu)
22196{
22197 struct die_info *die;
22198
cd6c91b4 22199 if (attr->form_is_ref ())
348e048f 22200 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22201 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22202 die = follow_die_sig (src_die, attr, ref_cu);
22203 else
22204 {
22205 dump_die_for_error (src_die);
22206 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22207 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22208 }
22209
22210 return die;
03dd20cc
DJ
22211}
22212
5c631832 22213/* Follow reference OFFSET.
673bfd45
DE
22214 On entry *REF_CU is the CU of the source die referencing OFFSET.
22215 On exit *REF_CU is the CU of the result.
22216 Returns NULL if OFFSET is invalid. */
f504f079 22217
f9aca02d 22218static struct die_info *
9c541725 22219follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22220 struct dwarf2_cu **ref_cu)
c906108c 22221{
10b3939b 22222 struct die_info temp_die;
f2f0e013 22223 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22224 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22225
348e048f
DE
22226 gdb_assert (cu->per_cu != NULL);
22227
98bfdba5
PA
22228 target_cu = cu;
22229
3019eac3 22230 if (cu->per_cu->is_debug_types)
348e048f
DE
22231 {
22232 /* .debug_types CUs cannot reference anything outside their CU.
22233 If they need to, they have to reference a signatured type via
55f1336d 22234 DW_FORM_ref_sig8. */
4057dfde 22235 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22236 return NULL;
348e048f 22237 }
36586728 22238 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22239 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22240 {
22241 struct dwarf2_per_cu_data *per_cu;
9a619af0 22242
9c541725 22243 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22244 dwarf2_per_objfile);
03dd20cc
DJ
22245
22246 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22247 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
ab432490 22248 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
03dd20cc 22249
10b3939b
DJ
22250 target_cu = per_cu->cu;
22251 }
98bfdba5
PA
22252 else if (cu->dies == NULL)
22253 {
22254 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22255 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
ab432490
SM
22256 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22257 language_minimal);
98bfdba5 22258 }
c906108c 22259
f2f0e013 22260 *ref_cu = target_cu;
9c541725 22261 temp_die.sect_off = sect_off;
c24bdb02
KS
22262
22263 if (target_cu != cu)
22264 target_cu->ancestor = cu;
22265
9a3c8263 22266 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22267 &temp_die,
22268 to_underlying (sect_off));
5c631832 22269}
10b3939b 22270
5c631832
JK
22271/* Follow reference attribute ATTR of SRC_DIE.
22272 On entry *REF_CU is the CU of SRC_DIE.
22273 On exit *REF_CU is the CU of the result. */
22274
22275static struct die_info *
ff39bb5e 22276follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22277 struct dwarf2_cu **ref_cu)
22278{
0826b30a 22279 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22280 struct dwarf2_cu *cu = *ref_cu;
22281 struct die_info *die;
22282
9c541725 22283 die = follow_die_offset (sect_off,
36586728
TT
22284 (attr->form == DW_FORM_GNU_ref_alt
22285 || cu->per_cu->is_dwz),
22286 ref_cu);
5c631832 22287 if (!die)
9d8780f0
SM
22288 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22289 "at %s [in module %s]"),
22290 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22291 objfile_name (cu->per_objfile->objfile));
348e048f 22292
5c631832
JK
22293 return die;
22294}
22295
d4c9a4f8 22296/* See read.h. */
5c631832
JK
22297
22298struct dwarf2_locexpr_baton
9c541725 22299dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22300 dwarf2_per_cu_data *per_cu,
8b9737bf 22301 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22302 void *baton, bool resolve_abstract_p)
5c631832 22303{
918dd910 22304 struct dwarf2_cu *cu;
5c631832
JK
22305 struct die_info *die;
22306 struct attribute *attr;
22307 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22308 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22309 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22310
918dd910 22311 if (per_cu->cu == NULL)
ab432490 22312 load_cu (per_cu, dwarf2_per_objfile, false);
918dd910 22313 cu = per_cu->cu;
cc12ce38
DE
22314 if (cu == NULL)
22315 {
22316 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22317 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22318 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22319 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22320 }
918dd910 22321
9c541725 22322 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22323 if (!die)
9d8780f0
SM
22324 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22325 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22326
22327 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22328 if (!attr && resolve_abstract_p
5989a64e
SM
22329 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22330 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22331 {
22332 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22333 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22334 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22335
3360b6e7 22336 for (const auto &cand_off
5989a64e 22337 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22338 {
3360b6e7
TV
22339 struct dwarf2_cu *cand_cu = cu;
22340 struct die_info *cand
22341 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22342 if (!cand
22343 || !cand->parent
e4a62c65
TV
22344 || cand->parent->tag != DW_TAG_subprogram)
22345 continue;
22346
22347 CORE_ADDR pc_low, pc_high;
22348 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22349 if (pc_low == ((CORE_ADDR) -1))
22350 continue;
22351 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22352 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22353 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22354 continue;
22355
22356 die = cand;
22357 attr = dwarf2_attr (die, DW_AT_location, cu);
22358 break;
22359 }
22360 }
22361
5c631832
JK
22362 if (!attr)
22363 {
e103e986
JK
22364 /* DWARF: "If there is no such attribute, then there is no effect.".
22365 DATA is ignored if SIZE is 0. */
5c631832 22366
e103e986 22367 retval.data = NULL;
5c631832
JK
22368 retval.size = 0;
22369 }
cd6c91b4 22370 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22371 {
22372 struct dwarf2_loclist_baton loclist_baton;
22373 CORE_ADDR pc = (*get_frame_pc) (baton);
22374 size_t size;
22375
22376 fill_in_loclist_baton (cu, &loclist_baton, attr);
22377
22378 retval.data = dwarf2_find_location_expression (&loclist_baton,
22379 &size, pc);
22380 retval.size = size;
22381 }
5c631832
JK
22382 else
22383 {
4fc6c0d5 22384 if (!attr->form_is_block ())
9d8780f0 22385 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22386 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22387 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22388
22389 retval.data = DW_BLOCK (attr)->data;
22390 retval.size = DW_BLOCK (attr)->size;
22391 }
a50264ba 22392 retval.per_objfile = dwarf2_per_objfile;
5c631832 22393 retval.per_cu = cu->per_cu;
918dd910 22394
ed2dc618 22395 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22396
5c631832 22397 return retval;
348e048f
DE
22398}
22399
d4c9a4f8 22400/* See read.h. */
8b9737bf
TT
22401
22402struct dwarf2_locexpr_baton
22403dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22404 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22405 CORE_ADDR (*get_frame_pc) (void *baton),
22406 void *baton)
22407{
9c541725 22408 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22409
9c541725 22410 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22411}
22412
b6807d98
TT
22413/* Write a constant of a given type as target-ordered bytes into
22414 OBSTACK. */
22415
22416static const gdb_byte *
22417write_constant_as_bytes (struct obstack *obstack,
22418 enum bfd_endian byte_order,
22419 struct type *type,
22420 ULONGEST value,
22421 LONGEST *len)
22422{
22423 gdb_byte *result;
22424
22425 *len = TYPE_LENGTH (type);
224c3ddb 22426 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22427 store_unsigned_integer (result, *len, byte_order, value);
22428
22429 return result;
22430}
22431
d4c9a4f8 22432/* See read.h. */
b6807d98
TT
22433
22434const gdb_byte *
9c541725 22435dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22436 dwarf2_per_cu_data *per_cu,
22437 obstack *obstack,
b6807d98
TT
22438 LONGEST *len)
22439{
22440 struct dwarf2_cu *cu;
22441 struct die_info *die;
22442 struct attribute *attr;
22443 const gdb_byte *result = NULL;
22444 struct type *type;
22445 LONGEST value;
22446 enum bfd_endian byte_order;
e3b94546 22447 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22448
b6807d98 22449 if (per_cu->cu == NULL)
ab432490 22450 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
b6807d98 22451 cu = per_cu->cu;
cc12ce38
DE
22452 if (cu == NULL)
22453 {
22454 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22455 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22456 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22457 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22458 }
b6807d98 22459
9c541725 22460 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22461 if (!die)
9d8780f0
SM
22462 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22463 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22464
22465 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22466 if (attr == NULL)
22467 return NULL;
22468
e3b94546 22469 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22470 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22471
22472 switch (attr->form)
22473 {
22474 case DW_FORM_addr:
336d760d 22475 case DW_FORM_addrx:
b6807d98
TT
22476 case DW_FORM_GNU_addr_index:
22477 {
22478 gdb_byte *tem;
22479
22480 *len = cu->header.addr_size;
224c3ddb 22481 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22482 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22483 result = tem;
22484 }
22485 break;
22486 case DW_FORM_string:
22487 case DW_FORM_strp:
cf532bd1 22488 case DW_FORM_strx:
b6807d98
TT
22489 case DW_FORM_GNU_str_index:
22490 case DW_FORM_GNU_strp_alt:
22491 /* DW_STRING is already allocated on the objfile obstack, point
22492 directly to it. */
22493 result = (const gdb_byte *) DW_STRING (attr);
22494 *len = strlen (DW_STRING (attr));
22495 break;
22496 case DW_FORM_block1:
22497 case DW_FORM_block2:
22498 case DW_FORM_block4:
22499 case DW_FORM_block:
22500 case DW_FORM_exprloc:
0224619f 22501 case DW_FORM_data16:
b6807d98
TT
22502 result = DW_BLOCK (attr)->data;
22503 *len = DW_BLOCK (attr)->size;
22504 break;
22505
22506 /* The DW_AT_const_value attributes are supposed to carry the
22507 symbol's value "represented as it would be on the target
22508 architecture." By the time we get here, it's already been
22509 converted to host endianness, so we just need to sign- or
22510 zero-extend it as appropriate. */
22511 case DW_FORM_data1:
22512 type = die_type (die, cu);
22513 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22514 if (result == NULL)
22515 result = write_constant_as_bytes (obstack, byte_order,
22516 type, value, len);
22517 break;
22518 case DW_FORM_data2:
22519 type = die_type (die, cu);
22520 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22521 if (result == NULL)
22522 result = write_constant_as_bytes (obstack, byte_order,
22523 type, value, len);
22524 break;
22525 case DW_FORM_data4:
22526 type = die_type (die, cu);
22527 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22528 if (result == NULL)
22529 result = write_constant_as_bytes (obstack, byte_order,
22530 type, value, len);
22531 break;
22532 case DW_FORM_data8:
22533 type = die_type (die, cu);
22534 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22535 if (result == NULL)
22536 result = write_constant_as_bytes (obstack, byte_order,
22537 type, value, len);
22538 break;
22539
22540 case DW_FORM_sdata:
663c44ac 22541 case DW_FORM_implicit_const:
b6807d98
TT
22542 type = die_type (die, cu);
22543 result = write_constant_as_bytes (obstack, byte_order,
22544 type, DW_SND (attr), len);
22545 break;
22546
22547 case DW_FORM_udata:
22548 type = die_type (die, cu);
22549 result = write_constant_as_bytes (obstack, byte_order,
22550 type, DW_UNSND (attr), len);
22551 break;
22552
22553 default:
b98664d3 22554 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22555 dwarf_form_name (attr->form));
22556 break;
22557 }
22558
22559 return result;
22560}
22561
d4c9a4f8 22562/* See read.h. */
7942e96e
AA
22563
22564struct type *
9c541725 22565dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22566 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22567{
22568 struct dwarf2_cu *cu;
22569 struct die_info *die;
22570
7942e96e 22571 if (per_cu->cu == NULL)
ab432490 22572 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
7942e96e
AA
22573 cu = per_cu->cu;
22574 if (!cu)
22575 return NULL;
22576
9c541725 22577 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22578 if (!die)
22579 return NULL;
22580
22581 return die_type (die, cu);
22582}
22583
8cb5117c 22584/* See read.h. */
8a9b8146
TT
22585
22586struct type *
b64f50a1 22587dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22588 struct dwarf2_per_cu_data *per_cu)
22589{
9c541725 22590 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22591 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22592}
22593
ac9ec31b 22594/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22595 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22596 On exit *REF_CU is the CU of the result.
22597 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22598
22599static struct die_info *
ac9ec31b
DE
22600follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22601 struct dwarf2_cu **ref_cu)
348e048f 22602{
348e048f 22603 struct die_info temp_die;
c24bdb02 22604 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22605 struct die_info *die;
22606
ac9ec31b
DE
22607 /* While it might be nice to assert sig_type->type == NULL here,
22608 we can get here for DW_AT_imported_declaration where we need
22609 the DIE not the type. */
348e048f
DE
22610
22611 /* If necessary, add it to the queue and load its DIEs. */
22612
95554aad 22613 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
ab432490 22614 read_signatured_type (sig_type, (*ref_cu)->per_objfile);
348e048f 22615
348e048f 22616 sig_cu = sig_type->per_cu.cu;
69d751e3 22617 gdb_assert (sig_cu != NULL);
9c541725
PA
22618 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22619 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22620 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22621 to_underlying (temp_die.sect_off));
348e048f
DE
22622 if (die)
22623 {
5e22e966 22624 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
ed2dc618 22625
796a7ff8
DE
22626 /* For .gdb_index version 7 keep track of included TUs.
22627 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22628 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22629 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22630 {
ae640021 22631 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22632 }
22633
348e048f 22634 *ref_cu = sig_cu;
c24bdb02
KS
22635 if (sig_cu != cu)
22636 sig_cu->ancestor = cu;
22637
348e048f
DE
22638 return die;
22639 }
22640
ac9ec31b
DE
22641 return NULL;
22642}
22643
22644/* Follow signatured type referenced by ATTR in SRC_DIE.
22645 On entry *REF_CU is the CU of SRC_DIE.
22646 On exit *REF_CU is the CU of the result.
22647 The result is the DIE of the type.
22648 If the referenced type cannot be found an error is thrown. */
22649
22650static struct die_info *
ff39bb5e 22651follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22652 struct dwarf2_cu **ref_cu)
22653{
22654 ULONGEST signature = DW_SIGNATURE (attr);
22655 struct signatured_type *sig_type;
22656 struct die_info *die;
22657
22658 gdb_assert (attr->form == DW_FORM_ref_sig8);
22659
a2ce51a0 22660 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22661 /* sig_type will be NULL if the signatured type is missing from
22662 the debug info. */
22663 if (sig_type == NULL)
22664 {
22665 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22666 " from DIE at %s [in module %s]"),
22667 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22668 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22669 }
22670
22671 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22672 if (die == NULL)
22673 {
22674 dump_die_for_error (src_die);
22675 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22676 " from DIE at %s [in module %s]"),
22677 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22678 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22679 }
22680
22681 return die;
22682}
22683
22684/* Get the type specified by SIGNATURE referenced in DIE/CU,
22685 reading in and processing the type unit if necessary. */
22686
22687static struct type *
22688get_signatured_type (struct die_info *die, ULONGEST signature,
22689 struct dwarf2_cu *cu)
22690{
5e22e966 22691 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b
DE
22692 struct signatured_type *sig_type;
22693 struct dwarf2_cu *type_cu;
22694 struct die_info *type_die;
22695 struct type *type;
22696
a2ce51a0 22697 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22698 /* sig_type will be NULL if the signatured type is missing from
22699 the debug info. */
22700 if (sig_type == NULL)
22701 {
b98664d3 22702 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22703 " from DIE at %s [in module %s]"),
22704 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22705 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22706 return build_error_marker_type (cu, die);
22707 }
22708
22709 /* If we already know the type we're done. */
22710 if (sig_type->type != NULL)
22711 return sig_type->type;
22712
22713 type_cu = cu;
22714 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22715 if (type_die != NULL)
22716 {
22717 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22718 is created. This is important, for example, because for c++ classes
22719 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22720 type = read_type_die (type_die, type_cu);
22721 if (type == NULL)
22722 {
b98664d3 22723 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22724 " referenced from DIE at %s [in module %s]"),
22725 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22726 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22727 type = build_error_marker_type (cu, die);
22728 }
22729 }
22730 else
22731 {
b98664d3 22732 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22733 " from DIE at %s [in module %s]"),
22734 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22735 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22736 type = build_error_marker_type (cu, die);
22737 }
22738 sig_type->type = type;
22739
22740 return type;
22741}
22742
22743/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22744 reading in and processing the type unit if necessary. */
22745
22746static struct type *
ff39bb5e 22747get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22748 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22749{
22750 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22751 if (attr->form_is_ref ())
ac9ec31b
DE
22752 {
22753 struct dwarf2_cu *type_cu = cu;
22754 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22755
22756 return read_type_die (type_die, type_cu);
22757 }
22758 else if (attr->form == DW_FORM_ref_sig8)
22759 {
22760 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22761 }
22762 else
22763 {
5e22e966 22764 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22765
b98664d3 22766 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22767 " at %s [in module %s]"),
22768 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22769 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22770 return build_error_marker_type (cu, die);
22771 }
348e048f
DE
22772}
22773
e5fe5e75 22774/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22775
22776static void
ab432490
SM
22777load_full_type_unit (dwarf2_per_cu_data *per_cu,
22778 dwarf2_per_objfile *per_objfile)
348e048f 22779{
52dc124a 22780 struct signatured_type *sig_type;
348e048f 22781
f4dc4d17 22782 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22783 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22784
6721b2ec
DE
22785 /* We have the per_cu, but we need the signatured_type.
22786 Fortunately this is an easy translation. */
22787 gdb_assert (per_cu->is_debug_types);
22788 sig_type = (struct signatured_type *) per_cu;
348e048f 22789
6721b2ec 22790 gdb_assert (per_cu->cu == NULL);
348e048f 22791
ab432490 22792 read_signatured_type (sig_type, per_objfile);
348e048f 22793
6721b2ec 22794 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22795}
22796
3019eac3
DE
22797/* Read in a signatured type and build its CU and DIEs.
22798 If the type is a stub for the real type in a DWO file,
22799 read in the real type from the DWO file as well. */
dee91e82
DE
22800
22801static void
ab432490
SM
22802read_signatured_type (signatured_type *sig_type,
22803 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22804{
22805 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22806
3019eac3 22807 gdb_assert (per_cu->is_debug_types);
dee91e82 22808 gdb_assert (per_cu->cu == NULL);
348e048f 22809
ab432490 22810 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
c0ab21c2
TT
22811
22812 if (!reader.dummy_p)
22813 {
22814 struct dwarf2_cu *cu = reader.cu;
22815 const gdb_byte *info_ptr = reader.info_ptr;
22816
22817 gdb_assert (cu->die_hash == NULL);
22818 cu->die_hash =
22819 htab_create_alloc_ex (cu->header.length / 12,
22820 die_hash,
22821 die_eq,
22822 NULL,
22823 &cu->comp_unit_obstack,
22824 hashtab_obstack_allocate,
22825 dummy_obstack_deallocate);
22826
3e225074 22827 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22828 reader.comp_unit_die->child
22829 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22830 reader.comp_unit_die);
22831 cu->dies = reader.comp_unit_die;
22832 /* comp_unit_die is not stored in die_hash, no need. */
22833
22834 /* We try not to read any attributes in this function, because
22835 not all CUs needed for references have been loaded yet, and
22836 symbol table processing isn't initialized. But we have to
22837 set the CU language, or we won't be able to build types
22838 correctly. Similarly, if we do not read the producer, we can
22839 not apply producer-specific interpretation. */
22840 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22841
22842 reader.keep ();
c0ab21c2
TT
22843 }
22844
7ee85ab1 22845 sig_type->per_cu.tu_read = 1;
c906108c
SS
22846}
22847
c906108c
SS
22848/* Decode simple location descriptions.
22849 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22850 the location and return the value. If COMPUTED is non-null, it is
22851 set to true to indicate that decoding was successful, and false
22852 otherwise. If COMPUTED is null, then this function may emit a
22853 complaint. */
c906108c
SS
22854
22855static CORE_ADDR
7d79de9a 22856decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22857{
5e22e966 22858 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22859 size_t i;
22860 size_t size = blk->size;
d521ce57 22861 const gdb_byte *data = blk->data;
21ae7a4d
JK
22862 CORE_ADDR stack[64];
22863 int stacki;
22864 unsigned int bytes_read, unsnd;
22865 gdb_byte op;
c906108c 22866
7d79de9a
TT
22867 if (computed != nullptr)
22868 *computed = false;
22869
21ae7a4d
JK
22870 i = 0;
22871 stacki = 0;
22872 stack[stacki] = 0;
22873 stack[++stacki] = 0;
22874
22875 while (i < size)
22876 {
22877 op = data[i++];
22878 switch (op)
22879 {
22880 case DW_OP_lit0:
22881 case DW_OP_lit1:
22882 case DW_OP_lit2:
22883 case DW_OP_lit3:
22884 case DW_OP_lit4:
22885 case DW_OP_lit5:
22886 case DW_OP_lit6:
22887 case DW_OP_lit7:
22888 case DW_OP_lit8:
22889 case DW_OP_lit9:
22890 case DW_OP_lit10:
22891 case DW_OP_lit11:
22892 case DW_OP_lit12:
22893 case DW_OP_lit13:
22894 case DW_OP_lit14:
22895 case DW_OP_lit15:
22896 case DW_OP_lit16:
22897 case DW_OP_lit17:
22898 case DW_OP_lit18:
22899 case DW_OP_lit19:
22900 case DW_OP_lit20:
22901 case DW_OP_lit21:
22902 case DW_OP_lit22:
22903 case DW_OP_lit23:
22904 case DW_OP_lit24:
22905 case DW_OP_lit25:
22906 case DW_OP_lit26:
22907 case DW_OP_lit27:
22908 case DW_OP_lit28:
22909 case DW_OP_lit29:
22910 case DW_OP_lit30:
22911 case DW_OP_lit31:
22912 stack[++stacki] = op - DW_OP_lit0;
22913 break;
f1bea926 22914
21ae7a4d
JK
22915 case DW_OP_reg0:
22916 case DW_OP_reg1:
22917 case DW_OP_reg2:
22918 case DW_OP_reg3:
22919 case DW_OP_reg4:
22920 case DW_OP_reg5:
22921 case DW_OP_reg6:
22922 case DW_OP_reg7:
22923 case DW_OP_reg8:
22924 case DW_OP_reg9:
22925 case DW_OP_reg10:
22926 case DW_OP_reg11:
22927 case DW_OP_reg12:
22928 case DW_OP_reg13:
22929 case DW_OP_reg14:
22930 case DW_OP_reg15:
22931 case DW_OP_reg16:
22932 case DW_OP_reg17:
22933 case DW_OP_reg18:
22934 case DW_OP_reg19:
22935 case DW_OP_reg20:
22936 case DW_OP_reg21:
22937 case DW_OP_reg22:
22938 case DW_OP_reg23:
22939 case DW_OP_reg24:
22940 case DW_OP_reg25:
22941 case DW_OP_reg26:
22942 case DW_OP_reg27:
22943 case DW_OP_reg28:
22944 case DW_OP_reg29:
22945 case DW_OP_reg30:
22946 case DW_OP_reg31:
22947 stack[++stacki] = op - DW_OP_reg0;
22948 if (i < size)
7d79de9a
TT
22949 {
22950 if (computed == nullptr)
22951 dwarf2_complex_location_expr_complaint ();
22952 else
22953 return 0;
22954 }
21ae7a4d 22955 break;
c906108c 22956
21ae7a4d
JK
22957 case DW_OP_regx:
22958 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22959 i += bytes_read;
22960 stack[++stacki] = unsnd;
22961 if (i < size)
7d79de9a
TT
22962 {
22963 if (computed == nullptr)
22964 dwarf2_complex_location_expr_complaint ();
22965 else
22966 return 0;
22967 }
21ae7a4d 22968 break;
c906108c 22969
21ae7a4d 22970 case DW_OP_addr:
c8a7a66f
TT
22971 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22972 &bytes_read);
21ae7a4d
JK
22973 i += bytes_read;
22974 break;
d53d4ac5 22975
21ae7a4d
JK
22976 case DW_OP_const1u:
22977 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22978 i += 1;
22979 break;
22980
22981 case DW_OP_const1s:
22982 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22983 i += 1;
22984 break;
22985
22986 case DW_OP_const2u:
22987 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22988 i += 2;
22989 break;
22990
22991 case DW_OP_const2s:
22992 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22993 i += 2;
22994 break;
d53d4ac5 22995
21ae7a4d
JK
22996 case DW_OP_const4u:
22997 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22998 i += 4;
22999 break;
23000
23001 case DW_OP_const4s:
23002 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23003 i += 4;
23004 break;
23005
585861ea
JK
23006 case DW_OP_const8u:
23007 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23008 i += 8;
23009 break;
23010
21ae7a4d
JK
23011 case DW_OP_constu:
23012 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23013 &bytes_read);
23014 i += bytes_read;
23015 break;
23016
23017 case DW_OP_consts:
23018 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23019 i += bytes_read;
23020 break;
23021
23022 case DW_OP_dup:
23023 stack[stacki + 1] = stack[stacki];
23024 stacki++;
23025 break;
23026
23027 case DW_OP_plus:
23028 stack[stacki - 1] += stack[stacki];
23029 stacki--;
23030 break;
23031
23032 case DW_OP_plus_uconst:
23033 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23034 &bytes_read);
23035 i += bytes_read;
23036 break;
23037
23038 case DW_OP_minus:
23039 stack[stacki - 1] -= stack[stacki];
23040 stacki--;
23041 break;
23042
23043 case DW_OP_deref:
23044 /* If we're not the last op, then we definitely can't encode
23045 this using GDB's address_class enum. This is valid for partial
23046 global symbols, although the variable's address will be bogus
23047 in the psymtab. */
23048 if (i < size)
7d79de9a
TT
23049 {
23050 if (computed == nullptr)
23051 dwarf2_complex_location_expr_complaint ();
23052 else
23053 return 0;
23054 }
21ae7a4d
JK
23055 break;
23056
23057 case DW_OP_GNU_push_tls_address:
4aa4e28b 23058 case DW_OP_form_tls_address:
21ae7a4d
JK
23059 /* The top of the stack has the offset from the beginning
23060 of the thread control block at which the variable is located. */
23061 /* Nothing should follow this operator, so the top of stack would
23062 be returned. */
23063 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23064 address will be bogus in the psymtab. Make it always at least
23065 non-zero to not look as a variable garbage collected by linker
23066 which have DW_OP_addr 0. */
21ae7a4d 23067 if (i < size)
7d79de9a
TT
23068 {
23069 if (computed == nullptr)
23070 dwarf2_complex_location_expr_complaint ();
23071 else
23072 return 0;
23073 }
585861ea 23074 stack[stacki]++;
21ae7a4d
JK
23075 break;
23076
23077 case DW_OP_GNU_uninit:
7d79de9a
TT
23078 if (computed != nullptr)
23079 return 0;
21ae7a4d
JK
23080 break;
23081
336d760d 23082 case DW_OP_addrx:
3019eac3 23083 case DW_OP_GNU_addr_index:
49f6c839 23084 case DW_OP_GNU_const_index:
3019eac3
DE
23085 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23086 &bytes_read);
23087 i += bytes_read;
23088 break;
23089
21ae7a4d 23090 default:
7d79de9a
TT
23091 if (computed == nullptr)
23092 {
23093 const char *name = get_DW_OP_name (op);
21ae7a4d 23094
7d79de9a
TT
23095 if (name)
23096 complaint (_("unsupported stack op: '%s'"),
23097 name);
23098 else
23099 complaint (_("unsupported stack op: '%02x'"),
23100 op);
23101 }
21ae7a4d
JK
23102
23103 return (stack[stacki]);
d53d4ac5 23104 }
3c6e0cb3 23105
21ae7a4d
JK
23106 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23107 outside of the allocated space. Also enforce minimum>0. */
23108 if (stacki >= ARRAY_SIZE (stack) - 1)
23109 {
7d79de9a
TT
23110 if (computed == nullptr)
23111 complaint (_("location description stack overflow"));
21ae7a4d
JK
23112 return 0;
23113 }
23114
23115 if (stacki <= 0)
23116 {
7d79de9a
TT
23117 if (computed == nullptr)
23118 complaint (_("location description stack underflow"));
21ae7a4d
JK
23119 return 0;
23120 }
23121 }
7d79de9a
TT
23122
23123 if (computed != nullptr)
23124 *computed = true;
21ae7a4d 23125 return (stack[stacki]);
c906108c
SS
23126}
23127
23128/* memory allocation interface */
23129
c906108c 23130static struct dwarf_block *
7b5a2f43 23131dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23132{
8d749320 23133 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23134}
23135
c906108c 23136static struct die_info *
b60c80d6 23137dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23138{
23139 struct die_info *die;
b60c80d6
DJ
23140 size_t size = sizeof (struct die_info);
23141
23142 if (num_attrs > 1)
23143 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23144
b60c80d6 23145 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23146 memset (die, 0, sizeof (struct die_info));
23147 return (die);
23148}
2e276125
JB
23149
23150\f
a036ba48 23151
c90ec28a 23152/* Macro support. */
cf2c3c16 23153
9eac9650
TT
23154/* An overload of dwarf_decode_macros that finds the correct section
23155 and ensures it is read in before calling the other overload. */
23156
23157static void
23158dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23159 int section_is_gnu)
23160{
5e22e966 23161 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23162 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23163 const struct line_header *lh = cu->line_header;
9eac9650
TT
23164 unsigned int offset_size = cu->header.offset_size;
23165 struct dwarf2_section_info *section;
23166 const char *section_name;
23167
23168 if (cu->dwo_unit != nullptr)
23169 {
23170 if (section_is_gnu)
23171 {
23172 section = &cu->dwo_unit->dwo_file->sections.macro;
23173 section_name = ".debug_macro.dwo";
23174 }
23175 else
23176 {
23177 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23178 section_name = ".debug_macinfo.dwo";
23179 }
23180 }
23181 else
23182 {
23183 if (section_is_gnu)
23184 {
5989a64e 23185 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23186 section_name = ".debug_macro";
23187 }
23188 else
23189 {
5989a64e 23190 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23191 section_name = ".debug_macinfo";
23192 }
23193 }
23194
23195 section->read (objfile);
23196 if (section->buffer == nullptr)
23197 {
23198 complaint (_("missing %s section"), section_name);
23199 return;
23200 }
23201
23202 buildsym_compunit *builder = cu->get_builder ();
23203
23204 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23205 offset_size, offset, section_is_gnu);
23206}
23207
3019eac3
DE
23208/* Return the .debug_loc section to use for CU.
23209 For DWO files use .debug_loc.dwo. */
23210
23211static struct dwarf2_section_info *
23212cu_debug_loc_section (struct dwarf2_cu *cu)
23213{
5e22e966 23214 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23215
3019eac3 23216 if (cu->dwo_unit)
43988095
JK
23217 {
23218 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23219
43988095
JK
23220 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23221 }
5989a64e
SM
23222 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23223 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23224}
23225
8cf6f0b1
TT
23226/* A helper function that fills in a dwarf2_loclist_baton. */
23227
23228static void
23229fill_in_loclist_baton (struct dwarf2_cu *cu,
23230 struct dwarf2_loclist_baton *baton,
ff39bb5e 23231 const struct attribute *attr)
8cf6f0b1 23232{
5e22e966 23233 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23234 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23235
96b79293 23236 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23237
a50264ba 23238 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23239 baton->per_cu = cu->per_cu;
23240 gdb_assert (baton->per_cu);
23241 /* We don't know how long the location list is, but make sure we
23242 don't run off the edge of the section. */
3019eac3
DE
23243 baton->size = section->size - DW_UNSND (attr);
23244 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23245 if (cu->base_address.has_value ())
23246 baton->base_address = *cu->base_address;
23247 else
23248 baton->base_address = 0;
f664829e 23249 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23250}
23251
4c2df51b 23252static void
ff39bb5e 23253dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23254 struct dwarf2_cu *cu, int is_block)
4c2df51b 23255{
5e22e966 23256 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23257 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23258 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23259
cd6c91b4 23260 if (attr->form_is_section_offset ()
3019eac3 23261 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23262 the section. If so, fall through to the complaint in the
23263 other branch. */
2c7d5afc 23264 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23265 {
0d53c4c4 23266 struct dwarf2_loclist_baton *baton;
4c2df51b 23267
8d749320 23268 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23269
8cf6f0b1 23270 fill_in_loclist_baton (cu, baton, attr);
be391dca 23271
2b24b6e4 23272 if (!cu->base_address.has_value ())
b98664d3 23273 complaint (_("Location list used without "
3e43a32a 23274 "specifying the CU base address."));
4c2df51b 23275
f1e6e072
TT
23276 SYMBOL_ACLASS_INDEX (sym) = (is_block
23277 ? dwarf2_loclist_block_index
23278 : dwarf2_loclist_index);
0d53c4c4
DJ
23279 SYMBOL_LOCATION_BATON (sym) = baton;
23280 }
23281 else
23282 {
23283 struct dwarf2_locexpr_baton *baton;
23284
8d749320 23285 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23286 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23287 baton->per_cu = cu->per_cu;
23288 gdb_assert (baton->per_cu);
0d53c4c4 23289
4fc6c0d5 23290 if (attr->form_is_block ())
0d53c4c4
DJ
23291 {
23292 /* Note that we're just copying the block's data pointer
23293 here, not the actual data. We're still pointing into the
6502dd73
DJ
23294 info_buffer for SYM's objfile; right now we never release
23295 that buffer, but when we do clean up properly this may
23296 need to change. */
0d53c4c4
DJ
23297 baton->size = DW_BLOCK (attr)->size;
23298 baton->data = DW_BLOCK (attr)->data;
23299 }
23300 else
23301 {
23302 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23303 sym->natural_name ());
0d53c4c4 23304 baton->size = 0;
0d53c4c4 23305 }
6e70227d 23306
f1e6e072
TT
23307 SYMBOL_ACLASS_INDEX (sym) = (is_block
23308 ? dwarf2_locexpr_block_index
23309 : dwarf2_locexpr_index);
0d53c4c4
DJ
23310 SYMBOL_LOCATION_BATON (sym) = baton;
23311 }
4c2df51b 23312}
6502dd73 23313
09ba997f 23314/* See read.h. */
ae0d2f24
UW
23315
23316struct objfile *
09ba997f 23317dwarf2_per_cu_data::objfile () const
ae0d2f24 23318{
09ba997f 23319 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23320
23321 /* Return the master objfile, so that we can report and look up the
23322 correct file containing this variable. */
23323 if (objfile->separate_debug_objfile_backlink)
23324 objfile = objfile->separate_debug_objfile_backlink;
23325
23326 return objfile;
23327}
23328
96408a79
SA
23329/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23330 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23331 CU_HEADERP first. */
23332
23333static const struct comp_unit_head *
23334per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23335 const struct dwarf2_per_cu_data *per_cu)
96408a79 23336{
d521ce57 23337 const gdb_byte *info_ptr;
96408a79
SA
23338
23339 if (per_cu->cu)
23340 return &per_cu->cu->header;
23341
9c541725 23342 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23343
23344 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23345 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23346 rcuh_kind::COMPILE);
96408a79
SA
23347
23348 return cu_headerp;
23349}
23350
09ba997f 23351/* See read.h. */
ae0d2f24 23352
98714339 23353int
09ba997f 23354dwarf2_per_cu_data::addr_size () const
ae0d2f24 23355{
96408a79
SA
23356 struct comp_unit_head cu_header_local;
23357 const struct comp_unit_head *cu_headerp;
c471e790 23358
09ba997f 23359 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23360
23361 return cu_headerp->addr_size;
ae0d2f24
UW
23362}
23363
09ba997f 23364/* See read.h. */
9eae7c52
TT
23365
23366int
09ba997f 23367dwarf2_per_cu_data::offset_size () const
9eae7c52 23368{
96408a79
SA
23369 struct comp_unit_head cu_header_local;
23370 const struct comp_unit_head *cu_headerp;
9c6c53f7 23371
09ba997f 23372 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23373
23374 return cu_headerp->offset_size;
23375}
23376
09ba997f 23377/* See read.h. */
96408a79
SA
23378
23379int
09ba997f 23380dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23381{
23382 struct comp_unit_head cu_header_local;
23383 const struct comp_unit_head *cu_headerp;
23384
09ba997f 23385 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23386
23387 if (cu_headerp->version == 2)
23388 return cu_headerp->addr_size;
23389 else
23390 return cu_headerp->offset_size;
181cebd4
JK
23391}
23392
09ba997f 23393/* See read.h. */
9aa1f1e3
TT
23394
23395CORE_ADDR
09ba997f 23396dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23397{
09ba997f
TT
23398 struct objfile *objfile = dwarf2_per_objfile->objfile;
23399
23400 return objfile->text_section_offset ();
9aa1f1e3
TT
23401}
23402
09ba997f
TT
23403/* See read.h. */
23404
23405struct type *
293e7e51 23406dwarf2_cu::addr_type () const
9a49df9d 23407{
293e7e51 23408 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23409 struct type *void_type = objfile_type (objfile)->builtin_void;
23410 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23411 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23412
23413 if (TYPE_LENGTH (addr_type) == addr_size)
23414 return addr_type;
23415
09ba997f 23416 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23417 return addr_type;
23418}
23419
22b6cd70
TT
23420/* A helper function for dwarf2_find_containing_comp_unit that returns
23421 the index of the result, and that searches a vector. It will
23422 return a result even if the offset in question does not actually
23423 occur in any CU. This is separate so that it can be unit
23424 tested. */
ae038cb0 23425
22b6cd70
TT
23426static int
23427dwarf2_find_containing_comp_unit
23428 (sect_offset sect_off,
23429 unsigned int offset_in_dwz,
23430 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23431{
ae038cb0
DJ
23432 int low, high;
23433
ae038cb0 23434 low = 0;
22b6cd70 23435 high = all_comp_units.size () - 1;
ae038cb0
DJ
23436 while (high > low)
23437 {
36586728 23438 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23439 int mid = low + (high - low) / 2;
9a619af0 23440
22b6cd70 23441 mid_cu = all_comp_units[mid];
36586728 23442 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23443 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23444 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23445 high = mid;
23446 else
23447 low = mid + 1;
23448 }
23449 gdb_assert (low == high);
22b6cd70
TT
23450 return low;
23451}
23452
23453/* Locate the .debug_info compilation unit from CU's objfile which contains
23454 the DIE at OFFSET. Raises an error on failure. */
23455
23456static struct dwarf2_per_cu_data *
23457dwarf2_find_containing_comp_unit (sect_offset sect_off,
23458 unsigned int offset_in_dwz,
23459 struct dwarf2_per_objfile *dwarf2_per_objfile)
23460{
23461 int low
23462 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23463 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23464 struct dwarf2_per_cu_data *this_cu
5989a64e 23465 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23466
45b8ae0c 23467 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23468 {
36586728 23469 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23470 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23471 "offset %s [in module %s]"),
23472 sect_offset_str (sect_off),
ed2dc618 23473 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23474
5989a64e 23475 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23476 <= sect_off);
5989a64e 23477 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23478 }
23479 else
23480 {
5989a64e 23481 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23482 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23483 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23484 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23485 return this_cu;
23486 }
23487}
23488
22b6cd70
TT
23489#if GDB_SELF_TEST
23490
23491namespace selftests {
23492namespace find_containing_comp_unit {
23493
23494static void
23495run_test ()
23496{
23497 struct dwarf2_per_cu_data one {};
23498 struct dwarf2_per_cu_data two {};
23499 struct dwarf2_per_cu_data three {};
23500 struct dwarf2_per_cu_data four {};
23501
23502 one.length = 5;
23503 two.sect_off = sect_offset (one.length);
23504 two.length = 7;
23505
23506 three.length = 5;
23507 three.is_dwz = 1;
23508 four.sect_off = sect_offset (three.length);
23509 four.length = 7;
23510 four.is_dwz = 1;
23511
23512 std::vector<dwarf2_per_cu_data *> units;
23513 units.push_back (&one);
23514 units.push_back (&two);
23515 units.push_back (&three);
23516 units.push_back (&four);
23517
23518 int result;
23519
23520 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23521 SELF_CHECK (units[result] == &one);
23522 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23523 SELF_CHECK (units[result] == &one);
23524 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23525 SELF_CHECK (units[result] == &two);
23526
23527 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23528 SELF_CHECK (units[result] == &three);
23529 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23530 SELF_CHECK (units[result] == &three);
23531 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23532 SELF_CHECK (units[result] == &four);
23533}
23534
23535}
23536}
23537
23538#endif /* GDB_SELF_TEST */
23539
9e021579 23540/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23541
9e021579
SM
23542dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23543 dwarf2_per_objfile *per_objfile)
23544 : per_cu (per_cu),
23545 per_objfile (per_objfile),
9068261f
AB
23546 mark (false),
23547 has_loclist (false),
23548 checked_producer (false),
23549 producer_is_gxx_lt_4_6 (false),
23550 producer_is_gcc_lt_4_3 (false),
eb77c9df 23551 producer_is_icc (false),
9068261f 23552 producer_is_icc_lt_14 (false),
c258c396 23553 producer_is_codewarrior (false),
9068261f 23554 processing_has_namespace_info (false)
93311388 23555{
fcd3b13d
SM
23556 per_cu->cu = this;
23557}
23558
23559/* Destroy a dwarf2_cu. */
23560
23561dwarf2_cu::~dwarf2_cu ()
23562{
23563 per_cu->cu = NULL;
9816fde3
JK
23564}
23565
23566/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23567
23568static void
95554aad
TT
23569prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23570 enum language pretend_language)
9816fde3
JK
23571{
23572 struct attribute *attr;
23573
23574 /* Set the language we're debugging. */
23575 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23576 if (attr != nullptr)
9816fde3
JK
23577 set_cu_language (DW_UNSND (attr), cu);
23578 else
9cded63f 23579 {
95554aad 23580 cu->language = pretend_language;
9cded63f
TT
23581 cu->language_defn = language_def (cu->language);
23582 }
dee91e82 23583
7d45c7c3 23584 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23585}
23586
ae038cb0
DJ
23587/* Increase the age counter on each cached compilation unit, and free
23588 any that are too old. */
23589
23590static void
ed2dc618 23591age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23592{
23593 struct dwarf2_per_cu_data *per_cu, **last_chain;
23594
5989a64e
SM
23595 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23596 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23597 while (per_cu != NULL)
23598 {
23599 per_cu->cu->last_used ++;
b4f54984 23600 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23601 dwarf2_mark (per_cu->cu);
23602 per_cu = per_cu->cu->read_in_chain;
23603 }
23604
5989a64e
SM
23605 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23606 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23607 while (per_cu != NULL)
23608 {
23609 struct dwarf2_per_cu_data *next_cu;
23610
23611 next_cu = per_cu->cu->read_in_chain;
23612
23613 if (!per_cu->cu->mark)
23614 {
fcd3b13d 23615 delete per_cu->cu;
ae038cb0
DJ
23616 *last_chain = next_cu;
23617 }
23618 else
23619 last_chain = &per_cu->cu->read_in_chain;
23620
23621 per_cu = next_cu;
23622 }
23623}
23624
23625/* Remove a single compilation unit from the cache. */
23626
23627static void
dee91e82 23628free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23629{
23630 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23631 struct dwarf2_per_objfile *dwarf2_per_objfile
23632 = target_per_cu->dwarf2_per_objfile;
ae038cb0 23633
5989a64e
SM
23634 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23635 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23636 while (per_cu != NULL)
23637 {
23638 struct dwarf2_per_cu_data *next_cu;
23639
23640 next_cu = per_cu->cu->read_in_chain;
23641
dee91e82 23642 if (per_cu == target_per_cu)
ae038cb0 23643 {
fcd3b13d 23644 delete per_cu->cu;
dee91e82 23645 per_cu->cu = NULL;
ae038cb0
DJ
23646 *last_chain = next_cu;
23647 break;
23648 }
23649 else
23650 last_chain = &per_cu->cu->read_in_chain;
23651
23652 per_cu = next_cu;
23653 }
23654}
23655
dee91e82
DE
23656/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23657 We store these in a hash table separate from the DIEs, and preserve them
23658 when the DIEs are flushed out of cache.
23659
23660 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23661 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23662 or the type may come from a DWO file. Furthermore, while it's more logical
23663 to use per_cu->section+offset, with Fission the section with the data is in
23664 the DWO file but we don't know that section at the point we need it.
23665 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23666 because we can enter the lookup routine, get_die_type_at_offset, from
23667 outside this file, and thus won't necessarily have PER_CU->cu.
23668 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23669
dee91e82 23670struct dwarf2_per_cu_offset_and_type
1c379e20 23671{
dee91e82 23672 const struct dwarf2_per_cu_data *per_cu;
9c541725 23673 sect_offset sect_off;
1c379e20
DJ
23674 struct type *type;
23675};
23676
dee91e82 23677/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23678
23679static hashval_t
dee91e82 23680per_cu_offset_and_type_hash (const void *item)
1c379e20 23681{
9a3c8263
SM
23682 const struct dwarf2_per_cu_offset_and_type *ofs
23683 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23684
9c541725 23685 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23686}
23687
dee91e82 23688/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23689
23690static int
dee91e82 23691per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23692{
9a3c8263
SM
23693 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23694 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23695 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23696 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23697
dee91e82 23698 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23699 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23700}
23701
23702/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23703 table if necessary. For convenience, return TYPE.
23704
23705 The DIEs reading must have careful ordering to:
85102364 23706 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23707 reading current DIE.
23708 * Not trying to dereference contents of still incompletely read in types
23709 while reading in other DIEs.
23710 * Enable referencing still incompletely read in types just by a pointer to
23711 the type without accessing its fields.
23712
23713 Therefore caller should follow these rules:
23714 * Try to fetch any prerequisite types we may need to build this DIE type
23715 before building the type and calling set_die_type.
e71ec853 23716 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23717 possible before fetching more types to complete the current type.
23718 * Make the type as complete as possible before fetching more types. */
1c379e20 23719
f792889a 23720static struct type *
1c379e20
DJ
23721set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23722{
5e22e966 23723 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23724 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23725 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23726 struct attribute *attr;
23727 struct dynamic_prop prop;
1c379e20 23728
b4ba55a1
JB
23729 /* For Ada types, make sure that the gnat-specific data is always
23730 initialized (if not already set). There are a few types where
23731 we should not be doing so, because the type-specific area is
23732 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23733 where the type-specific area is used to store the floatformat).
23734 But this is not a problem, because the gnat-specific information
23735 is actually not needed for these types. */
23736 if (need_gnat_info (cu)
78134374
SM
23737 && type->code () != TYPE_CODE_FUNC
23738 && type->code () != TYPE_CODE_FLT
23739 && type->code () != TYPE_CODE_METHODPTR
23740 && type->code () != TYPE_CODE_MEMBERPTR
23741 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23742 && !HAVE_GNAT_AUX_INFO (type))
23743 INIT_GNAT_SPECIFIC (type);
23744
3f2f83dd
KB
23745 /* Read DW_AT_allocated and set in type. */
23746 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23747 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23748 {
293e7e51 23749 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23750 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23751 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23752 }
23753 else if (attr != NULL)
23754 {
b98664d3 23755 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23756 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23757 sect_offset_str (die->sect_off));
3f2f83dd
KB
23758 }
23759
23760 /* Read DW_AT_associated and set in type. */
23761 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23762 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23763 {
293e7e51 23764 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23765 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23766 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23767 }
23768 else if (attr != NULL)
23769 {
b98664d3 23770 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23771 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23772 sect_offset_str (die->sect_off));
3f2f83dd
KB
23773 }
23774
3cdcd0ce
JB
23775 /* Read DW_AT_data_location and set in type. */
23776 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23777 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23778 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23779
ae090bdb
SM
23780 if (dwarf2_per_objfile->die_type_hash == NULL)
23781 dwarf2_per_objfile->die_type_hash
0335378b
TT
23782 = htab_up (htab_create_alloc (127,
23783 per_cu_offset_and_type_hash,
23784 per_cu_offset_and_type_eq,
23785 NULL, xcalloc, xfree));
1c379e20 23786
dee91e82 23787 ofs.per_cu = cu->per_cu;
9c541725 23788 ofs.sect_off = die->sect_off;
1c379e20 23789 ofs.type = type;
dee91e82 23790 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23791 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23792 if (*slot)
b98664d3 23793 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23794 sect_offset_str (die->sect_off));
8d749320
SM
23795 *slot = XOBNEW (&objfile->objfile_obstack,
23796 struct dwarf2_per_cu_offset_and_type);
1c379e20 23797 **slot = ofs;
f792889a 23798 return type;
1c379e20
DJ
23799}
23800
9c541725 23801/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23802 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23803
23804static struct type *
9c541725 23805get_die_type_at_offset (sect_offset sect_off,
673bfd45 23806 struct dwarf2_per_cu_data *per_cu)
1c379e20 23807{
dee91e82 23808 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23809 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23810
ae090bdb 23811 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23812 return NULL;
1c379e20 23813
dee91e82 23814 ofs.per_cu = per_cu;
9c541725 23815 ofs.sect_off = sect_off;
9a3c8263 23816 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23817 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23818 if (slot)
23819 return slot->type;
23820 else
23821 return NULL;
23822}
23823
02142a6c 23824/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23825 or return NULL if DIE does not have a saved type. */
23826
23827static struct type *
23828get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23829{
9c541725 23830 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23831}
23832
10b3939b
DJ
23833/* Add a dependence relationship from CU to REF_PER_CU. */
23834
23835static void
23836dwarf2_add_dependence (struct dwarf2_cu *cu,
23837 struct dwarf2_per_cu_data *ref_per_cu)
23838{
23839 void **slot;
23840
23841 if (cu->dependencies == NULL)
23842 cu->dependencies
23843 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23844 NULL, &cu->comp_unit_obstack,
23845 hashtab_obstack_allocate,
23846 dummy_obstack_deallocate);
23847
23848 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23849 if (*slot == NULL)
23850 *slot = ref_per_cu;
23851}
1c379e20 23852
f504f079
DE
23853/* Subroutine of dwarf2_mark to pass to htab_traverse.
23854 Set the mark field in every compilation unit in the
ae038cb0
DJ
23855 cache that we must keep because we are keeping CU. */
23856
10b3939b
DJ
23857static int
23858dwarf2_mark_helper (void **slot, void *data)
23859{
23860 struct dwarf2_per_cu_data *per_cu;
23861
23862 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23863
23864 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23865 reading of the chain. As such dependencies remain valid it is not much
23866 useful to track and undo them during QUIT cleanups. */
23867 if (per_cu->cu == NULL)
23868 return 1;
23869
10b3939b
DJ
23870 if (per_cu->cu->mark)
23871 return 1;
9068261f 23872 per_cu->cu->mark = true;
10b3939b
DJ
23873
23874 if (per_cu->cu->dependencies != NULL)
23875 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23876
23877 return 1;
23878}
23879
f504f079
DE
23880/* Set the mark field in CU and in every other compilation unit in the
23881 cache that we must keep because we are keeping CU. */
23882
ae038cb0
DJ
23883static void
23884dwarf2_mark (struct dwarf2_cu *cu)
23885{
23886 if (cu->mark)
23887 return;
9068261f 23888 cu->mark = true;
10b3939b
DJ
23889 if (cu->dependencies != NULL)
23890 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23891}
23892
23893static void
23894dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23895{
23896 while (per_cu)
23897 {
9068261f 23898 per_cu->cu->mark = false;
ae038cb0
DJ
23899 per_cu = per_cu->cu->read_in_chain;
23900 }
72bf9492
DJ
23901}
23902
72bf9492
DJ
23903/* Trivial hash function for partial_die_info: the hash value of a DIE
23904 is its offset in .debug_info for this objfile. */
23905
23906static hashval_t
23907partial_die_hash (const void *item)
23908{
9a3c8263
SM
23909 const struct partial_die_info *part_die
23910 = (const struct partial_die_info *) item;
9a619af0 23911
9c541725 23912 return to_underlying (part_die->sect_off);
72bf9492
DJ
23913}
23914
23915/* Trivial comparison function for partial_die_info structures: two DIEs
23916 are equal if they have the same offset. */
23917
23918static int
23919partial_die_eq (const void *item_lhs, const void *item_rhs)
23920{
9a3c8263
SM
23921 const struct partial_die_info *part_die_lhs
23922 = (const struct partial_die_info *) item_lhs;
23923 const struct partial_die_info *part_die_rhs
23924 = (const struct partial_die_info *) item_rhs;
9a619af0 23925
9c541725 23926 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23927}
23928
3c3bb058
AB
23929struct cmd_list_element *set_dwarf_cmdlist;
23930struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23931
9291a0cd 23932static void
cd4fb1b2
SM
23933show_check_physname (struct ui_file *file, int from_tty,
23934 struct cmd_list_element *c, const char *value)
9291a0cd 23935{
cd4fb1b2
SM
23936 fprintf_filtered (file,
23937 _("Whether to check \"physname\" is %s.\n"),
23938 value);
9291a0cd
TT
23939}
23940
6c265988 23941void _initialize_dwarf2_read ();
cd4fb1b2 23942void
6c265988 23943_initialize_dwarf2_read ()
9291a0cd 23944{
0743fc83 23945 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23946Set DWARF specific variables.\n\
590042fc 23947Configure DWARF variables such as the cache size."),
0743fc83
TT
23948 &set_dwarf_cmdlist, "maintenance set dwarf ",
23949 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23950
0743fc83 23951 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23952Show DWARF specific variables.\n\
23953Show DWARF variables such as the cache size."),
0743fc83
TT
23954 &show_dwarf_cmdlist, "maintenance show dwarf ",
23955 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23956
cd4fb1b2
SM
23957 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23958 &dwarf_max_cache_age, _("\
23959Set the upper bound on the age of cached DWARF compilation units."), _("\
23960Show the upper bound on the age of cached DWARF compilation units."), _("\
23961A higher limit means that cached compilation units will be stored\n\
23962in memory longer, and more total memory will be used. Zero disables\n\
23963caching, which can slow down startup."),
23964 NULL,
23965 show_dwarf_max_cache_age,
23966 &set_dwarf_cmdlist,
23967 &show_dwarf_cmdlist);
156942c7 23968
cd4fb1b2
SM
23969 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23970Set debugging of the DWARF reader."), _("\
23971Show debugging of the DWARF reader."), _("\
23972When enabled (non-zero), debugging messages are printed during DWARF\n\
23973reading and symtab expansion. A value of 1 (one) provides basic\n\
23974information. A value greater than 1 provides more verbose information."),
23975 NULL,
23976 NULL,
23977 &setdebuglist, &showdebuglist);
9291a0cd 23978
cd4fb1b2
SM
23979 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23980Set debugging of the DWARF DIE reader."), _("\
23981Show debugging of the DWARF DIE reader."), _("\
23982When enabled (non-zero), DIEs are dumped after they are read in.\n\
23983The value is the maximum depth to print."),
23984 NULL,
23985 NULL,
23986 &setdebuglist, &showdebuglist);
9291a0cd 23987
cd4fb1b2
SM
23988 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23989Set debugging of the dwarf line reader."), _("\
23990Show debugging of the dwarf line reader."), _("\
23991When enabled (non-zero), line number entries are dumped as they are read in.\n\
23992A value of 1 (one) provides basic information.\n\
23993A value greater than 1 provides more verbose information."),
23994 NULL,
23995 NULL,
23996 &setdebuglist, &showdebuglist);
437afbb8 23997
cd4fb1b2
SM
23998 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23999Set cross-checking of \"physname\" code against demangler."), _("\
24000Show cross-checking of \"physname\" code against demangler."), _("\
24001When enabled, GDB's internal \"physname\" code is checked against\n\
24002the demangler."),
24003 NULL, show_check_physname,
24004 &setdebuglist, &showdebuglist);
900e11f9 24005
e615022a
DE
24006 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24007 no_class, &use_deprecated_index_sections, _("\
24008Set whether to use deprecated gdb_index sections."), _("\
24009Show whether to use deprecated gdb_index sections."), _("\
24010When enabled, deprecated .gdb_index sections are used anyway.\n\
24011Normally they are ignored either because of a missing feature or\n\
24012performance issue.\n\
24013Warning: This option must be enabled before gdb reads the file."),
24014 NULL,
24015 NULL,
24016 &setlist, &showlist);
24017
f1e6e072
TT
24018 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24019 &dwarf2_locexpr_funcs);
24020 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24021 &dwarf2_loclist_funcs);
24022
24023 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24024 &dwarf2_block_frame_base_locexpr_funcs);
24025 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24026 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24027
24028#if GDB_SELF_TEST
24029 selftests::register_test ("dw2_expand_symtabs_matching",
24030 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24031 selftests::register_test ("dwarf2_find_containing_comp_unit",
24032 selftests::find_containing_comp_unit::run_test);
c62446b1 24033#endif
6502dd73 24034}
This page took 7.148998 seconds and 4 git commands to generate.