Remove sibling_die
[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"
4de283e4
TT
43#include "bfd.h"
44#include "elf-bfd.h"
45#include "symtab.h"
46#include "gdbtypes.h"
47#include "objfiles.h"
d55e5aa6 48#include "dwarf2.h"
4de283e4
TT
49#include "buildsym.h"
50#include "demangle.h"
51#include "gdb-demangle.h"
4de283e4 52#include "filenames.h" /* for DOSish file names */
4de283e4
TT
53#include "language.h"
54#include "complaints.h"
82ca8957
TT
55#include "dwarf2/expr.h"
56#include "dwarf2/loc.h"
4de283e4
TT
57#include "cp-support.h"
58#include "hashtab.h"
59#include "command.h"
d55e5aa6 60#include "gdbcmd.h"
4de283e4
TT
61#include "block.h"
62#include "addrmap.h"
63#include "typeprint.h"
64#include "psympriv.h"
4de283e4 65#include "c-lang.h"
d55e5aa6 66#include "go-lang.h"
4de283e4
TT
67#include "valprint.h"
68#include "gdbcore.h" /* for gnutarget */
69#include "gdb/gdb-index.h"
4de283e4
TT
70#include "gdb_bfd.h"
71#include "f-lang.h"
72#include "source.h"
4de283e4 73#include "build-id.h"
d55e5aa6 74#include "namespace.h"
268a13a5
TT
75#include "gdbsupport/function-view.h"
76#include "gdbsupport/gdb_optional.h"
77#include "gdbsupport/underlying.h"
268a13a5 78#include "gdbsupport/hash_enum.h"
4de283e4 79#include "filename-seen-cache.h"
b32b108a 80#include "producer.h"
4de283e4 81#include <fcntl.h>
4de283e4 82#include <algorithm>
4de283e4 83#include <unordered_map>
268a13a5 84#include "gdbsupport/selftest.h"
c9317f21 85#include "rust-lang.h"
268a13a5 86#include "gdbsupport/pathstuff.h"
edd45eb0 87#include "count-one-bits.h"
0d79cdc4 88#include "debuginfod-support.h"
437afbb8 89
73be47f5
DE
90/* When == 1, print basic high level tracing messages.
91 When > 1, be more verbose.
b4f54984
DE
92 This is in contrast to the low level DIE reading of dwarf_die_debug. */
93static unsigned int dwarf_read_debug = 0;
45cfd468 94
d97bc12b 95/* When non-zero, dump DIEs after they are read in. */
b4f54984 96static unsigned int dwarf_die_debug = 0;
d97bc12b 97
27e0867f 98/* When non-zero, dump line number entries as they are read in. */
8fdd972c 99unsigned int dwarf_line_debug = 0;
27e0867f 100
491144b5
CB
101/* When true, cross-check physname against demangler. */
102static bool check_physname = false;
900e11f9 103
491144b5
CB
104/* When true, do not reject deprecated .gdb_index sections. */
105static bool use_deprecated_index_sections = false;
481860b3 106
5bfd760d 107static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 108
f1e6e072
TT
109/* The "aclass" indices for various kinds of computed DWARF symbols. */
110
111static int dwarf2_locexpr_index;
112static int dwarf2_loclist_index;
113static int dwarf2_locexpr_block_index;
114static int dwarf2_loclist_block_index;
115
3f563c84
PA
116/* An index into a (C++) symbol name component in a symbol name as
117 recorded in the mapped_index's symbol table. For each C++ symbol
118 in the symbol table, we record one entry for the start of each
119 component in the symbol in a table of name components, and then
120 sort the table, in order to be able to binary search symbol names,
121 ignoring leading namespaces, both completion and regular look up.
122 For example, for symbol "A::B::C", we'll have an entry that points
123 to "A::B::C", another that points to "B::C", and another for "C".
124 Note that function symbols in GDB index have no parameter
125 information, just the function/method names. You can convert a
126 name_component to a "const char *" using the
127 'mapped_index::symbol_name_at(offset_type)' method. */
128
129struct name_component
130{
131 /* Offset in the symbol name where the component starts. Stored as
132 a (32-bit) offset instead of a pointer to save memory and improve
133 locality on 64-bit architectures. */
134 offset_type name_offset;
135
136 /* The symbol's index in the symbol and constant pool tables of a
137 mapped_index. */
138 offset_type idx;
139};
140
44ed8f3e
PA
141/* Base class containing bits shared by both .gdb_index and
142 .debug_name indexes. */
143
144struct mapped_index_base
145{
22ca247e
TT
146 mapped_index_base () = default;
147 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
148
44ed8f3e
PA
149 /* The name_component table (a sorted vector). See name_component's
150 description above. */
151 std::vector<name_component> name_components;
152
153 /* How NAME_COMPONENTS is sorted. */
154 enum case_sensitivity name_components_casing;
155
156 /* Return the number of names in the symbol table. */
157 virtual size_t symbol_name_count () const = 0;
158
159 /* Get the name of the symbol at IDX in the symbol table. */
160 virtual const char *symbol_name_at (offset_type idx) const = 0;
161
162 /* Return whether the name at IDX in the symbol table should be
163 ignored. */
164 virtual bool symbol_name_slot_invalid (offset_type idx) const
165 {
166 return false;
167 }
168
169 /* Build the symbol name component sorted vector, if we haven't
170 yet. */
171 void build_name_components ();
172
173 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
174 possible matches for LN_NO_PARAMS in the name component
175 vector. */
176 std::pair<std::vector<name_component>::const_iterator,
177 std::vector<name_component>::const_iterator>
3b00ef10
TT
178 find_name_components_bounds (const lookup_name_info &ln_no_params,
179 enum language lang) const;
44ed8f3e
PA
180
181 /* Prevent deleting/destroying via a base class pointer. */
182protected:
183 ~mapped_index_base() = default;
184};
185
9291a0cd
TT
186/* A description of the mapped index. The file format is described in
187 a comment by the code that writes the index. */
fc898b42 188struct mapped_index final : public mapped_index_base
9291a0cd 189{
f00a2de2
PA
190 /* A slot/bucket in the symbol table hash. */
191 struct symbol_table_slot
192 {
193 const offset_type name;
194 const offset_type vec;
195 };
196
559a7a62 197 /* Index data format version. */
3063847f 198 int version = 0;
559a7a62 199
f00a2de2
PA
200 /* The address table data. */
201 gdb::array_view<const gdb_byte> address_table;
b11b1f88 202
3876f04e 203 /* The symbol table, implemented as a hash table. */
f00a2de2 204 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 205
9291a0cd 206 /* A pointer to the constant pool. */
3063847f 207 const char *constant_pool = nullptr;
3f563c84 208
44ed8f3e
PA
209 bool symbol_name_slot_invalid (offset_type idx) const override
210 {
211 const auto &bucket = this->symbol_table[idx];
9ab08412 212 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 213 }
5c58de74 214
3f563c84
PA
215 /* Convenience method to get at the name of the symbol at IDX in the
216 symbol table. */
44ed8f3e 217 const char *symbol_name_at (offset_type idx) const override
f00a2de2 218 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 219
44ed8f3e
PA
220 size_t symbol_name_count () const override
221 { return this->symbol_table.size (); }
9291a0cd
TT
222};
223
927aa2e7
JK
224/* A description of the mapped .debug_names.
225 Uninitialized map has CU_COUNT 0. */
fc898b42 226struct mapped_debug_names final : public mapped_index_base
927aa2e7 227{
ed2dc618
SM
228 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
229 : dwarf2_per_objfile (dwarf2_per_objfile_)
230 {}
231
232 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
233 bfd_endian dwarf5_byte_order;
234 bool dwarf5_is_dwarf64;
235 bool augmentation_is_gdb;
236 uint8_t offset_size;
237 uint32_t cu_count = 0;
238 uint32_t tu_count, bucket_count, name_count;
239 const gdb_byte *cu_table_reordered, *tu_table_reordered;
240 const uint32_t *bucket_table_reordered, *hash_table_reordered;
241 const gdb_byte *name_table_string_offs_reordered;
242 const gdb_byte *name_table_entry_offs_reordered;
243 const gdb_byte *entry_pool;
244
245 struct index_val
246 {
247 ULONGEST dwarf_tag;
248 struct attr
249 {
250 /* Attribute name DW_IDX_*. */
251 ULONGEST dw_idx;
252
253 /* Attribute form DW_FORM_*. */
254 ULONGEST form;
255
256 /* Value if FORM is DW_FORM_implicit_const. */
257 LONGEST implicit_const;
258 };
259 std::vector<attr> attr_vec;
260 };
261
262 std::unordered_map<ULONGEST, index_val> abbrev_map;
263
264 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
265
266 /* Implementation of the mapped_index_base virtual interface, for
267 the name_components cache. */
268
269 const char *symbol_name_at (offset_type idx) const override
270 { return namei_to_name (idx); }
271
272 size_t symbol_name_count () const override
273 { return this->name_count; }
927aa2e7
JK
274};
275
cd4fb1b2 276/* See dwarf2read.h. */
ed2dc618 277
cd4fb1b2 278dwarf2_per_objfile *
ed2dc618
SM
279get_dwarf2_per_objfile (struct objfile *objfile)
280{
5bfd760d 281 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 282}
c906108c 283
251d32d9 284/* Default names of the debugging sections. */
c906108c 285
233a11ab
CS
286/* Note that if the debugging section has been compressed, it might
287 have a name like .zdebug_info. */
288
9cdd5dbd
DE
289static const struct dwarf2_debug_sections dwarf2_elf_names =
290{
251d32d9
TG
291 { ".debug_info", ".zdebug_info" },
292 { ".debug_abbrev", ".zdebug_abbrev" },
293 { ".debug_line", ".zdebug_line" },
294 { ".debug_loc", ".zdebug_loc" },
43988095 295 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 296 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 297 { ".debug_macro", ".zdebug_macro" },
251d32d9 298 { ".debug_str", ".zdebug_str" },
18a8505e 299 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 300 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 301 { ".debug_ranges", ".zdebug_ranges" },
43988095 302 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 303 { ".debug_types", ".zdebug_types" },
3019eac3 304 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
305 { ".debug_frame", ".zdebug_frame" },
306 { ".eh_frame", NULL },
24d3216f 307 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
308 { ".debug_names", ".zdebug_names" },
309 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 310 23
251d32d9 311};
c906108c 312
80626a55 313/* List of DWO/DWP sections. */
3019eac3 314
80626a55 315static const struct dwop_section_names
3019eac3
DE
316{
317 struct dwarf2_section_names abbrev_dwo;
318 struct dwarf2_section_names info_dwo;
319 struct dwarf2_section_names line_dwo;
320 struct dwarf2_section_names loc_dwo;
43988095 321 struct dwarf2_section_names loclists_dwo;
09262596
DE
322 struct dwarf2_section_names macinfo_dwo;
323 struct dwarf2_section_names macro_dwo;
3019eac3
DE
324 struct dwarf2_section_names str_dwo;
325 struct dwarf2_section_names str_offsets_dwo;
326 struct dwarf2_section_names types_dwo;
80626a55
DE
327 struct dwarf2_section_names cu_index;
328 struct dwarf2_section_names tu_index;
3019eac3 329}
80626a55 330dwop_section_names =
3019eac3
DE
331{
332 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
333 { ".debug_info.dwo", ".zdebug_info.dwo" },
334 { ".debug_line.dwo", ".zdebug_line.dwo" },
335 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 336 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
337 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
338 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
339 { ".debug_str.dwo", ".zdebug_str.dwo" },
340 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
341 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
342 { ".debug_cu_index", ".zdebug_cu_index" },
343 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
344};
345
c906108c
SS
346/* local data types */
347
3da10d80
KS
348/* Type used for delaying computation of method physnames.
349 See comments for compute_delayed_physnames. */
350struct delayed_method_info
351{
352 /* The type to which the method is attached, i.e., its parent class. */
353 struct type *type;
354
355 /* The index of the method in the type's function fieldlists. */
356 int fnfield_index;
357
358 /* The index of the method in the fieldlist. */
359 int index;
360
361 /* The name of the DIE. */
362 const char *name;
363
364 /* The DIE associated with this method. */
365 struct die_info *die;
366};
367
e7c27a73
DJ
368/* Internal state when decoding a particular compilation unit. */
369struct dwarf2_cu
370{
fcd3b13d
SM
371 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
372 ~dwarf2_cu ();
373
374 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
375
c24bdb02
KS
376 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
377 Create the set of symtabs used by this TU, or if this TU is sharing
378 symtabs with another TU and the symtabs have already been created
379 then restore those symtabs in the line header.
380 We don't need the pc/line-number mapping for type units. */
381 void setup_type_unit_groups (struct die_info *die);
382
383 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
384 buildsym_compunit constructor. */
385 struct compunit_symtab *start_symtab (const char *name,
386 const char *comp_dir,
387 CORE_ADDR low_pc);
388
389 /* Reset the builder. */
390 void reset_builder () { m_builder.reset (); }
391
d00adf39 392 /* The header of the compilation unit. */
fcd3b13d 393 struct comp_unit_head header {};
e142c38c 394
d00adf39 395 /* Base address of this compilation unit. */
2b24b6e4 396 gdb::optional<CORE_ADDR> base_address;
d00adf39 397
e142c38c 398 /* The language we are debugging. */
fcd3b13d
SM
399 enum language language = language_unknown;
400 const struct language_defn *language_defn = nullptr;
e142c38c 401
fcd3b13d 402 const char *producer = nullptr;
b0f35d58 403
c24bdb02 404private:
804d2729
TT
405 /* The symtab builder for this CU. This is only non-NULL when full
406 symbols are being read. */
c24bdb02 407 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 408
c24bdb02 409public:
e142c38c
DJ
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
fcd3b13d 419 struct pending **list_in_scope = nullptr;
e142c38c 420
b64f50a1
JK
421 /* Hash table holding all the loaded partial DIEs
422 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 423 htab_t partial_dies = nullptr;
72bf9492
DJ
424
425 /* Storage for things with the same lifetime as this read-in compilation
426 unit, including partial DIEs. */
fcd3b13d 427 auto_obstack comp_unit_obstack;
72bf9492 428
ae038cb0
DJ
429 /* When multiple dwarf2_cu structures are living in memory, this field
430 chains them all together, so that they can be released efficiently.
431 We will probably also want a generation counter so that most-recently-used
432 compilation units are cached... */
fcd3b13d 433 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 434
69d751e3 435 /* Backlink to our per_cu entry. */
ae038cb0
DJ
436 struct dwarf2_per_cu_data *per_cu;
437
438 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 439 int last_used = 0;
ae038cb0 440
b64f50a1
JK
441 /* A hash table of DIE cu_offset for following references with
442 die_info->offset.sect_off as hash. */
fcd3b13d 443 htab_t die_hash = nullptr;
10b3939b
DJ
444
445 /* Full DIEs if read in. */
fcd3b13d 446 struct die_info *dies = nullptr;
10b3939b
DJ
447
448 /* A set of pointers to dwarf2_per_cu_data objects for compilation
449 units referenced by this one. Only set during full symbol processing;
450 partial symbol tables do not have dependencies. */
fcd3b13d 451 htab_t dependencies = nullptr;
10b3939b 452
cb1df416 453 /* Header data from the line table, during full symbol processing. */
fcd3b13d 454 struct line_header *line_header = nullptr;
4c8aa72d
PA
455 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
456 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
457 this is the DW_TAG_compile_unit die for this CU. We'll hold on
458 to the line header as long as this DIE is being processed. See
459 process_die_scope. */
fcd3b13d 460 die_info *line_header_die_owner = nullptr;
cb1df416 461
3da10d80
KS
462 /* A list of methods which need to have physnames computed
463 after all type information has been read. */
c89b44cd 464 std::vector<delayed_method_info> method_list;
3da10d80 465
96408a79 466 /* To be copied to symtab->call_site_htab. */
fcd3b13d 467 htab_t call_site_htab = nullptr;
96408a79 468
034e5797
DE
469 /* Non-NULL if this CU came from a DWO file.
470 There is an invariant here that is important to remember:
471 Except for attributes copied from the top level DIE in the "main"
472 (or "stub") file in preparation for reading the DWO file
18a8505e 473 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
474 Either there isn't a DWO file (in which case this is NULL and the point
475 is moot), or there is and either we're not going to read it (in which
476 case this is NULL) or there is and we are reading it (in which case this
477 is non-NULL). */
fcd3b13d 478 struct dwo_unit *dwo_unit = nullptr;
3019eac3 479
18a8505e 480 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 481 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 482 gdb::optional<ULONGEST> addr_base;
3019eac3 483
18a8505e 484 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 485 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 486 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
487 be used without needing to know whether DWO files are in use or not.
488 N.B. This does not apply to DW_AT_ranges appearing in
489 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
490 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 491 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 492 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 493 ULONGEST ranges_base = 0;
2e3cf129 494
c9317f21
TT
495 /* When reading debug info generated by older versions of rustc, we
496 have to rewrite some union types to be struct types with a
497 variant part. This rewriting must be done after the CU is fully
498 read in, because otherwise at the point of rewriting some struct
499 type might not have been fully processed. So, we keep a list of
500 all such types here and process them after expansion. */
501 std::vector<struct type *> rust_unions;
502
18a8505e
AT
503 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
504 files, the value is implicitly zero. For DWARF 5 version DWO files, the
505 value is often implicit and is the size of the header of
506 .debug_str_offsets section (8 or 4, depending on the address size). */
507 gdb::optional<ULONGEST> str_offsets_base;
508
ae038cb0 509 /* Mark used when releasing cached dies. */
9068261f 510 bool mark : 1;
ae038cb0 511
8be455d7
JK
512 /* This CU references .debug_loc. See the symtab->locations_valid field.
513 This test is imperfect as there may exist optimized debug code not using
514 any location list and still facing inlining issues if handled as
515 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 516 bool has_loclist : 1;
ba919b58 517
9068261f 518 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
519 if all the producer_is_* fields are valid. This information is cached
520 because profiling CU expansion showed excessive time spent in
521 producer_is_gxx_lt_4_6. */
9068261f
AB
522 bool checked_producer : 1;
523 bool producer_is_gxx_lt_4_6 : 1;
524 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 525 bool producer_is_icc : 1;
9068261f 526 bool producer_is_icc_lt_14 : 1;
c258c396 527 bool producer_is_codewarrior : 1;
4d4ec4e5 528
9068261f 529 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
530 debugging info for C++ namespaces. GCC 3.3.x did not produce
531 this information, but later versions do. */
532
9068261f 533 bool processing_has_namespace_info : 1;
d590ff25
YQ
534
535 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
536
537 /* If this CU was inherited by another CU (via specification,
538 abstract_origin, etc), this is the ancestor CU. */
539 dwarf2_cu *ancestor;
540
541 /* Get the buildsym_compunit for this CU. */
542 buildsym_compunit *get_builder ()
543 {
544 /* If this CU has a builder associated with it, use that. */
545 if (m_builder != nullptr)
546 return m_builder.get ();
547
548 /* Otherwise, search ancestors for a valid builder. */
549 if (ancestor != nullptr)
550 return ancestor->get_builder ();
551
552 return nullptr;
553 }
e7c27a73
DJ
554};
555
094b34ac
DE
556/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
557 This includes type_unit_group and quick_file_names. */
558
559struct stmt_list_hash
560{
561 /* The DWO unit this table is from or NULL if there is none. */
562 struct dwo_unit *dwo_unit;
563
564 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 565 sect_offset line_sect_off;
094b34ac
DE
566};
567
f4dc4d17
DE
568/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
569 an object of this type. */
570
571struct type_unit_group
572{
0186c6a7 573 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
574 To simplify things we create an artificial CU that "includes" all the
575 type units using this stmt_list so that the rest of the code still has
197400e8 576 a "per_cu" handle on the symtab. */
094b34ac
DE
577 struct dwarf2_per_cu_data per_cu;
578
0186c6a7
DE
579 /* The TUs that share this DW_AT_stmt_list entry.
580 This is added to while parsing type units to build partial symtabs,
581 and is deleted afterwards and not used again. */
a8b3b8e9 582 std::vector<signatured_type *> *tus;
f4dc4d17 583
43f3e411 584 /* The compunit symtab.
094b34ac 585 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
586 so we create an essentially anonymous symtab as the compunit symtab. */
587 struct compunit_symtab *compunit_symtab;
f4dc4d17 588
094b34ac
DE
589 /* The data used to construct the hash key. */
590 struct stmt_list_hash hash;
f4dc4d17 591
f4dc4d17
DE
592 /* The symbol tables for this TU (obtained from the files listed in
593 DW_AT_stmt_list).
594 WARNING: The order of entries here must match the order of entries
595 in the line header. After the first TU using this type_unit_group, the
596 line header for the subsequent TUs is recreated from this. This is done
597 because we need to use the same symtabs for each TU using the same
598 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
599 there's no guarantee the line header doesn't have duplicate entries. */
600 struct symtab **symtabs;
601};
602
73869dc2 603/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
604
605struct dwo_sections
606{
607 struct dwarf2_section_info abbrev;
3019eac3
DE
608 struct dwarf2_section_info line;
609 struct dwarf2_section_info loc;
43988095 610 struct dwarf2_section_info loclists;
09262596
DE
611 struct dwarf2_section_info macinfo;
612 struct dwarf2_section_info macro;
3019eac3
DE
613 struct dwarf2_section_info str;
614 struct dwarf2_section_info str_offsets;
80626a55
DE
615 /* In the case of a virtual DWO file, these two are unused. */
616 struct dwarf2_section_info info;
fd5866f6 617 std::vector<dwarf2_section_info> types;
3019eac3
DE
618};
619
c88ee1f0 620/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
621
622struct dwo_unit
623{
624 /* Backlink to the containing struct dwo_file. */
625 struct dwo_file *dwo_file;
626
627 /* The "id" that distinguishes this CU/TU.
628 .debug_info calls this "dwo_id", .debug_types calls this "signature".
629 Since signatures came first, we stick with it for consistency. */
630 ULONGEST signature;
631
632 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 633 struct dwarf2_section_info *section;
3019eac3 634
9c541725
PA
635 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
636 sect_offset sect_off;
3019eac3
DE
637 unsigned int length;
638
639 /* For types, offset in the type's DIE of the type defined by this TU. */
640 cu_offset type_offset_in_tu;
641};
642
73869dc2
DE
643/* include/dwarf2.h defines the DWP section codes.
644 It defines a max value but it doesn't define a min value, which we
645 use for error checking, so provide one. */
646
647enum dwp_v2_section_ids
648{
649 DW_SECT_MIN = 1
650};
651
80626a55 652/* Data for one DWO file.
57d63ce2
DE
653
654 This includes virtual DWO files (a virtual DWO file is a DWO file as it
655 appears in a DWP file). DWP files don't really have DWO files per se -
656 comdat folding of types "loses" the DWO file they came from, and from
657 a high level view DWP files appear to contain a mass of random types.
658 However, to maintain consistency with the non-DWP case we pretend DWP
659 files contain virtual DWO files, and we assign each TU with one virtual
660 DWO file (generally based on the line and abbrev section offsets -
661 a heuristic that seems to work in practice). */
3019eac3
DE
662
663struct dwo_file
664{
51ac9db5
SM
665 dwo_file () = default;
666 DISABLE_COPY_AND_ASSIGN (dwo_file);
667
18a8505e 668 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
669 For virtual DWO files the name is constructed from the section offsets
670 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
671 from related CU+TUs. */
51ac9db5 672 const char *dwo_name = nullptr;
0ac5b59e
DE
673
674 /* The DW_AT_comp_dir attribute. */
51ac9db5 675 const char *comp_dir = nullptr;
3019eac3 676
80626a55
DE
677 /* The bfd, when the file is open. Otherwise this is NULL.
678 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 679 gdb_bfd_ref_ptr dbfd;
3019eac3 680
73869dc2
DE
681 /* The sections that make up this DWO file.
682 Remember that for virtual DWO files in DWP V2, these are virtual
683 sections (for lack of a better name). */
51ac9db5 684 struct dwo_sections sections {};
3019eac3 685
33c5cd75
DB
686 /* The CUs in the file.
687 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
688 an extension to handle LLVM's Link Time Optimization output (where
689 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 690 htab_up cus;
3019eac3
DE
691
692 /* Table of TUs in the file.
693 Each element is a struct dwo_unit. */
b0b6a987 694 htab_up tus;
3019eac3
DE
695};
696
80626a55
DE
697/* These sections are what may appear in a DWP file. */
698
699struct dwp_sections
700{
73869dc2 701 /* These are used by both DWP version 1 and 2. */
80626a55
DE
702 struct dwarf2_section_info str;
703 struct dwarf2_section_info cu_index;
704 struct dwarf2_section_info tu_index;
73869dc2
DE
705
706 /* These are only used by DWP version 2 files.
707 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
708 sections are referenced by section number, and are not recorded here.
709 In DWP version 2 there is at most one copy of all these sections, each
710 section being (effectively) comprised of the concatenation of all of the
711 individual sections that exist in the version 1 format.
712 To keep the code simple we treat each of these concatenated pieces as a
713 section itself (a virtual section?). */
714 struct dwarf2_section_info abbrev;
715 struct dwarf2_section_info info;
716 struct dwarf2_section_info line;
717 struct dwarf2_section_info loc;
718 struct dwarf2_section_info macinfo;
719 struct dwarf2_section_info macro;
720 struct dwarf2_section_info str_offsets;
721 struct dwarf2_section_info types;
80626a55
DE
722};
723
73869dc2
DE
724/* These sections are what may appear in a virtual DWO file in DWP version 1.
725 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 726
73869dc2 727struct virtual_v1_dwo_sections
80626a55
DE
728{
729 struct dwarf2_section_info abbrev;
730 struct dwarf2_section_info line;
731 struct dwarf2_section_info loc;
732 struct dwarf2_section_info macinfo;
733 struct dwarf2_section_info macro;
734 struct dwarf2_section_info str_offsets;
735 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 736 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
737 struct dwarf2_section_info info_or_types;
738};
739
73869dc2
DE
740/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
741 In version 2, the sections of the DWO files are concatenated together
742 and stored in one section of that name. Thus each ELF section contains
743 several "virtual" sections. */
744
745struct virtual_v2_dwo_sections
746{
747 bfd_size_type abbrev_offset;
748 bfd_size_type abbrev_size;
749
750 bfd_size_type line_offset;
751 bfd_size_type line_size;
752
753 bfd_size_type loc_offset;
754 bfd_size_type loc_size;
755
756 bfd_size_type macinfo_offset;
757 bfd_size_type macinfo_size;
758
759 bfd_size_type macro_offset;
760 bfd_size_type macro_size;
761
762 bfd_size_type str_offsets_offset;
763 bfd_size_type str_offsets_size;
764
765 /* Each DWP hash table entry records one CU or one TU.
766 That is recorded here, and copied to dwo_unit.section. */
767 bfd_size_type info_or_types_offset;
768 bfd_size_type info_or_types_size;
769};
770
80626a55
DE
771/* Contents of DWP hash tables. */
772
773struct dwp_hash_table
774{
73869dc2 775 uint32_t version, nr_columns;
80626a55 776 uint32_t nr_units, nr_slots;
73869dc2
DE
777 const gdb_byte *hash_table, *unit_table;
778 union
779 {
780 struct
781 {
782 const gdb_byte *indices;
783 } v1;
784 struct
785 {
786 /* This is indexed by column number and gives the id of the section
787 in that column. */
788#define MAX_NR_V2_DWO_SECTIONS \
789 (1 /* .debug_info or .debug_types */ \
790 + 1 /* .debug_abbrev */ \
791 + 1 /* .debug_line */ \
792 + 1 /* .debug_loc */ \
793 + 1 /* .debug_str_offsets */ \
794 + 1 /* .debug_macro or .debug_macinfo */)
795 int section_ids[MAX_NR_V2_DWO_SECTIONS];
796 const gdb_byte *offsets;
797 const gdb_byte *sizes;
798 } v2;
799 } section_pool;
80626a55
DE
800};
801
802/* Data for one DWP file. */
803
804struct dwp_file
805{
400174b1
TT
806 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
807 : name (name_),
808 dbfd (std::move (abfd))
809 {
810 }
811
80626a55
DE
812 /* Name of the file. */
813 const char *name;
814
73869dc2 815 /* File format version. */
400174b1 816 int version = 0;
73869dc2 817
93417882 818 /* The bfd. */
400174b1 819 gdb_bfd_ref_ptr dbfd;
80626a55
DE
820
821 /* Section info for this file. */
400174b1 822 struct dwp_sections sections {};
80626a55 823
57d63ce2 824 /* Table of CUs in the file. */
400174b1 825 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
826
827 /* Table of TUs in the file. */
400174b1 828 const struct dwp_hash_table *tus = nullptr;
80626a55 829
19ac8c2e 830 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
831 htab_up loaded_cus;
832 htab_up loaded_tus;
80626a55 833
73869dc2
DE
834 /* Table to map ELF section numbers to their sections.
835 This is only needed for the DWP V1 file format. */
400174b1
TT
836 unsigned int num_sections = 0;
837 asection **elf_sections = nullptr;
80626a55
DE
838};
839
0963b4bd
MS
840/* Struct used to pass misc. parameters to read_die_and_children, et
841 al. which are used for both .debug_info and .debug_types dies.
842 All parameters here are unchanging for the life of the call. This
dee91e82 843 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
844
845struct die_reader_specs
846{
a32a8923 847 /* The bfd of die_section. */
93311388
DE
848 bfd* abfd;
849
850 /* The CU of the DIE we are parsing. */
851 struct dwarf2_cu *cu;
852
80626a55 853 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
854 struct dwo_file *dwo_file;
855
dee91e82 856 /* The section the die comes from.
3019eac3 857 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
858 struct dwarf2_section_info *die_section;
859
860 /* die_section->buffer. */
d521ce57 861 const gdb_byte *buffer;
f664829e
DE
862
863 /* The end of the buffer. */
864 const gdb_byte *buffer_end;
a2ce51a0 865
685af9cd
TT
866 /* The abbreviation table to use when reading the DIEs. */
867 struct abbrev_table *abbrev_table;
93311388
DE
868};
869
c0ab21c2
TT
870/* A subclass of die_reader_specs that holds storage and has complex
871 constructor and destructor behavior. */
872
873class cutu_reader : public die_reader_specs
874{
875public:
876
877 cutu_reader (struct dwarf2_per_cu_data *this_cu,
878 struct abbrev_table *abbrev_table,
6751ebae 879 int use_existing_cu,
c0ab21c2
TT
880 bool skip_partial);
881
882 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
883 struct dwarf2_cu *parent_cu = nullptr,
884 struct dwo_file *dwo_file = nullptr);
885
c0ab21c2
TT
886 DISABLE_COPY_AND_ASSIGN (cutu_reader);
887
888 const gdb_byte *info_ptr = nullptr;
889 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
890 bool dummy_p = false;
891
6751ebae
TT
892 /* Release the new CU, putting it on the chain. This cannot be done
893 for dummy CUs. */
894 void keep ();
895
c0ab21c2
TT
896private:
897 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 898 int use_existing_cu);
c0ab21c2
TT
899
900 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
901 std::unique_ptr<dwarf2_cu> m_new_cu;
902
903 /* The ordinary abbreviation table. */
904 abbrev_table_up m_abbrev_table_holder;
905
906 /* The DWO abbreviation table. */
907 abbrev_table_up m_dwo_abbrev_table;
908};
dee91e82 909
c906108c 910/* When we construct a partial symbol table entry we only
0963b4bd 911 need this much information. */
6f06d47b 912struct partial_die_info : public allocate_on_obstack
c906108c 913 {
6f06d47b
YQ
914 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
915
916 /* Disable assign but still keep copy ctor, which is needed
917 load_partial_dies. */
918 partial_die_info& operator=(const partial_die_info& rhs) = delete;
919
52356b79
YQ
920 /* Adjust the partial die before generating a symbol for it. This
921 function may set the is_external flag or change the DIE's
922 name. */
923 void fixup (struct dwarf2_cu *cu);
924
48fbe735
YQ
925 /* Read a minimal amount of information into the minimal die
926 structure. */
927 const gdb_byte *read (const struct die_reader_specs *reader,
928 const struct abbrev_info &abbrev,
929 const gdb_byte *info_ptr);
930
72bf9492 931 /* Offset of this DIE. */
6f06d47b 932 const sect_offset sect_off;
72bf9492
DJ
933
934 /* DWARF-2 tag for this DIE. */
6f06d47b 935 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 936
72bf9492 937 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
938 const unsigned int has_children : 1;
939
72bf9492
DJ
940 unsigned int is_external : 1;
941 unsigned int is_declaration : 1;
942 unsigned int has_type : 1;
943 unsigned int has_specification : 1;
944 unsigned int has_pc_info : 1;
481860b3 945 unsigned int may_be_inlined : 1;
72bf9492 946
0c1b455e
TT
947 /* This DIE has been marked DW_AT_main_subprogram. */
948 unsigned int main_subprogram : 1;
949
72bf9492
DJ
950 /* Flag set if the SCOPE field of this structure has been
951 computed. */
952 unsigned int scope_set : 1;
953
fa4028e9
JB
954 /* Flag set if the DIE has a byte_size attribute. */
955 unsigned int has_byte_size : 1;
956
ff908ebf
AW
957 /* Flag set if the DIE has a DW_AT_const_value attribute. */
958 unsigned int has_const_value : 1;
959
98bfdba5
PA
960 /* Flag set if any of the DIE's children are template arguments. */
961 unsigned int has_template_arguments : 1;
962
52356b79 963 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
964 unsigned int fixup_called : 1;
965
36586728
TT
966 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
967 unsigned int is_dwz : 1;
968
969 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
970 unsigned int spec_is_dwz : 1;
971
72bf9492 972 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 973 sometimes a default name for unnamed DIEs. */
6f06d47b 974 const char *name = nullptr;
72bf9492 975
abc72ce4 976 /* The linkage name, if present. */
6f06d47b 977 const char *linkage_name = nullptr;
abc72ce4 978
72bf9492
DJ
979 /* The scope to prepend to our children. This is generally
980 allocated on the comp_unit_obstack, so will disappear
981 when this compilation unit leaves the cache. */
6f06d47b 982 const char *scope = nullptr;
72bf9492 983
95554aad
TT
984 /* Some data associated with the partial DIE. The tag determines
985 which field is live. */
986 union
987 {
988 /* The location description associated with this DIE, if any. */
989 struct dwarf_block *locdesc;
990 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 991 sect_offset sect_off;
6f06d47b 992 } d {};
72bf9492
DJ
993
994 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
995 CORE_ADDR lowpc = 0;
996 CORE_ADDR highpc = 0;
72bf9492 997
93311388 998 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 999 DW_AT_sibling, if any. */
48fbe735
YQ
1000 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1001 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1002 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1003
1004 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1005 DW_AT_specification (or DW_AT_abstract_origin or
1006 DW_AT_extension). */
6f06d47b 1007 sect_offset spec_offset {};
72bf9492
DJ
1008
1009 /* Pointers to this DIE's parent, first child, and next sibling,
1010 if any. */
6f06d47b
YQ
1011 struct partial_die_info *die_parent = nullptr;
1012 struct partial_die_info *die_child = nullptr;
1013 struct partial_die_info *die_sibling = nullptr;
1014
1015 friend struct partial_die_info *
1016 dwarf2_cu::find_partial_die (sect_offset sect_off);
1017
1018 private:
1019 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1020 partial_die_info (sect_offset sect_off)
1021 : partial_die_info (sect_off, DW_TAG_padding, 0)
1022 {
1023 }
1024
1025 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1026 int has_children_)
1027 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1028 {
1029 is_external = 0;
1030 is_declaration = 0;
1031 has_type = 0;
1032 has_specification = 0;
1033 has_pc_info = 0;
1034 may_be_inlined = 0;
1035 main_subprogram = 0;
1036 scope_set = 0;
1037 has_byte_size = 0;
1038 has_const_value = 0;
1039 has_template_arguments = 0;
1040 fixup_called = 0;
1041 is_dwz = 0;
1042 spec_is_dwz = 0;
1043 }
c906108c
SS
1044 };
1045
c906108c
SS
1046/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1047 but this would require a corresponding change in unpack_field_as_long
1048 and friends. */
1049static int bits_per_byte = 8;
1050
2ddeaf8a
TT
1051/* When reading a variant or variant part, we track a bit more
1052 information about the field, and store it in an object of this
1053 type. */
1054
1055struct variant_field
1056{
1057 /* If we see a DW_TAG_variant, then this will be the discriminant
1058 value. */
1059 ULONGEST discriminant_value;
1060 /* If we see a DW_TAG_variant, then this will be set if this is the
1061 default branch. */
1062 bool default_branch;
1063 /* While reading a DW_TAG_variant_part, this will be set if this
1064 field is the discriminant. */
1065 bool is_discriminant;
1066};
1067
52059ffd
TT
1068struct nextfield
1069{
be2daae6
TT
1070 int accessibility = 0;
1071 int virtuality = 0;
2ddeaf8a 1072 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1073 struct variant_field variant {};
1074 struct field field {};
52059ffd
TT
1075};
1076
1077struct fnfieldlist
1078{
be2daae6
TT
1079 const char *name = nullptr;
1080 std::vector<struct fn_field> fnfields;
52059ffd
TT
1081};
1082
c906108c
SS
1083/* The routines that read and process dies for a C struct or C++ class
1084 pass lists of data member fields and lists of member function fields
1085 in an instance of a field_info structure, as defined below. */
1086struct field_info
c5aa993b 1087 {
0963b4bd 1088 /* List of data member and baseclasses fields. */
be2daae6
TT
1089 std::vector<struct nextfield> fields;
1090 std::vector<struct nextfield> baseclasses;
c906108c 1091
85102364 1092 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1093 int non_public_fields = 0;
c906108c 1094
c5aa993b
JM
1095 /* Member function fieldlist array, contains name of possibly overloaded
1096 member function, number of overloaded member functions and a pointer
1097 to the head of the member function field chain. */
be2daae6 1098 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1099
1100 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1101 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1102 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1103
1104 /* Nested types defined by this class and the number of elements in this
1105 list. */
be2daae6 1106 std::vector<struct decl_field> nested_types_list;
317f7127
TT
1107
1108 /* Return the total number of fields (including baseclasses). */
1109 int nfields () const
1110 {
1111 return fields.size () + baseclasses.size ();
1112 }
c5aa993b 1113 };
c906108c 1114
ae038cb0
DJ
1115/* Loaded secondary compilation units are kept in memory until they
1116 have not been referenced for the processing of this many
1117 compilation units. Set this to zero to disable caching. Cache
1118 sizes of up to at least twenty will improve startup time for
1119 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1120static int dwarf_max_cache_age = 5;
920d2a44 1121static void
b4f54984
DE
1122show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1123 struct cmd_list_element *c, const char *value)
920d2a44 1124{
3e43a32a 1125 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1126 "DWARF compilation units is %s.\n"),
920d2a44
AC
1127 value);
1128}
4390d890 1129\f
c906108c
SS
1130/* local function prototypes */
1131
918dd910
JK
1132static void dwarf2_find_base_address (struct die_info *die,
1133 struct dwarf2_cu *cu);
1134
891813be 1135static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1136 (struct dwarf2_per_cu_data *per_cu, const char *name);
1137
f1902523
JK
1138static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1139 const gdb_byte *info_ptr,
3e225074 1140 struct die_info *type_unit_die);
f1902523 1141
ed2dc618
SM
1142static void dwarf2_build_psymtabs_hard
1143 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1144
72bf9492
DJ
1145static void scan_partial_symbols (struct partial_die_info *,
1146 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1147 int, struct dwarf2_cu *);
c906108c 1148
72bf9492
DJ
1149static void add_partial_symbol (struct partial_die_info *,
1150 struct dwarf2_cu *);
63d06c5c 1151
72bf9492
DJ
1152static void add_partial_namespace (struct partial_die_info *pdi,
1153 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1154 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1155
5d7cb8df 1156static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1157 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1158 struct dwarf2_cu *cu);
1159
72bf9492
DJ
1160static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1161 struct dwarf2_cu *cu);
91c24f0a 1162
bc30ff58
JB
1163static void add_partial_subprogram (struct partial_die_info *pdi,
1164 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1165 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1166
d521ce57 1167static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1168
dee91e82 1169static struct partial_die_info *load_partial_dies
d521ce57 1170 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1171
fb816e8b
TV
1172/* A pair of partial_die_info and compilation unit. */
1173struct cu_partial_die_info
1174{
1175 /* The compilation unit of the partial_die_info. */
1176 struct dwarf2_cu *cu;
1177 /* A partial_die_info. */
1178 struct partial_die_info *pdi;
122cf0f2
AB
1179
1180 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1181 : cu (cu),
1182 pdi (pdi)
405feb71 1183 { /* Nothing. */ }
122cf0f2
AB
1184
1185private:
1186 cu_partial_die_info () = delete;
fb816e8b
TV
1187};
1188
122cf0f2
AB
1189static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1190 struct dwarf2_cu *);
72bf9492 1191
d521ce57
TT
1192static const gdb_byte *read_attribute (const struct die_reader_specs *,
1193 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1194 const gdb_byte *, bool *need_reprocess);
1195
1196static void read_attribute_reprocess (const struct die_reader_specs *reader,
1197 struct attribute *attr);
1198
1199static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1200
ed2dc618
SM
1201static sect_offset read_abbrev_offset
1202 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1203 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1204
ed2dc618
SM
1205static const char *read_indirect_string
1206 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1207 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1208
ed2dc618 1209static const char *read_indirect_string_at_offset
4f44ae6c 1210 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1211
d521ce57
TT
1212static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1213 const gdb_byte *,
3019eac3
DE
1214 unsigned int *);
1215
18a8505e
AT
1216static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1217 ULONGEST str_index);
1218
1219static const char *read_stub_str_index (struct dwarf2_cu *cu,
1220 ULONGEST str_index);
3019eac3 1221
e142c38c 1222static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1223
e142c38c
DJ
1224static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1225 struct dwarf2_cu *);
c906108c 1226
7d45c7c3
KB
1227static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1228 struct dwarf2_cu *cu);
1229
a084a2a6
AT
1230static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1231
05cf31d1
JB
1232static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1233 struct dwarf2_cu *cu);
1234
e142c38c 1235static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1236
e142c38c 1237static struct die_info *die_specification (struct die_info *die,
f2f0e013 1238 struct dwarf2_cu **);
63d06c5c 1239
9c541725 1240static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1241 struct dwarf2_cu *cu);
debd256d 1242
f3f5162e 1243static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1244 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1245 CORE_ADDR, int decode_mapping);
c906108c 1246
804d2729
TT
1247static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1248 const char *);
c906108c 1249
a14ed312 1250static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1251 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1252
ff39bb5e 1253static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1254 struct dwarf2_cu *);
c906108c 1255
ff39bb5e 1256static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1257 struct type *type,
1258 const char *name,
1259 struct obstack *obstack,
12df843f 1260 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1261 const gdb_byte **bytes,
98bfdba5 1262 struct dwarf2_locexpr_baton **baton);
2df3850c 1263
e7c27a73 1264static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1265
b4ba55a1
JB
1266static int need_gnat_info (struct dwarf2_cu *);
1267
3e43a32a
MS
1268static struct type *die_descriptive_type (struct die_info *,
1269 struct dwarf2_cu *);
b4ba55a1
JB
1270
1271static void set_descriptive_type (struct type *, struct die_info *,
1272 struct dwarf2_cu *);
1273
e7c27a73
DJ
1274static struct type *die_containing_type (struct die_info *,
1275 struct dwarf2_cu *);
c906108c 1276
ff39bb5e 1277static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1278 struct dwarf2_cu *);
c906108c 1279
f792889a 1280static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1281
673bfd45
DE
1282static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1283
0d5cff50 1284static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1285
6e70227d 1286static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1287 const char *suffix, int physname,
1288 struct dwarf2_cu *cu);
63d06c5c 1289
e7c27a73 1290static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1291
348e048f
DE
1292static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1293
e7c27a73 1294static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1295
e7c27a73 1296static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1297
96408a79
SA
1298static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1299
71a3c369
TT
1300static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1301
ff013f42 1302static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1303 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1304
3a2b436a 1305/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1306 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1307enum pc_bounds_kind
1308{
e385593e 1309 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1310 PC_BOUNDS_NOT_PRESENT,
1311
e385593e
JK
1312 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1313 were present but they do not form a valid range of PC addresses. */
1314 PC_BOUNDS_INVALID,
1315
3a2b436a
JK
1316 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1317 PC_BOUNDS_RANGES,
1318
1319 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1320 PC_BOUNDS_HIGH_LOW,
1321};
1322
1323static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1324 CORE_ADDR *, CORE_ADDR *,
1325 struct dwarf2_cu *,
891813be 1326 dwarf2_psymtab *);
c906108c 1327
fae299cd
DC
1328static void get_scope_pc_bounds (struct die_info *,
1329 CORE_ADDR *, CORE_ADDR *,
1330 struct dwarf2_cu *);
1331
801e3a5b
JB
1332static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1333 CORE_ADDR, struct dwarf2_cu *);
1334
a14ed312 1335static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1336 struct dwarf2_cu *);
c906108c 1337
a14ed312 1338static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1339 struct type *, struct dwarf2_cu *);
c906108c 1340
a14ed312 1341static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1342 struct die_info *, struct type *,
e7c27a73 1343 struct dwarf2_cu *);
c906108c 1344
a14ed312 1345static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1346 struct type *,
1347 struct dwarf2_cu *);
c906108c 1348
134d01f1 1349static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1350
e7c27a73 1351static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1352
e7c27a73 1353static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1354
5d7cb8df
JK
1355static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1356
804d2729 1357static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1358
27aa8d6a
SW
1359static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1360
74921315
KS
1361static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1362
f55ee35c
JK
1363static struct type *read_module_type (struct die_info *die,
1364 struct dwarf2_cu *cu);
1365
38d518c9 1366static const char *namespace_name (struct die_info *die,
e142c38c 1367 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1368
134d01f1 1369static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1370
e7c27a73 1371static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1372
6e70227d 1373static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1374 struct dwarf2_cu *);
1375
bf6af496 1376static struct die_info *read_die_and_siblings_1
d521ce57 1377 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1378 struct die_info *);
639d11d3 1379
dee91e82 1380static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1381 const gdb_byte *info_ptr,
1382 const gdb_byte **new_info_ptr,
639d11d3
DC
1383 struct die_info *parent);
1384
d521ce57
TT
1385static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1386 struct die_info **, const gdb_byte *,
3e225074 1387 int);
3019eac3 1388
d521ce57 1389static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1390 struct die_info **, const gdb_byte *);
93311388 1391
e7c27a73 1392static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1393
15d034d0 1394static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1395 struct objfile *);
71c25dea 1396
15d034d0 1397static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1398
15d034d0 1399static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1400 struct die_info *die,
1401 struct dwarf2_cu *cu);
1402
ca69b9e6
DE
1403static const char *dwarf2_physname (const char *name, struct die_info *die,
1404 struct dwarf2_cu *cu);
1405
e142c38c 1406static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1407 struct dwarf2_cu **);
9219021c 1408
f39c6ffd 1409static const char *dwarf_tag_name (unsigned int);
c906108c 1410
f39c6ffd 1411static const char *dwarf_attr_name (unsigned int);
c906108c 1412
f39c6ffd 1413static const char *dwarf_form_name (unsigned int);
c906108c 1414
a121b7c1 1415static const char *dwarf_bool_name (unsigned int);
c906108c 1416
f39c6ffd 1417static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1418
d97bc12b
DE
1419static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1420
1421static void dump_die_for_error (struct die_info *);
1422
1423static void dump_die_1 (struct ui_file *, int level, int max_level,
1424 struct die_info *);
c906108c 1425
d97bc12b 1426/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1427
51545339 1428static void store_in_ref_table (struct die_info *,
10b3939b 1429 struct dwarf2_cu *);
c906108c 1430
ff39bb5e 1431static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1432
ff39bb5e 1433static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1434
348e048f 1435static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1436 const struct attribute *,
348e048f
DE
1437 struct dwarf2_cu **);
1438
10b3939b 1439static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1440 const struct attribute *,
f2f0e013 1441 struct dwarf2_cu **);
c906108c 1442
348e048f 1443static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1444 const struct attribute *,
348e048f
DE
1445 struct dwarf2_cu **);
1446
ac9ec31b
DE
1447static struct type *get_signatured_type (struct die_info *, ULONGEST,
1448 struct dwarf2_cu *);
1449
1450static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1451 const struct attribute *,
ac9ec31b
DE
1452 struct dwarf2_cu *);
1453
e5fe5e75 1454static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1455
52dc124a 1456static void read_signatured_type (struct signatured_type *);
348e048f 1457
63e43d3a
PMR
1458static int attr_to_dynamic_prop (const struct attribute *attr,
1459 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1460 struct dynamic_prop *prop, struct type *type);
63e43d3a 1461
c906108c
SS
1462/* memory allocation interface */
1463
7b5a2f43 1464static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1465
b60c80d6 1466static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1467
43f3e411 1468static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1469
8cf6f0b1
TT
1470static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1471 struct dwarf2_loclist_baton *baton,
ff39bb5e 1472 const struct attribute *attr);
8cf6f0b1 1473
ff39bb5e 1474static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1475 struct symbol *sym,
f1e6e072
TT
1476 struct dwarf2_cu *cu,
1477 int is_block);
4c2df51b 1478
d521ce57
TT
1479static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1480 const gdb_byte *info_ptr,
1481 struct abbrev_info *abbrev);
4bb7a0a7 1482
72bf9492
DJ
1483static hashval_t partial_die_hash (const void *item);
1484
1485static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1486
ae038cb0 1487static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1488 (sect_offset sect_off, unsigned int offset_in_dwz,
1489 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1490
9816fde3 1491static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1492 struct die_info *comp_unit_die,
1493 enum language pretend_language);
93311388 1494
ed2dc618 1495static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1496
dee91e82 1497static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1498
f792889a
DJ
1499static struct type *set_die_type (struct die_info *, struct type *,
1500 struct dwarf2_cu *);
1c379e20 1501
ed2dc618 1502static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1503
ed2dc618 1504static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1505
58f0c718 1506static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1507 enum language);
10b3939b 1508
95554aad
TT
1509static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1510 enum language);
10b3939b 1511
f4dc4d17
DE
1512static void process_full_type_unit (struct dwarf2_per_cu_data *,
1513 enum language);
1514
10b3939b
DJ
1515static void dwarf2_add_dependence (struct dwarf2_cu *,
1516 struct dwarf2_per_cu_data *);
1517
ae038cb0
DJ
1518static void dwarf2_mark (struct dwarf2_cu *);
1519
1520static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1521
b64f50a1 1522static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1523 struct dwarf2_per_cu_data *);
673bfd45 1524
f792889a 1525static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1526
95554aad
TT
1527static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1528 enum language pretend_language);
1529
ed2dc618 1530static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1531
b303c6f6
AB
1532/* Class, the destructor of which frees all allocated queue entries. This
1533 will only have work to do if an error was thrown while processing the
1534 dwarf. If no error was thrown then the queue entries should have all
1535 been processed, and freed, as we went along. */
1536
1537class dwarf2_queue_guard
1538{
1539public:
39856def
TT
1540 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1541 : m_per_objfile (per_objfile)
1542 {
1543 }
b303c6f6
AB
1544
1545 /* Free any entries remaining on the queue. There should only be
1546 entries left if we hit an error while processing the dwarf. */
1547 ~dwarf2_queue_guard ()
1548 {
39856def
TT
1549 /* Ensure that no memory is allocated by the queue. */
1550 std::queue<dwarf2_queue_item> empty;
1551 std::swap (m_per_objfile->queue, empty);
1552 }
b303c6f6 1553
39856def 1554 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1555
39856def
TT
1556private:
1557 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1558};
1559
39856def
TT
1560dwarf2_queue_item::~dwarf2_queue_item ()
1561{
1562 /* Anything still marked queued is likely to be in an
1563 inconsistent state, so discard it. */
1564 if (per_cu->queued)
1565 {
1566 if (per_cu->cu != NULL)
1567 free_one_cached_comp_unit (per_cu);
1568 per_cu->queued = 0;
1569 }
1570}
1571
d721ba37
PA
1572/* The return type of find_file_and_directory. Note, the enclosed
1573 string pointers are only valid while this object is valid. */
1574
1575struct file_and_directory
1576{
1577 /* The filename. This is never NULL. */
1578 const char *name;
1579
1580 /* The compilation directory. NULL if not known. If we needed to
1581 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1582 points directly to the DW_AT_comp_dir string attribute owned by
1583 the obstack that owns the DIE. */
1584 const char *comp_dir;
1585
1586 /* If we needed to build a new string for comp_dir, this is what
1587 owns the storage. */
1588 std::string comp_dir_storage;
1589};
1590
1591static file_and_directory find_file_and_directory (struct die_info *die,
1592 struct dwarf2_cu *cu);
9291a0cd 1593
298e9637 1594static htab_up allocate_signatured_type_table ();
1fd400ff 1595
298e9637 1596static htab_up allocate_dwo_unit_table ();
3019eac3 1597
57d63ce2 1598static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1599 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1600 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1601 ULONGEST signature, int is_debug_types);
a2ce51a0 1602
ed2dc618
SM
1603static struct dwp_file *get_dwp_file
1604 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1605
3019eac3 1606static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1607 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1608
1609static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1610 (struct signatured_type *, const char *, const char *);
3019eac3 1611
89e63ee4
DE
1612static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1613
263db9a1
TT
1614/* A unique pointer to a dwo_file. */
1615
51ac9db5 1616typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1617
ed2dc618 1618static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1619
1b80a9fa 1620static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1621
1622static void free_line_header_voidp (void *arg);
4390d890
DE
1623\f
1624/* Various complaints about symbol reading that don't abort the process. */
1625
4390d890
DE
1626static void
1627dwarf2_debug_line_missing_file_complaint (void)
1628{
b98664d3 1629 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1630}
1631
1632static void
1633dwarf2_debug_line_missing_end_sequence_complaint (void)
1634{
b98664d3 1635 complaint (_(".debug_line section has line "
4390d890
DE
1636 "program sequence without an end"));
1637}
1638
1639static void
1640dwarf2_complex_location_expr_complaint (void)
1641{
b98664d3 1642 complaint (_("location expression too complex"));
4390d890
DE
1643}
1644
1645static void
1646dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1647 int arg3)
1648{
b98664d3 1649 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1650 arg1, arg2, arg3);
1651}
1652
4390d890
DE
1653static void
1654dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1655{
b98664d3 1656 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1657 arg1, arg2);
1658}
527f3840
JK
1659
1660/* Hash function for line_header_hash. */
1661
1662static hashval_t
1663line_header_hash (const struct line_header *ofs)
1664{
9c541725 1665 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1666}
1667
1668/* Hash function for htab_create_alloc_ex for line_header_hash. */
1669
1670static hashval_t
1671line_header_hash_voidp (const void *item)
1672{
9a3c8263 1673 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1674
1675 return line_header_hash (ofs);
1676}
1677
1678/* Equality function for line_header_hash. */
1679
1680static int
1681line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1682{
9a3c8263
SM
1683 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1684 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1685
9c541725 1686 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1687 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1688}
1689
4390d890 1690\f
9291a0cd 1691
330cdd98
PA
1692/* See declaration. */
1693
1694dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1695 const dwarf2_debug_sections *names,
1696 bool can_copy_)
1697 : objfile (objfile_),
1698 can_copy (can_copy_)
330cdd98
PA
1699{
1700 if (names == NULL)
1701 names = &dwarf2_elf_names;
1702
1703 bfd *obfd = objfile->obfd;
1704
1705 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1706 locate_sections (obfd, sec, *names);
1707}
1708
1709dwarf2_per_objfile::~dwarf2_per_objfile ()
1710{
1711 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1712 free_cached_comp_units ();
1713
b76e467d 1714 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1715 per_cu->imported_symtabs_free ();
fc8e7e75 1716
b2bdb8cf 1717 for (signatured_type *sig_type : all_type_units)
ae640021 1718 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1719
330cdd98
PA
1720 /* Everything else should be on the objfile obstack. */
1721}
1722
1723/* See declaration. */
1724
1725void
1726dwarf2_per_objfile::free_cached_comp_units ()
1727{
1728 dwarf2_per_cu_data *per_cu = read_in_chain;
1729 dwarf2_per_cu_data **last_chain = &read_in_chain;
1730 while (per_cu != NULL)
1731 {
1732 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1733
fcd3b13d 1734 delete per_cu->cu;
330cdd98
PA
1735 *last_chain = next_cu;
1736 per_cu = next_cu;
1737 }
1738}
1739
11ed8cad
TT
1740/* A helper class that calls free_cached_comp_units on
1741 destruction. */
1742
1743class free_cached_comp_units
1744{
1745public:
1746
1747 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1748 : m_per_objfile (per_objfile)
1749 {
1750 }
1751
1752 ~free_cached_comp_units ()
1753 {
1754 m_per_objfile->free_cached_comp_units ();
1755 }
1756
1757 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1758
1759private:
1760
1761 dwarf2_per_objfile *m_per_objfile;
1762};
1763
c906108c 1764/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1765 information and return true if we have enough to do something.
1766 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1767 ELF names are used. CAN_COPY is true for formats where symbol
1768 interposition is possible and so symbol values must follow copy
1769 relocation rules. */
c906108c
SS
1770
1771int
251d32d9 1772dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1773 const struct dwarf2_debug_sections *names,
1774 bool can_copy)
c906108c 1775{
97cbe998
SDJ
1776 if (objfile->flags & OBJF_READNEVER)
1777 return 0;
1778
ed2dc618
SM
1779 struct dwarf2_per_objfile *dwarf2_per_objfile
1780 = get_dwarf2_per_objfile (objfile);
1781
1782 if (dwarf2_per_objfile == NULL)
5bfd760d 1783 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1784 names,
1785 can_copy);
5bfd760d 1786
73869dc2 1787 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1788 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1789 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1790 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1791}
1792
251d32d9
TG
1793/* When loading sections, we look either for uncompressed section or for
1794 compressed section names. */
233a11ab
CS
1795
1796static int
251d32d9
TG
1797section_is_p (const char *section_name,
1798 const struct dwarf2_section_names *names)
233a11ab 1799{
251d32d9
TG
1800 if (names->normal != NULL
1801 && strcmp (section_name, names->normal) == 0)
1802 return 1;
1803 if (names->compressed != NULL
1804 && strcmp (section_name, names->compressed) == 0)
1805 return 1;
1806 return 0;
233a11ab
CS
1807}
1808
330cdd98 1809/* See declaration. */
c906108c 1810
330cdd98
PA
1811void
1812dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1813 const dwarf2_debug_sections &names)
c906108c 1814{
fd361982 1815 flagword aflag = bfd_section_flags (sectp);
251d32d9 1816
dc7650b8
JK
1817 if ((aflag & SEC_HAS_CONTENTS) == 0)
1818 {
1819 }
950b7495
KS
1820 else if (elf_section_data (sectp)->this_hdr.sh_size
1821 > bfd_get_file_size (abfd))
1822 {
1823 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1824 warning (_("Discarding section %s which has a section size (%s"
1825 ") larger than the file size [in module %s]"),
1826 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1827 bfd_get_filename (abfd));
1828 }
330cdd98 1829 else if (section_is_p (sectp->name, &names.info))
c906108c 1830 {
330cdd98 1831 this->info.s.section = sectp;
fd361982 1832 this->info.size = bfd_section_size (sectp);
c906108c 1833 }
330cdd98 1834 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1835 {
330cdd98 1836 this->abbrev.s.section = sectp;
fd361982 1837 this->abbrev.size = bfd_section_size (sectp);
c906108c 1838 }
330cdd98 1839 else if (section_is_p (sectp->name, &names.line))
c906108c 1840 {
330cdd98 1841 this->line.s.section = sectp;
fd361982 1842 this->line.size = bfd_section_size (sectp);
c906108c 1843 }
330cdd98 1844 else if (section_is_p (sectp->name, &names.loc))
c906108c 1845 {
330cdd98 1846 this->loc.s.section = sectp;
fd361982 1847 this->loc.size = bfd_section_size (sectp);
c906108c 1848 }
330cdd98 1849 else if (section_is_p (sectp->name, &names.loclists))
43988095 1850 {
330cdd98 1851 this->loclists.s.section = sectp;
fd361982 1852 this->loclists.size = bfd_section_size (sectp);
43988095 1853 }
330cdd98 1854 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1855 {
330cdd98 1856 this->macinfo.s.section = sectp;
fd361982 1857 this->macinfo.size = bfd_section_size (sectp);
c906108c 1858 }
330cdd98 1859 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1860 {
330cdd98 1861 this->macro.s.section = sectp;
fd361982 1862 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1863 }
330cdd98 1864 else if (section_is_p (sectp->name, &names.str))
c906108c 1865 {
330cdd98 1866 this->str.s.section = sectp;
fd361982 1867 this->str.size = bfd_section_size (sectp);
c906108c 1868 }
18a8505e
AT
1869 else if (section_is_p (sectp->name, &names.str_offsets))
1870 {
1871 this->str_offsets.s.section = sectp;
1872 this->str_offsets.size = bfd_section_size (sectp);
1873 }
330cdd98 1874 else if (section_is_p (sectp->name, &names.line_str))
43988095 1875 {
330cdd98 1876 this->line_str.s.section = sectp;
fd361982 1877 this->line_str.size = bfd_section_size (sectp);
43988095 1878 }
330cdd98 1879 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1880 {
330cdd98 1881 this->addr.s.section = sectp;
fd361982 1882 this->addr.size = bfd_section_size (sectp);
3019eac3 1883 }
330cdd98 1884 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1885 {
330cdd98 1886 this->frame.s.section = sectp;
fd361982 1887 this->frame.size = bfd_section_size (sectp);
b6af0555 1888 }
330cdd98 1889 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1890 {
330cdd98 1891 this->eh_frame.s.section = sectp;
fd361982 1892 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1893 }
330cdd98 1894 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1895 {
330cdd98 1896 this->ranges.s.section = sectp;
fd361982 1897 this->ranges.size = bfd_section_size (sectp);
af34e669 1898 }
330cdd98 1899 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1900 {
330cdd98 1901 this->rnglists.s.section = sectp;
fd361982 1902 this->rnglists.size = bfd_section_size (sectp);
43988095 1903 }
330cdd98 1904 else if (section_is_p (sectp->name, &names.types))
348e048f 1905 {
8b70b953
TT
1906 struct dwarf2_section_info type_section;
1907
1908 memset (&type_section, 0, sizeof (type_section));
049412e3 1909 type_section.s.section = sectp;
fd361982 1910 type_section.size = bfd_section_size (sectp);
8b70b953 1911
fd5866f6 1912 this->types.push_back (type_section);
348e048f 1913 }
330cdd98 1914 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1915 {
330cdd98 1916 this->gdb_index.s.section = sectp;
fd361982 1917 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1918 }
927aa2e7
JK
1919 else if (section_is_p (sectp->name, &names.debug_names))
1920 {
1921 this->debug_names.s.section = sectp;
fd361982 1922 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1923 }
1924 else if (section_is_p (sectp->name, &names.debug_aranges))
1925 {
1926 this->debug_aranges.s.section = sectp;
fd361982 1927 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1928 }
dce234bc 1929
fd361982
AM
1930 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1931 && bfd_section_vma (sectp) == 0)
330cdd98 1932 this->has_section_at_zero = true;
c906108c
SS
1933}
1934
dce234bc 1935/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1936 SECTION_NAME. */
af34e669 1937
dce234bc 1938void
3017a003
TG
1939dwarf2_get_section_info (struct objfile *objfile,
1940 enum dwarf2_section_enum sect,
d521ce57 1941 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1942 bfd_size_type *sizep)
1943{
5bfd760d 1944 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 1945 struct dwarf2_section_info *info;
a3b2a86b
TT
1946
1947 /* We may see an objfile without any DWARF, in which case we just
1948 return nothing. */
1949 if (data == NULL)
1950 {
1951 *sectp = NULL;
1952 *bufp = NULL;
1953 *sizep = 0;
1954 return;
1955 }
3017a003
TG
1956 switch (sect)
1957 {
1958 case DWARF2_DEBUG_FRAME:
1959 info = &data->frame;
1960 break;
1961 case DWARF2_EH_FRAME:
1962 info = &data->eh_frame;
1963 break;
1964 default:
1965 gdb_assert_not_reached ("unexpected section");
1966 }
dce234bc 1967
96b79293 1968 info->read (objfile);
dce234bc 1969
96b79293 1970 *sectp = info->get_bfd_section ();
dce234bc
PP
1971 *bufp = info->buffer;
1972 *sizep = info->size;
1973}
1974
36586728
TT
1975/* A helper function to find the sections for a .dwz file. */
1976
1977static void
1978locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1979{
9a3c8263 1980 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
1981
1982 /* Note that we only support the standard ELF names, because .dwz
1983 is ELF-only (at the time of writing). */
1984 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1985 {
049412e3 1986 dwz_file->abbrev.s.section = sectp;
fd361982 1987 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
1988 }
1989 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1990 {
049412e3 1991 dwz_file->info.s.section = sectp;
fd361982 1992 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
1993 }
1994 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1995 {
049412e3 1996 dwz_file->str.s.section = sectp;
fd361982 1997 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
1998 }
1999 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2000 {
049412e3 2001 dwz_file->line.s.section = sectp;
fd361982 2002 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2003 }
2004 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2005 {
049412e3 2006 dwz_file->macro.s.section = sectp;
fd361982 2007 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2008 }
2ec9a5e0
TT
2009 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2010 {
049412e3 2011 dwz_file->gdb_index.s.section = sectp;
fd361982 2012 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2013 }
927aa2e7
JK
2014 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2015 {
2016 dwz_file->debug_names.s.section = sectp;
fd361982 2017 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2018 }
36586728
TT
2019}
2020
c4973306 2021/* See dwarf2read.h. */
36586728 2022
c4973306 2023struct dwz_file *
ed2dc618 2024dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2025{
36586728 2026 const char *filename;
acd13123 2027 bfd_size_type buildid_len_arg;
dc294be5
TT
2028 size_t buildid_len;
2029 bfd_byte *buildid;
36586728
TT
2030
2031 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2032 return dwarf2_per_objfile->dwz_file.get ();
36586728 2033
4db1a1dc 2034 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2035 gdb::unique_xmalloc_ptr<char> data
2036 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2037 &buildid_len_arg, &buildid));
4db1a1dc
TT
2038 if (data == NULL)
2039 {
2040 if (bfd_get_error () == bfd_error_no_error)
2041 return NULL;
2042 error (_("could not read '.gnu_debugaltlink' section: %s"),
2043 bfd_errmsg (bfd_get_error ()));
2044 }
791afaa2
TT
2045
2046 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2047
acd13123
TT
2048 buildid_len = (size_t) buildid_len_arg;
2049
791afaa2 2050 filename = data.get ();
d721ba37
PA
2051
2052 std::string abs_storage;
36586728
TT
2053 if (!IS_ABSOLUTE_PATH (filename))
2054 {
14278e1f
TT
2055 gdb::unique_xmalloc_ptr<char> abs
2056 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2057
14278e1f 2058 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2059 filename = abs_storage.c_str ();
36586728
TT
2060 }
2061
dc294be5
TT
2062 /* First try the file name given in the section. If that doesn't
2063 work, try to use the build-id instead. */
192b62ce 2064 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2065 if (dwz_bfd != NULL)
36586728 2066 {
192b62ce 2067 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2068 dwz_bfd.reset (nullptr);
36586728
TT
2069 }
2070
dc294be5
TT
2071 if (dwz_bfd == NULL)
2072 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2073
0d79cdc4
AM
2074 if (dwz_bfd == nullptr)
2075 {
2076 gdb::unique_xmalloc_ptr<char> alt_filename;
2077 const char *origname = dwarf2_per_objfile->objfile->original_name;
2078
2079 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2080 buildid_len,
2081 origname,
2082 &alt_filename));
2083
2084 if (fd.get () >= 0)
2085 {
2086 /* File successfully retrieved from server. */
2087 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2088
2089 if (dwz_bfd == nullptr)
2090 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2091 alt_filename.get ());
2092 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2093 dwz_bfd.reset (nullptr);
2094 }
2095 }
2096
dc294be5
TT
2097 if (dwz_bfd == NULL)
2098 error (_("could not find '.gnu_debugaltlink' file for %s"),
2099 objfile_name (dwarf2_per_objfile->objfile));
2100
7ff8cb8c
TT
2101 std::unique_ptr<struct dwz_file> result
2102 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2103
7ff8cb8c
TT
2104 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2105 result.get ());
36586728 2106
7ff8cb8c
TT
2107 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2108 result->dwz_bfd.get ());
2109 dwarf2_per_objfile->dwz_file = std::move (result);
2110 return dwarf2_per_objfile->dwz_file.get ();
36586728 2111}
9291a0cd 2112\f
7b9f3c50
DE
2113/* DWARF quick_symbols_functions support. */
2114
2115/* TUs can share .debug_line entries, and there can be a lot more TUs than
2116 unique line tables, so we maintain a separate table of all .debug_line
2117 derived entries to support the sharing.
2118 All the quick functions need is the list of file names. We discard the
2119 line_header when we're done and don't need to record it here. */
2120struct quick_file_names
2121{
094b34ac
DE
2122 /* The data used to construct the hash key. */
2123 struct stmt_list_hash hash;
7b9f3c50
DE
2124
2125 /* The number of entries in file_names, real_names. */
2126 unsigned int num_file_names;
2127
2128 /* The file names from the line table, after being run through
2129 file_full_name. */
2130 const char **file_names;
2131
2132 /* The file names from the line table after being run through
2133 gdb_realpath. These are computed lazily. */
2134 const char **real_names;
2135};
2136
2137/* When using the index (and thus not using psymtabs), each CU has an
2138 object of this type. This is used to hold information needed by
2139 the various "quick" methods. */
2140struct dwarf2_per_cu_quick_data
2141{
2142 /* The file table. This can be NULL if there was no file table
2143 or it's currently not read in.
2144 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2145 struct quick_file_names *file_names;
2146
2147 /* The corresponding symbol table. This is NULL if symbols for this
2148 CU have not yet been read. */
43f3e411 2149 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2150
2151 /* A temporary mark bit used when iterating over all CUs in
2152 expand_symtabs_matching. */
2153 unsigned int mark : 1;
2154
2155 /* True if we've tried to read the file table and found there isn't one.
2156 There will be no point in trying to read it again next time. */
2157 unsigned int no_file_data : 1;
2158};
2159
094b34ac
DE
2160/* Utility hash function for a stmt_list_hash. */
2161
2162static hashval_t
2163hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2164{
2165 hashval_t v = 0;
2166
2167 if (stmt_list_hash->dwo_unit != NULL)
2168 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2169 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2170 return v;
2171}
2172
2173/* Utility equality function for a stmt_list_hash. */
2174
2175static int
2176eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2177 const struct stmt_list_hash *rhs)
2178{
2179 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2180 return 0;
2181 if (lhs->dwo_unit != NULL
2182 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2183 return 0;
2184
9c541725 2185 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2186}
2187
7b9f3c50
DE
2188/* Hash function for a quick_file_names. */
2189
2190static hashval_t
2191hash_file_name_entry (const void *e)
2192{
9a3c8263
SM
2193 const struct quick_file_names *file_data
2194 = (const struct quick_file_names *) e;
7b9f3c50 2195
094b34ac 2196 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2197}
2198
2199/* Equality function for a quick_file_names. */
2200
2201static int
2202eq_file_name_entry (const void *a, const void *b)
2203{
9a3c8263
SM
2204 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2205 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2206
094b34ac 2207 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2208}
2209
2210/* Delete function for a quick_file_names. */
2211
2212static void
2213delete_file_name_entry (void *e)
2214{
9a3c8263 2215 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2216 int i;
2217
2218 for (i = 0; i < file_data->num_file_names; ++i)
2219 {
2220 xfree ((void*) file_data->file_names[i]);
2221 if (file_data->real_names)
2222 xfree ((void*) file_data->real_names[i]);
2223 }
2224
2225 /* The space for the struct itself lives on objfile_obstack,
2226 so we don't free it here. */
2227}
2228
2229/* Create a quick_file_names hash table. */
2230
5895093f 2231static htab_up
7b9f3c50
DE
2232create_quick_file_names_table (unsigned int nr_initial_entries)
2233{
5895093f
TT
2234 return htab_up (htab_create_alloc (nr_initial_entries,
2235 hash_file_name_entry, eq_file_name_entry,
2236 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2237}
9291a0cd 2238
918dd910
JK
2239/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2240 have to be created afterwards. You should call age_cached_comp_units after
2241 processing PER_CU->CU. dw2_setup must have been already called. */
2242
2243static void
58f0c718 2244load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2245{
3019eac3 2246 if (per_cu->is_debug_types)
e5fe5e75 2247 load_full_type_unit (per_cu);
918dd910 2248 else
58f0c718 2249 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2250
cc12ce38
DE
2251 if (per_cu->cu == NULL)
2252 return; /* Dummy CU. */
2dc860c0
DE
2253
2254 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2255}
2256
a0f42c21 2257/* Read in the symbols for PER_CU. */
2fdf6df6 2258
9291a0cd 2259static void
58f0c718 2260dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2261{
ed2dc618 2262 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2263
f4dc4d17
DE
2264 /* Skip type_unit_groups, reading the type units they contain
2265 is handled elsewhere. */
197400e8 2266 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2267 return;
2268
b303c6f6
AB
2269 /* The destructor of dwarf2_queue_guard frees any entries left on
2270 the queue. After this point we're guaranteed to leave this function
2271 with the dwarf queue empty. */
39856def 2272 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2273
95554aad 2274 if (dwarf2_per_objfile->using_index
43f3e411 2275 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2276 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2277 {
2278 queue_comp_unit (per_cu, language_minimal);
58f0c718 2279 load_cu (per_cu, skip_partial);
89e63ee4
DE
2280
2281 /* If we just loaded a CU from a DWO, and we're working with an index
2282 that may badly handle TUs, load all the TUs in that DWO as well.
2283 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2284 if (!per_cu->is_debug_types
cc12ce38 2285 && per_cu->cu != NULL
89e63ee4
DE
2286 && per_cu->cu->dwo_unit != NULL
2287 && dwarf2_per_objfile->index_table != NULL
2288 && dwarf2_per_objfile->index_table->version <= 7
2289 /* DWP files aren't supported yet. */
ed2dc618 2290 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2291 queue_and_load_all_dwo_tus (per_cu);
95554aad 2292 }
9291a0cd 2293
ed2dc618 2294 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2295
2296 /* Age the cache, releasing compilation units that have not
2297 been used recently. */
ed2dc618 2298 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2299}
2300
2301/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2302 the objfile from which this CU came. Returns the resulting symbol
2303 table. */
2fdf6df6 2304
43f3e411 2305static struct compunit_symtab *
58f0c718 2306dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2307{
ed2dc618
SM
2308 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2309
95554aad 2310 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2311 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2312 {
11ed8cad 2313 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2314 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2315 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2316 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2317 }
f194fefb 2318
43f3e411 2319 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2320}
2321
ff4c9fec 2322/* See declaration. */
f4dc4d17 2323
ff4c9fec
SM
2324dwarf2_per_cu_data *
2325dwarf2_per_objfile::get_cutu (int index)
2326{
b76e467d 2327 if (index >= this->all_comp_units.size ())
ff4c9fec 2328 {
b76e467d 2329 index -= this->all_comp_units.size ();
b2bdb8cf 2330 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2331 return &this->all_type_units[index]->per_cu;
2332 }
f4dc4d17 2333
ff4c9fec
SM
2334 return this->all_comp_units[index];
2335}
f4dc4d17 2336
ff4c9fec 2337/* See declaration. */
2fdf6df6 2338
ff4c9fec
SM
2339dwarf2_per_cu_data *
2340dwarf2_per_objfile::get_cu (int index)
1fd400ff 2341{
b76e467d 2342 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2343
ff4c9fec 2344 return this->all_comp_units[index];
f4dc4d17
DE
2345}
2346
ff4c9fec 2347/* See declaration. */
f4dc4d17 2348
ff4c9fec
SM
2349signatured_type *
2350dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2351{
b2bdb8cf 2352 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2353
ff4c9fec 2354 return this->all_type_units[index];
1fd400ff
TT
2355}
2356
4b514bc8
JK
2357/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2358 objfile_obstack, and constructed with the specified field
2359 values. */
2360
2361static dwarf2_per_cu_data *
ed2dc618 2362create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2363 struct dwarf2_section_info *section,
2364 int is_dwz,
2365 sect_offset sect_off, ULONGEST length)
2366{
ed2dc618 2367 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2368 dwarf2_per_cu_data *the_cu
2369 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2370 struct dwarf2_per_cu_data);
2371 the_cu->sect_off = sect_off;
2372 the_cu->length = length;
e3b94546 2373 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2374 the_cu->section = section;
2375 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2376 struct dwarf2_per_cu_quick_data);
2377 the_cu->is_dwz = is_dwz;
2378 return the_cu;
2379}
2380
2ec9a5e0
TT
2381/* A helper for create_cus_from_index that handles a given list of
2382 CUs. */
2fdf6df6 2383
74a0d9f6 2384static void
12359b5e 2385create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2386 const gdb_byte *cu_list, offset_type n_elements,
2387 struct dwarf2_section_info *section,
b76e467d 2388 int is_dwz)
9291a0cd 2389{
12359b5e 2390 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2391 {
74a0d9f6 2392 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2393
2394 sect_offset sect_off
2395 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2396 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2397 cu_list += 2 * 8;
2398
b76e467d 2399 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2400 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2401 sect_off, length);
b76e467d 2402 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2403 }
9291a0cd
TT
2404}
2405
2ec9a5e0 2406/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2407 the CU objects for this objfile. */
2ec9a5e0 2408
74a0d9f6 2409static void
12359b5e 2410create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2411 const gdb_byte *cu_list, offset_type cu_list_elements,
2412 const gdb_byte *dwz_list, offset_type dwz_elements)
2413{
b76e467d
SM
2414 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2415 dwarf2_per_objfile->all_comp_units.reserve
2416 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2417
12359b5e 2418 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2419 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2420
2421 if (dwz_elements == 0)
74a0d9f6 2422 return;
2ec9a5e0 2423
12359b5e
SM
2424 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2425 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2426 &dwz->info, 1);
2ec9a5e0
TT
2427}
2428
1fd400ff 2429/* Create the signatured type hash table from the index. */
673bfd45 2430
74a0d9f6 2431static void
12359b5e
SM
2432create_signatured_type_table_from_index
2433 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2434 struct dwarf2_section_info *section,
2435 const gdb_byte *bytes,
2436 offset_type elements)
1fd400ff 2437{
12359b5e 2438 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2439
b2bdb8cf
SM
2440 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2441 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2442
298e9637 2443 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2444
12359b5e 2445 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2446 {
52dc124a 2447 struct signatured_type *sig_type;
9c541725 2448 ULONGEST signature;
1fd400ff 2449 void **slot;
9c541725 2450 cu_offset type_offset_in_tu;
1fd400ff 2451
74a0d9f6 2452 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2453 sect_offset sect_off
2454 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2455 type_offset_in_tu
2456 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2457 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2458 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2459 bytes += 3 * 8;
2460
52dc124a 2461 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2462 struct signatured_type);
52dc124a 2463 sig_type->signature = signature;
9c541725 2464 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2465 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2466 sig_type->per_cu.section = section;
9c541725 2467 sig_type->per_cu.sect_off = sect_off;
e3b94546 2468 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2469 sig_type->per_cu.v.quick
1fd400ff
TT
2470 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2471 struct dwarf2_per_cu_quick_data);
2472
b0b6a987 2473 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2474 *slot = sig_type;
1fd400ff 2475
b2bdb8cf 2476 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2477 }
2478
b0b6a987 2479 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2480}
2481
927aa2e7
JK
2482/* Create the signatured type hash table from .debug_names. */
2483
2484static void
2485create_signatured_type_table_from_debug_names
ed2dc618 2486 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2487 const mapped_debug_names &map,
2488 struct dwarf2_section_info *section,
2489 struct dwarf2_section_info *abbrev_section)
2490{
ed2dc618
SM
2491 struct objfile *objfile = dwarf2_per_objfile->objfile;
2492
96b79293
TT
2493 section->read (objfile);
2494 abbrev_section->read (objfile);
927aa2e7 2495
b2bdb8cf
SM
2496 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2497 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2498
298e9637 2499 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2500
2501 for (uint32_t i = 0; i < map.tu_count; ++i)
2502 {
2503 struct signatured_type *sig_type;
927aa2e7 2504 void **slot;
927aa2e7
JK
2505
2506 sect_offset sect_off
2507 = (sect_offset) (extract_unsigned_integer
2508 (map.tu_table_reordered + i * map.offset_size,
2509 map.offset_size,
2510 map.dwarf5_byte_order));
2511
2512 comp_unit_head cu_header;
ed2dc618
SM
2513 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2514 abbrev_section,
927aa2e7
JK
2515 section->buffer + to_underlying (sect_off),
2516 rcuh_kind::TYPE);
2517
2518 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2519 struct signatured_type);
2520 sig_type->signature = cu_header.signature;
2521 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2522 sig_type->per_cu.is_debug_types = 1;
2523 sig_type->per_cu.section = section;
2524 sig_type->per_cu.sect_off = sect_off;
e3b94546 2525 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2526 sig_type->per_cu.v.quick
2527 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2528 struct dwarf2_per_cu_quick_data);
2529
b0b6a987 2530 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2531 *slot = sig_type;
2532
b2bdb8cf 2533 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2534 }
2535
b0b6a987 2536 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2537}
2538
9291a0cd
TT
2539/* Read the address map data from the mapped index, and use it to
2540 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2541
9291a0cd 2542static void
ed2dc618
SM
2543create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2544 struct mapped_index *index)
9291a0cd 2545{
ed2dc618 2546 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2547 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2548 const gdb_byte *iter, *end;
9291a0cd 2549 struct addrmap *mutable_map;
9291a0cd
TT
2550 CORE_ADDR baseaddr;
2551
8268c778
PA
2552 auto_obstack temp_obstack;
2553
9291a0cd
TT
2554 mutable_map = addrmap_create_mutable (&temp_obstack);
2555
f00a2de2
PA
2556 iter = index->address_table.data ();
2557 end = iter + index->address_table.size ();
9291a0cd 2558
b3b3bada 2559 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2560
2561 while (iter < end)
2562 {
2563 ULONGEST hi, lo, cu_index;
2564 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2565 iter += 8;
2566 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2567 iter += 8;
2568 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2569 iter += 4;
f652bce2 2570
24a55014 2571 if (lo > hi)
f652bce2 2572 {
b98664d3 2573 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2574 hex_string (lo), hex_string (hi));
24a55014 2575 continue;
f652bce2 2576 }
24a55014 2577
b76e467d 2578 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2579 {
b98664d3 2580 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2581 (unsigned) cu_index);
24a55014 2582 continue;
f652bce2 2583 }
24a55014 2584
79748972
TT
2585 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2586 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2587 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2588 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2589 }
2590
d320c2b5 2591 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2592 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2593}
2594
927aa2e7
JK
2595/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2596 populate the objfile's psymtabs_addrmap. */
2597
2598static void
ed2dc618 2599create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2600 struct dwarf2_section_info *section)
2601{
ed2dc618 2602 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2603 bfd *abfd = objfile->obfd;
2604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2605 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2606
2607 auto_obstack temp_obstack;
2608 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2609
2610 std::unordered_map<sect_offset,
2611 dwarf2_per_cu_data *,
2612 gdb::hash_enum<sect_offset>>
2613 debug_info_offset_to_per_cu;
b76e467d 2614 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2615 {
927aa2e7
JK
2616 const auto insertpair
2617 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2618 if (!insertpair.second)
2619 {
2620 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2621 "debug_info_offset %s, ignoring .debug_aranges."),
2622 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2623 return;
2624 }
2625 }
2626
96b79293 2627 section->read (objfile);
927aa2e7
JK
2628
2629 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2630
2631 const gdb_byte *addr = section->buffer;
2632
2633 while (addr < section->buffer + section->size)
2634 {
2635 const gdb_byte *const entry_addr = addr;
2636 unsigned int bytes_read;
2637
2638 const LONGEST entry_length = read_initial_length (abfd, addr,
2639 &bytes_read);
2640 addr += bytes_read;
2641
2642 const gdb_byte *const entry_end = addr + entry_length;
2643 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2644 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2645 if (addr + entry_length > section->buffer + section->size)
2646 {
47e3f474 2647 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2648 "length %s exceeds section length %s, "
2649 "ignoring .debug_aranges."),
47e3f474
TV
2650 objfile_name (objfile),
2651 plongest (entry_addr - section->buffer),
927aa2e7
JK
2652 plongest (bytes_read + entry_length),
2653 pulongest (section->size));
2654 return;
2655 }
2656
2657 /* The version number. */
2658 const uint16_t version = read_2_bytes (abfd, addr);
2659 addr += 2;
2660 if (version != 2)
2661 {
47e3f474 2662 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2663 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2664 objfile_name (objfile),
2665 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2666 return;
2667 }
2668
2669 const uint64_t debug_info_offset
2670 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2671 addr += offset_size;
2672 const auto per_cu_it
2673 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2674 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2675 {
47e3f474 2676 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2677 "debug_info_offset %s does not exists, "
2678 "ignoring .debug_aranges."),
47e3f474
TV
2679 objfile_name (objfile),
2680 plongest (entry_addr - section->buffer),
927aa2e7
JK
2681 pulongest (debug_info_offset));
2682 return;
2683 }
2684 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2685
2686 const uint8_t address_size = *addr++;
2687 if (address_size < 1 || address_size > 8)
2688 {
47e3f474 2689 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2690 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2691 objfile_name (objfile),
2692 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2693 return;
2694 }
2695
2696 const uint8_t segment_selector_size = *addr++;
2697 if (segment_selector_size != 0)
2698 {
47e3f474 2699 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2700 "segment_selector_size %u is not supported, "
2701 "ignoring .debug_aranges."),
47e3f474
TV
2702 objfile_name (objfile),
2703 plongest (entry_addr - section->buffer),
927aa2e7
JK
2704 segment_selector_size);
2705 return;
2706 }
2707
2708 /* Must pad to an alignment boundary that is twice the address
2709 size. It is undocumented by the DWARF standard but GCC does
2710 use it. */
2711 for (size_t padding = ((-(addr - section->buffer))
2712 & (2 * address_size - 1));
2713 padding > 0; padding--)
2714 if (*addr++ != 0)
2715 {
47e3f474 2716 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2717 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2718 objfile_name (objfile),
2719 plongest (entry_addr - section->buffer));
927aa2e7
JK
2720 return;
2721 }
2722
2723 for (;;)
2724 {
2725 if (addr + 2 * address_size > entry_end)
2726 {
47e3f474 2727 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2728 "address list is not properly terminated, "
2729 "ignoring .debug_aranges."),
47e3f474
TV
2730 objfile_name (objfile),
2731 plongest (entry_addr - section->buffer));
927aa2e7
JK
2732 return;
2733 }
2734 ULONGEST start = extract_unsigned_integer (addr, address_size,
2735 dwarf5_byte_order);
2736 addr += address_size;
2737 ULONGEST length = extract_unsigned_integer (addr, address_size,
2738 dwarf5_byte_order);
2739 addr += address_size;
2740 if (start == 0 && length == 0)
2741 break;
2742 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2743 {
2744 /* Symbol was eliminated due to a COMDAT group. */
2745 continue;
2746 }
2747 ULONGEST end = start + length;
79748972
TT
2748 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2749 - baseaddr);
2750 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2751 - baseaddr);
927aa2e7
JK
2752 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2753 }
2754 }
2755
d320c2b5 2756 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2757 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2758}
2759
9291a0cd
TT
2760/* Find a slot in the mapped index INDEX for the object named NAME.
2761 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2762 constant pool and return true. If NAME cannot be found, return
2763 false. */
2fdf6df6 2764
109483d9 2765static bool
9291a0cd
TT
2766find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2767 offset_type **vec_out)
2768{
0cf03b49 2769 offset_type hash;
9291a0cd 2770 offset_type slot, step;
559a7a62 2771 int (*cmp) (const char *, const char *);
9291a0cd 2772
791afaa2 2773 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2774 if (current_language->la_language == language_cplus
45280282
IB
2775 || current_language->la_language == language_fortran
2776 || current_language->la_language == language_d)
0cf03b49
JK
2777 {
2778 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2779 not contain any. */
a8719064 2780
72998fb3 2781 if (strchr (name, '(') != NULL)
0cf03b49 2782 {
109483d9 2783 without_params = cp_remove_params (name);
0cf03b49 2784
72998fb3 2785 if (without_params != NULL)
791afaa2 2786 name = without_params.get ();
0cf03b49
JK
2787 }
2788 }
2789
559a7a62 2790 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2791 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2792 simulate our NAME being searched is also lowercased. */
2793 hash = mapped_index_string_hash ((index->version == 4
2794 && case_sensitivity == case_sensitive_off
2795 ? 5 : index->version),
2796 name);
2797
f00a2de2
PA
2798 slot = hash & (index->symbol_table.size () - 1);
2799 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2800 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2801
2802 for (;;)
2803 {
9291a0cd 2804 const char *str;
f00a2de2
PA
2805
2806 const auto &bucket = index->symbol_table[slot];
2807 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2808 return false;
9291a0cd 2809
f00a2de2 2810 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2811 if (!cmp (name, str))
9291a0cd
TT
2812 {
2813 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2814 + MAYBE_SWAP (bucket.vec));
109483d9 2815 return true;
9291a0cd
TT
2816 }
2817
f00a2de2 2818 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2819 }
2820}
2821
4485a1c1
SM
2822/* A helper function that reads the .gdb_index from BUFFER and fills
2823 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2824 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2825 ok to use deprecated sections.
2826
2827 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2828 out parameters that are filled in with information about the CU and
2829 TU lists in the section.
2830
4485a1c1 2831 Returns true if all went well, false otherwise. */
2fdf6df6 2832
d33bc52e 2833static bool
4485a1c1
SM
2834read_gdb_index_from_buffer (struct objfile *objfile,
2835 const char *filename,
2836 bool deprecated_ok,
2837 gdb::array_view<const gdb_byte> buffer,
2838 struct mapped_index *map,
2839 const gdb_byte **cu_list,
2840 offset_type *cu_list_elements,
2841 const gdb_byte **types_list,
2842 offset_type *types_list_elements)
2843{
2844 const gdb_byte *addr = &buffer[0];
82430852 2845
9291a0cd 2846 /* Version check. */
4485a1c1 2847 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2848 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2849 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2850 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2851 indices. */
831adc1f 2852 if (version < 4)
481860b3
GB
2853 {
2854 static int warning_printed = 0;
2855 if (!warning_printed)
2856 {
2857 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2858 filename);
481860b3
GB
2859 warning_printed = 1;
2860 }
2861 return 0;
2862 }
2863 /* Index version 4 uses a different hash function than index version
2864 5 and later.
2865
2866 Versions earlier than 6 did not emit psymbols for inlined
2867 functions. Using these files will cause GDB not to be able to
2868 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2869 indices unless the user has done
2870 "set use-deprecated-index-sections on". */
2ec9a5e0 2871 if (version < 6 && !deprecated_ok)
481860b3
GB
2872 {
2873 static int warning_printed = 0;
2874 if (!warning_printed)
2875 {
e615022a
DE
2876 warning (_("\
2877Skipping deprecated .gdb_index section in %s.\n\
2878Do \"set use-deprecated-index-sections on\" before the file is read\n\
2879to use the section anyway."),
2ec9a5e0 2880 filename);
481860b3
GB
2881 warning_printed = 1;
2882 }
2883 return 0;
2884 }
796a7ff8 2885 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2886 of the TU (for symbols coming from TUs),
2887 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2888 Plus gold-generated indices can have duplicate entries for global symbols,
2889 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2890 These are just performance bugs, and we can't distinguish gdb-generated
2891 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2892
481860b3 2893 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2894 longer backward compatible. */
796a7ff8 2895 if (version > 8)
594e8718 2896 return 0;
9291a0cd 2897
559a7a62 2898 map->version = version;
9291a0cd 2899
4485a1c1 2900 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2901
4485a1c1 2902 int i = 0;
2ec9a5e0
TT
2903 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2904 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2905 / 8);
1fd400ff
TT
2906 ++i;
2907
2ec9a5e0
TT
2908 *types_list = addr + MAYBE_SWAP (metadata[i]);
2909 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2910 - MAYBE_SWAP (metadata[i]))
2911 / 8);
987d643c 2912 ++i;
1fd400ff 2913
f00a2de2
PA
2914 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2915 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2916 map->address_table
2917 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2918 ++i;
2919
f00a2de2
PA
2920 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2921 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2922 map->symbol_table
2923 = gdb::array_view<mapped_index::symbol_table_slot>
2924 ((mapped_index::symbol_table_slot *) symbol_table,
2925 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2926
f00a2de2 2927 ++i;
f9d83a0b 2928 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2929
2ec9a5e0
TT
2930 return 1;
2931}
2932
4485a1c1
SM
2933/* Callback types for dwarf2_read_gdb_index. */
2934
2935typedef gdb::function_view
2936 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2937 get_gdb_index_contents_ftype;
2938typedef gdb::function_view
2939 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2940 get_gdb_index_contents_dwz_ftype;
2941
927aa2e7 2942/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2943 elements of all the CUs and return 1. Otherwise, return 0. */
2944
2945static int
4485a1c1
SM
2946dwarf2_read_gdb_index
2947 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2948 get_gdb_index_contents_ftype get_gdb_index_contents,
2949 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2950{
2ec9a5e0
TT
2951 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2952 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2953 struct dwz_file *dwz;
12359b5e 2954 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 2955
4485a1c1
SM
2956 gdb::array_view<const gdb_byte> main_index_contents
2957 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2958
2959 if (main_index_contents.empty ())
2960 return 0;
2961
3063847f 2962 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
2963 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2964 use_deprecated_index_sections,
2965 main_index_contents, map.get (), &cu_list,
2966 &cu_list_elements, &types_list,
2967 &types_list_elements))
2ec9a5e0
TT
2968 return 0;
2969
0fefef59 2970 /* Don't use the index if it's empty. */
3063847f 2971 if (map->symbol_table.empty ())
0fefef59
DE
2972 return 0;
2973
2ec9a5e0
TT
2974 /* If there is a .dwz file, read it so we can get its CU list as
2975 well. */
ed2dc618 2976 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 2977 if (dwz != NULL)
2ec9a5e0 2978 {
2ec9a5e0
TT
2979 struct mapped_index dwz_map;
2980 const gdb_byte *dwz_types_ignore;
2981 offset_type dwz_types_elements_ignore;
2982
4485a1c1
SM
2983 gdb::array_view<const gdb_byte> dwz_index_content
2984 = get_gdb_index_contents_dwz (objfile, dwz);
2985
2986 if (dwz_index_content.empty ())
2987 return 0;
2988
2989 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
2990 bfd_get_filename (dwz->dwz_bfd.get ()),
2991 1, dwz_index_content, &dwz_map,
4485a1c1
SM
2992 &dwz_list, &dwz_list_elements,
2993 &dwz_types_ignore,
2994 &dwz_types_elements_ignore))
2ec9a5e0
TT
2995 {
2996 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 2997 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
2998 return 0;
2999 }
3000 }
3001
12359b5e
SM
3002 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3003 dwz_list, dwz_list_elements);
1fd400ff 3004
8b70b953
TT
3005 if (types_list_elements)
3006 {
8b70b953
TT
3007 /* We can only handle a single .debug_types when we have an
3008 index. */
fd5866f6 3009 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3010 return 0;
3011
fd5866f6 3012 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3013
12359b5e
SM
3014 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3015 types_list, types_list_elements);
8b70b953 3016 }
9291a0cd 3017
3063847f 3018 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3019
3063847f 3020 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3021 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3022 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3023 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3024
3025 return 1;
3026}
3027
dee91e82 3028/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3029
dee91e82
DE
3030static void
3031dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3032 const gdb_byte *info_ptr,
3e225074 3033 struct die_info *comp_unit_die)
9291a0cd 3034{
dee91e82 3035 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3036 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3037 struct dwarf2_per_objfile *dwarf2_per_objfile
3038 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3039 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3040 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3041 struct attribute *attr;
7b9f3c50
DE
3042 void **slot;
3043 struct quick_file_names *qfn;
9291a0cd 3044
0186c6a7
DE
3045 gdb_assert (! this_cu->is_debug_types);
3046
07261596
TT
3047 /* Our callers never want to match partial units -- instead they
3048 will match the enclosing full CU. */
3049 if (comp_unit_die->tag == DW_TAG_partial_unit)
3050 {
3051 this_cu->v.quick->no_file_data = 1;
3052 return;
3053 }
3054
0186c6a7 3055 lh_cu = this_cu;
7b9f3c50 3056 slot = NULL;
dee91e82 3057
fff8551c 3058 line_header_up lh;
9c541725 3059 sect_offset line_offset {};
fff8551c 3060
dee91e82 3061 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3062 if (attr != nullptr)
9291a0cd 3063 {
7b9f3c50
DE
3064 struct quick_file_names find_entry;
3065
9c541725 3066 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3067
3068 /* We may have already read in this line header (TU line header sharing).
3069 If we have we're done. */
094b34ac 3070 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3071 find_entry.hash.line_sect_off = line_offset;
5895093f 3072 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3073 &find_entry, INSERT);
3074 if (*slot != NULL)
3075 {
9a3c8263 3076 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3077 return;
7b9f3c50
DE
3078 }
3079
3019eac3 3080 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3081 }
3082 if (lh == NULL)
3083 {
094b34ac 3084 lh_cu->v.quick->no_file_data = 1;
dee91e82 3085 return;
9291a0cd
TT
3086 }
3087
8d749320 3088 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3089 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3090 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3091 gdb_assert (slot != NULL);
3092 *slot = qfn;
9291a0cd 3093
d721ba37 3094 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3095
aa391654
TT
3096 int offset = 0;
3097 if (strcmp (fnd.name, "<unknown>") != 0)
3098 ++offset;
3099
7ba99d21 3100 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3101 qfn->file_names =
aa391654
TT
3102 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3103 if (offset != 0)
3104 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3105 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3106 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3107 fnd.comp_dir).release ();
7b9f3c50 3108 qfn->real_names = NULL;
9291a0cd 3109
094b34ac 3110 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3111}
3112
3113/* A helper for the "quick" functions which attempts to read the line
3114 table for THIS_CU. */
3115
3116static struct quick_file_names *
e4a48d9d 3117dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3118{
0186c6a7
DE
3119 /* This should never be called for TUs. */
3120 gdb_assert (! this_cu->is_debug_types);
3121 /* Nor type unit groups. */
197400e8 3122 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3123
dee91e82
DE
3124 if (this_cu->v.quick->file_names != NULL)
3125 return this_cu->v.quick->file_names;
3126 /* If we know there is no line data, no point in looking again. */
3127 if (this_cu->v.quick->no_file_data)
3128 return NULL;
3129
c0ab21c2
TT
3130 cutu_reader reader (this_cu);
3131 if (!reader.dummy_p)
3e225074 3132 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3133
3134 if (this_cu->v.quick->no_file_data)
3135 return NULL;
3136 return this_cu->v.quick->file_names;
9291a0cd
TT
3137}
3138
3139/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3140 real path for a given file name from the line table. */
2fdf6df6 3141
9291a0cd 3142static const char *
7b9f3c50
DE
3143dw2_get_real_path (struct objfile *objfile,
3144 struct quick_file_names *qfn, int index)
9291a0cd 3145{
7b9f3c50
DE
3146 if (qfn->real_names == NULL)
3147 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3148 qfn->num_file_names, const char *);
9291a0cd 3149
7b9f3c50 3150 if (qfn->real_names[index] == NULL)
14278e1f 3151 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3152
7b9f3c50 3153 return qfn->real_names[index];
9291a0cd
TT
3154}
3155
3156static struct symtab *
3157dw2_find_last_source_symtab (struct objfile *objfile)
3158{
ed2dc618
SM
3159 struct dwarf2_per_objfile *dwarf2_per_objfile
3160 = get_dwarf2_per_objfile (objfile);
b76e467d 3161 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3162 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3163
43f3e411
DE
3164 if (cust == NULL)
3165 return NULL;
ed2dc618 3166
43f3e411 3167 return compunit_primary_filetab (cust);
9291a0cd
TT
3168}
3169
7b9f3c50
DE
3170/* Traversal function for dw2_forget_cached_source_info. */
3171
3172static int
3173dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3174{
7b9f3c50 3175 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3176
7b9f3c50 3177 if (file_data->real_names)
9291a0cd 3178 {
7b9f3c50 3179 int i;
9291a0cd 3180
7b9f3c50 3181 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3182 {
7b9f3c50
DE
3183 xfree ((void*) file_data->real_names[i]);
3184 file_data->real_names[i] = NULL;
9291a0cd
TT
3185 }
3186 }
7b9f3c50
DE
3187
3188 return 1;
3189}
3190
3191static void
3192dw2_forget_cached_source_info (struct objfile *objfile)
3193{
ed2dc618
SM
3194 struct dwarf2_per_objfile *dwarf2_per_objfile
3195 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3196
5895093f 3197 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3198 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3199}
3200
f8eba3c6
TT
3201/* Helper function for dw2_map_symtabs_matching_filename that expands
3202 the symtabs and calls the iterator. */
3203
3204static int
3205dw2_map_expand_apply (struct objfile *objfile,
3206 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3207 const char *name, const char *real_path,
14bc53a8 3208 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3209{
43f3e411 3210 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3211
3212 /* Don't visit already-expanded CUs. */
43f3e411 3213 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3214 return 0;
3215
3216 /* This may expand more than one symtab, and we want to iterate over
3217 all of them. */
58f0c718 3218 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3219
14bc53a8
PA
3220 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3221 last_made, callback);
f8eba3c6
TT
3222}
3223
3224/* Implementation of the map_symtabs_matching_filename method. */
3225
14bc53a8
PA
3226static bool
3227dw2_map_symtabs_matching_filename
3228 (struct objfile *objfile, const char *name, const char *real_path,
3229 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3230{
c011a4f4 3231 const char *name_basename = lbasename (name);
ed2dc618
SM
3232 struct dwarf2_per_objfile *dwarf2_per_objfile
3233 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3234
848e3e78
DE
3235 /* The rule is CUs specify all the files, including those used by
3236 any TU, so there's no need to scan TUs here. */
f4dc4d17 3237
b76e467d 3238 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3239 {
3d7bb9d9 3240 /* We only need to look at symtabs not already expanded. */
43f3e411 3241 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3242 continue;
3243
b76e467d 3244 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3245 if (file_data == NULL)
9291a0cd
TT
3246 continue;
3247
b76e467d 3248 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3249 {
7b9f3c50 3250 const char *this_name = file_data->file_names[j];
da235a7c 3251 const char *this_real_name;
9291a0cd 3252
af529f8f 3253 if (compare_filenames_for_search (this_name, name))
9291a0cd 3254 {
f5b95b50 3255 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3256 callback))
3257 return true;
288e77a7 3258 continue;
4aac40c8 3259 }
9291a0cd 3260
c011a4f4
DE
3261 /* Before we invoke realpath, which can get expensive when many
3262 files are involved, do a quick comparison of the basenames. */
3263 if (! basenames_may_differ
3264 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3265 continue;
3266
da235a7c
JK
3267 this_real_name = dw2_get_real_path (objfile, file_data, j);
3268 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3269 {
da235a7c 3270 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3271 callback))
3272 return true;
288e77a7 3273 continue;
da235a7c 3274 }
9291a0cd 3275
da235a7c
JK
3276 if (real_path != NULL)
3277 {
af529f8f
JK
3278 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3279 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3280 if (this_real_name != NULL
af529f8f 3281 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3282 {
f5b95b50 3283 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3284 callback))
3285 return true;
288e77a7 3286 continue;
9291a0cd
TT
3287 }
3288 }
3289 }
3290 }
3291
14bc53a8 3292 return false;
9291a0cd
TT
3293}
3294
da51c347
DE
3295/* Struct used to manage iterating over all CUs looking for a symbol. */
3296
3297struct dw2_symtab_iterator
9291a0cd 3298{
ed2dc618
SM
3299 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3300 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3301 /* If set, only look for symbols that match that block. Valid values are
3302 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3303 gdb::optional<block_enum> block_index;
da51c347
DE
3304 /* The kind of symbol we're looking for. */
3305 domain_enum domain;
3306 /* The list of CUs from the index entry of the symbol,
3307 or NULL if not found. */
3308 offset_type *vec;
3309 /* The next element in VEC to look at. */
3310 int next;
3311 /* The number of elements in VEC, or zero if there is no match. */
3312 int length;
8943b874
DE
3313 /* Have we seen a global version of the symbol?
3314 If so we can ignore all further global instances.
3315 This is to work around gold/15646, inefficient gold-generated
3316 indices. */
3317 int global_seen;
da51c347 3318};
9291a0cd 3319
2b79f376 3320/* Initialize the index symtab iterator ITER. */
2fdf6df6 3321
9291a0cd 3322static void
da51c347 3323dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3324 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3325 gdb::optional<block_enum> block_index,
da51c347
DE
3326 domain_enum domain,
3327 const char *name)
3328{
ed2dc618 3329 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3330 iter->block_index = block_index;
3331 iter->domain = domain;
3332 iter->next = 0;
8943b874 3333 iter->global_seen = 0;
da51c347 3334
3063847f 3335 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3336
3337 /* index is NULL if OBJF_READNOW. */
3338 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3339 iter->length = MAYBE_SWAP (*iter->vec);
3340 else
3341 {
3342 iter->vec = NULL;
3343 iter->length = 0;
3344 }
3345}
3346
3347/* Return the next matching CU or NULL if there are no more. */
3348
3349static struct dwarf2_per_cu_data *
3350dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3351{
ed2dc618
SM
3352 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3353
da51c347
DE
3354 for ( ; iter->next < iter->length; ++iter->next)
3355 {
3356 offset_type cu_index_and_attrs =
3357 MAYBE_SWAP (iter->vec[iter->next + 1]);
3358 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3359 gdb_index_symbol_kind symbol_kind =
3360 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3361 /* Only check the symbol attributes if they're present.
3362 Indices prior to version 7 don't record them,
3363 and indices >= 7 may elide them for certain symbols
3364 (gold does this). */
3365 int attrs_valid =
ed2dc618 3366 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3367 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3368
3190f0c6 3369 /* Don't crash on bad data. */
b76e467d 3370 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3371 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3372 {
b98664d3 3373 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3374 " [in module %s]"),
3375 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3376 continue;
3377 }
3378
ff4c9fec 3379 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3380
da51c347 3381 /* Skip if already read in. */
43f3e411 3382 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3383 continue;
3384
8943b874
DE
3385 /* Check static vs global. */
3386 if (attrs_valid)
3387 {
2b79f376
SM
3388 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3389
3390 if (iter->block_index.has_value ())
3391 {
3392 bool want_static = *iter->block_index == STATIC_BLOCK;
3393
3394 if (is_static != want_static)
3395 continue;
3396 }
3397
8943b874
DE
3398 /* Work around gold/15646. */
3399 if (!is_static && iter->global_seen)
3400 continue;
3401 if (!is_static)
3402 iter->global_seen = 1;
3403 }
da51c347
DE
3404
3405 /* Only check the symbol's kind if it has one. */
3406 if (attrs_valid)
3407 {
3408 switch (iter->domain)
3409 {
3410 case VAR_DOMAIN:
3411 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3412 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3413 /* Some types are also in VAR_DOMAIN. */
3414 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3415 continue;
3416 break;
3417 case STRUCT_DOMAIN:
3418 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3419 continue;
3420 break;
3421 case LABEL_DOMAIN:
3422 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3423 continue;
3424 break;
59c35742
AB
3425 case MODULE_DOMAIN:
3426 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3427 continue;
3428 break;
da51c347
DE
3429 default:
3430 break;
3431 }
3432 }
3433
3434 ++iter->next;
3435 return per_cu;
3436 }
3437
3438 return NULL;
3439}
3440
43f3e411 3441static struct compunit_symtab *
c7f839cb 3442dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3443 const char *name, domain_enum domain)
9291a0cd 3444{
43f3e411 3445 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3446 struct dwarf2_per_objfile *dwarf2_per_objfile
3447 = get_dwarf2_per_objfile (objfile);
9291a0cd 3448
b5ec771e
PA
3449 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3450
ed2dc618
SM
3451 struct dw2_symtab_iterator iter;
3452 struct dwarf2_per_cu_data *per_cu;
da51c347 3453
2b79f376 3454 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3455
ed2dc618
SM
3456 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3457 {
3458 struct symbol *sym, *with_opaque = NULL;
58f0c718 3459 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3460 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3461 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3462
ed2dc618
SM
3463 sym = block_find_symbol (block, name, domain,
3464 block_find_non_opaque_type_preferred,
3465 &with_opaque);
b2e2f908 3466
ed2dc618
SM
3467 /* Some caution must be observed with overloaded functions
3468 and methods, since the index will not contain any overload
3469 information (but NAME might contain it). */
da51c347 3470
ed2dc618
SM
3471 if (sym != NULL
3472 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3473 return stab;
3474 if (with_opaque != NULL
3475 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3476 stab_best = stab;
da51c347 3477
ed2dc618 3478 /* Keep looking through other CUs. */
9291a0cd 3479 }
9291a0cd 3480
da51c347 3481 return stab_best;
9291a0cd
TT
3482}
3483
3484static void
3485dw2_print_stats (struct objfile *objfile)
3486{
ed2dc618
SM
3487 struct dwarf2_per_objfile *dwarf2_per_objfile
3488 = get_dwarf2_per_objfile (objfile);
b76e467d 3489 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3490 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3491 int count = 0;
9291a0cd 3492
ed2dc618 3493 for (int i = 0; i < total; ++i)
9291a0cd 3494 {
ff4c9fec 3495 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3496
43f3e411 3497 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3498 ++count;
3499 }
e4a48d9d 3500 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3501 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3502}
3503
779bd270
DE
3504/* This dumps minimal information about the index.
3505 It is called via "mt print objfiles".
3506 One use is to verify .gdb_index has been loaded by the
3507 gdb.dwarf2/gdb-index.exp testcase. */
3508
9291a0cd
TT
3509static void
3510dw2_dump (struct objfile *objfile)
3511{
ed2dc618
SM
3512 struct dwarf2_per_objfile *dwarf2_per_objfile
3513 = get_dwarf2_per_objfile (objfile);
3514
779bd270
DE
3515 gdb_assert (dwarf2_per_objfile->using_index);
3516 printf_filtered (".gdb_index:");
3517 if (dwarf2_per_objfile->index_table != NULL)
3518 {
3519 printf_filtered (" version %d\n",
3520 dwarf2_per_objfile->index_table->version);
3521 }
3522 else
3523 printf_filtered (" faked for \"readnow\"\n");
3524 printf_filtered ("\n");
9291a0cd
TT
3525}
3526
9291a0cd
TT
3527static void
3528dw2_expand_symtabs_for_function (struct objfile *objfile,
3529 const char *func_name)
3530{
ed2dc618
SM
3531 struct dwarf2_per_objfile *dwarf2_per_objfile
3532 = get_dwarf2_per_objfile (objfile);
da51c347 3533
ed2dc618
SM
3534 struct dw2_symtab_iterator iter;
3535 struct dwarf2_per_cu_data *per_cu;
da51c347 3536
2b79f376 3537 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3538
ed2dc618 3539 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3540 dw2_instantiate_symtab (per_cu, false);
da51c347 3541
9291a0cd
TT
3542}
3543
3544static void
3545dw2_expand_all_symtabs (struct objfile *objfile)
3546{
ed2dc618
SM
3547 struct dwarf2_per_objfile *dwarf2_per_objfile
3548 = get_dwarf2_per_objfile (objfile);
b76e467d 3549 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3550 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3551
ed2dc618 3552 for (int i = 0; i < total_units; ++i)
9291a0cd 3553 {
ff4c9fec 3554 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3555
58f0c718
TT
3556 /* We don't want to directly expand a partial CU, because if we
3557 read it with the wrong language, then assertion failures can
3558 be triggered later on. See PR symtab/23010. So, tell
3559 dw2_instantiate_symtab to skip partial CUs -- any important
3560 partial CU will be read via DW_TAG_imported_unit anyway. */
3561 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3562 }
3563}
3564
3565static void
652a8996
JK
3566dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3567 const char *fullname)
9291a0cd 3568{
ed2dc618
SM
3569 struct dwarf2_per_objfile *dwarf2_per_objfile
3570 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3571
3572 /* We don't need to consider type units here.
3573 This is only called for examining code, e.g. expand_line_sal.
3574 There can be an order of magnitude (or more) more type units
3575 than comp units, and we avoid them if we can. */
3576
b76e467d 3577 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3578 {
3d7bb9d9 3579 /* We only need to look at symtabs not already expanded. */
43f3e411 3580 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3581 continue;
3582
b76e467d 3583 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3584 if (file_data == NULL)
9291a0cd
TT
3585 continue;
3586
b76e467d 3587 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3588 {
652a8996
JK
3589 const char *this_fullname = file_data->file_names[j];
3590
3591 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3592 {
58f0c718 3593 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3594 break;
3595 }
3596 }
3597 }
3598}
3599
9291a0cd 3600static void
199b4314
TT
3601dw2_map_matching_symbols
3602 (struct objfile *objfile,
b054970d 3603 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3604 int global,
3605 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3606 symbol_compare_ftype *ordered_compare)
9291a0cd 3607{
40658b94 3608 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3609 current language is Ada for a non-Ada objfile using GNU index. As Ada
3610 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3611}
3612
e1ef7d7a
PA
3613/* Starting from a search name, return the string that finds the upper
3614 bound of all strings that start with SEARCH_NAME in a sorted name
3615 list. Returns the empty string to indicate that the upper bound is
3616 the end of the list. */
3617
3618static std::string
3619make_sort_after_prefix_name (const char *search_name)
3620{
3621 /* When looking to complete "func", we find the upper bound of all
3622 symbols that start with "func" by looking for where we'd insert
3623 the closest string that would follow "func" in lexicographical
3624 order. Usually, that's "func"-with-last-character-incremented,
3625 i.e. "fund". Mind non-ASCII characters, though. Usually those
3626 will be UTF-8 multi-byte sequences, but we can't be certain.
3627 Especially mind the 0xff character, which is a valid character in
3628 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3629 rule out compilers allowing it in identifiers. Note that
3630 conveniently, strcmp/strcasecmp are specified to compare
3631 characters interpreted as unsigned char. So what we do is treat
3632 the whole string as a base 256 number composed of a sequence of
3633 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3634 to 0, and carries 1 to the following more-significant position.
3635 If the very first character in SEARCH_NAME ends up incremented
3636 and carries/overflows, then the upper bound is the end of the
3637 list. The string after the empty string is also the empty
3638 string.
3639
3640 Some examples of this operation:
3641
3642 SEARCH_NAME => "+1" RESULT
3643
3644 "abc" => "abd"
3645 "ab\xff" => "ac"
3646 "\xff" "a" "\xff" => "\xff" "b"
3647 "\xff" => ""
3648 "\xff\xff" => ""
3649 "" => ""
3650
3651 Then, with these symbols for example:
3652
3653 func
3654 func1
3655 fund
3656
3657 completing "func" looks for symbols between "func" and
3658 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3659 which finds "func" and "func1", but not "fund".
3660
3661 And with:
3662
3663 funcÿ (Latin1 'ÿ' [0xff])
3664 funcÿ1
3665 fund
3666
3667 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3668 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3669
3670 And with:
3671
3672 ÿÿ (Latin1 'ÿ' [0xff])
3673 ÿÿ1
3674
3675 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3676 the end of the list.
3677 */
3678 std::string after = search_name;
3679 while (!after.empty () && (unsigned char) after.back () == 0xff)
3680 after.pop_back ();
3681 if (!after.empty ())
3682 after.back () = (unsigned char) after.back () + 1;
3683 return after;
3684}
3685
5c58de74 3686/* See declaration. */
61d96d7e 3687
5c58de74
PA
3688std::pair<std::vector<name_component>::const_iterator,
3689 std::vector<name_component>::const_iterator>
44ed8f3e 3690mapped_index_base::find_name_components_bounds
3b00ef10 3691 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3692{
5c58de74
PA
3693 auto *name_cmp
3694 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3695
3b00ef10
TT
3696 const char *lang_name
3697 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3698
3f563c84
PA
3699 /* Comparison function object for lower_bound that matches against a
3700 given symbol name. */
3701 auto lookup_compare_lower = [&] (const name_component &elem,
3702 const char *name)
3703 {
5c58de74 3704 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3705 const char *elem_name = elem_qualified + elem.name_offset;
3706 return name_cmp (elem_name, name) < 0;
3707 };
3708
3709 /* Comparison function object for upper_bound that matches against a
3710 given symbol name. */
3711 auto lookup_compare_upper = [&] (const char *name,
3712 const name_component &elem)
3713 {
5c58de74 3714 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3715 const char *elem_name = elem_qualified + elem.name_offset;
3716 return name_cmp (name, elem_name) < 0;
3717 };
3718
5c58de74
PA
3719 auto begin = this->name_components.begin ();
3720 auto end = this->name_components.end ();
3f563c84
PA
3721
3722 /* Find the lower bound. */
3723 auto lower = [&] ()
3724 {
3b00ef10 3725 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3726 return begin;
3727 else
3b00ef10 3728 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3729 } ();
3730
3731 /* Find the upper bound. */
3732 auto upper = [&] ()
3733 {
5c58de74 3734 if (lookup_name_without_params.completion_mode ())
3f563c84 3735 {
e1ef7d7a
PA
3736 /* In completion mode, we want UPPER to point past all
3737 symbols names that have the same prefix. I.e., with
3738 these symbols, and completing "func":
3739
3740 function << lower bound
3741 function1
3742 other_function << upper bound
3743
3744 We find the upper bound by looking for the insertion
3745 point of "func"-with-last-character-incremented,
3746 i.e. "fund". */
3b00ef10 3747 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3748 if (after.empty ())
3f563c84 3749 return end;
e6b2f5ef
PA
3750 return std::lower_bound (lower, end, after.c_str (),
3751 lookup_compare_lower);
3f563c84
PA
3752 }
3753 else
3b00ef10 3754 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3755 } ();
3756
5c58de74
PA
3757 return {lower, upper};
3758}
3759
3760/* See declaration. */
3761
3762void
44ed8f3e 3763mapped_index_base::build_name_components ()
5c58de74
PA
3764{
3765 if (!this->name_components.empty ())
3766 return;
3767
3768 this->name_components_casing = case_sensitivity;
3769 auto *name_cmp
3770 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3771
3772 /* The code below only knows how to break apart components of C++
3773 symbol names (and other languages that use '::' as
3b00ef10 3774 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3775 auto count = this->symbol_name_count ();
3776 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3777 {
44ed8f3e 3778 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3779 continue;
3780
3781 const char *name = this->symbol_name_at (idx);
3782
3783 /* Add each name component to the name component table. */
3784 unsigned int previous_len = 0;
3b00ef10
TT
3785
3786 if (strstr (name, "::") != nullptr)
3787 {
3788 for (unsigned int current_len = cp_find_first_component (name);
3789 name[current_len] != '\0';
3790 current_len += cp_find_first_component (name + current_len))
3791 {
3792 gdb_assert (name[current_len] == ':');
3793 this->name_components.push_back ({previous_len, idx});
3794 /* Skip the '::'. */
3795 current_len += 2;
3796 previous_len = current_len;
3797 }
3798 }
3799 else
5c58de74 3800 {
3b00ef10
TT
3801 /* Handle the Ada encoded (aka mangled) form here. */
3802 for (const char *iter = strstr (name, "__");
3803 iter != nullptr;
3804 iter = strstr (iter, "__"))
3805 {
3806 this->name_components.push_back ({previous_len, idx});
3807 iter += 2;
3808 previous_len = iter - name;
3809 }
5c58de74 3810 }
3b00ef10 3811
5c58de74
PA
3812 this->name_components.push_back ({previous_len, idx});
3813 }
3814
3815 /* Sort name_components elements by name. */
3816 auto name_comp_compare = [&] (const name_component &left,
3817 const name_component &right)
3818 {
3819 const char *left_qualified = this->symbol_name_at (left.idx);
3820 const char *right_qualified = this->symbol_name_at (right.idx);
3821
3822 const char *left_name = left_qualified + left.name_offset;
3823 const char *right_name = right_qualified + right.name_offset;
3824
3825 return name_cmp (left_name, right_name) < 0;
3826 };
3827
3828 std::sort (this->name_components.begin (),
3829 this->name_components.end (),
3830 name_comp_compare);
3831}
3832
3833/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3834 mapped_index_base instead of the containing objfile. This is split
3835 to a separate function in order to be able to unit test the
3836 name_components matching using a mock mapped_index_base. For each
5c58de74 3837 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3838 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3839
3840static void
3841dw2_expand_symtabs_matching_symbol
44ed8f3e 3842 (mapped_index_base &index,
5c58de74
PA
3843 const lookup_name_info &lookup_name_in,
3844 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3845 enum search_domain kind,
3b00ef10 3846 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3847{
3848 lookup_name_info lookup_name_without_params
3849 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3850
3851 /* Build the symbol name component sorted vector, if we haven't
3852 yet. */
3853 index.build_name_components ();
3854
3f563c84
PA
3855 /* The same symbol may appear more than once in the range though.
3856 E.g., if we're looking for symbols that complete "w", and we have
3857 a symbol named "w1::w2", we'll find the two name components for
3858 that same symbol in the range. To be sure we only call the
3859 callback once per symbol, we first collect the symbol name
3860 indexes that matched in a temporary vector and ignore
3861 duplicates. */
3862 std::vector<offset_type> matches;
3f563c84 3863
3b00ef10
TT
3864 struct name_and_matcher
3865 {
3866 symbol_name_matcher_ftype *matcher;
3867 const std::string &name;
3868
3869 bool operator== (const name_and_matcher &other) const
3f563c84 3870 {
3b00ef10
TT
3871 return matcher == other.matcher && name == other.name;
3872 }
3873 };
3874
3875 /* A vector holding all the different symbol name matchers, for all
3876 languages. */
3877 std::vector<name_and_matcher> matchers;
3878
3879 for (int i = 0; i < nr_languages; i++)
3880 {
3881 enum language lang_e = (enum language) i;
3882
3883 const language_defn *lang = language_def (lang_e);
3884 symbol_name_matcher_ftype *name_matcher
3885 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3886
3b00ef10
TT
3887 name_and_matcher key {
3888 name_matcher,
3889 lookup_name_without_params.language_lookup_name (lang_e)
3890 };
3891
3892 /* Don't insert the same comparison routine more than once.
3893 Note that we do this linear walk. This is not a problem in
3894 practice because the number of supported languages is
3895 low. */
3896 if (std::find (matchers.begin (), matchers.end (), key)
3897 != matchers.end ())
9291a0cd 3898 continue;
3b00ef10
TT
3899 matchers.push_back (std::move (key));
3900
3901 auto bounds
3902 = index.find_name_components_bounds (lookup_name_without_params,
3903 lang_e);
3904
3905 /* Now for each symbol name in range, check to see if we have a name
3906 match, and if so, call the MATCH_CALLBACK callback. */
3907
3908 for (; bounds.first != bounds.second; ++bounds.first)
3909 {
3910 const char *qualified = index.symbol_name_at (bounds.first->idx);
3911
3912 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3913 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3914 continue;
9291a0cd 3915
3b00ef10
TT
3916 matches.push_back (bounds.first->idx);
3917 }
3f563c84
PA
3918 }
3919
3920 std::sort (matches.begin (), matches.end ());
3921
3922 /* Finally call the callback, once per match. */
3923 ULONGEST prev = -1;
3924 for (offset_type idx : matches)
3925 {
3926 if (prev != idx)
3927 {
3b00ef10
TT
3928 if (!match_callback (idx))
3929 break;
3f563c84
PA
3930 prev = idx;
3931 }
3932 }
3933
3934 /* Above we use a type wider than idx's for 'prev', since 0 and
3935 (offset_type)-1 are both possible values. */
3936 static_assert (sizeof (prev) > sizeof (offset_type), "");
3937}
3938
c62446b1
PA
3939#if GDB_SELF_TEST
3940
3941namespace selftests { namespace dw2_expand_symtabs_matching {
3942
a3c5fafd
PA
3943/* A mock .gdb_index/.debug_names-like name index table, enough to
3944 exercise dw2_expand_symtabs_matching_symbol, which works with the
3945 mapped_index_base interface. Builds an index from the symbol list
3946 passed as parameter to the constructor. */
3947class mock_mapped_index : public mapped_index_base
c62446b1
PA
3948{
3949public:
a3c5fafd
PA
3950 mock_mapped_index (gdb::array_view<const char *> symbols)
3951 : m_symbol_table (symbols)
c62446b1
PA
3952 {}
3953
a3c5fafd 3954 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3955
a3c5fafd 3956 /* Return the number of names in the symbol table. */
632e107b 3957 size_t symbol_name_count () const override
c62446b1 3958 {
a3c5fafd 3959 return m_symbol_table.size ();
c62446b1
PA
3960 }
3961
a3c5fafd 3962 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 3963 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
3964 {
3965 return m_symbol_table[idx];
3966 }
c62446b1 3967
a3c5fafd
PA
3968private:
3969 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3970};
3971
3972/* Convenience function that converts a NULL pointer to a "<null>"
3973 string, to pass to print routines. */
3974
3975static const char *
3976string_or_null (const char *str)
3977{
3978 return str != NULL ? str : "<null>";
3979}
3980
3981/* Check if a lookup_name_info built from
3982 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3983 index. EXPECTED_LIST is the list of expected matches, in expected
3984 matching order. If no match expected, then an empty list is
3985 specified. Returns true on success. On failure prints a warning
3986 indicating the file:line that failed, and returns false. */
3987
3988static bool
3989check_match (const char *file, int line,
3990 mock_mapped_index &mock_index,
3991 const char *name, symbol_name_match_type match_type,
3992 bool completion_mode,
3993 std::initializer_list<const char *> expected_list)
3994{
3995 lookup_name_info lookup_name (name, match_type, completion_mode);
3996
3997 bool matched = true;
3998
3999 auto mismatch = [&] (const char *expected_str,
4000 const char *got)
4001 {
4002 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4003 "expected=\"%s\", got=\"%s\"\n"),
4004 file, line,
4005 (match_type == symbol_name_match_type::FULL
4006 ? "FULL" : "WILD"),
4007 name, string_or_null (expected_str), string_or_null (got));
4008 matched = false;
4009 };
4010
4011 auto expected_it = expected_list.begin ();
4012 auto expected_end = expected_list.end ();
4013
a3c5fafd 4014 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4015 NULL, ALL_DOMAIN,
4016 [&] (offset_type idx)
4017 {
a3c5fafd 4018 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4019 const char *expected_str
4020 = expected_it == expected_end ? NULL : *expected_it++;
4021
4022 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4023 mismatch (expected_str, matched_name);
3b00ef10 4024 return true;
c62446b1
PA
4025 });
4026
4027 const char *expected_str
4028 = expected_it == expected_end ? NULL : *expected_it++;
4029 if (expected_str != NULL)
4030 mismatch (expected_str, NULL);
4031
4032 return matched;
4033}
4034
4035/* The symbols added to the mock mapped_index for testing (in
4036 canonical form). */
4037static const char *test_symbols[] = {
4038 "function",
4039 "std::bar",
4040 "std::zfunction",
4041 "std::zfunction2",
4042 "w1::w2",
4043 "ns::foo<char*>",
4044 "ns::foo<int>",
4045 "ns::foo<long>",
a20714ff
PA
4046 "ns2::tmpl<int>::foo2",
4047 "(anonymous namespace)::A::B::C",
c62446b1 4048
e1ef7d7a
PA
4049 /* These are used to check that the increment-last-char in the
4050 matching algorithm for completion doesn't match "t1_fund" when
4051 completing "t1_func". */
4052 "t1_func",
4053 "t1_func1",
4054 "t1_fund",
4055 "t1_fund1",
4056
4057 /* A UTF-8 name with multi-byte sequences to make sure that
4058 cp-name-parser understands this as a single identifier ("função"
4059 is "function" in PT). */
4060 u8"u8função",
4061
4062 /* \377 (0xff) is Latin1 'ÿ'. */
4063 "yfunc\377",
4064
4065 /* \377 (0xff) is Latin1 'ÿ'. */
4066 "\377",
4067 "\377\377123",
4068
c62446b1
PA
4069 /* A name with all sorts of complications. Starts with "z" to make
4070 it easier for the completion tests below. */
4071#define Z_SYM_NAME \
4072 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4073 "::tuple<(anonymous namespace)::ui*, " \
4074 "std::default_delete<(anonymous namespace)::ui>, void>"
4075
4076 Z_SYM_NAME
4077};
4078
a3c5fafd
PA
4079/* Returns true if the mapped_index_base::find_name_component_bounds
4080 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4081 in completion mode. */
5c58de74
PA
4082
4083static bool
a3c5fafd 4084check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4085 const char *search_name,
4086 gdb::array_view<const char *> expected_syms)
4087{
4088 lookup_name_info lookup_name (search_name,
4089 symbol_name_match_type::FULL, true);
4090
3b00ef10
TT
4091 auto bounds = index.find_name_components_bounds (lookup_name,
4092 language_cplus);
5c58de74
PA
4093
4094 size_t distance = std::distance (bounds.first, bounds.second);
4095 if (distance != expected_syms.size ())
4096 return false;
4097
4098 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4099 {
4100 auto nc_elem = bounds.first + exp_elem;
4101 const char *qualified = index.symbol_name_at (nc_elem->idx);
4102 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4103 return false;
4104 }
4105
4106 return true;
4107}
4108
4109/* Test the lower-level mapped_index::find_name_component_bounds
4110 method. */
4111
c62446b1 4112static void
5c58de74
PA
4113test_mapped_index_find_name_component_bounds ()
4114{
4115 mock_mapped_index mock_index (test_symbols);
4116
a3c5fafd 4117 mock_index.build_name_components ();
5c58de74
PA
4118
4119 /* Test the lower-level mapped_index::find_name_component_bounds
4120 method in completion mode. */
4121 {
4122 static const char *expected_syms[] = {
4123 "t1_func",
4124 "t1_func1",
5c58de74
PA
4125 };
4126
a3c5fafd 4127 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4128 "t1_func", expected_syms));
4129 }
4130
4131 /* Check that the increment-last-char in the name matching algorithm
4132 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4133 {
4134 static const char *expected_syms1[] = {
4135 "\377",
4136 "\377\377123",
4137 };
a3c5fafd 4138 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4139 "\377", expected_syms1));
4140
4141 static const char *expected_syms2[] = {
4142 "\377\377123",
4143 };
a3c5fafd 4144 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4145 "\377\377", expected_syms2));
4146 }
4147}
4148
4149/* Test dw2_expand_symtabs_matching_symbol. */
4150
4151static void
4152test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4153{
4154 mock_mapped_index mock_index (test_symbols);
4155
4156 /* We let all tests run until the end even if some fails, for debug
4157 convenience. */
4158 bool any_mismatch = false;
4159
4160 /* Create the expected symbols list (an initializer_list). Needed
4161 because lists have commas, and we need to pass them to CHECK,
4162 which is a macro. */
4163#define EXPECT(...) { __VA_ARGS__ }
4164
4165 /* Wrapper for check_match that passes down the current
4166 __FILE__/__LINE__. */
4167#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4168 any_mismatch |= !check_match (__FILE__, __LINE__, \
4169 mock_index, \
4170 NAME, MATCH_TYPE, COMPLETION_MODE, \
4171 EXPECTED_LIST)
4172
4173 /* Identity checks. */
4174 for (const char *sym : test_symbols)
4175 {
4176 /* Should be able to match all existing symbols. */
4177 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4178 EXPECT (sym));
4179
4180 /* Should be able to match all existing symbols with
4181 parameters. */
4182 std::string with_params = std::string (sym) + "(int)";
4183 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4184 EXPECT (sym));
4185
4186 /* Should be able to match all existing symbols with
4187 parameters and qualifiers. */
4188 with_params = std::string (sym) + " ( int ) const";
4189 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4190 EXPECT (sym));
4191
4192 /* This should really find sym, but cp-name-parser.y doesn't
4193 know about lvalue/rvalue qualifiers yet. */
4194 with_params = std::string (sym) + " ( int ) &&";
4195 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4196 {});
4197 }
4198
e1ef7d7a
PA
4199 /* Check that the name matching algorithm for completion doesn't get
4200 confused with Latin1 'ÿ' / 0xff. */
4201 {
4202 static const char str[] = "\377";
4203 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4204 EXPECT ("\377", "\377\377123"));
4205 }
4206
4207 /* Check that the increment-last-char in the matching algorithm for
4208 completion doesn't match "t1_fund" when completing "t1_func". */
4209 {
4210 static const char str[] = "t1_func";
4211 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4212 EXPECT ("t1_func", "t1_func1"));
4213 }
4214
c62446b1
PA
4215 /* Check that completion mode works at each prefix of the expected
4216 symbol name. */
4217 {
4218 static const char str[] = "function(int)";
4219 size_t len = strlen (str);
4220 std::string lookup;
4221
4222 for (size_t i = 1; i < len; i++)
4223 {
4224 lookup.assign (str, i);
4225 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4226 EXPECT ("function"));
4227 }
4228 }
4229
4230 /* While "w" is a prefix of both components, the match function
4231 should still only be called once. */
4232 {
4233 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4234 EXPECT ("w1::w2"));
a20714ff
PA
4235 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4236 EXPECT ("w1::w2"));
c62446b1
PA
4237 }
4238
4239 /* Same, with a "complicated" symbol. */
4240 {
4241 static const char str[] = Z_SYM_NAME;
4242 size_t len = strlen (str);
4243 std::string lookup;
4244
4245 for (size_t i = 1; i < len; i++)
4246 {
4247 lookup.assign (str, i);
4248 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4249 EXPECT (Z_SYM_NAME));
4250 }
4251 }
4252
4253 /* In FULL mode, an incomplete symbol doesn't match. */
4254 {
4255 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4256 {});
4257 }
4258
4259 /* A complete symbol with parameters matches any overload, since the
4260 index has no overload info. */
4261 {
4262 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4263 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4264 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4265 EXPECT ("std::zfunction", "std::zfunction2"));
4266 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4267 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4268 }
4269
4270 /* Check that whitespace is ignored appropriately. A symbol with a
4271 template argument list. */
4272 {
4273 static const char expected[] = "ns::foo<int>";
4274 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4275 EXPECT (expected));
a20714ff
PA
4276 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4277 EXPECT (expected));
c62446b1
PA
4278 }
4279
4280 /* Check that whitespace is ignored appropriately. A symbol with a
4281 template argument list that includes a pointer. */
4282 {
4283 static const char expected[] = "ns::foo<char*>";
4284 /* Try both completion and non-completion modes. */
4285 static const bool completion_mode[2] = {false, true};
4286 for (size_t i = 0; i < 2; i++)
4287 {
4288 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4289 completion_mode[i], EXPECT (expected));
a20714ff
PA
4290 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4291 completion_mode[i], EXPECT (expected));
c62446b1
PA
4292
4293 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4294 completion_mode[i], EXPECT (expected));
a20714ff
PA
4295 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4296 completion_mode[i], EXPECT (expected));
c62446b1
PA
4297 }
4298 }
4299
4300 {
4301 /* Check method qualifiers are ignored. */
4302 static const char expected[] = "ns::foo<char*>";
4303 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4304 symbol_name_match_type::FULL, true, EXPECT (expected));
4305 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4306 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4307 CHECK_MATCH ("foo < char * > ( int ) const",
4308 symbol_name_match_type::WILD, true, EXPECT (expected));
4309 CHECK_MATCH ("foo < char * > ( int ) &&",
4310 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4311 }
4312
4313 /* Test lookup names that don't match anything. */
4314 {
a20714ff
PA
4315 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4316 {});
4317
c62446b1
PA
4318 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4319 {});
4320 }
4321
a20714ff
PA
4322 /* Some wild matching tests, exercising "(anonymous namespace)",
4323 which should not be confused with a parameter list. */
4324 {
4325 static const char *syms[] = {
4326 "A::B::C",
4327 "B::C",
4328 "C",
4329 "A :: B :: C ( int )",
4330 "B :: C ( int )",
4331 "C ( int )",
4332 };
4333
4334 for (const char *s : syms)
4335 {
4336 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4337 EXPECT ("(anonymous namespace)::A::B::C"));
4338 }
4339 }
4340
4341 {
4342 static const char expected[] = "ns2::tmpl<int>::foo2";
4343 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4344 EXPECT (expected));
4345 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4346 EXPECT (expected));
4347 }
4348
c62446b1
PA
4349 SELF_CHECK (!any_mismatch);
4350
4351#undef EXPECT
4352#undef CHECK_MATCH
4353}
4354
5c58de74
PA
4355static void
4356run_test ()
4357{
4358 test_mapped_index_find_name_component_bounds ();
4359 test_dw2_expand_symtabs_matching_symbol ();
4360}
4361
c62446b1
PA
4362}} // namespace selftests::dw2_expand_symtabs_matching
4363
4364#endif /* GDB_SELF_TEST */
4365
4b514bc8
JK
4366/* If FILE_MATCHER is NULL or if PER_CU has
4367 dwarf2_per_cu_quick_data::MARK set (see
4368 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4369 EXPANSION_NOTIFY on it. */
4370
4371static void
4372dw2_expand_symtabs_matching_one
4373 (struct dwarf2_per_cu_data *per_cu,
4374 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4375 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4376{
4377 if (file_matcher == NULL || per_cu->v.quick->mark)
4378 {
4379 bool symtab_was_null
4380 = (per_cu->v.quick->compunit_symtab == NULL);
4381
58f0c718 4382 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4383
4384 if (expansion_notify != NULL
4385 && symtab_was_null
4386 && per_cu->v.quick->compunit_symtab != NULL)
4387 expansion_notify (per_cu->v.quick->compunit_symtab);
4388 }
4389}
4390
3f563c84
PA
4391/* Helper for dw2_expand_matching symtabs. Called on each symbol
4392 matched, to expand corresponding CUs that were marked. IDX is the
4393 index of the symbol name that matched. */
4394
4395static void
4396dw2_expand_marked_cus
ed2dc618 4397 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4398 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4399 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4400 search_domain kind)
4401{
3f563c84
PA
4402 offset_type *vec, vec_len, vec_idx;
4403 bool global_seen = false;
ed2dc618 4404 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4405
61920122 4406 vec = (offset_type *) (index.constant_pool
f00a2de2 4407 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4408 vec_len = MAYBE_SWAP (vec[0]);
4409 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4410 {
61920122
PA
4411 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4412 /* This value is only valid for index versions >= 7. */
4413 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4414 gdb_index_symbol_kind symbol_kind =
4415 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4416 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4417 /* Only check the symbol attributes if they're present.
4418 Indices prior to version 7 don't record them,
4419 and indices >= 7 may elide them for certain symbols
4420 (gold does this). */
4421 int attrs_valid =
4422 (index.version >= 7
4423 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4424
4425 /* Work around gold/15646. */
4426 if (attrs_valid)
9291a0cd 4427 {
61920122
PA
4428 if (!is_static && global_seen)
4429 continue;
4430 if (!is_static)
4431 global_seen = true;
4432 }
3190f0c6 4433
61920122
PA
4434 /* Only check the symbol's kind if it has one. */
4435 if (attrs_valid)
4436 {
4437 switch (kind)
8943b874 4438 {
61920122
PA
4439 case VARIABLES_DOMAIN:
4440 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4441 continue;
4442 break;
4443 case FUNCTIONS_DOMAIN:
4444 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4445 continue;
61920122
PA
4446 break;
4447 case TYPES_DOMAIN:
4448 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4449 continue;
4450 break;
59c35742
AB
4451 case MODULES_DOMAIN:
4452 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4453 continue;
4454 break;
61920122
PA
4455 default:
4456 break;
8943b874 4457 }
61920122 4458 }
8943b874 4459
61920122 4460 /* Don't crash on bad data. */
b76e467d 4461 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4462 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4463 {
b98664d3 4464 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4465 " [in module %s]"),
4466 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4467 continue;
4468 }
4469
ff4c9fec 4470 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4471 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4472 expansion_notify);
61920122
PA
4473 }
4474}
4475
4b514bc8
JK
4476/* If FILE_MATCHER is non-NULL, set all the
4477 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4478 that match FILE_MATCHER. */
4479
61920122 4480static void
4b514bc8 4481dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4482 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4483 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4484{
4b514bc8 4485 if (file_matcher == NULL)
61920122
PA
4486 return;
4487
4b514bc8
JK
4488 objfile *const objfile = dwarf2_per_objfile->objfile;
4489
4490 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4491 htab_eq_pointer,
4492 NULL, xcalloc, xfree));
4493 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4494 htab_eq_pointer,
4495 NULL, xcalloc, xfree));
61920122 4496
4b514bc8
JK
4497 /* The rule is CUs specify all the files, including those used by
4498 any TU, so there's no need to scan TUs here. */
61920122 4499
b76e467d 4500 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4501 {
927aa2e7
JK
4502 QUIT;
4503
4504 per_cu->v.quick->mark = 0;
4505
4506 /* We only need to look at symtabs not already expanded. */
4507 if (per_cu->v.quick->compunit_symtab)
4508 continue;
4509
b76e467d 4510 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4511 if (file_data == NULL)
4512 continue;
4513
4514 if (htab_find (visited_not_found.get (), file_data) != NULL)
4515 continue;
4516 else if (htab_find (visited_found.get (), file_data) != NULL)
4517 {
4518 per_cu->v.quick->mark = 1;
4519 continue;
4520 }
4521
b76e467d 4522 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4523 {
4524 const char *this_real_name;
4525
4526 if (file_matcher (file_data->file_names[j], false))
4527 {
4528 per_cu->v.quick->mark = 1;
4529 break;
4530 }
4531
4532 /* Before we invoke realpath, which can get expensive when many
4533 files are involved, do a quick comparison of the basenames. */
4534 if (!basenames_may_differ
4535 && !file_matcher (lbasename (file_data->file_names[j]),
4536 true))
4537 continue;
4538
4539 this_real_name = dw2_get_real_path (objfile, file_data, j);
4540 if (file_matcher (this_real_name, false))
4541 {
4542 per_cu->v.quick->mark = 1;
4543 break;
4544 }
4545 }
4546
b76e467d
SM
4547 void **slot = htab_find_slot (per_cu->v.quick->mark
4548 ? visited_found.get ()
4549 : visited_not_found.get (),
4550 file_data, INSERT);
927aa2e7
JK
4551 *slot = file_data;
4552 }
4553}
4554
4555static void
4556dw2_expand_symtabs_matching
4557 (struct objfile *objfile,
4558 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4559 const lookup_name_info &lookup_name,
4560 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4561 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4562 enum search_domain kind)
4563{
ed2dc618
SM
4564 struct dwarf2_per_objfile *dwarf2_per_objfile
4565 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4566
4567 /* index_table is NULL if OBJF_READNOW. */
4568 if (!dwarf2_per_objfile->index_table)
4569 return;
4570
ed2dc618 4571 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4572
4573 mapped_index &index = *dwarf2_per_objfile->index_table;
4574
4575 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4576 symbol_matcher,
4577 kind, [&] (offset_type idx)
4578 {
ed2dc618 4579 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4580 expansion_notify, kind);
3b00ef10 4581 return true;
927aa2e7
JK
4582 });
4583}
4584
4585/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4586 symtab. */
4587
4588static struct compunit_symtab *
4589recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4590 CORE_ADDR pc)
4591{
4592 int i;
4593
4594 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4595 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4596 return cust;
4597
4598 if (cust->includes == NULL)
4599 return NULL;
4600
4601 for (i = 0; cust->includes[i]; ++i)
4602 {
4603 struct compunit_symtab *s = cust->includes[i];
4604
4605 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4606 if (s != NULL)
4607 return s;
4608 }
4609
4610 return NULL;
4611}
4612
4613static struct compunit_symtab *
4614dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4615 struct bound_minimal_symbol msymbol,
4616 CORE_ADDR pc,
4617 struct obj_section *section,
4618 int warn_if_readin)
4619{
4620 struct dwarf2_per_cu_data *data;
4621 struct compunit_symtab *result;
4622
d320c2b5 4623 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4624 return NULL;
4625
b3b3bada 4626 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4627 data = (struct dwarf2_per_cu_data *) addrmap_find
4628 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4629 if (!data)
4630 return NULL;
4631
4632 if (warn_if_readin && data->v.quick->compunit_symtab)
4633 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4634 paddress (get_objfile_arch (objfile), pc));
4635
4636 result
58f0c718
TT
4637 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4638 false),
927aa2e7
JK
4639 pc);
4640 gdb_assert (result != NULL);
4641 return result;
4642}
4643
4644static void
4645dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4646 void *data, int need_fullname)
4647{
ed2dc618
SM
4648 struct dwarf2_per_objfile *dwarf2_per_objfile
4649 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4650
4651 if (!dwarf2_per_objfile->filenames_cache)
4652 {
4653 dwarf2_per_objfile->filenames_cache.emplace ();
4654
4655 htab_up visited (htab_create_alloc (10,
4656 htab_hash_pointer, htab_eq_pointer,
4657 NULL, xcalloc, xfree));
4658
4659 /* The rule is CUs specify all the files, including those used
4660 by any TU, so there's no need to scan TUs here. We can
4661 ignore file names coming from already-expanded CUs. */
4662
b76e467d 4663 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4664 {
927aa2e7
JK
4665 if (per_cu->v.quick->compunit_symtab)
4666 {
4667 void **slot = htab_find_slot (visited.get (),
4668 per_cu->v.quick->file_names,
4669 INSERT);
4670
4671 *slot = per_cu->v.quick->file_names;
4672 }
4673 }
4674
b76e467d 4675 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4676 {
927aa2e7
JK
4677 /* We only need to look at symtabs not already expanded. */
4678 if (per_cu->v.quick->compunit_symtab)
4679 continue;
4680
b76e467d 4681 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4682 if (file_data == NULL)
4683 continue;
4684
b76e467d 4685 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4686 if (*slot)
4687 {
4688 /* Already visited. */
4689 continue;
4690 }
4691 *slot = file_data;
4692
4693 for (int j = 0; j < file_data->num_file_names; ++j)
4694 {
4695 const char *filename = file_data->file_names[j];
4696 dwarf2_per_objfile->filenames_cache->seen (filename);
4697 }
4698 }
4699 }
4700
4701 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4702 {
4703 gdb::unique_xmalloc_ptr<char> this_real_name;
4704
4705 if (need_fullname)
4706 this_real_name = gdb_realpath (filename);
4707 (*fun) (filename, this_real_name.get (), data);
4708 });
4709}
4710
4711static int
4712dw2_has_symbols (struct objfile *objfile)
4713{
4714 return 1;
4715}
4716
4717const struct quick_symbol_functions dwarf2_gdb_index_functions =
4718{
4719 dw2_has_symbols,
4720 dw2_find_last_source_symtab,
4721 dw2_forget_cached_source_info,
4722 dw2_map_symtabs_matching_filename,
4723 dw2_lookup_symbol,
4724 dw2_print_stats,
4725 dw2_dump,
927aa2e7
JK
4726 dw2_expand_symtabs_for_function,
4727 dw2_expand_all_symtabs,
4728 dw2_expand_symtabs_with_fullname,
4729 dw2_map_matching_symbols,
4730 dw2_expand_symtabs_matching,
4731 dw2_find_pc_sect_compunit_symtab,
4732 NULL,
4733 dw2_map_symbol_filenames
4734};
4735
4736/* DWARF-5 debug_names reader. */
4737
4738/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4739static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4740
4741/* A helper function that reads the .debug_names section in SECTION
4742 and fills in MAP. FILENAME is the name of the file containing the
4743 section; it is used for error reporting.
4744
4745 Returns true if all went well, false otherwise. */
4746
4747static bool
4748read_debug_names_from_section (struct objfile *objfile,
4749 const char *filename,
4750 struct dwarf2_section_info *section,
4751 mapped_debug_names &map)
4752{
96b79293 4753 if (section->empty ())
927aa2e7
JK
4754 return false;
4755
4756 /* Older elfutils strip versions could keep the section in the main
4757 executable while splitting it for the separate debug info file. */
96b79293 4758 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4759 return false;
4760
96b79293 4761 section->read (objfile);
927aa2e7
JK
4762
4763 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4764
4765 const gdb_byte *addr = section->buffer;
4766
96b79293 4767 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4768
4769 unsigned int bytes_read;
4770 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4771 addr += bytes_read;
4772
4773 map.dwarf5_is_dwarf64 = bytes_read != 4;
4774 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4775 if (bytes_read + length != section->size)
4776 {
4777 /* There may be multiple per-CU indices. */
4778 warning (_("Section .debug_names in %s length %s does not match "
4779 "section length %s, ignoring .debug_names."),
4780 filename, plongest (bytes_read + length),
4781 pulongest (section->size));
4782 return false;
4783 }
4784
4785 /* The version number. */
4786 uint16_t version = read_2_bytes (abfd, addr);
4787 addr += 2;
4788 if (version != 5)
4789 {
4790 warning (_("Section .debug_names in %s has unsupported version %d, "
4791 "ignoring .debug_names."),
4792 filename, version);
4793 return false;
4794 }
4795
4796 /* Padding. */
4797 uint16_t padding = read_2_bytes (abfd, addr);
4798 addr += 2;
4799 if (padding != 0)
4800 {
4801 warning (_("Section .debug_names in %s has unsupported padding %d, "
4802 "ignoring .debug_names."),
4803 filename, padding);
4804 return false;
4805 }
4806
4807 /* comp_unit_count - The number of CUs in the CU list. */
4808 map.cu_count = read_4_bytes (abfd, addr);
4809 addr += 4;
4810
4811 /* local_type_unit_count - The number of TUs in the local TU
4812 list. */
4813 map.tu_count = read_4_bytes (abfd, addr);
4814 addr += 4;
4815
4816 /* foreign_type_unit_count - The number of TUs in the foreign TU
4817 list. */
4818 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4819 addr += 4;
4820 if (foreign_tu_count != 0)
4821 {
4822 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4823 "ignoring .debug_names."),
4824 filename, static_cast<unsigned long> (foreign_tu_count));
4825 return false;
4826 }
4827
4828 /* bucket_count - The number of hash buckets in the hash lookup
4829 table. */
4830 map.bucket_count = read_4_bytes (abfd, addr);
4831 addr += 4;
4832
4833 /* name_count - The number of unique names in the index. */
4834 map.name_count = read_4_bytes (abfd, addr);
4835 addr += 4;
4836
4837 /* abbrev_table_size - The size in bytes of the abbreviations
4838 table. */
4839 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4840 addr += 4;
4841
4842 /* augmentation_string_size - The size in bytes of the augmentation
4843 string. This value is rounded up to a multiple of 4. */
4844 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4845 addr += 4;
4846 map.augmentation_is_gdb = ((augmentation_string_size
4847 == sizeof (dwarf5_augmentation))
4848 && memcmp (addr, dwarf5_augmentation,
4849 sizeof (dwarf5_augmentation)) == 0);
4850 augmentation_string_size += (-augmentation_string_size) & 3;
4851 addr += augmentation_string_size;
4852
4853 /* List of CUs */
4854 map.cu_table_reordered = addr;
4855 addr += map.cu_count * map.offset_size;
4856
4857 /* List of Local TUs */
4858 map.tu_table_reordered = addr;
4859 addr += map.tu_count * map.offset_size;
4860
4861 /* Hash Lookup Table */
4862 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4863 addr += map.bucket_count * 4;
4864 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4865 addr += map.name_count * 4;
4866
4867 /* Name Table */
4868 map.name_table_string_offs_reordered = addr;
4869 addr += map.name_count * map.offset_size;
4870 map.name_table_entry_offs_reordered = addr;
4871 addr += map.name_count * map.offset_size;
4872
4873 const gdb_byte *abbrev_table_start = addr;
4874 for (;;)
4875 {
927aa2e7
JK
4876 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4877 addr += bytes_read;
4878 if (index_num == 0)
4879 break;
4880
4881 const auto insertpair
4882 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4883 if (!insertpair.second)
4884 {
4885 warning (_("Section .debug_names in %s has duplicate index %s, "
4886 "ignoring .debug_names."),
4887 filename, pulongest (index_num));
4888 return false;
4889 }
4890 mapped_debug_names::index_val &indexval = insertpair.first->second;
4891 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4892 addr += bytes_read;
4893
4894 for (;;)
4895 {
4896 mapped_debug_names::index_val::attr attr;
4897 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4898 addr += bytes_read;
4899 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4900 addr += bytes_read;
4901 if (attr.form == DW_FORM_implicit_const)
4902 {
4903 attr.implicit_const = read_signed_leb128 (abfd, addr,
4904 &bytes_read);
4905 addr += bytes_read;
4906 }
4907 if (attr.dw_idx == 0 && attr.form == 0)
4908 break;
4909 indexval.attr_vec.push_back (std::move (attr));
4910 }
4911 }
4912 if (addr != abbrev_table_start + abbrev_table_size)
4913 {
4914 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4915 "of size %s vs. written as %u, ignoring .debug_names."),
4916 filename, plongest (addr - abbrev_table_start),
4917 abbrev_table_size);
927aa2e7
JK
4918 return false;
4919 }
4920 map.entry_pool = addr;
4921
4922 return true;
4923}
4924
4925/* A helper for create_cus_from_debug_names that handles the MAP's CU
4926 list. */
4927
4928static void
ed2dc618 4929create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4930 const mapped_debug_names &map,
4931 dwarf2_section_info &section,
b76e467d 4932 bool is_dwz)
927aa2e7
JK
4933{
4934 sect_offset sect_off_prev;
4935 for (uint32_t i = 0; i <= map.cu_count; ++i)
4936 {
4937 sect_offset sect_off_next;
4938 if (i < map.cu_count)
4939 {
4940 sect_off_next
4941 = (sect_offset) (extract_unsigned_integer
4942 (map.cu_table_reordered + i * map.offset_size,
4943 map.offset_size,
4944 map.dwarf5_byte_order));
4945 }
4946 else
4947 sect_off_next = (sect_offset) section.size;
4948 if (i >= 1)
4949 {
4950 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 4951 dwarf2_per_cu_data *per_cu
ed2dc618 4952 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 4953 sect_off_prev, length);
b76e467d 4954 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
4955 }
4956 sect_off_prev = sect_off_next;
4957 }
4958}
4959
4960/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4961 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4962
4963static void
ed2dc618 4964create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4965 const mapped_debug_names &map,
4966 const mapped_debug_names &dwz_map)
4967{
b76e467d
SM
4968 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4969 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4970
ed2dc618
SM
4971 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4972 dwarf2_per_objfile->info,
b76e467d 4973 false /* is_dwz */);
927aa2e7
JK
4974
4975 if (dwz_map.cu_count == 0)
4976 return;
4977
ed2dc618
SM
4978 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4979 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 4980 true /* is_dwz */);
927aa2e7
JK
4981}
4982
4983/* Read .debug_names. If everything went ok, initialize the "quick"
4984 elements of all the CUs and return true. Otherwise, return false. */
4985
4986static bool
ed2dc618 4987dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 4988{
22ca247e
TT
4989 std::unique_ptr<mapped_debug_names> map
4990 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
4991 mapped_debug_names dwz_map (dwarf2_per_objfile);
4992 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
4993
4994 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4995 &dwarf2_per_objfile->debug_names,
22ca247e 4996 *map))
927aa2e7
JK
4997 return false;
4998
4999 /* Don't use the index if it's empty. */
22ca247e 5000 if (map->name_count == 0)
927aa2e7
JK
5001 return false;
5002
5003 /* If there is a .dwz file, read it so we can get its CU list as
5004 well. */
ed2dc618 5005 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5006 if (dwz != NULL)
5007 {
5008 if (!read_debug_names_from_section (objfile,
00f93c44 5009 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5010 &dwz->debug_names, dwz_map))
5011 {
5012 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5013 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5014 return false;
5015 }
5016 }
5017
22ca247e 5018 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5019
22ca247e 5020 if (map->tu_count != 0)
927aa2e7
JK
5021 {
5022 /* We can only handle a single .debug_types when we have an
5023 index. */
fd5866f6 5024 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5025 return false;
5026
fd5866f6 5027 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5028
5029 create_signatured_type_table_from_debug_names
22ca247e 5030 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5031 }
5032
ed2dc618
SM
5033 create_addrmap_from_aranges (dwarf2_per_objfile,
5034 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5035
22ca247e 5036 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5037 dwarf2_per_objfile->using_index = 1;
5038 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5039 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5040
5041 return true;
5042}
5043
927aa2e7
JK
5044/* Type used to manage iterating over all CUs looking for a symbol for
5045 .debug_names. */
5046
5047class dw2_debug_names_iterator
5048{
5049public:
927aa2e7 5050 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5051 gdb::optional<block_enum> block_index,
5052 domain_enum domain,
927aa2e7 5053 const char *name)
2b79f376 5054 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5055 m_addr (find_vec_in_debug_names (map, name))
5056 {}
5057
5058 dw2_debug_names_iterator (const mapped_debug_names &map,
5059 search_domain search, uint32_t namei)
5060 : m_map (map),
5061 m_search (search),
5062 m_addr (find_vec_in_debug_names (map, namei))
5063 {}
5064
3b00ef10
TT
5065 dw2_debug_names_iterator (const mapped_debug_names &map,
5066 block_enum block_index, domain_enum domain,
5067 uint32_t namei)
5068 : m_map (map), m_block_index (block_index), m_domain (domain),
5069 m_addr (find_vec_in_debug_names (map, namei))
5070 {}
5071
927aa2e7
JK
5072 /* Return the next matching CU or NULL if there are no more. */
5073 dwarf2_per_cu_data *next ();
5074
5075private:
5076 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5077 const char *name);
5078 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5079 uint32_t namei);
5080
5081 /* The internalized form of .debug_names. */
5082 const mapped_debug_names &m_map;
5083
2b79f376
SM
5084 /* If set, only look for symbols that match that block. Valid values are
5085 GLOBAL_BLOCK and STATIC_BLOCK. */
5086 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5087
5088 /* The kind of symbol we're looking for. */
5089 const domain_enum m_domain = UNDEF_DOMAIN;
5090 const search_domain m_search = ALL_DOMAIN;
5091
5092 /* The list of CUs from the index entry of the symbol, or NULL if
5093 not found. */
5094 const gdb_byte *m_addr;
5095};
5096
5097const char *
5098mapped_debug_names::namei_to_name (uint32_t namei) const
5099{
5100 const ULONGEST namei_string_offs
5101 = extract_unsigned_integer ((name_table_string_offs_reordered
5102 + namei * offset_size),
5103 offset_size,
5104 dwarf5_byte_order);
4f44ae6c
TT
5105 return read_indirect_string_at_offset (dwarf2_per_objfile,
5106 namei_string_offs);
927aa2e7
JK
5107}
5108
5109/* Find a slot in .debug_names for the object named NAME. If NAME is
5110 found, return pointer to its pool data. If NAME cannot be found,
5111 return NULL. */
5112
5113const gdb_byte *
5114dw2_debug_names_iterator::find_vec_in_debug_names
5115 (const mapped_debug_names &map, const char *name)
5116{
5117 int (*cmp) (const char *, const char *);
5118
54ee4252 5119 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5120 if (current_language->la_language == language_cplus
5121 || current_language->la_language == language_fortran
5122 || current_language->la_language == language_d)
5123 {
5124 /* NAME is already canonical. Drop any qualifiers as
5125 .debug_names does not contain any. */
5126
5127 if (strchr (name, '(') != NULL)
5128 {
54ee4252 5129 without_params = cp_remove_params (name);
927aa2e7 5130 if (without_params != NULL)
54ee4252 5131 name = without_params.get ();
927aa2e7
JK
5132 }
5133 }
5134
5135 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5136
5137 const uint32_t full_hash = dwarf5_djb_hash (name);
5138 uint32_t namei
5139 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5140 (map.bucket_table_reordered
5141 + (full_hash % map.bucket_count)), 4,
5142 map.dwarf5_byte_order);
5143 if (namei == 0)
5144 return NULL;
5145 --namei;
5146 if (namei >= map.name_count)
5147 {
b98664d3 5148 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5149 "[in module %s]"),
5150 namei, map.name_count,
ed2dc618 5151 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5152 return NULL;
5153 }
5154
5155 for (;;)
5156 {
5157 const uint32_t namei_full_hash
5158 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5159 (map.hash_table_reordered + namei), 4,
5160 map.dwarf5_byte_order);
5161 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5162 return NULL;
5163
5164 if (full_hash == namei_full_hash)
5165 {
5166 const char *const namei_string = map.namei_to_name (namei);
5167
5168#if 0 /* An expensive sanity check. */
5169 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5170 {
b98664d3 5171 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5172 "[in module %s]"),
5173 namei, objfile_name (dwarf2_per_objfile->objfile));
5174 return NULL;
5175 }
5176#endif
5177
5178 if (cmp (namei_string, name) == 0)
5179 {
5180 const ULONGEST namei_entry_offs
5181 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5182 + namei * map.offset_size),
5183 map.offset_size, map.dwarf5_byte_order);
5184 return map.entry_pool + namei_entry_offs;
5185 }
5186 }
5187
5188 ++namei;
5189 if (namei >= map.name_count)
5190 return NULL;
5191 }
5192}
5193
5194const gdb_byte *
5195dw2_debug_names_iterator::find_vec_in_debug_names
5196 (const mapped_debug_names &map, uint32_t namei)
5197{
5198 if (namei >= map.name_count)
5199 {
b98664d3 5200 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5201 "[in module %s]"),
5202 namei, map.name_count,
ed2dc618 5203 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5204 return NULL;
5205 }
5206
5207 const ULONGEST namei_entry_offs
5208 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5209 + namei * map.offset_size),
5210 map.offset_size, map.dwarf5_byte_order);
5211 return map.entry_pool + namei_entry_offs;
5212}
5213
5214/* See dw2_debug_names_iterator. */
5215
5216dwarf2_per_cu_data *
5217dw2_debug_names_iterator::next ()
5218{
5219 if (m_addr == NULL)
5220 return NULL;
5221
ed2dc618
SM
5222 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5223 struct objfile *objfile = dwarf2_per_objfile->objfile;
5224 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5225
5226 again:
5227
5228 unsigned int bytes_read;
5229 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5230 m_addr += bytes_read;
5231 if (abbrev == 0)
5232 return NULL;
5233
5234 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5235 if (indexval_it == m_map.abbrev_map.cend ())
5236 {
b98664d3 5237 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5238 "[in module %s]"),
ed2dc618 5239 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5240 return NULL;
5241 }
5242 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5243 enum class symbol_linkage {
5244 unknown,
5245 static_,
5246 extern_,
23c13d42 5247 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5248 dwarf2_per_cu_data *per_cu = NULL;
5249 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5250 {
5251 ULONGEST ull;
5252 switch (attr.form)
5253 {
5254 case DW_FORM_implicit_const:
5255 ull = attr.implicit_const;
5256 break;
5257 case DW_FORM_flag_present:
5258 ull = 1;
5259 break;
5260 case DW_FORM_udata:
5261 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5262 m_addr += bytes_read;
5263 break;
5264 default:
b98664d3 5265 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5266 dwarf_form_name (attr.form),
ed2dc618 5267 objfile_name (objfile));
927aa2e7
JK
5268 return NULL;
5269 }
5270 switch (attr.dw_idx)
5271 {
5272 case DW_IDX_compile_unit:
5273 /* Don't crash on bad data. */
b76e467d 5274 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5275 {
b98664d3 5276 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5277 " [in module %s]"),
5278 pulongest (ull),
5279 objfile_name (dwarf2_per_objfile->objfile));
5280 continue;
5281 }
ff4c9fec 5282 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5283 break;
8af5c486
JK
5284 case DW_IDX_type_unit:
5285 /* Don't crash on bad data. */
b2bdb8cf 5286 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5287 {
b98664d3 5288 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5289 " [in module %s]"),
5290 pulongest (ull),
5291 objfile_name (dwarf2_per_objfile->objfile));
5292 continue;
5293 }
ff4c9fec 5294 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5295 break;
927aa2e7
JK
5296 case DW_IDX_GNU_internal:
5297 if (!m_map.augmentation_is_gdb)
5298 break;
23c13d42 5299 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5300 break;
5301 case DW_IDX_GNU_external:
5302 if (!m_map.augmentation_is_gdb)
5303 break;
23c13d42 5304 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5305 break;
5306 }
5307 }
5308
5309 /* Skip if already read in. */
5310 if (per_cu->v.quick->compunit_symtab)
5311 goto again;
5312
5313 /* Check static vs global. */
23c13d42 5314 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5315 {
2b79f376 5316 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5317 const bool symbol_is_static =
5318 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5319 if (want_static != symbol_is_static)
2b79f376 5320 goto again;
927aa2e7
JK
5321 }
5322
5323 /* Match dw2_symtab_iter_next, symbol_kind
5324 and debug_names::psymbol_tag. */
5325 switch (m_domain)
5326 {
5327 case VAR_DOMAIN:
5328 switch (indexval.dwarf_tag)
5329 {
5330 case DW_TAG_variable:
5331 case DW_TAG_subprogram:
5332 /* Some types are also in VAR_DOMAIN. */
5333 case DW_TAG_typedef:
5334 case DW_TAG_structure_type:
5335 break;
5336 default:
5337 goto again;
5338 }
5339 break;
5340 case STRUCT_DOMAIN:
5341 switch (indexval.dwarf_tag)
5342 {
5343 case DW_TAG_typedef:
5344 case DW_TAG_structure_type:
5345 break;
5346 default:
5347 goto again;
5348 }
5349 break;
5350 case LABEL_DOMAIN:
5351 switch (indexval.dwarf_tag)
5352 {
5353 case 0:
5354 case DW_TAG_variable:
5355 break;
5356 default:
5357 goto again;
5358 }
5359 break;
59c35742
AB
5360 case MODULE_DOMAIN:
5361 switch (indexval.dwarf_tag)
5362 {
5363 case DW_TAG_module:
5364 break;
5365 default:
5366 goto again;
5367 }
5368 break;
927aa2e7
JK
5369 default:
5370 break;
5371 }
5372
5373 /* Match dw2_expand_symtabs_matching, symbol_kind and
5374 debug_names::psymbol_tag. */
5375 switch (m_search)
4b514bc8 5376 {
927aa2e7
JK
5377 case VARIABLES_DOMAIN:
5378 switch (indexval.dwarf_tag)
4b514bc8 5379 {
927aa2e7
JK
5380 case DW_TAG_variable:
5381 break;
5382 default:
5383 goto again;
4b514bc8 5384 }
927aa2e7
JK
5385 break;
5386 case FUNCTIONS_DOMAIN:
5387 switch (indexval.dwarf_tag)
4b514bc8 5388 {
927aa2e7
JK
5389 case DW_TAG_subprogram:
5390 break;
5391 default:
5392 goto again;
4b514bc8 5393 }
927aa2e7
JK
5394 break;
5395 case TYPES_DOMAIN:
5396 switch (indexval.dwarf_tag)
5397 {
5398 case DW_TAG_typedef:
5399 case DW_TAG_structure_type:
5400 break;
5401 default:
5402 goto again;
5403 }
5404 break;
59c35742
AB
5405 case MODULES_DOMAIN:
5406 switch (indexval.dwarf_tag)
5407 {
5408 case DW_TAG_module:
5409 break;
5410 default:
5411 goto again;
5412 }
927aa2e7
JK
5413 default:
5414 break;
4b514bc8 5415 }
927aa2e7
JK
5416
5417 return per_cu;
4b514bc8 5418}
61920122 5419
927aa2e7 5420static struct compunit_symtab *
c7f839cb 5421dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5422 const char *name, domain_enum domain)
4b514bc8 5423{
ed2dc618
SM
5424 struct dwarf2_per_objfile *dwarf2_per_objfile
5425 = get_dwarf2_per_objfile (objfile);
61920122 5426
927aa2e7
JK
5427 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5428 if (!mapp)
61920122 5429 {
927aa2e7
JK
5430 /* index is NULL if OBJF_READNOW. */
5431 return NULL;
5432 }
5433 const auto &map = *mapp;
9291a0cd 5434
2b79f376 5435 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5436
927aa2e7
JK
5437 struct compunit_symtab *stab_best = NULL;
5438 struct dwarf2_per_cu_data *per_cu;
5439 while ((per_cu = iter.next ()) != NULL)
5440 {
5441 struct symbol *sym, *with_opaque = NULL;
58f0c718 5442 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5443 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5444 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5445
927aa2e7
JK
5446 sym = block_find_symbol (block, name, domain,
5447 block_find_non_opaque_type_preferred,
5448 &with_opaque);
9703b513 5449
927aa2e7
JK
5450 /* Some caution must be observed with overloaded functions and
5451 methods, since the index will not contain any overload
5452 information (but NAME might contain it). */
a3ec0bb1 5453
927aa2e7 5454 if (sym != NULL
987012b8 5455 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5456 return stab;
5457 if (with_opaque != NULL
987012b8 5458 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5459 stab_best = stab;
9703b513 5460
927aa2e7 5461 /* Keep looking through other CUs. */
9703b513
TT
5462 }
5463
927aa2e7 5464 return stab_best;
9703b513
TT
5465}
5466
927aa2e7
JK
5467/* This dumps minimal information about .debug_names. It is called
5468 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5469 uses this to verify that .debug_names has been loaded. */
9291a0cd 5470
927aa2e7
JK
5471static void
5472dw2_debug_names_dump (struct objfile *objfile)
5473{
ed2dc618
SM
5474 struct dwarf2_per_objfile *dwarf2_per_objfile
5475 = get_dwarf2_per_objfile (objfile);
5476
927aa2e7
JK
5477 gdb_assert (dwarf2_per_objfile->using_index);
5478 printf_filtered (".debug_names:");
5479 if (dwarf2_per_objfile->debug_names_table)
5480 printf_filtered (" exists\n");
5481 else
5482 printf_filtered (" faked for \"readnow\"\n");
5483 printf_filtered ("\n");
9291a0cd
TT
5484}
5485
9291a0cd 5486static void
927aa2e7
JK
5487dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5488 const char *func_name)
9291a0cd 5489{
ed2dc618
SM
5490 struct dwarf2_per_objfile *dwarf2_per_objfile
5491 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5492
927aa2e7
JK
5493 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5494 if (dwarf2_per_objfile->debug_names_table)
24c79950 5495 {
927aa2e7 5496 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5497
2b79f376 5498 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5499
927aa2e7
JK
5500 struct dwarf2_per_cu_data *per_cu;
5501 while ((per_cu = iter.next ()) != NULL)
58f0c718 5502 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5503 }
5504}
24c79950 5505
3b00ef10
TT
5506static void
5507dw2_debug_names_map_matching_symbols
5508 (struct objfile *objfile,
5509 const lookup_name_info &name, domain_enum domain,
5510 int global,
5511 gdb::function_view<symbol_found_callback_ftype> callback,
5512 symbol_compare_ftype *ordered_compare)
5513{
5514 struct dwarf2_per_objfile *dwarf2_per_objfile
5515 = get_dwarf2_per_objfile (objfile);
5516
5517 /* debug_names_table is NULL if OBJF_READNOW. */
5518 if (!dwarf2_per_objfile->debug_names_table)
5519 return;
5520
5521 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5522 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5523
5524 const char *match_name = name.ada ().lookup_name ().c_str ();
5525 auto matcher = [&] (const char *symname)
5526 {
5527 if (ordered_compare == nullptr)
5528 return true;
5529 return ordered_compare (symname, match_name) == 0;
5530 };
5531
5532 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5533 [&] (offset_type namei)
5534 {
5535 /* The name was matched, now expand corresponding CUs that were
5536 marked. */
5537 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5538
5539 struct dwarf2_per_cu_data *per_cu;
5540 while ((per_cu = iter.next ()) != NULL)
5541 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5542 return true;
5543 });
5544
5545 /* It's a shame we couldn't do this inside the
5546 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5547 that have already been expanded. Instead, this loop matches what
5548 the psymtab code does. */
5549 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5550 {
5551 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5552 if (cust != nullptr)
5553 {
5554 const struct block *block
5555 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5556 if (!iterate_over_symbols_terminated (block, name,
5557 domain, callback))
5558 break;
5559 }
5560 }
5561}
5562
927aa2e7
JK
5563static void
5564dw2_debug_names_expand_symtabs_matching
5565 (struct objfile *objfile,
5566 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5567 const lookup_name_info &lookup_name,
5568 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5569 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5570 enum search_domain kind)
5571{
ed2dc618
SM
5572 struct dwarf2_per_objfile *dwarf2_per_objfile
5573 = get_dwarf2_per_objfile (objfile);
9291a0cd 5574
927aa2e7
JK
5575 /* debug_names_table is NULL if OBJF_READNOW. */
5576 if (!dwarf2_per_objfile->debug_names_table)
5577 return;
9291a0cd 5578
ed2dc618 5579 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5580
44ed8f3e 5581 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5582
44ed8f3e
PA
5583 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5584 symbol_matcher,
5585 kind, [&] (offset_type namei)
927aa2e7 5586 {
927aa2e7
JK
5587 /* The name was matched, now expand corresponding CUs that were
5588 marked. */
5589 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5590
927aa2e7
JK
5591 struct dwarf2_per_cu_data *per_cu;
5592 while ((per_cu = iter.next ()) != NULL)
5593 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5594 expansion_notify);
3b00ef10 5595 return true;
44ed8f3e 5596 });
9291a0cd
TT
5597}
5598
927aa2e7 5599const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5600{
5601 dw2_has_symbols,
5602 dw2_find_last_source_symtab,
5603 dw2_forget_cached_source_info,
f8eba3c6 5604 dw2_map_symtabs_matching_filename,
927aa2e7 5605 dw2_debug_names_lookup_symbol,
9291a0cd 5606 dw2_print_stats,
927aa2e7 5607 dw2_debug_names_dump,
927aa2e7 5608 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5609 dw2_expand_all_symtabs,
652a8996 5610 dw2_expand_symtabs_with_fullname,
3b00ef10 5611 dw2_debug_names_map_matching_symbols,
927aa2e7 5612 dw2_debug_names_expand_symtabs_matching,
43f3e411 5613 dw2_find_pc_sect_compunit_symtab,
71a3c369 5614 NULL,
9291a0cd
TT
5615 dw2_map_symbol_filenames
5616};
5617
4485a1c1
SM
5618/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5619 to either a dwarf2_per_objfile or dwz_file object. */
5620
5621template <typename T>
5622static gdb::array_view<const gdb_byte>
5623get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5624{
5625 dwarf2_section_info *section = &section_owner->gdb_index;
5626
96b79293 5627 if (section->empty ())
4485a1c1
SM
5628 return {};
5629
5630 /* Older elfutils strip versions could keep the section in the main
5631 executable while splitting it for the separate debug info file. */
96b79293 5632 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5633 return {};
5634
96b79293 5635 section->read (obj);
4485a1c1 5636
8bebfcda
PA
5637 /* dwarf2_section_info::size is a bfd_size_type, while
5638 gdb::array_view works with size_t. On 32-bit hosts, with
5639 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5640 is 32-bit. So we need an explicit narrowing conversion here.
5641 This is fine, because it's impossible to allocate or mmap an
5642 array/buffer larger than what size_t can represent. */
5643 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5644}
5645
87d6a7aa
SM
5646/* Lookup the index cache for the contents of the index associated to
5647 DWARF2_OBJ. */
5648
5649static gdb::array_view<const gdb_byte>
5650get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5651{
5652 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5653 if (build_id == nullptr)
5654 return {};
5655
5656 return global_index_cache.lookup_gdb_index (build_id,
5657 &dwarf2_obj->index_cache_res);
5658}
5659
5660/* Same as the above, but for DWZ. */
5661
5662static gdb::array_view<const gdb_byte>
5663get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5664{
5665 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5666 if (build_id == nullptr)
5667 return {};
5668
5669 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5670}
5671
3c0aa29a 5672/* See symfile.h. */
9291a0cd 5673
3c0aa29a
PA
5674bool
5675dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5676{
ed2dc618
SM
5677 struct dwarf2_per_objfile *dwarf2_per_objfile
5678 = get_dwarf2_per_objfile (objfile);
5679
9291a0cd
TT
5680 /* If we're about to read full symbols, don't bother with the
5681 indices. In this case we also don't care if some other debug
5682 format is making psymtabs, because they are all about to be
5683 expanded anyway. */
5684 if ((objfile->flags & OBJF_READNOW))
5685 {
9291a0cd 5686 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5687 create_all_comp_units (dwarf2_per_objfile);
5688 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5689 dwarf2_per_objfile->quick_file_names_table
5690 = create_quick_file_names_table
5691 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5692
b76e467d 5693 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5694 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5695 {
ff4c9fec 5696 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5697
e254ef6a
DE
5698 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5699 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5700 }
5701
5702 /* Return 1 so that gdb sees the "quick" functions. However,
5703 these functions will be no-ops because we will have expanded
5704 all symtabs. */
3c0aa29a
PA
5705 *index_kind = dw_index_kind::GDB_INDEX;
5706 return true;
9291a0cd
TT
5707 }
5708
ed2dc618 5709 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5710 {
5711 *index_kind = dw_index_kind::DEBUG_NAMES;
5712 return true;
5713 }
927aa2e7 5714
4485a1c1
SM
5715 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5716 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5717 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5718 {
5719 *index_kind = dw_index_kind::GDB_INDEX;
5720 return true;
5721 }
9291a0cd 5722
87d6a7aa
SM
5723 /* ... otherwise, try to find the index in the index cache. */
5724 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5725 get_gdb_index_contents_from_cache,
5726 get_gdb_index_contents_from_cache_dwz))
5727 {
5728 global_index_cache.hit ();
5729 *index_kind = dw_index_kind::GDB_INDEX;
5730 return true;
5731 }
5732
5733 global_index_cache.miss ();
3c0aa29a 5734 return false;
9291a0cd
TT
5735}
5736
5737\f
5738
dce234bc
PP
5739/* Build a partial symbol table. */
5740
5741void
f29dff0a 5742dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5743{
ed2dc618
SM
5744 struct dwarf2_per_objfile *dwarf2_per_objfile
5745 = get_dwarf2_per_objfile (objfile);
c9bf0622 5746
6eee24ce 5747 init_psymbol_list (objfile, 1024);
c906108c 5748
a70b8144 5749 try
c9bf0622
TT
5750 {
5751 /* This isn't really ideal: all the data we allocate on the
5752 objfile's obstack is still uselessly kept around. However,
5753 freeing it seems unsafe. */
906768f9 5754 psymtab_discarder psymtabs (objfile);
ed2dc618 5755 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5756 psymtabs.keep ();
87d6a7aa
SM
5757
5758 /* (maybe) store an index in the cache. */
5759 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5760 }
230d2906 5761 catch (const gdb_exception_error &except)
492d29ea
PA
5762 {
5763 exception_print (gdb_stderr, except);
5764 }
c906108c 5765}
c906108c 5766
3b80fe9b
DE
5767/* Find the base address of the compilation unit for range lists and
5768 location lists. It will normally be specified by DW_AT_low_pc.
5769 In DWARF-3 draft 4, the base address could be overridden by
5770 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5771 compilation units with discontinuous ranges. */
5772
5773static void
5774dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5775{
5776 struct attribute *attr;
5777
2b24b6e4 5778 cu->base_address.reset ();
3b80fe9b
DE
5779
5780 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5781 if (attr != nullptr)
2b24b6e4 5782 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5783 else
5784 {
5785 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5786 if (attr != nullptr)
2b24b6e4 5787 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5788 }
5789}
5790
36586728
TT
5791/* Helper function that returns the proper abbrev section for
5792 THIS_CU. */
5793
5794static struct dwarf2_section_info *
5795get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5796{
5797 struct dwarf2_section_info *abbrev;
ed2dc618 5798 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5799
5800 if (this_cu->is_dwz)
ed2dc618 5801 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5802 else
5803 abbrev = &dwarf2_per_objfile->abbrev;
5804
5805 return abbrev;
5806}
5807
f4dc4d17
DE
5808/* Fetch the abbreviation table offset from a comp or type unit header. */
5809
5810static sect_offset
ed2dc618
SM
5811read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5812 struct dwarf2_section_info *section,
9c541725 5813 sect_offset sect_off)
f4dc4d17 5814{
96b79293 5815 bfd *abfd = section->get_bfd_owner ();
d521ce57 5816 const gdb_byte *info_ptr;
ac298888 5817 unsigned int initial_length_size, offset_size;
43988095 5818 uint16_t version;
f4dc4d17 5819
96b79293 5820 section->read (dwarf2_per_objfile->objfile);
9c541725 5821 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5822 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5823 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5824 info_ptr += initial_length_size;
5825
5826 version = read_2_bytes (abfd, info_ptr);
5827 info_ptr += 2;
5828 if (version >= 5)
5829 {
5830 /* Skip unit type and address size. */
5831 info_ptr += 2;
5832 }
5833
24aa364d 5834 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5835}
5836
b83470bf
TT
5837/* A partial symtab that is used only for include files. */
5838struct dwarf2_include_psymtab : public partial_symtab
5839{
5840 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5841 : partial_symtab (filename, objfile)
5842 {
5843 }
5844
5845 void read_symtab (struct objfile *objfile) override
5846 {
5847 expand_psymtab (objfile);
5848 }
5849
5850 void expand_psymtab (struct objfile *objfile) override
5851 {
5852 if (m_readin)
5853 return;
5854 /* It's an include file, no symbols to read for it.
5855 Everything is in the parent symtab. */
5856 read_dependencies (objfile);
5857 m_readin = true;
5858 }
5859
5860 bool readin_p () const override
5861 {
5862 return m_readin;
5863 }
5864
5865 struct compunit_symtab *get_compunit_symtab () const override
5866 {
5867 return nullptr;
5868 }
5869
5870private:
5871
5872 bool m_readin = false;
5873};
5874
aaa75496
JB
5875/* Allocate a new partial symtab for file named NAME and mark this new
5876 partial symtab as being an include of PST. */
5877
5878static void
891813be 5879dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5880 struct objfile *objfile)
5881{
b83470bf 5882 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 5883
fbd9ab74
JK
5884 if (!IS_ABSOLUTE_PATH (subpst->filename))
5885 {
5886 /* It shares objfile->objfile_obstack. */
5887 subpst->dirname = pst->dirname;
5888 }
5889
a9342b62 5890 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5891 subpst->dependencies[0] = pst;
5892 subpst->number_of_dependencies = 1;
aaa75496
JB
5893}
5894
5895/* Read the Line Number Program data and extract the list of files
5896 included by the source file represented by PST. Build an include
d85a05f0 5897 partial symtab for each of these included files. */
aaa75496
JB
5898
5899static void
5900dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5901 struct die_info *die,
891813be 5902 dwarf2_psymtab *pst)
aaa75496 5903{
fff8551c 5904 line_header_up lh;
d85a05f0 5905 struct attribute *attr;
aaa75496 5906
d85a05f0 5907 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5908 if (attr != nullptr)
9c541725 5909 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5910 if (lh == NULL)
5911 return; /* No linetable, so no includes. */
5912
79748972
TT
5913 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5914 that we pass in the raw text_low here; that is ok because we're
5915 only decoding the line table to make include partial symtabs, and
5916 so the addresses aren't really used. */
4ae976d1 5917 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5918 pst->raw_text_low (), 1);
aaa75496
JB
5919}
5920
348e048f 5921static hashval_t
52dc124a 5922hash_signatured_type (const void *item)
348e048f 5923{
9a3c8263
SM
5924 const struct signatured_type *sig_type
5925 = (const struct signatured_type *) item;
9a619af0 5926
348e048f 5927 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5928 return sig_type->signature;
348e048f
DE
5929}
5930
5931static int
52dc124a 5932eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5933{
9a3c8263
SM
5934 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5935 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5936
348e048f
DE
5937 return lhs->signature == rhs->signature;
5938}
5939
1fd400ff
TT
5940/* Allocate a hash table for signatured types. */
5941
b0b6a987 5942static htab_up
298e9637 5943allocate_signatured_type_table ()
1fd400ff 5944{
b0b6a987
TT
5945 return htab_up (htab_create_alloc (41,
5946 hash_signatured_type,
5947 eq_signatured_type,
5948 NULL, xcalloc, xfree));
1fd400ff
TT
5949}
5950
d467dd73 5951/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
5952
5953static int
d467dd73 5954add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 5955{
9a3c8263 5956 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
5957 std::vector<signatured_type *> *all_type_units
5958 = (std::vector<signatured_type *> *) datum;
1fd400ff 5959
b2bdb8cf 5960 all_type_units->push_back (sigt);
1fd400ff
TT
5961
5962 return 1;
5963}
5964
78d4d2c5 5965/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5966 and fill them into TYPES_HTAB. It will process only type units,
5967 therefore DW_UT_type. */
c88ee1f0 5968
78d4d2c5 5969static void
ed2dc618
SM
5970create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5971 struct dwo_file *dwo_file,
b0b6a987 5972 dwarf2_section_info *section, htab_up &types_htab,
43988095 5973 rcuh_kind section_kind)
348e048f 5974{
3019eac3 5975 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 5976 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5977 bfd *abfd;
5978 const gdb_byte *info_ptr, *end_ptr;
348e048f 5979
4bdcc0c1
DE
5980 abbrev_section = (dwo_file != NULL
5981 ? &dwo_file->sections.abbrev
5982 : &dwarf2_per_objfile->abbrev);
5983
b4f54984 5984 if (dwarf_read_debug)
43988095 5985 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
5986 section->get_name (),
5987 abbrev_section->get_file_name ());
09406207 5988
96b79293 5989 section->read (objfile);
78d4d2c5 5990 info_ptr = section->buffer;
348e048f 5991
78d4d2c5
JK
5992 if (info_ptr == NULL)
5993 return;
348e048f 5994
78d4d2c5
JK
5995 /* We can't set abfd until now because the section may be empty or
5996 not present, in which case the bfd is unknown. */
96b79293 5997 abfd = section->get_bfd_owner ();
348e048f 5998
c0ab21c2
TT
5999 /* We don't use cutu_reader here because we don't need to read
6000 any dies: the signature is in the header. */
3019eac3 6001
78d4d2c5
JK
6002 end_ptr = info_ptr + section->size;
6003 while (info_ptr < end_ptr)
6004 {
78d4d2c5
JK
6005 struct signatured_type *sig_type;
6006 struct dwo_unit *dwo_tu;
6007 void **slot;
6008 const gdb_byte *ptr = info_ptr;
6009 struct comp_unit_head header;
6010 unsigned int length;
8b70b953 6011
9c541725 6012 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6013
a49dd8dd
JK
6014 /* Initialize it due to a false compiler warning. */
6015 header.signature = -1;
9c541725 6016 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6017
78d4d2c5
JK
6018 /* We need to read the type's signature in order to build the hash
6019 table, but we don't need anything else just yet. */
348e048f 6020
ed2dc618 6021 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6022 abbrev_section, ptr, section_kind);
348e048f 6023
4057dfde 6024 length = header.get_length ();
6caca83c 6025
78d4d2c5
JK
6026 /* Skip dummy type units. */
6027 if (ptr >= info_ptr + length
43988095
JK
6028 || peek_abbrev_code (abfd, ptr) == 0
6029 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6030 {
6031 info_ptr += length;
6032 continue;
6033 }
dee91e82 6034
78d4d2c5
JK
6035 if (types_htab == NULL)
6036 {
6037 if (dwo_file)
298e9637 6038 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6039 else
298e9637 6040 types_htab = allocate_signatured_type_table ();
78d4d2c5 6041 }
8b70b953 6042
78d4d2c5
JK
6043 if (dwo_file)
6044 {
6045 sig_type = NULL;
6046 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6047 struct dwo_unit);
6048 dwo_tu->dwo_file = dwo_file;
43988095 6049 dwo_tu->signature = header.signature;
9c541725 6050 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6051 dwo_tu->section = section;
9c541725 6052 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6053 dwo_tu->length = length;
6054 }
6055 else
6056 {
6057 /* N.B.: type_offset is not usable if this type uses a DWO file.
6058 The real type_offset is in the DWO file. */
6059 dwo_tu = NULL;
6060 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6061 struct signatured_type);
43988095 6062 sig_type->signature = header.signature;
9c541725 6063 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6064 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6065 sig_type->per_cu.is_debug_types = 1;
6066 sig_type->per_cu.section = section;
9c541725 6067 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6068 sig_type->per_cu.length = length;
6069 }
6070
b0b6a987 6071 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6072 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6073 INSERT);
6074 gdb_assert (slot != NULL);
6075 if (*slot != NULL)
6076 {
9c541725 6077 sect_offset dup_sect_off;
0349ea22 6078
3019eac3
DE
6079 if (dwo_file)
6080 {
78d4d2c5
JK
6081 const struct dwo_unit *dup_tu
6082 = (const struct dwo_unit *) *slot;
6083
9c541725 6084 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6085 }
6086 else
6087 {
78d4d2c5
JK
6088 const struct signatured_type *dup_tu
6089 = (const struct signatured_type *) *slot;
6090
9c541725 6091 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6092 }
8b70b953 6093
b98664d3 6094 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6095 " the entry at offset %s, signature %s"),
6096 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6097 hex_string (header.signature));
78d4d2c5
JK
6098 }
6099 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6100
78d4d2c5 6101 if (dwarf_read_debug > 1)
9d8780f0
SM
6102 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6103 sect_offset_str (sect_off),
43988095 6104 hex_string (header.signature));
3019eac3 6105
78d4d2c5
JK
6106 info_ptr += length;
6107 }
6108}
3019eac3 6109
78d4d2c5
JK
6110/* Create the hash table of all entries in the .debug_types
6111 (or .debug_types.dwo) section(s).
6112 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6113 otherwise it is NULL.
b3c8eb43 6114
78d4d2c5 6115 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6116
78d4d2c5 6117 Note: This function processes DWO files only, not DWP files. */
348e048f 6118
78d4d2c5 6119static void
ed2dc618
SM
6120create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6121 struct dwo_file *dwo_file,
fd5866f6 6122 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6123 htab_up &types_htab)
78d4d2c5 6124{
fd5866f6
SM
6125 for (dwarf2_section_info &section : type_sections)
6126 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6127 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6128}
6129
6130/* Create the hash table of all entries in the .debug_types section,
6131 and initialize all_type_units.
6132 The result is zero if there is an error (e.g. missing .debug_types section),
6133 otherwise non-zero. */
6134
6135static int
ed2dc618 6136create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6137{
b0b6a987 6138 htab_up types_htab;
3019eac3 6139
ed2dc618
SM
6140 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6141 &dwarf2_per_objfile->info, types_htab,
43988095 6142 rcuh_kind::COMPILE);
ed2dc618
SM
6143 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6144 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6145 if (types_htab == NULL)
6146 {
6147 dwarf2_per_objfile->signatured_types = NULL;
6148 return 0;
6149 }
6150
b0b6a987 6151 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6152
b2bdb8cf 6153 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6154 dwarf2_per_objfile->all_type_units.reserve
6155 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6156
b0b6a987
TT
6157 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6158 add_signatured_type_cu_to_table,
b2bdb8cf 6159 &dwarf2_per_objfile->all_type_units);
1fd400ff 6160
348e048f
DE
6161 return 1;
6162}
6163
6aa5f3a6
DE
6164/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6165 If SLOT is non-NULL, it is the entry to use in the hash table.
6166 Otherwise we find one. */
6167
6168static struct signatured_type *
ed2dc618
SM
6169add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6170 void **slot)
6aa5f3a6
DE
6171{
6172 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6173
b2bdb8cf
SM
6174 if (dwarf2_per_objfile->all_type_units.size ()
6175 == dwarf2_per_objfile->all_type_units.capacity ())
6176 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6177
b2bdb8cf
SM
6178 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6179 struct signatured_type);
6180
6181 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6182 sig_type->signature = sig;
6183 sig_type->per_cu.is_debug_types = 1;
6184 if (dwarf2_per_objfile->using_index)
6185 {
6186 sig_type->per_cu.v.quick =
6187 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6188 struct dwarf2_per_cu_quick_data);
6189 }
6190
6191 if (slot == NULL)
6192 {
b0b6a987 6193 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6194 sig_type, INSERT);
6195 }
6196 gdb_assert (*slot == NULL);
6197 *slot = sig_type;
6198 /* The rest of sig_type must be filled in by the caller. */
6199 return sig_type;
6200}
6201
a2ce51a0
DE
6202/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6203 Fill in SIG_ENTRY with DWO_ENTRY. */
6204
6205static void
ed2dc618 6206fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6207 struct signatured_type *sig_entry,
6208 struct dwo_unit *dwo_entry)
6209{
7ee85ab1 6210 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6211 gdb_assert (! sig_entry->per_cu.queued);
6212 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6213 if (dwarf2_per_objfile->using_index)
6214 {
6215 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6216 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6217 }
6218 else
6219 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6220 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6221 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6222 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6223 gdb_assert (sig_entry->dwo_unit == NULL);
6224
6225 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6226 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6227 sig_entry->per_cu.length = dwo_entry->length;
6228 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6229 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6230 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6231 sig_entry->dwo_unit = dwo_entry;
6232}
6233
6234/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6235 If we haven't read the TU yet, create the signatured_type data structure
6236 for a TU to be read in directly from a DWO file, bypassing the stub.
6237 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6238 using .gdb_index, then when reading a CU we want to stay in the DWO file
6239 containing that CU. Otherwise we could end up reading several other DWO
6240 files (due to comdat folding) to process the transitive closure of all the
6241 mentioned TUs, and that can be slow. The current DWO file will have every
6242 type signature that it needs.
a2ce51a0
DE
6243 We only do this for .gdb_index because in the psymtab case we already have
6244 to read all the DWOs to build the type unit groups. */
6245
6246static struct signatured_type *
6247lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6248{
518817b3
SM
6249 struct dwarf2_per_objfile *dwarf2_per_objfile
6250 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6251 struct dwo_file *dwo_file;
6252 struct dwo_unit find_dwo_entry, *dwo_entry;
6253 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6254 void **slot;
a2ce51a0
DE
6255
6256 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6257
6aa5f3a6
DE
6258 /* If TU skeletons have been removed then we may not have read in any
6259 TUs yet. */
6260 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6261 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6262
6263 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6264 Use the global signatured_types array to do our own comdat-folding
6265 of types. If this is the first time we're reading this TU, and
6266 the TU has an entry in .gdb_index, replace the recorded data from
6267 .gdb_index with this TU. */
a2ce51a0 6268
a2ce51a0 6269 find_sig_entry.signature = sig;
b0b6a987 6270 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6271 &find_sig_entry, INSERT);
9a3c8263 6272 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6273
6274 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6275 read. Don't reassign the global entry to point to this DWO if that's
6276 the case. Also note that if the TU is already being read, it may not
6277 have come from a DWO, the program may be a mix of Fission-compiled
6278 code and non-Fission-compiled code. */
6279
6280 /* Have we already tried to read this TU?
6281 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6282 needn't exist in the global table yet). */
6283 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6284 return sig_entry;
6285
6aa5f3a6
DE
6286 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6287 dwo_unit of the TU itself. */
6288 dwo_file = cu->dwo_unit->dwo_file;
6289
a2ce51a0
DE
6290 /* Ok, this is the first time we're reading this TU. */
6291 if (dwo_file->tus == NULL)
6292 return NULL;
6293 find_dwo_entry.signature = sig;
b0b6a987
TT
6294 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6295 &find_dwo_entry);
a2ce51a0
DE
6296 if (dwo_entry == NULL)
6297 return NULL;
6298
6aa5f3a6
DE
6299 /* If the global table doesn't have an entry for this TU, add one. */
6300 if (sig_entry == NULL)
ed2dc618 6301 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6302
ed2dc618 6303 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6304 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6305 return sig_entry;
6306}
6307
a2ce51a0
DE
6308/* Subroutine of lookup_signatured_type.
6309 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6310 then try the DWP file. If the TU stub (skeleton) has been removed then
6311 it won't be in .gdb_index. */
a2ce51a0
DE
6312
6313static struct signatured_type *
6314lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6315{
518817b3
SM
6316 struct dwarf2_per_objfile *dwarf2_per_objfile
6317 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6318 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6319 struct dwo_unit *dwo_entry;
6320 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6321 void **slot;
a2ce51a0
DE
6322
6323 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6324 gdb_assert (dwp_file != NULL);
6325
6aa5f3a6
DE
6326 /* If TU skeletons have been removed then we may not have read in any
6327 TUs yet. */
6328 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6329 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6330
6aa5f3a6 6331 find_sig_entry.signature = sig;
b0b6a987 6332 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6333 &find_sig_entry, INSERT);
9a3c8263 6334 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6335
6336 /* Have we already tried to read this TU?
6337 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6338 needn't exist in the global table yet). */
6339 if (sig_entry != NULL)
6340 return sig_entry;
6341
a2ce51a0
DE
6342 if (dwp_file->tus == NULL)
6343 return NULL;
ed2dc618 6344 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6345 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6346 if (dwo_entry == NULL)
6347 return NULL;
6348
ed2dc618
SM
6349 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6350 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6351
a2ce51a0
DE
6352 return sig_entry;
6353}
6354
380bca97 6355/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6356 Returns NULL if signature SIG is not present in the table.
6357 It is up to the caller to complain about this. */
348e048f
DE
6358
6359static struct signatured_type *
a2ce51a0 6360lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6361{
518817b3
SM
6362 struct dwarf2_per_objfile *dwarf2_per_objfile
6363 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6364
a2ce51a0
DE
6365 if (cu->dwo_unit
6366 && dwarf2_per_objfile->using_index)
6367 {
6368 /* We're in a DWO/DWP file, and we're using .gdb_index.
6369 These cases require special processing. */
ed2dc618 6370 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6371 return lookup_dwo_signatured_type (cu, sig);
6372 else
6373 return lookup_dwp_signatured_type (cu, sig);
6374 }
6375 else
6376 {
6377 struct signatured_type find_entry, *entry;
348e048f 6378
a2ce51a0
DE
6379 if (dwarf2_per_objfile->signatured_types == NULL)
6380 return NULL;
6381 find_entry.signature = sig;
9a3c8263 6382 entry = ((struct signatured_type *)
b0b6a987
TT
6383 htab_find (dwarf2_per_objfile->signatured_types.get (),
6384 &find_entry));
a2ce51a0
DE
6385 return entry;
6386 }
348e048f 6387}
18a8505e
AT
6388
6389/* Return the address base of the compile unit, which, if exists, is stored
6390 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6391static gdb::optional<ULONGEST>
6392lookup_addr_base (struct die_info *comp_unit_die)
6393{
6394 struct attribute *attr;
052c8bb8 6395 attr = comp_unit_die->attr (DW_AT_addr_base);
18a8505e 6396 if (attr == nullptr)
052c8bb8 6397 attr = comp_unit_die->attr (DW_AT_GNU_addr_base);
18a8505e
AT
6398 if (attr == nullptr)
6399 return gdb::optional<ULONGEST> ();
6400 return DW_UNSND (attr);
6401}
6402
6403/* Return range lists base of the compile unit, which, if exists, is stored
6404 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6405static ULONGEST
6406lookup_ranges_base (struct die_info *comp_unit_die)
6407{
6408 struct attribute *attr;
052c8bb8 6409 attr = comp_unit_die->attr (DW_AT_rnglists_base);
18a8505e 6410 if (attr == nullptr)
052c8bb8 6411 attr = comp_unit_die->attr (DW_AT_GNU_ranges_base);
18a8505e
AT
6412 if (attr == nullptr)
6413 return 0;
6414 return DW_UNSND (attr);
6415}
6416
42e7ad6c 6417/* Low level DIE reading support. */
348e048f 6418
d85a05f0
DJ
6419/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6420
6421static void
6422init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6423 struct dwarf2_cu *cu,
3019eac3 6424 struct dwarf2_section_info *section,
685af9cd
TT
6425 struct dwo_file *dwo_file,
6426 struct abbrev_table *abbrev_table)
d85a05f0 6427{
fceca515 6428 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6429 reader->abfd = section->get_bfd_owner ();
d85a05f0 6430 reader->cu = cu;
3019eac3 6431 reader->dwo_file = dwo_file;
dee91e82
DE
6432 reader->die_section = section;
6433 reader->buffer = section->buffer;
f664829e 6434 reader->buffer_end = section->buffer + section->size;
685af9cd 6435 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6436}
6437
c0ab21c2 6438/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6439 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6440 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6441 already.
6442
6443 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6444 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6445 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6446 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6447 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6448 STUB_COMP_DIR may be non-NULL.
3e225074 6449 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6450 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6451 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6452 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6453 kept around for at least as long as *RESULT_READER.
6454
b0c7bfa9
DE
6455 The result is non-zero if a valid (non-dummy) DIE was found. */
6456
6457static int
6458read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6459 struct dwo_unit *dwo_unit,
b0c7bfa9 6460 struct die_info *stub_comp_unit_die,
a2ce51a0 6461 const char *stub_comp_dir,
b0c7bfa9 6462 struct die_reader_specs *result_reader,
d521ce57 6463 const gdb_byte **result_info_ptr,
b0c7bfa9 6464 struct die_info **result_comp_unit_die,
685af9cd 6465 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6466{
ed2dc618 6467 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6468 struct objfile *objfile = dwarf2_per_objfile->objfile;
6469 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6470 bfd *abfd;
d521ce57 6471 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6472 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6473 int i,num_extra_attrs;
6474 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6475 struct die_info *comp_unit_die;
6476
b0aeadb3
DE
6477 /* At most one of these may be provided. */
6478 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6479
b0c7bfa9
DE
6480 /* These attributes aren't processed until later:
6481 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6482 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6483 referenced later. However, these attributes are found in the stub
6484 which we won't have later. In order to not impose this complication
6485 on the rest of the code, we read them here and copy them to the
6486 DWO CU/TU die. */
b0c7bfa9
DE
6487
6488 stmt_list = NULL;
6489 low_pc = NULL;
6490 high_pc = NULL;
6491 ranges = NULL;
6492 comp_dir = NULL;
6493
6494 if (stub_comp_unit_die != NULL)
6495 {
6496 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6497 DWO file. */
6498 if (! this_cu->is_debug_types)
6499 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6500 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6501 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6502 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6503 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6504
18a8505e 6505 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6506
18a8505e
AT
6507 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6508 here (if needed). We need the value before we can process
6509 DW_AT_ranges. */
6510 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6511 }
a2ce51a0
DE
6512 else if (stub_comp_dir != NULL)
6513 {
6514 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6515 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6516 comp_dir->name = DW_AT_comp_dir;
6517 comp_dir->form = DW_FORM_string;
6518 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6519 DW_STRING (comp_dir) = stub_comp_dir;
6520 }
b0c7bfa9
DE
6521
6522 /* Set up for reading the DWO CU/TU. */
6523 cu->dwo_unit = dwo_unit;
685af9cd 6524 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6525 section->read (objfile);
6526 abfd = section->get_bfd_owner ();
9c541725
PA
6527 begin_info_ptr = info_ptr = (section->buffer
6528 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6529 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6530
6531 if (this_cu->is_debug_types)
6532 {
b0c7bfa9
DE
6533 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6534
ed2dc618
SM
6535 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6536 &cu->header, section,
b0c7bfa9 6537 dwo_abbrev_section,
43988095 6538 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6539 /* This is not an assert because it can be caused by bad debug info. */
43988095 6540 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6541 {
6542 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6543 " TU at offset %s [in module %s]"),
a2ce51a0 6544 hex_string (sig_type->signature),
43988095 6545 hex_string (cu->header.signature),
9d8780f0 6546 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6547 bfd_get_filename (abfd));
6548 }
9c541725 6549 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6550 /* For DWOs coming from DWP files, we don't know the CU length
6551 nor the type's offset in the TU until now. */
4057dfde 6552 dwo_unit->length = cu->header.get_length ();
9c541725 6553 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6554
6555 /* Establish the type offset that can be used to lookup the type.
6556 For DWO files, we don't know it until now. */
9c541725
PA
6557 sig_type->type_offset_in_section
6558 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6559 }
6560 else
6561 {
ed2dc618
SM
6562 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6563 &cu->header, section,
b0c7bfa9 6564 dwo_abbrev_section,
43988095 6565 info_ptr, rcuh_kind::COMPILE);
9c541725 6566 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6567 /* For DWOs coming from DWP files, we don't know the CU length
6568 until now. */
4057dfde 6569 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6570 }
6571
685af9cd 6572 *result_dwo_abbrev_table
86de1d91
TT
6573 = abbrev_table::read (objfile, dwo_abbrev_section,
6574 cu->header.abbrev_sect_off);
685af9cd
TT
6575 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6576 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6577
6578 /* Read in the die, but leave space to copy over the attributes
6579 from the stub. This has the benefit of simplifying the rest of
6580 the code - all the work to maintain the illusion of a single
6581 DW_TAG_{compile,type}_unit DIE is done here. */
6582 num_extra_attrs = ((stmt_list != NULL)
6583 + (low_pc != NULL)
6584 + (high_pc != NULL)
6585 + (ranges != NULL)
6586 + (comp_dir != NULL));
6587 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6588 num_extra_attrs);
b0c7bfa9
DE
6589
6590 /* Copy over the attributes from the stub to the DIE we just read in. */
6591 comp_unit_die = *result_comp_unit_die;
6592 i = comp_unit_die->num_attrs;
6593 if (stmt_list != NULL)
6594 comp_unit_die->attrs[i++] = *stmt_list;
6595 if (low_pc != NULL)
6596 comp_unit_die->attrs[i++] = *low_pc;
6597 if (high_pc != NULL)
6598 comp_unit_die->attrs[i++] = *high_pc;
6599 if (ranges != NULL)
6600 comp_unit_die->attrs[i++] = *ranges;
6601 if (comp_dir != NULL)
6602 comp_unit_die->attrs[i++] = *comp_dir;
6603 comp_unit_die->num_attrs += num_extra_attrs;
6604
b4f54984 6605 if (dwarf_die_debug)
bf6af496
DE
6606 {
6607 fprintf_unfiltered (gdb_stdlog,
6608 "Read die from %s@0x%x of %s:\n",
96b79293 6609 section->get_name (),
bf6af496
DE
6610 (unsigned) (begin_info_ptr - section->buffer),
6611 bfd_get_filename (abfd));
b4f54984 6612 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6613 }
6614
b0c7bfa9
DE
6615 /* Skip dummy compilation units. */
6616 if (info_ptr >= begin_info_ptr + dwo_unit->length
6617 || peek_abbrev_code (abfd, info_ptr) == 0)
6618 return 0;
6619
6620 *result_info_ptr = info_ptr;
6621 return 1;
6622}
6623
a084a2a6
AT
6624/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6625 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6626 signature is part of the header. */
6627static gdb::optional<ULONGEST>
6628lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6629{
6630 if (cu->header.version >= 5)
6631 return cu->header.signature;
6632 struct attribute *attr;
6633 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6634 if (attr == nullptr)
6635 return gdb::optional<ULONGEST> ();
6636 return DW_UNSND (attr);
6637}
6638
c0ab21c2 6639/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6640 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6641 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6642
6643static struct dwo_unit *
6644lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6645 struct die_info *comp_unit_die,
6646 const char *dwo_name)
b0c7bfa9
DE
6647{
6648 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6649 struct dwo_unit *dwo_unit;
c0ab21c2 6650 const char *comp_dir;
b0c7bfa9 6651
a2ce51a0
DE
6652 gdb_assert (cu != NULL);
6653
b0c7bfa9 6654 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6655 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6656 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6657
6658 if (this_cu->is_debug_types)
6659 {
6660 struct signatured_type *sig_type;
6661
6662 /* Since this_cu is the first member of struct signatured_type,
6663 we can go from a pointer to one to a pointer to the other. */
6664 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6665 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6666 }
6667 else
6668 {
a084a2a6
AT
6669 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6670 if (!signature.has_value ())
b0c7bfa9
DE
6671 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6672 " [in module %s]"),
e3b94546 6673 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6674 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6675 *signature);
b0c7bfa9
DE
6676 }
6677
b0c7bfa9
DE
6678 return dwo_unit;
6679}
6680
c0ab21c2 6681/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6682 See it for a description of the parameters.
fcd3b13d 6683 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6684
c0ab21c2
TT
6685void
6686cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6687 int use_existing_cu)
a2ce51a0 6688{
a2ce51a0 6689 struct signatured_type *sig_type;
a2ce51a0
DE
6690
6691 /* Verify we can do the following downcast, and that we have the
6692 data we need. */
6693 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6694 sig_type = (struct signatured_type *) this_cu;
6695 gdb_assert (sig_type->dwo_unit != NULL);
6696
6aa5f3a6
DE
6697 if (use_existing_cu && this_cu->cu != NULL)
6698 {
6699 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6700 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6701 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6702 }
6703 else
6704 {
6705 /* If !use_existing_cu, this_cu->cu must be NULL. */
6706 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6707 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6708 }
6709
6710 /* A future optimization, if needed, would be to use an existing
6711 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6712 could share abbrev tables. */
a2ce51a0
DE
6713
6714 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6715 NULL /* stub_comp_unit_die */,
6716 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6717 this, &info_ptr,
3e225074 6718 &comp_unit_die,
c0ab21c2 6719 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6720 {
6721 /* Dummy die. */
c0ab21c2 6722 dummy_p = true;
a2ce51a0 6723 }
a2ce51a0
DE
6724}
6725
fd820528 6726/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6727 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6728
f4dc4d17
DE
6729 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6730 Otherwise the table specified in the comp unit header is read in and used.
6731 This is an optimization for when we already have the abbrev table.
6732
dee91e82 6733 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6734 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6735
c0ab21c2
TT
6736cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6737 struct abbrev_table *abbrev_table,
6751ebae 6738 int use_existing_cu,
c0ab21c2
TT
6739 bool skip_partial)
6740 : die_reader_specs {},
6751ebae 6741 m_this_cu (this_cu)
c906108c 6742{
ed2dc618 6743 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6744 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6745 struct dwarf2_section_info *section = this_cu->section;
96b79293 6746 bfd *abfd = section->get_bfd_owner ();
dee91e82 6747 struct dwarf2_cu *cu;
c0ab21c2 6748 const gdb_byte *begin_info_ptr;
dee91e82 6749 struct signatured_type *sig_type = NULL;
4bdcc0c1 6750 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6751 /* Non-zero if CU currently points to a DWO file and we need to
6752 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6753 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6754 int rereading_dwo_cu = 0;
c906108c 6755
b4f54984 6756 if (dwarf_die_debug)
9d8780f0 6757 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6758 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6759 sect_offset_str (this_cu->sect_off));
09406207 6760
a2ce51a0
DE
6761 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6762 file (instead of going through the stub), short-circuit all of this. */
6763 if (this_cu->reading_dwo_directly)
6764 {
6765 /* Narrow down the scope of possibilities to have to understand. */
6766 gdb_assert (this_cu->is_debug_types);
6767 gdb_assert (abbrev_table == NULL);
6751ebae 6768 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6769 return;
6770 }
6771
dee91e82 6772 /* This is cheap if the section is already read in. */
96b79293 6773 section->read (objfile);
dee91e82 6774
9c541725 6775 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6776
6777 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6778
6779 if (use_existing_cu && this_cu->cu != NULL)
6780 {
6781 cu = this_cu->cu;
42e7ad6c
DE
6782 /* If this CU is from a DWO file we need to start over, we need to
6783 refetch the attributes from the skeleton CU.
6784 This could be optimized by retrieving those attributes from when we
6785 were here the first time: the previous comp_unit_die was stored in
6786 comp_unit_obstack. But there's no data yet that we need this
6787 optimization. */
6788 if (cu->dwo_unit != NULL)
6789 rereading_dwo_cu = 1;
dee91e82
DE
6790 }
6791 else
6792 {
6793 /* If !use_existing_cu, this_cu->cu must be NULL. */
6794 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6795 m_new_cu.reset (new dwarf2_cu (this_cu));
6796 cu = m_new_cu.get ();
42e7ad6c 6797 }
dee91e82 6798
b0c7bfa9 6799 /* Get the header. */
9c541725 6800 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6801 {
6802 /* We already have the header, there's no need to read it in again. */
9c541725 6803 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6804 }
6805 else
6806 {
3019eac3 6807 if (this_cu->is_debug_types)
dee91e82 6808 {
ed2dc618
SM
6809 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6810 &cu->header, section,
4bdcc0c1 6811 abbrev_section, info_ptr,
43988095 6812 rcuh_kind::TYPE);
dee91e82 6813
42e7ad6c
DE
6814 /* Since per_cu is the first member of struct signatured_type,
6815 we can go from a pointer to one to a pointer to the other. */
6816 sig_type = (struct signatured_type *) this_cu;
43988095 6817 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6818 gdb_assert (sig_type->type_offset_in_tu
6819 == cu->header.type_cu_offset_in_tu);
6820 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6821
42e7ad6c
DE
6822 /* LENGTH has not been set yet for type units if we're
6823 using .gdb_index. */
4057dfde 6824 this_cu->length = cu->header.get_length ();
3019eac3
DE
6825
6826 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6827 sig_type->type_offset_in_section =
6828 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6829
6830 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6831 }
6832 else
6833 {
ed2dc618
SM
6834 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6835 &cu->header, section,
4bdcc0c1 6836 abbrev_section,
43988095
JK
6837 info_ptr,
6838 rcuh_kind::COMPILE);
dee91e82 6839
9c541725 6840 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6841 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6842 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6843 }
6844 }
10b3939b 6845
6caca83c 6846 /* Skip dummy compilation units. */
dee91e82 6847 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6848 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6849 {
6850 dummy_p = true;
6851 return;
6852 }
6caca83c 6853
433df2d4
DE
6854 /* If we don't have them yet, read the abbrevs for this compilation unit.
6855 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6856 done. */
f4dc4d17 6857 if (abbrev_table != NULL)
685af9cd
TT
6858 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6859 else
f4dc4d17 6860 {
c0ab21c2 6861 m_abbrev_table_holder
86de1d91
TT
6862 = abbrev_table::read (objfile, abbrev_section,
6863 cu->header.abbrev_sect_off);
c0ab21c2 6864 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6865 }
af703f96 6866
dee91e82 6867 /* Read the top level CU/TU die. */
c0ab21c2 6868 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6869 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6870
58f0c718 6871 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6872 {
6873 dummy_p = true;
6874 return;
6875 }
58f0c718 6876
b0c7bfa9 6877 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6878 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6879 table from the DWO file and pass the ownership over to us. It will be
6880 referenced from READER, so we must make sure to free it after we're done
6881 with READER.
6882
b0c7bfa9
DE
6883 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6884 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6885 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6886 if (dwo_name != nullptr)
3019eac3 6887 {
3019eac3 6888 struct dwo_unit *dwo_unit;
b0c7bfa9 6889 struct die_info *dwo_comp_unit_die;
3019eac3 6890
3e225074 6891 if (comp_unit_die->has_children)
6a506a2d 6892 {
b98664d3 6893 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6894 " has children (offset %s) [in module %s]"),
6895 sect_offset_str (this_cu->sect_off),
6896 bfd_get_filename (abfd));
6a506a2d 6897 }
c0ab21c2 6898 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6899 if (dwo_unit != NULL)
3019eac3 6900 {
6a506a2d 6901 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6902 comp_unit_die, NULL,
c0ab21c2 6903 this, &info_ptr,
3e225074 6904 &dwo_comp_unit_die,
c0ab21c2 6905 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6906 {
6907 /* Dummy die. */
c0ab21c2 6908 dummy_p = true;
6a506a2d
DE
6909 return;
6910 }
6911 comp_unit_die = dwo_comp_unit_die;
6912 }
6913 else
6914 {
6915 /* Yikes, we couldn't find the rest of the DIE, we only have
6916 the stub. A complaint has already been logged. There's
6917 not much more we can do except pass on the stub DIE to
6918 die_reader_func. We don't want to throw an error on bad
6919 debug info. */
3019eac3
DE
6920 }
6921 }
c0ab21c2 6922}
3019eac3 6923
6751ebae
TT
6924void
6925cutu_reader::keep ()
c0ab21c2 6926{
b0c7bfa9 6927 /* Done, clean up. */
6751ebae
TT
6928 gdb_assert (!dummy_p);
6929 if (m_new_cu != NULL)
348e048f 6930 {
c0ab21c2
TT
6931 struct dwarf2_per_objfile *dwarf2_per_objfile
6932 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 6933 /* Link this CU into read_in_chain. */
c0ab21c2
TT
6934 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6935 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 6936 /* The chain owns it now. */
c0ab21c2 6937 m_new_cu.release ();
348e048f 6938 }
dee91e82
DE
6939}
6940
18a8505e
AT
6941/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6942 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6943 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6944
6945 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6946 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6947
6948 We fill in THIS_CU->length.
6949
dee91e82 6950 THIS_CU->cu is always freed when done.
3019eac3 6951 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6952 to care whether it refers to the "main" CU or the DWO CU.
6953
6954 When parent_cu is passed, it is used to provide a default value for
6955 str_offsets_base and addr_base from the parent. */
dee91e82 6956
c0ab21c2
TT
6957cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6958 struct dwarf2_cu *parent_cu,
6959 struct dwo_file *dwo_file)
6960 : die_reader_specs {},
6961 m_this_cu (this_cu)
dee91e82 6962{
ed2dc618 6963 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6964 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6965 struct dwarf2_section_info *section = this_cu->section;
96b79293 6966 bfd *abfd = section->get_bfd_owner ();
33e80786 6967 struct dwarf2_section_info *abbrev_section;
d521ce57 6968 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6969
b4f54984 6970 if (dwarf_die_debug)
9d8780f0 6971 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6972 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6973 sect_offset_str (this_cu->sect_off));
09406207 6974
dee91e82
DE
6975 gdb_assert (this_cu->cu == NULL);
6976
33e80786
DE
6977 abbrev_section = (dwo_file != NULL
6978 ? &dwo_file->sections.abbrev
6979 : get_abbrev_section_for_cu (this_cu));
6980
dee91e82 6981 /* This is cheap if the section is already read in. */
96b79293 6982 section->read (objfile);
dee91e82 6983
c0ab21c2 6984 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 6985
9c541725 6986 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 6987 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 6988 &m_new_cu->header, section,
4bdcc0c1 6989 abbrev_section, info_ptr,
43988095
JK
6990 (this_cu->is_debug_types
6991 ? rcuh_kind::TYPE
6992 : rcuh_kind::COMPILE));
dee91e82 6993
18a8505e
AT
6994 if (parent_cu != nullptr)
6995 {
c0ab21c2
TT
6996 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6997 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 6998 }
4057dfde 6999 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7000
7001 /* Skip dummy compilation units. */
7002 if (info_ptr >= begin_info_ptr + this_cu->length
7003 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7004 {
7005 dummy_p = true;
7006 return;
7007 }
72bf9492 7008
c0ab21c2 7009 m_abbrev_table_holder
86de1d91
TT
7010 = abbrev_table::read (objfile, abbrev_section,
7011 m_new_cu->header.abbrev_sect_off);
dee91e82 7012
c0ab21c2
TT
7013 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7014 m_abbrev_table_holder.get ());
3e225074 7015 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7016}
7017
0018ea6f
DE
7018\f
7019/* Type Unit Groups.
dee91e82 7020
0018ea6f
DE
7021 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7022 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7023 so that all types coming from the same compilation (.o file) are grouped
7024 together. A future step could be to put the types in the same symtab as
7025 the CU the types ultimately came from. */
ff013f42 7026
f4dc4d17
DE
7027static hashval_t
7028hash_type_unit_group (const void *item)
7029{
9a3c8263
SM
7030 const struct type_unit_group *tu_group
7031 = (const struct type_unit_group *) item;
f4dc4d17 7032
094b34ac 7033 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7034}
348e048f
DE
7035
7036static int
f4dc4d17 7037eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7038{
9a3c8263
SM
7039 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7040 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7041
094b34ac 7042 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7043}
348e048f 7044
f4dc4d17
DE
7045/* Allocate a hash table for type unit groups. */
7046
eaa5fa8b 7047static htab_up
298e9637 7048allocate_type_unit_groups_table ()
f4dc4d17 7049{
eaa5fa8b
TT
7050 return htab_up (htab_create_alloc (3,
7051 hash_type_unit_group,
7052 eq_type_unit_group,
7053 NULL, xcalloc, xfree));
f4dc4d17 7054}
dee91e82 7055
f4dc4d17
DE
7056/* Type units that don't have DW_AT_stmt_list are grouped into their own
7057 partial symtabs. We combine several TUs per psymtab to not let the size
7058 of any one psymtab grow too big. */
7059#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7060#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7061
094b34ac 7062/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7063 Create the type_unit_group object used to hold one or more TUs. */
7064
7065static struct type_unit_group *
094b34ac 7066create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7067{
518817b3
SM
7068 struct dwarf2_per_objfile *dwarf2_per_objfile
7069 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7070 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7071 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7072 struct type_unit_group *tu_group;
f4dc4d17
DE
7073
7074 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7075 struct type_unit_group);
094b34ac 7076 per_cu = &tu_group->per_cu;
518817b3 7077 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7078
094b34ac
DE
7079 if (dwarf2_per_objfile->using_index)
7080 {
7081 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7082 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7083 }
7084 else
7085 {
9c541725 7086 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7087 dwarf2_psymtab *pst;
528e1572 7088 std::string name;
094b34ac
DE
7089
7090 /* Give the symtab a useful name for debug purposes. */
7091 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7092 name = string_printf ("<type_units_%d>",
7093 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7094 else
528e1572 7095 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7096
528e1572 7097 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7098 pst->anonymous = true;
094b34ac 7099 }
f4dc4d17 7100
094b34ac 7101 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7102 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7103
7104 return tu_group;
7105}
7106
094b34ac
DE
7107/* Look up the type_unit_group for type unit CU, and create it if necessary.
7108 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7109
7110static struct type_unit_group *
ff39bb5e 7111get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7112{
518817b3
SM
7113 struct dwarf2_per_objfile *dwarf2_per_objfile
7114 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7115 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7116 struct type_unit_group *tu_group;
7117 void **slot;
7118 unsigned int line_offset;
7119 struct type_unit_group type_unit_group_for_lookup;
7120
7121 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7122 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7123
7124 /* Do we need to create a new group, or can we use an existing one? */
7125
7126 if (stmt_list)
7127 {
7128 line_offset = DW_UNSND (stmt_list);
7129 ++tu_stats->nr_symtab_sharers;
7130 }
7131 else
7132 {
7133 /* Ugh, no stmt_list. Rare, but we have to handle it.
7134 We can do various things here like create one group per TU or
7135 spread them over multiple groups to split up the expansion work.
7136 To avoid worst case scenarios (too many groups or too large groups)
7137 we, umm, group them in bunches. */
7138 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7139 | (tu_stats->nr_stmt_less_type_units
7140 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7141 ++tu_stats->nr_stmt_less_type_units;
7142 }
7143
094b34ac 7144 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7145 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7146 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7147 &type_unit_group_for_lookup, INSERT);
7148 if (*slot != NULL)
7149 {
9a3c8263 7150 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7151 gdb_assert (tu_group != NULL);
7152 }
7153 else
7154 {
9c541725 7155 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7156 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7157 *slot = tu_group;
7158 ++tu_stats->nr_symtabs;
7159 }
7160
7161 return tu_group;
7162}
0018ea6f
DE
7163\f
7164/* Partial symbol tables. */
7165
7166/* Create a psymtab named NAME and assign it to PER_CU.
7167
7168 The caller must fill in the following details:
7169 dirname, textlow, texthigh. */
7170
891813be 7171static dwarf2_psymtab *
0018ea6f
DE
7172create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7173{
e3b94546 7174 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7175 dwarf2_psymtab *pst;
0018ea6f 7176
891813be 7177 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7178
6d94535f 7179 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7180
7181 /* This is the glue that links PST into GDB's symbol API. */
891813be 7182 pst->per_cu_data = per_cu;
0018ea6f
DE
7183 per_cu->v.psymtab = pst;
7184
7185 return pst;
7186}
7187
c0ab21c2 7188/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7189
7190static void
7191process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7192 const gdb_byte *info_ptr,
0018ea6f 7193 struct die_info *comp_unit_die,
c0ab21c2 7194 enum language pretend_language)
0018ea6f
DE
7195{
7196 struct dwarf2_cu *cu = reader->cu;
518817b3 7197 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7198 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7199 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7200 CORE_ADDR baseaddr;
7201 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7202 dwarf2_psymtab *pst;
3a2b436a 7203 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7204 const char *filename;
0018ea6f 7205
0018ea6f
DE
7206 gdb_assert (! per_cu->is_debug_types);
7207
c0ab21c2 7208 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7209
0018ea6f 7210 /* Allocate a new partial symbol table structure. */
2e927613
TV
7211 gdb::unique_xmalloc_ptr<char> debug_filename;
7212 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7213 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7214 if (filename == NULL)
0018ea6f 7215 filename = "";
2e927613
TV
7216 else if (strcmp (filename, artificial) == 0)
7217 {
7218 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7219 sect_offset_str (per_cu->sect_off),
7220 (char *) NULL));
2e927613
TV
7221 filename = debug_filename.get ();
7222 }
0018ea6f
DE
7223
7224 pst = create_partial_symtab (per_cu, filename);
7225
7226 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7227 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7228
b3b3bada 7229 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7230
7231 dwarf2_find_base_address (comp_unit_die, cu);
7232
7233 /* Possibly set the default values of LOWPC and HIGHPC from
7234 `DW_AT_ranges'. */
3a2b436a
JK
7235 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7236 &best_highpc, cu, pst);
7237 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7238 {
7239 CORE_ADDR low
7240 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7241 - baseaddr);
7242 CORE_ADDR high
7243 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7244 - baseaddr - 1);
7245 /* Store the contiguous range if it is not empty; it can be
7246 empty for CUs with no code. */
d320c2b5
TT
7247 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7248 low, high, pst);
79748972 7249 }
0018ea6f
DE
7250
7251 /* Check if comp unit has_children.
7252 If so, read the rest of the partial symbols from this comp unit.
7253 If not, there's no more debug_info for this comp unit. */
3e225074 7254 if (comp_unit_die->has_children)
0018ea6f
DE
7255 {
7256 struct partial_die_info *first_die;
7257 CORE_ADDR lowpc, highpc;
7258
7259 lowpc = ((CORE_ADDR) -1);
7260 highpc = ((CORE_ADDR) 0);
7261
7262 first_die = load_partial_dies (reader, info_ptr, 1);
7263
7264 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7265 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7266
7267 /* If we didn't find a lowpc, set it to highpc to avoid
7268 complaints from `maint check'. */
7269 if (lowpc == ((CORE_ADDR) -1))
7270 lowpc = highpc;
7271
7272 /* If the compilation unit didn't have an explicit address range,
7273 then use the information extracted from its child dies. */
e385593e 7274 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7275 {
7276 best_lowpc = lowpc;
7277 best_highpc = highpc;
7278 }
7279 }
4ae976d1 7280 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7281 best_lowpc + baseaddr)
7282 - baseaddr);
4ae976d1 7283 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7284 best_highpc + baseaddr)
7285 - baseaddr);
0018ea6f 7286
8763cede 7287 end_psymtab_common (objfile, pst);
0018ea6f 7288
ae640021 7289 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7290 {
7291 int i;
ae640021 7292 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7293
7294 /* Fill in 'dependencies' here; we fill in 'users' in a
7295 post-pass. */
7296 pst->number_of_dependencies = len;
a9342b62
TT
7297 pst->dependencies
7298 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7299 for (i = 0; i < len; ++i)
7300 {
7301 pst->dependencies[i]
7302 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7303 }
0018ea6f 7304
ae640021 7305 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7306 }
7307
7308 /* Get the list of files included in the current compilation unit,
7309 and build a psymtab for each of them. */
7310 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7311
b4f54984 7312 if (dwarf_read_debug)
b926417a
TT
7313 fprintf_unfiltered (gdb_stdlog,
7314 "Psymtab for %s unit @%s: %s - %s"
7315 ", %d global, %d static syms\n",
7316 per_cu->is_debug_types ? "type" : "comp",
7317 sect_offset_str (per_cu->sect_off),
7318 paddress (gdbarch, pst->text_low (objfile)),
7319 paddress (gdbarch, pst->text_high (objfile)),
7320 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7321}
7322
7323/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7324 Process compilation unit THIS_CU for a psymtab. */
7325
7326static void
7327process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7328 bool want_partial_unit,
b93601f3 7329 enum language pretend_language)
0018ea6f
DE
7330{
7331 /* If this compilation unit was already read in, free the
7332 cached copy in order to read it in again. This is
7333 necessary because we skipped some symbols when we first
7334 read in the compilation unit (see load_partial_dies).
7335 This problem could be avoided, but the benefit is unclear. */
7336 if (this_cu->cu != NULL)
7337 free_one_cached_comp_unit (this_cu);
7338
6751ebae 7339 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7340
58990295
TV
7341 switch (reader.comp_unit_die->tag)
7342 {
7343 case DW_TAG_compile_unit:
7344 this_cu->unit_type = DW_UT_compile;
7345 break;
7346 case DW_TAG_partial_unit:
7347 this_cu->unit_type = DW_UT_partial;
7348 break;
7349 default:
7350 abort ();
7351 }
7352
c0ab21c2 7353 if (reader.dummy_p)
f1902523 7354 {
c0ab21c2 7355 /* Nothing. */
f1902523 7356 }
c0ab21c2 7357 else if (this_cu->is_debug_types)
3e225074
TT
7358 build_type_psymtabs_reader (&reader, reader.info_ptr,
7359 reader.comp_unit_die);
135f5437
TT
7360 else if (want_partial_unit
7361 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7362 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7363 reader.comp_unit_die,
c0ab21c2 7364 pretend_language);
0018ea6f 7365
58990295
TV
7366 this_cu->lang = this_cu->cu->language;
7367
0018ea6f 7368 /* Age out any secondary CUs. */
ed2dc618 7369 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7370}
f4dc4d17
DE
7371
7372/* Reader function for build_type_psymtabs. */
7373
7374static void
7375build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7376 const gdb_byte *info_ptr,
3e225074 7377 struct die_info *type_unit_die)
f4dc4d17 7378{
ed2dc618 7379 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7380 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7381 struct objfile *objfile = dwarf2_per_objfile->objfile;
7382 struct dwarf2_cu *cu = reader->cu;
7383 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7384 struct signatured_type *sig_type;
f4dc4d17
DE
7385 struct type_unit_group *tu_group;
7386 struct attribute *attr;
7387 struct partial_die_info *first_die;
7388 CORE_ADDR lowpc, highpc;
891813be 7389 dwarf2_psymtab *pst;
f4dc4d17 7390
0186c6a7
DE
7391 gdb_assert (per_cu->is_debug_types);
7392 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7393
3e225074 7394 if (! type_unit_die->has_children)
f4dc4d17
DE
7395 return;
7396
052c8bb8 7397 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7398 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7399
df07e2c7 7400 if (tu_group->tus == nullptr)
a8b3b8e9 7401 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7402 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7403
7404 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7405 pst = create_partial_symtab (per_cu, "");
6d94535f 7406 pst->anonymous = true;
f4dc4d17
DE
7407
7408 first_die = load_partial_dies (reader, info_ptr, 1);
7409
7410 lowpc = (CORE_ADDR) -1;
7411 highpc = (CORE_ADDR) 0;
7412 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7413
8763cede 7414 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7415}
7416
73051182
DE
7417/* Struct used to sort TUs by their abbreviation table offset. */
7418
7419struct tu_abbrev_offset
7420{
b2bdb8cf
SM
7421 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7422 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7423 {}
7424
7425 signatured_type *sig_type;
73051182
DE
7426 sect_offset abbrev_offset;
7427};
7428
484cf504 7429/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7430
484cf504
TT
7431static bool
7432sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7433 const struct tu_abbrev_offset &b)
73051182 7434{
484cf504 7435 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7436}
7437
7438/* Efficiently read all the type units.
7439 This does the bulk of the work for build_type_psymtabs.
7440
7441 The efficiency is because we sort TUs by the abbrev table they use and
7442 only read each abbrev table once. In one program there are 200K TUs
7443 sharing 8K abbrev tables.
7444
7445 The main purpose of this function is to support building the
7446 dwarf2_per_objfile->type_unit_groups table.
7447 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7448 can collapse the search space by grouping them by stmt_list.
7449 The savings can be significant, in the same program from above the 200K TUs
7450 share 8K stmt_list tables.
7451
7452 FUNC is expected to call get_type_unit_group, which will create the
7453 struct type_unit_group if necessary and add it to
7454 dwarf2_per_objfile->type_unit_groups. */
7455
7456static void
ed2dc618 7457build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7458{
73051182 7459 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7460 abbrev_table_up abbrev_table;
73051182 7461 sect_offset abbrev_offset;
73051182
DE
7462
7463 /* It's up to the caller to not call us multiple times. */
7464 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7465
b2bdb8cf 7466 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7467 return;
7468
7469 /* TUs typically share abbrev tables, and there can be way more TUs than
7470 abbrev tables. Sort by abbrev table to reduce the number of times we
7471 read each abbrev table in.
7472 Alternatives are to punt or to maintain a cache of abbrev tables.
7473 This is simpler and efficient enough for now.
7474
7475 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7476 symtab to use). Typically TUs with the same abbrev offset have the same
7477 stmt_list value too so in practice this should work well.
7478
7479 The basic algorithm here is:
7480
7481 sort TUs by abbrev table
7482 for each TU with same abbrev table:
7483 read abbrev table if first user
7484 read TU top level DIE
7485 [IWBN if DWO skeletons had DW_AT_stmt_list]
7486 call FUNC */
7487
b4f54984 7488 if (dwarf_read_debug)
73051182
DE
7489 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7490
7491 /* Sort in a separate table to maintain the order of all_type_units
7492 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7493 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7494 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7495
7496 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7497 sorted_by_abbrev.emplace_back
7498 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7499 sig_type->per_cu.section,
7500 sig_type->per_cu.sect_off));
73051182 7501
484cf504
TT
7502 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7503 sort_tu_by_abbrev_offset);
73051182 7504
9c541725 7505 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7506
b2bdb8cf 7507 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7508 {
73051182
DE
7509 /* Switch to the next abbrev table if necessary. */
7510 if (abbrev_table == NULL
b2bdb8cf 7511 || tu.abbrev_offset != abbrev_offset)
73051182 7512 {
b2bdb8cf 7513 abbrev_offset = tu.abbrev_offset;
73051182 7514 abbrev_table =
86de1d91
TT
7515 abbrev_table::read (dwarf2_per_objfile->objfile,
7516 &dwarf2_per_objfile->abbrev,
7517 abbrev_offset);
73051182
DE
7518 ++tu_stats->nr_uniq_abbrev_tables;
7519 }
7520
c0ab21c2 7521 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7522 0, false);
c0ab21c2
TT
7523 if (!reader.dummy_p)
7524 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7525 reader.comp_unit_die);
73051182 7526 }
6aa5f3a6 7527}
73051182 7528
6aa5f3a6
DE
7529/* Print collected type unit statistics. */
7530
7531static void
ed2dc618 7532print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7533{
7534 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7535
7536 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7537 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7538 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7539 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7540 tu_stats->nr_uniq_abbrev_tables);
7541 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7542 tu_stats->nr_symtabs);
7543 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7544 tu_stats->nr_symtab_sharers);
7545 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7546 tu_stats->nr_stmt_less_type_units);
7547 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7548 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7549}
7550
f4dc4d17
DE
7551/* Traversal function for build_type_psymtabs. */
7552
7553static int
7554build_type_psymtab_dependencies (void **slot, void *info)
7555{
ed2dc618
SM
7556 struct dwarf2_per_objfile *dwarf2_per_objfile
7557 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7558 struct objfile *objfile = dwarf2_per_objfile->objfile;
7559 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7560 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7561 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7562 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7563 int i;
7564
7565 gdb_assert (len > 0);
197400e8 7566 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7567
7568 pst->number_of_dependencies = len;
a9342b62 7569 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7570 for (i = 0; i < len; ++i)
f4dc4d17 7571 {
df07e2c7 7572 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7573 gdb_assert (iter->per_cu.is_debug_types);
7574 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7575 iter->type_unit_group = tu_group;
f4dc4d17
DE
7576 }
7577
df07e2c7
AB
7578 delete tu_group->tus;
7579 tu_group->tus = nullptr;
348e048f
DE
7580
7581 return 1;
7582}
7583
7584/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7585 Build partial symbol tables for the .debug_types comp-units. */
7586
7587static void
ed2dc618 7588build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7589{
ed2dc618 7590 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7591 return;
7592
ed2dc618 7593 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7594}
f4dc4d17 7595
6aa5f3a6
DE
7596/* Traversal function for process_skeletonless_type_unit.
7597 Read a TU in a DWO file and build partial symbols for it. */
7598
7599static int
7600process_skeletonless_type_unit (void **slot, void *info)
7601{
7602 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7603 struct dwarf2_per_objfile *dwarf2_per_objfile
7604 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7605 struct signatured_type find_entry, *entry;
7606
7607 /* If this TU doesn't exist in the global table, add it and read it in. */
7608
7609 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7610 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7611
7612 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7613 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7614 &find_entry, INSERT);
6aa5f3a6
DE
7615 /* If we've already seen this type there's nothing to do. What's happening
7616 is we're doing our own version of comdat-folding here. */
7617 if (*slot != NULL)
7618 return 1;
7619
7620 /* This does the job that create_all_type_units would have done for
7621 this TU. */
ed2dc618
SM
7622 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7623 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7624 *slot = entry;
7625
7626 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7627 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7628 if (!reader.dummy_p)
7629 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7630 reader.comp_unit_die);
6aa5f3a6
DE
7631
7632 return 1;
7633}
7634
7635/* Traversal function for process_skeletonless_type_units. */
7636
7637static int
7638process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7639{
7640 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7641
7642 if (dwo_file->tus != NULL)
b0b6a987
TT
7643 htab_traverse_noresize (dwo_file->tus.get (),
7644 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7645
7646 return 1;
7647}
7648
7649/* Scan all TUs of DWO files, verifying we've processed them.
7650 This is needed in case a TU was emitted without its skeleton.
7651 Note: This can't be done until we know what all the DWO files are. */
7652
7653static void
ed2dc618 7654process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7655{
7656 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7657 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7658 && dwarf2_per_objfile->dwo_files != NULL)
7659 {
51ac9db5 7660 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7661 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7662 dwarf2_per_objfile);
6aa5f3a6 7663 }
348e048f
DE
7664}
7665
ed2dc618 7666/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7667
7668static void
ed2dc618 7669set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7670{
b76e467d 7671 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7672 {
891813be 7673 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7674
36586728
TT
7675 if (pst == NULL)
7676 continue;
7677
b76e467d 7678 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7679 {
7680 /* Set the 'user' field only if it is not already set. */
7681 if (pst->dependencies[j]->user == NULL)
7682 pst->dependencies[j]->user = pst;
7683 }
7684 }
7685}
7686
93311388
DE
7687/* Build the partial symbol table by doing a quick pass through the
7688 .debug_info and .debug_abbrev sections. */
72bf9492 7689
93311388 7690static void
ed2dc618 7691dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7692{
ed2dc618 7693 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7694
b4f54984 7695 if (dwarf_read_debug)
45cfd468
DE
7696 {
7697 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7698 objfile_name (objfile));
45cfd468
DE
7699 }
7700
76935768
TT
7701 scoped_restore restore_reading_psyms
7702 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7703 true);
98bfdba5 7704
96b79293 7705 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7706
93311388
DE
7707 /* Any cached compilation units will be linked by the per-objfile
7708 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7709 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7710
ed2dc618 7711 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7712
ed2dc618 7713 create_all_comp_units (dwarf2_per_objfile);
c906108c 7714
60606b2c
TT
7715 /* Create a temporary address map on a temporary obstack. We later
7716 copy this to the final obstack. */
8268c778 7717 auto_obstack temp_obstack;
791afaa2
TT
7718
7719 scoped_restore save_psymtabs_addrmap
d320c2b5 7720 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7721 addrmap_create_mutable (&temp_obstack));
72bf9492 7722
b76e467d 7723 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7724 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7725
6aa5f3a6 7726 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7727 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7728
7729 /* Now that all TUs have been processed we can fill in the dependencies. */
7730 if (dwarf2_per_objfile->type_unit_groups != NULL)
7731 {
eaa5fa8b 7732 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7733 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7734 }
7735
b4f54984 7736 if (dwarf_read_debug)
ed2dc618 7737 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7738
ed2dc618 7739 set_partial_user (dwarf2_per_objfile);
95554aad 7740
d320c2b5
TT
7741 objfile->partial_symtabs->psymtabs_addrmap
7742 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7743 objfile->partial_symtabs->obstack ());
791afaa2
TT
7744 /* At this point we want to keep the address map. */
7745 save_psymtabs_addrmap.release ();
ff013f42 7746
b4f54984 7747 if (dwarf_read_debug)
45cfd468 7748 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7749 objfile_name (objfile));
ae038cb0
DJ
7750}
7751
dee91e82
DE
7752/* Load the partial DIEs for a secondary CU into memory.
7753 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7754
dee91e82
DE
7755static void
7756load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7757{
6751ebae 7758 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7759
7760 if (!reader.dummy_p)
7761 {
7762 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7763 language_minimal);
7764
7765 /* Check if comp unit has_children.
7766 If so, read the rest of the partial symbols from this comp unit.
7767 If not, there's no more debug_info for this comp unit. */
3e225074 7768 if (reader.comp_unit_die->has_children)
c0ab21c2 7769 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7770
7771 reader.keep ();
c0ab21c2 7772 }
ae038cb0
DJ
7773}
7774
ae038cb0 7775static void
ed2dc618 7776read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7777 struct dwarf2_section_info *section,
f1902523 7778 struct dwarf2_section_info *abbrev_section,
b76e467d 7779 unsigned int is_dwz)
ae038cb0 7780{
d521ce57 7781 const gdb_byte *info_ptr;
ed2dc618 7782 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7783
b4f54984 7784 if (dwarf_read_debug)
bf6af496 7785 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7786 section->get_name (),
7787 section->get_file_name ());
bf6af496 7788
96b79293 7789 section->read (objfile);
ae038cb0 7790
36586728 7791 info_ptr = section->buffer;
6e70227d 7792
36586728 7793 while (info_ptr < section->buffer + section->size)
ae038cb0 7794 {
ae038cb0 7795 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7796
9c541725 7797 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7798
f1902523 7799 comp_unit_head cu_header;
ed2dc618
SM
7800 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7801 abbrev_section, info_ptr,
7802 rcuh_kind::COMPILE);
ae038cb0
DJ
7803
7804 /* Save the compilation unit for later lookup. */
f1902523
JK
7805 if (cu_header.unit_type != DW_UT_type)
7806 {
7807 this_cu = XOBNEW (&objfile->objfile_obstack,
7808 struct dwarf2_per_cu_data);
7809 memset (this_cu, 0, sizeof (*this_cu));
7810 }
7811 else
7812 {
7813 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7814 struct signatured_type);
7815 memset (sig_type, 0, sizeof (*sig_type));
7816 sig_type->signature = cu_header.signature;
7817 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7818 this_cu = &sig_type->per_cu;
7819 }
7820 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7821 this_cu->sect_off = sect_off;
f1902523 7822 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7823 this_cu->is_dwz = is_dwz;
e3b94546 7824 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7825 this_cu->section = section;
ae038cb0 7826
b76e467d 7827 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7828
7829 info_ptr = info_ptr + this_cu->length;
7830 }
36586728
TT
7831}
7832
7833/* Create a list of all compilation units in OBJFILE.
7834 This is only done for -readnow and building partial symtabs. */
7835
7836static void
ed2dc618 7837create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7838{
b76e467d 7839 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7840 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7841 &dwarf2_per_objfile->abbrev, 0);
36586728 7842
b76e467d 7843 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7844 if (dwz != NULL)
ed2dc618 7845 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7846 1);
c906108c
SS
7847}
7848
5734ee8b 7849/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7850 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7851 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7852 DW_AT_ranges). See the comments of add_partial_subprogram on how
7853 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7854
72bf9492
DJ
7855static void
7856scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7857 CORE_ADDR *highpc, int set_addrmap,
7858 struct dwarf2_cu *cu)
c906108c 7859{
72bf9492 7860 struct partial_die_info *pdi;
c906108c 7861
91c24f0a
DC
7862 /* Now, march along the PDI's, descending into ones which have
7863 interesting children but skipping the children of the other ones,
7864 until we reach the end of the compilation unit. */
c906108c 7865
72bf9492 7866 pdi = first_die;
91c24f0a 7867
72bf9492
DJ
7868 while (pdi != NULL)
7869 {
52356b79 7870 pdi->fixup (cu);
c906108c 7871
f55ee35c 7872 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7873 children, so we need to look at them. Ditto for anonymous
7874 enums. */
933c6fe4 7875
72bf9492 7876 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7877 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7878 || pdi->tag == DW_TAG_imported_unit
7879 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7880 {
72bf9492 7881 switch (pdi->tag)
c906108c
SS
7882 {
7883 case DW_TAG_subprogram:
b1dc1806 7884 case DW_TAG_inlined_subroutine:
cdc07690 7885 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7886 break;
72929c62 7887 case DW_TAG_constant:
c906108c
SS
7888 case DW_TAG_variable:
7889 case DW_TAG_typedef:
91c24f0a 7890 case DW_TAG_union_type:
72bf9492 7891 if (!pdi->is_declaration)
63d06c5c 7892 {
72bf9492 7893 add_partial_symbol (pdi, cu);
63d06c5c
DC
7894 }
7895 break;
c906108c 7896 case DW_TAG_class_type:
680b30c7 7897 case DW_TAG_interface_type:
c906108c 7898 case DW_TAG_structure_type:
72bf9492 7899 if (!pdi->is_declaration)
c906108c 7900 {
72bf9492 7901 add_partial_symbol (pdi, cu);
c906108c 7902 }
b7fee5a3
KS
7903 if ((cu->language == language_rust
7904 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7905 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7906 set_addrmap, cu);
c906108c 7907 break;
91c24f0a 7908 case DW_TAG_enumeration_type:
72bf9492
DJ
7909 if (!pdi->is_declaration)
7910 add_partial_enumeration (pdi, cu);
c906108c
SS
7911 break;
7912 case DW_TAG_base_type:
a02abb62 7913 case DW_TAG_subrange_type:
c906108c 7914 /* File scope base type definitions are added to the partial
c5aa993b 7915 symbol table. */
72bf9492 7916 add_partial_symbol (pdi, cu);
c906108c 7917 break;
d9fa45fe 7918 case DW_TAG_namespace:
cdc07690 7919 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7920 break;
5d7cb8df 7921 case DW_TAG_module:
59c35742
AB
7922 if (!pdi->is_declaration)
7923 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7924 break;
95554aad
TT
7925 case DW_TAG_imported_unit:
7926 {
7927 struct dwarf2_per_cu_data *per_cu;
7928
f4dc4d17
DE
7929 /* For now we don't handle imported units in type units. */
7930 if (cu->per_cu->is_debug_types)
7931 {
7932 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7933 " supported in type units [in module %s]"),
518817b3 7934 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
7935 }
7936
e3b94546
SM
7937 per_cu = dwarf2_find_containing_comp_unit
7938 (pdi->d.sect_off, pdi->is_dwz,
518817b3 7939 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
7940
7941 /* Go read the partial unit, if needed. */
7942 if (per_cu->v.psymtab == NULL)
135f5437 7943 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 7944
ae640021 7945 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7946 }
7947 break;
74921315
KS
7948 case DW_TAG_imported_declaration:
7949 add_partial_symbol (pdi, cu);
7950 break;
c906108c
SS
7951 default:
7952 break;
7953 }
7954 }
7955
72bf9492
DJ
7956 /* If the die has a sibling, skip to the sibling. */
7957
7958 pdi = pdi->die_sibling;
7959 }
7960}
7961
7962/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7963
72bf9492 7964 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7965 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7966 Enumerators are an exception; they use the scope of their parent
7967 enumeration type, i.e. the name of the enumeration type is not
7968 prepended to the enumerator.
91c24f0a 7969
72bf9492
DJ
7970 There are two complexities. One is DW_AT_specification; in this
7971 case "parent" means the parent of the target of the specification,
7972 instead of the direct parent of the DIE. The other is compilers
7973 which do not emit DW_TAG_namespace; in this case we try to guess
7974 the fully qualified name of structure types from their members'
7975 linkage names. This must be done using the DIE's children rather
7976 than the children of any DW_AT_specification target. We only need
7977 to do this for structures at the top level, i.e. if the target of
7978 any DW_AT_specification (if any; otherwise the DIE itself) does not
7979 have a parent. */
7980
7981/* Compute the scope prefix associated with PDI's parent, in
7982 compilation unit CU. The result will be allocated on CU's
7983 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7984 field. NULL is returned if no prefix is necessary. */
15d034d0 7985static const char *
72bf9492
DJ
7986partial_die_parent_scope (struct partial_die_info *pdi,
7987 struct dwarf2_cu *cu)
7988{
15d034d0 7989 const char *grandparent_scope;
72bf9492 7990 struct partial_die_info *parent, *real_pdi;
91c24f0a 7991
72bf9492
DJ
7992 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7993 then this means the parent of the specification DIE. */
7994
7995 real_pdi = pdi;
72bf9492 7996 while (real_pdi->has_specification)
fb816e8b 7997 {
122cf0f2
AB
7998 auto res = find_partial_die (real_pdi->spec_offset,
7999 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8000 real_pdi = res.pdi;
8001 cu = res.cu;
8002 }
72bf9492
DJ
8003
8004 parent = real_pdi->die_parent;
8005 if (parent == NULL)
8006 return NULL;
8007
8008 if (parent->scope_set)
8009 return parent->scope;
8010
52356b79 8011 parent->fixup (cu);
72bf9492 8012
10b3939b 8013 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8014
acebe513
UW
8015 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8016 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8017 Work around this problem here. */
8018 if (cu->language == language_cplus
6e70227d 8019 && parent->tag == DW_TAG_namespace
acebe513
UW
8020 && strcmp (parent->name, "::") == 0
8021 && grandparent_scope == NULL)
8022 {
8023 parent->scope = NULL;
8024 parent->scope_set = 1;
8025 return NULL;
8026 }
8027
0a4b0913 8028 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8029 if (pdi->tag == DW_TAG_enumerator)
8030 /* Enumerators should not get the name of the enumeration as a prefix. */
8031 parent->scope = grandparent_scope;
8032 else if (parent->tag == DW_TAG_namespace
f55ee35c 8033 || parent->tag == DW_TAG_module
72bf9492
DJ
8034 || parent->tag == DW_TAG_structure_type
8035 || parent->tag == DW_TAG_class_type
680b30c7 8036 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8037 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8038 || parent->tag == DW_TAG_enumeration_type
8039 || (cu->language == language_fortran
8040 && parent->tag == DW_TAG_subprogram
8041 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8042 {
8043 if (grandparent_scope == NULL)
8044 parent->scope = parent->name;
8045 else
3e43a32a
MS
8046 parent->scope = typename_concat (&cu->comp_unit_obstack,
8047 grandparent_scope,
f55ee35c 8048 parent->name, 0, cu);
72bf9492 8049 }
72bf9492
DJ
8050 else
8051 {
8052 /* FIXME drow/2004-04-01: What should we be doing with
8053 function-local names? For partial symbols, we should probably be
8054 ignoring them. */
fa9c3fa0
TT
8055 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8056 dwarf_tag_name (parent->tag),
8057 sect_offset_str (pdi->sect_off));
72bf9492 8058 parent->scope = grandparent_scope;
c906108c
SS
8059 }
8060
72bf9492
DJ
8061 parent->scope_set = 1;
8062 return parent->scope;
8063}
8064
8065/* Return the fully scoped name associated with PDI, from compilation unit
8066 CU. The result will be allocated with malloc. */
4568ecf9 8067
43816ebc 8068static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8069partial_die_full_name (struct partial_die_info *pdi,
8070 struct dwarf2_cu *cu)
8071{
15d034d0 8072 const char *parent_scope;
72bf9492 8073
98bfdba5
PA
8074 /* If this is a template instantiation, we can not work out the
8075 template arguments from partial DIEs. So, unfortunately, we have
8076 to go through the full DIEs. At least any work we do building
8077 types here will be reused if full symbols are loaded later. */
8078 if (pdi->has_template_arguments)
8079 {
52356b79 8080 pdi->fixup (cu);
98bfdba5
PA
8081
8082 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8083 {
8084 struct die_info *die;
8085 struct attribute attr;
8086 struct dwarf2_cu *ref_cu = cu;
8087
b64f50a1 8088 /* DW_FORM_ref_addr is using section offset. */
b4069958 8089 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8090 attr.form = DW_FORM_ref_addr;
9c541725 8091 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8092 die = follow_die_ref (NULL, &attr, &ref_cu);
8093
43816ebc 8094 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8095 }
8096 }
8097
72bf9492
DJ
8098 parent_scope = partial_die_parent_scope (pdi, cu);
8099 if (parent_scope == NULL)
8100 return NULL;
8101 else
43816ebc
TT
8102 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8103 pdi->name, 0, cu));
c906108c
SS
8104}
8105
8106static void
72bf9492 8107add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8108{
518817b3
SM
8109 struct dwarf2_per_objfile *dwarf2_per_objfile
8110 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8111 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8112 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8113 CORE_ADDR addr = 0;
15d034d0 8114 const char *actual_name = NULL;
e142c38c
DJ
8115 CORE_ADDR baseaddr;
8116
b3b3bada 8117 baseaddr = objfile->text_section_offset ();
c906108c 8118
43816ebc
TT
8119 gdb::unique_xmalloc_ptr<char> built_actual_name
8120 = partial_die_full_name (pdi, cu);
15d034d0 8121 if (built_actual_name != NULL)
43816ebc 8122 actual_name = built_actual_name.get ();
63d06c5c 8123
72bf9492
DJ
8124 if (actual_name == NULL)
8125 actual_name = pdi->name;
8126
c906108c
SS
8127 switch (pdi->tag)
8128 {
b1dc1806 8129 case DW_TAG_inlined_subroutine:
c906108c 8130 case DW_TAG_subprogram:
79748972
TT
8131 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8132 - baseaddr);
0a4b0913
AB
8133 if (pdi->is_external
8134 || cu->language == language_ada
8135 || (cu->language == language_fortran
8136 && pdi->die_parent != NULL
8137 && pdi->die_parent->tag == DW_TAG_subprogram))
8138 {
8139 /* Normally, only "external" DIEs are part of the global scope.
8140 But in Ada and Fortran, we want to be able to access nested
8141 procedures globally. So all Ada and Fortran subprograms are
8142 stored in the global scope. */
31edb802 8143 add_psymbol_to_list (actual_name,
15d034d0 8144 built_actual_name != NULL,
f47fb265 8145 VAR_DOMAIN, LOC_BLOCK,
79748972 8146 SECT_OFF_TEXT (objfile),
75aedd27 8147 psymbol_placement::GLOBAL,
79748972
TT
8148 addr,
8149 cu->language, objfile);
c906108c
SS
8150 }
8151 else
8152 {
31edb802 8153 add_psymbol_to_list (actual_name,
15d034d0 8154 built_actual_name != NULL,
f47fb265 8155 VAR_DOMAIN, LOC_BLOCK,
79748972 8156 SECT_OFF_TEXT (objfile),
75aedd27 8157 psymbol_placement::STATIC,
1762568f 8158 addr, cu->language, objfile);
c906108c 8159 }
0c1b455e
TT
8160
8161 if (pdi->main_subprogram && actual_name != NULL)
8162 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8163 break;
72929c62 8164 case DW_TAG_constant:
31edb802 8165 add_psymbol_to_list (actual_name,
75aedd27
TT
8166 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8167 -1, (pdi->is_external
8168 ? psymbol_placement::GLOBAL
8169 : psymbol_placement::STATIC),
8170 0, cu->language, objfile);
72929c62 8171 break;
c906108c 8172 case DW_TAG_variable:
95554aad
TT
8173 if (pdi->d.locdesc)
8174 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8175
95554aad 8176 if (pdi->d.locdesc
caac4577
JG
8177 && addr == 0
8178 && !dwarf2_per_objfile->has_section_at_zero)
8179 {
8180 /* A global or static variable may also have been stripped
8181 out by the linker if unused, in which case its address
8182 will be nullified; do not add such variables into partial
8183 symbol table then. */
8184 }
8185 else if (pdi->is_external)
c906108c
SS
8186 {
8187 /* Global Variable.
8188 Don't enter into the minimal symbol tables as there is
8189 a minimal symbol table entry from the ELF symbols already.
8190 Enter into partial symbol table if it has a location
8191 descriptor or a type.
8192 If the location descriptor is missing, new_symbol will create
8193 a LOC_UNRESOLVED symbol, the address of the variable will then
8194 be determined from the minimal symbol table whenever the variable
8195 is referenced.
8196 The address for the partial symbol table entry is not
8197 used by GDB, but it comes in handy for debugging partial symbol
8198 table building. */
8199
95554aad 8200 if (pdi->d.locdesc || pdi->has_type)
31edb802 8201 add_psymbol_to_list (actual_name,
15d034d0 8202 built_actual_name != NULL,
f47fb265 8203 VAR_DOMAIN, LOC_STATIC,
79748972 8204 SECT_OFF_TEXT (objfile),
75aedd27 8205 psymbol_placement::GLOBAL,
79748972 8206 addr, cu->language, objfile);
c906108c
SS
8207 }
8208 else
8209 {
ff908ebf
AW
8210 int has_loc = pdi->d.locdesc != NULL;
8211
8212 /* Static Variable. Skip symbols whose value we cannot know (those
8213 without location descriptors or constant values). */
8214 if (!has_loc && !pdi->has_const_value)
43816ebc 8215 return;
ff908ebf 8216
31edb802 8217 add_psymbol_to_list (actual_name,
15d034d0 8218 built_actual_name != NULL,
f47fb265 8219 VAR_DOMAIN, LOC_STATIC,
79748972 8220 SECT_OFF_TEXT (objfile),
75aedd27 8221 psymbol_placement::STATIC,
79748972 8222 has_loc ? addr : 0,
f47fb265 8223 cu->language, objfile);
c906108c
SS
8224 }
8225 break;
8226 case DW_TAG_typedef:
8227 case DW_TAG_base_type:
a02abb62 8228 case DW_TAG_subrange_type:
31edb802 8229 add_psymbol_to_list (actual_name,
15d034d0 8230 built_actual_name != NULL,
79748972 8231 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8232 psymbol_placement::STATIC,
1762568f 8233 0, cu->language, objfile);
c906108c 8234 break;
74921315 8235 case DW_TAG_imported_declaration:
72bf9492 8236 case DW_TAG_namespace:
31edb802 8237 add_psymbol_to_list (actual_name,
15d034d0 8238 built_actual_name != NULL,
79748972 8239 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8240 psymbol_placement::GLOBAL,
1762568f 8241 0, cu->language, objfile);
72bf9492 8242 break;
530e8392 8243 case DW_TAG_module:
a5fd13a9
BH
8244 /* With Fortran 77 there might be a "BLOCK DATA" module
8245 available without any name. If so, we skip the module as it
8246 doesn't bring any value. */
8247 if (actual_name != nullptr)
31edb802 8248 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8249 built_actual_name != NULL,
8250 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8251 psymbol_placement::GLOBAL,
8252 0, cu->language, objfile);
530e8392 8253 break;
c906108c 8254 case DW_TAG_class_type:
680b30c7 8255 case DW_TAG_interface_type:
c906108c
SS
8256 case DW_TAG_structure_type:
8257 case DW_TAG_union_type:
8258 case DW_TAG_enumeration_type:
fa4028e9
JB
8259 /* Skip external references. The DWARF standard says in the section
8260 about "Structure, Union, and Class Type Entries": "An incomplete
8261 structure, union or class type is represented by a structure,
8262 union or class entry that does not have a byte size attribute
8263 and that has a DW_AT_declaration attribute." */
8264 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8265 return;
fa4028e9 8266
63d06c5c
DC
8267 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8268 static vs. global. */
31edb802 8269 add_psymbol_to_list (actual_name,
15d034d0 8270 built_actual_name != NULL,
79748972 8271 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8272 cu->language == language_cplus
75aedd27
TT
8273 ? psymbol_placement::GLOBAL
8274 : psymbol_placement::STATIC,
1762568f 8275 0, cu->language, objfile);
c906108c 8276
c906108c
SS
8277 break;
8278 case DW_TAG_enumerator:
31edb802 8279 add_psymbol_to_list (actual_name,
15d034d0 8280 built_actual_name != NULL,
79748972 8281 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8282 cu->language == language_cplus
75aedd27
TT
8283 ? psymbol_placement::GLOBAL
8284 : psymbol_placement::STATIC,
1762568f 8285 0, cu->language, objfile);
c906108c
SS
8286 break;
8287 default:
8288 break;
8289 }
8290}
8291
5c4e30ca
DC
8292/* Read a partial die corresponding to a namespace; also, add a symbol
8293 corresponding to that namespace to the symbol table. NAMESPACE is
8294 the name of the enclosing namespace. */
91c24f0a 8295
72bf9492
DJ
8296static void
8297add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8298 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8299 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8300{
72bf9492 8301 /* Add a symbol for the namespace. */
e7c27a73 8302
72bf9492 8303 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8304
8305 /* Now scan partial symbols in that namespace. */
8306
91c24f0a 8307 if (pdi->has_children)
cdc07690 8308 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8309}
8310
5d7cb8df
JK
8311/* Read a partial die corresponding to a Fortran module. */
8312
8313static void
8314add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8315 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8316{
530e8392
KB
8317 /* Add a symbol for the namespace. */
8318
8319 add_partial_symbol (pdi, cu);
8320
f55ee35c 8321 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8322
8323 if (pdi->has_children)
cdc07690 8324 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8325}
8326
b1dc1806
XR
8327/* Read a partial die corresponding to a subprogram or an inlined
8328 subprogram and create a partial symbol for that subprogram.
8329 When the CU language allows it, this routine also defines a partial
8330 symbol for each nested subprogram that this subprogram contains.
8331 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8332 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8333
cdc07690
YQ
8334 PDI may also be a lexical block, in which case we simply search
8335 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8336 Again, this is only performed when the CU language allows this
8337 type of definitions. */
8338
8339static void
8340add_partial_subprogram (struct partial_die_info *pdi,
8341 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8342 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8343{
b1dc1806 8344 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8345 {
8346 if (pdi->has_pc_info)
8347 {
8348 if (pdi->lowpc < *lowpc)
8349 *lowpc = pdi->lowpc;
8350 if (pdi->highpc > *highpc)
8351 *highpc = pdi->highpc;
cdc07690 8352 if (set_addrmap)
5734ee8b 8353 {
518817b3 8354 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8355 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8356 CORE_ADDR baseaddr;
b926417a
TT
8357 CORE_ADDR this_highpc;
8358 CORE_ADDR this_lowpc;
5734ee8b 8359
b3b3bada 8360 baseaddr = objfile->text_section_offset ();
b926417a
TT
8361 this_lowpc
8362 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8363 pdi->lowpc + baseaddr)
8364 - baseaddr);
8365 this_highpc
8366 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8367 pdi->highpc + baseaddr)
8368 - baseaddr);
d320c2b5 8369 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8370 this_lowpc, this_highpc - 1,
9291a0cd 8371 cu->per_cu->v.psymtab);
5734ee8b 8372 }
481860b3
GB
8373 }
8374
8375 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8376 {
bc30ff58 8377 if (!pdi->is_declaration)
e8d05480
JB
8378 /* Ignore subprogram DIEs that do not have a name, they are
8379 illegal. Do not emit a complaint at this point, we will
8380 do so when we convert this psymtab into a symtab. */
8381 if (pdi->name)
8382 add_partial_symbol (pdi, cu);
bc30ff58
JB
8383 }
8384 }
6e70227d 8385
bc30ff58
JB
8386 if (! pdi->has_children)
8387 return;
8388
0a4b0913 8389 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8390 {
8391 pdi = pdi->die_child;
8392 while (pdi != NULL)
8393 {
52356b79 8394 pdi->fixup (cu);
bc30ff58 8395 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8396 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8397 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8398 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8399 pdi = pdi->die_sibling;
8400 }
8401 }
8402}
8403
91c24f0a
DC
8404/* Read a partial die corresponding to an enumeration type. */
8405
72bf9492
DJ
8406static void
8407add_partial_enumeration (struct partial_die_info *enum_pdi,
8408 struct dwarf2_cu *cu)
91c24f0a 8409{
72bf9492 8410 struct partial_die_info *pdi;
91c24f0a
DC
8411
8412 if (enum_pdi->name != NULL)
72bf9492
DJ
8413 add_partial_symbol (enum_pdi, cu);
8414
8415 pdi = enum_pdi->die_child;
8416 while (pdi)
91c24f0a 8417 {
72bf9492 8418 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8419 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8420 else
72bf9492
DJ
8421 add_partial_symbol (pdi, cu);
8422 pdi = pdi->die_sibling;
91c24f0a 8423 }
91c24f0a
DC
8424}
8425
6caca83c
CC
8426/* Return the initial uleb128 in the die at INFO_PTR. */
8427
8428static unsigned int
d521ce57 8429peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8430{
8431 unsigned int bytes_read;
8432
8433 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8434}
8435
685af9cd
TT
8436/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8437 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8438
4bb7a0a7
DJ
8439 Return the corresponding abbrev, or NULL if the number is zero (indicating
8440 an empty DIE). In either case *BYTES_READ will be set to the length of
8441 the initial number. */
8442
8443static struct abbrev_info *
685af9cd
TT
8444peek_die_abbrev (const die_reader_specs &reader,
8445 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8446{
685af9cd 8447 dwarf2_cu *cu = reader.cu;
518817b3 8448 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8449 unsigned int abbrev_number
8450 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8451
8452 if (abbrev_number == 0)
8453 return NULL;
8454
685af9cd 8455 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8456 if (!abbrev)
8457 {
422b9917 8458 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8459 " at offset %s [in module %s]"),
422b9917 8460 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8461 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8462 }
8463
8464 return abbrev;
8465}
8466
93311388
DE
8467/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8468 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8469 DIE. Any children of the skipped DIEs will also be skipped. */
8470
d521ce57
TT
8471static const gdb_byte *
8472skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8473{
4bb7a0a7
DJ
8474 while (1)
8475 {
685af9cd
TT
8476 unsigned int bytes_read;
8477 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8478
4bb7a0a7
DJ
8479 if (abbrev == NULL)
8480 return info_ptr + bytes_read;
8481 else
dee91e82 8482 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8483 }
8484}
8485
93311388
DE
8486/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8487 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8488 abbrev corresponding to that skipped uleb128 should be passed in
8489 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8490 children. */
8491
d521ce57
TT
8492static const gdb_byte *
8493skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8494 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8495{
8496 unsigned int bytes_read;
8497 struct attribute attr;
dee91e82
DE
8498 bfd *abfd = reader->abfd;
8499 struct dwarf2_cu *cu = reader->cu;
d521ce57 8500 const gdb_byte *buffer = reader->buffer;
f664829e 8501 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8502 unsigned int form, i;
8503
8504 for (i = 0; i < abbrev->num_attrs; i++)
8505 {
8506 /* The only abbrev we care about is DW_AT_sibling. */
8507 if (abbrev->attrs[i].name == DW_AT_sibling)
8508 {
18a8505e
AT
8509 bool ignored;
8510 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8511 &ignored);
4bb7a0a7 8512 if (attr.form == DW_FORM_ref_addr)
b98664d3 8513 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8514 else
b9502d3f 8515 {
9c541725
PA
8516 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8517 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8518
8519 if (sibling_ptr < info_ptr)
b98664d3 8520 complaint (_("DW_AT_sibling points backwards"));
22869d73 8521 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8522 reader->die_section->overflow_complaint ();
b9502d3f
WN
8523 else
8524 return sibling_ptr;
8525 }
4bb7a0a7
DJ
8526 }
8527
8528 /* If it isn't DW_AT_sibling, skip this attribute. */
8529 form = abbrev->attrs[i].form;
8530 skip_attribute:
8531 switch (form)
8532 {
4bb7a0a7 8533 case DW_FORM_ref_addr:
ae411497
TT
8534 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8535 and later it is offset sized. */
8536 if (cu->header.version == 2)
8537 info_ptr += cu->header.addr_size;
8538 else
8539 info_ptr += cu->header.offset_size;
8540 break;
36586728
TT
8541 case DW_FORM_GNU_ref_alt:
8542 info_ptr += cu->header.offset_size;
8543 break;
ae411497 8544 case DW_FORM_addr:
4bb7a0a7
DJ
8545 info_ptr += cu->header.addr_size;
8546 break;
8547 case DW_FORM_data1:
8548 case DW_FORM_ref1:
8549 case DW_FORM_flag:
8fe0f950 8550 case DW_FORM_strx1:
4bb7a0a7
DJ
8551 info_ptr += 1;
8552 break;
2dc7f7b3 8553 case DW_FORM_flag_present:
43988095 8554 case DW_FORM_implicit_const:
2dc7f7b3 8555 break;
4bb7a0a7
DJ
8556 case DW_FORM_data2:
8557 case DW_FORM_ref2:
8fe0f950 8558 case DW_FORM_strx2:
4bb7a0a7
DJ
8559 info_ptr += 2;
8560 break;
8fe0f950
AT
8561 case DW_FORM_strx3:
8562 info_ptr += 3;
8563 break;
4bb7a0a7
DJ
8564 case DW_FORM_data4:
8565 case DW_FORM_ref4:
8fe0f950 8566 case DW_FORM_strx4:
4bb7a0a7
DJ
8567 info_ptr += 4;
8568 break;
8569 case DW_FORM_data8:
8570 case DW_FORM_ref8:
55f1336d 8571 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8572 info_ptr += 8;
8573 break;
0224619f
JK
8574 case DW_FORM_data16:
8575 info_ptr += 16;
8576 break;
4bb7a0a7 8577 case DW_FORM_string:
9b1c24c8 8578 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8579 info_ptr += bytes_read;
8580 break;
2dc7f7b3 8581 case DW_FORM_sec_offset:
4bb7a0a7 8582 case DW_FORM_strp:
36586728 8583 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8584 info_ptr += cu->header.offset_size;
8585 break;
2dc7f7b3 8586 case DW_FORM_exprloc:
4bb7a0a7
DJ
8587 case DW_FORM_block:
8588 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8589 info_ptr += bytes_read;
8590 break;
8591 case DW_FORM_block1:
8592 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8593 break;
8594 case DW_FORM_block2:
8595 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8596 break;
8597 case DW_FORM_block4:
8598 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8599 break;
336d760d 8600 case DW_FORM_addrx:
cf532bd1 8601 case DW_FORM_strx:
4bb7a0a7
DJ
8602 case DW_FORM_sdata:
8603 case DW_FORM_udata:
8604 case DW_FORM_ref_udata:
3019eac3
DE
8605 case DW_FORM_GNU_addr_index:
8606 case DW_FORM_GNU_str_index:
18a8505e 8607 case DW_FORM_rnglistx:
d521ce57 8608 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8609 break;
8610 case DW_FORM_indirect:
8611 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8612 info_ptr += bytes_read;
8613 /* We need to continue parsing from here, so just go back to
8614 the top. */
8615 goto skip_attribute;
8616
8617 default:
3e43a32a
MS
8618 error (_("Dwarf Error: Cannot handle %s "
8619 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8620 dwarf_form_name (form),
8621 bfd_get_filename (abfd));
8622 }
8623 }
8624
8625 if (abbrev->has_children)
dee91e82 8626 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8627 else
8628 return info_ptr;
8629}
8630
93311388 8631/* Locate ORIG_PDI's sibling.
dee91e82 8632 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8633
d521ce57 8634static const gdb_byte *
dee91e82
DE
8635locate_pdi_sibling (const struct die_reader_specs *reader,
8636 struct partial_die_info *orig_pdi,
d521ce57 8637 const gdb_byte *info_ptr)
91c24f0a
DC
8638{
8639 /* Do we know the sibling already? */
72bf9492 8640
91c24f0a
DC
8641 if (orig_pdi->sibling)
8642 return orig_pdi->sibling;
8643
8644 /* Are there any children to deal with? */
8645
8646 if (!orig_pdi->has_children)
8647 return info_ptr;
8648
4bb7a0a7 8649 /* Skip the children the long way. */
91c24f0a 8650
dee91e82 8651 return skip_children (reader, info_ptr);
91c24f0a
DC
8652}
8653
257e7a09 8654/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8655 not NULL. */
c906108c 8656
891813be
TT
8657void
8658dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8659{
ed2dc618
SM
8660 struct dwarf2_per_objfile *dwarf2_per_objfile
8661 = get_dwarf2_per_objfile (objfile);
8662
077cbab2
TT
8663 gdb_assert (!readin);
8664 /* If this psymtab is constructed from a debug-only objfile, the
8665 has_section_at_zero flag will not necessarily be correct. We
8666 can get the correct value for this flag by looking at the data
8667 associated with the (presumably stripped) associated objfile. */
8668 if (objfile->separate_debug_objfile_backlink)
c906108c 8669 {
077cbab2
TT
8670 struct dwarf2_per_objfile *dpo_backlink
8671 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8672
077cbab2
TT
8673 dwarf2_per_objfile->has_section_at_zero
8674 = dpo_backlink->has_section_at_zero;
8675 }
98bfdba5 8676
8566b89b 8677 expand_psymtab (objfile);
95554aad 8678
ed2dc618 8679 process_cu_includes (dwarf2_per_objfile);
c906108c 8680}
9cdd5dbd
DE
8681\f
8682/* Reading in full CUs. */
c906108c 8683
10b3939b
DJ
8684/* Add PER_CU to the queue. */
8685
8686static void
95554aad
TT
8687queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8688 enum language pretend_language)
10b3939b 8689{
10b3939b 8690 per_cu->queued = 1;
39856def 8691 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8692}
8693
89e63ee4
DE
8694/* If PER_CU is not yet queued, add it to the queue.
8695 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8696 dependency.
0907af0c 8697 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8698 meaning either PER_CU is already queued or it is already loaded.
8699
8700 N.B. There is an invariant here that if a CU is queued then it is loaded.
8701 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8702
8703static int
89e63ee4 8704maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8705 struct dwarf2_per_cu_data *per_cu,
8706 enum language pretend_language)
8707{
8708 /* We may arrive here during partial symbol reading, if we need full
8709 DIEs to process an unusual case (e.g. template arguments). Do
8710 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8711 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8712 {
8713 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8714 return 1;
8715 return 0;
8716 }
8717
8718 /* Mark the dependence relation so that we don't flush PER_CU
8719 too early. */
89e63ee4
DE
8720 if (dependent_cu != NULL)
8721 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8722
8723 /* If it's already on the queue, we have nothing to do. */
8724 if (per_cu->queued)
8725 return 0;
8726
8727 /* If the compilation unit is already loaded, just mark it as
8728 used. */
8729 if (per_cu->cu != NULL)
8730 {
8731 per_cu->cu->last_used = 0;
8732 return 0;
8733 }
8734
8735 /* Add it to the queue. */
8736 queue_comp_unit (per_cu, pretend_language);
8737
8738 return 1;
8739}
8740
10b3939b
DJ
8741/* Process the queue. */
8742
8743static void
ed2dc618 8744process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8745{
b4f54984 8746 if (dwarf_read_debug)
45cfd468
DE
8747 {
8748 fprintf_unfiltered (gdb_stdlog,
8749 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8750 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8751 }
8752
03dd20cc
DJ
8753 /* The queue starts out with one item, but following a DIE reference
8754 may load a new CU, adding it to the end of the queue. */
39856def 8755 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8756 {
39856def
TT
8757 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8758
cc12ce38 8759 if ((dwarf2_per_objfile->using_index
39856def
TT
8760 ? !item.per_cu->v.quick->compunit_symtab
8761 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8762 /* Skip dummy CUs. */
39856def 8763 && item.per_cu->cu != NULL)
f4dc4d17 8764 {
39856def 8765 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8766 unsigned int debug_print_threshold;
247f5c4f 8767 char buf[100];
f4dc4d17 8768
247f5c4f 8769 if (per_cu->is_debug_types)
f4dc4d17 8770 {
247f5c4f
DE
8771 struct signatured_type *sig_type =
8772 (struct signatured_type *) per_cu;
8773
9d8780f0 8774 sprintf (buf, "TU %s at offset %s",
73be47f5 8775 hex_string (sig_type->signature),
9d8780f0 8776 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8777 /* There can be 100s of TUs.
8778 Only print them in verbose mode. */
8779 debug_print_threshold = 2;
f4dc4d17 8780 }
247f5c4f 8781 else
73be47f5 8782 {
9d8780f0
SM
8783 sprintf (buf, "CU at offset %s",
8784 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8785 debug_print_threshold = 1;
8786 }
247f5c4f 8787
b4f54984 8788 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8789 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8790
8791 if (per_cu->is_debug_types)
39856def 8792 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8793 else
39856def 8794 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8795
b4f54984 8796 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8797 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8798 }
10b3939b 8799
39856def
TT
8800 item.per_cu->queued = 0;
8801 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8802 }
8803
b4f54984 8804 if (dwarf_read_debug)
45cfd468
DE
8805 {
8806 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8807 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8808 }
10b3939b
DJ
8809}
8810
10b3939b
DJ
8811/* Read in full symbols for PST, and anything it depends on. */
8812
8566b89b
TT
8813void
8814dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8815{
8566b89b 8816 if (readin)
95554aad
TT
8817 return;
8818
0494dbec 8819 read_dependencies (objfile);
aaa75496 8820
b83470bf
TT
8821 dw2_do_instantiate_symtab (per_cu_data, false);
8822 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8823}
8824
dee91e82
DE
8825/* Trivial hash function for die_info: the hash value of a DIE
8826 is its offset in .debug_info for this objfile. */
10b3939b 8827
dee91e82
DE
8828static hashval_t
8829die_hash (const void *item)
10b3939b 8830{
9a3c8263 8831 const struct die_info *die = (const struct die_info *) item;
6502dd73 8832
9c541725 8833 return to_underlying (die->sect_off);
dee91e82 8834}
63d06c5c 8835
dee91e82
DE
8836/* Trivial comparison function for die_info structures: two DIEs
8837 are equal if they have the same offset. */
98bfdba5 8838
dee91e82
DE
8839static int
8840die_eq (const void *item_lhs, const void *item_rhs)
8841{
9a3c8263
SM
8842 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8843 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8844
9c541725 8845 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8846}
c906108c 8847
c0ab21c2 8848/* Load the DIEs associated with PER_CU into memory. */
c906108c 8849
dee91e82 8850static void
c0ab21c2
TT
8851load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8852 bool skip_partial,
8853 enum language pretend_language)
dee91e82 8854{
c0ab21c2
TT
8855 gdb_assert (! this_cu->is_debug_types);
8856
6751ebae 8857 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8858 if (reader.dummy_p)
8859 return;
8860
8861 struct dwarf2_cu *cu = reader.cu;
8862 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8863
dee91e82
DE
8864 gdb_assert (cu->die_hash == NULL);
8865 cu->die_hash =
8866 htab_create_alloc_ex (cu->header.length / 12,
8867 die_hash,
8868 die_eq,
8869 NULL,
8870 &cu->comp_unit_obstack,
8871 hashtab_obstack_allocate,
8872 dummy_obstack_deallocate);
e142c38c 8873
3e225074 8874 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8875 reader.comp_unit_die->child
8876 = read_die_and_siblings (&reader, reader.info_ptr,
8877 &info_ptr, reader.comp_unit_die);
8878 cu->dies = reader.comp_unit_die;
dee91e82 8879 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8880
8881 /* We try not to read any attributes in this function, because not
9cdd5dbd 8882 all CUs needed for references have been loaded yet, and symbol
10b3939b 8883 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8884 or we won't be able to build types correctly.
8885 Similarly, if we do not read the producer, we can not apply
8886 producer-specific interpretation. */
c0ab21c2 8887 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8888
8889 reader.keep ();
10b3939b
DJ
8890}
8891
3da10d80
KS
8892/* Add a DIE to the delayed physname list. */
8893
8894static void
8895add_to_method_list (struct type *type, int fnfield_index, int index,
8896 const char *name, struct die_info *die,
8897 struct dwarf2_cu *cu)
8898{
8899 struct delayed_method_info mi;
8900 mi.type = type;
8901 mi.fnfield_index = fnfield_index;
8902 mi.index = index;
8903 mi.name = name;
8904 mi.die = die;
c89b44cd 8905 cu->method_list.push_back (mi);
3da10d80
KS
8906}
8907
3693fdb3
PA
8908/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8909 "const" / "volatile". If so, decrements LEN by the length of the
8910 modifier and return true. Otherwise return false. */
8911
8912template<size_t N>
8913static bool
8914check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8915{
8916 size_t mod_len = sizeof (mod) - 1;
8917 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8918 {
8919 len -= mod_len;
8920 return true;
8921 }
8922 return false;
8923}
8924
3da10d80
KS
8925/* Compute the physnames of any methods on the CU's method list.
8926
8927 The computation of method physnames is delayed in order to avoid the
8928 (bad) condition that one of the method's formal parameters is of an as yet
8929 incomplete type. */
8930
8931static void
8932compute_delayed_physnames (struct dwarf2_cu *cu)
8933{
3693fdb3 8934 /* Only C++ delays computing physnames. */
c89b44cd 8935 if (cu->method_list.empty ())
3693fdb3
PA
8936 return;
8937 gdb_assert (cu->language == language_cplus);
8938
52941706 8939 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8940 {
1d06ead6 8941 const char *physname;
3da10d80 8942 struct fn_fieldlist *fn_flp
c89b44cd
TT
8943 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8944 physname = dwarf2_physname (mi.name, mi.die, cu);
8945 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8946 = physname ? physname : "";
3693fdb3
PA
8947
8948 /* Since there's no tag to indicate whether a method is a
8949 const/volatile overload, extract that information out of the
8950 demangled name. */
8951 if (physname != NULL)
8952 {
8953 size_t len = strlen (physname);
8954
8955 while (1)
8956 {
8957 if (physname[len] == ')') /* shortcut */
8958 break;
8959 else if (check_modifier (physname, len, " const"))
c89b44cd 8960 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8961 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8962 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8963 else
8964 break;
8965 }
8966 }
3da10d80 8967 }
c89b44cd
TT
8968
8969 /* The list is no longer needed. */
8970 cu->method_list.clear ();
3da10d80
KS
8971}
8972
a766d390
DE
8973/* Go objects should be embedded in a DW_TAG_module DIE,
8974 and it's not clear if/how imported objects will appear.
8975 To keep Go support simple until that's worked out,
8976 go back through what we've read and create something usable.
8977 We could do this while processing each DIE, and feels kinda cleaner,
8978 but that way is more invasive.
8979 This is to, for example, allow the user to type "p var" or "b main"
8980 without having to specify the package name, and allow lookups
8981 of module.object to work in contexts that use the expression
8982 parser. */
8983
8984static void
8985fixup_go_packaging (struct dwarf2_cu *cu)
8986{
421d1616 8987 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
8988 struct pending *list;
8989 int i;
8990
c24bdb02 8991 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
8992 list != NULL;
8993 list = list->next)
a766d390
DE
8994 {
8995 for (i = 0; i < list->nsyms; ++i)
8996 {
8997 struct symbol *sym = list->symbol[i];
8998
c1b5c1eb 8999 if (sym->language () == language_go
a766d390
DE
9000 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9001 {
421d1616
TT
9002 gdb::unique_xmalloc_ptr<char> this_package_name
9003 (go_symbol_package_name (sym));
a766d390
DE
9004
9005 if (this_package_name == NULL)
9006 continue;
9007 if (package_name == NULL)
421d1616 9008 package_name = std::move (this_package_name);
a766d390
DE
9009 else
9010 {
518817b3
SM
9011 struct objfile *objfile
9012 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9013 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9014 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9015 (symbol_symtab (sym) != NULL
9016 ? symtab_to_filename_for_display
9017 (symbol_symtab (sym))
e3b94546 9018 : objfile_name (objfile)),
421d1616 9019 this_package_name.get (), package_name.get ());
a766d390
DE
9020 }
9021 }
9022 }
9023 }
9024
9025 if (package_name != NULL)
9026 {
518817b3 9027 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9028 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9029 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9030 saved_package_name);
a766d390
DE
9031 struct symbol *sym;
9032
e623cf5d 9033 sym = allocate_symbol (objfile);
d3ecddab 9034 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9035 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9036 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9037 e.g., "main" finds the "main" module and not C's main(). */
9038 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9039 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9040 SYMBOL_TYPE (sym) = type;
9041
c24bdb02 9042 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9043 }
9044}
9045
c9317f21
TT
9046/* Allocate a fully-qualified name consisting of the two parts on the
9047 obstack. */
9048
9049static const char *
9050rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9051{
9052 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9053}
9054
9055/* A helper that allocates a struct discriminant_info to attach to a
9056 union type. */
9057
9058static struct discriminant_info *
9059alloc_discriminant_info (struct type *type, int discriminant_index,
9060 int default_index)
9061{
9062 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9063 gdb_assert (discriminant_index == -1
9064 || (discriminant_index >= 0
9065 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9066 gdb_assert (default_index == -1
c7b15a66 9067 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9068
9069 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9070
9071 struct discriminant_info *disc
9072 = ((struct discriminant_info *)
9073 TYPE_ZALLOC (type,
9074 offsetof (struct discriminant_info, discriminants)
9075 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9076 disc->default_index = default_index;
9077 disc->discriminant_index = discriminant_index;
9078
9079 struct dynamic_prop prop;
9080 prop.kind = PROP_UNDEFINED;
9081 prop.data.baton = disc;
9082
9083 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9084
9085 return disc;
9086}
9087
9088/* Some versions of rustc emitted enums in an unusual way.
9089
9090 Ordinary enums were emitted as unions. The first element of each
9091 structure in the union was named "RUST$ENUM$DISR". This element
9092 held the discriminant.
9093
9094 These versions of Rust also implemented the "non-zero"
9095 optimization. When the enum had two values, and one is empty and
9096 the other holds a pointer that cannot be zero, the pointer is used
9097 as the discriminant, with a zero value meaning the empty variant.
9098 Here, the union's first member is of the form
9099 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9100 where the fieldnos are the indices of the fields that should be
9101 traversed in order to find the field (which may be several fields deep)
9102 and the variantname is the name of the variant of the case when the
9103 field is zero.
9104
9105 This function recognizes whether TYPE is of one of these forms,
9106 and, if so, smashes it to be a variant type. */
9107
9108static void
9109quirk_rust_enum (struct type *type, struct objfile *objfile)
9110{
9111 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9112
9113 /* We don't need to deal with empty enums. */
9114 if (TYPE_NFIELDS (type) == 0)
9115 return;
9116
9117#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9118 if (TYPE_NFIELDS (type) == 1
9119 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9120 {
9121 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9122
9123 /* Decode the field name to find the offset of the
9124 discriminant. */
9125 ULONGEST bit_offset = 0;
9126 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9127 while (name[0] >= '0' && name[0] <= '9')
9128 {
9129 char *tail;
9130 unsigned long index = strtoul (name, &tail, 10);
9131 name = tail;
9132 if (*name != '$'
9133 || index >= TYPE_NFIELDS (field_type)
9134 || (TYPE_FIELD_LOC_KIND (field_type, index)
9135 != FIELD_LOC_KIND_BITPOS))
9136 {
b98664d3 9137 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9138 "[in module %s]"),
9139 TYPE_FIELD_NAME (type, 0),
9140 objfile_name (objfile));
9141 return;
9142 }
9143 ++name;
9144
9145 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9146 field_type = TYPE_FIELD_TYPE (field_type, index);
9147 }
9148
9149 /* Make a union to hold the variants. */
9150 struct type *union_type = alloc_type (objfile);
9151 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9152 TYPE_NFIELDS (union_type) = 3;
9153 TYPE_FIELDS (union_type)
9154 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9155 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9156 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9157
9158 /* Put the discriminant must at index 0. */
9159 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9160 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9161 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9162 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9163
9164 /* The order of fields doesn't really matter, so put the real
9165 field at index 1 and the data-less field at index 2. */
9166 struct discriminant_info *disc
9167 = alloc_discriminant_info (union_type, 0, 1);
9168 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9169 TYPE_FIELD_NAME (union_type, 1)
9170 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9171 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9172 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9173 TYPE_FIELD_NAME (union_type, 1));
9174
9175 const char *dataless_name
9176 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9177 name);
9178 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9179 dataless_name);
9180 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9181 /* NAME points into the original discriminant name, which
9182 already has the correct lifetime. */
9183 TYPE_FIELD_NAME (union_type, 2) = name;
9184 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9185 disc->discriminants[2] = 0;
9186
9187 /* Smash this type to be a structure type. We have to do this
9188 because the type has already been recorded. */
9189 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9190 TYPE_NFIELDS (type) = 1;
9191 TYPE_FIELDS (type)
9192 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9193
9194 /* Install the variant part. */
9195 TYPE_FIELD_TYPE (type, 0) = union_type;
9196 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9197 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9198 }
77c2dba3
TT
9199 /* A union with a single anonymous field is probably an old-style
9200 univariant enum. */
9201 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9202 {
c9317f21
TT
9203 /* Smash this type to be a structure type. We have to do this
9204 because the type has already been recorded. */
9205 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9206
9207 /* Make a union to hold the variants. */
9208 struct type *union_type = alloc_type (objfile);
9209 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9210 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9211 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9212 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9213 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9214
9215 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9216 const char *variant_name
9217 = rust_last_path_segment (TYPE_NAME (field_type));
9218 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9219 TYPE_NAME (field_type)
9220 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9221 TYPE_NAME (type), variant_name);
c9317f21
TT
9222
9223 /* Install the union in the outer struct type. */
9224 TYPE_NFIELDS (type) = 1;
9225 TYPE_FIELDS (type)
9226 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9227 TYPE_FIELD_TYPE (type, 0) = union_type;
9228 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9229 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9230
9231 alloc_discriminant_info (union_type, -1, 0);
9232 }
9233 else
9234 {
9235 struct type *disr_type = nullptr;
9236 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9237 {
9238 disr_type = TYPE_FIELD_TYPE (type, i);
9239
a037790e
TT
9240 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9241 {
9242 /* All fields of a true enum will be structs. */
9243 return;
9244 }
9245 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9246 {
9247 /* Could be data-less variant, so keep going. */
a037790e 9248 disr_type = nullptr;
c9317f21
TT
9249 }
9250 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9251 "RUST$ENUM$DISR") != 0)
9252 {
9253 /* Not a Rust enum. */
9254 return;
9255 }
9256 else
9257 {
9258 /* Found one. */
9259 break;
9260 }
9261 }
9262
9263 /* If we got here without a discriminant, then it's probably
9264 just a union. */
9265 if (disr_type == nullptr)
9266 return;
9267
9268 /* Smash this type to be a structure type. We have to do this
9269 because the type has already been recorded. */
9270 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9271
9272 /* Make a union to hold the variants. */
9273 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9274 struct type *union_type = alloc_type (objfile);
9275 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9276 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9277 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9278 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9279 TYPE_FIELDS (union_type)
9280 = (struct field *) TYPE_ZALLOC (union_type,
9281 (TYPE_NFIELDS (union_type)
9282 * sizeof (struct field)));
9283
9284 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9285 TYPE_NFIELDS (type) * sizeof (struct field));
9286
9287 /* Install the discriminant at index 0 in the union. */
9288 TYPE_FIELD (union_type, 0) = *disr_field;
9289 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9290 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9291
9292 /* Install the union in the outer struct type. */
9293 TYPE_FIELD_TYPE (type, 0) = union_type;
9294 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9295 TYPE_NFIELDS (type) = 1;
9296
9297 /* Set the size and offset of the union type. */
9298 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9299
9300 /* We need a way to find the correct discriminant given a
9301 variant name. For convenience we build a map here. */
9302 struct type *enum_type = FIELD_TYPE (*disr_field);
9303 std::unordered_map<std::string, ULONGEST> discriminant_map;
9304 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9305 {
9306 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9307 {
9308 const char *name
9309 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9310 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9311 }
9312 }
9313
9314 int n_fields = TYPE_NFIELDS (union_type);
9315 struct discriminant_info *disc
9316 = alloc_discriminant_info (union_type, 0, -1);
9317 /* Skip the discriminant here. */
9318 for (int i = 1; i < n_fields; ++i)
9319 {
9320 /* Find the final word in the name of this variant's type.
9321 That name can be used to look up the correct
9322 discriminant. */
9323 const char *variant_name
9324 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9325 i)));
9326
9327 auto iter = discriminant_map.find (variant_name);
9328 if (iter != discriminant_map.end ())
9329 disc->discriminants[i] = iter->second;
9330
bedda9ac 9331 /* Remove the discriminant field, if it exists. */
c9317f21 9332 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9333 if (TYPE_NFIELDS (sub_type) > 0)
9334 {
9335 --TYPE_NFIELDS (sub_type);
9336 ++TYPE_FIELDS (sub_type);
9337 }
c9317f21
TT
9338 TYPE_FIELD_NAME (union_type, i) = variant_name;
9339 TYPE_NAME (sub_type)
9340 = rust_fully_qualify (&objfile->objfile_obstack,
9341 TYPE_NAME (type), variant_name);
9342 }
9343 }
9344}
9345
9346/* Rewrite some Rust unions to be structures with variants parts. */
9347
9348static void
9349rust_union_quirks (struct dwarf2_cu *cu)
9350{
9351 gdb_assert (cu->language == language_rust);
52941706
SM
9352 for (type *type_ : cu->rust_unions)
9353 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9354 /* We don't need this any more. */
9355 cu->rust_unions.clear ();
c9317f21
TT
9356}
9357
95554aad
TT
9358/* Return the symtab for PER_CU. This works properly regardless of
9359 whether we're using the index or psymtabs. */
9360
43f3e411
DE
9361static struct compunit_symtab *
9362get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9363{
ed2dc618 9364 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9365 ? per_cu->v.quick->compunit_symtab
9366 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9367}
9368
9369/* A helper function for computing the list of all symbol tables
9370 included by PER_CU. */
9371
9372static void
4c39bc03 9373recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9374 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9375 struct dwarf2_per_cu_data *per_cu,
43f3e411 9376 struct compunit_symtab *immediate_parent)
95554aad
TT
9377{
9378 void **slot;
43f3e411 9379 struct compunit_symtab *cust;
95554aad
TT
9380
9381 slot = htab_find_slot (all_children, per_cu, INSERT);
9382 if (*slot != NULL)
9383 {
9384 /* This inclusion and its children have been processed. */
9385 return;
9386 }
9387
9388 *slot = per_cu;
9389 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9390 cust = get_compunit_symtab (per_cu);
9391 if (cust != NULL)
ec94af83
DE
9392 {
9393 /* If this is a type unit only add its symbol table if we haven't
9394 seen it yet (type unit per_cu's can share symtabs). */
9395 if (per_cu->is_debug_types)
9396 {
43f3e411 9397 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9398 if (*slot == NULL)
9399 {
43f3e411 9400 *slot = cust;
4c39bc03 9401 result->push_back (cust);
43f3e411
DE
9402 if (cust->user == NULL)
9403 cust->user = immediate_parent;
ec94af83
DE
9404 }
9405 }
9406 else
f9125b6c 9407 {
4c39bc03 9408 result->push_back (cust);
43f3e411
DE
9409 if (cust->user == NULL)
9410 cust->user = immediate_parent;
f9125b6c 9411 }
ec94af83 9412 }
95554aad 9413
ae640021
AB
9414 if (!per_cu->imported_symtabs_empty ())
9415 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9416 {
9417 recursively_compute_inclusions (result, all_children,
9418 all_type_symtabs, ptr, cust);
9419 }
95554aad
TT
9420}
9421
43f3e411 9422/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9423 PER_CU. */
9424
9425static void
43f3e411 9426compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9427{
f4dc4d17
DE
9428 gdb_assert (! per_cu->is_debug_types);
9429
ae640021 9430 if (!per_cu->imported_symtabs_empty ())
95554aad 9431 {
ae640021 9432 int len;
4c39bc03 9433 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9434 htab_t all_children, all_type_symtabs;
43f3e411 9435 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9436
9437 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9438 if (cust == NULL)
95554aad
TT
9439 return;
9440
9441 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9442 NULL, xcalloc, xfree);
ec94af83
DE
9443 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9444 NULL, xcalloc, xfree);
95554aad 9445
ae640021 9446 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9447 {
9448 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9449 all_type_symtabs, ptr, cust);
ec94af83 9450 }
95554aad 9451
ec94af83 9452 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9453 len = result_symtabs.size ();
43f3e411 9454 cust->includes
ed2dc618 9455 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9456 struct compunit_symtab *, len + 1);
4c39bc03
TT
9457 memcpy (cust->includes, result_symtabs.data (),
9458 len * sizeof (compunit_symtab *));
43f3e411 9459 cust->includes[len] = NULL;
95554aad 9460
95554aad 9461 htab_delete (all_children);
ec94af83 9462 htab_delete (all_type_symtabs);
95554aad
TT
9463 }
9464}
9465
9466/* Compute the 'includes' field for the symtabs of all the CUs we just
9467 read. */
9468
9469static void
ed2dc618 9470process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9471{
71b73764 9472 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9473 {
9474 if (! iter->is_debug_types)
43f3e411 9475 compute_compunit_symtab_includes (iter);
f4dc4d17 9476 }
95554aad 9477
c5d0225d 9478 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9479}
9480
9cdd5dbd 9481/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9482 already been loaded into memory. */
9483
9484static void
95554aad
TT
9485process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9486 enum language pretend_language)
10b3939b 9487{
10b3939b 9488 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9489 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9490 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9491 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9492 CORE_ADDR lowpc, highpc;
43f3e411 9493 struct compunit_symtab *cust;
10b3939b 9494 CORE_ADDR baseaddr;
4359dff1 9495 struct block *static_block;
3e29f34a 9496 CORE_ADDR addr;
10b3939b 9497
b3b3bada 9498 baseaddr = objfile->text_section_offset ();
10b3939b 9499
c89b44cd
TT
9500 /* Clear the list here in case something was left over. */
9501 cu->method_list.clear ();
10b3939b 9502
95554aad
TT
9503 cu->language = pretend_language;
9504 cu->language_defn = language_def (cu->language);
9505
c906108c 9506 /* Do line number decoding in read_file_scope () */
10b3939b 9507 process_die (cu->dies, cu);
c906108c 9508
a766d390
DE
9509 /* For now fudge the Go package. */
9510 if (cu->language == language_go)
9511 fixup_go_packaging (cu);
9512
5f48f8f3 9513 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9514 should be complete, and it should now be safe to compute all of the
9515 physnames. */
9516 compute_delayed_physnames (cu);
3da10d80 9517
c9317f21
TT
9518 if (cu->language == language_rust)
9519 rust_union_quirks (cu);
9520
fae299cd
DC
9521 /* Some compilers don't define a DW_AT_high_pc attribute for the
9522 compilation unit. If the DW_AT_high_pc is missing, synthesize
9523 it, by scanning the DIE's below the compilation unit. */
10b3939b 9524 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9525
3e29f34a 9526 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9527 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9528
9529 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9530 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9531 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9532 addrmap to help ensure it has an accurate map of pc values belonging to
9533 this comp unit. */
9534 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9535
c24bdb02 9536 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9537 SECT_OFF_TEXT (objfile),
9538 0);
c906108c 9539
43f3e411 9540 if (cust != NULL)
c906108c 9541 {
df15bd07 9542 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9543
8be455d7
JK
9544 /* Set symtab language to language from DW_AT_language. If the
9545 compilation is from a C file generated by language preprocessors, do
9546 not set the language if it was already deduced by start_subfile. */
43f3e411 9547 if (!(cu->language == language_c
40e3ad0e 9548 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9549 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9550
9551 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9552 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9553 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9554 there were bugs in prologue debug info, fixed later in GCC-4.5
9555 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9556
9557 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9558 needed, it would be wrong due to missing DW_AT_producer there.
9559
9560 Still one can confuse GDB by using non-standard GCC compilation
9561 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9562 */
ab260dad 9563 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9564 cust->locations_valid = 1;
e0d00bc7
JK
9565
9566 if (gcc_4_minor >= 5)
43f3e411 9567 cust->epilogue_unwind_valid = 1;
96408a79 9568
43f3e411 9569 cust->call_site_htab = cu->call_site_htab;
c906108c 9570 }
9291a0cd
TT
9571
9572 if (dwarf2_per_objfile->using_index)
43f3e411 9573 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9574 else
9575 {
891813be 9576 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9577 pst->compunit_symtab = cust;
6d94535f 9578 pst->readin = true;
9291a0cd 9579 }
c906108c 9580
95554aad 9581 /* Push it for inclusion processing later. */
c5d0225d 9582 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9583
9584 /* Not needed any more. */
c24bdb02 9585 cu->reset_builder ();
f4dc4d17 9586}
45cfd468 9587
f4dc4d17
DE
9588/* Generate full symbol information for type unit PER_CU, whose DIEs have
9589 already been loaded into memory. */
9590
9591static void
9592process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9593 enum language pretend_language)
9594{
9595 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9596 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9597 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9598 struct compunit_symtab *cust;
0186c6a7
DE
9599 struct signatured_type *sig_type;
9600
9601 gdb_assert (per_cu->is_debug_types);
9602 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9603
c89b44cd
TT
9604 /* Clear the list here in case something was left over. */
9605 cu->method_list.clear ();
f4dc4d17 9606
f4dc4d17
DE
9607 cu->language = pretend_language;
9608 cu->language_defn = language_def (cu->language);
9609
9610 /* The symbol tables are set up in read_type_unit_scope. */
9611 process_die (cu->dies, cu);
9612
9613 /* For now fudge the Go package. */
9614 if (cu->language == language_go)
9615 fixup_go_packaging (cu);
9616
5f48f8f3 9617 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9618 should be complete, and it should now be safe to compute all of the
9619 physnames. */
9620 compute_delayed_physnames (cu);
f4dc4d17 9621
c9317f21
TT
9622 if (cu->language == language_rust)
9623 rust_union_quirks (cu);
9624
f4dc4d17
DE
9625 /* TUs share symbol tables.
9626 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9627 of it with end_expandable_symtab. Otherwise, complete the addition of
9628 this TU's symbols to the existing symtab. */
43f3e411 9629 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9630 {
c24bdb02
KS
9631 buildsym_compunit *builder = cu->get_builder ();
9632 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9633 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9634
43f3e411 9635 if (cust != NULL)
f4dc4d17
DE
9636 {
9637 /* Set symtab language to language from DW_AT_language. If the
9638 compilation is from a C file generated by language preprocessors,
9639 do not set the language if it was already deduced by
9640 start_subfile. */
43f3e411
DE
9641 if (!(cu->language == language_c
9642 && COMPUNIT_FILETABS (cust)->language != language_c))
9643 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9644 }
9645 }
9646 else
9647 {
c24bdb02 9648 cu->get_builder ()->augment_type_symtab ();
43f3e411 9649 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9650 }
9651
9652 if (dwarf2_per_objfile->using_index)
43f3e411 9653 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9654 else
9655 {
891813be 9656 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9657 pst->compunit_symtab = cust;
6d94535f 9658 pst->readin = true;
45cfd468 9659 }
804d2729
TT
9660
9661 /* Not needed any more. */
c24bdb02 9662 cu->reset_builder ();
c906108c
SS
9663}
9664
95554aad
TT
9665/* Process an imported unit DIE. */
9666
9667static void
9668process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9669{
9670 struct attribute *attr;
9671
f4dc4d17
DE
9672 /* For now we don't handle imported units in type units. */
9673 if (cu->per_cu->is_debug_types)
9674 {
9675 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9676 " supported in type units [in module %s]"),
518817b3 9677 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9678 }
9679
95554aad
TT
9680 attr = dwarf2_attr (die, DW_AT_import, cu);
9681 if (attr != NULL)
9682 {
9c541725
PA
9683 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9684 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9685 dwarf2_per_cu_data *per_cu
e3b94546 9686 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9687 cu->per_cu->dwarf2_per_objfile);
95554aad 9688
58990295
TV
9689 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9690 into another compilation unit, at root level. Regard this as a hint,
9691 and ignore it. */
9692 if (die->parent && die->parent->parent == NULL
9693 && per_cu->unit_type == DW_UT_compile
9694 && per_cu->lang == language_cplus)
9695 return;
9696
69d751e3 9697 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9698 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9699 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9700
ae640021 9701 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9702 }
9703}
9704
4c8aa72d
PA
9705/* RAII object that represents a process_die scope: i.e.,
9706 starts/finishes processing a DIE. */
9707class process_die_scope
adde2bff 9708{
4c8aa72d
PA
9709public:
9710 process_die_scope (die_info *die, dwarf2_cu *cu)
9711 : m_die (die), m_cu (cu)
9712 {
9713 /* We should only be processing DIEs not already in process. */
9714 gdb_assert (!m_die->in_process);
9715 m_die->in_process = true;
9716 }
8c3cb9fa 9717
4c8aa72d
PA
9718 ~process_die_scope ()
9719 {
9720 m_die->in_process = false;
9721
9722 /* If we're done processing the DIE for the CU that owns the line
9723 header, we don't need the line header anymore. */
9724 if (m_cu->line_header_die_owner == m_die)
9725 {
9726 delete m_cu->line_header;
9727 m_cu->line_header = NULL;
9728 m_cu->line_header_die_owner = NULL;
9729 }
9730 }
9731
9732private:
9733 die_info *m_die;
9734 dwarf2_cu *m_cu;
9735};
adde2bff 9736
c906108c
SS
9737/* Process a die and its children. */
9738
9739static void
e7c27a73 9740process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9741{
4c8aa72d 9742 process_die_scope scope (die, cu);
adde2bff 9743
c906108c
SS
9744 switch (die->tag)
9745 {
9746 case DW_TAG_padding:
9747 break;
9748 case DW_TAG_compile_unit:
95554aad 9749 case DW_TAG_partial_unit:
e7c27a73 9750 read_file_scope (die, cu);
c906108c 9751 break;
348e048f
DE
9752 case DW_TAG_type_unit:
9753 read_type_unit_scope (die, cu);
9754 break;
c906108c 9755 case DW_TAG_subprogram:
0a4b0913
AB
9756 /* Nested subprograms in Fortran get a prefix. */
9757 if (cu->language == language_fortran
9758 && die->parent != NULL
9759 && die->parent->tag == DW_TAG_subprogram)
9760 cu->processing_has_namespace_info = true;
9761 /* Fall through. */
c906108c 9762 case DW_TAG_inlined_subroutine:
edb3359d 9763 read_func_scope (die, cu);
c906108c
SS
9764 break;
9765 case DW_TAG_lexical_block:
14898363
L
9766 case DW_TAG_try_block:
9767 case DW_TAG_catch_block:
e7c27a73 9768 read_lexical_block_scope (die, cu);
c906108c 9769 break;
216f72a1 9770 case DW_TAG_call_site:
96408a79
SA
9771 case DW_TAG_GNU_call_site:
9772 read_call_site_scope (die, cu);
9773 break;
c906108c 9774 case DW_TAG_class_type:
680b30c7 9775 case DW_TAG_interface_type:
c906108c
SS
9776 case DW_TAG_structure_type:
9777 case DW_TAG_union_type:
134d01f1 9778 process_structure_scope (die, cu);
c906108c
SS
9779 break;
9780 case DW_TAG_enumeration_type:
134d01f1 9781 process_enumeration_scope (die, cu);
c906108c 9782 break;
134d01f1 9783
f792889a
DJ
9784 /* These dies have a type, but processing them does not create
9785 a symbol or recurse to process the children. Therefore we can
9786 read them on-demand through read_type_die. */
c906108c 9787 case DW_TAG_subroutine_type:
72019c9c 9788 case DW_TAG_set_type:
c906108c 9789 case DW_TAG_array_type:
c906108c 9790 case DW_TAG_pointer_type:
c906108c 9791 case DW_TAG_ptr_to_member_type:
c906108c 9792 case DW_TAG_reference_type:
4297a3f0 9793 case DW_TAG_rvalue_reference_type:
c906108c 9794 case DW_TAG_string_type:
c906108c 9795 break;
134d01f1 9796
c906108c 9797 case DW_TAG_base_type:
a02abb62 9798 case DW_TAG_subrange_type:
cb249c71 9799 case DW_TAG_typedef:
134d01f1
DJ
9800 /* Add a typedef symbol for the type definition, if it has a
9801 DW_AT_name. */
f792889a 9802 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9803 break;
c906108c 9804 case DW_TAG_common_block:
e7c27a73 9805 read_common_block (die, cu);
c906108c
SS
9806 break;
9807 case DW_TAG_common_inclusion:
9808 break;
d9fa45fe 9809 case DW_TAG_namespace:
9068261f 9810 cu->processing_has_namespace_info = true;
e7c27a73 9811 read_namespace (die, cu);
d9fa45fe 9812 break;
5d7cb8df 9813 case DW_TAG_module:
9068261f 9814 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9815 read_module (die, cu);
9816 break;
d9fa45fe 9817 case DW_TAG_imported_declaration:
9068261f 9818 cu->processing_has_namespace_info = true;
74921315
KS
9819 if (read_namespace_alias (die, cu))
9820 break;
86a73007
TT
9821 /* The declaration is not a global namespace alias. */
9822 /* Fall through. */
d9fa45fe 9823 case DW_TAG_imported_module:
9068261f 9824 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9825 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9826 || cu->language != language_fortran))
b98664d3 9827 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9828 dwarf_tag_name (die->tag));
9829 read_import_statement (die, cu);
d9fa45fe 9830 break;
95554aad
TT
9831
9832 case DW_TAG_imported_unit:
9833 process_imported_unit_die (die, cu);
9834 break;
9835
71a3c369
TT
9836 case DW_TAG_variable:
9837 read_variable (die, cu);
9838 break;
9839
c906108c 9840 default:
e7c27a73 9841 new_symbol (die, NULL, cu);
c906108c
SS
9842 break;
9843 }
9844}
ca69b9e6
DE
9845\f
9846/* DWARF name computation. */
c906108c 9847
94af9270
KS
9848/* A helper function for dwarf2_compute_name which determines whether DIE
9849 needs to have the name of the scope prepended to the name listed in the
9850 die. */
9851
9852static int
9853die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9854{
1c809c68
TT
9855 struct attribute *attr;
9856
94af9270
KS
9857 switch (die->tag)
9858 {
9859 case DW_TAG_namespace:
9860 case DW_TAG_typedef:
9861 case DW_TAG_class_type:
9862 case DW_TAG_interface_type:
9863 case DW_TAG_structure_type:
9864 case DW_TAG_union_type:
9865 case DW_TAG_enumeration_type:
9866 case DW_TAG_enumerator:
9867 case DW_TAG_subprogram:
08a76f8a 9868 case DW_TAG_inlined_subroutine:
94af9270 9869 case DW_TAG_member:
74921315 9870 case DW_TAG_imported_declaration:
94af9270
KS
9871 return 1;
9872
9873 case DW_TAG_variable:
c2b0a229 9874 case DW_TAG_constant:
94af9270
KS
9875 /* We only need to prefix "globally" visible variables. These include
9876 any variable marked with DW_AT_external or any variable that
9877 lives in a namespace. [Variables in anonymous namespaces
9878 require prefixing, but they are not DW_AT_external.] */
9879
9880 if (dwarf2_attr (die, DW_AT_specification, cu))
9881 {
9882 struct dwarf2_cu *spec_cu = cu;
9a619af0 9883
94af9270
KS
9884 return die_needs_namespace (die_specification (die, &spec_cu),
9885 spec_cu);
9886 }
9887
1c809c68 9888 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9889 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9890 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9891 return 0;
9892 /* A variable in a lexical block of some kind does not need a
9893 namespace, even though in C++ such variables may be external
9894 and have a mangled name. */
9895 if (die->parent->tag == DW_TAG_lexical_block
9896 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9897 || die->parent->tag == DW_TAG_catch_block
9898 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9899 return 0;
9900 return 1;
94af9270
KS
9901
9902 default:
9903 return 0;
9904 }
9905}
9906
73b9be8b
KS
9907/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9908 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9909 defined for the given DIE. */
9910
9911static struct attribute *
9912dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9913{
9914 struct attribute *attr;
9915
9916 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9917 if (attr == NULL)
9918 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9919
9920 return attr;
9921}
9922
9923/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9924 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9925 defined for the given DIE. */
9926
9927static const char *
9928dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9929{
9930 const char *linkage_name;
9931
9932 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9933 if (linkage_name == NULL)
9934 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9935
9936 return linkage_name;
9937}
9938
94af9270 9939/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9940 compute the physname for the object, which include a method's:
9c37b5ae 9941 - formal parameters (C++),
a766d390 9942 - receiver type (Go),
a766d390
DE
9943
9944 The term "physname" is a bit confusing.
9945 For C++, for example, it is the demangled name.
9946 For Go, for example, it's the mangled name.
94af9270 9947
af6b7be1
JB
9948 For Ada, return the DIE's linkage name rather than the fully qualified
9949 name. PHYSNAME is ignored..
9950
94af9270
KS
9951 The result is allocated on the objfile_obstack and canonicalized. */
9952
9953static const char *
15d034d0
TT
9954dwarf2_compute_name (const char *name,
9955 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9956 int physname)
9957{
518817b3 9958 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 9959
94af9270
KS
9960 if (name == NULL)
9961 name = dwarf2_name (die, cu);
9962
2ee7123e
DE
9963 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9964 but otherwise compute it by typename_concat inside GDB.
9965 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 9966 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 9967 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
9968 will set the demangled name to the result of dwarf2_full_name, and it is
9969 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9970 if (cu->language == language_ada
9971 || (cu->language == language_fortran && physname))
9972 {
9973 /* For Ada unit, we prefer the linkage name over the name, as
9974 the former contains the exported name, which the user expects
9975 to be able to reference. Ideally, we want the user to be able
9976 to reference this entity using either natural or linkage name,
9977 but we haven't started looking at this enhancement yet. */
73b9be8b 9978 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9979
2ee7123e
DE
9980 if (linkage_name != NULL)
9981 return linkage_name;
f55ee35c
JK
9982 }
9983
94af9270
KS
9984 /* These are the only languages we know how to qualify names in. */
9985 if (name != NULL
9c37b5ae 9986 && (cu->language == language_cplus
c44af4eb
TT
9987 || cu->language == language_fortran || cu->language == language_d
9988 || cu->language == language_rust))
94af9270
KS
9989 {
9990 if (die_needs_namespace (die, cu))
9991 {
0d5cff50 9992 const char *prefix;
34a68019 9993 const char *canonical_name = NULL;
94af9270 9994
d7e74731
PA
9995 string_file buf;
9996
94af9270 9997 prefix = determine_prefix (die, cu);
94af9270
KS
9998 if (*prefix != '\0')
9999 {
43816ebc
TT
10000 gdb::unique_xmalloc_ptr<char> prefixed_name
10001 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10002
43816ebc 10003 buf.puts (prefixed_name.get ());
94af9270
KS
10004 }
10005 else
d7e74731 10006 buf.puts (name);
94af9270 10007
98bfdba5
PA
10008 /* Template parameters may be specified in the DIE's DW_AT_name, or
10009 as children with DW_TAG_template_type_param or
10010 DW_TAG_value_type_param. If the latter, add them to the name
10011 here. If the name already has template parameters, then
10012 skip this step; some versions of GCC emit both, and
10013 it is more efficient to use the pre-computed name.
10014
10015 Something to keep in mind about this process: it is very
10016 unlikely, or in some cases downright impossible, to produce
10017 something that will match the mangled name of a function.
10018 If the definition of the function has the same debug info,
10019 we should be able to match up with it anyway. But fallbacks
10020 using the minimal symbol, for instance to find a method
10021 implemented in a stripped copy of libstdc++, will not work.
10022 If we do not have debug info for the definition, we will have to
10023 match them up some other way.
10024
10025 When we do name matching there is a related problem with function
10026 templates; two instantiated function templates are allowed to
10027 differ only by their return types, which we do not add here. */
10028
10029 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10030 {
10031 struct attribute *attr;
10032 struct die_info *child;
10033 int first = 1;
10034
10035 die->building_fullname = 1;
10036
10037 for (child = die->child; child != NULL; child = child->sibling)
10038 {
10039 struct type *type;
12df843f 10040 LONGEST value;
d521ce57 10041 const gdb_byte *bytes;
98bfdba5
PA
10042 struct dwarf2_locexpr_baton *baton;
10043 struct value *v;
10044
10045 if (child->tag != DW_TAG_template_type_param
10046 && child->tag != DW_TAG_template_value_param)
10047 continue;
10048
10049 if (first)
10050 {
d7e74731 10051 buf.puts ("<");
98bfdba5
PA
10052 first = 0;
10053 }
10054 else
d7e74731 10055 buf.puts (", ");
98bfdba5
PA
10056
10057 attr = dwarf2_attr (child, DW_AT_type, cu);
10058 if (attr == NULL)
10059 {
b98664d3 10060 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10061 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10062 continue;
10063 }
10064 type = die_type (child, cu);
10065
10066 if (child->tag == DW_TAG_template_type_param)
10067 {
c1ec8cea
TT
10068 c_print_type (type, "", &buf, -1, 0, cu->language,
10069 &type_print_raw_options);
98bfdba5
PA
10070 continue;
10071 }
10072
10073 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10074 if (attr == NULL)
10075 {
b98664d3 10076 complaint (_("template parameter missing "
3e43a32a 10077 "DW_AT_const_value"));
d7e74731 10078 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10079 continue;
10080 }
10081
10082 dwarf2_const_value_attr (attr, type, name,
10083 &cu->comp_unit_obstack, cu,
10084 &value, &bytes, &baton);
10085
10086 if (TYPE_NOSIGN (type))
10087 /* GDB prints characters as NUMBER 'CHAR'. If that's
10088 changed, this can use value_print instead. */
d7e74731 10089 c_printchar (value, type, &buf);
98bfdba5
PA
10090 else
10091 {
10092 struct value_print_options opts;
10093
10094 if (baton != NULL)
10095 v = dwarf2_evaluate_loc_desc (type, NULL,
10096 baton->data,
10097 baton->size,
10098 baton->per_cu);
10099 else if (bytes != NULL)
10100 {
10101 v = allocate_value (type);
10102 memcpy (value_contents_writeable (v), bytes,
10103 TYPE_LENGTH (type));
10104 }
10105 else
10106 v = value_from_longest (type, value);
10107
3e43a32a
MS
10108 /* Specify decimal so that we do not depend on
10109 the radix. */
98bfdba5
PA
10110 get_formatted_print_options (&opts, 'd');
10111 opts.raw = 1;
d7e74731 10112 value_print (v, &buf, &opts);
98bfdba5 10113 release_value (v);
98bfdba5
PA
10114 }
10115 }
10116
10117 die->building_fullname = 0;
10118
10119 if (!first)
10120 {
10121 /* Close the argument list, with a space if necessary
10122 (nested templates). */
d7e74731
PA
10123 if (!buf.empty () && buf.string ().back () == '>')
10124 buf.puts (" >");
98bfdba5 10125 else
d7e74731 10126 buf.puts (">");
98bfdba5
PA
10127 }
10128 }
10129
9c37b5ae 10130 /* For C++ methods, append formal parameter type
94af9270 10131 information, if PHYSNAME. */
6e70227d 10132
94af9270 10133 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10134 && cu->language == language_cplus)
94af9270
KS
10135 {
10136 struct type *type = read_type_die (die, cu);
10137
d7e74731 10138 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10139 &type_print_raw_options);
94af9270 10140
9c37b5ae 10141 if (cu->language == language_cplus)
94af9270 10142 {
60430eff
DJ
10143 /* Assume that an artificial first parameter is
10144 "this", but do not crash if it is not. RealView
10145 marks unnamed (and thus unused) parameters as
10146 artificial; there is no way to differentiate
10147 the two cases. */
94af9270
KS
10148 if (TYPE_NFIELDS (type) > 0
10149 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10150 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10151 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10152 0))))
d7e74731 10153 buf.puts (" const");
94af9270
KS
10154 }
10155 }
10156
d7e74731 10157 const std::string &intermediate_name = buf.string ();
94af9270
KS
10158
10159 if (cu->language == language_cplus)
34a68019 10160 canonical_name
322a8516 10161 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10162 objfile);
34a68019
TT
10163
10164 /* If we only computed INTERMEDIATE_NAME, or if
10165 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10166 intern it. */
322a8516 10167 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10168 name = objfile->intern (intermediate_name);
34a68019
TT
10169 else
10170 name = canonical_name;
94af9270
KS
10171 }
10172 }
10173
10174 return name;
10175}
10176
0114d602
DJ
10177/* Return the fully qualified name of DIE, based on its DW_AT_name.
10178 If scope qualifiers are appropriate they will be added. The result
34a68019 10179 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10180 not have a name. NAME may either be from a previous call to
10181 dwarf2_name or NULL.
10182
9c37b5ae 10183 The output string will be canonicalized (if C++). */
0114d602
DJ
10184
10185static const char *
15d034d0 10186dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10187{
94af9270
KS
10188 return dwarf2_compute_name (name, die, cu, 0);
10189}
0114d602 10190
94af9270
KS
10191/* Construct a physname for the given DIE in CU. NAME may either be
10192 from a previous call to dwarf2_name or NULL. The result will be
10193 allocated on the objfile_objstack or NULL if the DIE does not have a
10194 name.
0114d602 10195
9c37b5ae 10196 The output string will be canonicalized (if C++). */
0114d602 10197
94af9270 10198static const char *
15d034d0 10199dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10200{
518817b3 10201 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10202 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10203 int need_copy = 1;
10204
10205 /* In this case dwarf2_compute_name is just a shortcut not building anything
10206 on its own. */
10207 if (!die_needs_namespace (die, cu))
10208 return dwarf2_compute_name (name, die, cu, 1);
10209
73b9be8b 10210 mangled = dw2_linkage_name (die, cu);
900e11f9 10211
e98c9e7c
TT
10212 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10213 See https://github.com/rust-lang/rust/issues/32925. */
10214 if (cu->language == language_rust && mangled != NULL
10215 && strchr (mangled, '{') != NULL)
10216 mangled = NULL;
10217
900e11f9
JK
10218 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10219 has computed. */
791afaa2 10220 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10221 if (mangled != NULL)
900e11f9 10222 {
900e11f9 10223
59cc4834
JB
10224 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10225 {
10226 /* Do nothing (do not demangle the symbol name). */
10227 }
10228 else if (cu->language == language_go)
a766d390 10229 {
5e2db402
TT
10230 /* This is a lie, but we already lie to the caller new_symbol.
10231 new_symbol assumes we return the mangled name.
a766d390 10232 This just undoes that lie until things are cleaned up. */
a766d390
DE
10233 }
10234 else
10235 {
0eb876f5
JB
10236 /* Use DMGL_RET_DROP for C++ template functions to suppress
10237 their return type. It is easier for GDB users to search
10238 for such functions as `name(params)' than `long name(params)'.
10239 In such case the minimal symbol names do not match the full
10240 symbol names but for template functions there is never a need
10241 to look up their definition from their declaration so
10242 the only disadvantage remains the minimal symbol variant
10243 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10244 demangled.reset (gdb_demangle (mangled,
10245 (DMGL_PARAMS | DMGL_ANSI
10246 | DMGL_RET_DROP)));
a766d390 10247 }
900e11f9 10248 if (demangled)
791afaa2 10249 canon = demangled.get ();
900e11f9
JK
10250 else
10251 {
10252 canon = mangled;
10253 need_copy = 0;
10254 }
10255 }
10256
10257 if (canon == NULL || check_physname)
10258 {
10259 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10260
10261 if (canon != NULL && strcmp (physname, canon) != 0)
10262 {
10263 /* It may not mean a bug in GDB. The compiler could also
10264 compute DW_AT_linkage_name incorrectly. But in such case
10265 GDB would need to be bug-to-bug compatible. */
10266
b98664d3 10267 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10268 "(from linkage <%s>) - DIE at %s [in module %s]"),
10269 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10270 objfile_name (objfile));
900e11f9
JK
10271
10272 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10273 is available here - over computed PHYSNAME. It is safer
10274 against both buggy GDB and buggy compilers. */
10275
10276 retval = canon;
10277 }
10278 else
10279 {
10280 retval = physname;
10281 need_copy = 0;
10282 }
10283 }
10284 else
10285 retval = canon;
10286
10287 if (need_copy)
be1e3d3e 10288 retval = objfile->intern (retval);
900e11f9 10289
900e11f9 10290 return retval;
0114d602
DJ
10291}
10292
74921315
KS
10293/* Inspect DIE in CU for a namespace alias. If one exists, record
10294 a new symbol for it.
10295
10296 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10297
10298static int
10299read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10300{
10301 struct attribute *attr;
10302
10303 /* If the die does not have a name, this is not a namespace
10304 alias. */
10305 attr = dwarf2_attr (die, DW_AT_name, cu);
10306 if (attr != NULL)
10307 {
10308 int num;
10309 struct die_info *d = die;
10310 struct dwarf2_cu *imported_cu = cu;
10311
10312 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10313 keep inspecting DIEs until we hit the underlying import. */
10314#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10315 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10316 {
10317 attr = dwarf2_attr (d, DW_AT_import, cu);
10318 if (attr == NULL)
10319 break;
10320
10321 d = follow_die_ref (d, attr, &imported_cu);
10322 if (d->tag != DW_TAG_imported_declaration)
10323 break;
10324 }
10325
10326 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10327 {
b98664d3 10328 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10329 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10330 return 0;
10331 }
10332
10333 if (attr != NULL)
10334 {
10335 struct type *type;
9c541725 10336 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10337
9c541725 10338 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10339 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10340 {
10341 /* This declaration is a global namespace alias. Add
10342 a symbol for it whose type is the aliased namespace. */
10343 new_symbol (die, type, cu);
10344 return 1;
10345 }
10346 }
10347 }
10348
10349 return 0;
10350}
10351
22cee43f 10352/* Return the using directives repository (global or local?) to use in the
804d2729 10353 current context for CU.
22cee43f
PMR
10354
10355 For Ada, imported declarations can materialize renamings, which *may* be
10356 global. However it is impossible (for now?) in DWARF to distinguish
10357 "external" imported declarations and "static" ones. As all imported
10358 declarations seem to be static in all other languages, make them all CU-wide
10359 global only in Ada. */
10360
10361static struct using_direct **
804d2729 10362using_directives (struct dwarf2_cu *cu)
22cee43f 10363{
c24bdb02
KS
10364 if (cu->language == language_ada
10365 && cu->get_builder ()->outermost_context_p ())
10366 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10367 else
c24bdb02 10368 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10369}
10370
27aa8d6a
SW
10371/* Read the import statement specified by the given die and record it. */
10372
10373static void
10374read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10375{
518817b3 10376 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10377 struct attribute *import_attr;
32019081 10378 struct die_info *imported_die, *child_die;
de4affc9 10379 struct dwarf2_cu *imported_cu;
27aa8d6a 10380 const char *imported_name;
794684b6 10381 const char *imported_name_prefix;
13387711
SW
10382 const char *canonical_name;
10383 const char *import_alias;
10384 const char *imported_declaration = NULL;
794684b6 10385 const char *import_prefix;
eb1e02fd 10386 std::vector<const char *> excludes;
13387711 10387
27aa8d6a
SW
10388 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10389 if (import_attr == NULL)
10390 {
b98664d3 10391 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10392 dwarf_tag_name (die->tag));
10393 return;
10394 }
10395
de4affc9
CC
10396 imported_cu = cu;
10397 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10398 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10399 if (imported_name == NULL)
10400 {
10401 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10402
10403 The import in the following code:
10404 namespace A
10405 {
10406 typedef int B;
10407 }
10408
10409 int main ()
10410 {
10411 using A::B;
10412 B b;
10413 return b;
10414 }
10415
10416 ...
10417 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10418 <52> DW_AT_decl_file : 1
10419 <53> DW_AT_decl_line : 6
10420 <54> DW_AT_import : <0x75>
10421 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10422 <59> DW_AT_name : B
10423 <5b> DW_AT_decl_file : 1
10424 <5c> DW_AT_decl_line : 2
10425 <5d> DW_AT_type : <0x6e>
10426 ...
10427 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10428 <76> DW_AT_byte_size : 4
10429 <77> DW_AT_encoding : 5 (signed)
10430
10431 imports the wrong die ( 0x75 instead of 0x58 ).
10432 This case will be ignored until the gcc bug is fixed. */
10433 return;
10434 }
10435
82856980
SW
10436 /* Figure out the local name after import. */
10437 import_alias = dwarf2_name (die, cu);
27aa8d6a 10438
794684b6
SW
10439 /* Figure out where the statement is being imported to. */
10440 import_prefix = determine_prefix (die, cu);
10441
10442 /* Figure out what the scope of the imported die is and prepend it
10443 to the name of the imported die. */
de4affc9 10444 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10445
f55ee35c
JK
10446 if (imported_die->tag != DW_TAG_namespace
10447 && imported_die->tag != DW_TAG_module)
794684b6 10448 {
13387711
SW
10449 imported_declaration = imported_name;
10450 canonical_name = imported_name_prefix;
794684b6 10451 }
13387711 10452 else if (strlen (imported_name_prefix) > 0)
12aaed36 10453 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10454 imported_name_prefix,
10455 (cu->language == language_d ? "." : "::"),
10456 imported_name, (char *) NULL);
13387711
SW
10457 else
10458 canonical_name = imported_name;
794684b6 10459
32019081
JK
10460 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10461 for (child_die = die->child; child_die && child_die->tag;
436c571c 10462 child_die = child_die->sibling)
32019081
JK
10463 {
10464 /* DWARF-4: A Fortran use statement with a “rename list” may be
10465 represented by an imported module entry with an import attribute
10466 referring to the module and owned entries corresponding to those
10467 entities that are renamed as part of being imported. */
10468
10469 if (child_die->tag != DW_TAG_imported_declaration)
10470 {
b98664d3 10471 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10472 "- DIE at %s [in module %s]"),
10473 sect_offset_str (child_die->sect_off),
10474 objfile_name (objfile));
32019081
JK
10475 continue;
10476 }
10477
10478 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10479 if (import_attr == NULL)
10480 {
b98664d3 10481 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10482 dwarf_tag_name (child_die->tag));
10483 continue;
10484 }
10485
10486 imported_cu = cu;
10487 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10488 &imported_cu);
10489 imported_name = dwarf2_name (imported_die, imported_cu);
10490 if (imported_name == NULL)
10491 {
b98664d3 10492 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10493 "imported name - DIE at %s [in module %s]"),
10494 sect_offset_str (child_die->sect_off),
10495 objfile_name (objfile));
32019081
JK
10496 continue;
10497 }
10498
eb1e02fd 10499 excludes.push_back (imported_name);
32019081
JK
10500
10501 process_die (child_die, cu);
10502 }
10503
804d2729 10504 add_using_directive (using_directives (cu),
22cee43f
PMR
10505 import_prefix,
10506 canonical_name,
10507 import_alias,
10508 imported_declaration,
10509 excludes,
10510 0,
10511 &objfile->objfile_obstack);
27aa8d6a
SW
10512}
10513
5230b05a
WT
10514/* ICC<14 does not output the required DW_AT_declaration on incomplete
10515 types, but gives them a size of zero. Starting with version 14,
10516 ICC is compatible with GCC. */
10517
9068261f 10518static bool
5230b05a
WT
10519producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10520{
10521 if (!cu->checked_producer)
10522 check_producer (cu);
10523
10524 return cu->producer_is_icc_lt_14;
10525}
10526
eb77c9df
AB
10527/* ICC generates a DW_AT_type for C void functions. This was observed on
10528 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10529 which says that void functions should not have a DW_AT_type. */
10530
10531static bool
10532producer_is_icc (struct dwarf2_cu *cu)
10533{
10534 if (!cu->checked_producer)
10535 check_producer (cu);
10536
10537 return cu->producer_is_icc;
10538}
10539
1b80a9fa
JK
10540/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10541 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10542 this, it was first present in GCC release 4.3.0. */
10543
9068261f 10544static bool
1b80a9fa
JK
10545producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10546{
10547 if (!cu->checked_producer)
10548 check_producer (cu);
10549
10550 return cu->producer_is_gcc_lt_4_3;
10551}
10552
d721ba37
PA
10553static file_and_directory
10554find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10555{
d721ba37
PA
10556 file_and_directory res;
10557
9291a0cd
TT
10558 /* Find the filename. Do not use dwarf2_name here, since the filename
10559 is not a source language identifier. */
d721ba37
PA
10560 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10561 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10562
d721ba37
PA
10563 if (res.comp_dir == NULL
10564 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10565 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10566 {
d721ba37
PA
10567 res.comp_dir_storage = ldirname (res.name);
10568 if (!res.comp_dir_storage.empty ())
10569 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10570 }
d721ba37 10571 if (res.comp_dir != NULL)
9291a0cd
TT
10572 {
10573 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10574 directory, get rid of it. */
d721ba37 10575 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10576
d721ba37
PA
10577 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10578 res.comp_dir = cp + 1;
9291a0cd
TT
10579 }
10580
d721ba37
PA
10581 if (res.name == NULL)
10582 res.name = "<unknown>";
10583
10584 return res;
9291a0cd
TT
10585}
10586
f4dc4d17
DE
10587/* Handle DW_AT_stmt_list for a compilation unit.
10588 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10589 COMP_DIR is the compilation directory. LOWPC is passed to
10590 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10591
10592static void
10593handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10594 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10595{
518817b3
SM
10596 struct dwarf2_per_objfile *dwarf2_per_objfile
10597 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10598 struct attribute *attr;
527f3840
JK
10599 struct line_header line_header_local;
10600 hashval_t line_header_local_hash;
527f3840
JK
10601 void **slot;
10602 int decode_mapping;
2ab95328 10603
f4dc4d17
DE
10604 gdb_assert (! cu->per_cu->is_debug_types);
10605
2ab95328 10606 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10607 if (attr == NULL)
10608 return;
10609
9c541725 10610 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10611
10612 /* The line header hash table is only created if needed (it exists to
10613 prevent redundant reading of the line table for partial_units).
10614 If we're given a partial_unit, we'll need it. If we're given a
10615 compile_unit, then use the line header hash table if it's already
10616 created, but don't create one just yet. */
10617
10618 if (dwarf2_per_objfile->line_header_hash == NULL
10619 && die->tag == DW_TAG_partial_unit)
2ab95328 10620 {
527f3840 10621 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10622 .reset (htab_create_alloc (127, line_header_hash_voidp,
10623 line_header_eq_voidp,
10624 free_line_header_voidp,
10625 xcalloc, xfree));
527f3840 10626 }
2ab95328 10627
9c541725 10628 line_header_local.sect_off = line_offset;
527f3840
JK
10629 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10630 line_header_local_hash = line_header_hash (&line_header_local);
10631 if (dwarf2_per_objfile->line_header_hash != NULL)
10632 {
d15acc42 10633 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10634 &line_header_local,
10635 line_header_local_hash, NO_INSERT);
10636
10637 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10638 is not present in *SLOT (since if there is something in *SLOT then
10639 it will be for a partial_unit). */
10640 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10641 {
527f3840 10642 gdb_assert (*slot != NULL);
9a3c8263 10643 cu->line_header = (struct line_header *) *slot;
527f3840 10644 return;
dee91e82 10645 }
2ab95328 10646 }
527f3840
JK
10647
10648 /* dwarf_decode_line_header does not yet provide sufficient information.
10649 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10650 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10651 if (lh == NULL)
527f3840 10652 return;
4c8aa72d
PA
10653
10654 cu->line_header = lh.release ();
10655 cu->line_header_die_owner = die;
527f3840
JK
10656
10657 if (dwarf2_per_objfile->line_header_hash == NULL)
10658 slot = NULL;
10659 else
10660 {
d15acc42 10661 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10662 &line_header_local,
10663 line_header_local_hash, INSERT);
10664 gdb_assert (slot != NULL);
10665 }
10666 if (slot != NULL && *slot == NULL)
10667 {
10668 /* This newly decoded line number information unit will be owned
10669 by line_header_hash hash table. */
10670 *slot = cu->line_header;
4c8aa72d 10671 cu->line_header_die_owner = NULL;
527f3840
JK
10672 }
10673 else
10674 {
10675 /* We cannot free any current entry in (*slot) as that struct line_header
10676 may be already used by multiple CUs. Create only temporary decoded
10677 line_header for this CU - it may happen at most once for each line
10678 number information unit. And if we're not using line_header_hash
10679 then this is what we want as well. */
10680 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10681 }
10682 decode_mapping = (die->tag != DW_TAG_partial_unit);
10683 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10684 decode_mapping);
fff8551c 10685
2ab95328
TT
10686}
10687
95554aad 10688/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10689
c906108c 10690static void
e7c27a73 10691read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10692{
518817b3
SM
10693 struct dwarf2_per_objfile *dwarf2_per_objfile
10694 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10695 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10696 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10697 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10698 CORE_ADDR highpc = ((CORE_ADDR) 0);
10699 struct attribute *attr;
c906108c 10700 struct die_info *child_die;
e142c38c 10701 CORE_ADDR baseaddr;
6e70227d 10702
380618d6 10703 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10704 baseaddr = objfile->text_section_offset ();
c906108c 10705
fae299cd 10706 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10707
10708 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10709 from finish_block. */
2acceee2 10710 if (lowpc == ((CORE_ADDR) -1))
c906108c 10711 lowpc = highpc;
3e29f34a 10712 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10713
d721ba37 10714 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10715
f4b8a18d
KW
10716 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10717 standardised yet. As a workaround for the language detection we fall
10718 back to the DW_AT_producer string. */
10719 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10720 cu->language = language_opencl;
10721
3019eac3
DE
10722 /* Similar hack for Go. */
10723 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10724 set_cu_language (DW_LANG_Go, cu);
10725
c24bdb02 10726 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10727
10728 /* Decode line number information if present. We do this before
10729 processing child DIEs, so that the line header table is available
10730 for DW_AT_decl_file. */
d721ba37 10731 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10732
10733 /* Process all dies in compilation unit. */
10734 if (die->child != NULL)
10735 {
10736 child_die = die->child;
10737 while (child_die && child_die->tag)
10738 {
10739 process_die (child_die, cu);
436c571c 10740 child_die = child_die->sibling;
3019eac3
DE
10741 }
10742 }
10743
10744 /* Decode macro information, if present. Dwarf 2 macro information
10745 refers to information in the line number info statement program
10746 header, so we can only read it if we've read the header
10747 successfully. */
0af92d60
JK
10748 attr = dwarf2_attr (die, DW_AT_macros, cu);
10749 if (attr == NULL)
10750 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10751 if (attr && cu->line_header)
10752 {
10753 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10754 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10755
43f3e411 10756 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10757 }
10758 else
10759 {
10760 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10761 if (attr && cu->line_header)
10762 {
10763 unsigned int macro_offset = DW_UNSND (attr);
10764
43f3e411 10765 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10766 }
10767 }
3019eac3
DE
10768}
10769
c24bdb02
KS
10770void
10771dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10772{
f4dc4d17
DE
10773 struct type_unit_group *tu_group;
10774 int first_time;
3019eac3 10775 struct attribute *attr;
9c541725 10776 unsigned int i;
0186c6a7 10777 struct signatured_type *sig_type;
3019eac3 10778
f4dc4d17 10779 gdb_assert (per_cu->is_debug_types);
0186c6a7 10780 sig_type = (struct signatured_type *) per_cu;
3019eac3 10781
c24bdb02 10782 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10783
f4dc4d17 10784 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10785 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10786 if (sig_type->type_unit_group == NULL)
c24bdb02 10787 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10788 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10789
10790 /* If we've already processed this stmt_list there's no real need to
10791 do it again, we could fake it and just recreate the part we need
10792 (file name,index -> symtab mapping). If data shows this optimization
10793 is useful we can do it then. */
43f3e411 10794 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10795
10796 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10797 debug info. */
fff8551c 10798 line_header_up lh;
f4dc4d17 10799 if (attr != NULL)
3019eac3 10800 {
9c541725 10801 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10802 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10803 }
10804 if (lh == NULL)
10805 {
10806 if (first_time)
c24bdb02 10807 start_symtab ("", NULL, 0);
f4dc4d17
DE
10808 else
10809 {
10810 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10811 gdb_assert (m_builder == nullptr);
804d2729 10812 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10813 m_builder.reset (new struct buildsym_compunit
10814 (COMPUNIT_OBJFILE (cust), "",
10815 COMPUNIT_DIRNAME (cust),
10816 compunit_language (cust),
10817 0, cust));
f4dc4d17 10818 }
f4dc4d17 10819 return;
3019eac3
DE
10820 }
10821
c24bdb02
KS
10822 line_header = lh.release ();
10823 line_header_die_owner = die;
3019eac3 10824
f4dc4d17
DE
10825 if (first_time)
10826 {
c24bdb02 10827 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10828
1fd60fc0
DE
10829 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10830 still initializing it, and our caller (a few levels up)
10831 process_full_type_unit still needs to know if this is the first
10832 time. */
10833
4ac93832
TT
10834 tu_group->symtabs
10835 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10836 struct symtab *, line_header->file_names_size ());
3019eac3 10837
7ba99d21
AT
10838 auto &file_names = line_header->file_names ();
10839 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10840 {
7ba99d21 10841 file_entry &fe = file_names[i];
c24bdb02
KS
10842 dwarf2_start_subfile (this, fe.name,
10843 fe.include_dir (line_header));
10844 buildsym_compunit *b = get_builder ();
10845 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10846 {
4c8aa72d
PA
10847 /* NOTE: start_subfile will recognize when it's been
10848 passed a file it has already seen. So we can't
10849 assume there's a simple mapping from
10850 cu->line_header->file_names to subfiles, plus
10851 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10852 b->get_current_subfile ()->symtab
10853 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10854 }
10855
c24bdb02 10856 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10857 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10858 }
10859 }
10860 else
3019eac3 10861 {
c24bdb02 10862 gdb_assert (m_builder == nullptr);
804d2729 10863 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10864 m_builder.reset (new struct buildsym_compunit
10865 (COMPUNIT_OBJFILE (cust), "",
10866 COMPUNIT_DIRNAME (cust),
10867 compunit_language (cust),
10868 0, cust));
f4dc4d17 10869
7ba99d21
AT
10870 auto &file_names = line_header->file_names ();
10871 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10872 {
7ba99d21 10873 file_entry &fe = file_names[i];
4c8aa72d 10874 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10875 }
3019eac3
DE
10876 }
10877
f4dc4d17
DE
10878 /* The main symtab is allocated last. Type units don't have DW_AT_name
10879 so they don't have a "real" (so to speak) symtab anyway.
10880 There is later code that will assign the main symtab to all symbols
10881 that don't have one. We need to handle the case of a symbol with a
10882 missing symtab (DW_AT_decl_file) anyway. */
10883}
3019eac3 10884
f4dc4d17
DE
10885/* Process DW_TAG_type_unit.
10886 For TUs we want to skip the first top level sibling if it's not the
10887 actual type being defined by this TU. In this case the first top
10888 level sibling is there to provide context only. */
3019eac3 10889
f4dc4d17
DE
10890static void
10891read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10892{
10893 struct die_info *child_die;
3019eac3 10894
f4dc4d17
DE
10895 prepare_one_comp_unit (cu, die, language_minimal);
10896
10897 /* Initialize (or reinitialize) the machinery for building symtabs.
10898 We do this before processing child DIEs, so that the line header table
10899 is available for DW_AT_decl_file. */
c24bdb02 10900 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10901
10902 if (die->child != NULL)
10903 {
10904 child_die = die->child;
10905 while (child_die && child_die->tag)
10906 {
10907 process_die (child_die, cu);
436c571c 10908 child_die = child_die->sibling;
f4dc4d17
DE
10909 }
10910 }
3019eac3
DE
10911}
10912\f
80626a55
DE
10913/* DWO/DWP files.
10914
10915 http://gcc.gnu.org/wiki/DebugFission
10916 http://gcc.gnu.org/wiki/DebugFissionDWP
10917
10918 To simplify handling of both DWO files ("object" files with the DWARF info)
10919 and DWP files (a file with the DWOs packaged up into one file), we treat
10920 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10921
10922static hashval_t
10923hash_dwo_file (const void *item)
10924{
9a3c8263 10925 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10926 hashval_t hash;
3019eac3 10927
a2ce51a0
DE
10928 hash = htab_hash_string (dwo_file->dwo_name);
10929 if (dwo_file->comp_dir != NULL)
10930 hash += htab_hash_string (dwo_file->comp_dir);
10931 return hash;
3019eac3
DE
10932}
10933
10934static int
10935eq_dwo_file (const void *item_lhs, const void *item_rhs)
10936{
9a3c8263
SM
10937 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10938 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10939
a2ce51a0
DE
10940 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10941 return 0;
10942 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10943 return lhs->comp_dir == rhs->comp_dir;
10944 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10945}
10946
10947/* Allocate a hash table for DWO files. */
10948
51ac9db5 10949static htab_up
298e9637 10950allocate_dwo_file_hash_table ()
3019eac3 10951{
51ac9db5
SM
10952 auto delete_dwo_file = [] (void *item)
10953 {
10954 struct dwo_file *dwo_file = (struct dwo_file *) item;
10955
10956 delete dwo_file;
10957 };
10958
bc68fb19
TT
10959 return htab_up (htab_create_alloc (41,
10960 hash_dwo_file,
10961 eq_dwo_file,
10962 delete_dwo_file,
10963 xcalloc, xfree));
3019eac3
DE
10964}
10965
80626a55
DE
10966/* Lookup DWO file DWO_NAME. */
10967
10968static void **
ed2dc618
SM
10969lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10970 const char *dwo_name,
10971 const char *comp_dir)
80626a55
DE
10972{
10973 struct dwo_file find_entry;
10974 void **slot;
10975
10976 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 10977 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 10978
0ac5b59e
DE
10979 find_entry.dwo_name = dwo_name;
10980 find_entry.comp_dir = comp_dir;
51ac9db5
SM
10981 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10982 INSERT);
80626a55
DE
10983
10984 return slot;
10985}
10986
3019eac3
DE
10987static hashval_t
10988hash_dwo_unit (const void *item)
10989{
9a3c8263 10990 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10991
10992 /* This drops the top 32 bits of the id, but is ok for a hash. */
10993 return dwo_unit->signature;
10994}
10995
10996static int
10997eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10998{
9a3c8263
SM
10999 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11000 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11001
11002 /* The signature is assumed to be unique within the DWO file.
11003 So while object file CU dwo_id's always have the value zero,
11004 that's OK, assuming each object file DWO file has only one CU,
11005 and that's the rule for now. */
11006 return lhs->signature == rhs->signature;
11007}
11008
11009/* Allocate a hash table for DWO CUs,TUs.
11010 There is one of these tables for each of CUs,TUs for each DWO file. */
11011
b0b6a987 11012static htab_up
298e9637 11013allocate_dwo_unit_table ()
3019eac3
DE
11014{
11015 /* Start out with a pretty small number.
11016 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11017 return htab_up (htab_create_alloc (3,
11018 hash_dwo_unit,
11019 eq_dwo_unit,
11020 NULL, xcalloc, xfree));
3019eac3
DE
11021}
11022
19c3d4c9 11023/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11024
11025static void
19c3d4c9
DE
11026create_dwo_cu_reader (const struct die_reader_specs *reader,
11027 const gdb_byte *info_ptr,
11028 struct die_info *comp_unit_die,
c0ab21c2
TT
11029 struct dwo_file *dwo_file,
11030 struct dwo_unit *dwo_unit)
3019eac3
DE
11031{
11032 struct dwarf2_cu *cu = reader->cu;
9c541725 11033 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11034 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11035
a084a2a6
AT
11036 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11037 if (!signature.has_value ())
3019eac3 11038 {
b98664d3 11039 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11040 " its dwo_id [in module %s]"),
9d8780f0 11041 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11042 return;
11043 }
11044
3019eac3 11045 dwo_unit->dwo_file = dwo_file;
a084a2a6 11046 dwo_unit->signature = *signature;
8a0459fd 11047 dwo_unit->section = section;
9c541725 11048 dwo_unit->sect_off = sect_off;
3019eac3
DE
11049 dwo_unit->length = cu->per_cu->length;
11050
b4f54984 11051 if (dwarf_read_debug)
9d8780f0
SM
11052 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11053 sect_offset_str (sect_off),
9c541725 11054 hex_string (dwo_unit->signature));
3019eac3
DE
11055}
11056
33c5cd75 11057/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11058 Note: This function processes DWO files only, not DWP files. */
3019eac3 11059
33c5cd75 11060static void
ed2dc618 11061create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11062 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11063 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11064{
11065 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11066 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11067
96b79293 11068 section.read (objfile);
33c5cd75 11069 info_ptr = section.buffer;
3019eac3
DE
11070
11071 if (info_ptr == NULL)
33c5cd75 11072 return;
3019eac3 11073
b4f54984 11074 if (dwarf_read_debug)
19c3d4c9
DE
11075 {
11076 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11077 section.get_name (),
11078 section.get_file_name ());
19c3d4c9 11079 }
3019eac3 11080
33c5cd75 11081 end_ptr = info_ptr + section.size;
3019eac3
DE
11082 while (info_ptr < end_ptr)
11083 {
11084 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11085 struct dwo_unit read_unit {};
33c5cd75
DB
11086 struct dwo_unit *dwo_unit;
11087 void **slot;
11088 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11089
11090 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11091 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11092 per_cu.is_debug_types = 0;
33c5cd75
DB
11093 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11094 per_cu.section = &section;
11095
c0ab21c2
TT
11096 cutu_reader reader (&per_cu, cu, &dwo_file);
11097 if (!reader.dummy_p)
11098 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11099 &dwo_file, &read_unit);
33c5cd75
DB
11100 info_ptr += per_cu.length;
11101
11102 // If the unit could not be parsed, skip it.
c0ab21c2 11103 if (read_unit.dwo_file == NULL)
33c5cd75 11104 continue;
3019eac3 11105
33c5cd75 11106 if (cus_htab == NULL)
298e9637 11107 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11108
33c5cd75 11109 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11110 *dwo_unit = read_unit;
b0b6a987 11111 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11112 gdb_assert (slot != NULL);
11113 if (*slot != NULL)
19c3d4c9 11114 {
33c5cd75
DB
11115 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11116 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11117
b98664d3 11118 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11119 " the entry at offset %s, signature %s"),
11120 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11121 hex_string (dwo_unit->signature));
19c3d4c9 11122 }
33c5cd75 11123 *slot = (void *)dwo_unit;
3019eac3 11124 }
3019eac3
DE
11125}
11126
80626a55
DE
11127/* DWP file .debug_{cu,tu}_index section format:
11128 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11129
d2415c6c
DE
11130 DWP Version 1:
11131
80626a55
DE
11132 Both index sections have the same format, and serve to map a 64-bit
11133 signature to a set of section numbers. Each section begins with a header,
11134 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11135 indexes, and a pool of 32-bit section numbers. The index sections will be
11136 aligned at 8-byte boundaries in the file.
11137
d2415c6c
DE
11138 The index section header consists of:
11139
11140 V, 32 bit version number
11141 -, 32 bits unused
11142 N, 32 bit number of compilation units or type units in the index
11143 M, 32 bit number of slots in the hash table
80626a55 11144
d2415c6c 11145 Numbers are recorded using the byte order of the application binary.
80626a55 11146
d2415c6c
DE
11147 The hash table begins at offset 16 in the section, and consists of an array
11148 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11149 order of the application binary). Unused slots in the hash table are 0.
11150 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11151
d2415c6c
DE
11152 The parallel table begins immediately after the hash table
11153 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11154 array of 32-bit indexes (using the byte order of the application binary),
11155 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11156 table contains a 32-bit index into the pool of section numbers. For unused
11157 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11158
73869dc2
DE
11159 The pool of section numbers begins immediately following the hash table
11160 (at offset 16 + 12 * M from the beginning of the section). The pool of
11161 section numbers consists of an array of 32-bit words (using the byte order
11162 of the application binary). Each item in the array is indexed starting
11163 from 0. The hash table entry provides the index of the first section
11164 number in the set. Additional section numbers in the set follow, and the
11165 set is terminated by a 0 entry (section number 0 is not used in ELF).
11166
11167 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11168 section must be the first entry in the set, and the .debug_abbrev.dwo must
11169 be the second entry. Other members of the set may follow in any order.
11170
11171 ---
11172
11173 DWP Version 2:
11174
11175 DWP Version 2 combines all the .debug_info, etc. sections into one,
11176 and the entries in the index tables are now offsets into these sections.
11177 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11178 section.
11179
11180 Index Section Contents:
11181 Header
11182 Hash Table of Signatures dwp_hash_table.hash_table
11183 Parallel Table of Indices dwp_hash_table.unit_table
11184 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11185 Table of Section Sizes dwp_hash_table.v2.sizes
11186
11187 The index section header consists of:
11188
11189 V, 32 bit version number
11190 L, 32 bit number of columns in the table of section offsets
11191 N, 32 bit number of compilation units or type units in the index
11192 M, 32 bit number of slots in the hash table
11193
11194 Numbers are recorded using the byte order of the application binary.
11195
11196 The hash table has the same format as version 1.
11197 The parallel table of indices has the same format as version 1,
11198 except that the entries are origin-1 indices into the table of sections
11199 offsets and the table of section sizes.
11200
11201 The table of offsets begins immediately following the parallel table
11202 (at offset 16 + 12 * M from the beginning of the section). The table is
11203 a two-dimensional array of 32-bit words (using the byte order of the
11204 application binary), with L columns and N+1 rows, in row-major order.
11205 Each row in the array is indexed starting from 0. The first row provides
11206 a key to the remaining rows: each column in this row provides an identifier
11207 for a debug section, and the offsets in the same column of subsequent rows
11208 refer to that section. The section identifiers are:
11209
11210 DW_SECT_INFO 1 .debug_info.dwo
11211 DW_SECT_TYPES 2 .debug_types.dwo
11212 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11213 DW_SECT_LINE 4 .debug_line.dwo
11214 DW_SECT_LOC 5 .debug_loc.dwo
11215 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11216 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11217 DW_SECT_MACRO 8 .debug_macro.dwo
11218
11219 The offsets provided by the CU and TU index sections are the base offsets
11220 for the contributions made by each CU or TU to the corresponding section
11221 in the package file. Each CU and TU header contains an abbrev_offset
11222 field, used to find the abbreviations table for that CU or TU within the
11223 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11224 be interpreted as relative to the base offset given in the index section.
11225 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11226 should be interpreted as relative to the base offset for .debug_line.dwo,
11227 and offsets into other debug sections obtained from DWARF attributes should
11228 also be interpreted as relative to the corresponding base offset.
11229
11230 The table of sizes begins immediately following the table of offsets.
11231 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11232 with L columns and N rows, in row-major order. Each row in the array is
11233 indexed starting from 1 (row 0 is shared by the two tables).
11234
11235 ---
11236
11237 Hash table lookup is handled the same in version 1 and 2:
11238
11239 We assume that N and M will not exceed 2^32 - 1.
11240 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11241
d2415c6c
DE
11242 Given a 64-bit compilation unit signature or a type signature S, an entry
11243 in the hash table is located as follows:
80626a55 11244
d2415c6c
DE
11245 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11246 the low-order k bits all set to 1.
80626a55 11247
d2415c6c 11248 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11249
d2415c6c
DE
11250 3) If the hash table entry at index H matches the signature, use that
11251 entry. If the hash table entry at index H is unused (all zeroes),
11252 terminate the search: the signature is not present in the table.
80626a55 11253
d2415c6c 11254 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11255
d2415c6c 11256 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11257 to stop at an unused slot or find the match. */
80626a55
DE
11258
11259/* Create a hash table to map DWO IDs to their CU/TU entry in
11260 .debug_{info,types}.dwo in DWP_FILE.
11261 Returns NULL if there isn't one.
11262 Note: This function processes DWP files only, not DWO files. */
11263
11264static struct dwp_hash_table *
ed2dc618
SM
11265create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11266 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11267{
11268 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11269 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11270 const gdb_byte *index_ptr, *index_end;
80626a55 11271 struct dwarf2_section_info *index;
73869dc2 11272 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11273 struct dwp_hash_table *htab;
11274
11275 if (is_debug_types)
11276 index = &dwp_file->sections.tu_index;
11277 else
11278 index = &dwp_file->sections.cu_index;
11279
96b79293 11280 if (index->empty ())
80626a55 11281 return NULL;
96b79293 11282 index->read (objfile);
80626a55
DE
11283
11284 index_ptr = index->buffer;
11285 index_end = index_ptr + index->size;
11286
11287 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11288 index_ptr += 4;
11289 if (version == 2)
11290 nr_columns = read_4_bytes (dbfd, index_ptr);
11291 else
11292 nr_columns = 0;
11293 index_ptr += 4;
80626a55
DE
11294 nr_units = read_4_bytes (dbfd, index_ptr);
11295 index_ptr += 4;
11296 nr_slots = read_4_bytes (dbfd, index_ptr);
11297 index_ptr += 4;
11298
73869dc2 11299 if (version != 1 && version != 2)
80626a55 11300 {
21aa081e 11301 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11302 " [in module %s]"),
21aa081e 11303 pulongest (version), dwp_file->name);
80626a55
DE
11304 }
11305 if (nr_slots != (nr_slots & -nr_slots))
11306 {
21aa081e 11307 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11308 " is not power of 2 [in module %s]"),
21aa081e 11309 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11310 }
11311
11312 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11313 htab->version = version;
11314 htab->nr_columns = nr_columns;
80626a55
DE
11315 htab->nr_units = nr_units;
11316 htab->nr_slots = nr_slots;
11317 htab->hash_table = index_ptr;
11318 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11319
11320 /* Exit early if the table is empty. */
11321 if (nr_slots == 0 || nr_units == 0
11322 || (version == 2 && nr_columns == 0))
11323 {
11324 /* All must be zero. */
11325 if (nr_slots != 0 || nr_units != 0
11326 || (version == 2 && nr_columns != 0))
11327 {
b98664d3 11328 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11329 " all zero [in modules %s]"),
11330 dwp_file->name);
11331 }
11332 return htab;
11333 }
11334
11335 if (version == 1)
11336 {
11337 htab->section_pool.v1.indices =
11338 htab->unit_table + sizeof (uint32_t) * nr_slots;
11339 /* It's harder to decide whether the section is too small in v1.
11340 V1 is deprecated anyway so we punt. */
11341 }
11342 else
11343 {
11344 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11345 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11346 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11347 /* Reverse map for error checking. */
11348 int ids_seen[DW_SECT_MAX + 1];
11349 int i;
11350
11351 if (nr_columns < 2)
11352 {
11353 error (_("Dwarf Error: bad DWP hash table, too few columns"
11354 " in section table [in module %s]"),
11355 dwp_file->name);
11356 }
11357 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11358 {
11359 error (_("Dwarf Error: bad DWP hash table, too many columns"
11360 " in section table [in module %s]"),
11361 dwp_file->name);
11362 }
04fd5eed
GB
11363 memset (ids, 255, sizeof_ids);
11364 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11365 for (i = 0; i < nr_columns; ++i)
11366 {
11367 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11368
11369 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11370 {
11371 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11372 " in section table [in module %s]"),
11373 id, dwp_file->name);
11374 }
11375 if (ids_seen[id] != -1)
11376 {
11377 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11378 " id %d in section table [in module %s]"),
11379 id, dwp_file->name);
11380 }
11381 ids_seen[id] = i;
11382 ids[i] = id;
11383 }
11384 /* Must have exactly one info or types section. */
11385 if (((ids_seen[DW_SECT_INFO] != -1)
11386 + (ids_seen[DW_SECT_TYPES] != -1))
11387 != 1)
11388 {
11389 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11390 " DWO info/types section [in module %s]"),
11391 dwp_file->name);
11392 }
11393 /* Must have an abbrev section. */
11394 if (ids_seen[DW_SECT_ABBREV] == -1)
11395 {
11396 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11397 " section [in module %s]"),
11398 dwp_file->name);
11399 }
11400 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11401 htab->section_pool.v2.sizes =
11402 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11403 * nr_units * nr_columns);
11404 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11405 * nr_units * nr_columns))
11406 > index_end)
11407 {
11408 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11409 " [in module %s]"),
11410 dwp_file->name);
11411 }
11412 }
80626a55
DE
11413
11414 return htab;
11415}
11416
11417/* Update SECTIONS with the data from SECTP.
11418
11419 This function is like the other "locate" section routines that are
11420 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11421 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11422
11423 The result is non-zero for success, or zero if an error was found. */
11424
11425static int
73869dc2
DE
11426locate_v1_virtual_dwo_sections (asection *sectp,
11427 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11428{
11429 const struct dwop_section_names *names = &dwop_section_names;
11430
11431 if (section_is_p (sectp->name, &names->abbrev_dwo))
11432 {
11433 /* There can be only one. */
049412e3 11434 if (sections->abbrev.s.section != NULL)
80626a55 11435 return 0;
049412e3 11436 sections->abbrev.s.section = sectp;
fd361982 11437 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11438 }
11439 else if (section_is_p (sectp->name, &names->info_dwo)
11440 || section_is_p (sectp->name, &names->types_dwo))
11441 {
11442 /* There can be only one. */
049412e3 11443 if (sections->info_or_types.s.section != NULL)
80626a55 11444 return 0;
049412e3 11445 sections->info_or_types.s.section = sectp;
fd361982 11446 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11447 }
11448 else if (section_is_p (sectp->name, &names->line_dwo))
11449 {
11450 /* There can be only one. */
049412e3 11451 if (sections->line.s.section != NULL)
80626a55 11452 return 0;
049412e3 11453 sections->line.s.section = sectp;
fd361982 11454 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11455 }
11456 else if (section_is_p (sectp->name, &names->loc_dwo))
11457 {
11458 /* There can be only one. */
049412e3 11459 if (sections->loc.s.section != NULL)
80626a55 11460 return 0;
049412e3 11461 sections->loc.s.section = sectp;
fd361982 11462 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11463 }
11464 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11465 {
11466 /* There can be only one. */
049412e3 11467 if (sections->macinfo.s.section != NULL)
80626a55 11468 return 0;
049412e3 11469 sections->macinfo.s.section = sectp;
fd361982 11470 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11471 }
11472 else if (section_is_p (sectp->name, &names->macro_dwo))
11473 {
11474 /* There can be only one. */
049412e3 11475 if (sections->macro.s.section != NULL)
80626a55 11476 return 0;
049412e3 11477 sections->macro.s.section = sectp;
fd361982 11478 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11479 }
11480 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11481 {
11482 /* There can be only one. */
049412e3 11483 if (sections->str_offsets.s.section != NULL)
80626a55 11484 return 0;
049412e3 11485 sections->str_offsets.s.section = sectp;
fd361982 11486 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11487 }
11488 else
11489 {
11490 /* No other kind of section is valid. */
11491 return 0;
11492 }
11493
11494 return 1;
11495}
11496
73869dc2
DE
11497/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11498 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11499 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11500 This is for DWP version 1 files. */
80626a55
DE
11501
11502static struct dwo_unit *
ed2dc618
SM
11503create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11504 struct dwp_file *dwp_file,
73869dc2
DE
11505 uint32_t unit_index,
11506 const char *comp_dir,
11507 ULONGEST signature, int is_debug_types)
80626a55
DE
11508{
11509 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11510 const struct dwp_hash_table *dwp_htab =
11511 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11512 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11513 const char *kind = is_debug_types ? "TU" : "CU";
11514 struct dwo_file *dwo_file;
11515 struct dwo_unit *dwo_unit;
73869dc2 11516 struct virtual_v1_dwo_sections sections;
80626a55 11517 void **dwo_file_slot;
80626a55
DE
11518 int i;
11519
73869dc2
DE
11520 gdb_assert (dwp_file->version == 1);
11521
b4f54984 11522 if (dwarf_read_debug)
80626a55 11523 {
73869dc2 11524 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11525 kind,
73869dc2 11526 pulongest (unit_index), hex_string (signature),
80626a55
DE
11527 dwp_file->name);
11528 }
11529
19ac8c2e 11530 /* Fetch the sections of this DWO unit.
80626a55
DE
11531 Put a limit on the number of sections we look for so that bad data
11532 doesn't cause us to loop forever. */
11533
73869dc2 11534#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11535 (1 /* .debug_info or .debug_types */ \
11536 + 1 /* .debug_abbrev */ \
11537 + 1 /* .debug_line */ \
11538 + 1 /* .debug_loc */ \
11539 + 1 /* .debug_str_offsets */ \
19ac8c2e 11540 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11541 + 1 /* trailing zero */)
11542
11543 memset (&sections, 0, sizeof (sections));
80626a55 11544
73869dc2 11545 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11546 {
11547 asection *sectp;
11548 uint32_t section_nr =
11549 read_4_bytes (dbfd,
73869dc2
DE
11550 dwp_htab->section_pool.v1.indices
11551 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11552
11553 if (section_nr == 0)
11554 break;
11555 if (section_nr >= dwp_file->num_sections)
11556 {
11557 error (_("Dwarf Error: bad DWP hash table, section number too large"
11558 " [in module %s]"),
11559 dwp_file->name);
11560 }
11561
11562 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11563 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11564 {
11565 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11566 " [in module %s]"),
11567 dwp_file->name);
11568 }
11569 }
11570
11571 if (i < 2
96b79293
TT
11572 || sections.info_or_types.empty ()
11573 || sections.abbrev.empty ())
80626a55
DE
11574 {
11575 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11576 " [in module %s]"),
11577 dwp_file->name);
11578 }
73869dc2 11579 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11580 {
11581 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11582 " [in module %s]"),
11583 dwp_file->name);
11584 }
11585
11586 /* It's easier for the rest of the code if we fake a struct dwo_file and
11587 have dwo_unit "live" in that. At least for now.
11588
11589 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11590 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11591 file, we can combine them back into a virtual DWO file to save space
11592 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11593 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11594
791afaa2
TT
11595 std::string virtual_dwo_name =
11596 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11597 sections.abbrev.get_id (),
11598 sections.line.get_id (),
11599 sections.loc.get_id (),
11600 sections.str_offsets.get_id ());
80626a55 11601 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11602 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11603 virtual_dwo_name.c_str (),
11604 comp_dir);
80626a55
DE
11605 /* Create one if necessary. */
11606 if (*dwo_file_slot == NULL)
11607 {
b4f54984 11608 if (dwarf_read_debug)
80626a55
DE
11609 {
11610 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11611 virtual_dwo_name.c_str ());
80626a55 11612 }
51ac9db5 11613 dwo_file = new struct dwo_file;
be1e3d3e 11614 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
0ac5b59e 11615 dwo_file->comp_dir = comp_dir;
80626a55
DE
11616 dwo_file->sections.abbrev = sections.abbrev;
11617 dwo_file->sections.line = sections.line;
11618 dwo_file->sections.loc = sections.loc;
11619 dwo_file->sections.macinfo = sections.macinfo;
11620 dwo_file->sections.macro = sections.macro;
11621 dwo_file->sections.str_offsets = sections.str_offsets;
11622 /* The "str" section is global to the entire DWP file. */
11623 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11624 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11625 there's no need to record it in dwo_file.
11626 Also, we can't simply record type sections in dwo_file because
11627 we record a pointer into the vector in dwo_unit. As we collect more
11628 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11629 for it, invalidating all copies of pointers into the previous
11630 contents. */
80626a55
DE
11631 *dwo_file_slot = dwo_file;
11632 }
11633 else
11634 {
b4f54984 11635 if (dwarf_read_debug)
80626a55
DE
11636 {
11637 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11638 virtual_dwo_name.c_str ());
80626a55 11639 }
9a3c8263 11640 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11641 }
80626a55
DE
11642
11643 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11644 dwo_unit->dwo_file = dwo_file;
11645 dwo_unit->signature = signature;
8d749320
SM
11646 dwo_unit->section =
11647 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11648 *dwo_unit->section = sections.info_or_types;
57d63ce2 11649 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11650
11651 return dwo_unit;
11652}
11653
73869dc2
DE
11654/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11655 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11656 piece within that section used by a TU/CU, return a virtual section
11657 of just that piece. */
11658
11659static struct dwarf2_section_info
ed2dc618
SM
11660create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11661 struct dwarf2_section_info *section,
73869dc2
DE
11662 bfd_size_type offset, bfd_size_type size)
11663{
11664 struct dwarf2_section_info result;
11665 asection *sectp;
11666
11667 gdb_assert (section != NULL);
11668 gdb_assert (!section->is_virtual);
11669
11670 memset (&result, 0, sizeof (result));
11671 result.s.containing_section = section;
dc4ccb6f 11672 result.is_virtual = true;
73869dc2
DE
11673
11674 if (size == 0)
11675 return result;
11676
96b79293 11677 sectp = section->get_bfd_section ();
73869dc2
DE
11678
11679 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11680 bounds of the real section. This is a pretty-rare event, so just
11681 flag an error (easier) instead of a warning and trying to cope. */
11682 if (sectp == NULL
fd361982 11683 || offset + size > bfd_section_size (sectp))
73869dc2 11684 {
73869dc2
DE
11685 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11686 " in section %s [in module %s]"),
fd361982 11687 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11688 objfile_name (dwarf2_per_objfile->objfile));
11689 }
11690
11691 result.virtual_offset = offset;
11692 result.size = size;
11693 return result;
11694}
11695
11696/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11697 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11698 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11699 This is for DWP version 2 files. */
11700
11701static struct dwo_unit *
ed2dc618
SM
11702create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11703 struct dwp_file *dwp_file,
73869dc2
DE
11704 uint32_t unit_index,
11705 const char *comp_dir,
11706 ULONGEST signature, int is_debug_types)
11707{
11708 struct objfile *objfile = dwarf2_per_objfile->objfile;
11709 const struct dwp_hash_table *dwp_htab =
11710 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11711 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11712 const char *kind = is_debug_types ? "TU" : "CU";
11713 struct dwo_file *dwo_file;
11714 struct dwo_unit *dwo_unit;
11715 struct virtual_v2_dwo_sections sections;
11716 void **dwo_file_slot;
73869dc2
DE
11717 int i;
11718
11719 gdb_assert (dwp_file->version == 2);
11720
b4f54984 11721 if (dwarf_read_debug)
73869dc2
DE
11722 {
11723 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11724 kind,
11725 pulongest (unit_index), hex_string (signature),
11726 dwp_file->name);
11727 }
11728
11729 /* Fetch the section offsets of this DWO unit. */
11730
11731 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11732
11733 for (i = 0; i < dwp_htab->nr_columns; ++i)
11734 {
11735 uint32_t offset = read_4_bytes (dbfd,
11736 dwp_htab->section_pool.v2.offsets
11737 + (((unit_index - 1) * dwp_htab->nr_columns
11738 + i)
11739 * sizeof (uint32_t)));
11740 uint32_t size = read_4_bytes (dbfd,
11741 dwp_htab->section_pool.v2.sizes
11742 + (((unit_index - 1) * dwp_htab->nr_columns
11743 + i)
11744 * sizeof (uint32_t)));
11745
11746 switch (dwp_htab->section_pool.v2.section_ids[i])
11747 {
11748 case DW_SECT_INFO:
11749 case DW_SECT_TYPES:
11750 sections.info_or_types_offset = offset;
11751 sections.info_or_types_size = size;
11752 break;
11753 case DW_SECT_ABBREV:
11754 sections.abbrev_offset = offset;
11755 sections.abbrev_size = size;
11756 break;
11757 case DW_SECT_LINE:
11758 sections.line_offset = offset;
11759 sections.line_size = size;
11760 break;
11761 case DW_SECT_LOC:
11762 sections.loc_offset = offset;
11763 sections.loc_size = size;
11764 break;
11765 case DW_SECT_STR_OFFSETS:
11766 sections.str_offsets_offset = offset;
11767 sections.str_offsets_size = size;
11768 break;
11769 case DW_SECT_MACINFO:
11770 sections.macinfo_offset = offset;
11771 sections.macinfo_size = size;
11772 break;
11773 case DW_SECT_MACRO:
11774 sections.macro_offset = offset;
11775 sections.macro_size = size;
11776 break;
11777 }
11778 }
11779
11780 /* It's easier for the rest of the code if we fake a struct dwo_file and
11781 have dwo_unit "live" in that. At least for now.
11782
11783 The DWP file can be made up of a random collection of CUs and TUs.
11784 However, for each CU + set of TUs that came from the same original DWO
11785 file, we can combine them back into a virtual DWO file to save space
11786 (fewer struct dwo_file objects to allocate). Remember that for really
11787 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11788
791afaa2
TT
11789 std::string virtual_dwo_name =
11790 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11791 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11792 (long) (sections.line_size ? sections.line_offset : 0),
11793 (long) (sections.loc_size ? sections.loc_offset : 0),
11794 (long) (sections.str_offsets_size
11795 ? sections.str_offsets_offset : 0));
73869dc2 11796 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11797 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11798 virtual_dwo_name.c_str (),
11799 comp_dir);
73869dc2
DE
11800 /* Create one if necessary. */
11801 if (*dwo_file_slot == NULL)
11802 {
b4f54984 11803 if (dwarf_read_debug)
73869dc2
DE
11804 {
11805 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11806 virtual_dwo_name.c_str ());
73869dc2 11807 }
51ac9db5 11808 dwo_file = new struct dwo_file;
be1e3d3e 11809 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
73869dc2
DE
11810 dwo_file->comp_dir = comp_dir;
11811 dwo_file->sections.abbrev =
ed2dc618 11812 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11813 sections.abbrev_offset, sections.abbrev_size);
11814 dwo_file->sections.line =
ed2dc618 11815 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11816 sections.line_offset, sections.line_size);
11817 dwo_file->sections.loc =
ed2dc618 11818 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11819 sections.loc_offset, sections.loc_size);
11820 dwo_file->sections.macinfo =
ed2dc618 11821 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11822 sections.macinfo_offset, sections.macinfo_size);
11823 dwo_file->sections.macro =
ed2dc618 11824 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11825 sections.macro_offset, sections.macro_size);
11826 dwo_file->sections.str_offsets =
ed2dc618
SM
11827 create_dwp_v2_section (dwarf2_per_objfile,
11828 &dwp_file->sections.str_offsets,
73869dc2
DE
11829 sections.str_offsets_offset,
11830 sections.str_offsets_size);
11831 /* The "str" section is global to the entire DWP file. */
11832 dwo_file->sections.str = dwp_file->sections.str;
11833 /* The info or types section is assigned below to dwo_unit,
11834 there's no need to record it in dwo_file.
11835 Also, we can't simply record type sections in dwo_file because
11836 we record a pointer into the vector in dwo_unit. As we collect more
11837 types we'll grow the vector and eventually have to reallocate space
11838 for it, invalidating all copies of pointers into the previous
11839 contents. */
11840 *dwo_file_slot = dwo_file;
11841 }
11842 else
11843 {
b4f54984 11844 if (dwarf_read_debug)
73869dc2
DE
11845 {
11846 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11847 virtual_dwo_name.c_str ());
73869dc2 11848 }
9a3c8263 11849 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11850 }
73869dc2
DE
11851
11852 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11853 dwo_unit->dwo_file = dwo_file;
11854 dwo_unit->signature = signature;
8d749320
SM
11855 dwo_unit->section =
11856 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11857 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11858 is_debug_types
73869dc2
DE
11859 ? &dwp_file->sections.types
11860 : &dwp_file->sections.info,
11861 sections.info_or_types_offset,
11862 sections.info_or_types_size);
11863 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11864
11865 return dwo_unit;
11866}
11867
57d63ce2
DE
11868/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11869 Returns NULL if the signature isn't found. */
80626a55
DE
11870
11871static struct dwo_unit *
ed2dc618
SM
11872lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11873 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11874 ULONGEST signature, int is_debug_types)
80626a55 11875{
57d63ce2
DE
11876 const struct dwp_hash_table *dwp_htab =
11877 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11878 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11879 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11880 uint32_t hash = signature & mask;
11881 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11882 unsigned int i;
11883 void **slot;
870f88f7 11884 struct dwo_unit find_dwo_cu;
80626a55
DE
11885
11886 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11887 find_dwo_cu.signature = signature;
19ac8c2e 11888 slot = htab_find_slot (is_debug_types
48b490f2
TT
11889 ? dwp_file->loaded_tus.get ()
11890 : dwp_file->loaded_cus.get (),
19ac8c2e 11891 &find_dwo_cu, INSERT);
80626a55
DE
11892
11893 if (*slot != NULL)
9a3c8263 11894 return (struct dwo_unit *) *slot;
80626a55
DE
11895
11896 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11897 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11898 {
11899 ULONGEST signature_in_table;
11900
11901 signature_in_table =
57d63ce2 11902 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11903 if (signature_in_table == signature)
11904 {
57d63ce2
DE
11905 uint32_t unit_index =
11906 read_4_bytes (dbfd,
11907 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11908
73869dc2
DE
11909 if (dwp_file->version == 1)
11910 {
ed2dc618
SM
11911 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11912 dwp_file, unit_index,
73869dc2
DE
11913 comp_dir, signature,
11914 is_debug_types);
11915 }
11916 else
11917 {
ed2dc618
SM
11918 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11919 dwp_file, unit_index,
73869dc2
DE
11920 comp_dir, signature,
11921 is_debug_types);
11922 }
9a3c8263 11923 return (struct dwo_unit *) *slot;
80626a55
DE
11924 }
11925 if (signature_in_table == 0)
11926 return NULL;
11927 hash = (hash + hash2) & mask;
11928 }
11929
11930 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11931 " [in module %s]"),
11932 dwp_file->name);
11933}
11934
ab5088bf 11935/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11936 Open the file specified by FILE_NAME and hand it off to BFD for
11937 preliminary analysis. Return a newly initialized bfd *, which
11938 includes a canonicalized copy of FILE_NAME.
80626a55 11939 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11940 SEARCH_CWD is true if the current directory is to be searched.
11941 It will be searched before debug-file-directory.
13aaf454
DE
11942 If successful, the file is added to the bfd include table of the
11943 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11944 If unable to find/open the file, return NULL.
3019eac3
DE
11945 NOTE: This function is derived from symfile_bfd_open. */
11946
192b62ce 11947static gdb_bfd_ref_ptr
ed2dc618
SM
11948try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11949 const char *file_name, int is_dwp, int search_cwd)
3019eac3 11950{
24b9144d 11951 int desc;
9c02c129
DE
11952 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11953 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11954 to debug_file_directory. */
e0cc99a6 11955 const char *search_path;
9c02c129
DE
11956 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11957
e0cc99a6 11958 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
11959 if (search_cwd)
11960 {
11961 if (*debug_file_directory != '\0')
e0cc99a6
TT
11962 {
11963 search_path_holder.reset (concat (".", dirname_separator_string,
11964 debug_file_directory,
11965 (char *) NULL));
11966 search_path = search_path_holder.get ();
11967 }
6ac97d4c 11968 else
e0cc99a6 11969 search_path = ".";
6ac97d4c 11970 }
9c02c129 11971 else
e0cc99a6 11972 search_path = debug_file_directory;
3019eac3 11973
24b9144d 11974 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
11975 if (is_dwp)
11976 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
11977
11978 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 11979 desc = openp (search_path, flags, file_name,
3019eac3
DE
11980 O_RDONLY | O_BINARY, &absolute_name);
11981 if (desc < 0)
11982 return NULL;
11983
e0cc99a6
TT
11984 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11985 gnutarget, desc));
9c02c129
DE
11986 if (sym_bfd == NULL)
11987 return NULL;
192b62ce 11988 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 11989
192b62ce
TT
11990 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11991 return NULL;
3019eac3 11992
13aaf454
DE
11993 /* Success. Record the bfd as having been included by the objfile's bfd.
11994 This is important because things like demangled_names_hash lives in the
11995 objfile's per_bfd space and may have references to things like symbol
11996 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 11997 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 11998
3019eac3
DE
11999 return sym_bfd;
12000}
12001
ab5088bf 12002/* Try to open DWO file FILE_NAME.
3019eac3
DE
12003 COMP_DIR is the DW_AT_comp_dir attribute.
12004 The result is the bfd handle of the file.
12005 If there is a problem finding or opening the file, return NULL.
12006 Upon success, the canonicalized path of the file is stored in the bfd,
12007 same as symfile_bfd_open. */
12008
192b62ce 12009static gdb_bfd_ref_ptr
ed2dc618
SM
12010open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12011 const char *file_name, const char *comp_dir)
3019eac3 12012{
80626a55 12013 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12014 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12015 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12016
12017 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12018
12019 if (comp_dir != NULL)
12020 {
43816ebc
TT
12021 gdb::unique_xmalloc_ptr<char> path_to_try
12022 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12023
12024 /* NOTE: If comp_dir is a relative path, this will also try the
12025 search path, which seems useful. */
ed2dc618 12026 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12027 path_to_try.get (),
ed2dc618 12028 0 /*is_dwp*/,
192b62ce 12029 1 /*search_cwd*/));
3019eac3
DE
12030 if (abfd != NULL)
12031 return abfd;
12032 }
12033
12034 /* That didn't work, try debug-file-directory, which, despite its name,
12035 is a list of paths. */
12036
12037 if (*debug_file_directory == '\0')
12038 return NULL;
12039
ed2dc618
SM
12040 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12041 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12042}
12043
80626a55
DE
12044/* This function is mapped across the sections and remembers the offset and
12045 size of each of the DWO debugging sections we are interested in. */
12046
12047static void
12048dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12049{
9a3c8263 12050 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12051 const struct dwop_section_names *names = &dwop_section_names;
12052
12053 if (section_is_p (sectp->name, &names->abbrev_dwo))
12054 {
049412e3 12055 dwo_sections->abbrev.s.section = sectp;
fd361982 12056 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12057 }
12058 else if (section_is_p (sectp->name, &names->info_dwo))
12059 {
049412e3 12060 dwo_sections->info.s.section = sectp;
fd361982 12061 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12062 }
12063 else if (section_is_p (sectp->name, &names->line_dwo))
12064 {
049412e3 12065 dwo_sections->line.s.section = sectp;
fd361982 12066 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12067 }
12068 else if (section_is_p (sectp->name, &names->loc_dwo))
12069 {
049412e3 12070 dwo_sections->loc.s.section = sectp;
fd361982 12071 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12072 }
12073 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12074 {
049412e3 12075 dwo_sections->macinfo.s.section = sectp;
fd361982 12076 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12077 }
12078 else if (section_is_p (sectp->name, &names->macro_dwo))
12079 {
049412e3 12080 dwo_sections->macro.s.section = sectp;
fd361982 12081 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12082 }
12083 else if (section_is_p (sectp->name, &names->str_dwo))
12084 {
049412e3 12085 dwo_sections->str.s.section = sectp;
fd361982 12086 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12087 }
12088 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12089 {
049412e3 12090 dwo_sections->str_offsets.s.section = sectp;
fd361982 12091 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12092 }
12093 else if (section_is_p (sectp->name, &names->types_dwo))
12094 {
12095 struct dwarf2_section_info type_section;
12096
12097 memset (&type_section, 0, sizeof (type_section));
049412e3 12098 type_section.s.section = sectp;
fd361982 12099 type_section.size = bfd_section_size (sectp);
fd5866f6 12100 dwo_sections->types.push_back (type_section);
80626a55
DE
12101 }
12102}
12103
ab5088bf 12104/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12105 by PER_CU. This is for the non-DWP case.
80626a55 12106 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12107
12108static struct dwo_file *
0ac5b59e
DE
12109open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12110 const char *dwo_name, const char *comp_dir)
3019eac3 12111{
ed2dc618 12112 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12113
fb1eb2f9 12114 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12115 if (dbfd == NULL)
12116 {
b4f54984 12117 if (dwarf_read_debug)
80626a55
DE
12118 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12119 return NULL;
12120 }
263db9a1 12121
51ac9db5 12122 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12123 dwo_file->dwo_name = dwo_name;
12124 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12125 dwo_file->dbfd = std::move (dbfd);
3019eac3 12126
fb1eb2f9 12127 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12128 &dwo_file->sections);
3019eac3 12129
18a8505e
AT
12130 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12131 dwo_file->sections.info, dwo_file->cus);
3019eac3 12132
263db9a1 12133 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12134 dwo_file->sections.types, dwo_file->tus);
3019eac3 12135
b4f54984 12136 if (dwarf_read_debug)
80626a55
DE
12137 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12138
263db9a1 12139 return dwo_file.release ();
3019eac3
DE
12140}
12141
80626a55 12142/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12143 size of each of the DWP debugging sections common to version 1 and 2 that
12144 we are interested in. */
3019eac3 12145
80626a55 12146static void
73869dc2
DE
12147dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12148 void *dwp_file_ptr)
3019eac3 12149{
9a3c8263 12150 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12151 const struct dwop_section_names *names = &dwop_section_names;
12152 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12153
80626a55 12154 /* Record the ELF section number for later lookup: this is what the
73869dc2 12155 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12156 gdb_assert (elf_section_nr < dwp_file->num_sections);
12157 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12158
80626a55
DE
12159 /* Look for specific sections that we need. */
12160 if (section_is_p (sectp->name, &names->str_dwo))
12161 {
049412e3 12162 dwp_file->sections.str.s.section = sectp;
fd361982 12163 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12164 }
12165 else if (section_is_p (sectp->name, &names->cu_index))
12166 {
049412e3 12167 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12168 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12169 }
12170 else if (section_is_p (sectp->name, &names->tu_index))
12171 {
049412e3 12172 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12173 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12174 }
12175}
3019eac3 12176
73869dc2
DE
12177/* This function is mapped across the sections and remembers the offset and
12178 size of each of the DWP version 2 debugging sections that we are interested
12179 in. This is split into a separate function because we don't know if we
12180 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12181
12182static void
12183dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12184{
9a3c8263 12185 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12186 const struct dwop_section_names *names = &dwop_section_names;
12187 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12188
12189 /* Record the ELF section number for later lookup: this is what the
12190 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12191 gdb_assert (elf_section_nr < dwp_file->num_sections);
12192 dwp_file->elf_sections[elf_section_nr] = sectp;
12193
12194 /* Look for specific sections that we need. */
12195 if (section_is_p (sectp->name, &names->abbrev_dwo))
12196 {
049412e3 12197 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12198 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12199 }
12200 else if (section_is_p (sectp->name, &names->info_dwo))
12201 {
049412e3 12202 dwp_file->sections.info.s.section = sectp;
fd361982 12203 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12204 }
12205 else if (section_is_p (sectp->name, &names->line_dwo))
12206 {
049412e3 12207 dwp_file->sections.line.s.section = sectp;
fd361982 12208 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12209 }
12210 else if (section_is_p (sectp->name, &names->loc_dwo))
12211 {
049412e3 12212 dwp_file->sections.loc.s.section = sectp;
fd361982 12213 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12214 }
12215 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12216 {
049412e3 12217 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12218 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12219 }
12220 else if (section_is_p (sectp->name, &names->macro_dwo))
12221 {
049412e3 12222 dwp_file->sections.macro.s.section = sectp;
fd361982 12223 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12224 }
12225 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12226 {
049412e3 12227 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12228 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12229 }
12230 else if (section_is_p (sectp->name, &names->types_dwo))
12231 {
049412e3 12232 dwp_file->sections.types.s.section = sectp;
fd361982 12233 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12234 }
12235}
12236
80626a55 12237/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12238
80626a55
DE
12239static hashval_t
12240hash_dwp_loaded_cutus (const void *item)
12241{
9a3c8263 12242 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12243
80626a55
DE
12244 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12245 return dwo_unit->signature;
3019eac3
DE
12246}
12247
80626a55 12248/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12249
80626a55
DE
12250static int
12251eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12252{
9a3c8263
SM
12253 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12254 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12255
80626a55
DE
12256 return dua->signature == dub->signature;
12257}
3019eac3 12258
80626a55 12259/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12260
48b490f2 12261static htab_up
298e9637 12262allocate_dwp_loaded_cutus_table ()
80626a55 12263{
48b490f2
TT
12264 return htab_up (htab_create_alloc (3,
12265 hash_dwp_loaded_cutus,
12266 eq_dwp_loaded_cutus,
12267 NULL, xcalloc, xfree));
80626a55 12268}
3019eac3 12269
ab5088bf
DE
12270/* Try to open DWP file FILE_NAME.
12271 The result is the bfd handle of the file.
12272 If there is a problem finding or opening the file, return NULL.
12273 Upon success, the canonicalized path of the file is stored in the bfd,
12274 same as symfile_bfd_open. */
12275
192b62ce 12276static gdb_bfd_ref_ptr
ed2dc618
SM
12277open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12278 const char *file_name)
ab5088bf 12279{
ed2dc618
SM
12280 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12281 1 /*is_dwp*/,
192b62ce 12282 1 /*search_cwd*/));
6ac97d4c
DE
12283 if (abfd != NULL)
12284 return abfd;
12285
12286 /* Work around upstream bug 15652.
12287 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12288 [Whether that's a "bug" is debatable, but it is getting in our way.]
12289 We have no real idea where the dwp file is, because gdb's realpath-ing
12290 of the executable's path may have discarded the needed info.
12291 [IWBN if the dwp file name was recorded in the executable, akin to
12292 .gnu_debuglink, but that doesn't exist yet.]
12293 Strip the directory from FILE_NAME and search again. */
12294 if (*debug_file_directory != '\0')
12295 {
12296 /* Don't implicitly search the current directory here.
12297 If the user wants to search "." to handle this case,
12298 it must be added to debug-file-directory. */
ed2dc618
SM
12299 return try_open_dwop_file (dwarf2_per_objfile,
12300 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12301 0 /*search_cwd*/);
12302 }
12303
12304 return NULL;
ab5088bf
DE
12305}
12306
80626a55
DE
12307/* Initialize the use of the DWP file for the current objfile.
12308 By convention the name of the DWP file is ${objfile}.dwp.
12309 The result is NULL if it can't be found. */
a766d390 12310
400174b1 12311static std::unique_ptr<struct dwp_file>
ed2dc618 12312open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12313{
12314 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12315
82bf32bc
JK
12316 /* Try to find first .dwp for the binary file before any symbolic links
12317 resolving. */
6c447423
DE
12318
12319 /* If the objfile is a debug file, find the name of the real binary
12320 file and get the name of dwp file from there. */
d721ba37 12321 std::string dwp_name;
6c447423
DE
12322 if (objfile->separate_debug_objfile_backlink != NULL)
12323 {
12324 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12325 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12326
d721ba37 12327 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12328 }
12329 else
d721ba37
PA
12330 dwp_name = objfile->original_name;
12331
12332 dwp_name += ".dwp";
80626a55 12333
ed2dc618 12334 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12335 if (dbfd == NULL
12336 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12337 {
12338 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12339 dwp_name = objfile_name (objfile);
12340 dwp_name += ".dwp";
ed2dc618 12341 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12342 }
12343
80626a55
DE
12344 if (dbfd == NULL)
12345 {
b4f54984 12346 if (dwarf_read_debug)
d721ba37 12347 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12348 return std::unique_ptr<dwp_file> ();
3019eac3 12349 }
400174b1
TT
12350
12351 const char *name = bfd_get_filename (dbfd.get ());
12352 std::unique_ptr<struct dwp_file> dwp_file
12353 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12354
0a0f4c01 12355 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12356 dwp_file->elf_sections =
12357 OBSTACK_CALLOC (&objfile->objfile_obstack,
12358 dwp_file->num_sections, asection *);
12359
400174b1
TT
12360 bfd_map_over_sections (dwp_file->dbfd.get (),
12361 dwarf2_locate_common_dwp_sections,
12362 dwp_file.get ());
80626a55 12363
400174b1
TT
12364 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12365 0);
80626a55 12366
400174b1
TT
12367 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12368 1);
80626a55 12369
73869dc2 12370 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12371 if (dwp_file->cus && dwp_file->tus
12372 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12373 {
12374 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12375 pretty bizarre. We use pulongest here because that's the established
4d65956b 12376 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12377 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12378 " TU version %s [in DWP file %s]"),
12379 pulongest (dwp_file->cus->version),
d721ba37 12380 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12381 }
08302ed2
DE
12382
12383 if (dwp_file->cus)
12384 dwp_file->version = dwp_file->cus->version;
12385 else if (dwp_file->tus)
12386 dwp_file->version = dwp_file->tus->version;
12387 else
12388 dwp_file->version = 2;
73869dc2
DE
12389
12390 if (dwp_file->version == 2)
400174b1
TT
12391 bfd_map_over_sections (dwp_file->dbfd.get (),
12392 dwarf2_locate_v2_dwp_sections,
12393 dwp_file.get ());
73869dc2 12394
298e9637
SM
12395 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12396 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12397
b4f54984 12398 if (dwarf_read_debug)
80626a55
DE
12399 {
12400 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12401 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12402 " %s CUs, %s TUs\n",
12403 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12404 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12405 }
12406
12407 return dwp_file;
3019eac3 12408}
c906108c 12409
ab5088bf
DE
12410/* Wrapper around open_and_init_dwp_file, only open it once. */
12411
12412static struct dwp_file *
ed2dc618 12413get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12414{
12415 if (! dwarf2_per_objfile->dwp_checked)
12416 {
ed2dc618
SM
12417 dwarf2_per_objfile->dwp_file
12418 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12419 dwarf2_per_objfile->dwp_checked = 1;
12420 }
400174b1 12421 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12422}
12423
80626a55
DE
12424/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12425 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12426 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12427 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12428 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12429
12430 This is called, for example, when wanting to read a variable with a
12431 complex location. Therefore we don't want to do file i/o for every call.
12432 Therefore we don't want to look for a DWO file on every call.
12433 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12434 then we check if we've already seen DWO_NAME, and only THEN do we check
12435 for a DWO file.
12436
1c658ad5 12437 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12438 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12439
3019eac3 12440static struct dwo_unit *
80626a55
DE
12441lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12442 const char *dwo_name, const char *comp_dir,
12443 ULONGEST signature, int is_debug_types)
3019eac3 12444{
ed2dc618 12445 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12446 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12447 const char *kind = is_debug_types ? "TU" : "CU";
12448 void **dwo_file_slot;
3019eac3 12449 struct dwo_file *dwo_file;
80626a55 12450 struct dwp_file *dwp_file;
cb1df416 12451
6a506a2d
DE
12452 /* First see if there's a DWP file.
12453 If we have a DWP file but didn't find the DWO inside it, don't
12454 look for the original DWO file. It makes gdb behave differently
12455 depending on whether one is debugging in the build tree. */
cf2c3c16 12456
ed2dc618 12457 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12458 if (dwp_file != NULL)
cf2c3c16 12459 {
80626a55
DE
12460 const struct dwp_hash_table *dwp_htab =
12461 is_debug_types ? dwp_file->tus : dwp_file->cus;
12462
12463 if (dwp_htab != NULL)
12464 {
12465 struct dwo_unit *dwo_cutu =
ed2dc618 12466 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12467 signature, is_debug_types);
80626a55
DE
12468
12469 if (dwo_cutu != NULL)
12470 {
b4f54984 12471 if (dwarf_read_debug)
80626a55
DE
12472 {
12473 fprintf_unfiltered (gdb_stdlog,
12474 "Virtual DWO %s %s found: @%s\n",
12475 kind, hex_string (signature),
12476 host_address_to_string (dwo_cutu));
12477 }
12478 return dwo_cutu;
12479 }
12480 }
12481 }
6a506a2d 12482 else
80626a55 12483 {
6a506a2d 12484 /* No DWP file, look for the DWO file. */
80626a55 12485
ed2dc618
SM
12486 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12487 dwo_name, comp_dir);
6a506a2d 12488 if (*dwo_file_slot == NULL)
80626a55 12489 {
6a506a2d
DE
12490 /* Read in the file and build a table of the CUs/TUs it contains. */
12491 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12492 }
6a506a2d 12493 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12494 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12495
6a506a2d 12496 if (dwo_file != NULL)
19c3d4c9 12497 {
6a506a2d
DE
12498 struct dwo_unit *dwo_cutu = NULL;
12499
12500 if (is_debug_types && dwo_file->tus)
12501 {
12502 struct dwo_unit find_dwo_cutu;
12503
12504 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12505 find_dwo_cutu.signature = signature;
9a3c8263 12506 dwo_cutu
b0b6a987
TT
12507 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12508 &find_dwo_cutu);
6a506a2d 12509 }
33c5cd75 12510 else if (!is_debug_types && dwo_file->cus)
80626a55 12511 {
33c5cd75
DB
12512 struct dwo_unit find_dwo_cutu;
12513
12514 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12515 find_dwo_cutu.signature = signature;
b0b6a987 12516 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12517 &find_dwo_cutu);
6a506a2d
DE
12518 }
12519
12520 if (dwo_cutu != NULL)
12521 {
b4f54984 12522 if (dwarf_read_debug)
6a506a2d
DE
12523 {
12524 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12525 kind, dwo_name, hex_string (signature),
12526 host_address_to_string (dwo_cutu));
12527 }
12528 return dwo_cutu;
80626a55
DE
12529 }
12530 }
2e276125 12531 }
9cdd5dbd 12532
80626a55
DE
12533 /* We didn't find it. This could mean a dwo_id mismatch, or
12534 someone deleted the DWO/DWP file, or the search path isn't set up
12535 correctly to find the file. */
12536
b4f54984 12537 if (dwarf_read_debug)
80626a55
DE
12538 {
12539 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12540 kind, dwo_name, hex_string (signature));
12541 }
3019eac3 12542
6656a72d
DE
12543 /* This is a warning and not a complaint because it can be caused by
12544 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12545 {
12546 /* Print the name of the DWP file if we looked there, helps the user
12547 better diagnose the problem. */
791afaa2 12548 std::string dwp_text;
43942612
DE
12549
12550 if (dwp_file != NULL)
791afaa2
TT
12551 dwp_text = string_printf (" [in DWP file %s]",
12552 lbasename (dwp_file->name));
43942612 12553
9d8780f0 12554 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12555 " [in module %s]"),
12556 kind, dwo_name, hex_string (signature),
791afaa2 12557 dwp_text.c_str (),
43942612 12558 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12559 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12560 }
3019eac3 12561 return NULL;
5fb290d7
DJ
12562}
12563
80626a55
DE
12564/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12565 See lookup_dwo_cutu_unit for details. */
12566
12567static struct dwo_unit *
12568lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12569 const char *dwo_name, const char *comp_dir,
12570 ULONGEST signature)
12571{
12572 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12573}
12574
12575/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12576 See lookup_dwo_cutu_unit for details. */
12577
12578static struct dwo_unit *
12579lookup_dwo_type_unit (struct signatured_type *this_tu,
12580 const char *dwo_name, const char *comp_dir)
12581{
12582 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12583}
12584
89e63ee4
DE
12585/* Traversal function for queue_and_load_all_dwo_tus. */
12586
12587static int
12588queue_and_load_dwo_tu (void **slot, void *info)
12589{
12590 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12591 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12592 ULONGEST signature = dwo_unit->signature;
12593 struct signatured_type *sig_type =
12594 lookup_dwo_signatured_type (per_cu->cu, signature);
12595
12596 if (sig_type != NULL)
12597 {
12598 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12599
12600 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12601 a real dependency of PER_CU on SIG_TYPE. That is detected later
12602 while processing PER_CU. */
12603 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12604 load_full_type_unit (sig_cu);
ae640021 12605 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12606 }
12607
12608 return 1;
12609}
12610
12611/* Queue all TUs contained in the DWO of PER_CU to be read in.
12612 The DWO may have the only definition of the type, though it may not be
12613 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12614 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12615
12616static void
12617queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12618{
12619 struct dwo_unit *dwo_unit;
12620 struct dwo_file *dwo_file;
12621
12622 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12623 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12624 gdb_assert (per_cu->cu != NULL);
12625
12626 dwo_unit = per_cu->cu->dwo_unit;
12627 gdb_assert (dwo_unit != NULL);
12628
12629 dwo_file = dwo_unit->dwo_file;
12630 if (dwo_file->tus != NULL)
b0b6a987
TT
12631 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12632 per_cu);
89e63ee4
DE
12633}
12634
3019eac3 12635/* Read in various DIEs. */
348e048f 12636
d389af10 12637/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12638 Inherit only the children of the DW_AT_abstract_origin DIE not being
12639 already referenced by DW_AT_abstract_origin from the children of the
12640 current DIE. */
d389af10
JK
12641
12642static void
12643inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12644{
12645 struct die_info *child_die;
791afaa2 12646 sect_offset *offsetp;
d389af10
JK
12647 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12648 struct die_info *origin_die;
12649 /* Iterator of the ORIGIN_DIE children. */
12650 struct die_info *origin_child_die;
d389af10 12651 struct attribute *attr;
cd02d79d
PA
12652 struct dwarf2_cu *origin_cu;
12653 struct pending **origin_previous_list_in_scope;
d389af10
JK
12654
12655 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12656 if (!attr)
12657 return;
12658
cd02d79d
PA
12659 /* Note that following die references may follow to a die in a
12660 different cu. */
12661
12662 origin_cu = cu;
12663 origin_die = follow_die_ref (die, attr, &origin_cu);
12664
12665 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12666 symbols in. */
12667 origin_previous_list_in_scope = origin_cu->list_in_scope;
12668 origin_cu->list_in_scope = cu->list_in_scope;
12669
edb3359d
DJ
12670 if (die->tag != origin_die->tag
12671 && !(die->tag == DW_TAG_inlined_subroutine
12672 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12673 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12674 sect_offset_str (die->sect_off),
12675 sect_offset_str (origin_die->sect_off));
d389af10 12676
791afaa2 12677 std::vector<sect_offset> offsets;
d389af10 12678
3ea89b92
PMR
12679 for (child_die = die->child;
12680 child_die && child_die->tag;
436c571c 12681 child_die = child_die->sibling)
3ea89b92
PMR
12682 {
12683 struct die_info *child_origin_die;
12684 struct dwarf2_cu *child_origin_cu;
12685
12686 /* We are trying to process concrete instance entries:
216f72a1 12687 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12688 it's not relevant to our analysis here. i.e. detecting DIEs that are
12689 present in the abstract instance but not referenced in the concrete
12690 one. */
216f72a1
JK
12691 if (child_die->tag == DW_TAG_call_site
12692 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12693 continue;
12694
c38f313d
DJ
12695 /* For each CHILD_DIE, find the corresponding child of
12696 ORIGIN_DIE. If there is more than one layer of
12697 DW_AT_abstract_origin, follow them all; there shouldn't be,
12698 but GCC versions at least through 4.4 generate this (GCC PR
12699 40573). */
3ea89b92
PMR
12700 child_origin_die = child_die;
12701 child_origin_cu = cu;
c38f313d
DJ
12702 while (1)
12703 {
cd02d79d
PA
12704 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12705 child_origin_cu);
c38f313d
DJ
12706 if (attr == NULL)
12707 break;
cd02d79d
PA
12708 child_origin_die = follow_die_ref (child_origin_die, attr,
12709 &child_origin_cu);
c38f313d
DJ
12710 }
12711
d389af10
JK
12712 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12713 counterpart may exist. */
c38f313d 12714 if (child_origin_die != child_die)
d389af10 12715 {
edb3359d
DJ
12716 if (child_die->tag != child_origin_die->tag
12717 && !(child_die->tag == DW_TAG_inlined_subroutine
12718 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12719 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12720 "different tags"),
9d8780f0
SM
12721 sect_offset_str (child_die->sect_off),
12722 sect_offset_str (child_origin_die->sect_off));
c38f313d 12723 if (child_origin_die->parent != origin_die)
b98664d3 12724 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12725 "different parents"),
9d8780f0
SM
12726 sect_offset_str (child_die->sect_off),
12727 sect_offset_str (child_origin_die->sect_off));
c38f313d 12728 else
791afaa2 12729 offsets.push_back (child_origin_die->sect_off);
d389af10 12730 }
d389af10 12731 }
791afaa2
TT
12732 std::sort (offsets.begin (), offsets.end ());
12733 sect_offset *offsets_end = offsets.data () + offsets.size ();
12734 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12735 if (offsetp[-1] == *offsetp)
b98664d3 12736 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12737 "to DIE %s as their abstract origin"),
12738 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12739
791afaa2 12740 offsetp = offsets.data ();
d389af10
JK
12741 origin_child_die = origin_die->child;
12742 while (origin_child_die && origin_child_die->tag)
12743 {
12744 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12745 while (offsetp < offsets_end
9c541725 12746 && *offsetp < origin_child_die->sect_off)
d389af10 12747 offsetp++;
b64f50a1 12748 if (offsetp >= offsets_end
9c541725 12749 || *offsetp > origin_child_die->sect_off)
d389af10 12750 {
adde2bff
DE
12751 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12752 Check whether we're already processing ORIGIN_CHILD_DIE.
12753 This can happen with mutually referenced abstract_origins.
12754 PR 16581. */
12755 if (!origin_child_die->in_process)
12756 process_die (origin_child_die, origin_cu);
d389af10 12757 }
436c571c 12758 origin_child_die = origin_child_die->sibling;
d389af10 12759 }
cd02d79d 12760 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12761
12762 if (cu != origin_cu)
12763 compute_delayed_physnames (origin_cu);
d389af10
JK
12764}
12765
c906108c 12766static void
e7c27a73 12767read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12768{
518817b3 12769 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12770 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12771 struct context_stack *newobj;
c906108c
SS
12772 CORE_ADDR lowpc;
12773 CORE_ADDR highpc;
12774 struct die_info *child_die;
edb3359d 12775 struct attribute *attr, *call_line, *call_file;
15d034d0 12776 const char *name;
e142c38c 12777 CORE_ADDR baseaddr;
801e3a5b 12778 struct block *block;
edb3359d 12779 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12780 std::vector<struct symbol *> template_args;
34eaf542 12781 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12782
12783 if (inlined_func)
12784 {
12785 /* If we do not have call site information, we can't show the
12786 caller of this inlined function. That's too confusing, so
12787 only use the scope for local variables. */
12788 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12789 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12790 if (call_line == NULL || call_file == NULL)
12791 {
12792 read_lexical_block_scope (die, cu);
12793 return;
12794 }
12795 }
c906108c 12796
b3b3bada 12797 baseaddr = objfile->text_section_offset ();
e142c38c 12798
94af9270 12799 name = dwarf2_name (die, cu);
c906108c 12800
e8d05480
JB
12801 /* Ignore functions with missing or empty names. These are actually
12802 illegal according to the DWARF standard. */
12803 if (name == NULL)
12804 {
b98664d3 12805 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12806 sect_offset_str (die->sect_off));
e8d05480
JB
12807 return;
12808 }
12809
12810 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12811 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12812 <= PC_BOUNDS_INVALID)
e8d05480 12813 {
ae4d0c03
PM
12814 attr = dwarf2_attr (die, DW_AT_external, cu);
12815 if (!attr || !DW_UNSND (attr))
b98664d3 12816 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12817 "for subprogram DIE at %s"),
12818 sect_offset_str (die->sect_off));
e8d05480
JB
12819 return;
12820 }
c906108c 12821
3e29f34a
MR
12822 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12823 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12824
34eaf542
TT
12825 /* If we have any template arguments, then we must allocate a
12826 different sort of symbol. */
436c571c 12827 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
12828 {
12829 if (child_die->tag == DW_TAG_template_type_param
12830 || child_die->tag == DW_TAG_template_value_param)
12831 {
e623cf5d 12832 templ_func = allocate_template_symbol (objfile);
cf724bc9 12833 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12834 break;
12835 }
12836 }
12837
c24bdb02 12838 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12839 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12840 (struct symbol *) templ_func);
4c2df51b 12841
81873cc8 12842 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12843 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12844 cu->language);
12845
4cecd739
DJ
12846 /* If there is a location expression for DW_AT_frame_base, record
12847 it. */
e142c38c 12848 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12849 if (attr != nullptr)
fe978cb0 12850 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12851
63e43d3a
PMR
12852 /* If there is a location for the static link, record it. */
12853 newobj->static_link = NULL;
12854 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12855 if (attr != nullptr)
63e43d3a 12856 {
224c3ddb
SM
12857 newobj->static_link
12858 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12859 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12860 cu->per_cu->addr_type ());
63e43d3a
PMR
12861 }
12862
c24bdb02 12863 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12864
639d11d3 12865 if (die->child != NULL)
c906108c 12866 {
639d11d3 12867 child_die = die->child;
c906108c
SS
12868 while (child_die && child_die->tag)
12869 {
34eaf542
TT
12870 if (child_die->tag == DW_TAG_template_type_param
12871 || child_die->tag == DW_TAG_template_value_param)
12872 {
12873 struct symbol *arg = new_symbol (child_die, NULL, cu);
12874
f1078f66 12875 if (arg != NULL)
2f4732b0 12876 template_args.push_back (arg);
34eaf542
TT
12877 }
12878 else
12879 process_die (child_die, cu);
436c571c 12880 child_die = child_die->sibling;
c906108c
SS
12881 }
12882 }
12883
d389af10
JK
12884 inherit_abstract_dies (die, cu);
12885
4a811a97
UW
12886 /* If we have a DW_AT_specification, we might need to import using
12887 directives from the context of the specification DIE. See the
12888 comment in determine_prefix. */
12889 if (cu->language == language_cplus
12890 && dwarf2_attr (die, DW_AT_specification, cu))
12891 {
12892 struct dwarf2_cu *spec_cu = cu;
12893 struct die_info *spec_die = die_specification (die, &spec_cu);
12894
12895 while (spec_die)
12896 {
12897 child_die = spec_die->child;
12898 while (child_die && child_die->tag)
12899 {
12900 if (child_die->tag == DW_TAG_imported_module)
12901 process_die (child_die, spec_cu);
436c571c 12902 child_die = child_die->sibling;
4a811a97
UW
12903 }
12904
12905 /* In some cases, GCC generates specification DIEs that
12906 themselves contain DW_AT_specification attributes. */
12907 spec_die = die_specification (spec_die, &spec_cu);
12908 }
12909 }
12910
c24bdb02 12911 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12912 /* Make a block for the local symbols within. */
c24bdb02 12913 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12914 cstk.static_link, lowpc, highpc);
801e3a5b 12915
df8a16a1 12916 /* For C++, set the block's scope. */
45280282
IB
12917 if ((cu->language == language_cplus
12918 || cu->language == language_fortran
c44af4eb
TT
12919 || cu->language == language_d
12920 || cu->language == language_rust)
4d4ec4e5 12921 && cu->processing_has_namespace_info)
195a3f6c
TT
12922 block_set_scope (block, determine_prefix (die, cu),
12923 &objfile->objfile_obstack);
df8a16a1 12924
801e3a5b
JB
12925 /* If we have address ranges, record them. */
12926 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12927
a60f3166 12928 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12929
34eaf542 12930 /* Attach template arguments to function. */
2f4732b0 12931 if (!template_args.empty ())
34eaf542
TT
12932 {
12933 gdb_assert (templ_func != NULL);
12934
2f4732b0 12935 templ_func->n_template_arguments = template_args.size ();
34eaf542 12936 templ_func->template_arguments
8d749320
SM
12937 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12938 templ_func->n_template_arguments);
34eaf542 12939 memcpy (templ_func->template_arguments,
2f4732b0 12940 template_args.data (),
34eaf542 12941 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
12942
12943 /* Make sure that the symtab is set on the new symbols. Even
12944 though they don't appear in this symtab directly, other parts
12945 of gdb assume that symbols do, and this is reasonably
12946 true. */
8634679f 12947 for (symbol *sym : template_args)
3e1d3d8c 12948 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
12949 }
12950
208d8187
JB
12951 /* In C++, we can have functions nested inside functions (e.g., when
12952 a function declares a class that has methods). This means that
12953 when we finish processing a function scope, we may need to go
12954 back to building a containing block's symbol lists. */
c24bdb02
KS
12955 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12956 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 12957
921e78cf
JB
12958 /* If we've finished processing a top-level function, subsequent
12959 symbols go in the file symbol list. */
c24bdb02
KS
12960 if (cu->get_builder ()->outermost_context_p ())
12961 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
12962}
12963
12964/* Process all the DIES contained within a lexical block scope. Start
12965 a new scope, process the dies, and then close the scope. */
12966
12967static void
e7c27a73 12968read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12969{
518817b3 12970 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12971 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12972 CORE_ADDR lowpc, highpc;
12973 struct die_info *child_die;
e142c38c
DJ
12974 CORE_ADDR baseaddr;
12975
b3b3bada 12976 baseaddr = objfile->text_section_offset ();
c906108c
SS
12977
12978 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
12979 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12980 as multiple lexical blocks? Handling children in a sane way would
6e70227d 12981 be nasty. Might be easier to properly extend generic blocks to
af34e669 12982 describe ranges. */
e385593e
JK
12983 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12984 {
12985 case PC_BOUNDS_NOT_PRESENT:
12986 /* DW_TAG_lexical_block has no attributes, process its children as if
12987 there was no wrapping by that DW_TAG_lexical_block.
12988 GCC does no longer produces such DWARF since GCC r224161. */
12989 for (child_die = die->child;
12990 child_die != NULL && child_die->tag;
436c571c 12991 child_die = child_die->sibling)
e385593e
JK
12992 process_die (child_die, cu);
12993 return;
12994 case PC_BOUNDS_INVALID:
12995 return;
12996 }
3e29f34a
MR
12997 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12998 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12999
c24bdb02 13000 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13001 if (die->child != NULL)
c906108c 13002 {
639d11d3 13003 child_die = die->child;
c906108c
SS
13004 while (child_die && child_die->tag)
13005 {
e7c27a73 13006 process_die (child_die, cu);
436c571c 13007 child_die = child_die->sibling;
c906108c
SS
13008 }
13009 }
3ea89b92 13010 inherit_abstract_dies (die, cu);
c24bdb02 13011 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13012
c24bdb02
KS
13013 if (*cu->get_builder ()->get_local_symbols () != NULL
13014 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13015 {
801e3a5b 13016 struct block *block
c24bdb02 13017 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13018 cstk.start_addr, highpc);
801e3a5b
JB
13019
13020 /* Note that recording ranges after traversing children, as we
13021 do here, means that recording a parent's ranges entails
13022 walking across all its children's ranges as they appear in
13023 the address map, which is quadratic behavior.
13024
13025 It would be nicer to record the parent's ranges before
13026 traversing its children, simply overriding whatever you find
13027 there. But since we don't even decide whether to create a
13028 block until after we've traversed its children, that's hard
13029 to do. */
13030 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13031 }
c24bdb02
KS
13032 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13033 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13034}
13035
216f72a1 13036/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13037
13038static void
13039read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13040{
518817b3 13041 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13043 CORE_ADDR pc, baseaddr;
13044 struct attribute *attr;
13045 struct call_site *call_site, call_site_local;
13046 void **slot;
13047 int nparams;
13048 struct die_info *child_die;
13049
b3b3bada 13050 baseaddr = objfile->text_section_offset ();
96408a79 13051
216f72a1
JK
13052 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13053 if (attr == NULL)
13054 {
13055 /* This was a pre-DWARF-5 GNU extension alias
13056 for DW_AT_call_return_pc. */
13057 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13058 }
96408a79
SA
13059 if (!attr)
13060 {
b98664d3 13061 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13062 "DIE %s [in module %s]"),
13063 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13064 return;
13065 }
cd6c91b4 13066 pc = attr->value_as_address () + baseaddr;
3e29f34a 13067 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13068
13069 if (cu->call_site_htab == NULL)
13070 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13071 NULL, &objfile->objfile_obstack,
13072 hashtab_obstack_allocate, NULL);
13073 call_site_local.pc = pc;
13074 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13075 if (*slot != NULL)
13076 {
b98664d3 13077 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13078 "DIE %s [in module %s]"),
13079 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13080 objfile_name (objfile));
96408a79
SA
13081 return;
13082 }
13083
13084 /* Count parameters at the caller. */
13085
13086 nparams = 0;
13087 for (child_die = die->child; child_die && child_die->tag;
436c571c 13088 child_die = child_die->sibling)
96408a79 13089 {
216f72a1
JK
13090 if (child_die->tag != DW_TAG_call_site_parameter
13091 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13092 {
b98664d3 13093 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13094 "DW_TAG_call_site child DIE %s [in module %s]"),
13095 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13096 objfile_name (objfile));
96408a79
SA
13097 continue;
13098 }
13099
13100 nparams++;
13101 }
13102
224c3ddb
SM
13103 call_site
13104 = ((struct call_site *)
13105 obstack_alloc (&objfile->objfile_obstack,
13106 sizeof (*call_site)
13107 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13108 *slot = call_site;
13109 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13110 call_site->pc = pc;
13111
216f72a1
JK
13112 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13113 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13114 {
13115 struct die_info *func_die;
13116
13117 /* Skip also over DW_TAG_inlined_subroutine. */
13118 for (func_die = die->parent;
13119 func_die && func_die->tag != DW_TAG_subprogram
13120 && func_die->tag != DW_TAG_subroutine_type;
13121 func_die = func_die->parent);
13122
216f72a1
JK
13123 /* DW_AT_call_all_calls is a superset
13124 of DW_AT_call_all_tail_calls. */
96408a79 13125 if (func_die
216f72a1 13126 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13127 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13128 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13129 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13130 {
13131 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13132 not complete. But keep CALL_SITE for look ups via call_site_htab,
13133 both the initial caller containing the real return address PC and
13134 the final callee containing the current PC of a chain of tail
13135 calls do not need to have the tail call list complete. But any
13136 function candidate for a virtual tail call frame searched via
13137 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13138 determined unambiguously. */
13139 }
13140 else
13141 {
13142 struct type *func_type = NULL;
13143
13144 if (func_die)
13145 func_type = get_die_type (func_die, cu);
13146 if (func_type != NULL)
13147 {
13148 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13149
13150 /* Enlist this call site to the function. */
13151 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13152 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13153 }
13154 else
b98664d3 13155 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13156 "DIE %s [in module %s]"),
13157 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13158 }
13159 }
13160
216f72a1
JK
13161 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13162 if (attr == NULL)
13163 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13164 if (attr == NULL)
13165 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13166 if (attr == NULL)
216f72a1
JK
13167 {
13168 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13169 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13170 }
96408a79 13171 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13172 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13173 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13174 else if (attr->form_is_block ())
96408a79
SA
13175 {
13176 struct dwarf2_locexpr_baton *dlbaton;
13177
8d749320 13178 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13179 dlbaton->data = DW_BLOCK (attr)->data;
13180 dlbaton->size = DW_BLOCK (attr)->size;
13181 dlbaton->per_cu = cu->per_cu;
13182
13183 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13184 }
cd6c91b4 13185 else if (attr->form_is_ref ())
96408a79 13186 {
96408a79
SA
13187 struct dwarf2_cu *target_cu = cu;
13188 struct die_info *target_die;
13189
ac9ec31b 13190 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13191 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13192 if (die_is_declaration (target_die, target_cu))
13193 {
7d45c7c3 13194 const char *target_physname;
9112db09
JK
13195
13196 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13197 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13198 if (target_physname == NULL)
9112db09 13199 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13200 if (target_physname == NULL)
b98664d3 13201 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13202 "physname, for referencing DIE %s [in module %s]"),
13203 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13204 else
7d455152 13205 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13206 }
13207 else
13208 {
13209 CORE_ADDR lowpc;
13210
13211 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13212 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13213 <= PC_BOUNDS_INVALID)
b98664d3 13214 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13215 "low pc, for referencing DIE %s [in module %s]"),
13216 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13217 else
3e29f34a
MR
13218 {
13219 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13220 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13221 }
96408a79
SA
13222 }
13223 }
13224 else
b98664d3 13225 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13226 "block nor reference, for DIE %s [in module %s]"),
13227 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13228
13229 call_site->per_cu = cu->per_cu;
13230
13231 for (child_die = die->child;
13232 child_die && child_die->tag;
436c571c 13233 child_die = child_die->sibling)
96408a79 13234 {
96408a79 13235 struct call_site_parameter *parameter;
1788b2d3 13236 struct attribute *loc, *origin;
96408a79 13237
216f72a1
JK
13238 if (child_die->tag != DW_TAG_call_site_parameter
13239 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13240 {
13241 /* Already printed the complaint above. */
13242 continue;
13243 }
13244
13245 gdb_assert (call_site->parameter_count < nparams);
13246 parameter = &call_site->parameter[call_site->parameter_count];
13247
1788b2d3
JK
13248 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13249 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13250 register is contained in DW_AT_call_value. */
96408a79 13251
24c5c679 13252 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13253 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13254 if (origin == NULL)
13255 {
13256 /* This was a pre-DWARF-5 GNU extension alias
13257 for DW_AT_call_parameter. */
13258 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13259 }
cd6c91b4 13260 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13261 {
1788b2d3 13262 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13263
13264 sect_offset sect_off
13265 = (sect_offset) dwarf2_get_ref_die_offset (origin);
4057dfde 13266 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13267 {
13268 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13269 binding can be done only inside one CU. Such referenced DIE
13270 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13271 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13272 "DW_TAG_call_site child DIE %s [in module %s]"),
13273 sect_offset_str (child_die->sect_off),
9c541725 13274 objfile_name (objfile));
d76b7dbc
JK
13275 continue;
13276 }
9c541725
PA
13277 parameter->u.param_cu_off
13278 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13279 }
4fc6c0d5 13280 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13281 {
b98664d3 13282 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13283 "DW_TAG_call_site child DIE %s [in module %s]"),
13284 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13285 continue;
13286 }
24c5c679 13287 else
96408a79 13288 {
24c5c679
JK
13289 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13290 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13291 if (parameter->u.dwarf_reg != -1)
13292 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13293 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13294 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13295 &parameter->u.fb_offset))
13296 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13297 else
13298 {
b98664d3 13299 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13300 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13301 "DW_TAG_call_site child DIE %s "
24c5c679 13302 "[in module %s]"),
9d8780f0 13303 sect_offset_str (child_die->sect_off),
9c541725 13304 objfile_name (objfile));
24c5c679
JK
13305 continue;
13306 }
96408a79
SA
13307 }
13308
216f72a1
JK
13309 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13310 if (attr == NULL)
13311 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13312 if (attr == NULL || !attr->form_is_block ())
96408a79 13313 {
b98664d3 13314 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13315 "DW_TAG_call_site child DIE %s [in module %s]"),
13316 sect_offset_str (child_die->sect_off),
9c541725 13317 objfile_name (objfile));
96408a79
SA
13318 continue;
13319 }
13320 parameter->value = DW_BLOCK (attr)->data;
13321 parameter->value_size = DW_BLOCK (attr)->size;
13322
13323 /* Parameters are not pre-cleared by memset above. */
13324 parameter->data_value = NULL;
13325 parameter->data_value_size = 0;
13326 call_site->parameter_count++;
13327
216f72a1
JK
13328 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13329 if (attr == NULL)
13330 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13331 if (attr != nullptr)
96408a79 13332 {
4fc6c0d5 13333 if (!attr->form_is_block ())
b98664d3 13334 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13335 "DW_TAG_call_site child DIE %s [in module %s]"),
13336 sect_offset_str (child_die->sect_off),
9c541725 13337 objfile_name (objfile));
96408a79
SA
13338 else
13339 {
13340 parameter->data_value = DW_BLOCK (attr)->data;
13341 parameter->data_value_size = DW_BLOCK (attr)->size;
13342 }
13343 }
13344 }
13345}
13346
71a3c369
TT
13347/* Helper function for read_variable. If DIE represents a virtual
13348 table, then return the type of the concrete object that is
13349 associated with the virtual table. Otherwise, return NULL. */
13350
13351static struct type *
13352rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13353{
13354 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13355 if (attr == NULL)
13356 return NULL;
13357
13358 /* Find the type DIE. */
13359 struct die_info *type_die = NULL;
13360 struct dwarf2_cu *type_cu = cu;
13361
cd6c91b4 13362 if (attr->form_is_ref ())
71a3c369
TT
13363 type_die = follow_die_ref (die, attr, &type_cu);
13364 if (type_die == NULL)
13365 return NULL;
13366
13367 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13368 return NULL;
13369 return die_containing_type (type_die, type_cu);
13370}
13371
13372/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13373
13374static void
13375read_variable (struct die_info *die, struct dwarf2_cu *cu)
13376{
13377 struct rust_vtable_symbol *storage = NULL;
13378
13379 if (cu->language == language_rust)
13380 {
13381 struct type *containing_type = rust_containing_type (die, cu);
13382
13383 if (containing_type != NULL)
13384 {
518817b3 13385 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13386
468c0cbb 13387 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13388 initialize_objfile_symbol (storage);
13389 storage->concrete_type = containing_type;
cf724bc9 13390 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13391 }
13392 }
13393
e4a62c65
TV
13394 struct symbol *res = new_symbol (die, NULL, cu, storage);
13395 struct attribute *abstract_origin
13396 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13397 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13398 if (res == NULL && loc && abstract_origin)
13399 {
13400 /* We have a variable without a name, but with a location and an abstract
13401 origin. This may be a concrete instance of an abstract variable
13402 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13403 later. */
13404 struct dwarf2_cu *origin_cu = cu;
13405 struct die_info *origin_die
13406 = follow_die_ref (die, abstract_origin, &origin_cu);
13407 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13408 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13409 }
71a3c369
TT
13410}
13411
43988095
JK
13412/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13413 reading .debug_rnglists.
13414 Callback's type should be:
13415 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13416 Return true if the attributes are present and valid, otherwise,
13417 return false. */
13418
13419template <typename Callback>
13420static bool
13421dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13422 Callback &&callback)
13423{
ed2dc618 13424 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13425 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13426 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13427 bfd *obfd = objfile->obfd;
43988095 13428 /* Base address selection entry. */
2b24b6e4 13429 gdb::optional<CORE_ADDR> base;
43988095 13430 const gdb_byte *buffer;
43988095
JK
13431 CORE_ADDR baseaddr;
13432 bool overflow = false;
13433
43988095
JK
13434 base = cu->base_address;
13435
96b79293 13436 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13437 if (offset >= dwarf2_per_objfile->rnglists.size)
13438 {
b98664d3 13439 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13440 offset);
13441 return false;
13442 }
13443 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13444
b3b3bada 13445 baseaddr = objfile->text_section_offset ();
43988095
JK
13446
13447 while (1)
13448 {
7814882a
JK
13449 /* Initialize it due to a false compiler warning. */
13450 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13451 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13452 + dwarf2_per_objfile->rnglists.size);
13453 unsigned int bytes_read;
13454
13455 if (buffer == buf_end)
13456 {
13457 overflow = true;
13458 break;
13459 }
13460 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13461 switch (rlet)
13462 {
13463 case DW_RLE_end_of_list:
13464 break;
13465 case DW_RLE_base_address:
13466 if (buffer + cu->header.addr_size > buf_end)
13467 {
13468 overflow = true;
13469 break;
13470 }
c8a7a66f 13471 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13472 buffer += bytes_read;
13473 break;
13474 case DW_RLE_start_length:
13475 if (buffer + cu->header.addr_size > buf_end)
13476 {
13477 overflow = true;
13478 break;
13479 }
c8a7a66f
TT
13480 range_beginning = cu->header.read_address (obfd, buffer,
13481 &bytes_read);
43988095
JK
13482 buffer += bytes_read;
13483 range_end = (range_beginning
13484 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13485 buffer += bytes_read;
13486 if (buffer > buf_end)
13487 {
13488 overflow = true;
13489 break;
13490 }
13491 break;
13492 case DW_RLE_offset_pair:
13493 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13494 buffer += bytes_read;
13495 if (buffer > buf_end)
13496 {
13497 overflow = true;
13498 break;
13499 }
13500 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13501 buffer += bytes_read;
13502 if (buffer > buf_end)
13503 {
13504 overflow = true;
13505 break;
13506 }
13507 break;
13508 case DW_RLE_start_end:
13509 if (buffer + 2 * cu->header.addr_size > buf_end)
13510 {
13511 overflow = true;
13512 break;
13513 }
c8a7a66f
TT
13514 range_beginning = cu->header.read_address (obfd, buffer,
13515 &bytes_read);
43988095 13516 buffer += bytes_read;
c8a7a66f 13517 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13518 buffer += bytes_read;
13519 break;
13520 default:
b98664d3 13521 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13522 return false;
13523 }
13524 if (rlet == DW_RLE_end_of_list || overflow)
13525 break;
13526 if (rlet == DW_RLE_base_address)
13527 continue;
13528
2b24b6e4 13529 if (!base.has_value ())
43988095
JK
13530 {
13531 /* We have no valid base address for the ranges
13532 data. */
b98664d3 13533 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13534 return false;
13535 }
13536
13537 if (range_beginning > range_end)
13538 {
13539 /* Inverted range entries are invalid. */
b98664d3 13540 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13541 return false;
13542 }
13543
13544 /* Empty range entries have no effect. */
13545 if (range_beginning == range_end)
13546 continue;
13547
2b24b6e4
TT
13548 range_beginning += *base;
13549 range_end += *base;
43988095
JK
13550
13551 /* A not-uncommon case of bad debug info.
13552 Don't pollute the addrmap with bad data. */
13553 if (range_beginning + baseaddr == 0
13554 && !dwarf2_per_objfile->has_section_at_zero)
13555 {
b98664d3 13556 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13557 " [in module %s]"), objfile_name (objfile));
13558 continue;
13559 }
13560
13561 callback (range_beginning, range_end);
13562 }
13563
13564 if (overflow)
13565 {
b98664d3 13566 complaint (_("Offset %d is not terminated "
43988095
JK
13567 "for DW_AT_ranges attribute"),
13568 offset);
13569 return false;
13570 }
13571
13572 return true;
13573}
13574
13575/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13576 Callback's type should be:
13577 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13578 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13579
43988095 13580template <typename Callback>
43039443 13581static int
5f46c5a5 13582dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13583 Callback &&callback)
43039443 13584{
ed2dc618 13585 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13586 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13587 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13588 struct comp_unit_head *cu_header = &cu->header;
13589 bfd *obfd = objfile->obfd;
13590 unsigned int addr_size = cu_header->addr_size;
13591 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13592 /* Base address selection entry. */
2b24b6e4 13593 gdb::optional<CORE_ADDR> base;
43039443 13594 unsigned int dummy;
d521ce57 13595 const gdb_byte *buffer;
ff013f42 13596 CORE_ADDR baseaddr;
43039443 13597
43988095
JK
13598 if (cu_header->version >= 5)
13599 return dwarf2_rnglists_process (offset, cu, callback);
13600
d00adf39 13601 base = cu->base_address;
43039443 13602
96b79293 13603 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13604 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13605 {
b98664d3 13606 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13607 offset);
13608 return 0;
13609 }
dce234bc 13610 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13611
b3b3bada 13612 baseaddr = objfile->text_section_offset ();
ff013f42 13613
43039443
JK
13614 while (1)
13615 {
13616 CORE_ADDR range_beginning, range_end;
13617
c8a7a66f 13618 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13619 buffer += addr_size;
c8a7a66f 13620 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13621 buffer += addr_size;
13622 offset += 2 * addr_size;
13623
13624 /* An end of list marker is a pair of zero addresses. */
13625 if (range_beginning == 0 && range_end == 0)
13626 /* Found the end of list entry. */
13627 break;
13628
13629 /* Each base address selection entry is a pair of 2 values.
13630 The first is the largest possible address, the second is
13631 the base address. Check for a base address here. */
13632 if ((range_beginning & mask) == mask)
13633 {
28d2bfb9
AB
13634 /* If we found the largest possible address, then we already
13635 have the base address in range_end. */
13636 base = range_end;
43039443
JK
13637 continue;
13638 }
13639
2b24b6e4 13640 if (!base.has_value ())
43039443
JK
13641 {
13642 /* We have no valid base address for the ranges
13643 data. */
b98664d3 13644 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13645 return 0;
13646 }
13647
9277c30c
UW
13648 if (range_beginning > range_end)
13649 {
13650 /* Inverted range entries are invalid. */
b98664d3 13651 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13652 return 0;
13653 }
13654
13655 /* Empty range entries have no effect. */
13656 if (range_beginning == range_end)
13657 continue;
13658
2b24b6e4
TT
13659 range_beginning += *base;
13660 range_end += *base;
43039443 13661
01093045
DE
13662 /* A not-uncommon case of bad debug info.
13663 Don't pollute the addrmap with bad data. */
13664 if (range_beginning + baseaddr == 0
13665 && !dwarf2_per_objfile->has_section_at_zero)
13666 {
b98664d3 13667 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13668 " [in module %s]"), objfile_name (objfile));
01093045
DE
13669 continue;
13670 }
13671
5f46c5a5
JK
13672 callback (range_beginning, range_end);
13673 }
13674
13675 return 1;
13676}
13677
13678/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13679 Return 1 if the attributes are present and valid, otherwise, return 0.
13680 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13681
13682static int
13683dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13684 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13685 dwarf2_psymtab *ranges_pst)
5f46c5a5 13686{
518817b3 13687 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13688 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13689 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13690 int low_set = 0;
13691 CORE_ADDR low = 0;
13692 CORE_ADDR high = 0;
13693 int retval;
13694
13695 retval = dwarf2_ranges_process (offset, cu,
13696 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13697 {
9277c30c 13698 if (ranges_pst != NULL)
3e29f34a
MR
13699 {
13700 CORE_ADDR lowpc;
13701 CORE_ADDR highpc;
13702
79748972
TT
13703 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13704 range_beginning + baseaddr)
13705 - baseaddr);
13706 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13707 range_end + baseaddr)
13708 - baseaddr);
d320c2b5
TT
13709 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13710 lowpc, highpc - 1, ranges_pst);
3e29f34a 13711 }
ff013f42 13712
43039443
JK
13713 /* FIXME: This is recording everything as a low-high
13714 segment of consecutive addresses. We should have a
13715 data structure for discontiguous block ranges
13716 instead. */
13717 if (! low_set)
13718 {
13719 low = range_beginning;
13720 high = range_end;
13721 low_set = 1;
13722 }
13723 else
13724 {
13725 if (range_beginning < low)
13726 low = range_beginning;
13727 if (range_end > high)
13728 high = range_end;
13729 }
5f46c5a5
JK
13730 });
13731 if (!retval)
13732 return 0;
43039443
JK
13733
13734 if (! low_set)
13735 /* If the first entry is an end-of-list marker, the range
13736 describes an empty scope, i.e. no instructions. */
13737 return 0;
13738
13739 if (low_return)
13740 *low_return = low;
13741 if (high_return)
13742 *high_return = high;
13743 return 1;
13744}
13745
3a2b436a
JK
13746/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13747 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13748 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13749
3a2b436a 13750static enum pc_bounds_kind
af34e669 13751dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13752 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13753 dwarf2_psymtab *pst)
c906108c 13754{
518817b3
SM
13755 struct dwarf2_per_objfile *dwarf2_per_objfile
13756 = cu->per_cu->dwarf2_per_objfile;
c906108c 13757 struct attribute *attr;
91da1414 13758 struct attribute *attr_high;
af34e669
DJ
13759 CORE_ADDR low = 0;
13760 CORE_ADDR high = 0;
e385593e 13761 enum pc_bounds_kind ret;
c906108c 13762
91da1414
MW
13763 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13764 if (attr_high)
af34e669 13765 {
e142c38c 13766 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13767 if (attr != nullptr)
91da1414 13768 {
cd6c91b4
TT
13769 low = attr->value_as_address ();
13770 high = attr_high->value_as_address ();
13771 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13772 high += low;
91da1414 13773 }
af34e669
DJ
13774 else
13775 /* Found high w/o low attribute. */
e385593e 13776 return PC_BOUNDS_INVALID;
af34e669
DJ
13777
13778 /* Found consecutive range of addresses. */
3a2b436a 13779 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13780 }
c906108c 13781 else
af34e669 13782 {
e142c38c 13783 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13784 if (attr != NULL)
13785 {
18a8505e 13786 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13787 We take advantage of the fact that DW_AT_ranges does not appear
13788 in DW_TAG_compile_unit of DWO files. */
13789 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13790 unsigned int ranges_offset = (DW_UNSND (attr)
13791 + (need_ranges_base
13792 ? cu->ranges_base
13793 : 0));
2e3cf129 13794
af34e669 13795 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13796 .debug_ranges section. */
2e3cf129 13797 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13798 return PC_BOUNDS_INVALID;
43039443 13799 /* Found discontinuous range of addresses. */
3a2b436a 13800 ret = PC_BOUNDS_RANGES;
af34e669 13801 }
e385593e
JK
13802 else
13803 return PC_BOUNDS_NOT_PRESENT;
af34e669 13804 }
c906108c 13805
48fbe735 13806 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13807 if (high <= low)
e385593e 13808 return PC_BOUNDS_INVALID;
c906108c
SS
13809
13810 /* When using the GNU linker, .gnu.linkonce. sections are used to
13811 eliminate duplicate copies of functions and vtables and such.
13812 The linker will arbitrarily choose one and discard the others.
13813 The AT_*_pc values for such functions refer to local labels in
13814 these sections. If the section from that file was discarded, the
13815 labels are not in the output, so the relocs get a value of 0.
13816 If this is a discarded function, mark the pc bounds as invalid,
13817 so that GDB will ignore it. */
72dca2f5 13818 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13819 return PC_BOUNDS_INVALID;
c906108c
SS
13820
13821 *lowpc = low;
96408a79
SA
13822 if (highpc)
13823 *highpc = high;
af34e669 13824 return ret;
c906108c
SS
13825}
13826
b084d499
JB
13827/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13828 its low and high PC addresses. Do nothing if these addresses could not
13829 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13830 and HIGHPC to the high address if greater than HIGHPC. */
13831
13832static void
13833dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13834 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13835 struct dwarf2_cu *cu)
13836{
13837 CORE_ADDR low, high;
13838 struct die_info *child = die->child;
13839
e385593e 13840 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13841 {
325fac50
PA
13842 *lowpc = std::min (*lowpc, low);
13843 *highpc = std::max (*highpc, high);
b084d499
JB
13844 }
13845
13846 /* If the language does not allow nested subprograms (either inside
13847 subprograms or lexical blocks), we're done. */
13848 if (cu->language != language_ada)
13849 return;
6e70227d 13850
b084d499
JB
13851 /* Check all the children of the given DIE. If it contains nested
13852 subprograms, then check their pc bounds. Likewise, we need to
13853 check lexical blocks as well, as they may also contain subprogram
13854 definitions. */
13855 while (child && child->tag)
13856 {
13857 if (child->tag == DW_TAG_subprogram
13858 || child->tag == DW_TAG_lexical_block)
13859 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 13860 child = child->sibling;
b084d499
JB
13861 }
13862}
13863
fae299cd
DC
13864/* Get the low and high pc's represented by the scope DIE, and store
13865 them in *LOWPC and *HIGHPC. If the correct values can't be
13866 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13867
13868static void
13869get_scope_pc_bounds (struct die_info *die,
13870 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13871 struct dwarf2_cu *cu)
13872{
13873 CORE_ADDR best_low = (CORE_ADDR) -1;
13874 CORE_ADDR best_high = (CORE_ADDR) 0;
13875 CORE_ADDR current_low, current_high;
13876
3a2b436a 13877 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13878 >= PC_BOUNDS_RANGES)
fae299cd
DC
13879 {
13880 best_low = current_low;
13881 best_high = current_high;
13882 }
13883 else
13884 {
13885 struct die_info *child = die->child;
13886
13887 while (child && child->tag)
13888 {
13889 switch (child->tag) {
13890 case DW_TAG_subprogram:
b084d499 13891 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13892 break;
13893 case DW_TAG_namespace:
f55ee35c 13894 case DW_TAG_module:
fae299cd
DC
13895 /* FIXME: carlton/2004-01-16: Should we do this for
13896 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13897 that current GCC's always emit the DIEs corresponding
13898 to definitions of methods of classes as children of a
13899 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13900 the DIEs giving the declarations, which could be
13901 anywhere). But I don't see any reason why the
13902 standards says that they have to be there. */
13903 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13904
13905 if (current_low != ((CORE_ADDR) -1))
13906 {
325fac50
PA
13907 best_low = std::min (best_low, current_low);
13908 best_high = std::max (best_high, current_high);
fae299cd
DC
13909 }
13910 break;
13911 default:
0963b4bd 13912 /* Ignore. */
fae299cd
DC
13913 break;
13914 }
13915
436c571c 13916 child = child->sibling;
fae299cd
DC
13917 }
13918 }
13919
13920 *lowpc = best_low;
13921 *highpc = best_high;
13922}
13923
801e3a5b
JB
13924/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13925 in DIE. */
380bca97 13926
801e3a5b
JB
13927static void
13928dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13929 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13930{
518817b3 13931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13932 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13933 struct attribute *attr;
91da1414 13934 struct attribute *attr_high;
801e3a5b 13935
91da1414
MW
13936 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13937 if (attr_high)
801e3a5b 13938 {
801e3a5b 13939 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13940 if (attr != nullptr)
801e3a5b 13941 {
cd6c91b4
TT
13942 CORE_ADDR low = attr->value_as_address ();
13943 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 13944
cd6c91b4 13945 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13946 high += low;
9a619af0 13947
3e29f34a
MR
13948 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13949 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 13950 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
13951 }
13952 }
13953
13954 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 13955 if (attr != nullptr)
801e3a5b 13956 {
18a8505e 13957 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13958 We take advantage of the fact that DW_AT_ranges does not appear
13959 in DW_TAG_compile_unit of DWO files. */
13960 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13961
13962 /* The value of the DW_AT_ranges attribute is the offset of the
13963 address range list in the .debug_ranges section. */
ab435259
DE
13964 unsigned long offset = (DW_UNSND (attr)
13965 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 13966
2d5f09ec 13967 std::vector<blockrange> blockvec;
5f46c5a5
JK
13968 dwarf2_ranges_process (offset, cu,
13969 [&] (CORE_ADDR start, CORE_ADDR end)
13970 {
58fdfd2c
JK
13971 start += baseaddr;
13972 end += baseaddr;
5f46c5a5
JK
13973 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13974 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 13975 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 13976 blockvec.emplace_back (start, end);
5f46c5a5 13977 });
2d5f09ec
KB
13978
13979 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
13980 }
13981}
13982
685b1105
JK
13983/* Check whether the producer field indicates either of GCC < 4.6, or the
13984 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 13985
685b1105
JK
13986static void
13987check_producer (struct dwarf2_cu *cu)
60d5a603 13988{
38360086 13989 int major, minor;
60d5a603
JK
13990
13991 if (cu->producer == NULL)
13992 {
13993 /* For unknown compilers expect their behavior is DWARF version
13994 compliant.
13995
13996 GCC started to support .debug_types sections by -gdwarf-4 since
13997 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13998 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13999 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14000 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14001 }
b1ffba5a 14002 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14003 {
38360086
MW
14004 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14005 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14006 }
5230b05a 14007 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14008 {
14009 cu->producer_is_icc = true;
14010 cu->producer_is_icc_lt_14 = major < 14;
14011 }
c258c396
JD
14012 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14013 cu->producer_is_codewarrior = true;
685b1105
JK
14014 else
14015 {
14016 /* For other non-GCC compilers, expect their behavior is DWARF version
14017 compliant. */
60d5a603
JK
14018 }
14019
9068261f 14020 cu->checked_producer = true;
685b1105 14021}
ba919b58 14022
685b1105
JK
14023/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14024 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14025 during 4.6.0 experimental. */
14026
9068261f 14027static bool
685b1105
JK
14028producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14029{
14030 if (!cu->checked_producer)
14031 check_producer (cu);
14032
14033 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14034}
14035
c258c396
JD
14036
14037/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14038 with incorrect is_stmt attributes. */
14039
14040static bool
14041producer_is_codewarrior (struct dwarf2_cu *cu)
14042{
14043 if (!cu->checked_producer)
14044 check_producer (cu);
14045
14046 return cu->producer_is_codewarrior;
14047}
14048
405feb71 14049/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14050 DW_AT_accessibility. */
14051
14052static enum dwarf_access_attribute
14053dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14054{
14055 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14056 {
14057 /* The default DWARF 2 accessibility for members is public, the default
14058 accessibility for inheritance is private. */
14059
14060 if (die->tag != DW_TAG_inheritance)
14061 return DW_ACCESS_public;
14062 else
14063 return DW_ACCESS_private;
14064 }
14065 else
14066 {
14067 /* DWARF 3+ defines the default accessibility a different way. The same
14068 rules apply now for DW_TAG_inheritance as for the members and it only
14069 depends on the container kind. */
14070
14071 if (die->parent->tag == DW_TAG_class_type)
14072 return DW_ACCESS_private;
14073 else
14074 return DW_ACCESS_public;
14075 }
14076}
14077
74ac6d43
TT
14078/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14079 offset. If the attribute was not found return 0, otherwise return
14080 1. If it was found but could not properly be handled, set *OFFSET
14081 to 0. */
14082
14083static int
14084handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14085 LONGEST *offset)
14086{
14087 struct attribute *attr;
14088
14089 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14090 if (attr != NULL)
14091 {
14092 *offset = 0;
14093
14094 /* Note that we do not check for a section offset first here.
14095 This is because DW_AT_data_member_location is new in DWARF 4,
14096 so if we see it, we can assume that a constant form is really
14097 a constant and not a section offset. */
cd6c91b4 14098 if (attr->form_is_constant ())
74ac6d43 14099 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14100 else if (attr->form_is_section_offset ())
74ac6d43 14101 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14102 else if (attr->form_is_block ())
74ac6d43
TT
14103 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14104 else
14105 dwarf2_complex_location_expr_complaint ();
14106
14107 return 1;
14108 }
14109
14110 return 0;
14111}
14112
c906108c
SS
14113/* Add an aggregate field to the field list. */
14114
14115static void
107d2387 14116dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14117 struct dwarf2_cu *cu)
6e70227d 14118{
518817b3 14119 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14120 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14121 struct nextfield *new_field;
14122 struct attribute *attr;
14123 struct field *fp;
15d034d0 14124 const char *fieldname = "";
c906108c 14125
7d0ccb61
DJ
14126 if (die->tag == DW_TAG_inheritance)
14127 {
be2daae6
TT
14128 fip->baseclasses.emplace_back ();
14129 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14130 }
14131 else
14132 {
be2daae6
TT
14133 fip->fields.emplace_back ();
14134 new_field = &fip->fields.back ();
7d0ccb61 14135 }
be2daae6 14136
e142c38c 14137 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14138 if (attr != nullptr)
c906108c 14139 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14140 else
14141 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14142 if (new_field->accessibility != DW_ACCESS_public)
14143 fip->non_public_fields = 1;
60d5a603 14144
e142c38c 14145 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14146 if (attr != nullptr)
c906108c 14147 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14148 else
14149 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14150
14151 fp = &new_field->field;
a9a9bd0f 14152
e142c38c 14153 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14154 {
74ac6d43
TT
14155 LONGEST offset;
14156
a9a9bd0f 14157 /* Data member other than a C++ static data member. */
6e70227d 14158
c906108c 14159 /* Get type of field. */
e7c27a73 14160 fp->type = die_type (die, cu);
c906108c 14161
d6a843b5 14162 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14163
c906108c 14164 /* Get bit size of field (zero if none). */
e142c38c 14165 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14166 if (attr != nullptr)
c906108c
SS
14167 {
14168 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14169 }
14170 else
14171 {
14172 FIELD_BITSIZE (*fp) = 0;
14173 }
14174
14175 /* Get bit offset of field. */
74ac6d43
TT
14176 if (handle_data_member_location (die, cu, &offset))
14177 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14178 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14179 if (attr != nullptr)
c906108c 14180 {
d5a22e77 14181 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14182 {
14183 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14184 additional bit offset from the MSB of the containing
14185 anonymous object to the MSB of the field. We don't
14186 have to do anything special since we don't need to
14187 know the size of the anonymous object. */
f41f5e61 14188 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14189 }
14190 else
14191 {
14192 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14193 MSB of the anonymous object, subtract off the number of
14194 bits from the MSB of the field to the MSB of the
14195 object, and then subtract off the number of bits of
14196 the field itself. The result is the bit offset of
14197 the LSB of the field. */
c906108c
SS
14198 int anonymous_size;
14199 int bit_offset = DW_UNSND (attr);
14200
e142c38c 14201 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14202 if (attr != nullptr)
c906108c
SS
14203 {
14204 /* The size of the anonymous object containing
14205 the bit field is explicit, so use the
14206 indicated size (in bytes). */
14207 anonymous_size = DW_UNSND (attr);
14208 }
14209 else
14210 {
14211 /* The size of the anonymous object containing
14212 the bit field must be inferred from the type
14213 attribute of the data member containing the
14214 bit field. */
14215 anonymous_size = TYPE_LENGTH (fp->type);
14216 }
f41f5e61
PA
14217 SET_FIELD_BITPOS (*fp,
14218 (FIELD_BITPOS (*fp)
14219 + anonymous_size * bits_per_byte
14220 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14221 }
14222 }
da5b30da
AA
14223 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14224 if (attr != NULL)
14225 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14226 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14227
14228 /* Get name of field. */
39cbfefa
DJ
14229 fieldname = dwarf2_name (die, cu);
14230 if (fieldname == NULL)
14231 fieldname = "";
d8151005
DJ
14232
14233 /* The name is already allocated along with this objfile, so we don't
14234 need to duplicate it for the type. */
14235 fp->name = fieldname;
c906108c
SS
14236
14237 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14238 pointer or virtual base class pointer) to private. */
e142c38c 14239 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14240 {
d48cc9dd 14241 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14242 new_field->accessibility = DW_ACCESS_private;
14243 fip->non_public_fields = 1;
14244 }
14245 }
a9a9bd0f 14246 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14247 {
a9a9bd0f
DC
14248 /* C++ static member. */
14249
14250 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14251 is a declaration, but all versions of G++ as of this writing
14252 (so through at least 3.2.1) incorrectly generate
14253 DW_TAG_variable tags. */
6e70227d 14254
ff355380 14255 const char *physname;
c906108c 14256
a9a9bd0f 14257 /* Get name of field. */
39cbfefa
DJ
14258 fieldname = dwarf2_name (die, cu);
14259 if (fieldname == NULL)
c906108c
SS
14260 return;
14261
254e6b9e 14262 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14263 if (attr
14264 /* Only create a symbol if this is an external value.
14265 new_symbol checks this and puts the value in the global symbol
14266 table, which we want. If it is not external, new_symbol
14267 will try to put the value in cu->list_in_scope which is wrong. */
14268 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14269 {
14270 /* A static const member, not much different than an enum as far as
14271 we're concerned, except that we can support more types. */
14272 new_symbol (die, NULL, cu);
14273 }
14274
2df3850c 14275 /* Get physical name. */
ff355380 14276 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14277
d8151005
DJ
14278 /* The name is already allocated along with this objfile, so we don't
14279 need to duplicate it for the type. */
14280 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14281 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14282 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14283 }
14284 else if (die->tag == DW_TAG_inheritance)
14285 {
74ac6d43 14286 LONGEST offset;
d4b96c9a 14287
74ac6d43
TT
14288 /* C++ base class field. */
14289 if (handle_data_member_location (die, cu, &offset))
14290 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14291 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14292 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14293 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14294 }
2ddeaf8a
TT
14295 else if (die->tag == DW_TAG_variant_part)
14296 {
14297 /* process_structure_scope will treat this DIE as a union. */
14298 process_structure_scope (die, cu);
14299
14300 /* The variant part is relative to the start of the enclosing
14301 structure. */
14302 SET_FIELD_BITPOS (*fp, 0);
14303 fp->type = get_die_type (die, cu);
14304 fp->artificial = 1;
14305 fp->name = "<<variant>>";
c8c81635
TT
14306
14307 /* Normally a DW_TAG_variant_part won't have a size, but our
14308 representation requires one, so set it to the maximum of the
489dbda6
TT
14309 child sizes, being sure to account for the offset at which
14310 each child is seen. */
c8c81635
TT
14311 if (TYPE_LENGTH (fp->type) == 0)
14312 {
14313 unsigned max = 0;
14314 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14315 {
14316 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14317 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14318 if (len > max)
14319 max = len;
14320 }
c8c81635
TT
14321 TYPE_LENGTH (fp->type) = max;
14322 }
2ddeaf8a
TT
14323 }
14324 else
14325 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14326}
14327
883fd55a
KS
14328/* Can the type given by DIE define another type? */
14329
14330static bool
14331type_can_define_types (const struct die_info *die)
14332{
14333 switch (die->tag)
14334 {
14335 case DW_TAG_typedef:
14336 case DW_TAG_class_type:
14337 case DW_TAG_structure_type:
14338 case DW_TAG_union_type:
14339 case DW_TAG_enumeration_type:
14340 return true;
14341
14342 default:
14343 return false;
14344 }
14345}
14346
14347/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14348
14349static void
883fd55a
KS
14350dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14351 struct dwarf2_cu *cu)
6e70227d 14352{
be2daae6
TT
14353 struct decl_field fp;
14354 memset (&fp, 0, sizeof (fp));
98751a41 14355
883fd55a 14356 gdb_assert (type_can_define_types (die));
98751a41 14357
883fd55a 14358 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14359 fp.name = dwarf2_name (die, cu);
14360 fp.type = read_type_die (die, cu);
98751a41 14361
c191a687
KS
14362 /* Save accessibility. */
14363 enum dwarf_access_attribute accessibility;
14364 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14365 if (attr != NULL)
14366 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14367 else
14368 accessibility = dwarf2_default_access_attribute (die, cu);
14369 switch (accessibility)
14370 {
14371 case DW_ACCESS_public:
14372 /* The assumed value if neither private nor protected. */
14373 break;
14374 case DW_ACCESS_private:
be2daae6 14375 fp.is_private = 1;
c191a687
KS
14376 break;
14377 case DW_ACCESS_protected:
be2daae6 14378 fp.is_protected = 1;
c191a687
KS
14379 break;
14380 default:
b98664d3 14381 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14382 }
14383
883fd55a 14384 if (die->tag == DW_TAG_typedef)
be2daae6 14385 fip->typedef_field_list.push_back (fp);
883fd55a 14386 else
be2daae6 14387 fip->nested_types_list.push_back (fp);
98751a41
JK
14388}
14389
c906108c
SS
14390/* Create the vector of fields, and attach it to the type. */
14391
14392static void
fba45db2 14393dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14394 struct dwarf2_cu *cu)
c906108c 14395{
317f7127 14396 int nfields = fip->nfields ();
c906108c
SS
14397
14398 /* Record the field count, allocate space for the array of fields,
14399 and create blank accessibility bitfields if necessary. */
14400 TYPE_NFIELDS (type) = nfields;
14401 TYPE_FIELDS (type) = (struct field *)
be2daae6 14402 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14403
b4ba55a1 14404 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14405 {
14406 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14407
14408 TYPE_FIELD_PRIVATE_BITS (type) =
14409 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14410 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14411
14412 TYPE_FIELD_PROTECTED_BITS (type) =
14413 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14414 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14415
774b6a14
TT
14416 TYPE_FIELD_IGNORE_BITS (type) =
14417 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14418 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14419 }
14420
14421 /* If the type has baseclasses, allocate and clear a bit vector for
14422 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14423 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14424 {
be2daae6 14425 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14426 unsigned char *pointer;
c906108c
SS
14427
14428 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14429 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14430 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14431 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14432 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14433 }
14434
2ddeaf8a
TT
14435 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14436 {
14437 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14438
be2daae6 14439 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14440 {
be2daae6
TT
14441 struct nextfield &field = fip->fields[index];
14442
14443 if (field.variant.is_discriminant)
2ddeaf8a 14444 di->discriminant_index = index;
be2daae6 14445 else if (field.variant.default_branch)
2ddeaf8a
TT
14446 di->default_index = index;
14447 else
be2daae6 14448 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14449 }
14450 }
14451
be2daae6
TT
14452 /* Copy the saved-up fields into the field vector. */
14453 for (int i = 0; i < nfields; ++i)
c906108c 14454 {
be2daae6
TT
14455 struct nextfield &field
14456 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14457 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14458
be2daae6
TT
14459 TYPE_FIELD (type, i) = field.field;
14460 switch (field.accessibility)
c906108c 14461 {
c5aa993b 14462 case DW_ACCESS_private:
b4ba55a1 14463 if (cu->language != language_ada)
be2daae6 14464 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14465 break;
c906108c 14466
c5aa993b 14467 case DW_ACCESS_protected:
b4ba55a1 14468 if (cu->language != language_ada)
be2daae6 14469 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14470 break;
c906108c 14471
c5aa993b
JM
14472 case DW_ACCESS_public:
14473 break;
c906108c 14474
c5aa993b
JM
14475 default:
14476 /* Unknown accessibility. Complain and treat it as public. */
14477 {
b98664d3 14478 complaint (_("unsupported accessibility %d"),
be2daae6 14479 field.accessibility);
c5aa993b
JM
14480 }
14481 break;
c906108c 14482 }
be2daae6 14483 if (i < fip->baseclasses.size ())
c906108c 14484 {
be2daae6 14485 switch (field.virtuality)
c906108c 14486 {
c5aa993b
JM
14487 case DW_VIRTUALITY_virtual:
14488 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14489 if (cu->language == language_ada)
a73c6dcd 14490 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14491 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14492 break;
c906108c
SS
14493 }
14494 }
c906108c
SS
14495 }
14496}
14497
7d27a96d
TT
14498/* Return true if this member function is a constructor, false
14499 otherwise. */
14500
14501static int
14502dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14503{
14504 const char *fieldname;
fe978cb0 14505 const char *type_name;
7d27a96d
TT
14506 int len;
14507
14508 if (die->parent == NULL)
14509 return 0;
14510
14511 if (die->parent->tag != DW_TAG_structure_type
14512 && die->parent->tag != DW_TAG_union_type
14513 && die->parent->tag != DW_TAG_class_type)
14514 return 0;
14515
14516 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14517 type_name = dwarf2_name (die->parent, cu);
14518 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14519 return 0;
14520
14521 len = strlen (fieldname);
fe978cb0
PA
14522 return (strncmp (fieldname, type_name, len) == 0
14523 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14524}
14525
e35000a7
TBA
14526/* Check if the given VALUE is a recognized enum
14527 dwarf_defaulted_attribute constant according to DWARF5 spec,
14528 Table 7.24. */
14529
14530static bool
14531is_valid_DW_AT_defaulted (ULONGEST value)
14532{
14533 switch (value)
14534 {
14535 case DW_DEFAULTED_no:
14536 case DW_DEFAULTED_in_class:
14537 case DW_DEFAULTED_out_of_class:
14538 return true;
14539 }
14540
3142e908 14541 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14542 return false;
14543}
14544
c906108c
SS
14545/* Add a member function to the proper fieldlist. */
14546
14547static void
107d2387 14548dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14549 struct type *type, struct dwarf2_cu *cu)
c906108c 14550{
518817b3 14551 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14552 struct attribute *attr;
c906108c 14553 int i;
be2daae6 14554 struct fnfieldlist *flp = nullptr;
c906108c 14555 struct fn_field *fnp;
15d034d0 14556 const char *fieldname;
f792889a 14557 struct type *this_type;
60d5a603 14558 enum dwarf_access_attribute accessibility;
c906108c 14559
b4ba55a1 14560 if (cu->language == language_ada)
a73c6dcd 14561 error (_("unexpected member function in Ada type"));
b4ba55a1 14562
2df3850c 14563 /* Get name of member function. */
39cbfefa
DJ
14564 fieldname = dwarf2_name (die, cu);
14565 if (fieldname == NULL)
2df3850c 14566 return;
c906108c 14567
c906108c 14568 /* Look up member function name in fieldlist. */
be2daae6 14569 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14570 {
27bfe10e 14571 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14572 {
14573 flp = &fip->fnfieldlists[i];
14574 break;
14575 }
c906108c
SS
14576 }
14577
be2daae6
TT
14578 /* Create a new fnfieldlist if necessary. */
14579 if (flp == nullptr)
c906108c 14580 {
be2daae6
TT
14581 fip->fnfieldlists.emplace_back ();
14582 flp = &fip->fnfieldlists.back ();
c906108c 14583 flp->name = fieldname;
be2daae6 14584 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14585 }
14586
be2daae6
TT
14587 /* Create a new member function field and add it to the vector of
14588 fnfieldlists. */
14589 flp->fnfields.emplace_back ();
14590 fnp = &flp->fnfields.back ();
3da10d80
KS
14591
14592 /* Delay processing of the physname until later. */
9c37b5ae 14593 if (cu->language == language_cplus)
be2daae6
TT
14594 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14595 die, cu);
3da10d80
KS
14596 else
14597 {
1d06ead6 14598 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14599 fnp->physname = physname ? physname : "";
14600 }
14601
c906108c 14602 fnp->type = alloc_type (objfile);
f792889a
DJ
14603 this_type = read_type_die (die, cu);
14604 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14605 {
f792889a 14606 int nparams = TYPE_NFIELDS (this_type);
c906108c 14607
f792889a 14608 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14609 of the method itself (TYPE_CODE_METHOD). */
14610 smash_to_method_type (fnp->type, type,
f792889a
DJ
14611 TYPE_TARGET_TYPE (this_type),
14612 TYPE_FIELDS (this_type),
14613 TYPE_NFIELDS (this_type),
14614 TYPE_VARARGS (this_type));
c906108c
SS
14615
14616 /* Handle static member functions.
c5aa993b 14617 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14618 member functions. G++ helps GDB by marking the first
14619 parameter for non-static member functions (which is the this
14620 pointer) as artificial. We obtain this information from
14621 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14622 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14623 fnp->voffset = VOFFSET_STATIC;
14624 }
14625 else
b98664d3 14626 complaint (_("member function type missing for '%s'"),
3da10d80 14627 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14628
14629 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14630 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14631 fnp->fcontext = die_containing_type (die, cu);
c906108c 14632
3e43a32a
MS
14633 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14634 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14635
14636 /* Get accessibility. */
e142c38c 14637 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14638 if (attr != nullptr)
aead7601 14639 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14640 else
14641 accessibility = dwarf2_default_access_attribute (die, cu);
14642 switch (accessibility)
c906108c 14643 {
60d5a603
JK
14644 case DW_ACCESS_private:
14645 fnp->is_private = 1;
14646 break;
14647 case DW_ACCESS_protected:
14648 fnp->is_protected = 1;
14649 break;
c906108c
SS
14650 }
14651
b02dede2 14652 /* Check for artificial methods. */
e142c38c 14653 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14654 if (attr && DW_UNSND (attr) != 0)
14655 fnp->is_artificial = 1;
14656
e35000a7
TBA
14657 /* Check for defaulted methods. */
14658 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14659 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14660 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14661
14662 /* Check for deleted methods. */
14663 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14664 if (attr != nullptr && DW_UNSND (attr) != 0)
14665 fnp->is_deleted = 1;
14666
7d27a96d
TT
14667 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14668
0d564a31 14669 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14670 function. For older versions of GCC, this is an offset in the
14671 appropriate virtual table, as specified by DW_AT_containing_type.
14672 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14673 to the object address. */
14674
e142c38c 14675 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14676 if (attr != nullptr)
8e19ed76 14677 {
4fc6c0d5 14678 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14679 {
aec5aa8b
TT
14680 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14681 {
14682 /* Old-style GCC. */
14683 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14684 }
14685 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14686 || (DW_BLOCK (attr)->size > 1
14687 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14688 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14689 {
aec5aa8b
TT
14690 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14691 if ((fnp->voffset % cu->header.addr_size) != 0)
14692 dwarf2_complex_location_expr_complaint ();
14693 else
14694 fnp->voffset /= cu->header.addr_size;
14695 fnp->voffset += 2;
14696 }
14697 else
14698 dwarf2_complex_location_expr_complaint ();
14699
14700 if (!fnp->fcontext)
7e993ebf
KS
14701 {
14702 /* If there is no `this' field and no DW_AT_containing_type,
14703 we cannot actually find a base class context for the
14704 vtable! */
14705 if (TYPE_NFIELDS (this_type) == 0
14706 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14707 {
b98664d3 14708 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14709 "function \"%s\" (offset %s)"),
14710 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14711 }
14712 else
14713 {
14714 fnp->fcontext
14715 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14716 }
14717 }
aec5aa8b 14718 }
cd6c91b4 14719 else if (attr->form_is_section_offset ())
8e19ed76 14720 {
4d3c2250 14721 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14722 }
14723 else
14724 {
4d3c2250
KB
14725 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14726 fieldname);
8e19ed76 14727 }
0d564a31 14728 }
d48cc9dd
DJ
14729 else
14730 {
14731 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14732 if (attr && DW_UNSND (attr))
14733 {
14734 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14735 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14736 "but the vtable offset is not specified"),
9d8780f0 14737 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14738 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14739 TYPE_CPLUS_DYNAMIC (type) = 1;
14740 }
14741 }
c906108c
SS
14742}
14743
14744/* Create the vector of member function fields, and attach it to the type. */
14745
14746static void
fba45db2 14747dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14748 struct dwarf2_cu *cu)
c906108c 14749{
b4ba55a1 14750 if (cu->language == language_ada)
a73c6dcd 14751 error (_("unexpected member functions in Ada type"));
b4ba55a1 14752
c906108c
SS
14753 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14754 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14755 TYPE_ALLOC (type,
14756 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14757
be2daae6 14758 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14759 {
be2daae6 14760 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14761 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14762
be2daae6
TT
14763 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14764 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14765 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14766 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14767
14768 for (int k = 0; k < nf.fnfields.size (); ++k)
14769 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14770 }
14771
be2daae6 14772 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14773}
14774
1168df01
JB
14775/* Returns non-zero if NAME is the name of a vtable member in CU's
14776 language, zero otherwise. */
14777static int
14778is_vtable_name (const char *name, struct dwarf2_cu *cu)
14779{
14780 static const char vptr[] = "_vptr";
14781
9c37b5ae
TT
14782 /* Look for the C++ form of the vtable. */
14783 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14784 return 1;
14785
14786 return 0;
14787}
14788
c0dd20ea 14789/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14790 functions, with the ABI-specified layout. If TYPE describes
14791 such a structure, smash it into a member function type.
61049d3b
DJ
14792
14793 GCC shouldn't do this; it should just output pointer to member DIEs.
14794 This is GCC PR debug/28767. */
c0dd20ea 14795
0b92b5bb
TT
14796static void
14797quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14798{
09e2d7c7 14799 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14800
14801 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14802 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14803 return;
c0dd20ea
DJ
14804
14805 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14806 if (TYPE_FIELD_NAME (type, 0) == NULL
14807 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14808 || TYPE_FIELD_NAME (type, 1) == NULL
14809 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14810 return;
c0dd20ea
DJ
14811
14812 /* Find the type of the method. */
0b92b5bb 14813 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14814 if (pfn_type == NULL
14815 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14816 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14817 return;
c0dd20ea
DJ
14818
14819 /* Look for the "this" argument. */
14820 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14821 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14822 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14823 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14824 return;
c0dd20ea 14825
09e2d7c7 14826 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14827 new_type = alloc_type (objfile);
09e2d7c7 14828 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14829 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14830 TYPE_VARARGS (pfn_type));
0b92b5bb 14831 smash_to_methodptr_type (type, new_type);
c0dd20ea 14832}
1168df01 14833
2b4424c3
TT
14834/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14835 appropriate error checking and issuing complaints if there is a
14836 problem. */
14837
14838static ULONGEST
14839get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14840{
14841 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14842
14843 if (attr == nullptr)
14844 return 0;
14845
cd6c91b4 14846 if (!attr->form_is_constant ())
2b4424c3 14847 {
b98664d3 14848 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14849 " - DIE at %s [in module %s]"),
14850 sect_offset_str (die->sect_off),
14851 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14852 return 0;
14853 }
14854
14855 ULONGEST align;
14856 if (attr->form == DW_FORM_sdata)
14857 {
14858 LONGEST val = DW_SND (attr);
14859 if (val < 0)
14860 {
b98664d3 14861 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14862 " - DIE at %s [in module %s]"),
14863 sect_offset_str (die->sect_off),
14864 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14865 return 0;
14866 }
14867 align = val;
14868 }
14869 else
14870 align = DW_UNSND (attr);
14871
14872 if (align == 0)
14873 {
b98664d3 14874 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14875 " - DIE at %s [in module %s]"),
14876 sect_offset_str (die->sect_off),
14877 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14878 return 0;
14879 }
14880 if ((align & (align - 1)) != 0)
14881 {
b98664d3 14882 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14883 " - DIE at %s [in module %s]"),
14884 sect_offset_str (die->sect_off),
14885 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14886 return 0;
14887 }
14888
14889 return align;
14890}
14891
14892/* If the DIE has a DW_AT_alignment attribute, use its value to set
14893 the alignment for TYPE. */
14894
14895static void
14896maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14897 struct type *type)
14898{
14899 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14900 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14901 " - DIE at %s [in module %s]"),
14902 sect_offset_str (die->sect_off),
14903 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14904}
685b1105 14905
e35000a7
TBA
14906/* Check if the given VALUE is a valid enum dwarf_calling_convention
14907 constant for a type, according to DWARF5 spec, Table 5.5. */
14908
14909static bool
14910is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14911{
14912 switch (value)
14913 {
14914 case DW_CC_normal:
14915 case DW_CC_pass_by_reference:
14916 case DW_CC_pass_by_value:
14917 return true;
14918
14919 default:
14920 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14921 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14922 return false;
14923 }
14924}
14925
d0922fcf
TBA
14926/* Check if the given VALUE is a valid enum dwarf_calling_convention
14927 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14928 also according to GNU-specific values (see include/dwarf2.h). */
14929
14930static bool
14931is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14932{
14933 switch (value)
14934 {
14935 case DW_CC_normal:
14936 case DW_CC_program:
14937 case DW_CC_nocall:
14938 return true;
14939
14940 case DW_CC_GNU_renesas_sh:
14941 case DW_CC_GNU_borland_fastcall_i386:
14942 case DW_CC_GDB_IBM_OpenCL:
14943 return true;
14944
14945 default:
14946 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14947 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
14948 return false;
14949 }
14950}
14951
c906108c 14952/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14953 (definition) to create a type for the structure or union. Fill in
14954 the type's name and general properties; the members will not be
83655187
DE
14955 processed until process_structure_scope. A symbol table entry for
14956 the type will also not be done until process_structure_scope (assuming
14957 the type has a name).
c906108c 14958
c767944b
DJ
14959 NOTE: we need to call these functions regardless of whether or not the
14960 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 14961 structure or union. This gets the type entered into our set of
83655187 14962 user defined types. */
c906108c 14963
f792889a 14964static struct type *
134d01f1 14965read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14966{
518817b3 14967 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
14968 struct type *type;
14969 struct attribute *attr;
15d034d0 14970 const char *name;
c906108c 14971
348e048f
DE
14972 /* If the definition of this type lives in .debug_types, read that type.
14973 Don't follow DW_AT_specification though, that will take us back up
14974 the chain and we want to go down. */
052c8bb8 14975 attr = die->attr (DW_AT_signature);
435d3d88 14976 if (attr != nullptr)
348e048f 14977 {
ac9ec31b 14978 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14979
ac9ec31b 14980 /* The type's CU may not be the same as CU.
02142a6c 14981 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14982 return set_die_type (die, type, cu);
14983 }
14984
c0dd20ea 14985 type = alloc_type (objfile);
c906108c 14986 INIT_CPLUS_SPECIFIC (type);
93311388 14987
39cbfefa
DJ
14988 name = dwarf2_name (die, cu);
14989 if (name != NULL)
c906108c 14990 {
987504bb 14991 if (cu->language == language_cplus
c44af4eb
TT
14992 || cu->language == language_d
14993 || cu->language == language_rust)
63d06c5c 14994 {
15d034d0 14995 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
14996
14997 /* dwarf2_full_name might have already finished building the DIE's
14998 type. If so, there is no need to continue. */
14999 if (get_die_type (die, cu) != NULL)
15000 return get_die_type (die, cu);
15001
e86ca25f 15002 TYPE_NAME (type) = full_name;
63d06c5c
DC
15003 }
15004 else
15005 {
d8151005
DJ
15006 /* The name is already allocated along with this objfile, so
15007 we don't need to duplicate it for the type. */
e86ca25f 15008 TYPE_NAME (type) = name;
63d06c5c 15009 }
c906108c
SS
15010 }
15011
15012 if (die->tag == DW_TAG_structure_type)
15013 {
15014 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15015 }
15016 else if (die->tag == DW_TAG_union_type)
15017 {
15018 TYPE_CODE (type) = TYPE_CODE_UNION;
15019 }
2ddeaf8a
TT
15020 else if (die->tag == DW_TAG_variant_part)
15021 {
15022 TYPE_CODE (type) = TYPE_CODE_UNION;
15023 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15024 }
c906108c
SS
15025 else
15026 {
4753d33b 15027 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15028 }
15029
0cc2414c
TT
15030 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15031 TYPE_DECLARED_CLASS (type) = 1;
15032
e35000a7
TBA
15033 /* Store the calling convention in the type if it's available in
15034 the die. Otherwise the calling convention remains set to
15035 the default value DW_CC_normal. */
15036 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15037 if (attr != nullptr
15038 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15039 {
15040 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15041 TYPE_CPLUS_CALLING_CONVENTION (type)
15042 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15043 }
15044
e142c38c 15045 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15046 if (attr != nullptr)
c906108c 15047 {
cd6c91b4 15048 if (attr->form_is_constant ())
155bfbd3
JB
15049 TYPE_LENGTH (type) = DW_UNSND (attr);
15050 else
15051 {
15052 /* For the moment, dynamic type sizes are not supported
15053 by GDB's struct type. The actual size is determined
15054 on-demand when resolving the type of a given object,
15055 so set the type's length to zero for now. Otherwise,
15056 we record an expression as the length, and that expression
15057 could lead to a very large value, which could eventually
15058 lead to us trying to allocate that much memory when creating
15059 a value of that type. */
15060 TYPE_LENGTH (type) = 0;
15061 }
c906108c
SS
15062 }
15063 else
15064 {
15065 TYPE_LENGTH (type) = 0;
15066 }
15067
2b4424c3
TT
15068 maybe_set_alignment (cu, die, type);
15069
5230b05a 15070 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15071 {
5230b05a
WT
15072 /* ICC<14 does not output the required DW_AT_declaration on
15073 incomplete types, but gives them a size of zero. */
422b1cb0 15074 TYPE_STUB (type) = 1;
685b1105
JK
15075 }
15076 else
15077 TYPE_STUB_SUPPORTED (type) = 1;
15078
dc718098 15079 if (die_is_declaration (die, cu))
876cecd0 15080 TYPE_STUB (type) = 1;
a6c727b2
DJ
15081 else if (attr == NULL && die->child == NULL
15082 && producer_is_realview (cu->producer))
15083 /* RealView does not output the required DW_AT_declaration
15084 on incomplete types. */
15085 TYPE_STUB (type) = 1;
dc718098 15086
c906108c
SS
15087 /* We need to add the type field to the die immediately so we don't
15088 infinitely recurse when dealing with pointers to the structure
0963b4bd 15089 type within the structure itself. */
1c379e20 15090 set_die_type (die, type, cu);
c906108c 15091
7e314c57
JK
15092 /* set_die_type should be already done. */
15093 set_descriptive_type (type, die, cu);
15094
c767944b
DJ
15095 return type;
15096}
15097
2ddeaf8a
TT
15098/* A helper for process_structure_scope that handles a single member
15099 DIE. */
15100
15101static void
15102handle_struct_member_die (struct die_info *child_die, struct type *type,
15103 struct field_info *fi,
15104 std::vector<struct symbol *> *template_args,
15105 struct dwarf2_cu *cu)
15106{
15107 if (child_die->tag == DW_TAG_member
15108 || child_die->tag == DW_TAG_variable
15109 || child_die->tag == DW_TAG_variant_part)
15110 {
15111 /* NOTE: carlton/2002-11-05: A C++ static data member
15112 should be a DW_TAG_member that is a declaration, but
15113 all versions of G++ as of this writing (so through at
15114 least 3.2.1) incorrectly generate DW_TAG_variable
15115 tags for them instead. */
15116 dwarf2_add_field (fi, child_die, cu);
15117 }
15118 else if (child_die->tag == DW_TAG_subprogram)
15119 {
15120 /* Rust doesn't have member functions in the C++ sense.
15121 However, it does emit ordinary functions as children
15122 of a struct DIE. */
15123 if (cu->language == language_rust)
15124 read_func_scope (child_die, cu);
15125 else
15126 {
15127 /* C++ member function. */
15128 dwarf2_add_member_fn (fi, child_die, type, cu);
15129 }
15130 }
15131 else if (child_die->tag == DW_TAG_inheritance)
15132 {
15133 /* C++ base class field. */
15134 dwarf2_add_field (fi, child_die, cu);
15135 }
15136 else if (type_can_define_types (child_die))
15137 dwarf2_add_type_defn (fi, child_die, cu);
15138 else if (child_die->tag == DW_TAG_template_type_param
15139 || child_die->tag == DW_TAG_template_value_param)
15140 {
15141 struct symbol *arg = new_symbol (child_die, NULL, cu);
15142
15143 if (arg != NULL)
15144 template_args->push_back (arg);
15145 }
15146 else if (child_die->tag == DW_TAG_variant)
15147 {
15148 /* In a variant we want to get the discriminant and also add a
15149 field for our sole member child. */
15150 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15151
bde09ab7 15152 for (die_info *variant_child = child_die->child;
2ddeaf8a 15153 variant_child != NULL;
436c571c 15154 variant_child = variant_child->sibling)
2ddeaf8a
TT
15155 {
15156 if (variant_child->tag == DW_TAG_member)
15157 {
15158 handle_struct_member_die (variant_child, type, fi,
15159 template_args, cu);
15160 /* Only handle the one. */
15161 break;
15162 }
15163 }
15164
15165 /* We don't handle this but we might as well report it if we see
15166 it. */
15167 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15168 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15169 " - DIE at %s [in module %s]"),
15170 sect_offset_str (child_die->sect_off),
15171 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15172
15173 /* The first field was just added, so we can stash the
15174 discriminant there. */
be2daae6 15175 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15176 if (discr == NULL)
be2daae6 15177 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15178 else
be2daae6 15179 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15180 }
15181}
15182
c767944b
DJ
15183/* Finish creating a structure or union type, including filling in
15184 its members and creating a symbol for it. */
15185
15186static void
15187process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15188{
518817b3 15189 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15190 struct die_info *child_die;
c767944b
DJ
15191 struct type *type;
15192
15193 type = get_die_type (die, cu);
15194 if (type == NULL)
15195 type = read_structure_type (die, cu);
15196
2ddeaf8a
TT
15197 /* When reading a DW_TAG_variant_part, we need to notice when we
15198 read the discriminant member, so we can record it later in the
15199 discriminant_info. */
15200 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15201 sect_offset discr_offset {};
3e1d3d8c 15202 bool has_template_parameters = false;
2ddeaf8a
TT
15203
15204 if (is_variant_part)
15205 {
15206 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15207 if (discr == NULL)
15208 {
15209 /* Maybe it's a univariant form, an extension we support.
15210 In this case arrange not to check the offset. */
15211 is_variant_part = false;
15212 }
cd6c91b4 15213 else if (discr->form_is_ref ())
2ddeaf8a
TT
15214 {
15215 struct dwarf2_cu *target_cu = cu;
15216 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15217
15218 discr_offset = target_die->sect_off;
15219 }
15220 else
15221 {
b98664d3 15222 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15223 " - DIE at %s [in module %s]"),
15224 sect_offset_str (die->sect_off),
15225 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15226 is_variant_part = false;
15227 }
15228 }
15229
e142c38c 15230 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15231 {
15232 struct field_info fi;
2f4732b0 15233 std::vector<struct symbol *> template_args;
c906108c 15234
639d11d3 15235 child_die = die->child;
c906108c
SS
15236
15237 while (child_die && child_die->tag)
15238 {
2ddeaf8a 15239 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15240
2ddeaf8a 15241 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15242 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15243
436c571c 15244 child_die = child_die->sibling;
c906108c
SS
15245 }
15246
34eaf542 15247 /* Attach template arguments to type. */
2f4732b0 15248 if (!template_args.empty ())
34eaf542 15249 {
3e1d3d8c 15250 has_template_parameters = true;
34eaf542 15251 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15252 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15253 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15254 = XOBNEWVEC (&objfile->objfile_obstack,
15255 struct symbol *,
15256 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15257 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15258 template_args.data (),
34eaf542
TT
15259 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15260 * sizeof (struct symbol *)));
34eaf542
TT
15261 }
15262
c906108c 15263 /* Attach fields and member functions to the type. */
317f7127 15264 if (fi.nfields () > 0)
e7c27a73 15265 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15266 if (!fi.fnfieldlists.empty ())
c906108c 15267 {
e7c27a73 15268 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15269
c5aa993b 15270 /* Get the type which refers to the base class (possibly this
c906108c 15271 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15272 class from the DW_AT_containing_type attribute. This use of
15273 DW_AT_containing_type is a GNU extension. */
c906108c 15274
e142c38c 15275 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15276 {
e7c27a73 15277 struct type *t = die_containing_type (die, cu);
c906108c 15278
ae6ae975 15279 set_type_vptr_basetype (type, t);
c906108c
SS
15280 if (type == t)
15281 {
c906108c
SS
15282 int i;
15283
15284 /* Our own class provides vtbl ptr. */
15285 for (i = TYPE_NFIELDS (t) - 1;
15286 i >= TYPE_N_BASECLASSES (t);
15287 --i)
15288 {
0d5cff50 15289 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15290
1168df01 15291 if (is_vtable_name (fieldname, cu))
c906108c 15292 {
ae6ae975 15293 set_type_vptr_fieldno (type, i);
c906108c
SS
15294 break;
15295 }
15296 }
15297
15298 /* Complain if virtual function table field not found. */
15299 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15300 complaint (_("virtual function table pointer "
3e43a32a 15301 "not found when defining class '%s'"),
e86ca25f 15302 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15303 }
15304 else
15305 {
ae6ae975 15306 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15307 }
15308 }
f6235d4c 15309 else if (cu->producer
61012eef 15310 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15311 {
15312 /* The IBM XLC compiler does not provide direct indication
15313 of the containing type, but the vtable pointer is
15314 always named __vfp. */
15315
15316 int i;
15317
15318 for (i = TYPE_NFIELDS (type) - 1;
15319 i >= TYPE_N_BASECLASSES (type);
15320 --i)
15321 {
15322 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15323 {
ae6ae975
DE
15324 set_type_vptr_fieldno (type, i);
15325 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15326 break;
15327 }
15328 }
15329 }
c906108c 15330 }
98751a41
JK
15331
15332 /* Copy fi.typedef_field_list linked list elements content into the
15333 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15334 if (!fi.typedef_field_list.empty ())
98751a41 15335 {
be2daae6 15336 int count = fi.typedef_field_list.size ();
98751a41 15337
a0d7a4ff 15338 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15339 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15340 = ((struct decl_field *)
be2daae6
TT
15341 TYPE_ALLOC (type,
15342 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15343 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15344
be2daae6
TT
15345 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15346 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15347 }
c767944b 15348
883fd55a
KS
15349 /* Copy fi.nested_types_list linked list elements content into the
15350 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15351 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15352 {
be2daae6 15353 int count = fi.nested_types_list.size ();
883fd55a
KS
15354
15355 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15356 TYPE_NESTED_TYPES_ARRAY (type)
15357 = ((struct decl_field *)
be2daae6
TT
15358 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15359 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15360
be2daae6
TT
15361 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15362 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15363 }
c906108c 15364 }
63d06c5c 15365
bb5ed363 15366 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15367 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15368 cu->rust_unions.push_back (type);
0b92b5bb 15369
90aeadfc
DC
15370 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15371 snapshots) has been known to create a die giving a declaration
15372 for a class that has, as a child, a die giving a definition for a
15373 nested class. So we have to process our children even if the
15374 current die is a declaration. Normally, of course, a declaration
15375 won't have any children at all. */
134d01f1 15376
ca040673
DE
15377 child_die = die->child;
15378
90aeadfc
DC
15379 while (child_die != NULL && child_die->tag)
15380 {
15381 if (child_die->tag == DW_TAG_member
15382 || child_die->tag == DW_TAG_variable
34eaf542
TT
15383 || child_die->tag == DW_TAG_inheritance
15384 || child_die->tag == DW_TAG_template_value_param
15385 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15386 {
90aeadfc 15387 /* Do nothing. */
134d01f1 15388 }
90aeadfc
DC
15389 else
15390 process_die (child_die, cu);
134d01f1 15391
436c571c 15392 child_die = child_die->sibling;
134d01f1
DJ
15393 }
15394
fa4028e9
JB
15395 /* Do not consider external references. According to the DWARF standard,
15396 these DIEs are identified by the fact that they have no byte_size
15397 attribute, and a declaration attribute. */
15398 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15399 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15400 {
15401 struct symbol *sym = new_symbol (die, type, cu);
15402
15403 if (has_template_parameters)
15404 {
a776957c
TT
15405 struct symtab *symtab;
15406 if (sym != nullptr)
15407 symtab = symbol_symtab (sym);
15408 else if (cu->line_header != nullptr)
15409 {
15410 /* Any related symtab will do. */
15411 symtab
7ba99d21 15412 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15413 }
15414 else
15415 {
15416 symtab = nullptr;
15417 complaint (_("could not find suitable "
15418 "symtab for template parameter"
15419 " - DIE at %s [in module %s]"),
15420 sect_offset_str (die->sect_off),
15421 objfile_name (objfile));
15422 }
15423
15424 if (symtab != nullptr)
15425 {
15426 /* Make sure that the symtab is set on the new symbols.
15427 Even though they don't appear in this symtab directly,
15428 other parts of gdb assume that symbols do, and this is
15429 reasonably true. */
15430 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15431 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15432 }
3e1d3d8c
TT
15433 }
15434 }
134d01f1
DJ
15435}
15436
55426c9d
JB
15437/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15438 update TYPE using some information only available in DIE's children. */
15439
15440static void
15441update_enumeration_type_from_children (struct die_info *die,
15442 struct type *type,
15443 struct dwarf2_cu *cu)
15444{
60f7655a 15445 struct die_info *child_die;
55426c9d
JB
15446 int unsigned_enum = 1;
15447 int flag_enum = 1;
55426c9d 15448
8268c778 15449 auto_obstack obstack;
55426c9d 15450
60f7655a
DE
15451 for (child_die = die->child;
15452 child_die != NULL && child_die->tag;
436c571c 15453 child_die = child_die->sibling)
55426c9d
JB
15454 {
15455 struct attribute *attr;
15456 LONGEST value;
15457 const gdb_byte *bytes;
15458 struct dwarf2_locexpr_baton *baton;
15459 const char *name;
60f7655a 15460
55426c9d
JB
15461 if (child_die->tag != DW_TAG_enumerator)
15462 continue;
15463
15464 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15465 if (attr == NULL)
15466 continue;
15467
15468 name = dwarf2_name (child_die, cu);
15469 if (name == NULL)
15470 name = "<anonymous enumerator>";
15471
15472 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15473 &value, &bytes, &baton);
15474 if (value < 0)
15475 {
15476 unsigned_enum = 0;
15477 flag_enum = 0;
15478 }
55426c9d 15479 else
edd45eb0
SM
15480 {
15481 if (count_one_bits_ll (value) >= 2)
15482 flag_enum = 0;
edd45eb0 15483 }
55426c9d
JB
15484
15485 /* If we already know that the enum type is neither unsigned, nor
15486 a flag type, no need to look at the rest of the enumerates. */
15487 if (!unsigned_enum && !flag_enum)
15488 break;
55426c9d
JB
15489 }
15490
15491 if (unsigned_enum)
15492 TYPE_UNSIGNED (type) = 1;
15493 if (flag_enum)
15494 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15495}
15496
134d01f1
DJ
15497/* Given a DW_AT_enumeration_type die, set its type. We do not
15498 complete the type's fields yet, or create any symbols. */
c906108c 15499
f792889a 15500static struct type *
134d01f1 15501read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15502{
518817b3 15503 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15504 struct type *type;
c906108c 15505 struct attribute *attr;
0114d602 15506 const char *name;
134d01f1 15507
348e048f
DE
15508 /* If the definition of this type lives in .debug_types, read that type.
15509 Don't follow DW_AT_specification though, that will take us back up
15510 the chain and we want to go down. */
052c8bb8 15511 attr = die->attr (DW_AT_signature);
435d3d88 15512 if (attr != nullptr)
348e048f 15513 {
ac9ec31b 15514 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15515
ac9ec31b 15516 /* The type's CU may not be the same as CU.
02142a6c 15517 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15518 return set_die_type (die, type, cu);
15519 }
15520
c906108c
SS
15521 type = alloc_type (objfile);
15522
15523 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15524 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15525 if (name != NULL)
e86ca25f 15526 TYPE_NAME (type) = name;
c906108c 15527
0626fc76
TT
15528 attr = dwarf2_attr (die, DW_AT_type, cu);
15529 if (attr != NULL)
15530 {
15531 struct type *underlying_type = die_type (die, cu);
15532
15533 TYPE_TARGET_TYPE (type) = underlying_type;
15534 }
15535
e142c38c 15536 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15537 if (attr != nullptr)
c906108c
SS
15538 {
15539 TYPE_LENGTH (type) = DW_UNSND (attr);
15540 }
15541 else
15542 {
15543 TYPE_LENGTH (type) = 0;
15544 }
15545
2b4424c3
TT
15546 maybe_set_alignment (cu, die, type);
15547
137033e9
JB
15548 /* The enumeration DIE can be incomplete. In Ada, any type can be
15549 declared as private in the package spec, and then defined only
15550 inside the package body. Such types are known as Taft Amendment
15551 Types. When another package uses such a type, an incomplete DIE
15552 may be generated by the compiler. */
02eb380e 15553 if (die_is_declaration (die, cu))
876cecd0 15554 TYPE_STUB (type) = 1;
02eb380e 15555
0626fc76
TT
15556 /* Finish the creation of this type by using the enum's children.
15557 We must call this even when the underlying type has been provided
15558 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15559 update_enumeration_type_from_children (die, type, cu);
15560
0626fc76
TT
15561 /* If this type has an underlying type that is not a stub, then we
15562 may use its attributes. We always use the "unsigned" attribute
15563 in this situation, because ordinarily we guess whether the type
15564 is unsigned -- but the guess can be wrong and the underlying type
15565 can tell us the reality. However, we defer to a local size
15566 attribute if one exists, because this lets the compiler override
15567 the underlying type if needed. */
15568 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15569 {
15570 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15571 if (TYPE_LENGTH (type) == 0)
15572 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15573 if (TYPE_RAW_ALIGN (type) == 0
15574 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15575 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15576 }
15577
3d567982
TT
15578 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15579
f792889a 15580 return set_die_type (die, type, cu);
134d01f1
DJ
15581}
15582
15583/* Given a pointer to a die which begins an enumeration, process all
15584 the dies that define the members of the enumeration, and create the
15585 symbol for the enumeration type.
15586
15587 NOTE: We reverse the order of the element list. */
15588
15589static void
15590process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15591{
f792889a 15592 struct type *this_type;
134d01f1 15593
f792889a
DJ
15594 this_type = get_die_type (die, cu);
15595 if (this_type == NULL)
15596 this_type = read_enumeration_type (die, cu);
9dc481d3 15597
639d11d3 15598 if (die->child != NULL)
c906108c 15599 {
9dc481d3
DE
15600 struct die_info *child_die;
15601 struct symbol *sym;
43816ebc 15602 std::vector<struct field> fields;
15d034d0 15603 const char *name;
9dc481d3 15604
639d11d3 15605 child_die = die->child;
c906108c
SS
15606 while (child_die && child_die->tag)
15607 {
15608 if (child_die->tag != DW_TAG_enumerator)
15609 {
e7c27a73 15610 process_die (child_die, cu);
c906108c
SS
15611 }
15612 else
15613 {
39cbfefa
DJ
15614 name = dwarf2_name (child_die, cu);
15615 if (name)
c906108c 15616 {
f792889a 15617 sym = new_symbol (child_die, this_type, cu);
c906108c 15618
43816ebc
TT
15619 fields.emplace_back ();
15620 struct field &field = fields.back ();
c906108c 15621
43816ebc
TT
15622 FIELD_NAME (field) = sym->linkage_name ();
15623 FIELD_TYPE (field) = NULL;
15624 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15625 FIELD_BITSIZE (field) = 0;
c906108c
SS
15626 }
15627 }
15628
436c571c 15629 child_die = child_die->sibling;
c906108c
SS
15630 }
15631
43816ebc 15632 if (!fields.empty ())
c906108c 15633 {
43816ebc 15634 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15635 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15636 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15637 memcpy (TYPE_FIELDS (this_type), fields.data (),
15638 sizeof (struct field) * fields.size ());
c906108c 15639 }
c906108c 15640 }
134d01f1 15641
6c83ed52
TT
15642 /* If we are reading an enum from a .debug_types unit, and the enum
15643 is a declaration, and the enum is not the signatured type in the
15644 unit, then we do not want to add a symbol for it. Adding a
15645 symbol would in some cases obscure the true definition of the
15646 enum, giving users an incomplete type when the definition is
15647 actually available. Note that we do not want to do this for all
15648 enums which are just declarations, because C++0x allows forward
15649 enum declarations. */
3019eac3 15650 if (cu->per_cu->is_debug_types
6c83ed52
TT
15651 && die_is_declaration (die, cu))
15652 {
52dc124a 15653 struct signatured_type *sig_type;
6c83ed52 15654
c0f78cd4 15655 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15656 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15657 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15658 return;
15659 }
15660
f792889a 15661 new_symbol (die, this_type, cu);
c906108c
SS
15662}
15663
15664/* Extract all information from a DW_TAG_array_type DIE and put it in
15665 the DIE's type field. For now, this only handles one dimensional
15666 arrays. */
15667
f792889a 15668static struct type *
e7c27a73 15669read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15670{
518817b3 15671 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15672 struct die_info *child_die;
7e314c57 15673 struct type *type;
c906108c 15674 struct type *element_type, *range_type, *index_type;
c906108c 15675 struct attribute *attr;
15d034d0 15676 const char *name;
a405673c 15677 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15678 unsigned int bit_stride = 0;
c906108c 15679
e7c27a73 15680 element_type = die_type (die, cu);
c906108c 15681
7e314c57
JK
15682 /* The die_type call above may have already set the type for this DIE. */
15683 type = get_die_type (die, cu);
15684 if (type)
15685 return type;
15686
dc53a7ad
JB
15687 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15688 if (attr != NULL)
a405673c
JB
15689 {
15690 int stride_ok;
09ba997f 15691 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15692
15693 byte_stride_prop
15694 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15695 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15696 prop_type);
a405673c
JB
15697 if (!stride_ok)
15698 {
b98664d3 15699 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15700 " - DIE at %s [in module %s]"),
15701 sect_offset_str (die->sect_off),
518817b3 15702 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15703 /* Ignore this attribute. We will likely not be able to print
15704 arrays of this type correctly, but there is little we can do
15705 to help if we cannot read the attribute's value. */
15706 byte_stride_prop = NULL;
15707 }
15708 }
dc53a7ad
JB
15709
15710 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15711 if (attr != NULL)
15712 bit_stride = DW_UNSND (attr);
15713
c906108c
SS
15714 /* Irix 6.2 native cc creates array types without children for
15715 arrays with unspecified length. */
639d11d3 15716 if (die->child == NULL)
c906108c 15717 {
46bf5051 15718 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15719 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15720 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15721 byte_stride_prop, bit_stride);
f792889a 15722 return set_die_type (die, type, cu);
c906108c
SS
15723 }
15724
791afaa2 15725 std::vector<struct type *> range_types;
639d11d3 15726 child_die = die->child;
c906108c
SS
15727 while (child_die && child_die->tag)
15728 {
15729 if (child_die->tag == DW_TAG_subrange_type)
15730 {
f792889a 15731 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15732
f792889a 15733 if (child_type != NULL)
a02abb62 15734 {
0963b4bd
MS
15735 /* The range type was succesfully read. Save it for the
15736 array type creation. */
791afaa2 15737 range_types.push_back (child_type);
a02abb62 15738 }
c906108c 15739 }
436c571c 15740 child_die = child_die->sibling;
c906108c
SS
15741 }
15742
15743 /* Dwarf2 dimensions are output from left to right, create the
15744 necessary array types in backwards order. */
7ca2d3a3 15745
c906108c 15746 type = element_type;
7ca2d3a3
DL
15747
15748 if (read_array_order (die, cu) == DW_ORD_col_major)
15749 {
15750 int i = 0;
9a619af0 15751
791afaa2 15752 while (i < range_types.size ())
dc53a7ad 15753 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15754 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15755 }
15756 else
15757 {
791afaa2 15758 size_t ndim = range_types.size ();
7ca2d3a3 15759 while (ndim-- > 0)
dc53a7ad 15760 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15761 byte_stride_prop, bit_stride);
7ca2d3a3 15762 }
c906108c 15763
f5f8a009
EZ
15764 /* Understand Dwarf2 support for vector types (like they occur on
15765 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15766 array type. This is not part of the Dwarf2/3 standard yet, but a
15767 custom vendor extension. The main difference between a regular
15768 array and the vector variant is that vectors are passed by value
15769 to functions. */
e142c38c 15770 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15771 if (attr != nullptr)
ea37ba09 15772 make_vector_type (type);
f5f8a009 15773
dbc98a8b
KW
15774 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15775 implementation may choose to implement triple vectors using this
15776 attribute. */
15777 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15778 if (attr != nullptr)
dbc98a8b
KW
15779 {
15780 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15781 TYPE_LENGTH (type) = DW_UNSND (attr);
15782 else
b98664d3 15783 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15784 "than the total size of elements"));
dbc98a8b
KW
15785 }
15786
39cbfefa
DJ
15787 name = dwarf2_name (die, cu);
15788 if (name)
15789 TYPE_NAME (type) = name;
6e70227d 15790
2b4424c3
TT
15791 maybe_set_alignment (cu, die, type);
15792
0963b4bd 15793 /* Install the type in the die. */
7e314c57
JK
15794 set_die_type (die, type, cu);
15795
15796 /* set_die_type should be already done. */
b4ba55a1
JB
15797 set_descriptive_type (type, die, cu);
15798
7e314c57 15799 return type;
c906108c
SS
15800}
15801
7ca2d3a3 15802static enum dwarf_array_dim_ordering
6e70227d 15803read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15804{
15805 struct attribute *attr;
15806
15807 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15808
435d3d88 15809 if (attr != nullptr)
aead7601 15810 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15811
0963b4bd
MS
15812 /* GNU F77 is a special case, as at 08/2004 array type info is the
15813 opposite order to the dwarf2 specification, but data is still
15814 laid out as per normal fortran.
7ca2d3a3 15815
0963b4bd
MS
15816 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15817 version checking. */
7ca2d3a3 15818
905e0470
PM
15819 if (cu->language == language_fortran
15820 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15821 {
15822 return DW_ORD_row_major;
15823 }
15824
6e70227d 15825 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15826 {
15827 case array_column_major:
15828 return DW_ORD_col_major;
15829 case array_row_major:
15830 default:
15831 return DW_ORD_row_major;
15832 };
15833}
15834
72019c9c 15835/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15836 the DIE's type field. */
72019c9c 15837
f792889a 15838static struct type *
72019c9c
GM
15839read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15840{
7e314c57
JK
15841 struct type *domain_type, *set_type;
15842 struct attribute *attr;
f792889a 15843
7e314c57
JK
15844 domain_type = die_type (die, cu);
15845
15846 /* The die_type call above may have already set the type for this DIE. */
15847 set_type = get_die_type (die, cu);
15848 if (set_type)
15849 return set_type;
15850
15851 set_type = create_set_type (NULL, domain_type);
15852
15853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15854 if (attr != nullptr)
d09039dd 15855 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15856
2b4424c3
TT
15857 maybe_set_alignment (cu, die, set_type);
15858
f792889a 15859 return set_die_type (die, set_type, cu);
72019c9c 15860}
7ca2d3a3 15861
0971de02
TT
15862/* A helper for read_common_block that creates a locexpr baton.
15863 SYM is the symbol which we are marking as computed.
15864 COMMON_DIE is the DIE for the common block.
15865 COMMON_LOC is the location expression attribute for the common
15866 block itself.
15867 MEMBER_LOC is the location expression attribute for the particular
15868 member of the common block that we are processing.
15869 CU is the CU from which the above come. */
15870
15871static void
15872mark_common_block_symbol_computed (struct symbol *sym,
15873 struct die_info *common_die,
15874 struct attribute *common_loc,
15875 struct attribute *member_loc,
15876 struct dwarf2_cu *cu)
15877{
518817b3
SM
15878 struct dwarf2_per_objfile *dwarf2_per_objfile
15879 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15880 struct objfile *objfile = dwarf2_per_objfile->objfile;
15881 struct dwarf2_locexpr_baton *baton;
15882 gdb_byte *ptr;
15883 unsigned int cu_off;
15884 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15885 LONGEST offset = 0;
15886
15887 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15888 gdb_assert (common_loc->form_is_block ());
15889 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15890 || member_loc->form_is_constant ());
0971de02 15891
8d749320 15892 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15893 baton->per_cu = cu->per_cu;
15894 gdb_assert (baton->per_cu);
15895
15896 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15897
cd6c91b4 15898 if (member_loc->form_is_constant ())
0971de02
TT
15899 {
15900 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15901 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15902 }
15903 else
15904 baton->size += DW_BLOCK (member_loc)->size;
15905
224c3ddb 15906 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15907 baton->data = ptr;
15908
15909 *ptr++ = DW_OP_call4;
9c541725 15910 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15911 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15912 ptr += 4;
15913
cd6c91b4 15914 if (member_loc->form_is_constant ())
0971de02
TT
15915 {
15916 *ptr++ = DW_OP_addr;
15917 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15918 ptr += cu->header.addr_size;
15919 }
15920 else
15921 {
15922 /* We have to copy the data here, because DW_OP_call4 will only
15923 use a DW_AT_location attribute. */
15924 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15925 ptr += DW_BLOCK (member_loc)->size;
15926 }
15927
15928 *ptr++ = DW_OP_plus;
15929 gdb_assert (ptr - baton->data == baton->size);
15930
0971de02 15931 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15932 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15933}
15934
4357ac6c
TT
15935/* Create appropriate locally-scoped variables for all the
15936 DW_TAG_common_block entries. Also create a struct common_block
15937 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 15938 is used to separate the common blocks name namespace from regular
4357ac6c 15939 variable names. */
c906108c
SS
15940
15941static void
e7c27a73 15942read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15943{
0971de02
TT
15944 struct attribute *attr;
15945
15946 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 15947 if (attr != nullptr)
0971de02
TT
15948 {
15949 /* Support the .debug_loc offsets. */
4fc6c0d5 15950 if (attr->form_is_block ())
0971de02
TT
15951 {
15952 /* Ok. */
15953 }
cd6c91b4 15954 else if (attr->form_is_section_offset ())
0971de02
TT
15955 {
15956 dwarf2_complex_location_expr_complaint ();
15957 attr = NULL;
15958 }
15959 else
15960 {
15961 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15962 "common block member");
15963 attr = NULL;
15964 }
15965 }
15966
639d11d3 15967 if (die->child != NULL)
c906108c 15968 {
518817b3 15969 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
15970 struct die_info *child_die;
15971 size_t n_entries = 0, size;
15972 struct common_block *common_block;
15973 struct symbol *sym;
74ac6d43 15974
4357ac6c
TT
15975 for (child_die = die->child;
15976 child_die && child_die->tag;
436c571c 15977 child_die = child_die->sibling)
4357ac6c
TT
15978 ++n_entries;
15979
15980 size = (sizeof (struct common_block)
15981 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
15982 common_block
15983 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15984 size);
4357ac6c
TT
15985 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15986 common_block->n_entries = 0;
15987
15988 for (child_die = die->child;
15989 child_die && child_die->tag;
436c571c 15990 child_die = child_die->sibling)
4357ac6c
TT
15991 {
15992 /* Create the symbol in the DW_TAG_common_block block in the current
15993 symbol scope. */
e7c27a73 15994 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
15995 if (sym != NULL)
15996 {
15997 struct attribute *member_loc;
15998
15999 common_block->contents[common_block->n_entries++] = sym;
16000
16001 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16002 cu);
16003 if (member_loc)
16004 {
16005 /* GDB has handled this for a long time, but it is
16006 not specified by DWARF. It seems to have been
16007 emitted by gfortran at least as recently as:
16008 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16009 complaint (_("Variable in common block has "
0971de02 16010 "DW_AT_data_member_location "
9d8780f0
SM
16011 "- DIE at %s [in module %s]"),
16012 sect_offset_str (child_die->sect_off),
518817b3 16013 objfile_name (objfile));
0971de02 16014
cd6c91b4 16015 if (member_loc->form_is_section_offset ())
0971de02 16016 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16017 else if (member_loc->form_is_constant ()
4fc6c0d5 16018 || member_loc->form_is_block ())
0971de02 16019 {
435d3d88 16020 if (attr != nullptr)
0971de02
TT
16021 mark_common_block_symbol_computed (sym, die, attr,
16022 member_loc, cu);
16023 }
16024 else
16025 dwarf2_complex_location_expr_complaint ();
16026 }
16027 }
c906108c 16028 }
4357ac6c
TT
16029
16030 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16031 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16032 }
16033}
16034
0114d602 16035/* Create a type for a C++ namespace. */
d9fa45fe 16036
0114d602
DJ
16037static struct type *
16038read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16039{
518817b3 16040 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16041 const char *previous_prefix, *name;
9219021c 16042 int is_anonymous;
0114d602
DJ
16043 struct type *type;
16044
16045 /* For extensions, reuse the type of the original namespace. */
16046 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16047 {
16048 struct die_info *ext_die;
16049 struct dwarf2_cu *ext_cu = cu;
9a619af0 16050
0114d602
DJ
16051 ext_die = dwarf2_extension (die, &ext_cu);
16052 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16053
16054 /* EXT_CU may not be the same as CU.
02142a6c 16055 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16056 return set_die_type (die, type, cu);
16057 }
9219021c 16058
e142c38c 16059 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16060
16061 /* Now build the name of the current namespace. */
16062
0114d602
DJ
16063 previous_prefix = determine_prefix (die, cu);
16064 if (previous_prefix[0] != '\0')
16065 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16066 previous_prefix, name, 0, cu);
0114d602
DJ
16067
16068 /* Create the type. */
19f392bc 16069 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16070
60531b24 16071 return set_die_type (die, type, cu);
0114d602
DJ
16072}
16073
22cee43f 16074/* Read a namespace scope. */
0114d602
DJ
16075
16076static void
16077read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16078{
518817b3 16079 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16080 int is_anonymous;
9219021c 16081
5c4e30ca
DC
16082 /* Add a symbol associated to this if we haven't seen the namespace
16083 before. Also, add a using directive if it's an anonymous
16084 namespace. */
9219021c 16085
f2f0e013 16086 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16087 {
16088 struct type *type;
16089
0114d602 16090 type = read_type_die (die, cu);
e7c27a73 16091 new_symbol (die, type, cu);
5c4e30ca 16092
e8e80198 16093 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16094 if (is_anonymous)
0114d602
DJ
16095 {
16096 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16097
eb1e02fd 16098 std::vector<const char *> excludes;
804d2729 16099 add_using_directive (using_directives (cu),
22cee43f 16100 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16101 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16102 }
5c4e30ca 16103 }
9219021c 16104
639d11d3 16105 if (die->child != NULL)
d9fa45fe 16106 {
639d11d3 16107 struct die_info *child_die = die->child;
6e70227d 16108
d9fa45fe
DC
16109 while (child_die && child_die->tag)
16110 {
e7c27a73 16111 process_die (child_die, cu);
436c571c 16112 child_die = child_die->sibling;
d9fa45fe
DC
16113 }
16114 }
38d518c9
EZ
16115}
16116
f55ee35c
JK
16117/* Read a Fortran module as type. This DIE can be only a declaration used for
16118 imported module. Still we need that type as local Fortran "use ... only"
16119 declaration imports depend on the created type in determine_prefix. */
16120
16121static struct type *
16122read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16123{
518817b3 16124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16125 const char *module_name;
f55ee35c
JK
16126 struct type *type;
16127
16128 module_name = dwarf2_name (die, cu);
19f392bc 16129 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16130
f55ee35c
JK
16131 return set_die_type (die, type, cu);
16132}
16133
5d7cb8df
JK
16134/* Read a Fortran module. */
16135
16136static void
16137read_module (struct die_info *die, struct dwarf2_cu *cu)
16138{
16139 struct die_info *child_die = die->child;
530e8392
KB
16140 struct type *type;
16141
16142 type = read_type_die (die, cu);
16143 new_symbol (die, type, cu);
5d7cb8df 16144
5d7cb8df
JK
16145 while (child_die && child_die->tag)
16146 {
16147 process_die (child_die, cu);
436c571c 16148 child_die = child_die->sibling;
5d7cb8df
JK
16149 }
16150}
16151
38d518c9
EZ
16152/* Return the name of the namespace represented by DIE. Set
16153 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16154 namespace. */
16155
16156static const char *
e142c38c 16157namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16158{
16159 struct die_info *current_die;
16160 const char *name = NULL;
16161
16162 /* Loop through the extensions until we find a name. */
16163
16164 for (current_die = die;
16165 current_die != NULL;
f2f0e013 16166 current_die = dwarf2_extension (die, &cu))
38d518c9 16167 {
96553a0c
DE
16168 /* We don't use dwarf2_name here so that we can detect the absence
16169 of a name -> anonymous namespace. */
7d45c7c3 16170 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16171
38d518c9
EZ
16172 if (name != NULL)
16173 break;
16174 }
16175
16176 /* Is it an anonymous namespace? */
16177
16178 *is_anonymous = (name == NULL);
16179 if (*is_anonymous)
2b1dbab0 16180 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16181
16182 return name;
d9fa45fe
DC
16183}
16184
c906108c
SS
16185/* Extract all information from a DW_TAG_pointer_type DIE and add to
16186 the user defined type vector. */
16187
f792889a 16188static struct type *
e7c27a73 16189read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16190{
518817b3
SM
16191 struct gdbarch *gdbarch
16192 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16193 struct comp_unit_head *cu_header = &cu->header;
c906108c 16194 struct type *type;
8b2dbe47
KB
16195 struct attribute *attr_byte_size;
16196 struct attribute *attr_address_class;
16197 int byte_size, addr_class;
7e314c57
JK
16198 struct type *target_type;
16199
16200 target_type = die_type (die, cu);
c906108c 16201
7e314c57
JK
16202 /* The die_type call above may have already set the type for this DIE. */
16203 type = get_die_type (die, cu);
16204 if (type)
16205 return type;
16206
16207 type = lookup_pointer_type (target_type);
8b2dbe47 16208
e142c38c 16209 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16210 if (attr_byte_size)
16211 byte_size = DW_UNSND (attr_byte_size);
c906108c 16212 else
8b2dbe47
KB
16213 byte_size = cu_header->addr_size;
16214
e142c38c 16215 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16216 if (attr_address_class)
16217 addr_class = DW_UNSND (attr_address_class);
16218 else
16219 addr_class = DW_ADDR_none;
16220
2b4424c3
TT
16221 ULONGEST alignment = get_alignment (cu, die);
16222
16223 /* If the pointer size, alignment, or address class is different
16224 than the default, create a type variant marked as such and set
16225 the length accordingly. */
16226 if (TYPE_LENGTH (type) != byte_size
16227 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16228 && alignment != TYPE_RAW_ALIGN (type))
16229 || addr_class != DW_ADDR_none)
c906108c 16230 {
5e2b427d 16231 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16232 {
16233 int type_flags;
16234
849957d9 16235 type_flags = gdbarch_address_class_type_flags
5e2b427d 16236 (gdbarch, byte_size, addr_class);
876cecd0
TT
16237 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16238 == 0);
8b2dbe47
KB
16239 type = make_type_with_address_space (type, type_flags);
16240 }
16241 else if (TYPE_LENGTH (type) != byte_size)
16242 {
b98664d3 16243 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16244 }
2b4424c3
TT
16245 else if (TYPE_RAW_ALIGN (type) != alignment)
16246 {
b98664d3 16247 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16248 " - DIE at %s [in module %s]"),
16249 sect_offset_str (die->sect_off),
16250 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16251 }
6e70227d 16252 else
9a619af0
MS
16253 {
16254 /* Should we also complain about unhandled address classes? */
16255 }
c906108c 16256 }
8b2dbe47
KB
16257
16258 TYPE_LENGTH (type) = byte_size;
2b4424c3 16259 set_type_align (type, alignment);
f792889a 16260 return set_die_type (die, type, cu);
c906108c
SS
16261}
16262
16263/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16264 the user defined type vector. */
16265
f792889a 16266static struct type *
e7c27a73 16267read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16268{
16269 struct type *type;
16270 struct type *to_type;
16271 struct type *domain;
16272
e7c27a73
DJ
16273 to_type = die_type (die, cu);
16274 domain = die_containing_type (die, cu);
0d5de010 16275
7e314c57
JK
16276 /* The calls above may have already set the type for this DIE. */
16277 type = get_die_type (die, cu);
16278 if (type)
16279 return type;
16280
0d5de010
DJ
16281 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16282 type = lookup_methodptr_type (to_type);
7078baeb
TT
16283 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16284 {
518817b3
SM
16285 struct type *new_type
16286 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16287
16288 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16289 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16290 TYPE_VARARGS (to_type));
16291 type = lookup_methodptr_type (new_type);
16292 }
0d5de010
DJ
16293 else
16294 type = lookup_memberptr_type (to_type, domain);
c906108c 16295
f792889a 16296 return set_die_type (die, type, cu);
c906108c
SS
16297}
16298
4297a3f0 16299/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16300 the user defined type vector. */
16301
f792889a 16302static struct type *
4297a3f0
AV
16303read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16304 enum type_code refcode)
c906108c 16305{
e7c27a73 16306 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16307 struct type *type, *target_type;
c906108c
SS
16308 struct attribute *attr;
16309
4297a3f0
AV
16310 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16311
7e314c57
JK
16312 target_type = die_type (die, cu);
16313
16314 /* The die_type call above may have already set the type for this DIE. */
16315 type = get_die_type (die, cu);
16316 if (type)
16317 return type;
16318
4297a3f0 16319 type = lookup_reference_type (target_type, refcode);
e142c38c 16320 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16321 if (attr != nullptr)
c906108c
SS
16322 {
16323 TYPE_LENGTH (type) = DW_UNSND (attr);
16324 }
16325 else
16326 {
107d2387 16327 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16328 }
2b4424c3 16329 maybe_set_alignment (cu, die, type);
f792889a 16330 return set_die_type (die, type, cu);
c906108c
SS
16331}
16332
cf363f18
MW
16333/* Add the given cv-qualifiers to the element type of the array. GCC
16334 outputs DWARF type qualifiers that apply to an array, not the
16335 element type. But GDB relies on the array element type to carry
16336 the cv-qualifiers. This mimics section 6.7.3 of the C99
16337 specification. */
16338
16339static struct type *
16340add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16341 struct type *base_type, int cnst, int voltl)
16342{
16343 struct type *el_type, *inner_array;
16344
16345 base_type = copy_type (base_type);
16346 inner_array = base_type;
16347
16348 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16349 {
16350 TYPE_TARGET_TYPE (inner_array) =
16351 copy_type (TYPE_TARGET_TYPE (inner_array));
16352 inner_array = TYPE_TARGET_TYPE (inner_array);
16353 }
16354
16355 el_type = TYPE_TARGET_TYPE (inner_array);
16356 cnst |= TYPE_CONST (el_type);
16357 voltl |= TYPE_VOLATILE (el_type);
16358 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16359
16360 return set_die_type (die, base_type, cu);
16361}
16362
f792889a 16363static struct type *
e7c27a73 16364read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16365{
f792889a 16366 struct type *base_type, *cv_type;
c906108c 16367
e7c27a73 16368 base_type = die_type (die, cu);
7e314c57
JK
16369
16370 /* The die_type call above may have already set the type for this DIE. */
16371 cv_type = get_die_type (die, cu);
16372 if (cv_type)
16373 return cv_type;
16374
2f608a3a
KW
16375 /* In case the const qualifier is applied to an array type, the element type
16376 is so qualified, not the array type (section 6.7.3 of C99). */
16377 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16378 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16379
f792889a
DJ
16380 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16381 return set_die_type (die, cv_type, cu);
c906108c
SS
16382}
16383
f792889a 16384static struct type *
e7c27a73 16385read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16386{
f792889a 16387 struct type *base_type, *cv_type;
c906108c 16388
e7c27a73 16389 base_type = die_type (die, cu);
7e314c57
JK
16390
16391 /* The die_type call above may have already set the type for this DIE. */
16392 cv_type = get_die_type (die, cu);
16393 if (cv_type)
16394 return cv_type;
16395
cf363f18
MW
16396 /* In case the volatile qualifier is applied to an array type, the
16397 element type is so qualified, not the array type (section 6.7.3
16398 of C99). */
16399 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16400 return add_array_cv_type (die, cu, base_type, 0, 1);
16401
f792889a
DJ
16402 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16403 return set_die_type (die, cv_type, cu);
c906108c
SS
16404}
16405
06d66ee9
TT
16406/* Handle DW_TAG_restrict_type. */
16407
16408static struct type *
16409read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16410{
16411 struct type *base_type, *cv_type;
16412
16413 base_type = die_type (die, cu);
16414
16415 /* The die_type call above may have already set the type for this DIE. */
16416 cv_type = get_die_type (die, cu);
16417 if (cv_type)
16418 return cv_type;
16419
16420 cv_type = make_restrict_type (base_type);
16421 return set_die_type (die, cv_type, cu);
16422}
16423
a2c2acaf
MW
16424/* Handle DW_TAG_atomic_type. */
16425
16426static struct type *
16427read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16428{
16429 struct type *base_type, *cv_type;
16430
16431 base_type = die_type (die, cu);
16432
16433 /* The die_type call above may have already set the type for this DIE. */
16434 cv_type = get_die_type (die, cu);
16435 if (cv_type)
16436 return cv_type;
16437
16438 cv_type = make_atomic_type (base_type);
16439 return set_die_type (die, cv_type, cu);
16440}
16441
c906108c
SS
16442/* Extract all information from a DW_TAG_string_type DIE and add to
16443 the user defined type vector. It isn't really a user defined type,
16444 but it behaves like one, with other DIE's using an AT_user_def_type
16445 attribute to reference it. */
16446
f792889a 16447static struct type *
e7c27a73 16448read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16449{
518817b3 16450 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16451 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16452 struct type *type, *range_type, *index_type, *char_type;
16453 struct attribute *attr;
216a7e6b
AB
16454 struct dynamic_prop prop;
16455 bool length_is_constant = true;
16456 LONGEST length;
16457
16458 /* There are a couple of places where bit sizes might be made use of
16459 when parsing a DW_TAG_string_type, however, no producer that we know
16460 of make use of these. Handling bit sizes that are a multiple of the
16461 byte size is easy enough, but what about other bit sizes? Lets deal
16462 with that problem when we have to. Warn about these attributes being
16463 unsupported, then parse the type and ignore them like we always
16464 have. */
16465 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16466 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16467 {
16468 static bool warning_printed = false;
16469 if (!warning_printed)
16470 {
16471 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16472 "currently supported on DW_TAG_string_type."));
16473 warning_printed = true;
16474 }
16475 }
c906108c 16476
e142c38c 16477 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16478 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16479 {
16480 /* The string length describes the location at which the length of
16481 the string can be found. The size of the length field can be
16482 specified with one of the attributes below. */
16483 struct type *prop_type;
16484 struct attribute *len
16485 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16486 if (len == nullptr)
16487 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16488 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16489 {
16490 /* Pass 0 as the default as we know this attribute is constant
16491 and the default value will not be returned. */
16492 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
09ba997f 16493 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16494 }
16495 else
16496 {
16497 /* If the size is not specified then we assume it is the size of
16498 an address on this target. */
09ba997f 16499 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16500 }
16501
16502 /* Convert the attribute into a dynamic property. */
16503 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16504 length = 1;
16505 else
16506 length_is_constant = false;
16507 }
16508 else if (attr != nullptr)
16509 {
16510 /* This DW_AT_string_length just contains the length with no
16511 indirection. There's no need to create a dynamic property in this
16512 case. Pass 0 for the default value as we know it will not be
16513 returned in this case. */
16514 length = dwarf2_get_attr_constant_value (attr, 0);
16515 }
16516 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16517 {
216a7e6b
AB
16518 /* We don't currently support non-constant byte sizes for strings. */
16519 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
16520 }
16521 else
16522 {
216a7e6b
AB
16523 /* Use 1 as a fallback length if we have nothing else. */
16524 length = 1;
c906108c 16525 }
6ccb9162 16526
46bf5051 16527 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16528 if (length_is_constant)
16529 range_type = create_static_range_type (NULL, index_type, 1, length);
16530 else
16531 {
16532 struct dynamic_prop low_bound;
16533
16534 low_bound.kind = PROP_CONST;
16535 low_bound.data.const_val = 1;
16536 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16537 }
3b7538c0
UW
16538 char_type = language_string_char_type (cu->language_defn, gdbarch);
16539 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16540
f792889a 16541 return set_die_type (die, type, cu);
c906108c
SS
16542}
16543
4d804846
JB
16544/* Assuming that DIE corresponds to a function, returns nonzero
16545 if the function is prototyped. */
16546
16547static int
16548prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16549{
16550 struct attribute *attr;
16551
16552 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16553 if (attr && (DW_UNSND (attr) != 0))
16554 return 1;
16555
16556 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16557 is only meaningful for C, but the concept also extends to other
4d804846
JB
16558 languages that allow unprototyped functions (Eg: Objective C).
16559 For all other languages, assume that functions are always
16560 prototyped. */
16561 if (cu->language != language_c
16562 && cu->language != language_objc
16563 && cu->language != language_opencl)
16564 return 1;
16565
16566 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16567 prototyped and unprototyped functions; default to prototyped,
16568 since that is more common in modern code (and RealView warns
16569 about unprototyped functions). */
16570 if (producer_is_realview (cu->producer))
16571 return 1;
16572
16573 return 0;
16574}
16575
c906108c
SS
16576/* Handle DIES due to C code like:
16577
16578 struct foo
c5aa993b
JM
16579 {
16580 int (*funcp)(int a, long l);
16581 int b;
16582 };
c906108c 16583
0963b4bd 16584 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16585
f792889a 16586static struct type *
e7c27a73 16587read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16588{
518817b3 16589 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16590 struct type *type; /* Type that this function returns. */
16591 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16592 struct attribute *attr;
16593
e7c27a73 16594 type = die_type (die, cu);
7e314c57
JK
16595
16596 /* The die_type call above may have already set the type for this DIE. */
16597 ftype = get_die_type (die, cu);
16598 if (ftype)
16599 return ftype;
16600
0c8b41f1 16601 ftype = lookup_function_type (type);
c906108c 16602
4d804846 16603 if (prototyped_function_p (die, cu))
a6c727b2 16604 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16605
c055b101
CV
16606 /* Store the calling convention in the type if it's available in
16607 the subroutine die. Otherwise set the calling convention to
16608 the default value DW_CC_normal. */
16609 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16610 if (attr != nullptr
16611 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16612 TYPE_CALLING_CONVENTION (ftype)
16613 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16614 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16615 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16616 else
16617 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16618
743649fd
MW
16619 /* Record whether the function returns normally to its caller or not
16620 if the DWARF producer set that information. */
16621 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16622 if (attr && (DW_UNSND (attr) != 0))
16623 TYPE_NO_RETURN (ftype) = 1;
16624
76c10ea2
GM
16625 /* We need to add the subroutine type to the die immediately so
16626 we don't infinitely recurse when dealing with parameters
0963b4bd 16627 declared as the same subroutine type. */
76c10ea2 16628 set_die_type (die, ftype, cu);
6e70227d 16629
639d11d3 16630 if (die->child != NULL)
c906108c 16631 {
bb5ed363 16632 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16633 struct die_info *child_die;
8072405b 16634 int nparams, iparams;
c906108c
SS
16635
16636 /* Count the number of parameters.
16637 FIXME: GDB currently ignores vararg functions, but knows about
16638 vararg member functions. */
8072405b 16639 nparams = 0;
639d11d3 16640 child_die = die->child;
c906108c
SS
16641 while (child_die && child_die->tag)
16642 {
16643 if (child_die->tag == DW_TAG_formal_parameter)
16644 nparams++;
16645 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16646 TYPE_VARARGS (ftype) = 1;
436c571c 16647 child_die = child_die->sibling;
c906108c
SS
16648 }
16649
16650 /* Allocate storage for parameters and fill them in. */
16651 TYPE_NFIELDS (ftype) = nparams;
16652 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16653 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16654
8072405b
JK
16655 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16656 even if we error out during the parameters reading below. */
16657 for (iparams = 0; iparams < nparams; iparams++)
16658 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16659
16660 iparams = 0;
639d11d3 16661 child_die = die->child;
c906108c
SS
16662 while (child_die && child_die->tag)
16663 {
16664 if (child_die->tag == DW_TAG_formal_parameter)
16665 {
3ce3b1ba
PA
16666 struct type *arg_type;
16667
16668 /* DWARF version 2 has no clean way to discern C++
16669 static and non-static member functions. G++ helps
16670 GDB by marking the first parameter for non-static
16671 member functions (which is the this pointer) as
16672 artificial. We pass this information to
16673 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16674
16675 DWARF version 3 added DW_AT_object_pointer, which GCC
16676 4.5 does not yet generate. */
e142c38c 16677 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16678 if (attr != nullptr)
c906108c
SS
16679 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16680 else
9c37b5ae 16681 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16682 arg_type = die_type (child_die, cu);
16683
16684 /* RealView does not mark THIS as const, which the testsuite
16685 expects. GCC marks THIS as const in method definitions,
16686 but not in the class specifications (GCC PR 43053). */
16687 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16688 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16689 {
16690 int is_this = 0;
16691 struct dwarf2_cu *arg_cu = cu;
16692 const char *name = dwarf2_name (child_die, cu);
16693
16694 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16695 if (attr != nullptr)
3ce3b1ba
PA
16696 {
16697 /* If the compiler emits this, use it. */
16698 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16699 is_this = 1;
16700 }
16701 else if (name && strcmp (name, "this") == 0)
16702 /* Function definitions will have the argument names. */
16703 is_this = 1;
16704 else if (name == NULL && iparams == 0)
16705 /* Declarations may not have the names, so like
16706 elsewhere in GDB, assume an artificial first
16707 argument is "this". */
16708 is_this = 1;
16709
16710 if (is_this)
16711 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16712 arg_type, 0);
16713 }
16714
16715 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16716 iparams++;
16717 }
436c571c 16718 child_die = child_die->sibling;
c906108c
SS
16719 }
16720 }
16721
76c10ea2 16722 return ftype;
c906108c
SS
16723}
16724
f792889a 16725static struct type *
e7c27a73 16726read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16727{
518817b3 16728 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16729 const char *name = NULL;
3c8e0968 16730 struct type *this_type, *target_type;
c906108c 16731
94af9270 16732 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16733 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16734 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16735 set_die_type (die, this_type, cu);
3c8e0968
DE
16736 target_type = die_type (die, cu);
16737 if (target_type != this_type)
16738 TYPE_TARGET_TYPE (this_type) = target_type;
16739 else
16740 {
16741 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16742 spec and cause infinite loops in GDB. */
b98664d3 16743 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16744 "- DIE at %s [in module %s]"),
16745 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16746 TYPE_TARGET_TYPE (this_type) = NULL;
16747 }
e4003a34
TV
16748 if (name == NULL)
16749 {
16750 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16751 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16752 Handle these by just returning the target type, rather than
16753 constructing an anonymous typedef type and trying to handle this
16754 elsewhere. */
16755 set_die_type (die, target_type, cu);
16756 return target_type;
16757 }
f792889a 16758 return this_type;
c906108c
SS
16759}
16760
9b790ce7
UW
16761/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16762 (which may be different from NAME) to the architecture back-end to allow
16763 it to guess the correct format if necessary. */
16764
16765static struct type *
16766dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16767 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16768{
16769 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16770 const struct floatformat **format;
16771 struct type *type;
16772
16773 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16774 if (format)
103a685e 16775 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16776 else
77b7c781 16777 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16778
16779 return type;
16780}
16781
eb77c9df
AB
16782/* Allocate an integer type of size BITS and name NAME. */
16783
16784static struct type *
16785dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16786 int bits, int unsigned_p, const char *name)
16787{
16788 struct type *type;
16789
16790 /* Versions of Intel's C Compiler generate an integer type called "void"
16791 instead of using DW_TAG_unspecified_type. This has been seen on
16792 at least versions 14, 17, and 18. */
35ee2dc2
AB
16793 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16794 && strcmp (name, "void") == 0)
eb77c9df
AB
16795 type = objfile_type (objfile)->builtin_void;
16796 else
16797 type = init_integer_type (objfile, bits, unsigned_p, name);
16798
16799 return type;
16800}
16801
8bdc1658
AB
16802/* Initialise and return a floating point type of size BITS suitable for
16803 use as a component of a complex number. The NAME_HINT is passed through
16804 when initialising the floating point type and is the name of the complex
16805 type.
16806
16807 As DWARF doesn't currently provide an explicit name for the components
16808 of a complex number, but it can be helpful to have these components
16809 named, we try to select a suitable name based on the size of the
16810 component. */
16811static struct type *
16812dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16813 struct objfile *objfile,
103a685e
TT
16814 int bits, const char *name_hint,
16815 enum bfd_endian byte_order)
8bdc1658
AB
16816{
16817 gdbarch *gdbarch = get_objfile_arch (objfile);
16818 struct type *tt = nullptr;
16819
35add35e
AB
16820 /* Try to find a suitable floating point builtin type of size BITS.
16821 We're going to use the name of this type as the name for the complex
16822 target type that we are about to create. */
1db455a7 16823 switch (cu->language)
8bdc1658 16824 {
1db455a7
AB
16825 case language_fortran:
16826 switch (bits)
16827 {
16828 case 32:
16829 tt = builtin_f_type (gdbarch)->builtin_real;
16830 break;
16831 case 64:
16832 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16833 break;
16834 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16835 case 128:
16836 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16837 break;
16838 }
8bdc1658 16839 break;
1db455a7
AB
16840 default:
16841 switch (bits)
16842 {
16843 case 32:
16844 tt = builtin_type (gdbarch)->builtin_float;
16845 break;
16846 case 64:
16847 tt = builtin_type (gdbarch)->builtin_double;
16848 break;
16849 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16850 case 128:
16851 tt = builtin_type (gdbarch)->builtin_long_double;
16852 break;
16853 }
8bdc1658
AB
16854 break;
16855 }
16856
35add35e
AB
16857 /* If the type we found doesn't match the size we were looking for, then
16858 pretend we didn't find a type at all, the complex target type we
16859 create will then be nameless. */
a12e5744 16860 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16861 tt = nullptr;
16862
8bdc1658 16863 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16864 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16865}
16866
c906108c
SS
16867/* Find a representation of a given base type and install
16868 it in the TYPE field of the die. */
16869
f792889a 16870static struct type *
e7c27a73 16871read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16872{
518817b3 16873 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16874 struct type *type;
16875 struct attribute *attr;
19f392bc 16876 int encoding = 0, bits = 0;
15d034d0 16877 const char *name;
34877895 16878 gdbarch *arch;
c906108c 16879
e142c38c 16880 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16881 if (attr != nullptr)
34877895 16882 encoding = DW_UNSND (attr);
e142c38c 16883 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16884 if (attr != nullptr)
34877895 16885 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16886 name = dwarf2_name (die, cu);
6ccb9162 16887 if (!name)
34877895 16888 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16889
16890 arch = get_objfile_arch (objfile);
16891 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16892
34877895
PJ
16893 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16894 if (attr)
103a685e
TT
16895 {
16896 int endianity = DW_UNSND (attr);
16897
16898 switch (endianity)
16899 {
16900 case DW_END_big:
16901 byte_order = BFD_ENDIAN_BIG;
16902 break;
16903 case DW_END_little:
16904 byte_order = BFD_ENDIAN_LITTLE;
16905 break;
16906 default:
16907 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16908 break;
16909 }
16910 }
6ccb9162
UW
16911
16912 switch (encoding)
c906108c 16913 {
6ccb9162
UW
16914 case DW_ATE_address:
16915 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16916 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16917 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16918 break;
16919 case DW_ATE_boolean:
19f392bc 16920 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16921 break;
16922 case DW_ATE_complex_float:
103a685e
TT
16923 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16924 byte_order);
19f392bc 16925 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16926 break;
16927 case DW_ATE_decimal_float:
19f392bc 16928 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16929 break;
16930 case DW_ATE_float:
103a685e 16931 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
16932 break;
16933 case DW_ATE_signed:
eb77c9df 16934 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16935 break;
16936 case DW_ATE_unsigned:
3b2b8fea
TT
16937 if (cu->language == language_fortran
16938 && name
61012eef 16939 && startswith (name, "character("))
19f392bc
UW
16940 type = init_character_type (objfile, bits, 1, name);
16941 else
eb77c9df 16942 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
16943 break;
16944 case DW_ATE_signed_char:
6e70227d 16945 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16946 || cu->language == language_pascal
16947 || cu->language == language_fortran)
19f392bc
UW
16948 type = init_character_type (objfile, bits, 0, name);
16949 else
eb77c9df 16950 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16951 break;
16952 case DW_ATE_unsigned_char:
868a0084 16953 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16954 || cu->language == language_pascal
c44af4eb
TT
16955 || cu->language == language_fortran
16956 || cu->language == language_rust)
19f392bc
UW
16957 type = init_character_type (objfile, bits, 1, name);
16958 else
eb77c9df 16959 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 16960 break;
75079b2b 16961 case DW_ATE_UTF:
53e710ac 16962 {
53e710ac
PA
16963 if (bits == 16)
16964 type = builtin_type (arch)->builtin_char16;
16965 else if (bits == 32)
16966 type = builtin_type (arch)->builtin_char32;
16967 else
16968 {
b98664d3 16969 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 16970 bits);
eb77c9df 16971 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
16972 }
16973 return set_die_type (die, type, cu);
16974 }
75079b2b
TT
16975 break;
16976
6ccb9162 16977 default:
b98664d3 16978 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 16979 dwarf_type_encoding_name (encoding));
77b7c781 16980 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 16981 break;
c906108c 16982 }
6ccb9162 16983
0114d602 16984 if (name && strcmp (name, "char") == 0)
876cecd0 16985 TYPE_NOSIGN (type) = 1;
0114d602 16986
2b4424c3
TT
16987 maybe_set_alignment (cu, die, type);
16988
103a685e 16989 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 16990
f792889a 16991 return set_die_type (die, type, cu);
c906108c
SS
16992}
16993
80180f79
SA
16994/* Parse dwarf attribute if it's a block, reference or constant and put the
16995 resulting value of the attribute into struct bound_prop.
16996 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16997
16998static int
16999attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17000 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17001 struct type *default_type)
80180f79
SA
17002{
17003 struct dwarf2_property_baton *baton;
518817b3
SM
17004 struct obstack *obstack
17005 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17006
9a49df9d
AB
17007 gdb_assert (default_type != NULL);
17008
80180f79
SA
17009 if (attr == NULL || prop == NULL)
17010 return 0;
17011
4fc6c0d5 17012 if (attr->form_is_block ())
80180f79 17013 {
8d749320 17014 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17015 baton->property_type = default_type;
80180f79
SA
17016 baton->locexpr.per_cu = cu->per_cu;
17017 baton->locexpr.size = DW_BLOCK (attr)->size;
17018 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17019 switch (attr->name)
17020 {
17021 case DW_AT_string_length:
17022 baton->locexpr.is_reference = true;
17023 break;
17024 default:
17025 baton->locexpr.is_reference = false;
17026 break;
17027 }
80180f79
SA
17028 prop->data.baton = baton;
17029 prop->kind = PROP_LOCEXPR;
17030 gdb_assert (prop->data.baton != NULL);
17031 }
cd6c91b4 17032 else if (attr->form_is_ref ())
80180f79
SA
17033 {
17034 struct dwarf2_cu *target_cu = cu;
17035 struct die_info *target_die;
17036 struct attribute *target_attr;
17037
17038 target_die = follow_die_ref (die, attr, &target_cu);
17039 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17040 if (target_attr == NULL)
17041 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17042 target_cu);
80180f79
SA
17043 if (target_attr == NULL)
17044 return 0;
17045
df25ebbd 17046 switch (target_attr->name)
80180f79 17047 {
df25ebbd 17048 case DW_AT_location:
cd6c91b4 17049 if (target_attr->form_is_section_offset ())
df25ebbd 17050 {
8d749320 17051 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17052 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17053 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17054 prop->data.baton = baton;
17055 prop->kind = PROP_LOCLIST;
17056 gdb_assert (prop->data.baton != NULL);
17057 }
4fc6c0d5 17058 else if (target_attr->form_is_block ())
df25ebbd 17059 {
8d749320 17060 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17061 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17062 baton->locexpr.per_cu = cu->per_cu;
17063 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17064 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17065 baton->locexpr.is_reference = true;
df25ebbd
JB
17066 prop->data.baton = baton;
17067 prop->kind = PROP_LOCEXPR;
17068 gdb_assert (prop->data.baton != NULL);
17069 }
17070 else
17071 {
17072 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17073 "dynamic property");
17074 return 0;
17075 }
17076 break;
17077 case DW_AT_data_member_location:
17078 {
17079 LONGEST offset;
17080
17081 if (!handle_data_member_location (target_die, target_cu,
17082 &offset))
17083 return 0;
17084
8d749320 17085 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17086 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17087 target_cu);
df25ebbd
JB
17088 baton->offset_info.offset = offset;
17089 baton->offset_info.type = die_type (target_die, target_cu);
17090 prop->data.baton = baton;
17091 prop->kind = PROP_ADDR_OFFSET;
17092 break;
17093 }
80180f79
SA
17094 }
17095 }
cd6c91b4 17096 else if (attr->form_is_constant ())
80180f79
SA
17097 {
17098 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17099 prop->kind = PROP_CONST;
17100 }
17101 else
17102 {
17103 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17104 dwarf2_name (die, cu));
17105 return 0;
17106 }
17107
17108 return 1;
17109}
17110
09ba997f 17111/* See read.h. */
9a49df9d 17112
09ba997f
TT
17113struct type *
17114dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17115{
09ba997f 17116 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17117 struct type *int_type;
17118
17119 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17120#define TRY_TYPE(F) \
17121 int_type = (unsigned_p \
17122 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17123 : objfile_type (objfile)->builtin_ ## F); \
17124 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17125 return int_type
17126
17127 TRY_TYPE (char);
17128 TRY_TYPE (short);
17129 TRY_TYPE (int);
17130 TRY_TYPE (long);
17131 TRY_TYPE (long_long);
17132
17133#undef TRY_TYPE
17134
17135 gdb_assert_not_reached ("unable to find suitable integer type");
17136}
17137
09ba997f 17138/* See read.h. */
11a8b164 17139
09ba997f
TT
17140struct type *
17141dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17142{
09ba997f
TT
17143 int addr_size = this->addr_size ();
17144 return int_type (addr_size, unsigned_p);
11a8b164
AB
17145}
17146
b86352cf
AB
17147/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17148 present (which is valid) then compute the default type based on the
17149 compilation units address size. */
17150
17151static struct type *
17152read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17153{
17154 struct type *index_type = die_type (die, cu);
17155
17156 /* Dwarf-2 specifications explicitly allows to create subrange types
17157 without specifying a base type.
17158 In that case, the base type must be set to the type of
17159 the lower bound, upper bound or count, in that order, if any of these
17160 three attributes references an object that has a type.
17161 If no base type is found, the Dwarf-2 specifications say that
17162 a signed integer type of size equal to the size of an address should
17163 be used.
17164 For the following C code: `extern char gdb_int [];'
17165 GCC produces an empty range DIE.
17166 FIXME: muller/2010-05-28: Possible references to object for low bound,
17167 high bound or count are not yet handled by this code. */
17168 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17169 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17170
17171 return index_type;
17172}
17173
a02abb62
JB
17174/* Read the given DW_AT_subrange DIE. */
17175
f792889a 17176static struct type *
a02abb62
JB
17177read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17178{
4c9ad8c2 17179 struct type *base_type, *orig_base_type;
a02abb62
JB
17180 struct type *range_type;
17181 struct attribute *attr;
729efb13 17182 struct dynamic_prop low, high;
4fae6e18 17183 int low_default_is_valid;
c451ebe5 17184 int high_bound_is_count = 0;
15d034d0 17185 const char *name;
d359392f 17186 ULONGEST negative_mask;
e77813c8 17187
b86352cf
AB
17188 orig_base_type = read_subrange_index_type (die, cu);
17189
4c9ad8c2
TT
17190 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17191 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17192 creating the range type, but we use the result of check_typedef
17193 when examining properties of the type. */
17194 base_type = check_typedef (orig_base_type);
a02abb62 17195
7e314c57
JK
17196 /* The die_type call above may have already set the type for this DIE. */
17197 range_type = get_die_type (die, cu);
17198 if (range_type)
17199 return range_type;
17200
729efb13
SA
17201 low.kind = PROP_CONST;
17202 high.kind = PROP_CONST;
17203 high.data.const_val = 0;
17204
4fae6e18
JK
17205 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17206 omitting DW_AT_lower_bound. */
17207 switch (cu->language)
6e70227d 17208 {
4fae6e18
JK
17209 case language_c:
17210 case language_cplus:
729efb13 17211 low.data.const_val = 0;
4fae6e18
JK
17212 low_default_is_valid = 1;
17213 break;
17214 case language_fortran:
729efb13 17215 low.data.const_val = 1;
4fae6e18
JK
17216 low_default_is_valid = 1;
17217 break;
17218 case language_d:
4fae6e18 17219 case language_objc:
c44af4eb 17220 case language_rust:
729efb13 17221 low.data.const_val = 0;
4fae6e18
JK
17222 low_default_is_valid = (cu->header.version >= 4);
17223 break;
17224 case language_ada:
17225 case language_m2:
17226 case language_pascal:
729efb13 17227 low.data.const_val = 1;
4fae6e18
JK
17228 low_default_is_valid = (cu->header.version >= 4);
17229 break;
17230 default:
729efb13 17231 low.data.const_val = 0;
4fae6e18
JK
17232 low_default_is_valid = 0;
17233 break;
a02abb62
JB
17234 }
17235
e142c38c 17236 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17237 if (attr != nullptr)
9a49df9d 17238 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17239 else if (!low_default_is_valid)
b98664d3 17240 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17241 "- DIE at %s [in module %s]"),
17242 sect_offset_str (die->sect_off),
518817b3 17243 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17244
506f5c41
TV
17245 struct attribute *attr_ub, *attr_count;
17246 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17247 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17248 {
506f5c41 17249 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17250 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17251 {
c451ebe5
SA
17252 /* If bounds are constant do the final calculation here. */
17253 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17254 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17255 else
17256 high_bound_is_count = 1;
c2ff108b 17257 }
506f5c41
TV
17258 else
17259 {
17260 if (attr_ub != NULL)
17261 complaint (_("Unresolved DW_AT_upper_bound "
17262 "- DIE at %s [in module %s]"),
17263 sect_offset_str (die->sect_off),
17264 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17265 if (attr_count != NULL)
17266 complaint (_("Unresolved DW_AT_count "
17267 "- DIE at %s [in module %s]"),
17268 sect_offset_str (die->sect_off),
17269 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17270 }
e77813c8 17271 }
a02abb62 17272
4e962e74
TT
17273 LONGEST bias = 0;
17274 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17275 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17276 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17277
dbb9c2b1
JB
17278 /* Normally, the DWARF producers are expected to use a signed
17279 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17280 But this is unfortunately not always the case, as witnessed
17281 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17282 is used instead. To work around that ambiguity, we treat
17283 the bounds as signed, and thus sign-extend their values, when
17284 the base type is signed. */
6e70227d 17285 negative_mask =
d359392f 17286 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17287 if (low.kind == PROP_CONST
17288 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17289 low.data.const_val |= negative_mask;
17290 if (high.kind == PROP_CONST
17291 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17292 high.data.const_val |= negative_mask;
43bbcdc2 17293
5bbd8269
AB
17294 /* Check for bit and byte strides. */
17295 struct dynamic_prop byte_stride_prop;
17296 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17297 if (attr_byte_stride != nullptr)
17298 {
09ba997f 17299 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17300 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17301 prop_type);
17302 }
17303
17304 struct dynamic_prop bit_stride_prop;
17305 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17306 if (attr_bit_stride != nullptr)
17307 {
17308 /* It only makes sense to have either a bit or byte stride. */
17309 if (attr_byte_stride != nullptr)
17310 {
17311 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17312 "- DIE at %s [in module %s]"),
17313 sect_offset_str (die->sect_off),
17314 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17315 attr_bit_stride = nullptr;
17316 }
17317 else
17318 {
09ba997f 17319 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17320 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17321 prop_type);
17322 }
17323 }
17324
17325 if (attr_byte_stride != nullptr
17326 || attr_bit_stride != nullptr)
17327 {
17328 bool byte_stride_p = (attr_byte_stride != nullptr);
17329 struct dynamic_prop *stride
17330 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17331
17332 range_type
17333 = create_range_type_with_stride (NULL, orig_base_type, &low,
17334 &high, bias, stride, byte_stride_p);
17335 }
17336 else
17337 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17338
c451ebe5
SA
17339 if (high_bound_is_count)
17340 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17341
c2ff108b
JK
17342 /* Ada expects an empty array on no boundary attributes. */
17343 if (attr == NULL && cu->language != language_ada)
729efb13 17344 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17345
39cbfefa
DJ
17346 name = dwarf2_name (die, cu);
17347 if (name)
17348 TYPE_NAME (range_type) = name;
6e70227d 17349
e142c38c 17350 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17351 if (attr != nullptr)
a02abb62
JB
17352 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17353
2b4424c3
TT
17354 maybe_set_alignment (cu, die, range_type);
17355
7e314c57
JK
17356 set_die_type (die, range_type, cu);
17357
17358 /* set_die_type should be already done. */
b4ba55a1
JB
17359 set_descriptive_type (range_type, die, cu);
17360
7e314c57 17361 return range_type;
a02abb62 17362}
6e70227d 17363
f792889a 17364static struct type *
81a17f79
JB
17365read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17366{
17367 struct type *type;
81a17f79 17368
518817b3
SM
17369 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17370 NULL);
0114d602 17371 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17372
74a2f8ff 17373 /* In Ada, an unspecified type is typically used when the description
85102364 17374 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17375 such a type, we treat it as a stub, and try to resolve it later on,
17376 when needed. */
17377 if (cu->language == language_ada)
17378 TYPE_STUB (type) = 1;
17379
f792889a 17380 return set_die_type (die, type, cu);
81a17f79 17381}
a02abb62 17382
639d11d3
DC
17383/* Read a single die and all its descendents. Set the die's sibling
17384 field to NULL; set other fields in the die correctly, and set all
17385 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17386 location of the info_ptr after reading all of those dies. PARENT
17387 is the parent of the die in question. */
17388
17389static struct die_info *
dee91e82 17390read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17391 const gdb_byte *info_ptr,
17392 const gdb_byte **new_info_ptr,
dee91e82 17393 struct die_info *parent)
639d11d3
DC
17394{
17395 struct die_info *die;
d521ce57 17396 const gdb_byte *cur_ptr;
639d11d3 17397
3e225074 17398 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17399 if (die == NULL)
17400 {
17401 *new_info_ptr = cur_ptr;
17402 return NULL;
17403 }
93311388 17404 store_in_ref_table (die, reader->cu);
639d11d3 17405
3e225074 17406 if (die->has_children)
bf6af496 17407 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17408 else
17409 {
17410 die->child = NULL;
17411 *new_info_ptr = cur_ptr;
17412 }
17413
17414 die->sibling = NULL;
17415 die->parent = parent;
17416 return die;
17417}
17418
17419/* Read a die, all of its descendents, and all of its siblings; set
17420 all of the fields of all of the dies correctly. Arguments are as
17421 in read_die_and_children. */
17422
17423static struct die_info *
bf6af496 17424read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17425 const gdb_byte *info_ptr,
17426 const gdb_byte **new_info_ptr,
bf6af496 17427 struct die_info *parent)
639d11d3
DC
17428{
17429 struct die_info *first_die, *last_sibling;
d521ce57 17430 const gdb_byte *cur_ptr;
639d11d3 17431
c906108c 17432 cur_ptr = info_ptr;
639d11d3
DC
17433 first_die = last_sibling = NULL;
17434
17435 while (1)
c906108c 17436 {
639d11d3 17437 struct die_info *die
dee91e82 17438 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17439
1d325ec1 17440 if (die == NULL)
c906108c 17441 {
639d11d3
DC
17442 *new_info_ptr = cur_ptr;
17443 return first_die;
c906108c 17444 }
1d325ec1
DJ
17445
17446 if (!first_die)
17447 first_die = die;
c906108c 17448 else
1d325ec1
DJ
17449 last_sibling->sibling = die;
17450
17451 last_sibling = die;
c906108c 17452 }
c906108c
SS
17453}
17454
bf6af496
DE
17455/* Read a die, all of its descendents, and all of its siblings; set
17456 all of the fields of all of the dies correctly. Arguments are as
17457 in read_die_and_children.
17458 This the main entry point for reading a DIE and all its children. */
17459
17460static struct die_info *
17461read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17462 const gdb_byte *info_ptr,
17463 const gdb_byte **new_info_ptr,
bf6af496
DE
17464 struct die_info *parent)
17465{
17466 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17467 new_info_ptr, parent);
17468
b4f54984 17469 if (dwarf_die_debug)
bf6af496
DE
17470 {
17471 fprintf_unfiltered (gdb_stdlog,
17472 "Read die from %s@0x%x of %s:\n",
96b79293 17473 reader->die_section->get_name (),
bf6af496
DE
17474 (unsigned) (info_ptr - reader->die_section->buffer),
17475 bfd_get_filename (reader->abfd));
b4f54984 17476 dump_die (die, dwarf_die_debug);
bf6af496
DE
17477 }
17478
17479 return die;
17480}
17481
3019eac3
DE
17482/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17483 attributes.
17484 The caller is responsible for filling in the extra attributes
17485 and updating (*DIEP)->num_attrs.
17486 Set DIEP to point to a newly allocated die with its information,
3e225074 17487 except for its child, sibling, and parent fields. */
93311388 17488
d521ce57 17489static const gdb_byte *
3019eac3 17490read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17491 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17492 int num_extra_attrs)
93311388 17493{
b64f50a1 17494 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17495 struct abbrev_info *abbrev;
17496 struct die_info *die;
17497 struct dwarf2_cu *cu = reader->cu;
17498 bfd *abfd = reader->abfd;
17499
9c541725 17500 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17501 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17502 info_ptr += bytes_read;
17503 if (!abbrev_number)
17504 {
17505 *diep = NULL;
93311388
DE
17506 return info_ptr;
17507 }
17508
685af9cd 17509 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17510 if (!abbrev)
348e048f
DE
17511 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17512 abbrev_number,
17513 bfd_get_filename (abfd));
17514
3019eac3 17515 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17516 die->sect_off = sect_off;
93311388
DE
17517 die->tag = abbrev->tag;
17518 die->abbrev = abbrev_number;
3e225074 17519 die->has_children = abbrev->has_children;
93311388 17520
3019eac3
DE
17521 /* Make the result usable.
17522 The caller needs to update num_attrs after adding the extra
17523 attributes. */
93311388
DE
17524 die->num_attrs = abbrev->num_attrs;
17525
18a8505e 17526 std::vector<int> indexes_that_need_reprocess;
93311388 17527 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17528 {
17529 bool need_reprocess;
17530 info_ptr =
17531 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17532 info_ptr, &need_reprocess);
17533 if (need_reprocess)
17534 indexes_that_need_reprocess.push_back (i);
17535 }
17536
052c8bb8 17537 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
17538 if (attr != nullptr)
17539 cu->str_offsets_base = DW_UNSND (attr);
93311388 17540
18a8505e
AT
17541 auto maybe_addr_base = lookup_addr_base(die);
17542 if (maybe_addr_base.has_value ())
17543 cu->addr_base = *maybe_addr_base;
17544 for (int index : indexes_that_need_reprocess)
17545 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17546 *diep = die;
93311388
DE
17547 return info_ptr;
17548}
17549
3019eac3
DE
17550/* Read a die and all its attributes.
17551 Set DIEP to point to a newly allocated die with its information,
3e225074 17552 except for its child, sibling, and parent fields. */
3019eac3 17553
d521ce57 17554static const gdb_byte *
3019eac3 17555read_full_die (const struct die_reader_specs *reader,
3e225074 17556 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17557{
d521ce57 17558 const gdb_byte *result;
bf6af496 17559
3e225074 17560 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17561
b4f54984 17562 if (dwarf_die_debug)
bf6af496
DE
17563 {
17564 fprintf_unfiltered (gdb_stdlog,
17565 "Read die from %s@0x%x of %s:\n",
96b79293 17566 reader->die_section->get_name (),
bf6af496
DE
17567 (unsigned) (info_ptr - reader->die_section->buffer),
17568 bfd_get_filename (reader->abfd));
b4f54984 17569 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17570 }
17571
17572 return result;
3019eac3 17573}
433df2d4 17574\f
c906108c 17575
72bf9492
DJ
17576/* Returns nonzero if TAG represents a type that we might generate a partial
17577 symbol for. */
17578
17579static int
17580is_type_tag_for_partial (int tag)
17581{
17582 switch (tag)
17583 {
17584#if 0
17585 /* Some types that would be reasonable to generate partial symbols for,
17586 that we don't at present. */
17587 case DW_TAG_array_type:
17588 case DW_TAG_file_type:
17589 case DW_TAG_ptr_to_member_type:
17590 case DW_TAG_set_type:
17591 case DW_TAG_string_type:
17592 case DW_TAG_subroutine_type:
17593#endif
17594 case DW_TAG_base_type:
17595 case DW_TAG_class_type:
680b30c7 17596 case DW_TAG_interface_type:
72bf9492
DJ
17597 case DW_TAG_enumeration_type:
17598 case DW_TAG_structure_type:
17599 case DW_TAG_subrange_type:
17600 case DW_TAG_typedef:
17601 case DW_TAG_union_type:
17602 return 1;
17603 default:
17604 return 0;
17605 }
17606}
17607
17608/* Load all DIEs that are interesting for partial symbols into memory. */
17609
17610static struct partial_die_info *
dee91e82 17611load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17612 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17613{
dee91e82 17614 struct dwarf2_cu *cu = reader->cu;
518817b3 17615 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17616 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17617 unsigned int bytes_read;
5afb4e99 17618 unsigned int load_all = 0;
72bf9492
DJ
17619 int nesting_level = 1;
17620
17621 parent_die = NULL;
17622 last_die = NULL;
17623
7adf1e79
DE
17624 gdb_assert (cu->per_cu != NULL);
17625 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17626 load_all = 1;
17627
72bf9492
DJ
17628 cu->partial_dies
17629 = htab_create_alloc_ex (cu->header.length / 12,
17630 partial_die_hash,
17631 partial_die_eq,
17632 NULL,
17633 &cu->comp_unit_obstack,
17634 hashtab_obstack_allocate,
17635 dummy_obstack_deallocate);
17636
72bf9492
DJ
17637 while (1)
17638 {
685af9cd 17639 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17640
17641 /* A NULL abbrev means the end of a series of children. */
17642 if (abbrev == NULL)
17643 {
17644 if (--nesting_level == 0)
cd9983dd
YQ
17645 return first_die;
17646
72bf9492
DJ
17647 info_ptr += bytes_read;
17648 last_die = parent_die;
17649 parent_die = parent_die->die_parent;
17650 continue;
17651 }
17652
98bfdba5
PA
17653 /* Check for template arguments. We never save these; if
17654 they're seen, we just mark the parent, and go on our way. */
17655 if (parent_die != NULL
17656 && cu->language == language_cplus
17657 && (abbrev->tag == DW_TAG_template_type_param
17658 || abbrev->tag == DW_TAG_template_value_param))
17659 {
17660 parent_die->has_template_arguments = 1;
17661
17662 if (!load_all)
17663 {
17664 /* We don't need a partial DIE for the template argument. */
dee91e82 17665 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17666 continue;
17667 }
17668 }
17669
0d99eb77 17670 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17671 Skip their other children. */
17672 if (!load_all
17673 && cu->language == language_cplus
17674 && parent_die != NULL
17675 && parent_die->tag == DW_TAG_subprogram)
17676 {
dee91e82 17677 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17678 continue;
17679 }
17680
5afb4e99
DJ
17681 /* Check whether this DIE is interesting enough to save. Normally
17682 we would not be interested in members here, but there may be
17683 later variables referencing them via DW_AT_specification (for
17684 static members). */
17685 if (!load_all
17686 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17687 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17688 && abbrev->tag != DW_TAG_enumerator
17689 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17690 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17691 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17692 && abbrev->tag != DW_TAG_variable
5afb4e99 17693 && abbrev->tag != DW_TAG_namespace
f55ee35c 17694 && abbrev->tag != DW_TAG_module
95554aad 17695 && abbrev->tag != DW_TAG_member
74921315
KS
17696 && abbrev->tag != DW_TAG_imported_unit
17697 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17698 {
17699 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17700 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17701 continue;
17702 }
17703
6f06d47b
YQ
17704 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17705 abbrev);
cd9983dd 17706
48fbe735 17707 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17708
17709 /* This two-pass algorithm for processing partial symbols has a
17710 high cost in cache pressure. Thus, handle some simple cases
17711 here which cover the majority of C partial symbols. DIEs
17712 which neither have specification tags in them, nor could have
17713 specification tags elsewhere pointing at them, can simply be
17714 processed and discarded.
17715
17716 This segment is also optional; scan_partial_symbols and
17717 add_partial_symbol will handle these DIEs if we chain
17718 them in normally. When compilers which do not emit large
17719 quantities of duplicate debug information are more common,
17720 this code can probably be removed. */
17721
17722 /* Any complete simple types at the top level (pretty much all
17723 of them, for a language without namespaces), can be processed
17724 directly. */
17725 if (parent_die == NULL
cd9983dd
YQ
17726 && pdi.has_specification == 0
17727 && pdi.is_declaration == 0
17728 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17729 || pdi.tag == DW_TAG_base_type
17730 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17731 {
cd9983dd 17732 if (building_psymtab && pdi.name != NULL)
31edb802 17733 add_psymbol_to_list (pdi.name, false,
79748972 17734 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17735 psymbol_placement::STATIC,
1762568f 17736 0, cu->language, objfile);
cd9983dd 17737 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17738 continue;
17739 }
17740
d8228535
JK
17741 /* The exception for DW_TAG_typedef with has_children above is
17742 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17743 type_name_or_error will error on such types later.
d8228535
JK
17744
17745 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17746 it could not find the child DIEs referenced later, this is checked
17747 above. In correct DWARF DW_TAG_typedef should have no children. */
17748
cd9983dd 17749 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17750 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17751 "- DIE at %s [in module %s]"),
cd9983dd 17752 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17753
72bf9492
DJ
17754 /* If we're at the second level, and we're an enumerator, and
17755 our parent has no specification (meaning possibly lives in a
17756 namespace elsewhere), then we can add the partial symbol now
17757 instead of queueing it. */
cd9983dd 17758 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17759 && parent_die != NULL
17760 && parent_die->die_parent == NULL
17761 && parent_die->tag == DW_TAG_enumeration_type
17762 && parent_die->has_specification == 0)
17763 {
cd9983dd 17764 if (pdi.name == NULL)
b98664d3 17765 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17766 else if (building_psymtab)
31edb802 17767 add_psymbol_to_list (pdi.name, false,
79748972 17768 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17769 cu->language == language_cplus
75aedd27
TT
17770 ? psymbol_placement::GLOBAL
17771 : psymbol_placement::STATIC,
1762568f 17772 0, cu->language, objfile);
72bf9492 17773
cd9983dd 17774 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17775 continue;
17776 }
17777
cd9983dd 17778 struct partial_die_info *part_die
6f06d47b 17779 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17780
72bf9492
DJ
17781 /* We'll save this DIE so link it in. */
17782 part_die->die_parent = parent_die;
17783 part_die->die_sibling = NULL;
17784 part_die->die_child = NULL;
17785
17786 if (last_die && last_die == parent_die)
17787 last_die->die_child = part_die;
17788 else if (last_die)
17789 last_die->die_sibling = part_die;
17790
17791 last_die = part_die;
17792
17793 if (first_die == NULL)
17794 first_die = part_die;
17795
17796 /* Maybe add the DIE to the hash table. Not all DIEs that we
17797 find interesting need to be in the hash table, because we
17798 also have the parent/sibling/child chains; only those that we
17799 might refer to by offset later during partial symbol reading.
17800
17801 For now this means things that might have be the target of a
17802 DW_AT_specification, DW_AT_abstract_origin, or
17803 DW_AT_extension. DW_AT_extension will refer only to
17804 namespaces; DW_AT_abstract_origin refers to functions (and
17805 many things under the function DIE, but we do not recurse
17806 into function DIEs during partial symbol reading) and
17807 possibly variables as well; DW_AT_specification refers to
17808 declarations. Declarations ought to have the DW_AT_declaration
17809 flag. It happens that GCC forgets to put it in sometimes, but
17810 only for functions, not for types.
17811
17812 Adding more things than necessary to the hash table is harmless
17813 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17814 wasted time in find_partial_die, when we reread the compilation
17815 unit with load_all_dies set. */
72bf9492 17816
5afb4e99 17817 if (load_all
72929c62 17818 || abbrev->tag == DW_TAG_constant
5afb4e99 17819 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17820 || abbrev->tag == DW_TAG_variable
17821 || abbrev->tag == DW_TAG_namespace
17822 || part_die->is_declaration)
17823 {
17824 void **slot;
17825
17826 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17827 to_underlying (part_die->sect_off),
17828 INSERT);
72bf9492
DJ
17829 *slot = part_die;
17830 }
17831
72bf9492 17832 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17833 we have no reason to follow the children of structures; for other
98bfdba5
PA
17834 languages we have to, so that we can get at method physnames
17835 to infer fully qualified class names, for DW_AT_specification,
17836 and for C++ template arguments. For C++, we also look one level
17837 inside functions to find template arguments (if the name of the
17838 function does not already contain the template arguments).
bc30ff58 17839
0a4b0913
AB
17840 For Ada and Fortran, we need to scan the children of subprograms
17841 and lexical blocks as well because these languages allow the
17842 definition of nested entities that could be interesting for the
17843 debugger, such as nested subprograms for instance. */
72bf9492 17844 if (last_die->has_children
5afb4e99
DJ
17845 && (load_all
17846 || last_die->tag == DW_TAG_namespace
f55ee35c 17847 || last_die->tag == DW_TAG_module
72bf9492 17848 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17849 || (cu->language == language_cplus
17850 && last_die->tag == DW_TAG_subprogram
17851 && (last_die->name == NULL
17852 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17853 || (cu->language != language_c
17854 && (last_die->tag == DW_TAG_class_type
680b30c7 17855 || last_die->tag == DW_TAG_interface_type
72bf9492 17856 || last_die->tag == DW_TAG_structure_type
bc30ff58 17857 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17858 || ((cu->language == language_ada
17859 || cu->language == language_fortran)
bc30ff58
JB
17860 && (last_die->tag == DW_TAG_subprogram
17861 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17862 {
17863 nesting_level++;
17864 parent_die = last_die;
17865 continue;
17866 }
17867
17868 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17869 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17870
17871 /* Back to the top, do it again. */
17872 }
17873}
17874
6f06d47b
YQ
17875partial_die_info::partial_die_info (sect_offset sect_off_,
17876 struct abbrev_info *abbrev)
17877 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17878{
17879}
17880
35cc7ed7
YQ
17881/* Read a minimal amount of information into the minimal die structure.
17882 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17883
48fbe735
YQ
17884const gdb_byte *
17885partial_die_info::read (const struct die_reader_specs *reader,
17886 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17887{
dee91e82 17888 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17889 struct dwarf2_per_objfile *dwarf2_per_objfile
17890 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17891 unsigned int i;
c5aa993b 17892 int has_low_pc_attr = 0;
c906108c 17893 int has_high_pc_attr = 0;
91da1414 17894 int high_pc_relative = 0;
c906108c 17895
18a8505e 17896 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 17897 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17898 {
18a8505e
AT
17899 bool need_reprocess;
17900 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17901 info_ptr, &need_reprocess);
17902 /* String and address offsets that need to do the reprocessing have
17903 already been read at this point, so there is no need to wait until
17904 the loop terminates to do the reprocessing. */
17905 if (need_reprocess)
17906 read_attribute_reprocess (reader, &attr_vec[i]);
17907 attribute &attr = attr_vec[i];
c906108c 17908 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17909 partial symbol table. */
c906108c
SS
17910 switch (attr.name)
17911 {
17912 case DW_AT_name:
48fbe735 17913 switch (tag)
71c25dea
TT
17914 {
17915 case DW_TAG_compile_unit:
95554aad 17916 case DW_TAG_partial_unit:
348e048f 17917 case DW_TAG_type_unit:
71c25dea
TT
17918 /* Compilation units have a DW_AT_name that is a filename, not
17919 a source language identifier. */
17920 case DW_TAG_enumeration_type:
17921 case DW_TAG_enumerator:
17922 /* These tags always have simple identifiers already; no need
17923 to canonicalize them. */
48fbe735 17924 name = DW_STRING (&attr);
71c25dea
TT
17925 break;
17926 default:
48fbe735
YQ
17927 {
17928 struct objfile *objfile = dwarf2_per_objfile->objfile;
17929
17930 name
be1e3d3e 17931 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 17932 }
71c25dea
TT
17933 break;
17934 }
c906108c 17935 break;
31ef98ae 17936 case DW_AT_linkage_name:
c906108c 17937 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17938 /* Note that both forms of linkage name might appear. We
17939 assume they will be the same, and we only store the last
17940 one we see. */
48fbe735 17941 linkage_name = DW_STRING (&attr);
c906108c
SS
17942 break;
17943 case DW_AT_low_pc:
17944 has_low_pc_attr = 1;
cd6c91b4 17945 lowpc = attr.value_as_address ();
c906108c
SS
17946 break;
17947 case DW_AT_high_pc:
17948 has_high_pc_attr = 1;
cd6c91b4
TT
17949 highpc = attr.value_as_address ();
17950 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 17951 high_pc_relative = 1;
c906108c
SS
17952 break;
17953 case DW_AT_location:
0963b4bd 17954 /* Support the .debug_loc offsets. */
4fc6c0d5 17955 if (attr.form_is_block ())
8e19ed76 17956 {
48fbe735 17957 d.locdesc = DW_BLOCK (&attr);
8e19ed76 17958 }
cd6c91b4 17959 else if (attr.form_is_section_offset ())
8e19ed76 17960 {
4d3c2250 17961 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
17962 }
17963 else
17964 {
4d3c2250
KB
17965 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17966 "partial symbol information");
8e19ed76 17967 }
c906108c 17968 break;
c906108c 17969 case DW_AT_external:
48fbe735 17970 is_external = DW_UNSND (&attr);
c906108c
SS
17971 break;
17972 case DW_AT_declaration:
48fbe735 17973 is_declaration = DW_UNSND (&attr);
c906108c
SS
17974 break;
17975 case DW_AT_type:
48fbe735 17976 has_type = 1;
c906108c
SS
17977 break;
17978 case DW_AT_abstract_origin:
17979 case DW_AT_specification:
72bf9492 17980 case DW_AT_extension:
48fbe735
YQ
17981 has_specification = 1;
17982 spec_offset = dwarf2_get_ref_die_offset (&attr);
17983 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 17984 || cu->per_cu->is_dwz);
c906108c
SS
17985 break;
17986 case DW_AT_sibling:
17987 /* Ignore absolute siblings, they might point outside of
17988 the current compile unit. */
17989 if (attr.form == DW_FORM_ref_addr)
b98664d3 17990 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 17991 else
b9502d3f 17992 {
48fbe735 17993 const gdb_byte *buffer = reader->buffer;
9c541725
PA
17994 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17995 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
17996
17997 if (sibling_ptr < info_ptr)
b98664d3 17998 complaint (_("DW_AT_sibling points backwards"));
22869d73 17999 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18000 reader->die_section->overflow_complaint ();
b9502d3f 18001 else
48fbe735 18002 sibling = sibling_ptr;
b9502d3f 18003 }
c906108c 18004 break;
fa4028e9 18005 case DW_AT_byte_size:
48fbe735 18006 has_byte_size = 1;
fa4028e9 18007 break;
ff908ebf 18008 case DW_AT_const_value:
48fbe735 18009 has_const_value = 1;
ff908ebf 18010 break;
68511cec
CES
18011 case DW_AT_calling_convention:
18012 /* DWARF doesn't provide a way to identify a program's source-level
18013 entry point. DW_AT_calling_convention attributes are only meant
18014 to describe functions' calling conventions.
18015
18016 However, because it's a necessary piece of information in
0c1b455e
TT
18017 Fortran, and before DWARF 4 DW_CC_program was the only
18018 piece of debugging information whose definition refers to
18019 a 'main program' at all, several compilers marked Fortran
18020 main programs with DW_CC_program --- even when those
18021 functions use the standard calling conventions.
18022
18023 Although DWARF now specifies a way to provide this
18024 information, we support this practice for backward
18025 compatibility. */
68511cec 18026 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18027 && cu->language == language_fortran)
48fbe735 18028 main_subprogram = 1;
68511cec 18029 break;
481860b3
GB
18030 case DW_AT_inline:
18031 if (DW_UNSND (&attr) == DW_INL_inlined
18032 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18033 may_be_inlined = 1;
481860b3 18034 break;
95554aad
TT
18035
18036 case DW_AT_import:
48fbe735 18037 if (tag == DW_TAG_imported_unit)
36586728 18038 {
48fbe735
YQ
18039 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18040 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18041 || cu->per_cu->is_dwz);
18042 }
95554aad
TT
18043 break;
18044
0c1b455e 18045 case DW_AT_main_subprogram:
48fbe735 18046 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18047 break;
18048
05caa1d2
TT
18049 case DW_AT_ranges:
18050 {
18051 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18052 but that requires a full DIE, so instead we just
18053 reimplement it. */
18054 int need_ranges_base = tag != DW_TAG_compile_unit;
18055 unsigned int ranges_offset = (DW_UNSND (&attr)
18056 + (need_ranges_base
18057 ? cu->ranges_base
18058 : 0));
18059
18060 /* Value of the DW_AT_ranges attribute is the offset in the
18061 .debug_ranges section. */
18062 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18063 nullptr))
18064 has_pc_info = 1;
18065 }
18066 break;
18067
c906108c
SS
18068 default:
18069 break;
18070 }
18071 }
18072
10d06d82
TT
18073 /* For Ada, if both the name and the linkage name appear, we prefer
18074 the latter. This lets "catch exception" work better, regardless
18075 of the order in which the name and linkage name were emitted.
18076 Really, though, this is just a workaround for the fact that gdb
18077 doesn't store both the name and the linkage name. */
18078 if (cu->language == language_ada && linkage_name != nullptr)
18079 name = linkage_name;
18080
91da1414 18081 if (high_pc_relative)
48fbe735 18082 highpc += lowpc;
91da1414 18083
9373cf26
JK
18084 if (has_low_pc_attr && has_high_pc_attr)
18085 {
18086 /* When using the GNU linker, .gnu.linkonce. sections are used to
18087 eliminate duplicate copies of functions and vtables and such.
18088 The linker will arbitrarily choose one and discard the others.
18089 The AT_*_pc values for such functions refer to local labels in
18090 these sections. If the section from that file was discarded, the
18091 labels are not in the output, so the relocs get a value of 0.
18092 If this is a discarded function, mark the pc bounds as invalid,
18093 so that GDB will ignore it. */
48fbe735 18094 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18095 {
48fbe735 18096 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18097 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18098
b98664d3 18099 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18100 "for DIE at %s [in module %s]"),
48fbe735
YQ
18101 paddress (gdbarch, lowpc),
18102 sect_offset_str (sect_off),
9d8780f0 18103 objfile_name (objfile));
9373cf26
JK
18104 }
18105 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18106 else if (lowpc >= highpc)
9373cf26 18107 {
48fbe735 18108 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18109 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18110
b98664d3 18111 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18112 "for DIE at %s [in module %s]"),
48fbe735
YQ
18113 paddress (gdbarch, lowpc),
18114 paddress (gdbarch, highpc),
18115 sect_offset_str (sect_off),
9c541725 18116 objfile_name (objfile));
9373cf26
JK
18117 }
18118 else
48fbe735 18119 has_pc_info = 1;
9373cf26 18120 }
85cbf3d3 18121
c906108c
SS
18122 return info_ptr;
18123}
18124
72bf9492
DJ
18125/* Find a cached partial DIE at OFFSET in CU. */
18126
d590ff25
YQ
18127struct partial_die_info *
18128dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18129{
18130 struct partial_die_info *lookup_die = NULL;
6f06d47b 18131 struct partial_die_info part_die (sect_off);
72bf9492 18132
9a3c8263 18133 lookup_die = ((struct partial_die_info *)
d590ff25 18134 htab_find_with_hash (partial_dies, &part_die,
9c541725 18135 to_underlying (sect_off)));
72bf9492 18136
72bf9492
DJ
18137 return lookup_die;
18138}
18139
348e048f
DE
18140/* Find a partial DIE at OFFSET, which may or may not be in CU,
18141 except in the case of .debug_types DIEs which do not reference
18142 outside their CU (they do however referencing other types via
55f1336d 18143 DW_FORM_ref_sig8). */
72bf9492 18144
122cf0f2 18145static const struct cu_partial_die_info
9c541725 18146find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18147{
518817b3
SM
18148 struct dwarf2_per_objfile *dwarf2_per_objfile
18149 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18150 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18151 struct dwarf2_per_cu_data *per_cu = NULL;
18152 struct partial_die_info *pd = NULL;
72bf9492 18153
36586728 18154 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18155 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18156 {
d590ff25 18157 pd = cu->find_partial_die (sect_off);
5afb4e99 18158 if (pd != NULL)
fb816e8b 18159 return { cu, pd };
0d99eb77
DE
18160 /* We missed recording what we needed.
18161 Load all dies and try again. */
18162 per_cu = cu->per_cu;
5afb4e99 18163 }
0d99eb77
DE
18164 else
18165 {
18166 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18167 if (cu->per_cu->is_debug_types)
0d99eb77 18168 {
9d8780f0
SM
18169 error (_("Dwarf Error: Type Unit at offset %s contains"
18170 " external reference to offset %s [in module %s].\n"),
18171 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18172 bfd_get_filename (objfile->obfd));
18173 }
9c541725 18174 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18175 dwarf2_per_objfile);
72bf9492 18176
0d99eb77
DE
18177 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18178 load_partial_comp_unit (per_cu);
ae038cb0 18179
0d99eb77 18180 per_cu->cu->last_used = 0;
d590ff25 18181 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18182 }
5afb4e99 18183
dee91e82
DE
18184 /* If we didn't find it, and not all dies have been loaded,
18185 load them all and try again. */
18186
5afb4e99
DJ
18187 if (pd == NULL && per_cu->load_all_dies == 0)
18188 {
5afb4e99 18189 per_cu->load_all_dies = 1;
fd820528
DE
18190
18191 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18192 THIS_CU->cu may already be in use. So we can't just free it and
18193 replace its DIEs with the ones we read in. Instead, we leave those
18194 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18195 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18196 set. */
dee91e82 18197 load_partial_comp_unit (per_cu);
5afb4e99 18198
d590ff25 18199 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18200 }
18201
18202 if (pd == NULL)
18203 internal_error (__FILE__, __LINE__,
9d8780f0 18204 _("could not find partial DIE %s "
3e43a32a 18205 "in cache [from module %s]\n"),
9d8780f0 18206 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18207 return { per_cu->cu, pd };
72bf9492
DJ
18208}
18209
abc72ce4
DE
18210/* See if we can figure out if the class lives in a namespace. We do
18211 this by looking for a member function; its demangled name will
18212 contain namespace info, if there is any. */
18213
18214static void
18215guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18216 struct dwarf2_cu *cu)
18217{
18218 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18219 what template types look like, because the demangler
18220 frequently doesn't give the same name as the debug info. We
18221 could fix this by only using the demangled name to get the
18222 prefix (but see comment in read_structure_type). */
18223
18224 struct partial_die_info *real_pdi;
18225 struct partial_die_info *child_pdi;
18226
18227 /* If this DIE (this DIE's specification, if any) has a parent, then
18228 we should not do this. We'll prepend the parent's fully qualified
18229 name when we create the partial symbol. */
18230
18231 real_pdi = struct_pdi;
18232 while (real_pdi->has_specification)
fb816e8b 18233 {
122cf0f2
AB
18234 auto res = find_partial_die (real_pdi->spec_offset,
18235 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18236 real_pdi = res.pdi;
18237 cu = res.cu;
18238 }
abc72ce4
DE
18239
18240 if (real_pdi->die_parent != NULL)
18241 return;
18242
18243 for (child_pdi = struct_pdi->die_child;
18244 child_pdi != NULL;
18245 child_pdi = child_pdi->die_sibling)
18246 {
18247 if (child_pdi->tag == DW_TAG_subprogram
18248 && child_pdi->linkage_name != NULL)
18249 {
43816ebc
TT
18250 gdb::unique_xmalloc_ptr<char> actual_class_name
18251 (language_class_name_from_physname (cu->language_defn,
18252 child_pdi->linkage_name));
abc72ce4
DE
18253 if (actual_class_name != NULL)
18254 {
518817b3 18255 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18256 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18257 }
18258 break;
18259 }
18260 }
18261}
18262
52356b79
YQ
18263void
18264partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18265{
abc72ce4
DE
18266 /* Once we've fixed up a die, there's no point in doing so again.
18267 This also avoids a memory leak if we were to call
18268 guess_partial_die_structure_name multiple times. */
52356b79 18269 if (fixup_called)
abc72ce4
DE
18270 return;
18271
72bf9492
DJ
18272 /* If we found a reference attribute and the DIE has no name, try
18273 to find a name in the referred to DIE. */
18274
52356b79 18275 if (name == NULL && has_specification)
72bf9492
DJ
18276 {
18277 struct partial_die_info *spec_die;
72bf9492 18278
122cf0f2 18279 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18280 spec_die = res.pdi;
18281 cu = res.cu;
72bf9492 18282
52356b79 18283 spec_die->fixup (cu);
72bf9492
DJ
18284
18285 if (spec_die->name)
18286 {
52356b79 18287 name = spec_die->name;
72bf9492
DJ
18288
18289 /* Copy DW_AT_external attribute if it is set. */
18290 if (spec_die->is_external)
52356b79 18291 is_external = spec_die->is_external;
72bf9492
DJ
18292 }
18293 }
18294
18295 /* Set default names for some unnamed DIEs. */
72bf9492 18296
52356b79
YQ
18297 if (name == NULL && tag == DW_TAG_namespace)
18298 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18299
abc72ce4
DE
18300 /* If there is no parent die to provide a namespace, and there are
18301 children, see if we can determine the namespace from their linkage
122d1940 18302 name. */
abc72ce4 18303 if (cu->language == language_cplus
fd5866f6 18304 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18305 && die_parent == NULL
18306 && has_children
18307 && (tag == DW_TAG_class_type
18308 || tag == DW_TAG_structure_type
18309 || tag == DW_TAG_union_type))
18310 guess_partial_die_structure_name (this, cu);
abc72ce4 18311
53832f31
TT
18312 /* GCC might emit a nameless struct or union that has a linkage
18313 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18314 if (name == NULL
18315 && (tag == DW_TAG_class_type
18316 || tag == DW_TAG_interface_type
18317 || tag == DW_TAG_structure_type
18318 || tag == DW_TAG_union_type)
18319 && linkage_name != NULL)
53832f31 18320 {
43816ebc
TT
18321 gdb::unique_xmalloc_ptr<char> demangled
18322 (gdb_demangle (linkage_name, DMGL_TYPES));
18323 if (demangled != nullptr)
53832f31 18324 {
96408a79
SA
18325 const char *base;
18326
18327 /* Strip any leading namespaces/classes, keep only the base name.
18328 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18329 base = strrchr (demangled.get (), ':');
18330 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18331 base++;
18332 else
43816ebc 18333 base = demangled.get ();
96408a79 18334
518817b3 18335 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18336 name = objfile->intern (base);
53832f31
TT
18337 }
18338 }
18339
52356b79 18340 fixup_called = 1;
72bf9492
DJ
18341}
18342
18a8505e
AT
18343/* Process the attributes that had to be skipped in the first round. These
18344 attributes are the ones that need str_offsets_base or addr_base attributes.
18345 They could not have been processed in the first round, because at the time
18346 the values of str_offsets_base or addr_base may not have been known. */
18347void read_attribute_reprocess (const struct die_reader_specs *reader,
18348 struct attribute *attr)
18349{
18350 struct dwarf2_cu *cu = reader->cu;
18351 switch (attr->form)
18352 {
18353 case DW_FORM_addrx:
18354 case DW_FORM_GNU_addr_index:
18355 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18356 break;
18357 case DW_FORM_strx:
18358 case DW_FORM_strx1:
18359 case DW_FORM_strx2:
18360 case DW_FORM_strx3:
18361 case DW_FORM_strx4:
18362 case DW_FORM_GNU_str_index:
18363 {
18364 unsigned int str_index = DW_UNSND (attr);
18365 if (reader->dwo_file != NULL)
18366 {
18367 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18368 DW_STRING_IS_CANONICAL (attr) = 0;
18369 }
18370 else
18371 {
18372 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18373 DW_STRING_IS_CANONICAL (attr) = 0;
18374 }
18375 break;
18376 }
18377 default:
18378 gdb_assert_not_reached (_("Unexpected DWARF form."));
18379 }
18380}
18381
a8329558 18382/* Read an attribute value described by an attribute form. */
c906108c 18383
d521ce57 18384static const gdb_byte *
dee91e82
DE
18385read_attribute_value (const struct die_reader_specs *reader,
18386 struct attribute *attr, unsigned form,
18a8505e
AT
18387 LONGEST implicit_const, const gdb_byte *info_ptr,
18388 bool *need_reprocess)
c906108c 18389{
dee91e82 18390 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18391 struct dwarf2_per_objfile *dwarf2_per_objfile
18392 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18393 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18394 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18395 bfd *abfd = reader->abfd;
e7c27a73 18396 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18397 unsigned int bytes_read;
18398 struct dwarf_block *blk;
18a8505e 18399 *need_reprocess = false;
c906108c 18400
aead7601 18401 attr->form = (enum dwarf_form) form;
a8329558 18402 switch (form)
c906108c 18403 {
c906108c 18404 case DW_FORM_ref_addr:
ae411497 18405 if (cu->header.version == 2)
c8a7a66f
TT
18406 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18407 &bytes_read);
ae411497 18408 else
8266302d
TT
18409 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18410 &bytes_read);
ae411497
TT
18411 info_ptr += bytes_read;
18412 break;
36586728 18413 case DW_FORM_GNU_ref_alt:
8266302d 18414 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18415 info_ptr += bytes_read;
18416 break;
ae411497 18417 case DW_FORM_addr:
c8a7a66f 18418 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18419 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18420 info_ptr += bytes_read;
c906108c
SS
18421 break;
18422 case DW_FORM_block2:
7b5a2f43 18423 blk = dwarf_alloc_block (cu);
c906108c
SS
18424 blk->size = read_2_bytes (abfd, info_ptr);
18425 info_ptr += 2;
18426 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18427 info_ptr += blk->size;
18428 DW_BLOCK (attr) = blk;
18429 break;
18430 case DW_FORM_block4:
7b5a2f43 18431 blk = dwarf_alloc_block (cu);
c906108c
SS
18432 blk->size = read_4_bytes (abfd, info_ptr);
18433 info_ptr += 4;
18434 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18435 info_ptr += blk->size;
18436 DW_BLOCK (attr) = blk;
18437 break;
18438 case DW_FORM_data2:
18439 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18440 info_ptr += 2;
18441 break;
18442 case DW_FORM_data4:
18443 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18444 info_ptr += 4;
18445 break;
18446 case DW_FORM_data8:
18447 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18448 info_ptr += 8;
18449 break;
0224619f
JK
18450 case DW_FORM_data16:
18451 blk = dwarf_alloc_block (cu);
18452 blk->size = 16;
18453 blk->data = read_n_bytes (abfd, info_ptr, 16);
18454 info_ptr += 16;
18455 DW_BLOCK (attr) = blk;
18456 break;
2dc7f7b3 18457 case DW_FORM_sec_offset:
8266302d 18458 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18459 info_ptr += bytes_read;
18460 break;
c906108c 18461 case DW_FORM_string:
9b1c24c8 18462 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18463 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18464 info_ptr += bytes_read;
18465 break;
4bdf3d34 18466 case DW_FORM_strp:
36586728
TT
18467 if (!cu->per_cu->is_dwz)
18468 {
ed2dc618
SM
18469 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18470 abfd, info_ptr, cu_header,
36586728
TT
18471 &bytes_read);
18472 DW_STRING_IS_CANONICAL (attr) = 0;
18473 info_ptr += bytes_read;
18474 break;
18475 }
18476 /* FALLTHROUGH */
43988095
JK
18477 case DW_FORM_line_strp:
18478 if (!cu->per_cu->is_dwz)
18479 {
86c0bb4c
TT
18480 DW_STRING (attr)
18481 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18482 &bytes_read);
43988095
JK
18483 DW_STRING_IS_CANONICAL (attr) = 0;
18484 info_ptr += bytes_read;
18485 break;
18486 }
18487 /* FALLTHROUGH */
36586728
TT
18488 case DW_FORM_GNU_strp_alt:
18489 {
ed2dc618 18490 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18491 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18492 &bytes_read);
36586728 18493
0314b390 18494 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
18495 DW_STRING_IS_CANONICAL (attr) = 0;
18496 info_ptr += bytes_read;
18497 }
4bdf3d34 18498 break;
2dc7f7b3 18499 case DW_FORM_exprloc:
c906108c 18500 case DW_FORM_block:
7b5a2f43 18501 blk = dwarf_alloc_block (cu);
c906108c
SS
18502 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18503 info_ptr += bytes_read;
18504 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18505 info_ptr += blk->size;
18506 DW_BLOCK (attr) = blk;
18507 break;
18508 case DW_FORM_block1:
7b5a2f43 18509 blk = dwarf_alloc_block (cu);
c906108c
SS
18510 blk->size = read_1_byte (abfd, info_ptr);
18511 info_ptr += 1;
18512 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18513 info_ptr += blk->size;
18514 DW_BLOCK (attr) = blk;
18515 break;
18516 case DW_FORM_data1:
18517 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18518 info_ptr += 1;
18519 break;
18520 case DW_FORM_flag:
18521 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18522 info_ptr += 1;
18523 break;
2dc7f7b3
TT
18524 case DW_FORM_flag_present:
18525 DW_UNSND (attr) = 1;
18526 break;
c906108c
SS
18527 case DW_FORM_sdata:
18528 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18529 info_ptr += bytes_read;
18530 break;
18531 case DW_FORM_udata:
18a8505e 18532 case DW_FORM_rnglistx:
c906108c
SS
18533 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18534 info_ptr += bytes_read;
18535 break;
18536 case DW_FORM_ref1:
9c541725 18537 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18538 + read_1_byte (abfd, info_ptr));
c906108c
SS
18539 info_ptr += 1;
18540 break;
18541 case DW_FORM_ref2:
9c541725 18542 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18543 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18544 info_ptr += 2;
18545 break;
18546 case DW_FORM_ref4:
9c541725 18547 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18548 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18549 info_ptr += 4;
18550 break;
613e1657 18551 case DW_FORM_ref8:
9c541725 18552 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18553 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18554 info_ptr += 8;
18555 break;
55f1336d 18556 case DW_FORM_ref_sig8:
ac9ec31b 18557 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18558 info_ptr += 8;
18559 break;
c906108c 18560 case DW_FORM_ref_udata:
9c541725 18561 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18562 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18563 info_ptr += bytes_read;
18564 break;
c906108c 18565 case DW_FORM_indirect:
a8329558
KW
18566 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18567 info_ptr += bytes_read;
43988095
JK
18568 if (form == DW_FORM_implicit_const)
18569 {
18570 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18571 info_ptr += bytes_read;
18572 }
18573 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18574 info_ptr, need_reprocess);
43988095
JK
18575 break;
18576 case DW_FORM_implicit_const:
18577 DW_SND (attr) = implicit_const;
a8329558 18578 break;
336d760d 18579 case DW_FORM_addrx:
3019eac3 18580 case DW_FORM_GNU_addr_index:
18a8505e
AT
18581 *need_reprocess = true;
18582 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18583 info_ptr += bytes_read;
18584 break;
cf532bd1 18585 case DW_FORM_strx:
15f18d14
AT
18586 case DW_FORM_strx1:
18587 case DW_FORM_strx2:
18588 case DW_FORM_strx3:
18589 case DW_FORM_strx4:
3019eac3 18590 case DW_FORM_GNU_str_index:
3019eac3 18591 {
15f18d14
AT
18592 ULONGEST str_index;
18593 if (form == DW_FORM_strx1)
18594 {
18595 str_index = read_1_byte (abfd, info_ptr);
18596 info_ptr += 1;
18597 }
18598 else if (form == DW_FORM_strx2)
18599 {
18600 str_index = read_2_bytes (abfd, info_ptr);
18601 info_ptr += 2;
18602 }
18603 else if (form == DW_FORM_strx3)
18604 {
18605 str_index = read_3_bytes (abfd, info_ptr);
18606 info_ptr += 3;
18607 }
18608 else if (form == DW_FORM_strx4)
18609 {
18610 str_index = read_4_bytes (abfd, info_ptr);
18611 info_ptr += 4;
18612 }
18613 else
18614 {
18615 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18616 info_ptr += bytes_read;
18617 }
18a8505e
AT
18618 *need_reprocess = true;
18619 DW_UNSND (attr) = str_index;
18620 }
3019eac3 18621 break;
c906108c 18622 default:
8a3fe4f8 18623 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18624 dwarf_form_name (form),
18625 bfd_get_filename (abfd));
c906108c 18626 }
28e94949 18627
36586728 18628 /* Super hack. */
cd6c91b4 18629 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18630 attr->form = DW_FORM_GNU_ref_alt;
18631
28e94949
JB
18632 /* We have seen instances where the compiler tried to emit a byte
18633 size attribute of -1 which ended up being encoded as an unsigned
18634 0xffffffff. Although 0xffffffff is technically a valid size value,
18635 an object of this size seems pretty unlikely so we can relatively
18636 safely treat these cases as if the size attribute was invalid and
18637 treat them as zero by default. */
18638 if (attr->name == DW_AT_byte_size
18639 && form == DW_FORM_data4
18640 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18641 {
18642 complaint
b98664d3 18643 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18644 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18645 DW_UNSND (attr) = 0;
18646 }
28e94949 18647
c906108c
SS
18648 return info_ptr;
18649}
18650
a8329558
KW
18651/* Read an attribute described by an abbreviated attribute. */
18652
d521ce57 18653static const gdb_byte *
dee91e82
DE
18654read_attribute (const struct die_reader_specs *reader,
18655 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18656 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18657{
18658 attr->name = abbrev->name;
43988095 18659 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18660 abbrev->implicit_const, info_ptr,
18661 need_reprocess);
a8329558
KW
18662}
18663
43988095
JK
18664/* Return pointer to string at .debug_str offset STR_OFFSET. */
18665
18666static const char *
ed2dc618 18667read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 18668 LONGEST str_offset)
43988095 18669{
4f44ae6c
TT
18670 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18671 str_offset, "DW_FORM_strp");
c906108c
SS
18672}
18673
43988095
JK
18674/* Return pointer to string at .debug_str offset as read from BUF.
18675 BUF is assumed to be in a compilation unit described by CU_HEADER.
18676 Return *BYTES_READ_PTR count of bytes read from BUF. */
18677
d521ce57 18678static const char *
ed2dc618
SM
18679read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18680 const gdb_byte *buf,
cf2c3c16
TT
18681 const struct comp_unit_head *cu_header,
18682 unsigned int *bytes_read_ptr)
18683{
8266302d 18684 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18685
4f44ae6c 18686 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
18687}
18688
86c0bb4c 18689/* See read.h. */
43988095 18690
86c0bb4c
TT
18691const char *
18692dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
43988095
JK
18693 const struct comp_unit_head *cu_header,
18694 unsigned int *bytes_read_ptr)
18695{
86c0bb4c 18696 bfd *abfd = objfile->obfd;
8266302d 18697 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18698
86c0bb4c 18699 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
18700}
18701
3019eac3 18702/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18703 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18704 ADDR_SIZE is the size of addresses from the CU header. */
18705
18706static CORE_ADDR
ed2dc618 18707read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18708 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18709 int addr_size)
3019eac3
DE
18710{
18711 struct objfile *objfile = dwarf2_per_objfile->objfile;
18712 bfd *abfd = objfile->obfd;
18713 const gdb_byte *info_ptr;
18a8505e 18714 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18715
96b79293 18716 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18717 if (dwarf2_per_objfile->addr.buffer == NULL)
18718 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18719 objfile_name (objfile));
18a8505e
AT
18720 if (addr_base_or_zero + addr_index * addr_size
18721 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18722 error (_("DW_FORM_addr_index pointing outside of "
18723 ".debug_addr section [in module %s]"),
4262abfb 18724 objfile_name (objfile));
3019eac3 18725 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18726 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18727 if (addr_size == 4)
18728 return bfd_get_32 (abfd, info_ptr);
18729 else
18730 return bfd_get_64 (abfd, info_ptr);
18731}
18732
18733/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18734
18735static CORE_ADDR
18736read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18737{
518817b3
SM
18738 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18739 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18740}
18741
18742/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18743
18744static CORE_ADDR
d521ce57 18745read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18746 unsigned int *bytes_read)
18747{
518817b3 18748 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18749 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18750
18751 return read_addr_index (cu, addr_index);
18752}
18753
450a1bfc 18754/* See read.h. */
3019eac3
DE
18755
18756CORE_ADDR
450a1bfc 18757dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18758{
ed2dc618 18759 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18760 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18761 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18762 int addr_size;
18763
3019eac3
DE
18764 /* We need addr_base and addr_size.
18765 If we don't have PER_CU->cu, we have to get it.
18766 Nasty, but the alternative is storing the needed info in PER_CU,
18767 which at this point doesn't seem justified: it's not clear how frequently
18768 it would get used and it would increase the size of every PER_CU.
18769 Entry points like dwarf2_per_cu_addr_size do a similar thing
18770 so we're not in uncharted territory here.
18771 Alas we need to be a bit more complicated as addr_base is contained
18772 in the DIE.
18773
18774 We don't need to read the entire CU(/TU).
18775 We just need the header and top level die.
a1b64ce1 18776
3019eac3 18777 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18778 For now we skip this optimization. */
3019eac3
DE
18779
18780 if (cu != NULL)
18781 {
18782 addr_base = cu->addr_base;
18783 addr_size = cu->header.addr_size;
18784 }
18785 else
18786 {
6751ebae 18787 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18788 addr_base = reader.cu->addr_base;
18789 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18790 }
18791
ed2dc618
SM
18792 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18793 addr_size);
3019eac3
DE
18794}
18795
18a8505e
AT
18796/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18797 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18798 DWO file. */
3019eac3 18799
d521ce57 18800static const char *
18a8505e
AT
18801read_str_index (struct dwarf2_cu *cu,
18802 struct dwarf2_section_info *str_section,
18803 struct dwarf2_section_info *str_offsets_section,
18804 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18805{
518817b3
SM
18806 struct dwarf2_per_objfile *dwarf2_per_objfile
18807 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18808 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18809 const char *objf_name = objfile_name (objfile);
3019eac3 18810 bfd *abfd = objfile->obfd;
d521ce57 18811 const gdb_byte *info_ptr;
3019eac3 18812 ULONGEST str_offset;
cf532bd1 18813 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18814
96b79293
TT
18815 str_section->read (objfile);
18816 str_offsets_section->read (objfile);
73869dc2 18817 if (str_section->buffer == NULL)
18a8505e 18818 error (_("%s used without %s section"
9d8780f0 18819 " in CU at offset %s [in module %s]"),
96b79293 18820 form_name, str_section->get_name (),
18a8505e 18821 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18822 if (str_offsets_section->buffer == NULL)
18a8505e 18823 error (_("%s used without %s section"
9d8780f0 18824 " in CU at offset %s [in module %s]"),
96b79293 18825 form_name, str_section->get_name (),
18a8505e 18826 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18827 info_ptr = (str_offsets_section->buffer
18a8505e 18828 + str_offsets_base
3019eac3
DE
18829 + str_index * cu->header.offset_size);
18830 if (cu->header.offset_size == 4)
18831 str_offset = bfd_get_32 (abfd, info_ptr);
18832 else
18833 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18834 if (str_offset >= str_section->size)
57d63ce2 18835 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18836 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18837 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18838 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18839}
18840
18a8505e
AT
18841/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18842
18843static const char *
18844read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18845{
18846 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18847 ? reader->cu->header.addr_size : 0;
18848 return read_str_index (reader->cu,
18849 &reader->dwo_file->sections.str,
18850 &reader->dwo_file->sections.str_offsets,
18851 str_offsets_base, str_index);
18852}
18853
18854/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18855
18856static const char *
18857read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18858{
18859 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18860 const char *objf_name = objfile_name (objfile);
18861 static const char form_name[] = "DW_FORM_GNU_str_index";
18862 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18863
18864 if (!cu->str_offsets_base.has_value ())
18865 error (_("%s used in Fission stub without %s"
18866 " in CU at offset 0x%lx [in module %s]"),
18867 form_name, str_offsets_attr_name,
18868 (long) cu->header.offset_size, objf_name);
18869
18870 return read_str_index (cu,
18871 &cu->per_cu->dwarf2_per_objfile->str,
18872 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18873 *cu->str_offsets_base, str_index);
18874}
18875
3019eac3
DE
18876/* Return the length of an LEB128 number in BUF. */
18877
18878static int
18879leb128_size (const gdb_byte *buf)
18880{
18881 const gdb_byte *begin = buf;
18882 gdb_byte byte;
18883
18884 while (1)
18885 {
18886 byte = *buf++;
18887 if ((byte & 128) == 0)
18888 return buf - begin;
18889 }
18890}
18891
c906108c 18892static void
e142c38c 18893set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
18894{
18895 switch (lang)
18896 {
18897 case DW_LANG_C89:
76bee0cc 18898 case DW_LANG_C99:
0cfd832f 18899 case DW_LANG_C11:
c906108c 18900 case DW_LANG_C:
d1be3247 18901 case DW_LANG_UPC:
e142c38c 18902 cu->language = language_c;
c906108c 18903 break;
9c37b5ae 18904 case DW_LANG_Java:
c906108c 18905 case DW_LANG_C_plus_plus:
0cfd832f
MW
18906 case DW_LANG_C_plus_plus_11:
18907 case DW_LANG_C_plus_plus_14:
e142c38c 18908 cu->language = language_cplus;
c906108c 18909 break;
6aecb9c2
JB
18910 case DW_LANG_D:
18911 cu->language = language_d;
18912 break;
c906108c
SS
18913 case DW_LANG_Fortran77:
18914 case DW_LANG_Fortran90:
b21b22e0 18915 case DW_LANG_Fortran95:
f7de9aab
MW
18916 case DW_LANG_Fortran03:
18917 case DW_LANG_Fortran08:
e142c38c 18918 cu->language = language_fortran;
c906108c 18919 break;
a766d390
DE
18920 case DW_LANG_Go:
18921 cu->language = language_go;
18922 break;
c906108c 18923 case DW_LANG_Mips_Assembler:
e142c38c 18924 cu->language = language_asm;
c906108c
SS
18925 break;
18926 case DW_LANG_Ada83:
8aaf0b47 18927 case DW_LANG_Ada95:
bc5f45f8
JB
18928 cu->language = language_ada;
18929 break;
72019c9c
GM
18930 case DW_LANG_Modula2:
18931 cu->language = language_m2;
18932 break;
fe8e67fd
PM
18933 case DW_LANG_Pascal83:
18934 cu->language = language_pascal;
18935 break;
22566fbd
DJ
18936 case DW_LANG_ObjC:
18937 cu->language = language_objc;
18938 break;
c44af4eb
TT
18939 case DW_LANG_Rust:
18940 case DW_LANG_Rust_old:
18941 cu->language = language_rust;
18942 break;
c906108c
SS
18943 case DW_LANG_Cobol74:
18944 case DW_LANG_Cobol85:
c906108c 18945 default:
e142c38c 18946 cu->language = language_minimal;
c906108c
SS
18947 break;
18948 }
e142c38c 18949 cu->language_defn = language_def (cu->language);
c906108c
SS
18950}
18951
18952/* Return the named attribute or NULL if not there. */
18953
18954static struct attribute *
e142c38c 18955dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 18956{
a48e046c 18957 for (;;)
c906108c 18958 {
a48e046c
TT
18959 unsigned int i;
18960 struct attribute *spec = NULL;
18961
18962 for (i = 0; i < die->num_attrs; ++i)
18963 {
18964 if (die->attrs[i].name == name)
18965 return &die->attrs[i];
18966 if (die->attrs[i].name == DW_AT_specification
18967 || die->attrs[i].name == DW_AT_abstract_origin)
18968 spec = &die->attrs[i];
18969 }
18970
18971 if (!spec)
18972 break;
c906108c 18973
f2f0e013 18974 die = follow_die_ref (die, spec, &cu);
f2f0e013 18975 }
c5aa993b 18976
c906108c
SS
18977 return NULL;
18978}
18979
7d45c7c3
KB
18980/* Return the string associated with a string-typed attribute, or NULL if it
18981 is either not found or is of an incorrect type. */
18982
18983static const char *
18984dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18985{
18986 struct attribute *attr;
18987 const char *str = NULL;
18988
18989 attr = dwarf2_attr (die, name, cu);
18990
18991 if (attr != NULL)
18992 {
43988095 18993 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 18994 || attr->form == DW_FORM_string
cf532bd1 18995 || attr->form == DW_FORM_strx
8fe0f950
AT
18996 || attr->form == DW_FORM_strx1
18997 || attr->form == DW_FORM_strx2
18998 || attr->form == DW_FORM_strx3
18999 || attr->form == DW_FORM_strx4
b3340438 19000 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19001 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19002 str = DW_STRING (attr);
19003 else
b98664d3 19004 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19005 "DIE at %s in module %s"),
19006 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19007 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19008 }
19009
19010 return str;
19011}
19012
a084a2a6 19013/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19014 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19015static const char *
19016dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19017{
19018 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19019 if (dwo_name == nullptr)
19020 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19021 return dwo_name;
19022}
19023
05cf31d1
JB
19024/* Return non-zero iff the attribute NAME is defined for the given DIE,
19025 and holds a non-zero value. This function should only be used for
2dc7f7b3 19026 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19027
19028static int
19029dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19030{
19031 struct attribute *attr = dwarf2_attr (die, name, cu);
19032
19033 return (attr && DW_UNSND (attr));
19034}
19035
3ca72b44 19036static int
e142c38c 19037die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19038{
05cf31d1
JB
19039 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19040 which value is non-zero. However, we have to be careful with
19041 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19042 (via dwarf2_flag_true_p) follows this attribute. So we may
19043 end up accidently finding a declaration attribute that belongs
19044 to a different DIE referenced by the specification attribute,
19045 even though the given DIE does not have a declaration attribute. */
19046 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19047 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19048}
19049
63d06c5c 19050/* Return the die giving the specification for DIE, if there is
f2f0e013 19051 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19052 containing the return value on output. If there is no
19053 specification, but there is an abstract origin, that is
19054 returned. */
63d06c5c
DC
19055
19056static struct die_info *
f2f0e013 19057die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19058{
f2f0e013
DJ
19059 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19060 *spec_cu);
63d06c5c 19061
edb3359d
DJ
19062 if (spec_attr == NULL)
19063 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19064
63d06c5c
DC
19065 if (spec_attr == NULL)
19066 return NULL;
19067 else
f2f0e013 19068 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19069}
c906108c 19070
527f3840
JK
19071/* Stub for free_line_header to match void * callback types. */
19072
19073static void
19074free_line_header_voidp (void *arg)
19075{
9a3c8263 19076 struct line_header *lh = (struct line_header *) arg;
527f3840 19077
fff8551c 19078 delete lh;
527f3840
JK
19079}
19080
83769d0b 19081/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19082
19083static struct dwarf2_section_info *
19084get_debug_line_section (struct dwarf2_cu *cu)
19085{
19086 struct dwarf2_section_info *section;
518817b3
SM
19087 struct dwarf2_per_objfile *dwarf2_per_objfile
19088 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19089
19090 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19091 DWO file. */
19092 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19093 section = &cu->dwo_unit->dwo_file->sections.line;
19094 else if (cu->per_cu->is_dwz)
19095 {
ed2dc618 19096 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19097
19098 section = &dwz->line;
19099 }
19100 else
19101 section = &dwarf2_per_objfile->line;
19102
19103 return section;
19104}
19105
debd256d 19106/* Read the statement program header starting at OFFSET in
3019eac3 19107 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19108 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19109 Returns NULL if there is a problem reading the header, e.g., if it
19110 has a version we don't understand.
debd256d
JB
19111
19112 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19113 the returned object point into the dwarf line section buffer,
19114 and must not be freed. */
ae2de4f8 19115
fff8551c 19116static line_header_up
9c541725 19117dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19118{
3019eac3 19119 struct dwarf2_section_info *section;
518817b3
SM
19120 struct dwarf2_per_objfile *dwarf2_per_objfile
19121 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19122
36586728 19123 section = get_debug_line_section (cu);
96b79293 19124 section->read (dwarf2_per_objfile->objfile);
3019eac3 19125 if (section->buffer == NULL)
debd256d 19126 {
3019eac3 19127 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19128 complaint (_("missing .debug_line.dwo section"));
3019eac3 19129 else
b98664d3 19130 complaint (_("missing .debug_line section"));
debd256d
JB
19131 return 0;
19132 }
19133
0df7ad3a
TT
19134 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19135 dwarf2_per_objfile, section,
19136 &cu->header);
debd256d 19137}
c906108c 19138
c6da4cef 19139/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19140 Return the file name of the psymtab for the given file_entry.
c6da4cef 19141 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19142 If space for the result is malloc'd, *NAME_HOLDER will be set.
19143 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19144
d521ce57 19145static const char *
7ba99d21 19146psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19147 const dwarf2_psymtab *pst,
c89b44cd
TT
19148 const char *comp_dir,
19149 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19150{
d521ce57
TT
19151 const char *include_name = fe.name;
19152 const char *include_name_to_compare = include_name;
72b9f47f 19153 const char *pst_filename;
c6da4cef
DE
19154 int file_is_pst;
19155
8c43009f 19156 const char *dir_name = fe.include_dir (lh);
c6da4cef 19157
c89b44cd 19158 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19159 if (!IS_ABSOLUTE_PATH (include_name)
19160 && (dir_name != NULL || comp_dir != NULL))
19161 {
19162 /* Avoid creating a duplicate psymtab for PST.
19163 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19164 Before we do the comparison, however, we need to account
19165 for DIR_NAME and COMP_DIR.
19166 First prepend dir_name (if non-NULL). If we still don't
19167 have an absolute path prepend comp_dir (if non-NULL).
19168 However, the directory we record in the include-file's
19169 psymtab does not contain COMP_DIR (to match the
19170 corresponding symtab(s)).
19171
19172 Example:
19173
19174 bash$ cd /tmp
19175 bash$ gcc -g ./hello.c
19176 include_name = "hello.c"
19177 dir_name = "."
19178 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19179 DW_AT_name = "./hello.c"
19180
19181 */
c6da4cef
DE
19182
19183 if (dir_name != NULL)
19184 {
c89b44cd
TT
19185 name_holder->reset (concat (dir_name, SLASH_STRING,
19186 include_name, (char *) NULL));
19187 include_name = name_holder->get ();
c6da4cef 19188 include_name_to_compare = include_name;
c6da4cef
DE
19189 }
19190 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19191 {
c89b44cd
TT
19192 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19193 include_name, (char *) NULL));
19194 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19195 }
19196 }
19197
19198 pst_filename = pst->filename;
c89b44cd 19199 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19200 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19201 {
c89b44cd
TT
19202 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19203 pst_filename, (char *) NULL));
19204 pst_filename = copied_name.get ();
c6da4cef
DE
19205 }
19206
1e3fad37 19207 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19208
c6da4cef
DE
19209 if (file_is_pst)
19210 return NULL;
19211 return include_name;
19212}
19213
d9b3de22
DE
19214/* State machine to track the state of the line number program. */
19215
6f77053d 19216class lnp_state_machine
d9b3de22 19217{
6f77053d
PA
19218public:
19219 /* Initialize a machine state for the start of a line number
19220 program. */
804d2729
TT
19221 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19222 bool record_lines_p);
6f77053d 19223
8c43009f
PA
19224 file_entry *current_file ()
19225 {
19226 /* lh->file_names is 0-based, but the file name numbers in the
19227 statement program are 1-based. */
6f77053d
PA
19228 return m_line_header->file_name_at (m_file);
19229 }
19230
19231 /* Record the line in the state machine. END_SEQUENCE is true if
19232 we're processing the end of a sequence. */
19233 void record_line (bool end_sequence);
19234
7ab6656f
OJ
19235 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19236 nop-out rest of the lines in this sequence. */
6f77053d
PA
19237 void check_line_address (struct dwarf2_cu *cu,
19238 const gdb_byte *line_ptr,
7ab6656f 19239 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19240
19241 void handle_set_discriminator (unsigned int discriminator)
19242 {
19243 m_discriminator = discriminator;
19244 m_line_has_non_zero_discriminator |= discriminator != 0;
19245 }
19246
19247 /* Handle DW_LNE_set_address. */
19248 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19249 {
19250 m_op_index = 0;
19251 address += baseaddr;
19252 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19253 }
19254
19255 /* Handle DW_LNS_advance_pc. */
19256 void handle_advance_pc (CORE_ADDR adjust);
19257
19258 /* Handle a special opcode. */
19259 void handle_special_opcode (unsigned char op_code);
19260
19261 /* Handle DW_LNS_advance_line. */
19262 void handle_advance_line (int line_delta)
19263 {
19264 advance_line (line_delta);
19265 }
19266
19267 /* Handle DW_LNS_set_file. */
19268 void handle_set_file (file_name_index file);
19269
19270 /* Handle DW_LNS_negate_stmt. */
19271 void handle_negate_stmt ()
19272 {
19273 m_is_stmt = !m_is_stmt;
19274 }
19275
19276 /* Handle DW_LNS_const_add_pc. */
19277 void handle_const_add_pc ();
19278
19279 /* Handle DW_LNS_fixed_advance_pc. */
19280 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19281 {
19282 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19283 m_op_index = 0;
19284 }
19285
19286 /* Handle DW_LNS_copy. */
19287 void handle_copy ()
19288 {
19289 record_line (false);
19290 m_discriminator = 0;
19291 }
19292
19293 /* Handle DW_LNE_end_sequence. */
19294 void handle_end_sequence ()
19295 {
804d2729 19296 m_currently_recording_lines = true;
6f77053d
PA
19297 }
19298
19299private:
19300 /* Advance the line by LINE_DELTA. */
19301 void advance_line (int line_delta)
19302 {
19303 m_line += line_delta;
19304
19305 if (line_delta != 0)
19306 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19307 }
19308
804d2729
TT
19309 struct dwarf2_cu *m_cu;
19310
6f77053d
PA
19311 gdbarch *m_gdbarch;
19312
19313 /* True if we're recording lines.
19314 Otherwise we're building partial symtabs and are just interested in
19315 finding include files mentioned by the line number program. */
19316 bool m_record_lines_p;
19317
8c43009f 19318 /* The line number header. */
6f77053d 19319 line_header *m_line_header;
8c43009f 19320
6f77053d
PA
19321 /* These are part of the standard DWARF line number state machine,
19322 and initialized according to the DWARF spec. */
d9b3de22 19323
6f77053d 19324 unsigned char m_op_index = 0;
7ba99d21
AT
19325 /* The line table index of the current file. */
19326 file_name_index m_file = 1;
6f77053d
PA
19327 unsigned int m_line = 1;
19328
19329 /* These are initialized in the constructor. */
19330
19331 CORE_ADDR m_address;
19332 bool m_is_stmt;
19333 unsigned int m_discriminator;
d9b3de22
DE
19334
19335 /* Additional bits of state we need to track. */
19336
19337 /* The last file that we called dwarf2_start_subfile for.
19338 This is only used for TLLs. */
6f77053d 19339 unsigned int m_last_file = 0;
d9b3de22 19340 /* The last file a line number was recorded for. */
6f77053d 19341 struct subfile *m_last_subfile = NULL;
d9b3de22 19342
804d2729
TT
19343 /* When true, record the lines we decode. */
19344 bool m_currently_recording_lines = false;
d9b3de22
DE
19345
19346 /* The last line number that was recorded, used to coalesce
19347 consecutive entries for the same line. This can happen, for
19348 example, when discriminators are present. PR 17276. */
6f77053d
PA
19349 unsigned int m_last_line = 0;
19350 bool m_line_has_non_zero_discriminator = false;
8c43009f 19351};
d9b3de22 19352
6f77053d
PA
19353void
19354lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19355{
19356 CORE_ADDR addr_adj = (((m_op_index + adjust)
19357 / m_line_header->maximum_ops_per_instruction)
19358 * m_line_header->minimum_instruction_length);
19359 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19360 m_op_index = ((m_op_index + adjust)
19361 % m_line_header->maximum_ops_per_instruction);
19362}
d9b3de22 19363
6f77053d
PA
19364void
19365lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19366{
6f77053d 19367 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19368 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19369 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19370 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19371 / m_line_header->maximum_ops_per_instruction)
19372 * m_line_header->minimum_instruction_length);
19373 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19374 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19375 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19376
258bf0ee 19377 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19378 advance_line (line_delta);
19379 record_line (false);
19380 m_discriminator = 0;
19381}
d9b3de22 19382
6f77053d
PA
19383void
19384lnp_state_machine::handle_set_file (file_name_index file)
19385{
19386 m_file = file;
19387
19388 const file_entry *fe = current_file ();
19389 if (fe == NULL)
19390 dwarf2_debug_line_missing_file_complaint ();
19391 else if (m_record_lines_p)
19392 {
19393 const char *dir = fe->include_dir (m_line_header);
19394
c24bdb02 19395 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19396 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19397 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19398 }
19399}
19400
19401void
19402lnp_state_machine::handle_const_add_pc ()
19403{
19404 CORE_ADDR adjust
19405 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19406
19407 CORE_ADDR addr_adj
19408 = (((m_op_index + adjust)
19409 / m_line_header->maximum_ops_per_instruction)
19410 * m_line_header->minimum_instruction_length);
19411
19412 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19413 m_op_index = ((m_op_index + adjust)
19414 % m_line_header->maximum_ops_per_instruction);
19415}
d9b3de22 19416
a05a36a5
DE
19417/* Return non-zero if we should add LINE to the line number table.
19418 LINE is the line to add, LAST_LINE is the last line that was added,
19419 LAST_SUBFILE is the subfile for LAST_LINE.
19420 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19421 had a non-zero discriminator.
19422
19423 We have to be careful in the presence of discriminators.
19424 E.g., for this line:
19425
19426 for (i = 0; i < 100000; i++);
19427
19428 clang can emit four line number entries for that one line,
19429 each with a different discriminator.
19430 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19431
19432 However, we want gdb to coalesce all four entries into one.
19433 Otherwise the user could stepi into the middle of the line and
19434 gdb would get confused about whether the pc really was in the
19435 middle of the line.
19436
19437 Things are further complicated by the fact that two consecutive
19438 line number entries for the same line is a heuristic used by gcc
19439 to denote the end of the prologue. So we can't just discard duplicate
19440 entries, we have to be selective about it. The heuristic we use is
19441 that we only collapse consecutive entries for the same line if at least
19442 one of those entries has a non-zero discriminator. PR 17276.
19443
19444 Note: Addresses in the line number state machine can never go backwards
19445 within one sequence, thus this coalescing is ok. */
19446
19447static int
804d2729
TT
19448dwarf_record_line_p (struct dwarf2_cu *cu,
19449 unsigned int line, unsigned int last_line,
a05a36a5
DE
19450 int line_has_non_zero_discriminator,
19451 struct subfile *last_subfile)
19452{
c24bdb02 19453 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19454 return 1;
19455 if (line != last_line)
19456 return 1;
19457 /* Same line for the same file that we've seen already.
19458 As a last check, for pr 17276, only record the line if the line
19459 has never had a non-zero discriminator. */
19460 if (!line_has_non_zero_discriminator)
19461 return 1;
19462 return 0;
19463}
19464
804d2729
TT
19465/* Use the CU's builder to record line number LINE beginning at
19466 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19467
19468static void
d9b3de22 19469dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 19470 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 19471 struct dwarf2_cu *cu)
252a6764
DE
19472{
19473 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19474
27e0867f
DE
19475 if (dwarf_line_debug)
19476 {
19477 fprintf_unfiltered (gdb_stdlog,
19478 "Recording line %u, file %s, address %s\n",
19479 line, lbasename (subfile->name),
19480 paddress (gdbarch, address));
19481 }
19482
804d2729 19483 if (cu != nullptr)
8c95582d 19484 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
19485}
19486
19487/* Subroutine of dwarf_decode_lines_1 to simplify it.
19488 Mark the end of a set of line number records.
d9b3de22 19489 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19490 If SUBFILE is NULL the request is ignored. */
19491
19492static void
19493dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19494 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19495{
27e0867f
DE
19496 if (subfile == NULL)
19497 return;
19498
19499 if (dwarf_line_debug)
19500 {
19501 fprintf_unfiltered (gdb_stdlog,
19502 "Finishing current line, file %s, address %s\n",
19503 lbasename (subfile->name),
19504 paddress (gdbarch, address));
19505 }
19506
8c95582d 19507 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
19508}
19509
6f77053d
PA
19510void
19511lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19512{
d9b3de22
DE
19513 if (dwarf_line_debug)
19514 {
19515 fprintf_unfiltered (gdb_stdlog,
19516 "Processing actual line %u: file %u,"
94a72be7 19517 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19518 m_line, m_file,
6f77053d 19519 paddress (m_gdbarch, m_address),
94a72be7
AB
19520 m_is_stmt, m_discriminator,
19521 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19522 }
19523
6f77053d 19524 file_entry *fe = current_file ();
8c43009f
PA
19525
19526 if (fe == NULL)
d9b3de22
DE
19527 dwarf2_debug_line_missing_file_complaint ();
19528 /* For now we ignore lines not starting on an instruction boundary.
19529 But not when processing end_sequence for compatibility with the
19530 previous version of the code. */
6f77053d 19531 else if (m_op_index == 0 || end_sequence)
d9b3de22 19532 {
8c43009f 19533 fe->included_p = 1;
8c95582d 19534 if (m_record_lines_p)
d9b3de22 19535 {
c24bdb02 19536 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 19537 || end_sequence)
d9b3de22 19538 {
804d2729
TT
19539 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19540 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
19541 }
19542
19543 if (!end_sequence)
19544 {
8c95582d
AB
19545 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19546
804d2729 19547 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
19548 m_line_has_non_zero_discriminator,
19549 m_last_subfile))
d9b3de22 19550 {
c24bdb02 19551 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 19552 dwarf_record_line_1 (m_gdbarch,
c24bdb02 19553 builder->get_current_subfile (),
8c95582d 19554 m_line, m_address, is_stmt,
804d2729 19555 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 19556 }
c24bdb02 19557 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19558 m_last_line = m_line;
d9b3de22
DE
19559 }
19560 }
19561 }
19562}
19563
804d2729
TT
19564lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19565 line_header *lh, bool record_lines_p)
d9b3de22 19566{
804d2729 19567 m_cu = cu;
6f77053d
PA
19568 m_gdbarch = arch;
19569 m_record_lines_p = record_lines_p;
19570 m_line_header = lh;
d9b3de22 19571
804d2729 19572 m_currently_recording_lines = true;
d9b3de22 19573
d9b3de22
DE
19574 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19575 was a line entry for it so that the backend has a chance to adjust it
19576 and also record it in case it needs it. This is currently used by MIPS
19577 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19578 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19579 m_is_stmt = lh->default_is_stmt;
19580 m_discriminator = 0;
252a6764
DE
19581}
19582
6f77053d
PA
19583void
19584lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19585 const gdb_byte *line_ptr,
7ab6656f 19586 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 19587{
7ab6656f
OJ
19588 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19589 the pc range of the CU. However, we restrict the test to only ADDRESS
19590 values of zero to preserve GDB's previous behaviour which is to handle
19591 the specific case of a function being GC'd by the linker. */
924c2928 19592
7ab6656f 19593 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
19594 {
19595 /* This line table is for a function which has been
19596 GCd by the linker. Ignore it. PR gdb/12528 */
19597
518817b3 19598 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
19599 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19600
b98664d3 19601 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 19602 line_offset, objfile_name (objfile));
804d2729
TT
19603 m_currently_recording_lines = false;
19604 /* Note: m_currently_recording_lines is left as false until we see
19605 DW_LNE_end_sequence. */
924c2928
DE
19606 }
19607}
19608
f3f5162e 19609/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
19610 Process the line number information in LH.
19611 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19612 program in order to set included_p for every referenced header. */
debd256d 19613
c906108c 19614static void
43f3e411
DE
19615dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19616 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 19617{
d521ce57
TT
19618 const gdb_byte *line_ptr, *extended_end;
19619 const gdb_byte *line_end;
a8c50c1f 19620 unsigned int bytes_read, extended_len;
699ca60a 19621 unsigned char op_code, extended_op;
e142c38c 19622 CORE_ADDR baseaddr;
518817b3 19623 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 19624 bfd *abfd = objfile->obfd;
fbf65064 19625 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
19626 /* True if we're recording line info (as opposed to building partial
19627 symtabs and just interested in finding include files mentioned by
19628 the line number program). */
19629 bool record_lines_p = !decode_for_pst_p;
e142c38c 19630
b3b3bada 19631 baseaddr = objfile->text_section_offset ();
c906108c 19632
debd256d
JB
19633 line_ptr = lh->statement_program_start;
19634 line_end = lh->statement_program_end;
c906108c
SS
19635
19636 /* Read the statement sequences until there's nothing left. */
19637 while (line_ptr < line_end)
19638 {
6f77053d
PA
19639 /* The DWARF line number program state machine. Reset the state
19640 machine at the start of each sequence. */
804d2729 19641 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 19642 bool end_sequence = false;
d9b3de22 19643
8c43009f 19644 if (record_lines_p)
c906108c 19645 {
8c43009f
PA
19646 /* Start a subfile for the current file of the state
19647 machine. */
19648 const file_entry *fe = state_machine.current_file ();
19649
19650 if (fe != NULL)
804d2729 19651 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
19652 }
19653
a738430d 19654 /* Decode the table. */
d9b3de22 19655 while (line_ptr < line_end && !end_sequence)
c906108c
SS
19656 {
19657 op_code = read_1_byte (abfd, line_ptr);
19658 line_ptr += 1;
9aa1fe7e 19659
debd256d 19660 if (op_code >= lh->opcode_base)
6e70227d 19661 {
8e07a239 19662 /* Special opcode. */
6f77053d 19663 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
19664 }
19665 else switch (op_code)
c906108c
SS
19666 {
19667 case DW_LNS_extended_op:
3e43a32a
MS
19668 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19669 &bytes_read);
473b7be6 19670 line_ptr += bytes_read;
a8c50c1f 19671 extended_end = line_ptr + extended_len;
c906108c
SS
19672 extended_op = read_1_byte (abfd, line_ptr);
19673 line_ptr += 1;
19674 switch (extended_op)
19675 {
19676 case DW_LNE_end_sequence:
6f77053d
PA
19677 state_machine.handle_end_sequence ();
19678 end_sequence = true;
c906108c
SS
19679 break;
19680 case DW_LNE_set_address:
d9b3de22
DE
19681 {
19682 CORE_ADDR address
c8a7a66f 19683 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 19684 line_ptr += bytes_read;
6f77053d
PA
19685
19686 state_machine.check_line_address (cu, line_ptr,
7ab6656f 19687 lowpc - baseaddr, address);
6f77053d 19688 state_machine.handle_set_address (baseaddr, address);
d9b3de22 19689 }
c906108c
SS
19690 break;
19691 case DW_LNE_define_file:
debd256d 19692 {
d521ce57 19693 const char *cur_file;
ecfb656c
PA
19694 unsigned int mod_time, length;
19695 dir_index dindex;
6e70227d 19696
3e43a32a
MS
19697 cur_file = read_direct_string (abfd, line_ptr,
19698 &bytes_read);
debd256d 19699 line_ptr += bytes_read;
ecfb656c 19700 dindex = (dir_index)
debd256d
JB
19701 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19702 line_ptr += bytes_read;
19703 mod_time =
19704 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19705 line_ptr += bytes_read;
19706 length =
19707 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19708 line_ptr += bytes_read;
ecfb656c 19709 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 19710 }
c906108c 19711 break;
d0c6ba3d 19712 case DW_LNE_set_discriminator:
6f77053d
PA
19713 {
19714 /* The discriminator is not interesting to the
19715 debugger; just ignore it. We still need to
19716 check its value though:
19717 if there are consecutive entries for the same
19718 (non-prologue) line we want to coalesce them.
19719 PR 17276. */
19720 unsigned int discr
19721 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19722 line_ptr += bytes_read;
19723
19724 state_machine.handle_set_discriminator (discr);
19725 }
d0c6ba3d 19726 break;
c906108c 19727 default:
b98664d3 19728 complaint (_("mangled .debug_line section"));
debd256d 19729 return;
c906108c 19730 }
a8c50c1f
DJ
19731 /* Make sure that we parsed the extended op correctly. If e.g.
19732 we expected a different address size than the producer used,
19733 we may have read the wrong number of bytes. */
19734 if (line_ptr != extended_end)
19735 {
b98664d3 19736 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
19737 return;
19738 }
c906108c
SS
19739 break;
19740 case DW_LNS_copy:
6f77053d 19741 state_machine.handle_copy ();
c906108c
SS
19742 break;
19743 case DW_LNS_advance_pc:
2dc7f7b3
TT
19744 {
19745 CORE_ADDR adjust
19746 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 19747 line_ptr += bytes_read;
6f77053d
PA
19748
19749 state_machine.handle_advance_pc (adjust);
2dc7f7b3 19750 }
c906108c
SS
19751 break;
19752 case DW_LNS_advance_line:
a05a36a5
DE
19753 {
19754 int line_delta
19755 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 19756 line_ptr += bytes_read;
6f77053d
PA
19757
19758 state_machine.handle_advance_line (line_delta);
a05a36a5 19759 }
c906108c
SS
19760 break;
19761 case DW_LNS_set_file:
d9b3de22 19762 {
6f77053d 19763 file_name_index file
ecfb656c
PA
19764 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19765 &bytes_read);
d9b3de22 19766 line_ptr += bytes_read;
8c43009f 19767
6f77053d 19768 state_machine.handle_set_file (file);
d9b3de22 19769 }
c906108c
SS
19770 break;
19771 case DW_LNS_set_column:
0ad93d4f 19772 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
19773 line_ptr += bytes_read;
19774 break;
19775 case DW_LNS_negate_stmt:
6f77053d 19776 state_machine.handle_negate_stmt ();
c906108c
SS
19777 break;
19778 case DW_LNS_set_basic_block:
c906108c 19779 break;
c2c6d25f
JM
19780 /* Add to the address register of the state machine the
19781 address increment value corresponding to special opcode
a738430d
MK
19782 255. I.e., this value is scaled by the minimum
19783 instruction length since special opcode 255 would have
b021a221 19784 scaled the increment. */
c906108c 19785 case DW_LNS_const_add_pc:
6f77053d 19786 state_machine.handle_const_add_pc ();
c906108c
SS
19787 break;
19788 case DW_LNS_fixed_advance_pc:
3e29f34a 19789 {
6f77053d 19790 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 19791 line_ptr += 2;
6f77053d
PA
19792
19793 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 19794 }
c906108c 19795 break;
9aa1fe7e 19796 default:
a738430d
MK
19797 {
19798 /* Unknown standard opcode, ignore it. */
9aa1fe7e 19799 int i;
a738430d 19800
debd256d 19801 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
19802 {
19803 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19804 line_ptr += bytes_read;
19805 }
19806 }
c906108c
SS
19807 }
19808 }
d9b3de22
DE
19809
19810 if (!end_sequence)
19811 dwarf2_debug_line_missing_end_sequence_complaint ();
19812
19813 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19814 in which case we still finish recording the last line). */
6f77053d 19815 state_machine.record_line (true);
c906108c 19816 }
f3f5162e
DE
19817}
19818
19819/* Decode the Line Number Program (LNP) for the given line_header
19820 structure and CU. The actual information extracted and the type
19821 of structures created from the LNP depends on the value of PST.
19822
19823 1. If PST is NULL, then this procedure uses the data from the program
19824 to create all necessary symbol tables, and their linetables.
19825
19826 2. If PST is not NULL, this procedure reads the program to determine
19827 the list of files included by the unit represented by PST, and
19828 builds all the associated partial symbol tables.
19829
19830 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19831 It is used for relative paths in the line table.
19832 NOTE: When processing partial symtabs (pst != NULL),
19833 comp_dir == pst->dirname.
19834
19835 NOTE: It is important that psymtabs have the same file name (via strcmp)
19836 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19837 symtab we don't use it in the name of the psymtabs we create.
19838 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
19839 A good testcase for this is mb-inline.exp.
19840
527f3840
JK
19841 LOWPC is the lowest address in CU (or 0 if not known).
19842
19843 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19844 for its PC<->lines mapping information. Otherwise only the filename
19845 table is read in. */
f3f5162e
DE
19846
19847static void
19848dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 19849 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 19850 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 19851{
518817b3 19852 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 19853 const int decode_for_pst_p = (pst != NULL);
f3f5162e 19854
527f3840
JK
19855 if (decode_mapping)
19856 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
19857
19858 if (decode_for_pst_p)
19859 {
aaa75496
JB
19860 /* Now that we're done scanning the Line Header Program, we can
19861 create the psymtab of each included file. */
7ba99d21
AT
19862 for (auto &file_entry : lh->file_names ())
19863 if (file_entry.included_p == 1)
aaa75496 19864 {
c89b44cd 19865 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 19866 const char *include_name =
7ba99d21
AT
19867 psymtab_include_file_name (lh, file_entry, pst,
19868 comp_dir, &name_holder);
c6da4cef 19869 if (include_name != NULL)
aaa75496
JB
19870 dwarf2_create_include_psymtab (include_name, pst, objfile);
19871 }
19872 }
cb1df416
DJ
19873 else
19874 {
19875 /* Make sure a symtab is created for every file, even files
19876 which contain only variables (i.e. no code with associated
19877 line numbers). */
c24bdb02
KS
19878 buildsym_compunit *builder = cu->get_builder ();
19879 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 19880
7ba99d21 19881 for (auto &fe : lh->file_names ())
cb1df416 19882 {
804d2729 19883 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 19884 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 19885 {
c24bdb02 19886 builder->get_current_subfile ()->symtab
804d2729 19887 = allocate_symtab (cust,
c24bdb02 19888 builder->get_current_subfile ()->name);
43f3e411 19889 }
c24bdb02 19890 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
19891 }
19892 }
c906108c
SS
19893}
19894
19895/* Start a subfile for DWARF. FILENAME is the name of the file and
19896 DIRNAME the name of the source directory which contains FILENAME
4d663531 19897 or NULL if not known.
c906108c
SS
19898 This routine tries to keep line numbers from identical absolute and
19899 relative file names in a common subfile.
19900
19901 Using the `list' example from the GDB testsuite, which resides in
19902 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19903 of /srcdir/list0.c yields the following debugging information for list0.c:
19904
c5aa993b 19905 DW_AT_name: /srcdir/list0.c
4d663531 19906 DW_AT_comp_dir: /compdir
357e46e7 19907 files.files[0].name: list0.h
c5aa993b 19908 files.files[0].dir: /srcdir
357e46e7 19909 files.files[1].name: list0.c
c5aa993b 19910 files.files[1].dir: /srcdir
c906108c
SS
19911
19912 The line number information for list0.c has to end up in a single
4f1520fb
FR
19913 subfile, so that `break /srcdir/list0.c:1' works as expected.
19914 start_subfile will ensure that this happens provided that we pass the
19915 concatenation of files.files[1].dir and files.files[1].name as the
19916 subfile's name. */
c906108c
SS
19917
19918static void
804d2729
TT
19919dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19920 const char *dirname)
c906108c 19921{
43816ebc 19922 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 19923
4d663531 19924 /* In order not to lose the line information directory,
4f1520fb
FR
19925 we concatenate it to the filename when it makes sense.
19926 Note that the Dwarf3 standard says (speaking of filenames in line
19927 information): ``The directory index is ignored for file names
19928 that represent full path names''. Thus ignoring dirname in the
19929 `else' branch below isn't an issue. */
c906108c 19930
d5166ae1 19931 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 19932 {
43816ebc
TT
19933 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19934 filename = copy.get ();
d521ce57 19935 }
c906108c 19936
c24bdb02 19937 cu->get_builder ()->start_subfile (filename);
c906108c
SS
19938}
19939
804d2729
TT
19940/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19941 buildsym_compunit constructor. */
f4dc4d17 19942
c24bdb02
KS
19943struct compunit_symtab *
19944dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19945 CORE_ADDR low_pc)
f4dc4d17 19946{
c24bdb02 19947 gdb_assert (m_builder == nullptr);
43f3e411 19948
c24bdb02
KS
19949 m_builder.reset (new struct buildsym_compunit
19950 (per_cu->dwarf2_per_objfile->objfile,
19951 name, comp_dir, language, low_pc));
93b8bea4 19952
c24bdb02 19953 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 19954
c24bdb02
KS
19955 get_builder ()->record_debugformat ("DWARF 2");
19956 get_builder ()->record_producer (producer);
f4dc4d17 19957
c24bdb02 19958 processing_has_namespace_info = false;
43f3e411 19959
c24bdb02 19960 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
19961}
19962
4c2df51b
DJ
19963static void
19964var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 19965 struct dwarf2_cu *cu)
4c2df51b 19966{
518817b3 19967 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
19968 struct comp_unit_head *cu_header = &cu->header;
19969
4c2df51b
DJ
19970 /* NOTE drow/2003-01-30: There used to be a comment and some special
19971 code here to turn a symbol with DW_AT_external and a
19972 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19973 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19974 with some versions of binutils) where shared libraries could have
19975 relocations against symbols in their debug information - the
19976 minimal symbol would have the right address, but the debug info
19977 would not. It's no longer necessary, because we will explicitly
19978 apply relocations when we read in the debug information now. */
19979
19980 /* A DW_AT_location attribute with no contents indicates that a
19981 variable has been optimized away. */
4fc6c0d5 19982 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 19983 {
f1e6e072 19984 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
19985 return;
19986 }
19987
19988 /* Handle one degenerate form of location expression specially, to
19989 preserve GDB's previous behavior when section offsets are
336d760d
AT
19990 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
19991 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 19992
4fc6c0d5 19993 if (attr->form_is_block ()
3019eac3
DE
19994 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19995 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
19996 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19997 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
19998 && (DW_BLOCK (attr)->size
19999 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20000 {
891d2f0b 20001 unsigned int dummy;
4c2df51b 20002
3019eac3 20003 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20004 SET_SYMBOL_VALUE_ADDRESS
20005 (sym, cu->header.read_address (objfile->obfd,
20006 DW_BLOCK (attr)->data + 1,
20007 &dummy));
3019eac3 20008 else
38583298
TT
20009 SET_SYMBOL_VALUE_ADDRESS
20010 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20011 &dummy));
f1e6e072 20012 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20013 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20014 SET_SYMBOL_VALUE_ADDRESS
20015 (sym,
20016 SYMBOL_VALUE_ADDRESS (sym)
20017 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20018 return;
20019 }
20020
20021 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20022 expression evaluator, and use LOC_COMPUTED only when necessary
20023 (i.e. when the value of a register or memory location is
20024 referenced, or a thread-local block, etc.). Then again, it might
20025 not be worthwhile. I'm assuming that it isn't unless performance
20026 or memory numbers show me otherwise. */
20027
f1e6e072 20028 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20029
f1e6e072 20030 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20031 cu->has_loclist = true;
4c2df51b
DJ
20032}
20033
c906108c
SS
20034/* Given a pointer to a DWARF information entry, figure out if we need
20035 to make a symbol table entry for it, and if so, create a new entry
20036 and return a pointer to it.
20037 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20038 used the passed type.
20039 If SPACE is not NULL, use it to hold the new symbol. If it is
20040 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20041
20042static struct symbol *
5e2db402
TT
20043new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20044 struct symbol *space)
c906108c 20045{
518817b3
SM
20046 struct dwarf2_per_objfile *dwarf2_per_objfile
20047 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20048 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20049 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20050 struct symbol *sym = NULL;
15d034d0 20051 const char *name;
c906108c
SS
20052 struct attribute *attr = NULL;
20053 struct attribute *attr2 = NULL;
e142c38c 20054 CORE_ADDR baseaddr;
e37fd15a
SW
20055 struct pending **list_to_add = NULL;
20056
edb3359d 20057 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20058
b3b3bada 20059 baseaddr = objfile->text_section_offset ();
c906108c 20060
94af9270 20061 name = dwarf2_name (die, cu);
c906108c
SS
20062 if (name)
20063 {
94af9270 20064 const char *linkagename;
34eaf542 20065 int suppress_add = 0;
94af9270 20066
34eaf542
TT
20067 if (space)
20068 sym = space;
20069 else
e623cf5d 20070 sym = allocate_symbol (objfile);
c906108c 20071 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20072
20073 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20074 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20075 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20076 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20077
f55ee35c
JK
20078 /* Fortran does not have mangling standard and the mangling does differ
20079 between gfortran, iFort etc. */
20080 if (cu->language == language_fortran
468c0cbb
CB
20081 && symbol_get_demangled_name (sym) == NULL)
20082 symbol_set_demangled_name (sym,
cfc594ee 20083 dwarf2_full_name (name, die, cu),
29df156d 20084 NULL);
f55ee35c 20085
c906108c 20086 /* Default assumptions.
c5aa993b 20087 Use the passed type or decode it from the die. */
176620f1 20088 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20089 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20090 if (type != NULL)
20091 SYMBOL_TYPE (sym) = type;
20092 else
e7c27a73 20093 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20094 attr = dwarf2_attr (die,
20095 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20096 cu);
435d3d88 20097 if (attr != nullptr)
c906108c
SS
20098 {
20099 SYMBOL_LINE (sym) = DW_UNSND (attr);
20100 }
cb1df416 20101
edb3359d
DJ
20102 attr = dwarf2_attr (die,
20103 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20104 cu);
435d3d88 20105 if (attr != nullptr)
cb1df416 20106 {
ecfb656c 20107 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20108 struct file_entry *fe;
9a619af0 20109
ecfb656c
PA
20110 if (cu->line_header != NULL)
20111 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20112 else
20113 fe = NULL;
20114
20115 if (fe == NULL)
b98664d3 20116 complaint (_("file index out of range"));
8c43009f
PA
20117 else
20118 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20119 }
20120
c906108c
SS
20121 switch (die->tag)
20122 {
20123 case DW_TAG_label:
e142c38c 20124 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20125 if (attr != nullptr)
3e29f34a
MR
20126 {
20127 CORE_ADDR addr;
20128
cd6c91b4 20129 addr = attr->value_as_address ();
3e29f34a 20130 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20131 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20132 }
0f5238ed
TT
20133 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20134 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20135 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20136 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20137 break;
20138 case DW_TAG_subprogram:
20139 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20140 finish_block. */
f1e6e072 20141 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20142 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20143 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20144 || cu->language == language_ada
20145 || cu->language == language_fortran)
c906108c 20146 {
2cfa0c8d 20147 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20148 Ada and Fortran subprograms, whether marked external or
20149 not, are always stored as a global symbol, because we want
20150 to be able to access them globally. For instance, we want
20151 to be able to break on a nested subprogram without having
20152 to specify the context. */
c24bdb02 20153 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20154 }
20155 else
20156 {
e37fd15a 20157 list_to_add = cu->list_in_scope;
c906108c
SS
20158 }
20159 break;
edb3359d
DJ
20160 case DW_TAG_inlined_subroutine:
20161 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20162 finish_block. */
f1e6e072 20163 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20164 SYMBOL_INLINED (sym) = 1;
481860b3 20165 list_to_add = cu->list_in_scope;
edb3359d 20166 break;
34eaf542
TT
20167 case DW_TAG_template_value_param:
20168 suppress_add = 1;
20169 /* Fall through. */
72929c62 20170 case DW_TAG_constant:
c906108c 20171 case DW_TAG_variable:
254e6b9e 20172 case DW_TAG_member:
0963b4bd
MS
20173 /* Compilation with minimal debug info may result in
20174 variables with missing type entries. Change the
20175 misleading `void' type to something sensible. */
c906108c 20176 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20177 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20178
e142c38c 20179 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20180 /* In the case of DW_TAG_member, we should only be called for
20181 static const members. */
20182 if (die->tag == DW_TAG_member)
20183 {
3863f96c
DE
20184 /* dwarf2_add_field uses die_is_declaration,
20185 so we do the same. */
254e6b9e
DE
20186 gdb_assert (die_is_declaration (die, cu));
20187 gdb_assert (attr);
20188 }
435d3d88 20189 if (attr != nullptr)
c906108c 20190 {
e7c27a73 20191 dwarf2_const_value (attr, sym, cu);
e142c38c 20192 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20193 if (!suppress_add)
34eaf542
TT
20194 {
20195 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20196 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20197 else
e37fd15a 20198 list_to_add = cu->list_in_scope;
34eaf542 20199 }
c906108c
SS
20200 break;
20201 }
e142c38c 20202 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20203 if (attr != nullptr)
c906108c 20204 {
e7c27a73 20205 var_decode_location (attr, sym, cu);
e142c38c 20206 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20207
20208 /* Fortran explicitly imports any global symbols to the local
20209 scope by DW_TAG_common_block. */
20210 if (cu->language == language_fortran && die->parent
20211 && die->parent->tag == DW_TAG_common_block)
20212 attr2 = NULL;
20213
caac4577
JG
20214 if (SYMBOL_CLASS (sym) == LOC_STATIC
20215 && SYMBOL_VALUE_ADDRESS (sym) == 0
20216 && !dwarf2_per_objfile->has_section_at_zero)
20217 {
20218 /* When a static variable is eliminated by the linker,
20219 the corresponding debug information is not stripped
20220 out, but the variable address is set to null;
20221 do not add such variables into symbol table. */
20222 }
20223 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20224 {
4b610737
TT
20225 if (SYMBOL_CLASS (sym) == LOC_STATIC
20226 && (objfile->flags & OBJF_MAINLINE) == 0
20227 && dwarf2_per_objfile->can_copy)
20228 {
20229 /* A global static variable might be subject to
20230 copy relocation. We first check for a local
20231 minsym, though, because maybe the symbol was
20232 marked hidden, in which case this would not
20233 apply. */
20234 bound_minimal_symbol found
20235 = (lookup_minimal_symbol_linkage
987012b8 20236 (sym->linkage_name (), objfile));
4b610737
TT
20237 if (found.minsym != nullptr)
20238 sym->maybe_copied = 1;
20239 }
f55ee35c 20240
1c809c68
TT
20241 /* A variable with DW_AT_external is never static,
20242 but it may be block-scoped. */
804d2729 20243 list_to_add
c24bdb02
KS
20244 = ((cu->list_in_scope
20245 == cu->get_builder ()->get_file_symbols ())
20246 ? cu->get_builder ()->get_global_symbols ()
804d2729 20247 : cu->list_in_scope);
1c809c68 20248 }
c906108c 20249 else
e37fd15a 20250 list_to_add = cu->list_in_scope;
c906108c
SS
20251 }
20252 else
20253 {
20254 /* We do not know the address of this symbol.
c5aa993b
JM
20255 If it is an external symbol and we have type information
20256 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20257 The address of the variable will then be determined from
20258 the minimal symbol table whenever the variable is
20259 referenced. */
e142c38c 20260 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20261
20262 /* Fortran explicitly imports any global symbols to the local
20263 scope by DW_TAG_common_block. */
20264 if (cu->language == language_fortran && die->parent
20265 && die->parent->tag == DW_TAG_common_block)
20266 {
20267 /* SYMBOL_CLASS doesn't matter here because
20268 read_common_block is going to reset it. */
20269 if (!suppress_add)
20270 list_to_add = cu->list_in_scope;
20271 }
20272 else if (attr2 && (DW_UNSND (attr2) != 0)
20273 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20274 {
0fe7935b
DJ
20275 /* A variable with DW_AT_external is never static, but it
20276 may be block-scoped. */
804d2729 20277 list_to_add
c24bdb02
KS
20278 = ((cu->list_in_scope
20279 == cu->get_builder ()->get_file_symbols ())
20280 ? cu->get_builder ()->get_global_symbols ()
804d2729 20281 : cu->list_in_scope);
0fe7935b 20282
f1e6e072 20283 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20284 }
442ddf59
JK
20285 else if (!die_is_declaration (die, cu))
20286 {
20287 /* Use the default LOC_OPTIMIZED_OUT class. */
20288 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20289 if (!suppress_add)
20290 list_to_add = cu->list_in_scope;
442ddf59 20291 }
c906108c
SS
20292 }
20293 break;
20294 case DW_TAG_formal_parameter:
a60f3166
TT
20295 {
20296 /* If we are inside a function, mark this as an argument. If
20297 not, we might be looking at an argument to an inlined function
20298 when we do not have enough information to show inlined frames;
20299 pretend it's a local variable in that case so that the user can
20300 still see it. */
804d2729 20301 struct context_stack *curr
c24bdb02 20302 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20303 if (curr != nullptr && curr->name != nullptr)
20304 SYMBOL_IS_ARGUMENT (sym) = 1;
20305 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20306 if (attr != nullptr)
a60f3166
TT
20307 {
20308 var_decode_location (attr, sym, cu);
20309 }
20310 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20311 if (attr != nullptr)
a60f3166
TT
20312 {
20313 dwarf2_const_value (attr, sym, cu);
20314 }
f346a30d 20315
a60f3166
TT
20316 list_to_add = cu->list_in_scope;
20317 }
c906108c
SS
20318 break;
20319 case DW_TAG_unspecified_parameters:
20320 /* From varargs functions; gdb doesn't seem to have any
20321 interest in this information, so just ignore it for now.
20322 (FIXME?) */
20323 break;
34eaf542
TT
20324 case DW_TAG_template_type_param:
20325 suppress_add = 1;
20326 /* Fall through. */
c906108c 20327 case DW_TAG_class_type:
680b30c7 20328 case DW_TAG_interface_type:
c906108c
SS
20329 case DW_TAG_structure_type:
20330 case DW_TAG_union_type:
72019c9c 20331 case DW_TAG_set_type:
c906108c 20332 case DW_TAG_enumeration_type:
f1e6e072 20333 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20334 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20335
63d06c5c 20336 {
9c37b5ae 20337 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20338 really ever be static objects: otherwise, if you try
20339 to, say, break of a class's method and you're in a file
20340 which doesn't mention that class, it won't work unless
20341 the check for all static symbols in lookup_symbol_aux
20342 saves you. See the OtherFileClass tests in
20343 gdb.c++/namespace.exp. */
20344
e37fd15a 20345 if (!suppress_add)
34eaf542 20346 {
c24bdb02 20347 buildsym_compunit *builder = cu->get_builder ();
804d2729 20348 list_to_add
c24bdb02 20349 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20350 && cu->language == language_cplus
c24bdb02 20351 ? builder->get_global_symbols ()
804d2729 20352 : cu->list_in_scope);
63d06c5c 20353
64382290 20354 /* The semantics of C++ state that "struct foo {
9c37b5ae 20355 ... }" also defines a typedef for "foo". */
64382290 20356 if (cu->language == language_cplus
45280282 20357 || cu->language == language_ada
c44af4eb
TT
20358 || cu->language == language_d
20359 || cu->language == language_rust)
64382290
TT
20360 {
20361 /* The symbol's name is already allocated along
20362 with this objfile, so we don't need to
20363 duplicate it for the type. */
20364 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20365 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20366 }
63d06c5c
DC
20367 }
20368 }
c906108c
SS
20369 break;
20370 case DW_TAG_typedef:
f1e6e072 20371 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20372 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20373 list_to_add = cu->list_in_scope;
63d06c5c 20374 break;
c906108c 20375 case DW_TAG_base_type:
a02abb62 20376 case DW_TAG_subrange_type:
f1e6e072 20377 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20378 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20379 list_to_add = cu->list_in_scope;
c906108c
SS
20380 break;
20381 case DW_TAG_enumerator:
e142c38c 20382 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20383 if (attr != nullptr)
c906108c 20384 {
e7c27a73 20385 dwarf2_const_value (attr, sym, cu);
c906108c 20386 }
63d06c5c
DC
20387 {
20388 /* NOTE: carlton/2003-11-10: See comment above in the
20389 DW_TAG_class_type, etc. block. */
20390
804d2729 20391 list_to_add
c24bdb02 20392 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20393 && cu->language == language_cplus
c24bdb02 20394 ? cu->get_builder ()->get_global_symbols ()
804d2729 20395 : cu->list_in_scope);
63d06c5c 20396 }
c906108c 20397 break;
74921315 20398 case DW_TAG_imported_declaration:
5c4e30ca 20399 case DW_TAG_namespace:
f1e6e072 20400 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20401 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20402 break;
530e8392
KB
20403 case DW_TAG_module:
20404 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20405 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20406 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20407 break;
4357ac6c 20408 case DW_TAG_common_block:
f1e6e072 20409 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20410 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20411 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20412 break;
c906108c
SS
20413 default:
20414 /* Not a tag we recognize. Hopefully we aren't processing
20415 trash data, but since we must specifically ignore things
20416 we don't recognize, there is nothing else we should do at
0963b4bd 20417 this point. */
b98664d3 20418 complaint (_("unsupported tag: '%s'"),
4d3c2250 20419 dwarf_tag_name (die->tag));
c906108c
SS
20420 break;
20421 }
df8a16a1 20422
e37fd15a
SW
20423 if (suppress_add)
20424 {
20425 sym->hash_next = objfile->template_symbols;
20426 objfile->template_symbols = sym;
20427 list_to_add = NULL;
20428 }
20429
20430 if (list_to_add != NULL)
d3cb6808 20431 add_symbol_to_list (sym, list_to_add);
e37fd15a 20432
df8a16a1
DJ
20433 /* For the benefit of old versions of GCC, check for anonymous
20434 namespaces based on the demangled name. */
4d4ec4e5 20435 if (!cu->processing_has_namespace_info
94af9270 20436 && cu->language == language_cplus)
c24bdb02 20437 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20438 }
20439 return (sym);
20440}
20441
98bfdba5
PA
20442/* Given an attr with a DW_FORM_dataN value in host byte order,
20443 zero-extend it as appropriate for the symbol's type. The DWARF
20444 standard (v4) is not entirely clear about the meaning of using
20445 DW_FORM_dataN for a constant with a signed type, where the type is
20446 wider than the data. The conclusion of a discussion on the DWARF
20447 list was that this is unspecified. We choose to always zero-extend
20448 because that is the interpretation long in use by GCC. */
c906108c 20449
98bfdba5 20450static gdb_byte *
ff39bb5e 20451dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20452 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20453{
518817b3 20454 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20455 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20456 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20457 LONGEST l = DW_UNSND (attr);
20458
20459 if (bits < sizeof (*value) * 8)
20460 {
20461 l &= ((LONGEST) 1 << bits) - 1;
20462 *value = l;
20463 }
20464 else if (bits == sizeof (*value) * 8)
20465 *value = l;
20466 else
20467 {
224c3ddb 20468 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20469 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20470 return bytes;
20471 }
20472
20473 return NULL;
20474}
20475
20476/* Read a constant value from an attribute. Either set *VALUE, or if
20477 the value does not fit in *VALUE, set *BYTES - either already
20478 allocated on the objfile obstack, or newly allocated on OBSTACK,
20479 or, set *BATON, if we translated the constant to a location
20480 expression. */
20481
20482static void
ff39bb5e 20483dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20484 const char *name, struct obstack *obstack,
20485 struct dwarf2_cu *cu,
d521ce57 20486 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20487 struct dwarf2_locexpr_baton **baton)
20488{
518817b3 20489 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20490 struct comp_unit_head *cu_header = &cu->header;
c906108c 20491 struct dwarf_block *blk;
98bfdba5
PA
20492 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20493 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20494
20495 *value = 0;
20496 *bytes = NULL;
20497 *baton = NULL;
c906108c
SS
20498
20499 switch (attr->form)
20500 {
20501 case DW_FORM_addr:
336d760d 20502 case DW_FORM_addrx:
3019eac3 20503 case DW_FORM_GNU_addr_index:
ac56253d 20504 {
ac56253d
TT
20505 gdb_byte *data;
20506
98bfdba5
PA
20507 if (TYPE_LENGTH (type) != cu_header->addr_size)
20508 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20509 cu_header->addr_size,
98bfdba5 20510 TYPE_LENGTH (type));
ac56253d
TT
20511 /* Symbols of this form are reasonably rare, so we just
20512 piggyback on the existing location code rather than writing
20513 a new implementation of symbol_computed_ops. */
8d749320 20514 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20515 (*baton)->per_cu = cu->per_cu;
20516 gdb_assert ((*baton)->per_cu);
ac56253d 20517
98bfdba5 20518 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20519 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20520 (*baton)->data = data;
ac56253d
TT
20521
20522 data[0] = DW_OP_addr;
20523 store_unsigned_integer (&data[1], cu_header->addr_size,
20524 byte_order, DW_ADDR (attr));
20525 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20526 }
c906108c 20527 break;
4ac36638 20528 case DW_FORM_string:
93b5768b 20529 case DW_FORM_strp:
cf532bd1 20530 case DW_FORM_strx:
3019eac3 20531 case DW_FORM_GNU_str_index:
36586728 20532 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20533 /* DW_STRING is already allocated on the objfile obstack, point
20534 directly to it. */
d521ce57 20535 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20536 break;
c906108c
SS
20537 case DW_FORM_block1:
20538 case DW_FORM_block2:
20539 case DW_FORM_block4:
20540 case DW_FORM_block:
2dc7f7b3 20541 case DW_FORM_exprloc:
0224619f 20542 case DW_FORM_data16:
c906108c 20543 blk = DW_BLOCK (attr);
98bfdba5
PA
20544 if (TYPE_LENGTH (type) != blk->size)
20545 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20546 TYPE_LENGTH (type));
20547 *bytes = blk->data;
c906108c 20548 break;
2df3850c
JM
20549
20550 /* The DW_AT_const_value attributes are supposed to carry the
20551 symbol's value "represented as it would be on the target
20552 architecture." By the time we get here, it's already been
20553 converted to host endianness, so we just need to sign- or
20554 zero-extend it as appropriate. */
20555 case DW_FORM_data1:
3aef2284 20556 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20557 break;
c906108c 20558 case DW_FORM_data2:
3aef2284 20559 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20560 break;
c906108c 20561 case DW_FORM_data4:
3aef2284 20562 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20563 break;
c906108c 20564 case DW_FORM_data8:
3aef2284 20565 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20566 break;
20567
c906108c 20568 case DW_FORM_sdata:
663c44ac 20569 case DW_FORM_implicit_const:
98bfdba5 20570 *value = DW_SND (attr);
2df3850c
JM
20571 break;
20572
c906108c 20573 case DW_FORM_udata:
98bfdba5 20574 *value = DW_UNSND (attr);
c906108c 20575 break;
2df3850c 20576
c906108c 20577 default:
b98664d3 20578 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 20579 dwarf_form_name (attr->form));
98bfdba5 20580 *value = 0;
c906108c
SS
20581 break;
20582 }
20583}
20584
2df3850c 20585
98bfdba5
PA
20586/* Copy constant value from an attribute to a symbol. */
20587
2df3850c 20588static void
ff39bb5e 20589dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20590 struct dwarf2_cu *cu)
2df3850c 20591{
518817b3 20592 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 20593 LONGEST value;
d521ce57 20594 const gdb_byte *bytes;
98bfdba5 20595 struct dwarf2_locexpr_baton *baton;
2df3850c 20596
98bfdba5 20597 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 20598 sym->print_name (),
98bfdba5
PA
20599 &objfile->objfile_obstack, cu,
20600 &value, &bytes, &baton);
2df3850c 20601
98bfdba5
PA
20602 if (baton != NULL)
20603 {
98bfdba5 20604 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 20605 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
20606 }
20607 else if (bytes != NULL)
20608 {
20609 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 20610 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
20611 }
20612 else
20613 {
20614 SYMBOL_VALUE (sym) = value;
f1e6e072 20615 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 20616 }
2df3850c
JM
20617}
20618
c906108c
SS
20619/* Return the type of the die in question using its DW_AT_type attribute. */
20620
20621static struct type *
e7c27a73 20622die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20623{
c906108c 20624 struct attribute *type_attr;
c906108c 20625
e142c38c 20626 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
20627 if (!type_attr)
20628 {
518817b3 20629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 20630 /* A missing DW_AT_type represents a void type. */
518817b3 20631 return objfile_type (objfile)->builtin_void;
c906108c 20632 }
348e048f 20633
673bfd45 20634 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20635}
20636
b4ba55a1
JB
20637/* True iff CU's producer generates GNAT Ada auxiliary information
20638 that allows to find parallel types through that information instead
20639 of having to do expensive parallel lookups by type name. */
20640
20641static int
20642need_gnat_info (struct dwarf2_cu *cu)
20643{
de4cb04a
JB
20644 /* Assume that the Ada compiler was GNAT, which always produces
20645 the auxiliary information. */
20646 return (cu->language == language_ada);
b4ba55a1
JB
20647}
20648
b4ba55a1
JB
20649/* Return the auxiliary type of the die in question using its
20650 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20651 attribute is not present. */
20652
20653static struct type *
20654die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20655{
b4ba55a1 20656 struct attribute *type_attr;
b4ba55a1
JB
20657
20658 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20659 if (!type_attr)
20660 return NULL;
20661
673bfd45 20662 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
20663}
20664
20665/* If DIE has a descriptive_type attribute, then set the TYPE's
20666 descriptive type accordingly. */
20667
20668static void
20669set_descriptive_type (struct type *type, struct die_info *die,
20670 struct dwarf2_cu *cu)
20671{
20672 struct type *descriptive_type = die_descriptive_type (die, cu);
20673
20674 if (descriptive_type)
20675 {
20676 ALLOCATE_GNAT_AUX_TYPE (type);
20677 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20678 }
20679}
20680
c906108c
SS
20681/* Return the containing type of the die in question using its
20682 DW_AT_containing_type attribute. */
20683
20684static struct type *
e7c27a73 20685die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20686{
c906108c 20687 struct attribute *type_attr;
518817b3 20688 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 20689
e142c38c 20690 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
20691 if (!type_attr)
20692 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 20693 "[in module %s]"), objfile_name (objfile));
33ac96f0 20694
673bfd45 20695 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20696}
20697
ac9ec31b
DE
20698/* Return an error marker type to use for the ill formed type in DIE/CU. */
20699
20700static struct type *
20701build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20702{
518817b3
SM
20703 struct dwarf2_per_objfile *dwarf2_per_objfile
20704 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 20705 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 20706 char *saved;
ac9ec31b 20707
528e1572
SM
20708 std::string message
20709 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20710 objfile_name (objfile),
20711 sect_offset_str (cu->header.sect_off),
20712 sect_offset_str (die->sect_off));
efba19b0 20713 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 20714
19f392bc 20715 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
20716}
20717
673bfd45 20718/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
20719 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20720 DW_AT_containing_type.
673bfd45
DE
20721 If there is no type substitute an error marker. */
20722
c906108c 20723static struct type *
ff39bb5e 20724lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 20725 struct dwarf2_cu *cu)
c906108c 20726{
518817b3
SM
20727 struct dwarf2_per_objfile *dwarf2_per_objfile
20728 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20729 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
20730 struct type *this_type;
20731
ac9ec31b
DE
20732 gdb_assert (attr->name == DW_AT_type
20733 || attr->name == DW_AT_GNAT_descriptive_type
20734 || attr->name == DW_AT_containing_type);
20735
673bfd45
DE
20736 /* First see if we have it cached. */
20737
36586728
TT
20738 if (attr->form == DW_FORM_GNU_ref_alt)
20739 {
20740 struct dwarf2_per_cu_data *per_cu;
9c541725 20741 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 20742
ed2dc618
SM
20743 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20744 dwarf2_per_objfile);
9c541725 20745 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 20746 }
cd6c91b4 20747 else if (attr->form_is_ref ())
673bfd45 20748 {
9c541725 20749 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 20750
9c541725 20751 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 20752 }
55f1336d 20753 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 20754 {
ac9ec31b 20755 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 20756
ac9ec31b 20757 return get_signatured_type (die, signature, cu);
673bfd45
DE
20758 }
20759 else
20760 {
b98664d3 20761 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
20762 " at %s [in module %s]"),
20763 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 20764 objfile_name (objfile));
ac9ec31b 20765 return build_error_marker_type (cu, die);
673bfd45
DE
20766 }
20767
20768 /* If not cached we need to read it in. */
20769
20770 if (this_type == NULL)
20771 {
ac9ec31b 20772 struct die_info *type_die = NULL;
673bfd45
DE
20773 struct dwarf2_cu *type_cu = cu;
20774
cd6c91b4 20775 if (attr->form_is_ref ())
ac9ec31b
DE
20776 type_die = follow_die_ref (die, attr, &type_cu);
20777 if (type_die == NULL)
20778 return build_error_marker_type (cu, die);
20779 /* If we find the type now, it's probably because the type came
3019eac3
DE
20780 from an inter-CU reference and the type's CU got expanded before
20781 ours. */
ac9ec31b 20782 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
20783 }
20784
20785 /* If we still don't have a type use an error marker. */
20786
20787 if (this_type == NULL)
ac9ec31b 20788 return build_error_marker_type (cu, die);
673bfd45 20789
f792889a 20790 return this_type;
c906108c
SS
20791}
20792
673bfd45
DE
20793/* Return the type in DIE, CU.
20794 Returns NULL for invalid types.
20795
02142a6c 20796 This first does a lookup in die_type_hash,
673bfd45
DE
20797 and only reads the die in if necessary.
20798
20799 NOTE: This can be called when reading in partial or full symbols. */
20800
f792889a 20801static struct type *
e7c27a73 20802read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20803{
f792889a
DJ
20804 struct type *this_type;
20805
20806 this_type = get_die_type (die, cu);
20807 if (this_type)
20808 return this_type;
20809
673bfd45
DE
20810 return read_type_die_1 (die, cu);
20811}
20812
20813/* Read the type in DIE, CU.
20814 Returns NULL for invalid types. */
20815
20816static struct type *
20817read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20818{
20819 struct type *this_type = NULL;
20820
c906108c
SS
20821 switch (die->tag)
20822 {
20823 case DW_TAG_class_type:
680b30c7 20824 case DW_TAG_interface_type:
c906108c
SS
20825 case DW_TAG_structure_type:
20826 case DW_TAG_union_type:
f792889a 20827 this_type = read_structure_type (die, cu);
c906108c
SS
20828 break;
20829 case DW_TAG_enumeration_type:
f792889a 20830 this_type = read_enumeration_type (die, cu);
c906108c
SS
20831 break;
20832 case DW_TAG_subprogram:
20833 case DW_TAG_subroutine_type:
edb3359d 20834 case DW_TAG_inlined_subroutine:
f792889a 20835 this_type = read_subroutine_type (die, cu);
c906108c
SS
20836 break;
20837 case DW_TAG_array_type:
f792889a 20838 this_type = read_array_type (die, cu);
c906108c 20839 break;
72019c9c 20840 case DW_TAG_set_type:
f792889a 20841 this_type = read_set_type (die, cu);
72019c9c 20842 break;
c906108c 20843 case DW_TAG_pointer_type:
f792889a 20844 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
20845 break;
20846 case DW_TAG_ptr_to_member_type:
f792889a 20847 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
20848 break;
20849 case DW_TAG_reference_type:
4297a3f0
AV
20850 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20851 break;
20852 case DW_TAG_rvalue_reference_type:
20853 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
20854 break;
20855 case DW_TAG_const_type:
f792889a 20856 this_type = read_tag_const_type (die, cu);
c906108c
SS
20857 break;
20858 case DW_TAG_volatile_type:
f792889a 20859 this_type = read_tag_volatile_type (die, cu);
c906108c 20860 break;
06d66ee9
TT
20861 case DW_TAG_restrict_type:
20862 this_type = read_tag_restrict_type (die, cu);
20863 break;
c906108c 20864 case DW_TAG_string_type:
f792889a 20865 this_type = read_tag_string_type (die, cu);
c906108c
SS
20866 break;
20867 case DW_TAG_typedef:
f792889a 20868 this_type = read_typedef (die, cu);
c906108c 20869 break;
a02abb62 20870 case DW_TAG_subrange_type:
f792889a 20871 this_type = read_subrange_type (die, cu);
a02abb62 20872 break;
c906108c 20873 case DW_TAG_base_type:
f792889a 20874 this_type = read_base_type (die, cu);
c906108c 20875 break;
81a17f79 20876 case DW_TAG_unspecified_type:
f792889a 20877 this_type = read_unspecified_type (die, cu);
81a17f79 20878 break;
0114d602
DJ
20879 case DW_TAG_namespace:
20880 this_type = read_namespace_type (die, cu);
20881 break;
f55ee35c
JK
20882 case DW_TAG_module:
20883 this_type = read_module_type (die, cu);
20884 break;
a2c2acaf
MW
20885 case DW_TAG_atomic_type:
20886 this_type = read_tag_atomic_type (die, cu);
20887 break;
c906108c 20888 default:
b98664d3 20889 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 20890 dwarf_tag_name (die->tag));
c906108c
SS
20891 break;
20892 }
63d06c5c 20893
f792889a 20894 return this_type;
63d06c5c
DC
20895}
20896
abc72ce4
DE
20897/* See if we can figure out if the class lives in a namespace. We do
20898 this by looking for a member function; its demangled name will
20899 contain namespace info, if there is any.
20900 Return the computed name or NULL.
20901 Space for the result is allocated on the objfile's obstack.
20902 This is the full-die version of guess_partial_die_structure_name.
20903 In this case we know DIE has no useful parent. */
20904
43816ebc 20905static const char *
abc72ce4
DE
20906guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20907{
20908 struct die_info *spec_die;
20909 struct dwarf2_cu *spec_cu;
20910 struct die_info *child;
518817b3 20911 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
20912
20913 spec_cu = cu;
20914 spec_die = die_specification (die, &spec_cu);
20915 if (spec_die != NULL)
20916 {
20917 die = spec_die;
20918 cu = spec_cu;
20919 }
20920
20921 for (child = die->child;
20922 child != NULL;
20923 child = child->sibling)
20924 {
20925 if (child->tag == DW_TAG_subprogram)
20926 {
73b9be8b 20927 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 20928
7d45c7c3 20929 if (linkage_name != NULL)
abc72ce4 20930 {
43816ebc
TT
20931 gdb::unique_xmalloc_ptr<char> actual_name
20932 (language_class_name_from_physname (cu->language_defn,
20933 linkage_name));
20934 const char *name = NULL;
abc72ce4
DE
20935
20936 if (actual_name != NULL)
20937 {
15d034d0 20938 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
20939
20940 if (die_name != NULL
43816ebc 20941 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
20942 {
20943 /* Strip off the class name from the full name.
20944 We want the prefix. */
20945 int die_name_len = strlen (die_name);
43816ebc
TT
20946 int actual_name_len = strlen (actual_name.get ());
20947 const char *ptr = actual_name.get ();
abc72ce4
DE
20948
20949 /* Test for '::' as a sanity check. */
20950 if (actual_name_len > die_name_len + 2
43816ebc 20951 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 20952 name = obstack_strndup (
e3b94546 20953 &objfile->per_bfd->storage_obstack,
43816ebc 20954 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
20955 }
20956 }
abc72ce4
DE
20957 return name;
20958 }
20959 }
20960 }
20961
20962 return NULL;
20963}
20964
96408a79
SA
20965/* GCC might emit a nameless typedef that has a linkage name. Determine the
20966 prefix part in such case. See
20967 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20968
a121b7c1 20969static const char *
96408a79
SA
20970anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20971{
20972 struct attribute *attr;
e6a959d6 20973 const char *base;
96408a79
SA
20974
20975 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20976 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20977 return NULL;
20978
7d45c7c3 20979 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
20980 return NULL;
20981
73b9be8b 20982 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
20983 if (attr == NULL || DW_STRING (attr) == NULL)
20984 return NULL;
20985
20986 /* dwarf2_name had to be already called. */
20987 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20988
20989 /* Strip the base name, keep any leading namespaces/classes. */
20990 base = strrchr (DW_STRING (attr), ':');
20991 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20992 return "";
20993
518817b3 20994 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
20995 return obstack_strndup (&objfile->per_bfd->storage_obstack,
20996 DW_STRING (attr),
20997 &base[-1] - DW_STRING (attr));
96408a79
SA
20998}
20999
fdde2d81 21000/* Return the name of the namespace/class that DIE is defined within,
0114d602 21001 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21002
0114d602
DJ
21003 For example, if we're within the method foo() in the following
21004 code:
21005
21006 namespace N {
21007 class C {
21008 void foo () {
21009 }
21010 };
21011 }
21012
21013 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21014
0d5cff50 21015static const char *
e142c38c 21016determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21017{
518817b3
SM
21018 struct dwarf2_per_objfile *dwarf2_per_objfile
21019 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21020 struct die_info *parent, *spec_die;
21021 struct dwarf2_cu *spec_cu;
21022 struct type *parent_type;
a121b7c1 21023 const char *retval;
63d06c5c 21024
9c37b5ae 21025 if (cu->language != language_cplus
c44af4eb
TT
21026 && cu->language != language_fortran && cu->language != language_d
21027 && cu->language != language_rust)
0114d602
DJ
21028 return "";
21029
96408a79
SA
21030 retval = anonymous_struct_prefix (die, cu);
21031 if (retval)
21032 return retval;
21033
0114d602
DJ
21034 /* We have to be careful in the presence of DW_AT_specification.
21035 For example, with GCC 3.4, given the code
21036
21037 namespace N {
21038 void foo() {
21039 // Definition of N::foo.
21040 }
21041 }
21042
21043 then we'll have a tree of DIEs like this:
21044
21045 1: DW_TAG_compile_unit
21046 2: DW_TAG_namespace // N
21047 3: DW_TAG_subprogram // declaration of N::foo
21048 4: DW_TAG_subprogram // definition of N::foo
21049 DW_AT_specification // refers to die #3
21050
21051 Thus, when processing die #4, we have to pretend that we're in
21052 the context of its DW_AT_specification, namely the contex of die
21053 #3. */
21054 spec_cu = cu;
21055 spec_die = die_specification (die, &spec_cu);
21056 if (spec_die == NULL)
21057 parent = die->parent;
21058 else
63d06c5c 21059 {
0114d602
DJ
21060 parent = spec_die->parent;
21061 cu = spec_cu;
63d06c5c 21062 }
0114d602
DJ
21063
21064 if (parent == NULL)
21065 return "";
98bfdba5
PA
21066 else if (parent->building_fullname)
21067 {
21068 const char *name;
21069 const char *parent_name;
21070
21071 /* It has been seen on RealView 2.2 built binaries,
21072 DW_TAG_template_type_param types actually _defined_ as
21073 children of the parent class:
21074
21075 enum E {};
21076 template class <class Enum> Class{};
21077 Class<enum E> class_e;
21078
21079 1: DW_TAG_class_type (Class)
21080 2: DW_TAG_enumeration_type (E)
21081 3: DW_TAG_enumerator (enum1:0)
21082 3: DW_TAG_enumerator (enum2:1)
21083 ...
21084 2: DW_TAG_template_type_param
21085 DW_AT_type DW_FORM_ref_udata (E)
21086
21087 Besides being broken debug info, it can put GDB into an
21088 infinite loop. Consider:
21089
21090 When we're building the full name for Class<E>, we'll start
21091 at Class, and go look over its template type parameters,
21092 finding E. We'll then try to build the full name of E, and
21093 reach here. We're now trying to build the full name of E,
21094 and look over the parent DIE for containing scope. In the
21095 broken case, if we followed the parent DIE of E, we'd again
21096 find Class, and once again go look at its template type
21097 arguments, etc., etc. Simply don't consider such parent die
21098 as source-level parent of this die (it can't be, the language
21099 doesn't allow it), and break the loop here. */
21100 name = dwarf2_name (die, cu);
21101 parent_name = dwarf2_name (parent, cu);
b98664d3 21102 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21103 name ? name : "<unknown>",
21104 parent_name ? parent_name : "<unknown>");
21105 return "";
21106 }
63d06c5c 21107 else
0114d602
DJ
21108 switch (parent->tag)
21109 {
63d06c5c 21110 case DW_TAG_namespace:
0114d602 21111 parent_type = read_type_die (parent, cu);
acebe513
UW
21112 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21113 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21114 Work around this problem here. */
21115 if (cu->language == language_cplus
e86ca25f 21116 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21117 return "";
0114d602 21118 /* We give a name to even anonymous namespaces. */
e86ca25f 21119 return TYPE_NAME (parent_type);
63d06c5c 21120 case DW_TAG_class_type:
680b30c7 21121 case DW_TAG_interface_type:
63d06c5c 21122 case DW_TAG_structure_type:
0114d602 21123 case DW_TAG_union_type:
f55ee35c 21124 case DW_TAG_module:
0114d602 21125 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21126 if (TYPE_NAME (parent_type) != NULL)
21127 return TYPE_NAME (parent_type);
0114d602
DJ
21128 else
21129 /* An anonymous structure is only allowed non-static data
21130 members; no typedefs, no member functions, et cetera.
21131 So it does not need a prefix. */
21132 return "";
abc72ce4 21133 case DW_TAG_compile_unit:
95554aad 21134 case DW_TAG_partial_unit:
abc72ce4
DE
21135 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21136 if (cu->language == language_cplus
fd5866f6 21137 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21138 && die->child != NULL
21139 && (die->tag == DW_TAG_class_type
21140 || die->tag == DW_TAG_structure_type
21141 || die->tag == DW_TAG_union_type))
21142 {
43816ebc 21143 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21144 if (name != NULL)
21145 return name;
21146 }
21147 return "";
0a4b0913
AB
21148 case DW_TAG_subprogram:
21149 /* Nested subroutines in Fortran get a prefix with the name
21150 of the parent's subroutine. */
21151 if (cu->language == language_fortran)
21152 {
21153 if ((die->tag == DW_TAG_subprogram)
21154 && (dwarf2_name (parent, cu) != NULL))
21155 return dwarf2_name (parent, cu);
21156 }
21157 return determine_prefix (parent, cu);
3d567982
TT
21158 case DW_TAG_enumeration_type:
21159 parent_type = read_type_die (parent, cu);
21160 if (TYPE_DECLARED_CLASS (parent_type))
21161 {
e86ca25f
TT
21162 if (TYPE_NAME (parent_type) != NULL)
21163 return TYPE_NAME (parent_type);
3d567982
TT
21164 return "";
21165 }
21166 /* Fall through. */
63d06c5c 21167 default:
8176b9b8 21168 return determine_prefix (parent, cu);
63d06c5c 21169 }
63d06c5c
DC
21170}
21171
3e43a32a
MS
21172/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21173 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21174 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21175 an obconcat, otherwise allocate storage for the result. The CU argument is
21176 used to determine the language and hence, the appropriate separator. */
987504bb 21177
f55ee35c 21178#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21179
21180static char *
f55ee35c
JK
21181typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21182 int physname, struct dwarf2_cu *cu)
63d06c5c 21183{
f55ee35c 21184 const char *lead = "";
5c315b68 21185 const char *sep;
63d06c5c 21186
3e43a32a
MS
21187 if (suffix == NULL || suffix[0] == '\0'
21188 || prefix == NULL || prefix[0] == '\0')
987504bb 21189 sep = "";
45280282
IB
21190 else if (cu->language == language_d)
21191 {
21192 /* For D, the 'main' function could be defined in any module, but it
21193 should never be prefixed. */
21194 if (strcmp (suffix, "D main") == 0)
21195 {
21196 prefix = "";
21197 sep = "";
21198 }
21199 else
21200 sep = ".";
21201 }
f55ee35c
JK
21202 else if (cu->language == language_fortran && physname)
21203 {
21204 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21205 DW_AT_MIPS_linkage_name is preferred and used instead. */
21206
21207 lead = "__";
21208 sep = "_MOD_";
21209 }
987504bb
JJ
21210 else
21211 sep = "::";
63d06c5c 21212
6dd47d34
DE
21213 if (prefix == NULL)
21214 prefix = "";
21215 if (suffix == NULL)
21216 suffix = "";
21217
987504bb
JJ
21218 if (obs == NULL)
21219 {
3e43a32a 21220 char *retval
224c3ddb
SM
21221 = ((char *)
21222 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21223
f55ee35c
JK
21224 strcpy (retval, lead);
21225 strcat (retval, prefix);
6dd47d34
DE
21226 strcat (retval, sep);
21227 strcat (retval, suffix);
63d06c5c
DC
21228 return retval;
21229 }
987504bb
JJ
21230 else
21231 {
21232 /* We have an obstack. */
f55ee35c 21233 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21234 }
63d06c5c
DC
21235}
21236
71c25dea
TT
21237/* Get name of a die, return NULL if not found. */
21238
15d034d0
TT
21239static const char *
21240dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21241 struct objfile *objfile)
71c25dea
TT
21242{
21243 if (name && cu->language == language_cplus)
21244 {
2f408ecb 21245 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21246
2f408ecb 21247 if (!canon_name.empty ())
71c25dea 21248 {
2f408ecb 21249 if (canon_name != name)
be1e3d3e 21250 name = objfile->intern (canon_name);
71c25dea
TT
21251 }
21252 }
21253
21254 return name;
c906108c
SS
21255}
21256
96553a0c
DE
21257/* Get name of a die, return NULL if not found.
21258 Anonymous namespaces are converted to their magic string. */
9219021c 21259
15d034d0 21260static const char *
e142c38c 21261dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21262{
21263 struct attribute *attr;
518817b3 21264 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21265
e142c38c 21266 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21267 if ((!attr || !DW_STRING (attr))
96553a0c 21268 && die->tag != DW_TAG_namespace
53832f31
TT
21269 && die->tag != DW_TAG_class_type
21270 && die->tag != DW_TAG_interface_type
21271 && die->tag != DW_TAG_structure_type
21272 && die->tag != DW_TAG_union_type)
71c25dea
TT
21273 return NULL;
21274
21275 switch (die->tag)
21276 {
21277 case DW_TAG_compile_unit:
95554aad 21278 case DW_TAG_partial_unit:
71c25dea
TT
21279 /* Compilation units have a DW_AT_name that is a filename, not
21280 a source language identifier. */
21281 case DW_TAG_enumeration_type:
21282 case DW_TAG_enumerator:
21283 /* These tags always have simple identifiers already; no need
21284 to canonicalize them. */
21285 return DW_STRING (attr);
907af001 21286
96553a0c
DE
21287 case DW_TAG_namespace:
21288 if (attr != NULL && DW_STRING (attr) != NULL)
21289 return DW_STRING (attr);
21290 return CP_ANONYMOUS_NAMESPACE_STR;
21291
907af001
UW
21292 case DW_TAG_class_type:
21293 case DW_TAG_interface_type:
21294 case DW_TAG_structure_type:
21295 case DW_TAG_union_type:
21296 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21297 structures or unions. These were of the form "._%d" in GCC 4.1,
21298 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21299 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21300 if (attr && DW_STRING (attr)
61012eef
GB
21301 && (startswith (DW_STRING (attr), "._")
21302 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21303 return NULL;
53832f31
TT
21304
21305 /* GCC might emit a nameless typedef that has a linkage name. See
21306 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21307 if (!attr || DW_STRING (attr) == NULL)
21308 {
73b9be8b 21309 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21310 if (attr == NULL || DW_STRING (attr) == NULL)
21311 return NULL;
21312
df5c6c50
JK
21313 /* Avoid demangling DW_STRING (attr) the second time on a second
21314 call for the same DIE. */
21315 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21316 {
43816ebc
TT
21317 gdb::unique_xmalloc_ptr<char> demangled
21318 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21319 if (demangled == nullptr)
21320 return nullptr;
43816ebc 21321
be1e3d3e 21322 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21323 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21324 }
67430cd0
TT
21325
21326 /* Strip any leading namespaces/classes, keep only the base name.
21327 DW_AT_name for named DIEs does not contain the prefixes. */
21328 const char *base = strrchr (DW_STRING (attr), ':');
21329 if (base && base > DW_STRING (attr) && base[-1] == ':')
21330 return &base[1];
21331 else
21332 return DW_STRING (attr);
53832f31 21333 }
907af001
UW
21334 break;
21335
71c25dea 21336 default:
907af001
UW
21337 break;
21338 }
21339
21340 if (!DW_STRING_IS_CANONICAL (attr))
21341 {
be1e3d3e
TT
21342 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21343 objfile);
907af001 21344 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21345 }
907af001 21346 return DW_STRING (attr);
9219021c
DC
21347}
21348
21349/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21350 is none. *EXT_CU is the CU containing DIE on input, and the CU
21351 containing the return value on output. */
9219021c
DC
21352
21353static struct die_info *
f2f0e013 21354dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21355{
21356 struct attribute *attr;
9219021c 21357
f2f0e013 21358 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21359 if (attr == NULL)
21360 return NULL;
21361
f2f0e013 21362 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21363}
21364
fa9c3fa0
TT
21365/* A convenience function that returns an "unknown" DWARF name,
21366 including the value of V. STR is the name of the entity being
21367 printed, e.g., "TAG". */
21368
21369static const char *
21370dwarf_unknown (const char *str, unsigned v)
21371{
21372 char *cell = get_print_cell ();
21373 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21374 return cell;
21375}
21376
c906108c
SS
21377/* Convert a DIE tag into its string name. */
21378
f39c6ffd 21379static const char *
aa1ee363 21380dwarf_tag_name (unsigned tag)
c906108c 21381{
f39c6ffd
TT
21382 const char *name = get_DW_TAG_name (tag);
21383
21384 if (name == NULL)
fa9c3fa0 21385 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
21386
21387 return name;
c906108c
SS
21388}
21389
21390/* Convert a DWARF attribute code into its string name. */
21391
f39c6ffd 21392static const char *
aa1ee363 21393dwarf_attr_name (unsigned attr)
c906108c 21394{
f39c6ffd
TT
21395 const char *name;
21396
c764a876 21397#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21398 if (attr == DW_AT_MIPS_fde)
21399 return "DW_AT_MIPS_fde";
21400#else
21401 if (attr == DW_AT_HP_block_index)
21402 return "DW_AT_HP_block_index";
c764a876 21403#endif
f39c6ffd
TT
21404
21405 name = get_DW_AT_name (attr);
21406
21407 if (name == NULL)
fa9c3fa0 21408 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
21409
21410 return name;
c906108c
SS
21411}
21412
21413/* Convert a DWARF value form code into its string name. */
21414
f39c6ffd 21415static const char *
aa1ee363 21416dwarf_form_name (unsigned form)
c906108c 21417{
f39c6ffd
TT
21418 const char *name = get_DW_FORM_name (form);
21419
21420 if (name == NULL)
fa9c3fa0 21421 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
21422
21423 return name;
c906108c
SS
21424}
21425
a121b7c1 21426static const char *
fba45db2 21427dwarf_bool_name (unsigned mybool)
c906108c
SS
21428{
21429 if (mybool)
21430 return "TRUE";
21431 else
21432 return "FALSE";
21433}
21434
21435/* Convert a DWARF type code into its string name. */
21436
f39c6ffd 21437static const char *
aa1ee363 21438dwarf_type_encoding_name (unsigned enc)
c906108c 21439{
f39c6ffd 21440 const char *name = get_DW_ATE_name (enc);
c906108c 21441
f39c6ffd 21442 if (name == NULL)
fa9c3fa0 21443 return dwarf_unknown ("ATE", enc);
c906108c 21444
f39c6ffd 21445 return name;
c906108c 21446}
c906108c 21447
f9aca02d 21448static void
d97bc12b 21449dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21450{
21451 unsigned int i;
21452
d97bc12b 21453 print_spaces (indent, f);
9d8780f0 21454 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21455 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21456 sect_offset_str (die->sect_off));
d97bc12b
DE
21457
21458 if (die->parent != NULL)
21459 {
21460 print_spaces (indent, f);
9d8780f0
SM
21461 fprintf_unfiltered (f, " parent at offset: %s\n",
21462 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21463 }
21464
21465 print_spaces (indent, f);
21466 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21467 dwarf_bool_name (die->child != NULL));
c906108c 21468
d97bc12b
DE
21469 print_spaces (indent, f);
21470 fprintf_unfiltered (f, " attributes:\n");
21471
c906108c
SS
21472 for (i = 0; i < die->num_attrs; ++i)
21473 {
d97bc12b
DE
21474 print_spaces (indent, f);
21475 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21476 dwarf_attr_name (die->attrs[i].name),
21477 dwarf_form_name (die->attrs[i].form));
d97bc12b 21478
c906108c
SS
21479 switch (die->attrs[i].form)
21480 {
c906108c 21481 case DW_FORM_addr:
336d760d 21482 case DW_FORM_addrx:
3019eac3 21483 case DW_FORM_GNU_addr_index:
d97bc12b 21484 fprintf_unfiltered (f, "address: ");
5af949e3 21485 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21486 break;
21487 case DW_FORM_block2:
21488 case DW_FORM_block4:
21489 case DW_FORM_block:
21490 case DW_FORM_block1:
56eb65bd
SP
21491 fprintf_unfiltered (f, "block: size %s",
21492 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21493 break;
2dc7f7b3 21494 case DW_FORM_exprloc:
56eb65bd
SP
21495 fprintf_unfiltered (f, "expression: size %s",
21496 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21497 break;
0224619f
JK
21498 case DW_FORM_data16:
21499 fprintf_unfiltered (f, "constant of 16 bytes");
21500 break;
4568ecf9
DE
21501 case DW_FORM_ref_addr:
21502 fprintf_unfiltered (f, "ref address: ");
21503 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21504 break;
36586728
TT
21505 case DW_FORM_GNU_ref_alt:
21506 fprintf_unfiltered (f, "alt ref address: ");
21507 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21508 break;
10b3939b
DJ
21509 case DW_FORM_ref1:
21510 case DW_FORM_ref2:
21511 case DW_FORM_ref4:
4568ecf9
DE
21512 case DW_FORM_ref8:
21513 case DW_FORM_ref_udata:
d97bc12b 21514 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21515 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21516 break;
c906108c
SS
21517 case DW_FORM_data1:
21518 case DW_FORM_data2:
21519 case DW_FORM_data4:
ce5d95e1 21520 case DW_FORM_data8:
c906108c
SS
21521 case DW_FORM_udata:
21522 case DW_FORM_sdata:
43bbcdc2
PH
21523 fprintf_unfiltered (f, "constant: %s",
21524 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21525 break;
2dc7f7b3
TT
21526 case DW_FORM_sec_offset:
21527 fprintf_unfiltered (f, "section offset: %s",
21528 pulongest (DW_UNSND (&die->attrs[i])));
21529 break;
55f1336d 21530 case DW_FORM_ref_sig8:
ac9ec31b
DE
21531 fprintf_unfiltered (f, "signature: %s",
21532 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21533 break;
c906108c 21534 case DW_FORM_string:
4bdf3d34 21535 case DW_FORM_strp:
43988095 21536 case DW_FORM_line_strp:
cf532bd1 21537 case DW_FORM_strx:
3019eac3 21538 case DW_FORM_GNU_str_index:
36586728 21539 case DW_FORM_GNU_strp_alt:
8285870a 21540 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21541 DW_STRING (&die->attrs[i])
8285870a
JK
21542 ? DW_STRING (&die->attrs[i]) : "",
21543 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21544 break;
21545 case DW_FORM_flag:
21546 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21547 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21548 else
d97bc12b 21549 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21550 break;
2dc7f7b3
TT
21551 case DW_FORM_flag_present:
21552 fprintf_unfiltered (f, "flag: TRUE");
21553 break;
a8329558 21554 case DW_FORM_indirect:
0963b4bd
MS
21555 /* The reader will have reduced the indirect form to
21556 the "base form" so this form should not occur. */
5f48f8f3 21557 fprintf_unfiltered (f,
3e43a32a 21558 "unexpected attribute form: DW_FORM_indirect");
a8329558 21559 break;
663c44ac
JK
21560 case DW_FORM_implicit_const:
21561 fprintf_unfiltered (f, "constant: %s",
21562 plongest (DW_SND (&die->attrs[i])));
21563 break;
c906108c 21564 default:
d97bc12b 21565 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21566 die->attrs[i].form);
d97bc12b 21567 break;
c906108c 21568 }
d97bc12b 21569 fprintf_unfiltered (f, "\n");
c906108c
SS
21570 }
21571}
21572
f9aca02d 21573static void
d97bc12b 21574dump_die_for_error (struct die_info *die)
c906108c 21575{
d97bc12b
DE
21576 dump_die_shallow (gdb_stderr, 0, die);
21577}
21578
21579static void
21580dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21581{
21582 int indent = level * 4;
21583
21584 gdb_assert (die != NULL);
21585
21586 if (level >= max_level)
21587 return;
21588
21589 dump_die_shallow (f, indent, die);
21590
21591 if (die->child != NULL)
c906108c 21592 {
d97bc12b
DE
21593 print_spaces (indent, f);
21594 fprintf_unfiltered (f, " Children:");
21595 if (level + 1 < max_level)
21596 {
21597 fprintf_unfiltered (f, "\n");
21598 dump_die_1 (f, level + 1, max_level, die->child);
21599 }
21600 else
21601 {
3e43a32a
MS
21602 fprintf_unfiltered (f,
21603 " [not printed, max nesting level reached]\n");
d97bc12b
DE
21604 }
21605 }
21606
21607 if (die->sibling != NULL && level > 0)
21608 {
21609 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
21610 }
21611}
21612
d97bc12b
DE
21613/* This is called from the pdie macro in gdbinit.in.
21614 It's not static so gcc will keep a copy callable from gdb. */
21615
21616void
21617dump_die (struct die_info *die, int max_level)
21618{
21619 dump_die_1 (gdb_stdlog, 0, max_level, die);
21620}
21621
f9aca02d 21622static void
51545339 21623store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21624{
51545339 21625 void **slot;
c906108c 21626
9c541725
PA
21627 slot = htab_find_slot_with_hash (cu->die_hash, die,
21628 to_underlying (die->sect_off),
b64f50a1 21629 INSERT);
51545339
DJ
21630
21631 *slot = die;
c906108c
SS
21632}
21633
b64f50a1
JK
21634/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21635 required kind. */
21636
21637static sect_offset
ff39bb5e 21638dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 21639{
cd6c91b4 21640 if (attr->form_is_ref ())
9c541725 21641 return (sect_offset) DW_UNSND (attr);
93311388 21642
b98664d3 21643 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 21644 dwarf_form_name (attr->form));
9c541725 21645 return {};
c906108c
SS
21646}
21647
43bbcdc2
PH
21648/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21649 * the value held by the attribute is not constant. */
a02abb62 21650
43bbcdc2 21651static LONGEST
ff39bb5e 21652dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 21653{
663c44ac 21654 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
21655 return DW_SND (attr);
21656 else if (attr->form == DW_FORM_udata
21657 || attr->form == DW_FORM_data1
21658 || attr->form == DW_FORM_data2
21659 || attr->form == DW_FORM_data4
21660 || attr->form == DW_FORM_data8)
21661 return DW_UNSND (attr);
21662 else
21663 {
cd6c91b4 21664 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 21665 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
21666 dwarf_form_name (attr->form));
21667 return default_value;
21668 }
21669}
21670
348e048f
DE
21671/* Follow reference or signature attribute ATTR of SRC_DIE.
21672 On entry *REF_CU is the CU of SRC_DIE.
21673 On exit *REF_CU is the CU of the result. */
21674
21675static struct die_info *
ff39bb5e 21676follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
21677 struct dwarf2_cu **ref_cu)
21678{
21679 struct die_info *die;
21680
cd6c91b4 21681 if (attr->form_is_ref ())
348e048f 21682 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 21683 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
21684 die = follow_die_sig (src_die, attr, ref_cu);
21685 else
21686 {
21687 dump_die_for_error (src_die);
21688 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 21689 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
21690 }
21691
21692 return die;
03dd20cc
DJ
21693}
21694
5c631832 21695/* Follow reference OFFSET.
673bfd45
DE
21696 On entry *REF_CU is the CU of the source die referencing OFFSET.
21697 On exit *REF_CU is the CU of the result.
21698 Returns NULL if OFFSET is invalid. */
f504f079 21699
f9aca02d 21700static struct die_info *
9c541725 21701follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 21702 struct dwarf2_cu **ref_cu)
c906108c 21703{
10b3939b 21704 struct die_info temp_die;
f2f0e013 21705 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
21706 struct dwarf2_per_objfile *dwarf2_per_objfile
21707 = cu->per_cu->dwarf2_per_objfile;
10b3939b 21708
348e048f
DE
21709 gdb_assert (cu->per_cu != NULL);
21710
98bfdba5
PA
21711 target_cu = cu;
21712
3019eac3 21713 if (cu->per_cu->is_debug_types)
348e048f
DE
21714 {
21715 /* .debug_types CUs cannot reference anything outside their CU.
21716 If they need to, they have to reference a signatured type via
55f1336d 21717 DW_FORM_ref_sig8. */
4057dfde 21718 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 21719 return NULL;
348e048f 21720 }
36586728 21721 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 21722 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
21723 {
21724 struct dwarf2_per_cu_data *per_cu;
9a619af0 21725
9c541725 21726 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 21727 dwarf2_per_objfile);
03dd20cc
DJ
21728
21729 /* If necessary, add it to the queue and load its DIEs. */
95554aad 21730 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 21731 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 21732
10b3939b
DJ
21733 target_cu = per_cu->cu;
21734 }
98bfdba5
PA
21735 else if (cu->dies == NULL)
21736 {
21737 /* We're loading full DIEs during partial symbol reading. */
21738 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 21739 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 21740 }
c906108c 21741
f2f0e013 21742 *ref_cu = target_cu;
9c541725 21743 temp_die.sect_off = sect_off;
c24bdb02
KS
21744
21745 if (target_cu != cu)
21746 target_cu->ancestor = cu;
21747
9a3c8263 21748 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
21749 &temp_die,
21750 to_underlying (sect_off));
5c631832 21751}
10b3939b 21752
5c631832
JK
21753/* Follow reference attribute ATTR of SRC_DIE.
21754 On entry *REF_CU is the CU of SRC_DIE.
21755 On exit *REF_CU is the CU of the result. */
21756
21757static struct die_info *
ff39bb5e 21758follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
21759 struct dwarf2_cu **ref_cu)
21760{
9c541725 21761 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
21762 struct dwarf2_cu *cu = *ref_cu;
21763 struct die_info *die;
21764
9c541725 21765 die = follow_die_offset (sect_off,
36586728
TT
21766 (attr->form == DW_FORM_GNU_ref_alt
21767 || cu->per_cu->is_dwz),
21768 ref_cu);
5c631832 21769 if (!die)
9d8780f0
SM
21770 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21771 "at %s [in module %s]"),
21772 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 21773 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 21774
5c631832
JK
21775 return die;
21776}
21777
d4c9a4f8 21778/* See read.h. */
5c631832
JK
21779
21780struct dwarf2_locexpr_baton
9c541725 21781dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 21782 dwarf2_per_cu_data *per_cu,
8b9737bf 21783 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 21784 void *baton, bool resolve_abstract_p)
5c631832 21785{
918dd910 21786 struct dwarf2_cu *cu;
5c631832
JK
21787 struct die_info *die;
21788 struct attribute *attr;
21789 struct dwarf2_locexpr_baton retval;
12359b5e
SM
21790 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21791 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 21792
918dd910 21793 if (per_cu->cu == NULL)
58f0c718 21794 load_cu (per_cu, false);
918dd910 21795 cu = per_cu->cu;
cc12ce38
DE
21796 if (cu == NULL)
21797 {
21798 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21799 Instead just throw an error, not much else we can do. */
9d8780f0
SM
21800 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21801 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 21802 }
918dd910 21803
9c541725 21804 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 21805 if (!die)
9d8780f0
SM
21806 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21807 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
21808
21809 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 21810 if (!attr && resolve_abstract_p
3360b6e7 21811 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
21812 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21813 {
21814 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 21815 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 21816 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 21817
3360b6e7
TV
21818 for (const auto &cand_off
21819 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 21820 {
3360b6e7
TV
21821 struct dwarf2_cu *cand_cu = cu;
21822 struct die_info *cand
21823 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21824 if (!cand
21825 || !cand->parent
e4a62c65
TV
21826 || cand->parent->tag != DW_TAG_subprogram)
21827 continue;
21828
21829 CORE_ADDR pc_low, pc_high;
21830 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
21831 if (pc_low == ((CORE_ADDR) -1))
21832 continue;
21833 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21834 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21835 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
21836 continue;
21837
21838 die = cand;
21839 attr = dwarf2_attr (die, DW_AT_location, cu);
21840 break;
21841 }
21842 }
21843
5c631832
JK
21844 if (!attr)
21845 {
e103e986
JK
21846 /* DWARF: "If there is no such attribute, then there is no effect.".
21847 DATA is ignored if SIZE is 0. */
5c631832 21848
e103e986 21849 retval.data = NULL;
5c631832
JK
21850 retval.size = 0;
21851 }
cd6c91b4 21852 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
21853 {
21854 struct dwarf2_loclist_baton loclist_baton;
21855 CORE_ADDR pc = (*get_frame_pc) (baton);
21856 size_t size;
21857
21858 fill_in_loclist_baton (cu, &loclist_baton, attr);
21859
21860 retval.data = dwarf2_find_location_expression (&loclist_baton,
21861 &size, pc);
21862 retval.size = size;
21863 }
5c631832
JK
21864 else
21865 {
4fc6c0d5 21866 if (!attr->form_is_block ())
9d8780f0 21867 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 21868 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 21869 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
21870
21871 retval.data = DW_BLOCK (attr)->data;
21872 retval.size = DW_BLOCK (attr)->size;
21873 }
21874 retval.per_cu = cu->per_cu;
918dd910 21875
ed2dc618 21876 age_cached_comp_units (dwarf2_per_objfile);
918dd910 21877
5c631832 21878 return retval;
348e048f
DE
21879}
21880
d4c9a4f8 21881/* See read.h. */
8b9737bf
TT
21882
21883struct dwarf2_locexpr_baton
21884dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 21885 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
21886 CORE_ADDR (*get_frame_pc) (void *baton),
21887 void *baton)
21888{
9c541725 21889 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 21890
9c541725 21891 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
21892}
21893
b6807d98
TT
21894/* Write a constant of a given type as target-ordered bytes into
21895 OBSTACK. */
21896
21897static const gdb_byte *
21898write_constant_as_bytes (struct obstack *obstack,
21899 enum bfd_endian byte_order,
21900 struct type *type,
21901 ULONGEST value,
21902 LONGEST *len)
21903{
21904 gdb_byte *result;
21905
21906 *len = TYPE_LENGTH (type);
224c3ddb 21907 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21908 store_unsigned_integer (result, *len, byte_order, value);
21909
21910 return result;
21911}
21912
d4c9a4f8 21913/* See read.h. */
b6807d98
TT
21914
21915const gdb_byte *
9c541725 21916dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
21917 dwarf2_per_cu_data *per_cu,
21918 obstack *obstack,
b6807d98
TT
21919 LONGEST *len)
21920{
21921 struct dwarf2_cu *cu;
21922 struct die_info *die;
21923 struct attribute *attr;
21924 const gdb_byte *result = NULL;
21925 struct type *type;
21926 LONGEST value;
21927 enum bfd_endian byte_order;
e3b94546 21928 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 21929
b6807d98 21930 if (per_cu->cu == NULL)
58f0c718 21931 load_cu (per_cu, false);
b6807d98 21932 cu = per_cu->cu;
cc12ce38
DE
21933 if (cu == NULL)
21934 {
21935 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21936 Instead just throw an error, not much else we can do. */
9d8780f0
SM
21937 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21938 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 21939 }
b6807d98 21940
9c541725 21941 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 21942 if (!die)
9d8780f0
SM
21943 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21944 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
21945
21946 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21947 if (attr == NULL)
21948 return NULL;
21949
e3b94546 21950 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
21951 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21952
21953 switch (attr->form)
21954 {
21955 case DW_FORM_addr:
336d760d 21956 case DW_FORM_addrx:
b6807d98
TT
21957 case DW_FORM_GNU_addr_index:
21958 {
21959 gdb_byte *tem;
21960
21961 *len = cu->header.addr_size;
224c3ddb 21962 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21963 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21964 result = tem;
21965 }
21966 break;
21967 case DW_FORM_string:
21968 case DW_FORM_strp:
cf532bd1 21969 case DW_FORM_strx:
b6807d98
TT
21970 case DW_FORM_GNU_str_index:
21971 case DW_FORM_GNU_strp_alt:
21972 /* DW_STRING is already allocated on the objfile obstack, point
21973 directly to it. */
21974 result = (const gdb_byte *) DW_STRING (attr);
21975 *len = strlen (DW_STRING (attr));
21976 break;
21977 case DW_FORM_block1:
21978 case DW_FORM_block2:
21979 case DW_FORM_block4:
21980 case DW_FORM_block:
21981 case DW_FORM_exprloc:
0224619f 21982 case DW_FORM_data16:
b6807d98
TT
21983 result = DW_BLOCK (attr)->data;
21984 *len = DW_BLOCK (attr)->size;
21985 break;
21986
21987 /* The DW_AT_const_value attributes are supposed to carry the
21988 symbol's value "represented as it would be on the target
21989 architecture." By the time we get here, it's already been
21990 converted to host endianness, so we just need to sign- or
21991 zero-extend it as appropriate. */
21992 case DW_FORM_data1:
21993 type = die_type (die, cu);
21994 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21995 if (result == NULL)
21996 result = write_constant_as_bytes (obstack, byte_order,
21997 type, value, len);
21998 break;
21999 case DW_FORM_data2:
22000 type = die_type (die, cu);
22001 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22002 if (result == NULL)
22003 result = write_constant_as_bytes (obstack, byte_order,
22004 type, value, len);
22005 break;
22006 case DW_FORM_data4:
22007 type = die_type (die, cu);
22008 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22009 if (result == NULL)
22010 result = write_constant_as_bytes (obstack, byte_order,
22011 type, value, len);
22012 break;
22013 case DW_FORM_data8:
22014 type = die_type (die, cu);
22015 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22016 if (result == NULL)
22017 result = write_constant_as_bytes (obstack, byte_order,
22018 type, value, len);
22019 break;
22020
22021 case DW_FORM_sdata:
663c44ac 22022 case DW_FORM_implicit_const:
b6807d98
TT
22023 type = die_type (die, cu);
22024 result = write_constant_as_bytes (obstack, byte_order,
22025 type, DW_SND (attr), len);
22026 break;
22027
22028 case DW_FORM_udata:
22029 type = die_type (die, cu);
22030 result = write_constant_as_bytes (obstack, byte_order,
22031 type, DW_UNSND (attr), len);
22032 break;
22033
22034 default:
b98664d3 22035 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22036 dwarf_form_name (attr->form));
22037 break;
22038 }
22039
22040 return result;
22041}
22042
d4c9a4f8 22043/* See read.h. */
7942e96e
AA
22044
22045struct type *
9c541725 22046dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22047 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22048{
22049 struct dwarf2_cu *cu;
22050 struct die_info *die;
22051
7942e96e 22052 if (per_cu->cu == NULL)
58f0c718 22053 load_cu (per_cu, false);
7942e96e
AA
22054 cu = per_cu->cu;
22055 if (!cu)
22056 return NULL;
22057
9c541725 22058 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22059 if (!die)
22060 return NULL;
22061
22062 return die_type (die, cu);
22063}
22064
8cb5117c 22065/* See read.h. */
8a9b8146
TT
22066
22067struct type *
b64f50a1 22068dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22069 struct dwarf2_per_cu_data *per_cu)
22070{
9c541725 22071 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22072 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22073}
22074
ac9ec31b 22075/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22076 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22077 On exit *REF_CU is the CU of the result.
22078 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22079
22080static struct die_info *
ac9ec31b
DE
22081follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22082 struct dwarf2_cu **ref_cu)
348e048f 22083{
348e048f 22084 struct die_info temp_die;
c24bdb02 22085 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22086 struct die_info *die;
22087
ac9ec31b
DE
22088 /* While it might be nice to assert sig_type->type == NULL here,
22089 we can get here for DW_AT_imported_declaration where we need
22090 the DIE not the type. */
348e048f
DE
22091
22092 /* If necessary, add it to the queue and load its DIEs. */
22093
95554aad 22094 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22095 read_signatured_type (sig_type);
348e048f 22096
348e048f 22097 sig_cu = sig_type->per_cu.cu;
69d751e3 22098 gdb_assert (sig_cu != NULL);
9c541725
PA
22099 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22100 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22101 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22102 to_underlying (temp_die.sect_off));
348e048f
DE
22103 if (die)
22104 {
ed2dc618 22105 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22106 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22107
796a7ff8
DE
22108 /* For .gdb_index version 7 keep track of included TUs.
22109 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22110 if (dwarf2_per_objfile->index_table != NULL
22111 && dwarf2_per_objfile->index_table->version <= 7)
22112 {
ae640021 22113 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22114 }
22115
348e048f 22116 *ref_cu = sig_cu;
c24bdb02
KS
22117 if (sig_cu != cu)
22118 sig_cu->ancestor = cu;
22119
348e048f
DE
22120 return die;
22121 }
22122
ac9ec31b
DE
22123 return NULL;
22124}
22125
22126/* Follow signatured type referenced by ATTR in SRC_DIE.
22127 On entry *REF_CU is the CU of SRC_DIE.
22128 On exit *REF_CU is the CU of the result.
22129 The result is the DIE of the type.
22130 If the referenced type cannot be found an error is thrown. */
22131
22132static struct die_info *
ff39bb5e 22133follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22134 struct dwarf2_cu **ref_cu)
22135{
22136 ULONGEST signature = DW_SIGNATURE (attr);
22137 struct signatured_type *sig_type;
22138 struct die_info *die;
22139
22140 gdb_assert (attr->form == DW_FORM_ref_sig8);
22141
a2ce51a0 22142 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22143 /* sig_type will be NULL if the signatured type is missing from
22144 the debug info. */
22145 if (sig_type == NULL)
22146 {
22147 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22148 " from DIE at %s [in module %s]"),
22149 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22150 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22151 }
22152
22153 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22154 if (die == NULL)
22155 {
22156 dump_die_for_error (src_die);
22157 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22158 " from DIE at %s [in module %s]"),
22159 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22160 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22161 }
22162
22163 return die;
22164}
22165
22166/* Get the type specified by SIGNATURE referenced in DIE/CU,
22167 reading in and processing the type unit if necessary. */
22168
22169static struct type *
22170get_signatured_type (struct die_info *die, ULONGEST signature,
22171 struct dwarf2_cu *cu)
22172{
518817b3
SM
22173 struct dwarf2_per_objfile *dwarf2_per_objfile
22174 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22175 struct signatured_type *sig_type;
22176 struct dwarf2_cu *type_cu;
22177 struct die_info *type_die;
22178 struct type *type;
22179
a2ce51a0 22180 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22181 /* sig_type will be NULL if the signatured type is missing from
22182 the debug info. */
22183 if (sig_type == NULL)
22184 {
b98664d3 22185 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22186 " from DIE at %s [in module %s]"),
22187 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22188 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22189 return build_error_marker_type (cu, die);
22190 }
22191
22192 /* If we already know the type we're done. */
22193 if (sig_type->type != NULL)
22194 return sig_type->type;
22195
22196 type_cu = cu;
22197 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22198 if (type_die != NULL)
22199 {
22200 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22201 is created. This is important, for example, because for c++ classes
22202 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22203 type = read_type_die (type_die, type_cu);
22204 if (type == NULL)
22205 {
b98664d3 22206 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22207 " referenced from DIE at %s [in module %s]"),
22208 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22209 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22210 type = build_error_marker_type (cu, die);
22211 }
22212 }
22213 else
22214 {
b98664d3 22215 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22216 " from DIE at %s [in module %s]"),
22217 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22218 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22219 type = build_error_marker_type (cu, die);
22220 }
22221 sig_type->type = type;
22222
22223 return type;
22224}
22225
22226/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22227 reading in and processing the type unit if necessary. */
22228
22229static struct type *
ff39bb5e 22230get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22231 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22232{
22233 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22234 if (attr->form_is_ref ())
ac9ec31b
DE
22235 {
22236 struct dwarf2_cu *type_cu = cu;
22237 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22238
22239 return read_type_die (type_die, type_cu);
22240 }
22241 else if (attr->form == DW_FORM_ref_sig8)
22242 {
22243 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22244 }
22245 else
22246 {
518817b3
SM
22247 struct dwarf2_per_objfile *dwarf2_per_objfile
22248 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22249
b98664d3 22250 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22251 " at %s [in module %s]"),
22252 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22253 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22254 return build_error_marker_type (cu, die);
22255 }
348e048f
DE
22256}
22257
e5fe5e75 22258/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22259
22260static void
e5fe5e75 22261load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22262{
52dc124a 22263 struct signatured_type *sig_type;
348e048f 22264
f4dc4d17 22265 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22266 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22267
6721b2ec
DE
22268 /* We have the per_cu, but we need the signatured_type.
22269 Fortunately this is an easy translation. */
22270 gdb_assert (per_cu->is_debug_types);
22271 sig_type = (struct signatured_type *) per_cu;
348e048f 22272
6721b2ec 22273 gdb_assert (per_cu->cu == NULL);
348e048f 22274
52dc124a 22275 read_signatured_type (sig_type);
348e048f 22276
6721b2ec 22277 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22278}
22279
3019eac3
DE
22280/* Read in a signatured type and build its CU and DIEs.
22281 If the type is a stub for the real type in a DWO file,
22282 read in the real type from the DWO file as well. */
dee91e82
DE
22283
22284static void
22285read_signatured_type (struct signatured_type *sig_type)
22286{
22287 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22288
3019eac3 22289 gdb_assert (per_cu->is_debug_types);
dee91e82 22290 gdb_assert (per_cu->cu == NULL);
348e048f 22291
6751ebae 22292 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22293
22294 if (!reader.dummy_p)
22295 {
22296 struct dwarf2_cu *cu = reader.cu;
22297 const gdb_byte *info_ptr = reader.info_ptr;
22298
22299 gdb_assert (cu->die_hash == NULL);
22300 cu->die_hash =
22301 htab_create_alloc_ex (cu->header.length / 12,
22302 die_hash,
22303 die_eq,
22304 NULL,
22305 &cu->comp_unit_obstack,
22306 hashtab_obstack_allocate,
22307 dummy_obstack_deallocate);
22308
3e225074 22309 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22310 reader.comp_unit_die->child
22311 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22312 reader.comp_unit_die);
22313 cu->dies = reader.comp_unit_die;
22314 /* comp_unit_die is not stored in die_hash, no need. */
22315
22316 /* We try not to read any attributes in this function, because
22317 not all CUs needed for references have been loaded yet, and
22318 symbol table processing isn't initialized. But we have to
22319 set the CU language, or we won't be able to build types
22320 correctly. Similarly, if we do not read the producer, we can
22321 not apply producer-specific interpretation. */
22322 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22323
22324 reader.keep ();
c0ab21c2
TT
22325 }
22326
7ee85ab1 22327 sig_type->per_cu.tu_read = 1;
c906108c
SS
22328}
22329
c906108c
SS
22330/* Decode simple location descriptions.
22331 Given a pointer to a dwarf block that defines a location, compute
22332 the location and return the value.
22333
4cecd739
DJ
22334 NOTE drow/2003-11-18: This function is called in two situations
22335 now: for the address of static or global variables (partial symbols
22336 only) and for offsets into structures which are expected to be
22337 (more or less) constant. The partial symbol case should go away,
22338 and only the constant case should remain. That will let this
22339 function complain more accurately. A few special modes are allowed
22340 without complaint for global variables (for instance, global
22341 register values and thread-local values).
c906108c
SS
22342
22343 A location description containing no operations indicates that the
4cecd739 22344 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22345 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22346 callers will only want a very basic result and this can become a
21ae7a4d
JK
22347 complaint.
22348
22349 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22350
22351static CORE_ADDR
e7c27a73 22352decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22353{
518817b3 22354 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22355 size_t i;
22356 size_t size = blk->size;
d521ce57 22357 const gdb_byte *data = blk->data;
21ae7a4d
JK
22358 CORE_ADDR stack[64];
22359 int stacki;
22360 unsigned int bytes_read, unsnd;
22361 gdb_byte op;
c906108c 22362
21ae7a4d
JK
22363 i = 0;
22364 stacki = 0;
22365 stack[stacki] = 0;
22366 stack[++stacki] = 0;
22367
22368 while (i < size)
22369 {
22370 op = data[i++];
22371 switch (op)
22372 {
22373 case DW_OP_lit0:
22374 case DW_OP_lit1:
22375 case DW_OP_lit2:
22376 case DW_OP_lit3:
22377 case DW_OP_lit4:
22378 case DW_OP_lit5:
22379 case DW_OP_lit6:
22380 case DW_OP_lit7:
22381 case DW_OP_lit8:
22382 case DW_OP_lit9:
22383 case DW_OP_lit10:
22384 case DW_OP_lit11:
22385 case DW_OP_lit12:
22386 case DW_OP_lit13:
22387 case DW_OP_lit14:
22388 case DW_OP_lit15:
22389 case DW_OP_lit16:
22390 case DW_OP_lit17:
22391 case DW_OP_lit18:
22392 case DW_OP_lit19:
22393 case DW_OP_lit20:
22394 case DW_OP_lit21:
22395 case DW_OP_lit22:
22396 case DW_OP_lit23:
22397 case DW_OP_lit24:
22398 case DW_OP_lit25:
22399 case DW_OP_lit26:
22400 case DW_OP_lit27:
22401 case DW_OP_lit28:
22402 case DW_OP_lit29:
22403 case DW_OP_lit30:
22404 case DW_OP_lit31:
22405 stack[++stacki] = op - DW_OP_lit0;
22406 break;
f1bea926 22407
21ae7a4d
JK
22408 case DW_OP_reg0:
22409 case DW_OP_reg1:
22410 case DW_OP_reg2:
22411 case DW_OP_reg3:
22412 case DW_OP_reg4:
22413 case DW_OP_reg5:
22414 case DW_OP_reg6:
22415 case DW_OP_reg7:
22416 case DW_OP_reg8:
22417 case DW_OP_reg9:
22418 case DW_OP_reg10:
22419 case DW_OP_reg11:
22420 case DW_OP_reg12:
22421 case DW_OP_reg13:
22422 case DW_OP_reg14:
22423 case DW_OP_reg15:
22424 case DW_OP_reg16:
22425 case DW_OP_reg17:
22426 case DW_OP_reg18:
22427 case DW_OP_reg19:
22428 case DW_OP_reg20:
22429 case DW_OP_reg21:
22430 case DW_OP_reg22:
22431 case DW_OP_reg23:
22432 case DW_OP_reg24:
22433 case DW_OP_reg25:
22434 case DW_OP_reg26:
22435 case DW_OP_reg27:
22436 case DW_OP_reg28:
22437 case DW_OP_reg29:
22438 case DW_OP_reg30:
22439 case DW_OP_reg31:
22440 stack[++stacki] = op - DW_OP_reg0;
22441 if (i < size)
22442 dwarf2_complex_location_expr_complaint ();
22443 break;
c906108c 22444
21ae7a4d
JK
22445 case DW_OP_regx:
22446 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22447 i += bytes_read;
22448 stack[++stacki] = unsnd;
22449 if (i < size)
22450 dwarf2_complex_location_expr_complaint ();
22451 break;
c906108c 22452
21ae7a4d 22453 case DW_OP_addr:
c8a7a66f
TT
22454 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22455 &bytes_read);
21ae7a4d
JK
22456 i += bytes_read;
22457 break;
d53d4ac5 22458
21ae7a4d
JK
22459 case DW_OP_const1u:
22460 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22461 i += 1;
22462 break;
22463
22464 case DW_OP_const1s:
22465 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22466 i += 1;
22467 break;
22468
22469 case DW_OP_const2u:
22470 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22471 i += 2;
22472 break;
22473
22474 case DW_OP_const2s:
22475 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22476 i += 2;
22477 break;
d53d4ac5 22478
21ae7a4d
JK
22479 case DW_OP_const4u:
22480 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22481 i += 4;
22482 break;
22483
22484 case DW_OP_const4s:
22485 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22486 i += 4;
22487 break;
22488
585861ea
JK
22489 case DW_OP_const8u:
22490 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22491 i += 8;
22492 break;
22493
21ae7a4d
JK
22494 case DW_OP_constu:
22495 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22496 &bytes_read);
22497 i += bytes_read;
22498 break;
22499
22500 case DW_OP_consts:
22501 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22502 i += bytes_read;
22503 break;
22504
22505 case DW_OP_dup:
22506 stack[stacki + 1] = stack[stacki];
22507 stacki++;
22508 break;
22509
22510 case DW_OP_plus:
22511 stack[stacki - 1] += stack[stacki];
22512 stacki--;
22513 break;
22514
22515 case DW_OP_plus_uconst:
22516 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22517 &bytes_read);
22518 i += bytes_read;
22519 break;
22520
22521 case DW_OP_minus:
22522 stack[stacki - 1] -= stack[stacki];
22523 stacki--;
22524 break;
22525
22526 case DW_OP_deref:
22527 /* If we're not the last op, then we definitely can't encode
22528 this using GDB's address_class enum. This is valid for partial
22529 global symbols, although the variable's address will be bogus
22530 in the psymtab. */
22531 if (i < size)
22532 dwarf2_complex_location_expr_complaint ();
22533 break;
22534
22535 case DW_OP_GNU_push_tls_address:
4aa4e28b 22536 case DW_OP_form_tls_address:
21ae7a4d
JK
22537 /* The top of the stack has the offset from the beginning
22538 of the thread control block at which the variable is located. */
22539 /* Nothing should follow this operator, so the top of stack would
22540 be returned. */
22541 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22542 address will be bogus in the psymtab. Make it always at least
22543 non-zero to not look as a variable garbage collected by linker
22544 which have DW_OP_addr 0. */
21ae7a4d
JK
22545 if (i < size)
22546 dwarf2_complex_location_expr_complaint ();
585861ea 22547 stack[stacki]++;
21ae7a4d
JK
22548 break;
22549
22550 case DW_OP_GNU_uninit:
22551 break;
22552
336d760d 22553 case DW_OP_addrx:
3019eac3 22554 case DW_OP_GNU_addr_index:
49f6c839 22555 case DW_OP_GNU_const_index:
3019eac3
DE
22556 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22557 &bytes_read);
22558 i += bytes_read;
22559 break;
22560
21ae7a4d
JK
22561 default:
22562 {
f39c6ffd 22563 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22564
22565 if (name)
b98664d3 22566 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
22567 name);
22568 else
b98664d3 22569 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
22570 op);
22571 }
22572
22573 return (stack[stacki]);
d53d4ac5 22574 }
3c6e0cb3 22575
21ae7a4d
JK
22576 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22577 outside of the allocated space. Also enforce minimum>0. */
22578 if (stacki >= ARRAY_SIZE (stack) - 1)
22579 {
b98664d3 22580 complaint (_("location description stack overflow"));
21ae7a4d
JK
22581 return 0;
22582 }
22583
22584 if (stacki <= 0)
22585 {
b98664d3 22586 complaint (_("location description stack underflow"));
21ae7a4d
JK
22587 return 0;
22588 }
22589 }
22590 return (stack[stacki]);
c906108c
SS
22591}
22592
22593/* memory allocation interface */
22594
c906108c 22595static struct dwarf_block *
7b5a2f43 22596dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22597{
8d749320 22598 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22599}
22600
c906108c 22601static struct die_info *
b60c80d6 22602dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22603{
22604 struct die_info *die;
b60c80d6
DJ
22605 size_t size = sizeof (struct die_info);
22606
22607 if (num_attrs > 1)
22608 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22609
b60c80d6 22610 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
22611 memset (die, 0, sizeof (struct die_info));
22612 return (die);
22613}
2e276125
JB
22614
22615\f
a036ba48 22616
c90ec28a 22617/* Macro support. */
cf2c3c16 22618
9eac9650
TT
22619/* An overload of dwarf_decode_macros that finds the correct section
22620 and ensures it is read in before calling the other overload. */
22621
22622static void
22623dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22624 int section_is_gnu)
22625{
22626 struct dwarf2_per_objfile *dwarf2_per_objfile
22627 = cu->per_cu->dwarf2_per_objfile;
22628 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 22629 const struct line_header *lh = cu->line_header;
9eac9650
TT
22630 unsigned int offset_size = cu->header.offset_size;
22631 struct dwarf2_section_info *section;
22632 const char *section_name;
22633
22634 if (cu->dwo_unit != nullptr)
22635 {
22636 if (section_is_gnu)
22637 {
22638 section = &cu->dwo_unit->dwo_file->sections.macro;
22639 section_name = ".debug_macro.dwo";
22640 }
22641 else
22642 {
22643 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22644 section_name = ".debug_macinfo.dwo";
22645 }
22646 }
22647 else
22648 {
22649 if (section_is_gnu)
22650 {
22651 section = &dwarf2_per_objfile->macro;
22652 section_name = ".debug_macro";
22653 }
22654 else
22655 {
22656 section = &dwarf2_per_objfile->macinfo;
22657 section_name = ".debug_macinfo";
22658 }
22659 }
22660
22661 section->read (objfile);
22662 if (section->buffer == nullptr)
22663 {
22664 complaint (_("missing %s section"), section_name);
22665 return;
22666 }
22667
22668 buildsym_compunit *builder = cu->get_builder ();
22669
22670 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22671 offset_size, offset, section_is_gnu);
22672}
22673
3019eac3
DE
22674/* Return the .debug_loc section to use for CU.
22675 For DWO files use .debug_loc.dwo. */
22676
22677static struct dwarf2_section_info *
22678cu_debug_loc_section (struct dwarf2_cu *cu)
22679{
518817b3
SM
22680 struct dwarf2_per_objfile *dwarf2_per_objfile
22681 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22682
3019eac3 22683 if (cu->dwo_unit)
43988095
JK
22684 {
22685 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 22686
43988095
JK
22687 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22688 }
22689 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22690 : &dwarf2_per_objfile->loc);
3019eac3
DE
22691}
22692
8cf6f0b1
TT
22693/* A helper function that fills in a dwarf2_loclist_baton. */
22694
22695static void
22696fill_in_loclist_baton (struct dwarf2_cu *cu,
22697 struct dwarf2_loclist_baton *baton,
ff39bb5e 22698 const struct attribute *attr)
8cf6f0b1 22699{
518817b3
SM
22700 struct dwarf2_per_objfile *dwarf2_per_objfile
22701 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
22702 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22703
96b79293 22704 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
22705
22706 baton->per_cu = cu->per_cu;
22707 gdb_assert (baton->per_cu);
22708 /* We don't know how long the location list is, but make sure we
22709 don't run off the edge of the section. */
3019eac3
DE
22710 baton->size = section->size - DW_UNSND (attr);
22711 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
22712 if (cu->base_address.has_value ())
22713 baton->base_address = *cu->base_address;
22714 else
22715 baton->base_address = 0;
f664829e 22716 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22717}
22718
4c2df51b 22719static void
ff39bb5e 22720dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22721 struct dwarf2_cu *cu, int is_block)
4c2df51b 22722{
518817b3
SM
22723 struct dwarf2_per_objfile *dwarf2_per_objfile
22724 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 22725 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22726 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22727
cd6c91b4 22728 if (attr->form_is_section_offset ()
3019eac3 22729 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22730 the section. If so, fall through to the complaint in the
22731 other branch. */
2c7d5afc 22732 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 22733 {
0d53c4c4 22734 struct dwarf2_loclist_baton *baton;
4c2df51b 22735
8d749320 22736 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22737
8cf6f0b1 22738 fill_in_loclist_baton (cu, baton, attr);
be391dca 22739
2b24b6e4 22740 if (!cu->base_address.has_value ())
b98664d3 22741 complaint (_("Location list used without "
3e43a32a 22742 "specifying the CU base address."));
4c2df51b 22743
f1e6e072
TT
22744 SYMBOL_ACLASS_INDEX (sym) = (is_block
22745 ? dwarf2_loclist_block_index
22746 : dwarf2_loclist_index);
0d53c4c4
DJ
22747 SYMBOL_LOCATION_BATON (sym) = baton;
22748 }
22749 else
22750 {
22751 struct dwarf2_locexpr_baton *baton;
22752
8d749320 22753 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22754 baton->per_cu = cu->per_cu;
22755 gdb_assert (baton->per_cu);
0d53c4c4 22756
4fc6c0d5 22757 if (attr->form_is_block ())
0d53c4c4
DJ
22758 {
22759 /* Note that we're just copying the block's data pointer
22760 here, not the actual data. We're still pointing into the
6502dd73
DJ
22761 info_buffer for SYM's objfile; right now we never release
22762 that buffer, but when we do clean up properly this may
22763 need to change. */
0d53c4c4
DJ
22764 baton->size = DW_BLOCK (attr)->size;
22765 baton->data = DW_BLOCK (attr)->data;
22766 }
22767 else
22768 {
22769 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 22770 sym->natural_name ());
0d53c4c4 22771 baton->size = 0;
0d53c4c4 22772 }
6e70227d 22773
f1e6e072
TT
22774 SYMBOL_ACLASS_INDEX (sym) = (is_block
22775 ? dwarf2_locexpr_block_index
22776 : dwarf2_locexpr_index);
0d53c4c4
DJ
22777 SYMBOL_LOCATION_BATON (sym) = baton;
22778 }
4c2df51b 22779}
6502dd73 22780
09ba997f 22781/* See read.h. */
ae0d2f24
UW
22782
22783struct objfile *
09ba997f 22784dwarf2_per_cu_data::objfile () const
ae0d2f24 22785{
09ba997f 22786 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
22787
22788 /* Return the master objfile, so that we can report and look up the
22789 correct file containing this variable. */
22790 if (objfile->separate_debug_objfile_backlink)
22791 objfile = objfile->separate_debug_objfile_backlink;
22792
22793 return objfile;
22794}
22795
96408a79
SA
22796/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22797 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22798 CU_HEADERP first. */
22799
22800static const struct comp_unit_head *
22801per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 22802 const struct dwarf2_per_cu_data *per_cu)
96408a79 22803{
d521ce57 22804 const gdb_byte *info_ptr;
96408a79
SA
22805
22806 if (per_cu->cu)
22807 return &per_cu->cu->header;
22808
9c541725 22809 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22810
22811 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22812 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22813 rcuh_kind::COMPILE);
96408a79
SA
22814
22815 return cu_headerp;
22816}
22817
09ba997f 22818/* See read.h. */
ae0d2f24 22819
98714339 22820int
09ba997f 22821dwarf2_per_cu_data::addr_size () const
ae0d2f24 22822{
96408a79
SA
22823 struct comp_unit_head cu_header_local;
22824 const struct comp_unit_head *cu_headerp;
c471e790 22825
09ba997f 22826 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22827
22828 return cu_headerp->addr_size;
ae0d2f24
UW
22829}
22830
09ba997f 22831/* See read.h. */
9eae7c52
TT
22832
22833int
09ba997f 22834dwarf2_per_cu_data::offset_size () const
9eae7c52 22835{
96408a79
SA
22836 struct comp_unit_head cu_header_local;
22837 const struct comp_unit_head *cu_headerp;
9c6c53f7 22838
09ba997f 22839 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22840
22841 return cu_headerp->offset_size;
22842}
22843
09ba997f 22844/* See read.h. */
96408a79
SA
22845
22846int
09ba997f 22847dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
22848{
22849 struct comp_unit_head cu_header_local;
22850 const struct comp_unit_head *cu_headerp;
22851
09ba997f 22852 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22853
22854 if (cu_headerp->version == 2)
22855 return cu_headerp->addr_size;
22856 else
22857 return cu_headerp->offset_size;
181cebd4
JK
22858}
22859
09ba997f 22860/* See read.h. */
9aa1f1e3
TT
22861
22862CORE_ADDR
09ba997f 22863dwarf2_per_cu_data::text_offset () const
9aa1f1e3 22864{
09ba997f
TT
22865 struct objfile *objfile = dwarf2_per_objfile->objfile;
22866
22867 return objfile->text_section_offset ();
9aa1f1e3
TT
22868}
22869
09ba997f
TT
22870/* See read.h. */
22871
22872struct type *
22873dwarf2_per_cu_data::addr_type () const
9a49df9d 22874{
09ba997f 22875 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
22876 struct type *void_type = objfile_type (objfile)->builtin_void;
22877 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 22878 int addr_size = this->addr_size ();
9a49df9d
AB
22879
22880 if (TYPE_LENGTH (addr_type) == addr_size)
22881 return addr_type;
22882
09ba997f 22883 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
22884 return addr_type;
22885}
22886
22b6cd70
TT
22887/* A helper function for dwarf2_find_containing_comp_unit that returns
22888 the index of the result, and that searches a vector. It will
22889 return a result even if the offset in question does not actually
22890 occur in any CU. This is separate so that it can be unit
22891 tested. */
ae038cb0 22892
22b6cd70
TT
22893static int
22894dwarf2_find_containing_comp_unit
22895 (sect_offset sect_off,
22896 unsigned int offset_in_dwz,
22897 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 22898{
ae038cb0
DJ
22899 int low, high;
22900
ae038cb0 22901 low = 0;
22b6cd70 22902 high = all_comp_units.size () - 1;
ae038cb0
DJ
22903 while (high > low)
22904 {
36586728 22905 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22906 int mid = low + (high - low) / 2;
9a619af0 22907
22b6cd70 22908 mid_cu = all_comp_units[mid];
36586728 22909 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 22910 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 22911 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
22912 high = mid;
22913 else
22914 low = mid + 1;
22915 }
22916 gdb_assert (low == high);
22b6cd70
TT
22917 return low;
22918}
22919
22920/* Locate the .debug_info compilation unit from CU's objfile which contains
22921 the DIE at OFFSET. Raises an error on failure. */
22922
22923static struct dwarf2_per_cu_data *
22924dwarf2_find_containing_comp_unit (sect_offset sect_off,
22925 unsigned int offset_in_dwz,
22926 struct dwarf2_per_objfile *dwarf2_per_objfile)
22927{
22928 int low
22929 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22930 dwarf2_per_objfile->all_comp_units);
22931 struct dwarf2_per_cu_data *this_cu
22932 = dwarf2_per_objfile->all_comp_units[low];
22933
45b8ae0c 22934 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 22935 {
36586728 22936 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22937 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
22938 "offset %s [in module %s]"),
22939 sect_offset_str (sect_off),
ed2dc618 22940 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 22941
9c541725
PA
22942 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22943 <= sect_off);
ae038cb0
DJ
22944 return dwarf2_per_objfile->all_comp_units[low-1];
22945 }
22946 else
22947 {
b76e467d 22948 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 22949 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 22950 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 22951 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22952 return this_cu;
22953 }
22954}
22955
22b6cd70
TT
22956#if GDB_SELF_TEST
22957
22958namespace selftests {
22959namespace find_containing_comp_unit {
22960
22961static void
22962run_test ()
22963{
22964 struct dwarf2_per_cu_data one {};
22965 struct dwarf2_per_cu_data two {};
22966 struct dwarf2_per_cu_data three {};
22967 struct dwarf2_per_cu_data four {};
22968
22969 one.length = 5;
22970 two.sect_off = sect_offset (one.length);
22971 two.length = 7;
22972
22973 three.length = 5;
22974 three.is_dwz = 1;
22975 four.sect_off = sect_offset (three.length);
22976 four.length = 7;
22977 four.is_dwz = 1;
22978
22979 std::vector<dwarf2_per_cu_data *> units;
22980 units.push_back (&one);
22981 units.push_back (&two);
22982 units.push_back (&three);
22983 units.push_back (&four);
22984
22985 int result;
22986
22987 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22988 SELF_CHECK (units[result] == &one);
22989 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
22990 SELF_CHECK (units[result] == &one);
22991 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
22992 SELF_CHECK (units[result] == &two);
22993
22994 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
22995 SELF_CHECK (units[result] == &three);
22996 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
22997 SELF_CHECK (units[result] == &three);
22998 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
22999 SELF_CHECK (units[result] == &four);
23000}
23001
23002}
23003}
23004
23005#endif /* GDB_SELF_TEST */
23006
23745b47 23007/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23008
fcd3b13d
SM
23009dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23010 : per_cu (per_cu_),
9068261f
AB
23011 mark (false),
23012 has_loclist (false),
23013 checked_producer (false),
23014 producer_is_gxx_lt_4_6 (false),
23015 producer_is_gcc_lt_4_3 (false),
eb77c9df 23016 producer_is_icc (false),
9068261f 23017 producer_is_icc_lt_14 (false),
c258c396 23018 producer_is_codewarrior (false),
9068261f 23019 processing_has_namespace_info (false)
93311388 23020{
fcd3b13d
SM
23021 per_cu->cu = this;
23022}
23023
23024/* Destroy a dwarf2_cu. */
23025
23026dwarf2_cu::~dwarf2_cu ()
23027{
23028 per_cu->cu = NULL;
9816fde3
JK
23029}
23030
23031/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23032
23033static void
95554aad
TT
23034prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23035 enum language pretend_language)
9816fde3
JK
23036{
23037 struct attribute *attr;
23038
23039 /* Set the language we're debugging. */
23040 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23041 if (attr != nullptr)
9816fde3
JK
23042 set_cu_language (DW_UNSND (attr), cu);
23043 else
9cded63f 23044 {
95554aad 23045 cu->language = pretend_language;
9cded63f
TT
23046 cu->language_defn = language_def (cu->language);
23047 }
dee91e82 23048
7d45c7c3 23049 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23050}
23051
ae038cb0
DJ
23052/* Increase the age counter on each cached compilation unit, and free
23053 any that are too old. */
23054
23055static void
ed2dc618 23056age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23057{
23058 struct dwarf2_per_cu_data *per_cu, **last_chain;
23059
23060 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23061 per_cu = dwarf2_per_objfile->read_in_chain;
23062 while (per_cu != NULL)
23063 {
23064 per_cu->cu->last_used ++;
b4f54984 23065 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23066 dwarf2_mark (per_cu->cu);
23067 per_cu = per_cu->cu->read_in_chain;
23068 }
23069
23070 per_cu = dwarf2_per_objfile->read_in_chain;
23071 last_chain = &dwarf2_per_objfile->read_in_chain;
23072 while (per_cu != NULL)
23073 {
23074 struct dwarf2_per_cu_data *next_cu;
23075
23076 next_cu = per_cu->cu->read_in_chain;
23077
23078 if (!per_cu->cu->mark)
23079 {
fcd3b13d 23080 delete per_cu->cu;
ae038cb0
DJ
23081 *last_chain = next_cu;
23082 }
23083 else
23084 last_chain = &per_cu->cu->read_in_chain;
23085
23086 per_cu = next_cu;
23087 }
23088}
23089
23090/* Remove a single compilation unit from the cache. */
23091
23092static void
dee91e82 23093free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23094{
23095 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23096 struct dwarf2_per_objfile *dwarf2_per_objfile
23097 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
23098
23099 per_cu = dwarf2_per_objfile->read_in_chain;
23100 last_chain = &dwarf2_per_objfile->read_in_chain;
23101 while (per_cu != NULL)
23102 {
23103 struct dwarf2_per_cu_data *next_cu;
23104
23105 next_cu = per_cu->cu->read_in_chain;
23106
dee91e82 23107 if (per_cu == target_per_cu)
ae038cb0 23108 {
fcd3b13d 23109 delete per_cu->cu;
dee91e82 23110 per_cu->cu = NULL;
ae038cb0
DJ
23111 *last_chain = next_cu;
23112 break;
23113 }
23114 else
23115 last_chain = &per_cu->cu->read_in_chain;
23116
23117 per_cu = next_cu;
23118 }
23119}
23120
dee91e82
DE
23121/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23122 We store these in a hash table separate from the DIEs, and preserve them
23123 when the DIEs are flushed out of cache.
23124
23125 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23126 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23127 or the type may come from a DWO file. Furthermore, while it's more logical
23128 to use per_cu->section+offset, with Fission the section with the data is in
23129 the DWO file but we don't know that section at the point we need it.
23130 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23131 because we can enter the lookup routine, get_die_type_at_offset, from
23132 outside this file, and thus won't necessarily have PER_CU->cu.
23133 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23134
dee91e82 23135struct dwarf2_per_cu_offset_and_type
1c379e20 23136{
dee91e82 23137 const struct dwarf2_per_cu_data *per_cu;
9c541725 23138 sect_offset sect_off;
1c379e20
DJ
23139 struct type *type;
23140};
23141
dee91e82 23142/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23143
23144static hashval_t
dee91e82 23145per_cu_offset_and_type_hash (const void *item)
1c379e20 23146{
9a3c8263
SM
23147 const struct dwarf2_per_cu_offset_and_type *ofs
23148 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23149
9c541725 23150 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23151}
23152
dee91e82 23153/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23154
23155static int
dee91e82 23156per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23157{
9a3c8263
SM
23158 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23159 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23160 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23161 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23162
dee91e82 23163 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23164 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23165}
23166
23167/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23168 table if necessary. For convenience, return TYPE.
23169
23170 The DIEs reading must have careful ordering to:
85102364 23171 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23172 reading current DIE.
23173 * Not trying to dereference contents of still incompletely read in types
23174 while reading in other DIEs.
23175 * Enable referencing still incompletely read in types just by a pointer to
23176 the type without accessing its fields.
23177
23178 Therefore caller should follow these rules:
23179 * Try to fetch any prerequisite types we may need to build this DIE type
23180 before building the type and calling set_die_type.
e71ec853 23181 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23182 possible before fetching more types to complete the current type.
23183 * Make the type as complete as possible before fetching more types. */
1c379e20 23184
f792889a 23185static struct type *
1c379e20
DJ
23186set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23187{
518817b3
SM
23188 struct dwarf2_per_objfile *dwarf2_per_objfile
23189 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23190 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23191 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23192 struct attribute *attr;
23193 struct dynamic_prop prop;
1c379e20 23194
b4ba55a1
JB
23195 /* For Ada types, make sure that the gnat-specific data is always
23196 initialized (if not already set). There are a few types where
23197 we should not be doing so, because the type-specific area is
23198 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23199 where the type-specific area is used to store the floatformat).
23200 But this is not a problem, because the gnat-specific information
23201 is actually not needed for these types. */
23202 if (need_gnat_info (cu)
23203 && TYPE_CODE (type) != TYPE_CODE_FUNC
23204 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23205 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23206 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23207 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23208 && !HAVE_GNAT_AUX_INFO (type))
23209 INIT_GNAT_SPECIFIC (type);
23210
3f2f83dd
KB
23211 /* Read DW_AT_allocated and set in type. */
23212 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23213 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23214 {
09ba997f 23215 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23216 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23217 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
23218 }
23219 else if (attr != NULL)
23220 {
b98664d3 23221 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23222 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23223 sect_offset_str (die->sect_off));
3f2f83dd
KB
23224 }
23225
23226 /* Read DW_AT_associated and set in type. */
23227 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23228 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23229 {
09ba997f 23230 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23231 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23232 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
23233 }
23234 else if (attr != NULL)
23235 {
b98664d3 23236 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23237 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23238 sect_offset_str (die->sect_off));
3f2f83dd
KB
23239 }
23240
3cdcd0ce
JB
23241 /* Read DW_AT_data_location and set in type. */
23242 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23243 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23244 cu->per_cu->addr_type ()))
50a82047 23245 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 23246
dee91e82 23247 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
23248 dwarf2_per_objfile->die_type_hash
23249 = htab_up (htab_create_alloc (127,
23250 per_cu_offset_and_type_hash,
23251 per_cu_offset_and_type_eq,
23252 NULL, xcalloc, xfree));
1c379e20 23253
dee91e82 23254 ofs.per_cu = cu->per_cu;
9c541725 23255 ofs.sect_off = die->sect_off;
1c379e20 23256 ofs.type = type;
dee91e82 23257 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 23258 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23259 if (*slot)
b98664d3 23260 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23261 sect_offset_str (die->sect_off));
8d749320
SM
23262 *slot = XOBNEW (&objfile->objfile_obstack,
23263 struct dwarf2_per_cu_offset_and_type);
1c379e20 23264 **slot = ofs;
f792889a 23265 return type;
1c379e20
DJ
23266}
23267
9c541725 23268/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23269 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23270
23271static struct type *
9c541725 23272get_die_type_at_offset (sect_offset sect_off,
673bfd45 23273 struct dwarf2_per_cu_data *per_cu)
1c379e20 23274{
dee91e82 23275 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23276 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23277
dee91e82 23278 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23279 return NULL;
1c379e20 23280
dee91e82 23281 ofs.per_cu = per_cu;
9c541725 23282 ofs.sect_off = sect_off;
9a3c8263 23283 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 23284 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23285 if (slot)
23286 return slot->type;
23287 else
23288 return NULL;
23289}
23290
02142a6c 23291/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23292 or return NULL if DIE does not have a saved type. */
23293
23294static struct type *
23295get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23296{
9c541725 23297 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23298}
23299
10b3939b
DJ
23300/* Add a dependence relationship from CU to REF_PER_CU. */
23301
23302static void
23303dwarf2_add_dependence (struct dwarf2_cu *cu,
23304 struct dwarf2_per_cu_data *ref_per_cu)
23305{
23306 void **slot;
23307
23308 if (cu->dependencies == NULL)
23309 cu->dependencies
23310 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23311 NULL, &cu->comp_unit_obstack,
23312 hashtab_obstack_allocate,
23313 dummy_obstack_deallocate);
23314
23315 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23316 if (*slot == NULL)
23317 *slot = ref_per_cu;
23318}
1c379e20 23319
f504f079
DE
23320/* Subroutine of dwarf2_mark to pass to htab_traverse.
23321 Set the mark field in every compilation unit in the
ae038cb0
DJ
23322 cache that we must keep because we are keeping CU. */
23323
10b3939b
DJ
23324static int
23325dwarf2_mark_helper (void **slot, void *data)
23326{
23327 struct dwarf2_per_cu_data *per_cu;
23328
23329 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23330
23331 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23332 reading of the chain. As such dependencies remain valid it is not much
23333 useful to track and undo them during QUIT cleanups. */
23334 if (per_cu->cu == NULL)
23335 return 1;
23336
10b3939b
DJ
23337 if (per_cu->cu->mark)
23338 return 1;
9068261f 23339 per_cu->cu->mark = true;
10b3939b
DJ
23340
23341 if (per_cu->cu->dependencies != NULL)
23342 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23343
23344 return 1;
23345}
23346
f504f079
DE
23347/* Set the mark field in CU and in every other compilation unit in the
23348 cache that we must keep because we are keeping CU. */
23349
ae038cb0
DJ
23350static void
23351dwarf2_mark (struct dwarf2_cu *cu)
23352{
23353 if (cu->mark)
23354 return;
9068261f 23355 cu->mark = true;
10b3939b
DJ
23356 if (cu->dependencies != NULL)
23357 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23358}
23359
23360static void
23361dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23362{
23363 while (per_cu)
23364 {
9068261f 23365 per_cu->cu->mark = false;
ae038cb0
DJ
23366 per_cu = per_cu->cu->read_in_chain;
23367 }
72bf9492
DJ
23368}
23369
72bf9492
DJ
23370/* Trivial hash function for partial_die_info: the hash value of a DIE
23371 is its offset in .debug_info for this objfile. */
23372
23373static hashval_t
23374partial_die_hash (const void *item)
23375{
9a3c8263
SM
23376 const struct partial_die_info *part_die
23377 = (const struct partial_die_info *) item;
9a619af0 23378
9c541725 23379 return to_underlying (part_die->sect_off);
72bf9492
DJ
23380}
23381
23382/* Trivial comparison function for partial_die_info structures: two DIEs
23383 are equal if they have the same offset. */
23384
23385static int
23386partial_die_eq (const void *item_lhs, const void *item_rhs)
23387{
9a3c8263
SM
23388 const struct partial_die_info *part_die_lhs
23389 = (const struct partial_die_info *) item_lhs;
23390 const struct partial_die_info *part_die_rhs
23391 = (const struct partial_die_info *) item_rhs;
9a619af0 23392
9c541725 23393 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23394}
23395
3c3bb058
AB
23396struct cmd_list_element *set_dwarf_cmdlist;
23397struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23398
23399static void
981a3fb3 23400set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 23401{
b4f54984 23402 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23403 gdb_stdout);
ae038cb0
DJ
23404}
23405
23406static void
981a3fb3 23407show_dwarf_cmd (const char *args, int from_tty)
6e70227d 23408{
b4f54984 23409 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23410}
23411
9291a0cd 23412static void
cd4fb1b2
SM
23413show_check_physname (struct ui_file *file, int from_tty,
23414 struct cmd_list_element *c, const char *value)
9291a0cd 23415{
cd4fb1b2
SM
23416 fprintf_filtered (file,
23417 _("Whether to check \"physname\" is %s.\n"),
23418 value);
9291a0cd
TT
23419}
23420
6c265988 23421void _initialize_dwarf2_read ();
cd4fb1b2 23422void
6c265988 23423_initialize_dwarf2_read ()
9291a0cd 23424{
cd4fb1b2
SM
23425 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23426Set DWARF specific variables.\n\
590042fc 23427Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
23428 &set_dwarf_cmdlist, "maintenance set dwarf ",
23429 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23430
cd4fb1b2 23431 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
23432Show DWARF specific variables.\n\
23433Show DWARF variables such as the cache size."),
cd4fb1b2
SM
23434 &show_dwarf_cmdlist, "maintenance show dwarf ",
23435 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23436
cd4fb1b2
SM
23437 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23438 &dwarf_max_cache_age, _("\
23439Set the upper bound on the age of cached DWARF compilation units."), _("\
23440Show the upper bound on the age of cached DWARF compilation units."), _("\
23441A higher limit means that cached compilation units will be stored\n\
23442in memory longer, and more total memory will be used. Zero disables\n\
23443caching, which can slow down startup."),
23444 NULL,
23445 show_dwarf_max_cache_age,
23446 &set_dwarf_cmdlist,
23447 &show_dwarf_cmdlist);
156942c7 23448
cd4fb1b2
SM
23449 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23450Set debugging of the DWARF reader."), _("\
23451Show debugging of the DWARF reader."), _("\
23452When enabled (non-zero), debugging messages are printed during DWARF\n\
23453reading and symtab expansion. A value of 1 (one) provides basic\n\
23454information. A value greater than 1 provides more verbose information."),
23455 NULL,
23456 NULL,
23457 &setdebuglist, &showdebuglist);
9291a0cd 23458
cd4fb1b2
SM
23459 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23460Set debugging of the DWARF DIE reader."), _("\
23461Show debugging of the DWARF DIE reader."), _("\
23462When enabled (non-zero), DIEs are dumped after they are read in.\n\
23463The value is the maximum depth to print."),
23464 NULL,
23465 NULL,
23466 &setdebuglist, &showdebuglist);
9291a0cd 23467
cd4fb1b2
SM
23468 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23469Set debugging of the dwarf line reader."), _("\
23470Show debugging of the dwarf line reader."), _("\
23471When enabled (non-zero), line number entries are dumped as they are read in.\n\
23472A value of 1 (one) provides basic information.\n\
23473A value greater than 1 provides more verbose information."),
23474 NULL,
23475 NULL,
23476 &setdebuglist, &showdebuglist);
437afbb8 23477
cd4fb1b2
SM
23478 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23479Set cross-checking of \"physname\" code against demangler."), _("\
23480Show cross-checking of \"physname\" code against demangler."), _("\
23481When enabled, GDB's internal \"physname\" code is checked against\n\
23482the demangler."),
23483 NULL, show_check_physname,
23484 &setdebuglist, &showdebuglist);
900e11f9 23485
e615022a
DE
23486 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23487 no_class, &use_deprecated_index_sections, _("\
23488Set whether to use deprecated gdb_index sections."), _("\
23489Show whether to use deprecated gdb_index sections."), _("\
23490When enabled, deprecated .gdb_index sections are used anyway.\n\
23491Normally they are ignored either because of a missing feature or\n\
23492performance issue.\n\
23493Warning: This option must be enabled before gdb reads the file."),
23494 NULL,
23495 NULL,
23496 &setlist, &showlist);
23497
f1e6e072
TT
23498 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23499 &dwarf2_locexpr_funcs);
23500 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23501 &dwarf2_loclist_funcs);
23502
23503 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23504 &dwarf2_block_frame_base_locexpr_funcs);
23505 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23506 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23507
23508#if GDB_SELF_TEST
23509 selftests::register_test ("dw2_expand_symtabs_matching",
23510 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23511 selftests::register_test ("dwarf2_find_containing_comp_unit",
23512 selftests::find_containing_comp_unit::run_test);
c62446b1 23513#endif
6502dd73 23514}
This page took 7.146182 seconds and 4 git commands to generate.