Convert read_indirect_line_string to a method
[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"
4de283e4
TT
42#include "bfd.h"
43#include "elf-bfd.h"
44#include "symtab.h"
45#include "gdbtypes.h"
46#include "objfiles.h"
d55e5aa6 47#include "dwarf2.h"
4de283e4
TT
48#include "buildsym.h"
49#include "demangle.h"
50#include "gdb-demangle.h"
4de283e4 51#include "filenames.h" /* for DOSish file names */
4de283e4
TT
52#include "language.h"
53#include "complaints.h"
82ca8957
TT
54#include "dwarf2/expr.h"
55#include "dwarf2/loc.h"
4de283e4
TT
56#include "cp-support.h"
57#include "hashtab.h"
58#include "command.h"
d55e5aa6 59#include "gdbcmd.h"
4de283e4
TT
60#include "block.h"
61#include "addrmap.h"
62#include "typeprint.h"
63#include "psympriv.h"
4de283e4 64#include "c-lang.h"
d55e5aa6 65#include "go-lang.h"
4de283e4
TT
66#include "valprint.h"
67#include "gdbcore.h" /* for gnutarget */
68#include "gdb/gdb-index.h"
4de283e4
TT
69#include "gdb_bfd.h"
70#include "f-lang.h"
71#include "source.h"
4de283e4 72#include "build-id.h"
d55e5aa6 73#include "namespace.h"
268a13a5
TT
74#include "gdbsupport/function-view.h"
75#include "gdbsupport/gdb_optional.h"
76#include "gdbsupport/underlying.h"
268a13a5 77#include "gdbsupport/hash_enum.h"
4de283e4 78#include "filename-seen-cache.h"
b32b108a 79#include "producer.h"
4de283e4 80#include <fcntl.h>
4de283e4 81#include <algorithm>
4de283e4 82#include <unordered_map>
268a13a5 83#include "gdbsupport/selftest.h"
c9317f21 84#include "rust-lang.h"
268a13a5 85#include "gdbsupport/pathstuff.h"
edd45eb0 86#include "count-one-bits.h"
0d79cdc4 87#include "debuginfod-support.h"
437afbb8 88
73be47f5
DE
89/* When == 1, print basic high level tracing messages.
90 When > 1, be more verbose.
b4f54984
DE
91 This is in contrast to the low level DIE reading of dwarf_die_debug. */
92static unsigned int dwarf_read_debug = 0;
45cfd468 93
d97bc12b 94/* When non-zero, dump DIEs after they are read in. */
b4f54984 95static unsigned int dwarf_die_debug = 0;
d97bc12b 96
27e0867f 97/* When non-zero, dump line number entries as they are read in. */
8fdd972c 98unsigned int dwarf_line_debug = 0;
27e0867f 99
491144b5
CB
100/* When true, cross-check physname against demangler. */
101static bool check_physname = false;
900e11f9 102
491144b5
CB
103/* When true, do not reject deprecated .gdb_index sections. */
104static bool use_deprecated_index_sections = false;
481860b3 105
5bfd760d 106static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 107
f1e6e072
TT
108/* The "aclass" indices for various kinds of computed DWARF symbols. */
109
110static int dwarf2_locexpr_index;
111static int dwarf2_loclist_index;
112static int dwarf2_locexpr_block_index;
113static int dwarf2_loclist_block_index;
114
3f563c84
PA
115/* An index into a (C++) symbol name component in a symbol name as
116 recorded in the mapped_index's symbol table. For each C++ symbol
117 in the symbol table, we record one entry for the start of each
118 component in the symbol in a table of name components, and then
119 sort the table, in order to be able to binary search symbol names,
120 ignoring leading namespaces, both completion and regular look up.
121 For example, for symbol "A::B::C", we'll have an entry that points
122 to "A::B::C", another that points to "B::C", and another for "C".
123 Note that function symbols in GDB index have no parameter
124 information, just the function/method names. You can convert a
125 name_component to a "const char *" using the
126 'mapped_index::symbol_name_at(offset_type)' method. */
127
128struct name_component
129{
130 /* Offset in the symbol name where the component starts. Stored as
131 a (32-bit) offset instead of a pointer to save memory and improve
132 locality on 64-bit architectures. */
133 offset_type name_offset;
134
135 /* The symbol's index in the symbol and constant pool tables of a
136 mapped_index. */
137 offset_type idx;
138};
139
44ed8f3e
PA
140/* Base class containing bits shared by both .gdb_index and
141 .debug_name indexes. */
142
143struct mapped_index_base
144{
22ca247e
TT
145 mapped_index_base () = default;
146 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
147
44ed8f3e
PA
148 /* The name_component table (a sorted vector). See name_component's
149 description above. */
150 std::vector<name_component> name_components;
151
152 /* How NAME_COMPONENTS is sorted. */
153 enum case_sensitivity name_components_casing;
154
155 /* Return the number of names in the symbol table. */
156 virtual size_t symbol_name_count () const = 0;
157
158 /* Get the name of the symbol at IDX in the symbol table. */
159 virtual const char *symbol_name_at (offset_type idx) const = 0;
160
161 /* Return whether the name at IDX in the symbol table should be
162 ignored. */
163 virtual bool symbol_name_slot_invalid (offset_type idx) const
164 {
165 return false;
166 }
167
168 /* Build the symbol name component sorted vector, if we haven't
169 yet. */
170 void build_name_components ();
171
172 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
173 possible matches for LN_NO_PARAMS in the name component
174 vector. */
175 std::pair<std::vector<name_component>::const_iterator,
176 std::vector<name_component>::const_iterator>
3b00ef10
TT
177 find_name_components_bounds (const lookup_name_info &ln_no_params,
178 enum language lang) const;
44ed8f3e
PA
179
180 /* Prevent deleting/destroying via a base class pointer. */
181protected:
182 ~mapped_index_base() = default;
183};
184
9291a0cd
TT
185/* A description of the mapped index. The file format is described in
186 a comment by the code that writes the index. */
fc898b42 187struct mapped_index final : public mapped_index_base
9291a0cd 188{
f00a2de2
PA
189 /* A slot/bucket in the symbol table hash. */
190 struct symbol_table_slot
191 {
192 const offset_type name;
193 const offset_type vec;
194 };
195
559a7a62 196 /* Index data format version. */
3063847f 197 int version = 0;
559a7a62 198
f00a2de2
PA
199 /* The address table data. */
200 gdb::array_view<const gdb_byte> address_table;
b11b1f88 201
3876f04e 202 /* The symbol table, implemented as a hash table. */
f00a2de2 203 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 204
9291a0cd 205 /* A pointer to the constant pool. */
3063847f 206 const char *constant_pool = nullptr;
3f563c84 207
44ed8f3e
PA
208 bool symbol_name_slot_invalid (offset_type idx) const override
209 {
210 const auto &bucket = this->symbol_table[idx];
9ab08412 211 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 212 }
5c58de74 213
3f563c84
PA
214 /* Convenience method to get at the name of the symbol at IDX in the
215 symbol table. */
44ed8f3e 216 const char *symbol_name_at (offset_type idx) const override
f00a2de2 217 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 218
44ed8f3e
PA
219 size_t symbol_name_count () const override
220 { return this->symbol_table.size (); }
9291a0cd
TT
221};
222
927aa2e7
JK
223/* A description of the mapped .debug_names.
224 Uninitialized map has CU_COUNT 0. */
fc898b42 225struct mapped_debug_names final : public mapped_index_base
927aa2e7 226{
ed2dc618
SM
227 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
228 : dwarf2_per_objfile (dwarf2_per_objfile_)
229 {}
230
231 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
232 bfd_endian dwarf5_byte_order;
233 bool dwarf5_is_dwarf64;
234 bool augmentation_is_gdb;
235 uint8_t offset_size;
236 uint32_t cu_count = 0;
237 uint32_t tu_count, bucket_count, name_count;
238 const gdb_byte *cu_table_reordered, *tu_table_reordered;
239 const uint32_t *bucket_table_reordered, *hash_table_reordered;
240 const gdb_byte *name_table_string_offs_reordered;
241 const gdb_byte *name_table_entry_offs_reordered;
242 const gdb_byte *entry_pool;
243
244 struct index_val
245 {
246 ULONGEST dwarf_tag;
247 struct attr
248 {
249 /* Attribute name DW_IDX_*. */
250 ULONGEST dw_idx;
251
252 /* Attribute form DW_FORM_*. */
253 ULONGEST form;
254
255 /* Value if FORM is DW_FORM_implicit_const. */
256 LONGEST implicit_const;
257 };
258 std::vector<attr> attr_vec;
259 };
260
261 std::unordered_map<ULONGEST, index_val> abbrev_map;
262
263 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
264
265 /* Implementation of the mapped_index_base virtual interface, for
266 the name_components cache. */
267
268 const char *symbol_name_at (offset_type idx) const override
269 { return namei_to_name (idx); }
270
271 size_t symbol_name_count () const override
272 { return this->name_count; }
927aa2e7
JK
273};
274
cd4fb1b2 275/* See dwarf2read.h. */
ed2dc618 276
cd4fb1b2 277dwarf2_per_objfile *
ed2dc618
SM
278get_dwarf2_per_objfile (struct objfile *objfile)
279{
5bfd760d 280 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 281}
c906108c 282
251d32d9 283/* Default names of the debugging sections. */
c906108c 284
233a11ab
CS
285/* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
9cdd5dbd
DE
288static const struct dwarf2_debug_sections dwarf2_elf_names =
289{
251d32d9
TG
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
43988095 294 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 295 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 296 { ".debug_macro", ".zdebug_macro" },
251d32d9 297 { ".debug_str", ".zdebug_str" },
18a8505e 298 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 299 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 300 { ".debug_ranges", ".zdebug_ranges" },
43988095 301 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 302 { ".debug_types", ".zdebug_types" },
3019eac3 303 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
24d3216f 306 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
307 { ".debug_names", ".zdebug_names" },
308 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 309 23
251d32d9 310};
c906108c 311
80626a55 312/* List of DWO/DWP sections. */
3019eac3 313
80626a55 314static const struct dwop_section_names
3019eac3
DE
315{
316 struct dwarf2_section_names abbrev_dwo;
317 struct dwarf2_section_names info_dwo;
318 struct dwarf2_section_names line_dwo;
319 struct dwarf2_section_names loc_dwo;
43988095 320 struct dwarf2_section_names loclists_dwo;
09262596
DE
321 struct dwarf2_section_names macinfo_dwo;
322 struct dwarf2_section_names macro_dwo;
3019eac3
DE
323 struct dwarf2_section_names str_dwo;
324 struct dwarf2_section_names str_offsets_dwo;
325 struct dwarf2_section_names types_dwo;
80626a55
DE
326 struct dwarf2_section_names cu_index;
327 struct dwarf2_section_names tu_index;
3019eac3 328}
80626a55 329dwop_section_names =
3019eac3
DE
330{
331 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
332 { ".debug_info.dwo", ".zdebug_info.dwo" },
333 { ".debug_line.dwo", ".zdebug_line.dwo" },
334 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 335 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
336 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
337 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
338 { ".debug_str.dwo", ".zdebug_str.dwo" },
339 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
340 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
341 { ".debug_cu_index", ".zdebug_cu_index" },
342 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
343};
344
c906108c
SS
345/* local data types */
346
3da10d80
KS
347/* Type used for delaying computation of method physnames.
348 See comments for compute_delayed_physnames. */
349struct delayed_method_info
350{
351 /* The type to which the method is attached, i.e., its parent class. */
352 struct type *type;
353
354 /* The index of the method in the type's function fieldlists. */
355 int fnfield_index;
356
357 /* The index of the method in the fieldlist. */
358 int index;
359
360 /* The name of the DIE. */
361 const char *name;
362
363 /* The DIE associated with this method. */
364 struct die_info *die;
365};
366
e7c27a73
DJ
367/* Internal state when decoding a particular compilation unit. */
368struct dwarf2_cu
369{
fcd3b13d
SM
370 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
371 ~dwarf2_cu ();
372
373 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
374
c24bdb02
KS
375 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
376 Create the set of symtabs used by this TU, or if this TU is sharing
377 symtabs with another TU and the symtabs have already been created
378 then restore those symtabs in the line header.
379 We don't need the pc/line-number mapping for type units. */
380 void setup_type_unit_groups (struct die_info *die);
381
382 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
383 buildsym_compunit constructor. */
384 struct compunit_symtab *start_symtab (const char *name,
385 const char *comp_dir,
386 CORE_ADDR low_pc);
387
388 /* Reset the builder. */
389 void reset_builder () { m_builder.reset (); }
390
d00adf39 391 /* The header of the compilation unit. */
fcd3b13d 392 struct comp_unit_head header {};
e142c38c 393
d00adf39 394 /* Base address of this compilation unit. */
fcd3b13d 395 CORE_ADDR base_address = 0;
d00adf39
DE
396
397 /* Non-zero if base_address has been set. */
fcd3b13d 398 int base_known = 0;
d00adf39 399
e142c38c 400 /* The language we are debugging. */
fcd3b13d
SM
401 enum language language = language_unknown;
402 const struct language_defn *language_defn = nullptr;
e142c38c 403
fcd3b13d 404 const char *producer = nullptr;
b0f35d58 405
c24bdb02 406private:
804d2729
TT
407 /* The symtab builder for this CU. This is only non-NULL when full
408 symbols are being read. */
c24bdb02 409 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 410
c24bdb02 411public:
e142c38c
DJ
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
fcd3b13d 421 struct pending **list_in_scope = nullptr;
e142c38c 422
b64f50a1
JK
423 /* Hash table holding all the loaded partial DIEs
424 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 425 htab_t partial_dies = nullptr;
72bf9492
DJ
426
427 /* Storage for things with the same lifetime as this read-in compilation
428 unit, including partial DIEs. */
fcd3b13d 429 auto_obstack comp_unit_obstack;
72bf9492 430
ae038cb0
DJ
431 /* When multiple dwarf2_cu structures are living in memory, this field
432 chains them all together, so that they can be released efficiently.
433 We will probably also want a generation counter so that most-recently-used
434 compilation units are cached... */
fcd3b13d 435 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 436
69d751e3 437 /* Backlink to our per_cu entry. */
ae038cb0
DJ
438 struct dwarf2_per_cu_data *per_cu;
439
440 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 441 int last_used = 0;
ae038cb0 442
b64f50a1
JK
443 /* A hash table of DIE cu_offset for following references with
444 die_info->offset.sect_off as hash. */
fcd3b13d 445 htab_t die_hash = nullptr;
10b3939b
DJ
446
447 /* Full DIEs if read in. */
fcd3b13d 448 struct die_info *dies = nullptr;
10b3939b
DJ
449
450 /* A set of pointers to dwarf2_per_cu_data objects for compilation
451 units referenced by this one. Only set during full symbol processing;
452 partial symbol tables do not have dependencies. */
fcd3b13d 453 htab_t dependencies = nullptr;
10b3939b 454
cb1df416 455 /* Header data from the line table, during full symbol processing. */
fcd3b13d 456 struct line_header *line_header = nullptr;
4c8aa72d
PA
457 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
458 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
459 this is the DW_TAG_compile_unit die for this CU. We'll hold on
460 to the line header as long as this DIE is being processed. See
461 process_die_scope. */
fcd3b13d 462 die_info *line_header_die_owner = nullptr;
cb1df416 463
3da10d80
KS
464 /* A list of methods which need to have physnames computed
465 after all type information has been read. */
c89b44cd 466 std::vector<delayed_method_info> method_list;
3da10d80 467
96408a79 468 /* To be copied to symtab->call_site_htab. */
fcd3b13d 469 htab_t call_site_htab = nullptr;
96408a79 470
034e5797
DE
471 /* Non-NULL if this CU came from a DWO file.
472 There is an invariant here that is important to remember:
473 Except for attributes copied from the top level DIE in the "main"
474 (or "stub") file in preparation for reading the DWO file
18a8505e 475 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
476 Either there isn't a DWO file (in which case this is NULL and the point
477 is moot), or there is and either we're not going to read it (in which
478 case this is NULL) or there is and we are reading it (in which case this
479 is non-NULL). */
fcd3b13d 480 struct dwo_unit *dwo_unit = nullptr;
3019eac3 481
18a8505e 482 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 483 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 484 gdb::optional<ULONGEST> addr_base;
3019eac3 485
18a8505e 486 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 487 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 488 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
489 be used without needing to know whether DWO files are in use or not.
490 N.B. This does not apply to DW_AT_ranges appearing in
491 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
492 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 493 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 494 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 495 ULONGEST ranges_base = 0;
2e3cf129 496
c9317f21
TT
497 /* When reading debug info generated by older versions of rustc, we
498 have to rewrite some union types to be struct types with a
499 variant part. This rewriting must be done after the CU is fully
500 read in, because otherwise at the point of rewriting some struct
501 type might not have been fully processed. So, we keep a list of
502 all such types here and process them after expansion. */
503 std::vector<struct type *> rust_unions;
504
18a8505e
AT
505 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
506 files, the value is implicitly zero. For DWARF 5 version DWO files, the
507 value is often implicit and is the size of the header of
508 .debug_str_offsets section (8 or 4, depending on the address size). */
509 gdb::optional<ULONGEST> str_offsets_base;
510
ae038cb0 511 /* Mark used when releasing cached dies. */
9068261f 512 bool mark : 1;
ae038cb0 513
8be455d7
JK
514 /* This CU references .debug_loc. See the symtab->locations_valid field.
515 This test is imperfect as there may exist optimized debug code not using
516 any location list and still facing inlining issues if handled as
517 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 518 bool has_loclist : 1;
ba919b58 519
9068261f 520 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
521 if all the producer_is_* fields are valid. This information is cached
522 because profiling CU expansion showed excessive time spent in
523 producer_is_gxx_lt_4_6. */
9068261f
AB
524 bool checked_producer : 1;
525 bool producer_is_gxx_lt_4_6 : 1;
526 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 527 bool producer_is_icc : 1;
9068261f 528 bool producer_is_icc_lt_14 : 1;
c258c396 529 bool producer_is_codewarrior : 1;
4d4ec4e5 530
9068261f 531 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
532 debugging info for C++ namespaces. GCC 3.3.x did not produce
533 this information, but later versions do. */
534
9068261f 535 bool processing_has_namespace_info : 1;
d590ff25
YQ
536
537 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
538
539 /* If this CU was inherited by another CU (via specification,
540 abstract_origin, etc), this is the ancestor CU. */
541 dwarf2_cu *ancestor;
542
543 /* Get the buildsym_compunit for this CU. */
544 buildsym_compunit *get_builder ()
545 {
546 /* If this CU has a builder associated with it, use that. */
547 if (m_builder != nullptr)
548 return m_builder.get ();
549
550 /* Otherwise, search ancestors for a valid builder. */
551 if (ancestor != nullptr)
552 return ancestor->get_builder ();
553
554 return nullptr;
555 }
e7c27a73
DJ
556};
557
094b34ac
DE
558/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
559 This includes type_unit_group and quick_file_names. */
560
561struct stmt_list_hash
562{
563 /* The DWO unit this table is from or NULL if there is none. */
564 struct dwo_unit *dwo_unit;
565
566 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 567 sect_offset line_sect_off;
094b34ac
DE
568};
569
f4dc4d17
DE
570/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
571 an object of this type. */
572
573struct type_unit_group
574{
0186c6a7 575 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
576 To simplify things we create an artificial CU that "includes" all the
577 type units using this stmt_list so that the rest of the code still has
197400e8 578 a "per_cu" handle on the symtab. */
094b34ac
DE
579 struct dwarf2_per_cu_data per_cu;
580
0186c6a7
DE
581 /* The TUs that share this DW_AT_stmt_list entry.
582 This is added to while parsing type units to build partial symtabs,
583 and is deleted afterwards and not used again. */
a8b3b8e9 584 std::vector<signatured_type *> *tus;
f4dc4d17 585
43f3e411 586 /* The compunit symtab.
094b34ac 587 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
588 so we create an essentially anonymous symtab as the compunit symtab. */
589 struct compunit_symtab *compunit_symtab;
f4dc4d17 590
094b34ac
DE
591 /* The data used to construct the hash key. */
592 struct stmt_list_hash hash;
f4dc4d17 593
f4dc4d17
DE
594 /* The symbol tables for this TU (obtained from the files listed in
595 DW_AT_stmt_list).
596 WARNING: The order of entries here must match the order of entries
597 in the line header. After the first TU using this type_unit_group, the
598 line header for the subsequent TUs is recreated from this. This is done
599 because we need to use the same symtabs for each TU using the same
600 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
601 there's no guarantee the line header doesn't have duplicate entries. */
602 struct symtab **symtabs;
603};
604
73869dc2 605/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
606
607struct dwo_sections
608{
609 struct dwarf2_section_info abbrev;
3019eac3
DE
610 struct dwarf2_section_info line;
611 struct dwarf2_section_info loc;
43988095 612 struct dwarf2_section_info loclists;
09262596
DE
613 struct dwarf2_section_info macinfo;
614 struct dwarf2_section_info macro;
3019eac3
DE
615 struct dwarf2_section_info str;
616 struct dwarf2_section_info str_offsets;
80626a55
DE
617 /* In the case of a virtual DWO file, these two are unused. */
618 struct dwarf2_section_info info;
fd5866f6 619 std::vector<dwarf2_section_info> types;
3019eac3
DE
620};
621
c88ee1f0 622/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
623
624struct dwo_unit
625{
626 /* Backlink to the containing struct dwo_file. */
627 struct dwo_file *dwo_file;
628
629 /* The "id" that distinguishes this CU/TU.
630 .debug_info calls this "dwo_id", .debug_types calls this "signature".
631 Since signatures came first, we stick with it for consistency. */
632 ULONGEST signature;
633
634 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 635 struct dwarf2_section_info *section;
3019eac3 636
9c541725
PA
637 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
638 sect_offset sect_off;
3019eac3
DE
639 unsigned int length;
640
641 /* For types, offset in the type's DIE of the type defined by this TU. */
642 cu_offset type_offset_in_tu;
643};
644
73869dc2
DE
645/* include/dwarf2.h defines the DWP section codes.
646 It defines a max value but it doesn't define a min value, which we
647 use for error checking, so provide one. */
648
649enum dwp_v2_section_ids
650{
651 DW_SECT_MIN = 1
652};
653
80626a55 654/* Data for one DWO file.
57d63ce2
DE
655
656 This includes virtual DWO files (a virtual DWO file is a DWO file as it
657 appears in a DWP file). DWP files don't really have DWO files per se -
658 comdat folding of types "loses" the DWO file they came from, and from
659 a high level view DWP files appear to contain a mass of random types.
660 However, to maintain consistency with the non-DWP case we pretend DWP
661 files contain virtual DWO files, and we assign each TU with one virtual
662 DWO file (generally based on the line and abbrev section offsets -
663 a heuristic that seems to work in practice). */
3019eac3
DE
664
665struct dwo_file
666{
51ac9db5
SM
667 dwo_file () = default;
668 DISABLE_COPY_AND_ASSIGN (dwo_file);
669
18a8505e 670 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
671 For virtual DWO files the name is constructed from the section offsets
672 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
673 from related CU+TUs. */
51ac9db5 674 const char *dwo_name = nullptr;
0ac5b59e
DE
675
676 /* The DW_AT_comp_dir attribute. */
51ac9db5 677 const char *comp_dir = nullptr;
3019eac3 678
80626a55
DE
679 /* The bfd, when the file is open. Otherwise this is NULL.
680 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 681 gdb_bfd_ref_ptr dbfd;
3019eac3 682
73869dc2
DE
683 /* The sections that make up this DWO file.
684 Remember that for virtual DWO files in DWP V2, these are virtual
685 sections (for lack of a better name). */
51ac9db5 686 struct dwo_sections sections {};
3019eac3 687
33c5cd75
DB
688 /* The CUs in the file.
689 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
690 an extension to handle LLVM's Link Time Optimization output (where
691 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 692 htab_up cus;
3019eac3
DE
693
694 /* Table of TUs in the file.
695 Each element is a struct dwo_unit. */
b0b6a987 696 htab_up tus;
3019eac3
DE
697};
698
80626a55
DE
699/* These sections are what may appear in a DWP file. */
700
701struct dwp_sections
702{
73869dc2 703 /* These are used by both DWP version 1 and 2. */
80626a55
DE
704 struct dwarf2_section_info str;
705 struct dwarf2_section_info cu_index;
706 struct dwarf2_section_info tu_index;
73869dc2
DE
707
708 /* These are only used by DWP version 2 files.
709 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
710 sections are referenced by section number, and are not recorded here.
711 In DWP version 2 there is at most one copy of all these sections, each
712 section being (effectively) comprised of the concatenation of all of the
713 individual sections that exist in the version 1 format.
714 To keep the code simple we treat each of these concatenated pieces as a
715 section itself (a virtual section?). */
716 struct dwarf2_section_info abbrev;
717 struct dwarf2_section_info info;
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
722 struct dwarf2_section_info str_offsets;
723 struct dwarf2_section_info types;
80626a55
DE
724};
725
73869dc2
DE
726/* These sections are what may appear in a virtual DWO file in DWP version 1.
727 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 728
73869dc2 729struct virtual_v1_dwo_sections
80626a55
DE
730{
731 struct dwarf2_section_info abbrev;
732 struct dwarf2_section_info line;
733 struct dwarf2_section_info loc;
734 struct dwarf2_section_info macinfo;
735 struct dwarf2_section_info macro;
736 struct dwarf2_section_info str_offsets;
737 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 738 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
739 struct dwarf2_section_info info_or_types;
740};
741
73869dc2
DE
742/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
743 In version 2, the sections of the DWO files are concatenated together
744 and stored in one section of that name. Thus each ELF section contains
745 several "virtual" sections. */
746
747struct virtual_v2_dwo_sections
748{
749 bfd_size_type abbrev_offset;
750 bfd_size_type abbrev_size;
751
752 bfd_size_type line_offset;
753 bfd_size_type line_size;
754
755 bfd_size_type loc_offset;
756 bfd_size_type loc_size;
757
758 bfd_size_type macinfo_offset;
759 bfd_size_type macinfo_size;
760
761 bfd_size_type macro_offset;
762 bfd_size_type macro_size;
763
764 bfd_size_type str_offsets_offset;
765 bfd_size_type str_offsets_size;
766
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.section. */
769 bfd_size_type info_or_types_offset;
770 bfd_size_type info_or_types_size;
771};
772
80626a55
DE
773/* Contents of DWP hash tables. */
774
775struct dwp_hash_table
776{
73869dc2 777 uint32_t version, nr_columns;
80626a55 778 uint32_t nr_units, nr_slots;
73869dc2
DE
779 const gdb_byte *hash_table, *unit_table;
780 union
781 {
782 struct
783 {
784 const gdb_byte *indices;
785 } v1;
786 struct
787 {
788 /* This is indexed by column number and gives the id of the section
789 in that column. */
790#define MAX_NR_V2_DWO_SECTIONS \
791 (1 /* .debug_info or .debug_types */ \
792 + 1 /* .debug_abbrev */ \
793 + 1 /* .debug_line */ \
794 + 1 /* .debug_loc */ \
795 + 1 /* .debug_str_offsets */ \
796 + 1 /* .debug_macro or .debug_macinfo */)
797 int section_ids[MAX_NR_V2_DWO_SECTIONS];
798 const gdb_byte *offsets;
799 const gdb_byte *sizes;
800 } v2;
801 } section_pool;
80626a55
DE
802};
803
804/* Data for one DWP file. */
805
806struct dwp_file
807{
400174b1
TT
808 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
809 : name (name_),
810 dbfd (std::move (abfd))
811 {
812 }
813
80626a55
DE
814 /* Name of the file. */
815 const char *name;
816
73869dc2 817 /* File format version. */
400174b1 818 int version = 0;
73869dc2 819
93417882 820 /* The bfd. */
400174b1 821 gdb_bfd_ref_ptr dbfd;
80626a55
DE
822
823 /* Section info for this file. */
400174b1 824 struct dwp_sections sections {};
80626a55 825
57d63ce2 826 /* Table of CUs in the file. */
400174b1 827 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
828
829 /* Table of TUs in the file. */
400174b1 830 const struct dwp_hash_table *tus = nullptr;
80626a55 831
19ac8c2e 832 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
833 htab_up loaded_cus;
834 htab_up loaded_tus;
80626a55 835
73869dc2
DE
836 /* Table to map ELF section numbers to their sections.
837 This is only needed for the DWP V1 file format. */
400174b1
TT
838 unsigned int num_sections = 0;
839 asection **elf_sections = nullptr;
80626a55
DE
840};
841
0963b4bd
MS
842/* Struct used to pass misc. parameters to read_die_and_children, et
843 al. which are used for both .debug_info and .debug_types dies.
844 All parameters here are unchanging for the life of the call. This
dee91e82 845 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
846
847struct die_reader_specs
848{
a32a8923 849 /* The bfd of die_section. */
93311388
DE
850 bfd* abfd;
851
852 /* The CU of the DIE we are parsing. */
853 struct dwarf2_cu *cu;
854
80626a55 855 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
856 struct dwo_file *dwo_file;
857
dee91e82 858 /* The section the die comes from.
3019eac3 859 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
860 struct dwarf2_section_info *die_section;
861
862 /* die_section->buffer. */
d521ce57 863 const gdb_byte *buffer;
f664829e
DE
864
865 /* The end of the buffer. */
866 const gdb_byte *buffer_end;
a2ce51a0 867
685af9cd
TT
868 /* The abbreviation table to use when reading the DIEs. */
869 struct abbrev_table *abbrev_table;
93311388
DE
870};
871
c0ab21c2
TT
872/* A subclass of die_reader_specs that holds storage and has complex
873 constructor and destructor behavior. */
874
875class cutu_reader : public die_reader_specs
876{
877public:
878
879 cutu_reader (struct dwarf2_per_cu_data *this_cu,
880 struct abbrev_table *abbrev_table,
6751ebae 881 int use_existing_cu,
c0ab21c2
TT
882 bool skip_partial);
883
884 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
885 struct dwarf2_cu *parent_cu = nullptr,
886 struct dwo_file *dwo_file = nullptr);
887
c0ab21c2
TT
888 DISABLE_COPY_AND_ASSIGN (cutu_reader);
889
890 const gdb_byte *info_ptr = nullptr;
891 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
892 bool dummy_p = false;
893
6751ebae
TT
894 /* Release the new CU, putting it on the chain. This cannot be done
895 for dummy CUs. */
896 void keep ();
897
c0ab21c2
TT
898private:
899 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 900 int use_existing_cu);
c0ab21c2
TT
901
902 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
903 std::unique_ptr<dwarf2_cu> m_new_cu;
904
905 /* The ordinary abbreviation table. */
906 abbrev_table_up m_abbrev_table_holder;
907
908 /* The DWO abbreviation table. */
909 abbrev_table_up m_dwo_abbrev_table;
910};
dee91e82 911
c906108c 912/* When we construct a partial symbol table entry we only
0963b4bd 913 need this much information. */
6f06d47b 914struct partial_die_info : public allocate_on_obstack
c906108c 915 {
6f06d47b
YQ
916 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
917
918 /* Disable assign but still keep copy ctor, which is needed
919 load_partial_dies. */
920 partial_die_info& operator=(const partial_die_info& rhs) = delete;
921
52356b79
YQ
922 /* Adjust the partial die before generating a symbol for it. This
923 function may set the is_external flag or change the DIE's
924 name. */
925 void fixup (struct dwarf2_cu *cu);
926
48fbe735
YQ
927 /* Read a minimal amount of information into the minimal die
928 structure. */
929 const gdb_byte *read (const struct die_reader_specs *reader,
930 const struct abbrev_info &abbrev,
931 const gdb_byte *info_ptr);
932
72bf9492 933 /* Offset of this DIE. */
6f06d47b 934 const sect_offset sect_off;
72bf9492
DJ
935
936 /* DWARF-2 tag for this DIE. */
6f06d47b 937 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 938
72bf9492 939 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
940 const unsigned int has_children : 1;
941
72bf9492
DJ
942 unsigned int is_external : 1;
943 unsigned int is_declaration : 1;
944 unsigned int has_type : 1;
945 unsigned int has_specification : 1;
946 unsigned int has_pc_info : 1;
481860b3 947 unsigned int may_be_inlined : 1;
72bf9492 948
0c1b455e
TT
949 /* This DIE has been marked DW_AT_main_subprogram. */
950 unsigned int main_subprogram : 1;
951
72bf9492
DJ
952 /* Flag set if the SCOPE field of this structure has been
953 computed. */
954 unsigned int scope_set : 1;
955
fa4028e9
JB
956 /* Flag set if the DIE has a byte_size attribute. */
957 unsigned int has_byte_size : 1;
958
ff908ebf
AW
959 /* Flag set if the DIE has a DW_AT_const_value attribute. */
960 unsigned int has_const_value : 1;
961
98bfdba5
PA
962 /* Flag set if any of the DIE's children are template arguments. */
963 unsigned int has_template_arguments : 1;
964
52356b79 965 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
966 unsigned int fixup_called : 1;
967
36586728
TT
968 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
969 unsigned int is_dwz : 1;
970
971 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
972 unsigned int spec_is_dwz : 1;
973
72bf9492 974 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 975 sometimes a default name for unnamed DIEs. */
6f06d47b 976 const char *name = nullptr;
72bf9492 977
abc72ce4 978 /* The linkage name, if present. */
6f06d47b 979 const char *linkage_name = nullptr;
abc72ce4 980
72bf9492
DJ
981 /* The scope to prepend to our children. This is generally
982 allocated on the comp_unit_obstack, so will disappear
983 when this compilation unit leaves the cache. */
6f06d47b 984 const char *scope = nullptr;
72bf9492 985
95554aad
TT
986 /* Some data associated with the partial DIE. The tag determines
987 which field is live. */
988 union
989 {
990 /* The location description associated with this DIE, if any. */
991 struct dwarf_block *locdesc;
992 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 993 sect_offset sect_off;
6f06d47b 994 } d {};
72bf9492
DJ
995
996 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
997 CORE_ADDR lowpc = 0;
998 CORE_ADDR highpc = 0;
72bf9492 999
93311388 1000 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1001 DW_AT_sibling, if any. */
48fbe735
YQ
1002 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1003 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1004 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1005
1006 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1007 DW_AT_specification (or DW_AT_abstract_origin or
1008 DW_AT_extension). */
6f06d47b 1009 sect_offset spec_offset {};
72bf9492
DJ
1010
1011 /* Pointers to this DIE's parent, first child, and next sibling,
1012 if any. */
6f06d47b
YQ
1013 struct partial_die_info *die_parent = nullptr;
1014 struct partial_die_info *die_child = nullptr;
1015 struct partial_die_info *die_sibling = nullptr;
1016
1017 friend struct partial_die_info *
1018 dwarf2_cu::find_partial_die (sect_offset sect_off);
1019
1020 private:
1021 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1022 partial_die_info (sect_offset sect_off)
1023 : partial_die_info (sect_off, DW_TAG_padding, 0)
1024 {
1025 }
1026
1027 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1028 int has_children_)
1029 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1030 {
1031 is_external = 0;
1032 is_declaration = 0;
1033 has_type = 0;
1034 has_specification = 0;
1035 has_pc_info = 0;
1036 may_be_inlined = 0;
1037 main_subprogram = 0;
1038 scope_set = 0;
1039 has_byte_size = 0;
1040 has_const_value = 0;
1041 has_template_arguments = 0;
1042 fixup_called = 0;
1043 is_dwz = 0;
1044 spec_is_dwz = 0;
1045 }
c906108c
SS
1046 };
1047
0963b4bd 1048/* This data structure holds a complete die structure. */
c906108c
SS
1049struct die_info
1050 {
76815b17
DE
1051 /* DWARF-2 tag for this DIE. */
1052 ENUM_BITFIELD(dwarf_tag) tag : 16;
1053
1054 /* Number of attributes */
98bfdba5
PA
1055 unsigned char num_attrs;
1056
1057 /* True if we're presently building the full type name for the
1058 type derived from this DIE. */
1059 unsigned char building_fullname : 1;
76815b17 1060
adde2bff
DE
1061 /* True if this die is in process. PR 16581. */
1062 unsigned char in_process : 1;
1063
3e225074
TT
1064 /* True if this DIE has children. */
1065 unsigned char has_children : 1;
1066
76815b17
DE
1067 /* Abbrev number */
1068 unsigned int abbrev;
1069
93311388 1070 /* Offset in .debug_info or .debug_types section. */
9c541725 1071 sect_offset sect_off;
78ba4af6
JB
1072
1073 /* The dies in a compilation unit form an n-ary tree. PARENT
1074 points to this die's parent; CHILD points to the first child of
1075 this node; and all the children of a given node are chained
4950bc1c 1076 together via their SIBLING fields. */
639d11d3
DC
1077 struct die_info *child; /* Its first child, if any. */
1078 struct die_info *sibling; /* Its next sibling, if any. */
1079 struct die_info *parent; /* Its parent, if any. */
c906108c 1080
b60c80d6
DJ
1081 /* An array of attributes, with NUM_ATTRS elements. There may be
1082 zero, but it's not common and zero-sized arrays are not
1083 sufficiently portable C. */
1084 struct attribute attrs[1];
c906108c
SS
1085 };
1086
c906108c
SS
1087/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090static int bits_per_byte = 8;
1091
2ddeaf8a
TT
1092/* When reading a variant or variant part, we track a bit more
1093 information about the field, and store it in an object of this
1094 type. */
1095
1096struct variant_field
1097{
1098 /* If we see a DW_TAG_variant, then this will be the discriminant
1099 value. */
1100 ULONGEST discriminant_value;
1101 /* If we see a DW_TAG_variant, then this will be set if this is the
1102 default branch. */
1103 bool default_branch;
1104 /* While reading a DW_TAG_variant_part, this will be set if this
1105 field is the discriminant. */
1106 bool is_discriminant;
1107};
1108
52059ffd
TT
1109struct nextfield
1110{
be2daae6
TT
1111 int accessibility = 0;
1112 int virtuality = 0;
2ddeaf8a 1113 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1114 struct variant_field variant {};
1115 struct field field {};
52059ffd
TT
1116};
1117
1118struct fnfieldlist
1119{
be2daae6
TT
1120 const char *name = nullptr;
1121 std::vector<struct fn_field> fnfields;
52059ffd
TT
1122};
1123
c906108c
SS
1124/* The routines that read and process dies for a C struct or C++ class
1125 pass lists of data member fields and lists of member function fields
1126 in an instance of a field_info structure, as defined below. */
1127struct field_info
c5aa993b 1128 {
0963b4bd 1129 /* List of data member and baseclasses fields. */
be2daae6
TT
1130 std::vector<struct nextfield> fields;
1131 std::vector<struct nextfield> baseclasses;
c906108c 1132
85102364 1133 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1134 int non_public_fields = 0;
c906108c 1135
c5aa993b
JM
1136 /* Member function fieldlist array, contains name of possibly overloaded
1137 member function, number of overloaded member functions and a pointer
1138 to the head of the member function field chain. */
be2daae6 1139 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1140
1141 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1142 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1143 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1144
1145 /* Nested types defined by this class and the number of elements in this
1146 list. */
be2daae6 1147 std::vector<struct decl_field> nested_types_list;
317f7127
TT
1148
1149 /* Return the total number of fields (including baseclasses). */
1150 int nfields () const
1151 {
1152 return fields.size () + baseclasses.size ();
1153 }
c5aa993b 1154 };
c906108c 1155
ae038cb0
DJ
1156/* Loaded secondary compilation units are kept in memory until they
1157 have not been referenced for the processing of this many
1158 compilation units. Set this to zero to disable caching. Cache
1159 sizes of up to at least twenty will improve startup time for
1160 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1161static int dwarf_max_cache_age = 5;
920d2a44 1162static void
b4f54984
DE
1163show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1164 struct cmd_list_element *c, const char *value)
920d2a44 1165{
3e43a32a 1166 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1167 "DWARF compilation units is %s.\n"),
920d2a44
AC
1168 value);
1169}
4390d890 1170\f
c906108c
SS
1171/* local function prototypes */
1172
918dd910
JK
1173static void dwarf2_find_base_address (struct die_info *die,
1174 struct dwarf2_cu *cu);
1175
891813be 1176static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1177 (struct dwarf2_per_cu_data *per_cu, const char *name);
1178
f1902523
JK
1179static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1180 const gdb_byte *info_ptr,
3e225074 1181 struct die_info *type_unit_die);
f1902523 1182
ed2dc618
SM
1183static void dwarf2_build_psymtabs_hard
1184 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1185
72bf9492
DJ
1186static void scan_partial_symbols (struct partial_die_info *,
1187 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1188 int, struct dwarf2_cu *);
c906108c 1189
72bf9492
DJ
1190static void add_partial_symbol (struct partial_die_info *,
1191 struct dwarf2_cu *);
63d06c5c 1192
72bf9492
DJ
1193static void add_partial_namespace (struct partial_die_info *pdi,
1194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1195 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1196
5d7cb8df 1197static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1198 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1199 struct dwarf2_cu *cu);
1200
72bf9492
DJ
1201static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1202 struct dwarf2_cu *cu);
91c24f0a 1203
bc30ff58
JB
1204static void add_partial_subprogram (struct partial_die_info *pdi,
1205 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1206 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1207
d521ce57 1208static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1209
dee91e82 1210static struct partial_die_info *load_partial_dies
d521ce57 1211 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1212
fb816e8b
TV
1213/* A pair of partial_die_info and compilation unit. */
1214struct cu_partial_die_info
1215{
1216 /* The compilation unit of the partial_die_info. */
1217 struct dwarf2_cu *cu;
1218 /* A partial_die_info. */
1219 struct partial_die_info *pdi;
122cf0f2
AB
1220
1221 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1222 : cu (cu),
1223 pdi (pdi)
405feb71 1224 { /* Nothing. */ }
122cf0f2
AB
1225
1226private:
1227 cu_partial_die_info () = delete;
fb816e8b
TV
1228};
1229
122cf0f2
AB
1230static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1231 struct dwarf2_cu *);
72bf9492 1232
d521ce57
TT
1233static const gdb_byte *read_attribute (const struct die_reader_specs *,
1234 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1235 const gdb_byte *, bool *need_reprocess);
1236
1237static void read_attribute_reprocess (const struct die_reader_specs *reader,
1238 struct attribute *attr);
1239
1240static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1241
c764a876 1242static LONGEST read_checked_initial_length_and_offset
d521ce57 1243 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1244 unsigned int *, unsigned int *);
613e1657 1245
ed2dc618
SM
1246static sect_offset read_abbrev_offset
1247 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1248 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1249
ed2dc618
SM
1250static const char *read_indirect_string
1251 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1252 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1253
ed2dc618 1254static const char *read_indirect_string_at_offset
4f44ae6c 1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1256
d521ce57
TT
1257static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1258 const gdb_byte *,
3019eac3
DE
1259 unsigned int *);
1260
18a8505e
AT
1261static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1262 ULONGEST str_index);
1263
1264static const char *read_stub_str_index (struct dwarf2_cu *cu,
1265 ULONGEST str_index);
3019eac3 1266
e142c38c 1267static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1268
e142c38c
DJ
1269static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1270 struct dwarf2_cu *);
c906108c 1271
348e048f 1272static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1273 unsigned int);
348e048f 1274
7d45c7c3
KB
1275static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1276 struct dwarf2_cu *cu);
1277
a084a2a6
AT
1278static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1279
05cf31d1
JB
1280static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1281 struct dwarf2_cu *cu);
1282
e142c38c 1283static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1284
e142c38c 1285static struct die_info *die_specification (struct die_info *die,
f2f0e013 1286 struct dwarf2_cu **);
63d06c5c 1287
9c541725 1288static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1289 struct dwarf2_cu *cu);
debd256d 1290
f3f5162e 1291static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1292 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1293 CORE_ADDR, int decode_mapping);
c906108c 1294
804d2729
TT
1295static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1296 const char *);
c906108c 1297
a14ed312 1298static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1299 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1300
ff39bb5e 1301static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1302 struct dwarf2_cu *);
c906108c 1303
ff39bb5e 1304static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1305 struct type *type,
1306 const char *name,
1307 struct obstack *obstack,
12df843f 1308 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1309 const gdb_byte **bytes,
98bfdba5 1310 struct dwarf2_locexpr_baton **baton);
2df3850c 1311
e7c27a73 1312static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1313
b4ba55a1
JB
1314static int need_gnat_info (struct dwarf2_cu *);
1315
3e43a32a
MS
1316static struct type *die_descriptive_type (struct die_info *,
1317 struct dwarf2_cu *);
b4ba55a1
JB
1318
1319static void set_descriptive_type (struct type *, struct die_info *,
1320 struct dwarf2_cu *);
1321
e7c27a73
DJ
1322static struct type *die_containing_type (struct die_info *,
1323 struct dwarf2_cu *);
c906108c 1324
ff39bb5e 1325static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1326 struct dwarf2_cu *);
c906108c 1327
f792889a 1328static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1329
673bfd45
DE
1330static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1331
0d5cff50 1332static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1333
6e70227d 1334static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1335 const char *suffix, int physname,
1336 struct dwarf2_cu *cu);
63d06c5c 1337
e7c27a73 1338static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1339
348e048f
DE
1340static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1341
e7c27a73 1342static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1343
e7c27a73 1344static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1345
96408a79
SA
1346static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1347
71a3c369
TT
1348static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1349
ff013f42 1350static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1351 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1352
3a2b436a 1353/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1354 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1355enum pc_bounds_kind
1356{
e385593e 1357 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1358 PC_BOUNDS_NOT_PRESENT,
1359
e385593e
JK
1360 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1361 were present but they do not form a valid range of PC addresses. */
1362 PC_BOUNDS_INVALID,
1363
3a2b436a
JK
1364 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1365 PC_BOUNDS_RANGES,
1366
1367 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1368 PC_BOUNDS_HIGH_LOW,
1369};
1370
1371static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1372 CORE_ADDR *, CORE_ADDR *,
1373 struct dwarf2_cu *,
891813be 1374 dwarf2_psymtab *);
c906108c 1375
fae299cd
DC
1376static void get_scope_pc_bounds (struct die_info *,
1377 CORE_ADDR *, CORE_ADDR *,
1378 struct dwarf2_cu *);
1379
801e3a5b
JB
1380static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1381 CORE_ADDR, struct dwarf2_cu *);
1382
a14ed312 1383static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1384 struct dwarf2_cu *);
c906108c 1385
a14ed312 1386static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1387 struct type *, struct dwarf2_cu *);
c906108c 1388
a14ed312 1389static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1390 struct die_info *, struct type *,
e7c27a73 1391 struct dwarf2_cu *);
c906108c 1392
a14ed312 1393static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1394 struct type *,
1395 struct dwarf2_cu *);
c906108c 1396
134d01f1 1397static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1398
e7c27a73 1399static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1400
e7c27a73 1401static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1402
5d7cb8df
JK
1403static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1404
804d2729 1405static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1406
27aa8d6a
SW
1407static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1408
74921315
KS
1409static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1410
f55ee35c
JK
1411static struct type *read_module_type (struct die_info *die,
1412 struct dwarf2_cu *cu);
1413
38d518c9 1414static const char *namespace_name (struct die_info *die,
e142c38c 1415 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1416
134d01f1 1417static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1418
e7c27a73 1419static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1420
6e70227d 1421static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1422 struct dwarf2_cu *);
1423
bf6af496 1424static struct die_info *read_die_and_siblings_1
d521ce57 1425 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1426 struct die_info *);
639d11d3 1427
dee91e82 1428static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1429 const gdb_byte *info_ptr,
1430 const gdb_byte **new_info_ptr,
639d11d3
DC
1431 struct die_info *parent);
1432
d521ce57
TT
1433static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1434 struct die_info **, const gdb_byte *,
3e225074 1435 int);
3019eac3 1436
d521ce57 1437static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1438 struct die_info **, const gdb_byte *);
93311388 1439
e7c27a73 1440static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1441
15d034d0 1442static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1443 struct objfile *);
71c25dea 1444
15d034d0 1445static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1446
15d034d0 1447static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1448 struct die_info *die,
1449 struct dwarf2_cu *cu);
1450
ca69b9e6
DE
1451static const char *dwarf2_physname (const char *name, struct die_info *die,
1452 struct dwarf2_cu *cu);
1453
e142c38c 1454static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1455 struct dwarf2_cu **);
9219021c 1456
f39c6ffd 1457static const char *dwarf_tag_name (unsigned int);
c906108c 1458
f39c6ffd 1459static const char *dwarf_attr_name (unsigned int);
c906108c 1460
f39c6ffd 1461static const char *dwarf_form_name (unsigned int);
c906108c 1462
a121b7c1 1463static const char *dwarf_bool_name (unsigned int);
c906108c 1464
f39c6ffd 1465static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1466
f9aca02d 1467static struct die_info *sibling_die (struct die_info *);
c906108c 1468
d97bc12b
DE
1469static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1470
1471static void dump_die_for_error (struct die_info *);
1472
1473static void dump_die_1 (struct ui_file *, int level, int max_level,
1474 struct die_info *);
c906108c 1475
d97bc12b 1476/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1477
51545339 1478static void store_in_ref_table (struct die_info *,
10b3939b 1479 struct dwarf2_cu *);
c906108c 1480
ff39bb5e 1481static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1482
ff39bb5e 1483static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1484
348e048f 1485static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1486 const struct attribute *,
348e048f
DE
1487 struct dwarf2_cu **);
1488
10b3939b 1489static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1490 const struct attribute *,
f2f0e013 1491 struct dwarf2_cu **);
c906108c 1492
348e048f 1493static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1494 const struct attribute *,
348e048f
DE
1495 struct dwarf2_cu **);
1496
ac9ec31b
DE
1497static struct type *get_signatured_type (struct die_info *, ULONGEST,
1498 struct dwarf2_cu *);
1499
1500static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1501 const struct attribute *,
ac9ec31b
DE
1502 struct dwarf2_cu *);
1503
e5fe5e75 1504static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1505
52dc124a 1506static void read_signatured_type (struct signatured_type *);
348e048f 1507
63e43d3a
PMR
1508static int attr_to_dynamic_prop (const struct attribute *attr,
1509 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1510 struct dynamic_prop *prop, struct type *type);
63e43d3a 1511
c906108c
SS
1512/* memory allocation interface */
1513
7b5a2f43 1514static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1515
b60c80d6 1516static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1517
43f3e411 1518static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1519
8cf6f0b1
TT
1520static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1521 struct dwarf2_loclist_baton *baton,
ff39bb5e 1522 const struct attribute *attr);
8cf6f0b1 1523
ff39bb5e 1524static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1525 struct symbol *sym,
f1e6e072
TT
1526 struct dwarf2_cu *cu,
1527 int is_block);
4c2df51b 1528
d521ce57
TT
1529static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1530 const gdb_byte *info_ptr,
1531 struct abbrev_info *abbrev);
4bb7a0a7 1532
72bf9492
DJ
1533static hashval_t partial_die_hash (const void *item);
1534
1535static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1536
ae038cb0 1537static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1538 (sect_offset sect_off, unsigned int offset_in_dwz,
1539 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1540
9816fde3 1541static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1542 struct die_info *comp_unit_die,
1543 enum language pretend_language);
93311388 1544
ed2dc618 1545static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1546
dee91e82 1547static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1548
f792889a
DJ
1549static struct type *set_die_type (struct die_info *, struct type *,
1550 struct dwarf2_cu *);
1c379e20 1551
ed2dc618 1552static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1553
ed2dc618 1554static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1555
58f0c718 1556static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1557 enum language);
10b3939b 1558
95554aad
TT
1559static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1560 enum language);
10b3939b 1561
f4dc4d17
DE
1562static void process_full_type_unit (struct dwarf2_per_cu_data *,
1563 enum language);
1564
10b3939b
DJ
1565static void dwarf2_add_dependence (struct dwarf2_cu *,
1566 struct dwarf2_per_cu_data *);
1567
ae038cb0
DJ
1568static void dwarf2_mark (struct dwarf2_cu *);
1569
1570static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1571
b64f50a1 1572static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1573 struct dwarf2_per_cu_data *);
673bfd45 1574
f792889a 1575static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1576
95554aad
TT
1577static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1578 enum language pretend_language);
1579
ed2dc618 1580static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1581
b303c6f6
AB
1582/* Class, the destructor of which frees all allocated queue entries. This
1583 will only have work to do if an error was thrown while processing the
1584 dwarf. If no error was thrown then the queue entries should have all
1585 been processed, and freed, as we went along. */
1586
1587class dwarf2_queue_guard
1588{
1589public:
39856def
TT
1590 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1591 : m_per_objfile (per_objfile)
1592 {
1593 }
b303c6f6
AB
1594
1595 /* Free any entries remaining on the queue. There should only be
1596 entries left if we hit an error while processing the dwarf. */
1597 ~dwarf2_queue_guard ()
1598 {
39856def
TT
1599 /* Ensure that no memory is allocated by the queue. */
1600 std::queue<dwarf2_queue_item> empty;
1601 std::swap (m_per_objfile->queue, empty);
1602 }
b303c6f6 1603
39856def 1604 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1605
39856def
TT
1606private:
1607 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1608};
1609
39856def
TT
1610dwarf2_queue_item::~dwarf2_queue_item ()
1611{
1612 /* Anything still marked queued is likely to be in an
1613 inconsistent state, so discard it. */
1614 if (per_cu->queued)
1615 {
1616 if (per_cu->cu != NULL)
1617 free_one_cached_comp_unit (per_cu);
1618 per_cu->queued = 0;
1619 }
1620}
1621
d721ba37
PA
1622/* The return type of find_file_and_directory. Note, the enclosed
1623 string pointers are only valid while this object is valid. */
1624
1625struct file_and_directory
1626{
1627 /* The filename. This is never NULL. */
1628 const char *name;
1629
1630 /* The compilation directory. NULL if not known. If we needed to
1631 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1632 points directly to the DW_AT_comp_dir string attribute owned by
1633 the obstack that owns the DIE. */
1634 const char *comp_dir;
1635
1636 /* If we needed to build a new string for comp_dir, this is what
1637 owns the storage. */
1638 std::string comp_dir_storage;
1639};
1640
1641static file_and_directory find_file_and_directory (struct die_info *die,
1642 struct dwarf2_cu *cu);
9291a0cd 1643
298e9637 1644static htab_up allocate_signatured_type_table ();
1fd400ff 1645
298e9637 1646static htab_up allocate_dwo_unit_table ();
3019eac3 1647
57d63ce2 1648static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1649 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1650 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1651 ULONGEST signature, int is_debug_types);
a2ce51a0 1652
ed2dc618
SM
1653static struct dwp_file *get_dwp_file
1654 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1655
3019eac3 1656static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1657 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1658
1659static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1660 (struct signatured_type *, const char *, const char *);
3019eac3 1661
89e63ee4
DE
1662static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1663
263db9a1
TT
1664/* A unique pointer to a dwo_file. */
1665
51ac9db5 1666typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1667
ed2dc618 1668static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1669
1b80a9fa 1670static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1671
1672static void free_line_header_voidp (void *arg);
4390d890
DE
1673\f
1674/* Various complaints about symbol reading that don't abort the process. */
1675
1676static void
1677dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1678{
b98664d3 1679 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1680}
1681
1682static void
1683dwarf2_debug_line_missing_file_complaint (void)
1684{
b98664d3 1685 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1686}
1687
1688static void
1689dwarf2_debug_line_missing_end_sequence_complaint (void)
1690{
b98664d3 1691 complaint (_(".debug_line section has line "
4390d890
DE
1692 "program sequence without an end"));
1693}
1694
1695static void
1696dwarf2_complex_location_expr_complaint (void)
1697{
b98664d3 1698 complaint (_("location expression too complex"));
4390d890
DE
1699}
1700
1701static void
1702dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1703 int arg3)
1704{
b98664d3 1705 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1706 arg1, arg2, arg3);
1707}
1708
4390d890
DE
1709static void
1710dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1711{
b98664d3 1712 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1713 arg1, arg2);
1714}
527f3840
JK
1715
1716/* Hash function for line_header_hash. */
1717
1718static hashval_t
1719line_header_hash (const struct line_header *ofs)
1720{
9c541725 1721 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1722}
1723
1724/* Hash function for htab_create_alloc_ex for line_header_hash. */
1725
1726static hashval_t
1727line_header_hash_voidp (const void *item)
1728{
9a3c8263 1729 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1730
1731 return line_header_hash (ofs);
1732}
1733
1734/* Equality function for line_header_hash. */
1735
1736static int
1737line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1738{
9a3c8263
SM
1739 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1740 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1741
9c541725 1742 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1743 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1744}
1745
4390d890 1746\f
9291a0cd 1747
330cdd98
PA
1748/* See declaration. */
1749
1750dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1751 const dwarf2_debug_sections *names,
1752 bool can_copy_)
1753 : objfile (objfile_),
1754 can_copy (can_copy_)
330cdd98
PA
1755{
1756 if (names == NULL)
1757 names = &dwarf2_elf_names;
1758
1759 bfd *obfd = objfile->obfd;
1760
1761 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1762 locate_sections (obfd, sec, *names);
1763}
1764
1765dwarf2_per_objfile::~dwarf2_per_objfile ()
1766{
1767 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1768 free_cached_comp_units ();
1769
b76e467d 1770 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1771 per_cu->imported_symtabs_free ();
fc8e7e75 1772
b2bdb8cf 1773 for (signatured_type *sig_type : all_type_units)
ae640021 1774 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1775
330cdd98
PA
1776 /* Everything else should be on the objfile obstack. */
1777}
1778
1779/* See declaration. */
1780
1781void
1782dwarf2_per_objfile::free_cached_comp_units ()
1783{
1784 dwarf2_per_cu_data *per_cu = read_in_chain;
1785 dwarf2_per_cu_data **last_chain = &read_in_chain;
1786 while (per_cu != NULL)
1787 {
1788 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1789
fcd3b13d 1790 delete per_cu->cu;
330cdd98
PA
1791 *last_chain = next_cu;
1792 per_cu = next_cu;
1793 }
1794}
1795
11ed8cad
TT
1796/* A helper class that calls free_cached_comp_units on
1797 destruction. */
1798
1799class free_cached_comp_units
1800{
1801public:
1802
1803 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1804 : m_per_objfile (per_objfile)
1805 {
1806 }
1807
1808 ~free_cached_comp_units ()
1809 {
1810 m_per_objfile->free_cached_comp_units ();
1811 }
1812
1813 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1814
1815private:
1816
1817 dwarf2_per_objfile *m_per_objfile;
1818};
1819
c906108c 1820/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1821 information and return true if we have enough to do something.
1822 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1823 ELF names are used. CAN_COPY is true for formats where symbol
1824 interposition is possible and so symbol values must follow copy
1825 relocation rules. */
c906108c
SS
1826
1827int
251d32d9 1828dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1829 const struct dwarf2_debug_sections *names,
1830 bool can_copy)
c906108c 1831{
97cbe998
SDJ
1832 if (objfile->flags & OBJF_READNEVER)
1833 return 0;
1834
ed2dc618
SM
1835 struct dwarf2_per_objfile *dwarf2_per_objfile
1836 = get_dwarf2_per_objfile (objfile);
1837
1838 if (dwarf2_per_objfile == NULL)
5bfd760d 1839 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1840 names,
1841 can_copy);
5bfd760d 1842
73869dc2 1843 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1844 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1845 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1846 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1847}
1848
251d32d9
TG
1849/* When loading sections, we look either for uncompressed section or for
1850 compressed section names. */
233a11ab
CS
1851
1852static int
251d32d9
TG
1853section_is_p (const char *section_name,
1854 const struct dwarf2_section_names *names)
233a11ab 1855{
251d32d9
TG
1856 if (names->normal != NULL
1857 && strcmp (section_name, names->normal) == 0)
1858 return 1;
1859 if (names->compressed != NULL
1860 && strcmp (section_name, names->compressed) == 0)
1861 return 1;
1862 return 0;
233a11ab
CS
1863}
1864
330cdd98 1865/* See declaration. */
c906108c 1866
330cdd98
PA
1867void
1868dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1869 const dwarf2_debug_sections &names)
c906108c 1870{
fd361982 1871 flagword aflag = bfd_section_flags (sectp);
251d32d9 1872
dc7650b8
JK
1873 if ((aflag & SEC_HAS_CONTENTS) == 0)
1874 {
1875 }
950b7495
KS
1876 else if (elf_section_data (sectp)->this_hdr.sh_size
1877 > bfd_get_file_size (abfd))
1878 {
1879 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1880 warning (_("Discarding section %s which has a section size (%s"
1881 ") larger than the file size [in module %s]"),
1882 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1883 bfd_get_filename (abfd));
1884 }
330cdd98 1885 else if (section_is_p (sectp->name, &names.info))
c906108c 1886 {
330cdd98 1887 this->info.s.section = sectp;
fd361982 1888 this->info.size = bfd_section_size (sectp);
c906108c 1889 }
330cdd98 1890 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1891 {
330cdd98 1892 this->abbrev.s.section = sectp;
fd361982 1893 this->abbrev.size = bfd_section_size (sectp);
c906108c 1894 }
330cdd98 1895 else if (section_is_p (sectp->name, &names.line))
c906108c 1896 {
330cdd98 1897 this->line.s.section = sectp;
fd361982 1898 this->line.size = bfd_section_size (sectp);
c906108c 1899 }
330cdd98 1900 else if (section_is_p (sectp->name, &names.loc))
c906108c 1901 {
330cdd98 1902 this->loc.s.section = sectp;
fd361982 1903 this->loc.size = bfd_section_size (sectp);
c906108c 1904 }
330cdd98 1905 else if (section_is_p (sectp->name, &names.loclists))
43988095 1906 {
330cdd98 1907 this->loclists.s.section = sectp;
fd361982 1908 this->loclists.size = bfd_section_size (sectp);
43988095 1909 }
330cdd98 1910 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1911 {
330cdd98 1912 this->macinfo.s.section = sectp;
fd361982 1913 this->macinfo.size = bfd_section_size (sectp);
c906108c 1914 }
330cdd98 1915 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1916 {
330cdd98 1917 this->macro.s.section = sectp;
fd361982 1918 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1919 }
330cdd98 1920 else if (section_is_p (sectp->name, &names.str))
c906108c 1921 {
330cdd98 1922 this->str.s.section = sectp;
fd361982 1923 this->str.size = bfd_section_size (sectp);
c906108c 1924 }
18a8505e
AT
1925 else if (section_is_p (sectp->name, &names.str_offsets))
1926 {
1927 this->str_offsets.s.section = sectp;
1928 this->str_offsets.size = bfd_section_size (sectp);
1929 }
330cdd98 1930 else if (section_is_p (sectp->name, &names.line_str))
43988095 1931 {
330cdd98 1932 this->line_str.s.section = sectp;
fd361982 1933 this->line_str.size = bfd_section_size (sectp);
43988095 1934 }
330cdd98 1935 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1936 {
330cdd98 1937 this->addr.s.section = sectp;
fd361982 1938 this->addr.size = bfd_section_size (sectp);
3019eac3 1939 }
330cdd98 1940 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1941 {
330cdd98 1942 this->frame.s.section = sectp;
fd361982 1943 this->frame.size = bfd_section_size (sectp);
b6af0555 1944 }
330cdd98 1945 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1946 {
330cdd98 1947 this->eh_frame.s.section = sectp;
fd361982 1948 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1949 }
330cdd98 1950 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1951 {
330cdd98 1952 this->ranges.s.section = sectp;
fd361982 1953 this->ranges.size = bfd_section_size (sectp);
af34e669 1954 }
330cdd98 1955 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1956 {
330cdd98 1957 this->rnglists.s.section = sectp;
fd361982 1958 this->rnglists.size = bfd_section_size (sectp);
43988095 1959 }
330cdd98 1960 else if (section_is_p (sectp->name, &names.types))
348e048f 1961 {
8b70b953
TT
1962 struct dwarf2_section_info type_section;
1963
1964 memset (&type_section, 0, sizeof (type_section));
049412e3 1965 type_section.s.section = sectp;
fd361982 1966 type_section.size = bfd_section_size (sectp);
8b70b953 1967
fd5866f6 1968 this->types.push_back (type_section);
348e048f 1969 }
330cdd98 1970 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1971 {
330cdd98 1972 this->gdb_index.s.section = sectp;
fd361982 1973 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1974 }
927aa2e7
JK
1975 else if (section_is_p (sectp->name, &names.debug_names))
1976 {
1977 this->debug_names.s.section = sectp;
fd361982 1978 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1979 }
1980 else if (section_is_p (sectp->name, &names.debug_aranges))
1981 {
1982 this->debug_aranges.s.section = sectp;
fd361982 1983 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1984 }
dce234bc 1985
fd361982
AM
1986 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1987 && bfd_section_vma (sectp) == 0)
330cdd98 1988 this->has_section_at_zero = true;
c906108c
SS
1989}
1990
dce234bc 1991/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1992 SECTION_NAME. */
af34e669 1993
dce234bc 1994void
3017a003
TG
1995dwarf2_get_section_info (struct objfile *objfile,
1996 enum dwarf2_section_enum sect,
d521ce57 1997 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1998 bfd_size_type *sizep)
1999{
5bfd760d 2000 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2001 struct dwarf2_section_info *info;
a3b2a86b
TT
2002
2003 /* We may see an objfile without any DWARF, in which case we just
2004 return nothing. */
2005 if (data == NULL)
2006 {
2007 *sectp = NULL;
2008 *bufp = NULL;
2009 *sizep = 0;
2010 return;
2011 }
3017a003
TG
2012 switch (sect)
2013 {
2014 case DWARF2_DEBUG_FRAME:
2015 info = &data->frame;
2016 break;
2017 case DWARF2_EH_FRAME:
2018 info = &data->eh_frame;
2019 break;
2020 default:
2021 gdb_assert_not_reached ("unexpected section");
2022 }
dce234bc 2023
96b79293 2024 info->read (objfile);
dce234bc 2025
96b79293 2026 *sectp = info->get_bfd_section ();
dce234bc
PP
2027 *bufp = info->buffer;
2028 *sizep = info->size;
2029}
2030
36586728
TT
2031/* A helper function to find the sections for a .dwz file. */
2032
2033static void
2034locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2035{
9a3c8263 2036 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2037
2038 /* Note that we only support the standard ELF names, because .dwz
2039 is ELF-only (at the time of writing). */
2040 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2041 {
049412e3 2042 dwz_file->abbrev.s.section = sectp;
fd361982 2043 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2044 }
2045 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2046 {
049412e3 2047 dwz_file->info.s.section = sectp;
fd361982 2048 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2049 }
2050 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2051 {
049412e3 2052 dwz_file->str.s.section = sectp;
fd361982 2053 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2054 }
2055 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2056 {
049412e3 2057 dwz_file->line.s.section = sectp;
fd361982 2058 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2059 }
2060 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2061 {
049412e3 2062 dwz_file->macro.s.section = sectp;
fd361982 2063 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2064 }
2ec9a5e0
TT
2065 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2066 {
049412e3 2067 dwz_file->gdb_index.s.section = sectp;
fd361982 2068 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2069 }
927aa2e7
JK
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2071 {
2072 dwz_file->debug_names.s.section = sectp;
fd361982 2073 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2074 }
36586728
TT
2075}
2076
c4973306 2077/* See dwarf2read.h. */
36586728 2078
c4973306 2079struct dwz_file *
ed2dc618 2080dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2081{
36586728 2082 const char *filename;
acd13123 2083 bfd_size_type buildid_len_arg;
dc294be5
TT
2084 size_t buildid_len;
2085 bfd_byte *buildid;
36586728
TT
2086
2087 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2088 return dwarf2_per_objfile->dwz_file.get ();
36586728 2089
4db1a1dc 2090 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2091 gdb::unique_xmalloc_ptr<char> data
2092 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2093 &buildid_len_arg, &buildid));
4db1a1dc
TT
2094 if (data == NULL)
2095 {
2096 if (bfd_get_error () == bfd_error_no_error)
2097 return NULL;
2098 error (_("could not read '.gnu_debugaltlink' section: %s"),
2099 bfd_errmsg (bfd_get_error ()));
2100 }
791afaa2
TT
2101
2102 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2103
acd13123
TT
2104 buildid_len = (size_t) buildid_len_arg;
2105
791afaa2 2106 filename = data.get ();
d721ba37
PA
2107
2108 std::string abs_storage;
36586728
TT
2109 if (!IS_ABSOLUTE_PATH (filename))
2110 {
14278e1f
TT
2111 gdb::unique_xmalloc_ptr<char> abs
2112 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2113
14278e1f 2114 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2115 filename = abs_storage.c_str ();
36586728
TT
2116 }
2117
dc294be5
TT
2118 /* First try the file name given in the section. If that doesn't
2119 work, try to use the build-id instead. */
192b62ce 2120 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2121 if (dwz_bfd != NULL)
36586728 2122 {
192b62ce 2123 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2124 dwz_bfd.reset (nullptr);
36586728
TT
2125 }
2126
dc294be5
TT
2127 if (dwz_bfd == NULL)
2128 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2129
0d79cdc4
AM
2130 if (dwz_bfd == nullptr)
2131 {
2132 gdb::unique_xmalloc_ptr<char> alt_filename;
2133 const char *origname = dwarf2_per_objfile->objfile->original_name;
2134
2135 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2136 buildid_len,
2137 origname,
2138 &alt_filename));
2139
2140 if (fd.get () >= 0)
2141 {
2142 /* File successfully retrieved from server. */
2143 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2144
2145 if (dwz_bfd == nullptr)
2146 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2147 alt_filename.get ());
2148 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2149 dwz_bfd.reset (nullptr);
2150 }
2151 }
2152
dc294be5
TT
2153 if (dwz_bfd == NULL)
2154 error (_("could not find '.gnu_debugaltlink' file for %s"),
2155 objfile_name (dwarf2_per_objfile->objfile));
2156
7ff8cb8c
TT
2157 std::unique_ptr<struct dwz_file> result
2158 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2159
7ff8cb8c
TT
2160 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2161 result.get ());
36586728 2162
7ff8cb8c
TT
2163 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2164 result->dwz_bfd.get ());
2165 dwarf2_per_objfile->dwz_file = std::move (result);
2166 return dwarf2_per_objfile->dwz_file.get ();
36586728 2167}
9291a0cd 2168\f
7b9f3c50
DE
2169/* DWARF quick_symbols_functions support. */
2170
2171/* TUs can share .debug_line entries, and there can be a lot more TUs than
2172 unique line tables, so we maintain a separate table of all .debug_line
2173 derived entries to support the sharing.
2174 All the quick functions need is the list of file names. We discard the
2175 line_header when we're done and don't need to record it here. */
2176struct quick_file_names
2177{
094b34ac
DE
2178 /* The data used to construct the hash key. */
2179 struct stmt_list_hash hash;
7b9f3c50
DE
2180
2181 /* The number of entries in file_names, real_names. */
2182 unsigned int num_file_names;
2183
2184 /* The file names from the line table, after being run through
2185 file_full_name. */
2186 const char **file_names;
2187
2188 /* The file names from the line table after being run through
2189 gdb_realpath. These are computed lazily. */
2190 const char **real_names;
2191};
2192
2193/* When using the index (and thus not using psymtabs), each CU has an
2194 object of this type. This is used to hold information needed by
2195 the various "quick" methods. */
2196struct dwarf2_per_cu_quick_data
2197{
2198 /* The file table. This can be NULL if there was no file table
2199 or it's currently not read in.
2200 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2201 struct quick_file_names *file_names;
2202
2203 /* The corresponding symbol table. This is NULL if symbols for this
2204 CU have not yet been read. */
43f3e411 2205 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2206
2207 /* A temporary mark bit used when iterating over all CUs in
2208 expand_symtabs_matching. */
2209 unsigned int mark : 1;
2210
2211 /* True if we've tried to read the file table and found there isn't one.
2212 There will be no point in trying to read it again next time. */
2213 unsigned int no_file_data : 1;
2214};
2215
094b34ac
DE
2216/* Utility hash function for a stmt_list_hash. */
2217
2218static hashval_t
2219hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2220{
2221 hashval_t v = 0;
2222
2223 if (stmt_list_hash->dwo_unit != NULL)
2224 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2225 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2226 return v;
2227}
2228
2229/* Utility equality function for a stmt_list_hash. */
2230
2231static int
2232eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2233 const struct stmt_list_hash *rhs)
2234{
2235 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2236 return 0;
2237 if (lhs->dwo_unit != NULL
2238 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2239 return 0;
2240
9c541725 2241 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2242}
2243
7b9f3c50
DE
2244/* Hash function for a quick_file_names. */
2245
2246static hashval_t
2247hash_file_name_entry (const void *e)
2248{
9a3c8263
SM
2249 const struct quick_file_names *file_data
2250 = (const struct quick_file_names *) e;
7b9f3c50 2251
094b34ac 2252 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2253}
2254
2255/* Equality function for a quick_file_names. */
2256
2257static int
2258eq_file_name_entry (const void *a, const void *b)
2259{
9a3c8263
SM
2260 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2261 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2262
094b34ac 2263 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2264}
2265
2266/* Delete function for a quick_file_names. */
2267
2268static void
2269delete_file_name_entry (void *e)
2270{
9a3c8263 2271 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2272 int i;
2273
2274 for (i = 0; i < file_data->num_file_names; ++i)
2275 {
2276 xfree ((void*) file_data->file_names[i]);
2277 if (file_data->real_names)
2278 xfree ((void*) file_data->real_names[i]);
2279 }
2280
2281 /* The space for the struct itself lives on objfile_obstack,
2282 so we don't free it here. */
2283}
2284
2285/* Create a quick_file_names hash table. */
2286
5895093f 2287static htab_up
7b9f3c50
DE
2288create_quick_file_names_table (unsigned int nr_initial_entries)
2289{
5895093f
TT
2290 return htab_up (htab_create_alloc (nr_initial_entries,
2291 hash_file_name_entry, eq_file_name_entry,
2292 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2293}
9291a0cd 2294
918dd910
JK
2295/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2296 have to be created afterwards. You should call age_cached_comp_units after
2297 processing PER_CU->CU. dw2_setup must have been already called. */
2298
2299static void
58f0c718 2300load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2301{
3019eac3 2302 if (per_cu->is_debug_types)
e5fe5e75 2303 load_full_type_unit (per_cu);
918dd910 2304 else
58f0c718 2305 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2306
cc12ce38
DE
2307 if (per_cu->cu == NULL)
2308 return; /* Dummy CU. */
2dc860c0
DE
2309
2310 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2311}
2312
a0f42c21 2313/* Read in the symbols for PER_CU. */
2fdf6df6 2314
9291a0cd 2315static void
58f0c718 2316dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2317{
ed2dc618 2318 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2319
f4dc4d17
DE
2320 /* Skip type_unit_groups, reading the type units they contain
2321 is handled elsewhere. */
197400e8 2322 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2323 return;
2324
b303c6f6
AB
2325 /* The destructor of dwarf2_queue_guard frees any entries left on
2326 the queue. After this point we're guaranteed to leave this function
2327 with the dwarf queue empty. */
39856def 2328 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2329
95554aad 2330 if (dwarf2_per_objfile->using_index
43f3e411 2331 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2332 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2333 {
2334 queue_comp_unit (per_cu, language_minimal);
58f0c718 2335 load_cu (per_cu, skip_partial);
89e63ee4
DE
2336
2337 /* If we just loaded a CU from a DWO, and we're working with an index
2338 that may badly handle TUs, load all the TUs in that DWO as well.
2339 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2340 if (!per_cu->is_debug_types
cc12ce38 2341 && per_cu->cu != NULL
89e63ee4
DE
2342 && per_cu->cu->dwo_unit != NULL
2343 && dwarf2_per_objfile->index_table != NULL
2344 && dwarf2_per_objfile->index_table->version <= 7
2345 /* DWP files aren't supported yet. */
ed2dc618 2346 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2347 queue_and_load_all_dwo_tus (per_cu);
95554aad 2348 }
9291a0cd 2349
ed2dc618 2350 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2351
2352 /* Age the cache, releasing compilation units that have not
2353 been used recently. */
ed2dc618 2354 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2355}
2356
2357/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2358 the objfile from which this CU came. Returns the resulting symbol
2359 table. */
2fdf6df6 2360
43f3e411 2361static struct compunit_symtab *
58f0c718 2362dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2363{
ed2dc618
SM
2364 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2365
95554aad 2366 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2367 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2368 {
11ed8cad 2369 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2370 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2371 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2372 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2373 }
f194fefb 2374
43f3e411 2375 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2376}
2377
ff4c9fec 2378/* See declaration. */
f4dc4d17 2379
ff4c9fec
SM
2380dwarf2_per_cu_data *
2381dwarf2_per_objfile::get_cutu (int index)
2382{
b76e467d 2383 if (index >= this->all_comp_units.size ())
ff4c9fec 2384 {
b76e467d 2385 index -= this->all_comp_units.size ();
b2bdb8cf 2386 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2387 return &this->all_type_units[index]->per_cu;
2388 }
f4dc4d17 2389
ff4c9fec
SM
2390 return this->all_comp_units[index];
2391}
f4dc4d17 2392
ff4c9fec 2393/* See declaration. */
2fdf6df6 2394
ff4c9fec
SM
2395dwarf2_per_cu_data *
2396dwarf2_per_objfile::get_cu (int index)
1fd400ff 2397{
b76e467d 2398 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2399
ff4c9fec 2400 return this->all_comp_units[index];
f4dc4d17
DE
2401}
2402
ff4c9fec 2403/* See declaration. */
f4dc4d17 2404
ff4c9fec
SM
2405signatured_type *
2406dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2407{
b2bdb8cf 2408 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2409
ff4c9fec 2410 return this->all_type_units[index];
1fd400ff
TT
2411}
2412
4b514bc8
JK
2413/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2414 objfile_obstack, and constructed with the specified field
2415 values. */
2416
2417static dwarf2_per_cu_data *
ed2dc618 2418create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2419 struct dwarf2_section_info *section,
2420 int is_dwz,
2421 sect_offset sect_off, ULONGEST length)
2422{
ed2dc618 2423 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2424 dwarf2_per_cu_data *the_cu
2425 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2426 struct dwarf2_per_cu_data);
2427 the_cu->sect_off = sect_off;
2428 the_cu->length = length;
e3b94546 2429 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2430 the_cu->section = section;
2431 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2432 struct dwarf2_per_cu_quick_data);
2433 the_cu->is_dwz = is_dwz;
2434 return the_cu;
2435}
2436
2ec9a5e0
TT
2437/* A helper for create_cus_from_index that handles a given list of
2438 CUs. */
2fdf6df6 2439
74a0d9f6 2440static void
12359b5e 2441create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2442 const gdb_byte *cu_list, offset_type n_elements,
2443 struct dwarf2_section_info *section,
b76e467d 2444 int is_dwz)
9291a0cd 2445{
12359b5e 2446 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2447 {
74a0d9f6 2448 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2449
2450 sect_offset sect_off
2451 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2452 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2453 cu_list += 2 * 8;
2454
b76e467d 2455 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2456 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2457 sect_off, length);
b76e467d 2458 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2459 }
9291a0cd
TT
2460}
2461
2ec9a5e0 2462/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2463 the CU objects for this objfile. */
2ec9a5e0 2464
74a0d9f6 2465static void
12359b5e 2466create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2467 const gdb_byte *cu_list, offset_type cu_list_elements,
2468 const gdb_byte *dwz_list, offset_type dwz_elements)
2469{
b76e467d
SM
2470 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2471 dwarf2_per_objfile->all_comp_units.reserve
2472 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2473
12359b5e 2474 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2475 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2476
2477 if (dwz_elements == 0)
74a0d9f6 2478 return;
2ec9a5e0 2479
12359b5e
SM
2480 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2481 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2482 &dwz->info, 1);
2ec9a5e0
TT
2483}
2484
1fd400ff 2485/* Create the signatured type hash table from the index. */
673bfd45 2486
74a0d9f6 2487static void
12359b5e
SM
2488create_signatured_type_table_from_index
2489 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2490 struct dwarf2_section_info *section,
2491 const gdb_byte *bytes,
2492 offset_type elements)
1fd400ff 2493{
12359b5e 2494 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2495
b2bdb8cf
SM
2496 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2497 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2498
298e9637 2499 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2500
12359b5e 2501 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2502 {
52dc124a 2503 struct signatured_type *sig_type;
9c541725 2504 ULONGEST signature;
1fd400ff 2505 void **slot;
9c541725 2506 cu_offset type_offset_in_tu;
1fd400ff 2507
74a0d9f6 2508 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2509 sect_offset sect_off
2510 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2511 type_offset_in_tu
2512 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2513 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2514 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2515 bytes += 3 * 8;
2516
52dc124a 2517 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2518 struct signatured_type);
52dc124a 2519 sig_type->signature = signature;
9c541725 2520 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2521 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2522 sig_type->per_cu.section = section;
9c541725 2523 sig_type->per_cu.sect_off = sect_off;
e3b94546 2524 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2525 sig_type->per_cu.v.quick
1fd400ff
TT
2526 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2527 struct dwarf2_per_cu_quick_data);
2528
b0b6a987 2529 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2530 *slot = sig_type;
1fd400ff 2531
b2bdb8cf 2532 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2533 }
2534
b0b6a987 2535 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2536}
2537
927aa2e7
JK
2538/* Create the signatured type hash table from .debug_names. */
2539
2540static void
2541create_signatured_type_table_from_debug_names
ed2dc618 2542 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2543 const mapped_debug_names &map,
2544 struct dwarf2_section_info *section,
2545 struct dwarf2_section_info *abbrev_section)
2546{
ed2dc618
SM
2547 struct objfile *objfile = dwarf2_per_objfile->objfile;
2548
96b79293
TT
2549 section->read (objfile);
2550 abbrev_section->read (objfile);
927aa2e7 2551
b2bdb8cf
SM
2552 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2553 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2554
298e9637 2555 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2556
2557 for (uint32_t i = 0; i < map.tu_count; ++i)
2558 {
2559 struct signatured_type *sig_type;
927aa2e7 2560 void **slot;
927aa2e7
JK
2561
2562 sect_offset sect_off
2563 = (sect_offset) (extract_unsigned_integer
2564 (map.tu_table_reordered + i * map.offset_size,
2565 map.offset_size,
2566 map.dwarf5_byte_order));
2567
2568 comp_unit_head cu_header;
ed2dc618
SM
2569 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2570 abbrev_section,
927aa2e7
JK
2571 section->buffer + to_underlying (sect_off),
2572 rcuh_kind::TYPE);
2573
2574 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2575 struct signatured_type);
2576 sig_type->signature = cu_header.signature;
2577 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2578 sig_type->per_cu.is_debug_types = 1;
2579 sig_type->per_cu.section = section;
2580 sig_type->per_cu.sect_off = sect_off;
e3b94546 2581 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2582 sig_type->per_cu.v.quick
2583 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2584 struct dwarf2_per_cu_quick_data);
2585
b0b6a987 2586 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2587 *slot = sig_type;
2588
b2bdb8cf 2589 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2590 }
2591
b0b6a987 2592 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2593}
2594
9291a0cd
TT
2595/* Read the address map data from the mapped index, and use it to
2596 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2597
9291a0cd 2598static void
ed2dc618
SM
2599create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2600 struct mapped_index *index)
9291a0cd 2601{
ed2dc618 2602 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2603 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2604 const gdb_byte *iter, *end;
9291a0cd 2605 struct addrmap *mutable_map;
9291a0cd
TT
2606 CORE_ADDR baseaddr;
2607
8268c778
PA
2608 auto_obstack temp_obstack;
2609
9291a0cd
TT
2610 mutable_map = addrmap_create_mutable (&temp_obstack);
2611
f00a2de2
PA
2612 iter = index->address_table.data ();
2613 end = iter + index->address_table.size ();
9291a0cd 2614
b3b3bada 2615 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2616
2617 while (iter < end)
2618 {
2619 ULONGEST hi, lo, cu_index;
2620 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2621 iter += 8;
2622 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2623 iter += 8;
2624 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2625 iter += 4;
f652bce2 2626
24a55014 2627 if (lo > hi)
f652bce2 2628 {
b98664d3 2629 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2630 hex_string (lo), hex_string (hi));
24a55014 2631 continue;
f652bce2 2632 }
24a55014 2633
b76e467d 2634 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2635 {
b98664d3 2636 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2637 (unsigned) cu_index);
24a55014 2638 continue;
f652bce2 2639 }
24a55014 2640
79748972
TT
2641 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2642 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2643 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2644 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2645 }
2646
d320c2b5 2647 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2648 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2649}
2650
927aa2e7
JK
2651/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2652 populate the objfile's psymtabs_addrmap. */
2653
2654static void
ed2dc618 2655create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2656 struct dwarf2_section_info *section)
2657{
ed2dc618 2658 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2659 bfd *abfd = objfile->obfd;
2660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2661 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2662
2663 auto_obstack temp_obstack;
2664 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2665
2666 std::unordered_map<sect_offset,
2667 dwarf2_per_cu_data *,
2668 gdb::hash_enum<sect_offset>>
2669 debug_info_offset_to_per_cu;
b76e467d 2670 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2671 {
927aa2e7
JK
2672 const auto insertpair
2673 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2674 if (!insertpair.second)
2675 {
2676 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2677 "debug_info_offset %s, ignoring .debug_aranges."),
2678 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2679 return;
2680 }
2681 }
2682
96b79293 2683 section->read (objfile);
927aa2e7
JK
2684
2685 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2686
2687 const gdb_byte *addr = section->buffer;
2688
2689 while (addr < section->buffer + section->size)
2690 {
2691 const gdb_byte *const entry_addr = addr;
2692 unsigned int bytes_read;
2693
2694 const LONGEST entry_length = read_initial_length (abfd, addr,
2695 &bytes_read);
2696 addr += bytes_read;
2697
2698 const gdb_byte *const entry_end = addr + entry_length;
2699 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2700 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2701 if (addr + entry_length > section->buffer + section->size)
2702 {
47e3f474 2703 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2704 "length %s exceeds section length %s, "
2705 "ignoring .debug_aranges."),
47e3f474
TV
2706 objfile_name (objfile),
2707 plongest (entry_addr - section->buffer),
927aa2e7
JK
2708 plongest (bytes_read + entry_length),
2709 pulongest (section->size));
2710 return;
2711 }
2712
2713 /* The version number. */
2714 const uint16_t version = read_2_bytes (abfd, addr);
2715 addr += 2;
2716 if (version != 2)
2717 {
47e3f474 2718 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2719 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2720 objfile_name (objfile),
2721 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2722 return;
2723 }
2724
2725 const uint64_t debug_info_offset
2726 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2727 addr += offset_size;
2728 const auto per_cu_it
2729 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2730 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2731 {
47e3f474 2732 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2733 "debug_info_offset %s does not exists, "
2734 "ignoring .debug_aranges."),
47e3f474
TV
2735 objfile_name (objfile),
2736 plongest (entry_addr - section->buffer),
927aa2e7
JK
2737 pulongest (debug_info_offset));
2738 return;
2739 }
2740 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2741
2742 const uint8_t address_size = *addr++;
2743 if (address_size < 1 || address_size > 8)
2744 {
47e3f474 2745 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2746 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2747 objfile_name (objfile),
2748 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2749 return;
2750 }
2751
2752 const uint8_t segment_selector_size = *addr++;
2753 if (segment_selector_size != 0)
2754 {
47e3f474 2755 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2756 "segment_selector_size %u is not supported, "
2757 "ignoring .debug_aranges."),
47e3f474
TV
2758 objfile_name (objfile),
2759 plongest (entry_addr - section->buffer),
927aa2e7
JK
2760 segment_selector_size);
2761 return;
2762 }
2763
2764 /* Must pad to an alignment boundary that is twice the address
2765 size. It is undocumented by the DWARF standard but GCC does
2766 use it. */
2767 for (size_t padding = ((-(addr - section->buffer))
2768 & (2 * address_size - 1));
2769 padding > 0; padding--)
2770 if (*addr++ != 0)
2771 {
47e3f474 2772 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2773 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2774 objfile_name (objfile),
2775 plongest (entry_addr - section->buffer));
927aa2e7
JK
2776 return;
2777 }
2778
2779 for (;;)
2780 {
2781 if (addr + 2 * address_size > entry_end)
2782 {
47e3f474 2783 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2784 "address list is not properly terminated, "
2785 "ignoring .debug_aranges."),
47e3f474
TV
2786 objfile_name (objfile),
2787 plongest (entry_addr - section->buffer));
927aa2e7
JK
2788 return;
2789 }
2790 ULONGEST start = extract_unsigned_integer (addr, address_size,
2791 dwarf5_byte_order);
2792 addr += address_size;
2793 ULONGEST length = extract_unsigned_integer (addr, address_size,
2794 dwarf5_byte_order);
2795 addr += address_size;
2796 if (start == 0 && length == 0)
2797 break;
2798 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2799 {
2800 /* Symbol was eliminated due to a COMDAT group. */
2801 continue;
2802 }
2803 ULONGEST end = start + length;
79748972
TT
2804 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2805 - baseaddr);
2806 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2807 - baseaddr);
927aa2e7
JK
2808 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2809 }
2810 }
2811
d320c2b5 2812 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2813 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2814}
2815
9291a0cd
TT
2816/* Find a slot in the mapped index INDEX for the object named NAME.
2817 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2818 constant pool and return true. If NAME cannot be found, return
2819 false. */
2fdf6df6 2820
109483d9 2821static bool
9291a0cd
TT
2822find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2823 offset_type **vec_out)
2824{
0cf03b49 2825 offset_type hash;
9291a0cd 2826 offset_type slot, step;
559a7a62 2827 int (*cmp) (const char *, const char *);
9291a0cd 2828
791afaa2 2829 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2830 if (current_language->la_language == language_cplus
45280282
IB
2831 || current_language->la_language == language_fortran
2832 || current_language->la_language == language_d)
0cf03b49
JK
2833 {
2834 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2835 not contain any. */
a8719064 2836
72998fb3 2837 if (strchr (name, '(') != NULL)
0cf03b49 2838 {
109483d9 2839 without_params = cp_remove_params (name);
0cf03b49 2840
72998fb3 2841 if (without_params != NULL)
791afaa2 2842 name = without_params.get ();
0cf03b49
JK
2843 }
2844 }
2845
559a7a62 2846 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2847 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2848 simulate our NAME being searched is also lowercased. */
2849 hash = mapped_index_string_hash ((index->version == 4
2850 && case_sensitivity == case_sensitive_off
2851 ? 5 : index->version),
2852 name);
2853
f00a2de2
PA
2854 slot = hash & (index->symbol_table.size () - 1);
2855 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2856 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2857
2858 for (;;)
2859 {
9291a0cd 2860 const char *str;
f00a2de2
PA
2861
2862 const auto &bucket = index->symbol_table[slot];
2863 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2864 return false;
9291a0cd 2865
f00a2de2 2866 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2867 if (!cmp (name, str))
9291a0cd
TT
2868 {
2869 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2870 + MAYBE_SWAP (bucket.vec));
109483d9 2871 return true;
9291a0cd
TT
2872 }
2873
f00a2de2 2874 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2875 }
2876}
2877
4485a1c1
SM
2878/* A helper function that reads the .gdb_index from BUFFER and fills
2879 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2880 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2881 ok to use deprecated sections.
2882
2883 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2884 out parameters that are filled in with information about the CU and
2885 TU lists in the section.
2886
4485a1c1 2887 Returns true if all went well, false otherwise. */
2fdf6df6 2888
d33bc52e 2889static bool
4485a1c1
SM
2890read_gdb_index_from_buffer (struct objfile *objfile,
2891 const char *filename,
2892 bool deprecated_ok,
2893 gdb::array_view<const gdb_byte> buffer,
2894 struct mapped_index *map,
2895 const gdb_byte **cu_list,
2896 offset_type *cu_list_elements,
2897 const gdb_byte **types_list,
2898 offset_type *types_list_elements)
2899{
2900 const gdb_byte *addr = &buffer[0];
82430852 2901
9291a0cd 2902 /* Version check. */
4485a1c1 2903 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2904 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2905 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2906 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2907 indices. */
831adc1f 2908 if (version < 4)
481860b3
GB
2909 {
2910 static int warning_printed = 0;
2911 if (!warning_printed)
2912 {
2913 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2914 filename);
481860b3
GB
2915 warning_printed = 1;
2916 }
2917 return 0;
2918 }
2919 /* Index version 4 uses a different hash function than index version
2920 5 and later.
2921
2922 Versions earlier than 6 did not emit psymbols for inlined
2923 functions. Using these files will cause GDB not to be able to
2924 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2925 indices unless the user has done
2926 "set use-deprecated-index-sections on". */
2ec9a5e0 2927 if (version < 6 && !deprecated_ok)
481860b3
GB
2928 {
2929 static int warning_printed = 0;
2930 if (!warning_printed)
2931 {
e615022a
DE
2932 warning (_("\
2933Skipping deprecated .gdb_index section in %s.\n\
2934Do \"set use-deprecated-index-sections on\" before the file is read\n\
2935to use the section anyway."),
2ec9a5e0 2936 filename);
481860b3
GB
2937 warning_printed = 1;
2938 }
2939 return 0;
2940 }
796a7ff8 2941 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2942 of the TU (for symbols coming from TUs),
2943 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2944 Plus gold-generated indices can have duplicate entries for global symbols,
2945 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2946 These are just performance bugs, and we can't distinguish gdb-generated
2947 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2948
481860b3 2949 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2950 longer backward compatible. */
796a7ff8 2951 if (version > 8)
594e8718 2952 return 0;
9291a0cd 2953
559a7a62 2954 map->version = version;
9291a0cd 2955
4485a1c1 2956 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2957
4485a1c1 2958 int i = 0;
2ec9a5e0
TT
2959 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2960 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2961 / 8);
1fd400ff
TT
2962 ++i;
2963
2ec9a5e0
TT
2964 *types_list = addr + MAYBE_SWAP (metadata[i]);
2965 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2966 - MAYBE_SWAP (metadata[i]))
2967 / 8);
987d643c 2968 ++i;
1fd400ff 2969
f00a2de2
PA
2970 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2971 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2972 map->address_table
2973 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2974 ++i;
2975
f00a2de2
PA
2976 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2977 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2978 map->symbol_table
2979 = gdb::array_view<mapped_index::symbol_table_slot>
2980 ((mapped_index::symbol_table_slot *) symbol_table,
2981 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2982
f00a2de2 2983 ++i;
f9d83a0b 2984 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2985
2ec9a5e0
TT
2986 return 1;
2987}
2988
4485a1c1
SM
2989/* Callback types for dwarf2_read_gdb_index. */
2990
2991typedef gdb::function_view
2992 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2993 get_gdb_index_contents_ftype;
2994typedef gdb::function_view
2995 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2996 get_gdb_index_contents_dwz_ftype;
2997
927aa2e7 2998/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2999 elements of all the CUs and return 1. Otherwise, return 0. */
3000
3001static int
4485a1c1
SM
3002dwarf2_read_gdb_index
3003 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3004 get_gdb_index_contents_ftype get_gdb_index_contents,
3005 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3006{
2ec9a5e0
TT
3007 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3008 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3009 struct dwz_file *dwz;
12359b5e 3010 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3011
4485a1c1
SM
3012 gdb::array_view<const gdb_byte> main_index_contents
3013 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3014
3015 if (main_index_contents.empty ())
3016 return 0;
3017
3063847f 3018 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3019 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3020 use_deprecated_index_sections,
3021 main_index_contents, map.get (), &cu_list,
3022 &cu_list_elements, &types_list,
3023 &types_list_elements))
2ec9a5e0
TT
3024 return 0;
3025
0fefef59 3026 /* Don't use the index if it's empty. */
3063847f 3027 if (map->symbol_table.empty ())
0fefef59
DE
3028 return 0;
3029
2ec9a5e0
TT
3030 /* If there is a .dwz file, read it so we can get its CU list as
3031 well. */
ed2dc618 3032 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3033 if (dwz != NULL)
2ec9a5e0 3034 {
2ec9a5e0
TT
3035 struct mapped_index dwz_map;
3036 const gdb_byte *dwz_types_ignore;
3037 offset_type dwz_types_elements_ignore;
3038
4485a1c1
SM
3039 gdb::array_view<const gdb_byte> dwz_index_content
3040 = get_gdb_index_contents_dwz (objfile, dwz);
3041
3042 if (dwz_index_content.empty ())
3043 return 0;
3044
3045 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3046 bfd_get_filename (dwz->dwz_bfd.get ()),
3047 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3048 &dwz_list, &dwz_list_elements,
3049 &dwz_types_ignore,
3050 &dwz_types_elements_ignore))
2ec9a5e0
TT
3051 {
3052 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3053 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3054 return 0;
3055 }
3056 }
3057
12359b5e
SM
3058 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3059 dwz_list, dwz_list_elements);
1fd400ff 3060
8b70b953
TT
3061 if (types_list_elements)
3062 {
8b70b953
TT
3063 /* We can only handle a single .debug_types when we have an
3064 index. */
fd5866f6 3065 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3066 return 0;
3067
fd5866f6 3068 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3069
12359b5e
SM
3070 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3071 types_list, types_list_elements);
8b70b953 3072 }
9291a0cd 3073
3063847f 3074 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3075
3063847f 3076 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3077 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3078 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3079 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3080
3081 return 1;
3082}
3083
dee91e82 3084/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3085
dee91e82
DE
3086static void
3087dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3088 const gdb_byte *info_ptr,
3e225074 3089 struct die_info *comp_unit_die)
9291a0cd 3090{
dee91e82 3091 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3092 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3093 struct dwarf2_per_objfile *dwarf2_per_objfile
3094 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3095 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3096 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3097 struct attribute *attr;
7b9f3c50
DE
3098 void **slot;
3099 struct quick_file_names *qfn;
9291a0cd 3100
0186c6a7
DE
3101 gdb_assert (! this_cu->is_debug_types);
3102
07261596
TT
3103 /* Our callers never want to match partial units -- instead they
3104 will match the enclosing full CU. */
3105 if (comp_unit_die->tag == DW_TAG_partial_unit)
3106 {
3107 this_cu->v.quick->no_file_data = 1;
3108 return;
3109 }
3110
0186c6a7 3111 lh_cu = this_cu;
7b9f3c50 3112 slot = NULL;
dee91e82 3113
fff8551c 3114 line_header_up lh;
9c541725 3115 sect_offset line_offset {};
fff8551c 3116
dee91e82 3117 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3118 if (attr != nullptr)
9291a0cd 3119 {
7b9f3c50
DE
3120 struct quick_file_names find_entry;
3121
9c541725 3122 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3123
3124 /* We may have already read in this line header (TU line header sharing).
3125 If we have we're done. */
094b34ac 3126 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3127 find_entry.hash.line_sect_off = line_offset;
5895093f 3128 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3129 &find_entry, INSERT);
3130 if (*slot != NULL)
3131 {
9a3c8263 3132 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3133 return;
7b9f3c50
DE
3134 }
3135
3019eac3 3136 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3137 }
3138 if (lh == NULL)
3139 {
094b34ac 3140 lh_cu->v.quick->no_file_data = 1;
dee91e82 3141 return;
9291a0cd
TT
3142 }
3143
8d749320 3144 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3145 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3146 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3147 gdb_assert (slot != NULL);
3148 *slot = qfn;
9291a0cd 3149
d721ba37 3150 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3151
aa391654
TT
3152 int offset = 0;
3153 if (strcmp (fnd.name, "<unknown>") != 0)
3154 ++offset;
3155
7ba99d21 3156 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3157 qfn->file_names =
aa391654
TT
3158 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3159 if (offset != 0)
3160 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3161 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3162 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3163 fnd.comp_dir).release ();
7b9f3c50 3164 qfn->real_names = NULL;
9291a0cd 3165
094b34ac 3166 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3167}
3168
3169/* A helper for the "quick" functions which attempts to read the line
3170 table for THIS_CU. */
3171
3172static struct quick_file_names *
e4a48d9d 3173dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3174{
0186c6a7
DE
3175 /* This should never be called for TUs. */
3176 gdb_assert (! this_cu->is_debug_types);
3177 /* Nor type unit groups. */
197400e8 3178 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3179
dee91e82
DE
3180 if (this_cu->v.quick->file_names != NULL)
3181 return this_cu->v.quick->file_names;
3182 /* If we know there is no line data, no point in looking again. */
3183 if (this_cu->v.quick->no_file_data)
3184 return NULL;
3185
c0ab21c2
TT
3186 cutu_reader reader (this_cu);
3187 if (!reader.dummy_p)
3e225074 3188 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3189
3190 if (this_cu->v.quick->no_file_data)
3191 return NULL;
3192 return this_cu->v.quick->file_names;
9291a0cd
TT
3193}
3194
3195/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3196 real path for a given file name from the line table. */
2fdf6df6 3197
9291a0cd 3198static const char *
7b9f3c50
DE
3199dw2_get_real_path (struct objfile *objfile,
3200 struct quick_file_names *qfn, int index)
9291a0cd 3201{
7b9f3c50
DE
3202 if (qfn->real_names == NULL)
3203 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3204 qfn->num_file_names, const char *);
9291a0cd 3205
7b9f3c50 3206 if (qfn->real_names[index] == NULL)
14278e1f 3207 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3208
7b9f3c50 3209 return qfn->real_names[index];
9291a0cd
TT
3210}
3211
3212static struct symtab *
3213dw2_find_last_source_symtab (struct objfile *objfile)
3214{
ed2dc618
SM
3215 struct dwarf2_per_objfile *dwarf2_per_objfile
3216 = get_dwarf2_per_objfile (objfile);
b76e467d 3217 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3218 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3219
43f3e411
DE
3220 if (cust == NULL)
3221 return NULL;
ed2dc618 3222
43f3e411 3223 return compunit_primary_filetab (cust);
9291a0cd
TT
3224}
3225
7b9f3c50
DE
3226/* Traversal function for dw2_forget_cached_source_info. */
3227
3228static int
3229dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3230{
7b9f3c50 3231 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3232
7b9f3c50 3233 if (file_data->real_names)
9291a0cd 3234 {
7b9f3c50 3235 int i;
9291a0cd 3236
7b9f3c50 3237 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3238 {
7b9f3c50
DE
3239 xfree ((void*) file_data->real_names[i]);
3240 file_data->real_names[i] = NULL;
9291a0cd
TT
3241 }
3242 }
7b9f3c50
DE
3243
3244 return 1;
3245}
3246
3247static void
3248dw2_forget_cached_source_info (struct objfile *objfile)
3249{
ed2dc618
SM
3250 struct dwarf2_per_objfile *dwarf2_per_objfile
3251 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3252
5895093f 3253 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3254 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3255}
3256
f8eba3c6
TT
3257/* Helper function for dw2_map_symtabs_matching_filename that expands
3258 the symtabs and calls the iterator. */
3259
3260static int
3261dw2_map_expand_apply (struct objfile *objfile,
3262 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3263 const char *name, const char *real_path,
14bc53a8 3264 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3265{
43f3e411 3266 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3267
3268 /* Don't visit already-expanded CUs. */
43f3e411 3269 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3270 return 0;
3271
3272 /* This may expand more than one symtab, and we want to iterate over
3273 all of them. */
58f0c718 3274 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3275
14bc53a8
PA
3276 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3277 last_made, callback);
f8eba3c6
TT
3278}
3279
3280/* Implementation of the map_symtabs_matching_filename method. */
3281
14bc53a8
PA
3282static bool
3283dw2_map_symtabs_matching_filename
3284 (struct objfile *objfile, const char *name, const char *real_path,
3285 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3286{
c011a4f4 3287 const char *name_basename = lbasename (name);
ed2dc618
SM
3288 struct dwarf2_per_objfile *dwarf2_per_objfile
3289 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3290
848e3e78
DE
3291 /* The rule is CUs specify all the files, including those used by
3292 any TU, so there's no need to scan TUs here. */
f4dc4d17 3293
b76e467d 3294 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3295 {
3d7bb9d9 3296 /* We only need to look at symtabs not already expanded. */
43f3e411 3297 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3298 continue;
3299
b76e467d 3300 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3301 if (file_data == NULL)
9291a0cd
TT
3302 continue;
3303
b76e467d 3304 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3305 {
7b9f3c50 3306 const char *this_name = file_data->file_names[j];
da235a7c 3307 const char *this_real_name;
9291a0cd 3308
af529f8f 3309 if (compare_filenames_for_search (this_name, name))
9291a0cd 3310 {
f5b95b50 3311 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3312 callback))
3313 return true;
288e77a7 3314 continue;
4aac40c8 3315 }
9291a0cd 3316
c011a4f4
DE
3317 /* Before we invoke realpath, which can get expensive when many
3318 files are involved, do a quick comparison of the basenames. */
3319 if (! basenames_may_differ
3320 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3321 continue;
3322
da235a7c
JK
3323 this_real_name = dw2_get_real_path (objfile, file_data, j);
3324 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3325 {
da235a7c 3326 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3327 callback))
3328 return true;
288e77a7 3329 continue;
da235a7c 3330 }
9291a0cd 3331
da235a7c
JK
3332 if (real_path != NULL)
3333 {
af529f8f
JK
3334 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3335 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3336 if (this_real_name != NULL
af529f8f 3337 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3338 {
f5b95b50 3339 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3340 callback))
3341 return true;
288e77a7 3342 continue;
9291a0cd
TT
3343 }
3344 }
3345 }
3346 }
3347
14bc53a8 3348 return false;
9291a0cd
TT
3349}
3350
da51c347
DE
3351/* Struct used to manage iterating over all CUs looking for a symbol. */
3352
3353struct dw2_symtab_iterator
9291a0cd 3354{
ed2dc618
SM
3355 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3356 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3357 /* If set, only look for symbols that match that block. Valid values are
3358 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3359 gdb::optional<block_enum> block_index;
da51c347
DE
3360 /* The kind of symbol we're looking for. */
3361 domain_enum domain;
3362 /* The list of CUs from the index entry of the symbol,
3363 or NULL if not found. */
3364 offset_type *vec;
3365 /* The next element in VEC to look at. */
3366 int next;
3367 /* The number of elements in VEC, or zero if there is no match. */
3368 int length;
8943b874
DE
3369 /* Have we seen a global version of the symbol?
3370 If so we can ignore all further global instances.
3371 This is to work around gold/15646, inefficient gold-generated
3372 indices. */
3373 int global_seen;
da51c347 3374};
9291a0cd 3375
2b79f376 3376/* Initialize the index symtab iterator ITER. */
2fdf6df6 3377
9291a0cd 3378static void
da51c347 3379dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3380 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3381 gdb::optional<block_enum> block_index,
da51c347
DE
3382 domain_enum domain,
3383 const char *name)
3384{
ed2dc618 3385 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3386 iter->block_index = block_index;
3387 iter->domain = domain;
3388 iter->next = 0;
8943b874 3389 iter->global_seen = 0;
da51c347 3390
3063847f 3391 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3392
3393 /* index is NULL if OBJF_READNOW. */
3394 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3395 iter->length = MAYBE_SWAP (*iter->vec);
3396 else
3397 {
3398 iter->vec = NULL;
3399 iter->length = 0;
3400 }
3401}
3402
3403/* Return the next matching CU or NULL if there are no more. */
3404
3405static struct dwarf2_per_cu_data *
3406dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3407{
ed2dc618
SM
3408 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3409
da51c347
DE
3410 for ( ; iter->next < iter->length; ++iter->next)
3411 {
3412 offset_type cu_index_and_attrs =
3413 MAYBE_SWAP (iter->vec[iter->next + 1]);
3414 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3415 gdb_index_symbol_kind symbol_kind =
3416 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3417 /* Only check the symbol attributes if they're present.
3418 Indices prior to version 7 don't record them,
3419 and indices >= 7 may elide them for certain symbols
3420 (gold does this). */
3421 int attrs_valid =
ed2dc618 3422 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3423 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3424
3190f0c6 3425 /* Don't crash on bad data. */
b76e467d 3426 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3427 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3428 {
b98664d3 3429 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3430 " [in module %s]"),
3431 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3432 continue;
3433 }
3434
ff4c9fec 3435 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3436
da51c347 3437 /* Skip if already read in. */
43f3e411 3438 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3439 continue;
3440
8943b874
DE
3441 /* Check static vs global. */
3442 if (attrs_valid)
3443 {
2b79f376
SM
3444 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3445
3446 if (iter->block_index.has_value ())
3447 {
3448 bool want_static = *iter->block_index == STATIC_BLOCK;
3449
3450 if (is_static != want_static)
3451 continue;
3452 }
3453
8943b874
DE
3454 /* Work around gold/15646. */
3455 if (!is_static && iter->global_seen)
3456 continue;
3457 if (!is_static)
3458 iter->global_seen = 1;
3459 }
da51c347
DE
3460
3461 /* Only check the symbol's kind if it has one. */
3462 if (attrs_valid)
3463 {
3464 switch (iter->domain)
3465 {
3466 case VAR_DOMAIN:
3467 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3468 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3469 /* Some types are also in VAR_DOMAIN. */
3470 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3471 continue;
3472 break;
3473 case STRUCT_DOMAIN:
3474 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3475 continue;
3476 break;
3477 case LABEL_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3479 continue;
3480 break;
59c35742
AB
3481 case MODULE_DOMAIN:
3482 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3483 continue;
3484 break;
da51c347
DE
3485 default:
3486 break;
3487 }
3488 }
3489
3490 ++iter->next;
3491 return per_cu;
3492 }
3493
3494 return NULL;
3495}
3496
43f3e411 3497static struct compunit_symtab *
c7f839cb 3498dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3499 const char *name, domain_enum domain)
9291a0cd 3500{
43f3e411 3501 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3502 struct dwarf2_per_objfile *dwarf2_per_objfile
3503 = get_dwarf2_per_objfile (objfile);
9291a0cd 3504
b5ec771e
PA
3505 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3506
ed2dc618
SM
3507 struct dw2_symtab_iterator iter;
3508 struct dwarf2_per_cu_data *per_cu;
da51c347 3509
2b79f376 3510 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3511
ed2dc618
SM
3512 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3513 {
3514 struct symbol *sym, *with_opaque = NULL;
58f0c718 3515 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3516 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3517 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3518
ed2dc618
SM
3519 sym = block_find_symbol (block, name, domain,
3520 block_find_non_opaque_type_preferred,
3521 &with_opaque);
b2e2f908 3522
ed2dc618
SM
3523 /* Some caution must be observed with overloaded functions
3524 and methods, since the index will not contain any overload
3525 information (but NAME might contain it). */
da51c347 3526
ed2dc618
SM
3527 if (sym != NULL
3528 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3529 return stab;
3530 if (with_opaque != NULL
3531 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3532 stab_best = stab;
da51c347 3533
ed2dc618 3534 /* Keep looking through other CUs. */
9291a0cd 3535 }
9291a0cd 3536
da51c347 3537 return stab_best;
9291a0cd
TT
3538}
3539
3540static void
3541dw2_print_stats (struct objfile *objfile)
3542{
ed2dc618
SM
3543 struct dwarf2_per_objfile *dwarf2_per_objfile
3544 = get_dwarf2_per_objfile (objfile);
b76e467d 3545 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3546 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3547 int count = 0;
9291a0cd 3548
ed2dc618 3549 for (int i = 0; i < total; ++i)
9291a0cd 3550 {
ff4c9fec 3551 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3552
43f3e411 3553 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3554 ++count;
3555 }
e4a48d9d 3556 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3557 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3558}
3559
779bd270
DE
3560/* This dumps minimal information about the index.
3561 It is called via "mt print objfiles".
3562 One use is to verify .gdb_index has been loaded by the
3563 gdb.dwarf2/gdb-index.exp testcase. */
3564
9291a0cd
TT
3565static void
3566dw2_dump (struct objfile *objfile)
3567{
ed2dc618
SM
3568 struct dwarf2_per_objfile *dwarf2_per_objfile
3569 = get_dwarf2_per_objfile (objfile);
3570
779bd270
DE
3571 gdb_assert (dwarf2_per_objfile->using_index);
3572 printf_filtered (".gdb_index:");
3573 if (dwarf2_per_objfile->index_table != NULL)
3574 {
3575 printf_filtered (" version %d\n",
3576 dwarf2_per_objfile->index_table->version);
3577 }
3578 else
3579 printf_filtered (" faked for \"readnow\"\n");
3580 printf_filtered ("\n");
9291a0cd
TT
3581}
3582
9291a0cd
TT
3583static void
3584dw2_expand_symtabs_for_function (struct objfile *objfile,
3585 const char *func_name)
3586{
ed2dc618
SM
3587 struct dwarf2_per_objfile *dwarf2_per_objfile
3588 = get_dwarf2_per_objfile (objfile);
da51c347 3589
ed2dc618
SM
3590 struct dw2_symtab_iterator iter;
3591 struct dwarf2_per_cu_data *per_cu;
da51c347 3592
2b79f376 3593 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3594
ed2dc618 3595 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3596 dw2_instantiate_symtab (per_cu, false);
da51c347 3597
9291a0cd
TT
3598}
3599
3600static void
3601dw2_expand_all_symtabs (struct objfile *objfile)
3602{
ed2dc618
SM
3603 struct dwarf2_per_objfile *dwarf2_per_objfile
3604 = get_dwarf2_per_objfile (objfile);
b76e467d 3605 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3606 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3607
ed2dc618 3608 for (int i = 0; i < total_units; ++i)
9291a0cd 3609 {
ff4c9fec 3610 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3611
58f0c718
TT
3612 /* We don't want to directly expand a partial CU, because if we
3613 read it with the wrong language, then assertion failures can
3614 be triggered later on. See PR symtab/23010. So, tell
3615 dw2_instantiate_symtab to skip partial CUs -- any important
3616 partial CU will be read via DW_TAG_imported_unit anyway. */
3617 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3618 }
3619}
3620
3621static void
652a8996
JK
3622dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3623 const char *fullname)
9291a0cd 3624{
ed2dc618
SM
3625 struct dwarf2_per_objfile *dwarf2_per_objfile
3626 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3627
3628 /* We don't need to consider type units here.
3629 This is only called for examining code, e.g. expand_line_sal.
3630 There can be an order of magnitude (or more) more type units
3631 than comp units, and we avoid them if we can. */
3632
b76e467d 3633 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3634 {
3d7bb9d9 3635 /* We only need to look at symtabs not already expanded. */
43f3e411 3636 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3637 continue;
3638
b76e467d 3639 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3640 if (file_data == NULL)
9291a0cd
TT
3641 continue;
3642
b76e467d 3643 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3644 {
652a8996
JK
3645 const char *this_fullname = file_data->file_names[j];
3646
3647 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3648 {
58f0c718 3649 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3650 break;
3651 }
3652 }
3653 }
3654}
3655
9291a0cd 3656static void
199b4314
TT
3657dw2_map_matching_symbols
3658 (struct objfile *objfile,
b054970d 3659 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3660 int global,
3661 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3662 symbol_compare_ftype *ordered_compare)
9291a0cd 3663{
40658b94 3664 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3665 current language is Ada for a non-Ada objfile using GNU index. As Ada
3666 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3667}
3668
e1ef7d7a
PA
3669/* Starting from a search name, return the string that finds the upper
3670 bound of all strings that start with SEARCH_NAME in a sorted name
3671 list. Returns the empty string to indicate that the upper bound is
3672 the end of the list. */
3673
3674static std::string
3675make_sort_after_prefix_name (const char *search_name)
3676{
3677 /* When looking to complete "func", we find the upper bound of all
3678 symbols that start with "func" by looking for where we'd insert
3679 the closest string that would follow "func" in lexicographical
3680 order. Usually, that's "func"-with-last-character-incremented,
3681 i.e. "fund". Mind non-ASCII characters, though. Usually those
3682 will be UTF-8 multi-byte sequences, but we can't be certain.
3683 Especially mind the 0xff character, which is a valid character in
3684 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3685 rule out compilers allowing it in identifiers. Note that
3686 conveniently, strcmp/strcasecmp are specified to compare
3687 characters interpreted as unsigned char. So what we do is treat
3688 the whole string as a base 256 number composed of a sequence of
3689 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3690 to 0, and carries 1 to the following more-significant position.
3691 If the very first character in SEARCH_NAME ends up incremented
3692 and carries/overflows, then the upper bound is the end of the
3693 list. The string after the empty string is also the empty
3694 string.
3695
3696 Some examples of this operation:
3697
3698 SEARCH_NAME => "+1" RESULT
3699
3700 "abc" => "abd"
3701 "ab\xff" => "ac"
3702 "\xff" "a" "\xff" => "\xff" "b"
3703 "\xff" => ""
3704 "\xff\xff" => ""
3705 "" => ""
3706
3707 Then, with these symbols for example:
3708
3709 func
3710 func1
3711 fund
3712
3713 completing "func" looks for symbols between "func" and
3714 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3715 which finds "func" and "func1", but not "fund".
3716
3717 And with:
3718
3719 funcÿ (Latin1 'ÿ' [0xff])
3720 funcÿ1
3721 fund
3722
3723 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3724 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3725
3726 And with:
3727
3728 ÿÿ (Latin1 'ÿ' [0xff])
3729 ÿÿ1
3730
3731 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3732 the end of the list.
3733 */
3734 std::string after = search_name;
3735 while (!after.empty () && (unsigned char) after.back () == 0xff)
3736 after.pop_back ();
3737 if (!after.empty ())
3738 after.back () = (unsigned char) after.back () + 1;
3739 return after;
3740}
3741
5c58de74 3742/* See declaration. */
61d96d7e 3743
5c58de74
PA
3744std::pair<std::vector<name_component>::const_iterator,
3745 std::vector<name_component>::const_iterator>
44ed8f3e 3746mapped_index_base::find_name_components_bounds
3b00ef10 3747 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3748{
5c58de74
PA
3749 auto *name_cmp
3750 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3751
3b00ef10
TT
3752 const char *lang_name
3753 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3754
3f563c84
PA
3755 /* Comparison function object for lower_bound that matches against a
3756 given symbol name. */
3757 auto lookup_compare_lower = [&] (const name_component &elem,
3758 const char *name)
3759 {
5c58de74 3760 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3761 const char *elem_name = elem_qualified + elem.name_offset;
3762 return name_cmp (elem_name, name) < 0;
3763 };
3764
3765 /* Comparison function object for upper_bound that matches against a
3766 given symbol name. */
3767 auto lookup_compare_upper = [&] (const char *name,
3768 const name_component &elem)
3769 {
5c58de74 3770 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3771 const char *elem_name = elem_qualified + elem.name_offset;
3772 return name_cmp (name, elem_name) < 0;
3773 };
3774
5c58de74
PA
3775 auto begin = this->name_components.begin ();
3776 auto end = this->name_components.end ();
3f563c84
PA
3777
3778 /* Find the lower bound. */
3779 auto lower = [&] ()
3780 {
3b00ef10 3781 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3782 return begin;
3783 else
3b00ef10 3784 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3785 } ();
3786
3787 /* Find the upper bound. */
3788 auto upper = [&] ()
3789 {
5c58de74 3790 if (lookup_name_without_params.completion_mode ())
3f563c84 3791 {
e1ef7d7a
PA
3792 /* In completion mode, we want UPPER to point past all
3793 symbols names that have the same prefix. I.e., with
3794 these symbols, and completing "func":
3795
3796 function << lower bound
3797 function1
3798 other_function << upper bound
3799
3800 We find the upper bound by looking for the insertion
3801 point of "func"-with-last-character-incremented,
3802 i.e. "fund". */
3b00ef10 3803 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3804 if (after.empty ())
3f563c84 3805 return end;
e6b2f5ef
PA
3806 return std::lower_bound (lower, end, after.c_str (),
3807 lookup_compare_lower);
3f563c84
PA
3808 }
3809 else
3b00ef10 3810 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3811 } ();
3812
5c58de74
PA
3813 return {lower, upper};
3814}
3815
3816/* See declaration. */
3817
3818void
44ed8f3e 3819mapped_index_base::build_name_components ()
5c58de74
PA
3820{
3821 if (!this->name_components.empty ())
3822 return;
3823
3824 this->name_components_casing = case_sensitivity;
3825 auto *name_cmp
3826 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3827
3828 /* The code below only knows how to break apart components of C++
3829 symbol names (and other languages that use '::' as
3b00ef10 3830 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3831 auto count = this->symbol_name_count ();
3832 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3833 {
44ed8f3e 3834 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3835 continue;
3836
3837 const char *name = this->symbol_name_at (idx);
3838
3839 /* Add each name component to the name component table. */
3840 unsigned int previous_len = 0;
3b00ef10
TT
3841
3842 if (strstr (name, "::") != nullptr)
3843 {
3844 for (unsigned int current_len = cp_find_first_component (name);
3845 name[current_len] != '\0';
3846 current_len += cp_find_first_component (name + current_len))
3847 {
3848 gdb_assert (name[current_len] == ':');
3849 this->name_components.push_back ({previous_len, idx});
3850 /* Skip the '::'. */
3851 current_len += 2;
3852 previous_len = current_len;
3853 }
3854 }
3855 else
5c58de74 3856 {
3b00ef10
TT
3857 /* Handle the Ada encoded (aka mangled) form here. */
3858 for (const char *iter = strstr (name, "__");
3859 iter != nullptr;
3860 iter = strstr (iter, "__"))
3861 {
3862 this->name_components.push_back ({previous_len, idx});
3863 iter += 2;
3864 previous_len = iter - name;
3865 }
5c58de74 3866 }
3b00ef10 3867
5c58de74
PA
3868 this->name_components.push_back ({previous_len, idx});
3869 }
3870
3871 /* Sort name_components elements by name. */
3872 auto name_comp_compare = [&] (const name_component &left,
3873 const name_component &right)
3874 {
3875 const char *left_qualified = this->symbol_name_at (left.idx);
3876 const char *right_qualified = this->symbol_name_at (right.idx);
3877
3878 const char *left_name = left_qualified + left.name_offset;
3879 const char *right_name = right_qualified + right.name_offset;
3880
3881 return name_cmp (left_name, right_name) < 0;
3882 };
3883
3884 std::sort (this->name_components.begin (),
3885 this->name_components.end (),
3886 name_comp_compare);
3887}
3888
3889/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3890 mapped_index_base instead of the containing objfile. This is split
3891 to a separate function in order to be able to unit test the
3892 name_components matching using a mock mapped_index_base. For each
5c58de74 3893 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3894 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3895
3896static void
3897dw2_expand_symtabs_matching_symbol
44ed8f3e 3898 (mapped_index_base &index,
5c58de74
PA
3899 const lookup_name_info &lookup_name_in,
3900 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3901 enum search_domain kind,
3b00ef10 3902 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3903{
3904 lookup_name_info lookup_name_without_params
3905 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3906
3907 /* Build the symbol name component sorted vector, if we haven't
3908 yet. */
3909 index.build_name_components ();
3910
3f563c84
PA
3911 /* The same symbol may appear more than once in the range though.
3912 E.g., if we're looking for symbols that complete "w", and we have
3913 a symbol named "w1::w2", we'll find the two name components for
3914 that same symbol in the range. To be sure we only call the
3915 callback once per symbol, we first collect the symbol name
3916 indexes that matched in a temporary vector and ignore
3917 duplicates. */
3918 std::vector<offset_type> matches;
3f563c84 3919
3b00ef10
TT
3920 struct name_and_matcher
3921 {
3922 symbol_name_matcher_ftype *matcher;
3923 const std::string &name;
3924
3925 bool operator== (const name_and_matcher &other) const
3f563c84 3926 {
3b00ef10
TT
3927 return matcher == other.matcher && name == other.name;
3928 }
3929 };
3930
3931 /* A vector holding all the different symbol name matchers, for all
3932 languages. */
3933 std::vector<name_and_matcher> matchers;
3934
3935 for (int i = 0; i < nr_languages; i++)
3936 {
3937 enum language lang_e = (enum language) i;
3938
3939 const language_defn *lang = language_def (lang_e);
3940 symbol_name_matcher_ftype *name_matcher
3941 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3942
3b00ef10
TT
3943 name_and_matcher key {
3944 name_matcher,
3945 lookup_name_without_params.language_lookup_name (lang_e)
3946 };
3947
3948 /* Don't insert the same comparison routine more than once.
3949 Note that we do this linear walk. This is not a problem in
3950 practice because the number of supported languages is
3951 low. */
3952 if (std::find (matchers.begin (), matchers.end (), key)
3953 != matchers.end ())
9291a0cd 3954 continue;
3b00ef10
TT
3955 matchers.push_back (std::move (key));
3956
3957 auto bounds
3958 = index.find_name_components_bounds (lookup_name_without_params,
3959 lang_e);
3960
3961 /* Now for each symbol name in range, check to see if we have a name
3962 match, and if so, call the MATCH_CALLBACK callback. */
3963
3964 for (; bounds.first != bounds.second; ++bounds.first)
3965 {
3966 const char *qualified = index.symbol_name_at (bounds.first->idx);
3967
3968 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3969 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3970 continue;
9291a0cd 3971
3b00ef10
TT
3972 matches.push_back (bounds.first->idx);
3973 }
3f563c84
PA
3974 }
3975
3976 std::sort (matches.begin (), matches.end ());
3977
3978 /* Finally call the callback, once per match. */
3979 ULONGEST prev = -1;
3980 for (offset_type idx : matches)
3981 {
3982 if (prev != idx)
3983 {
3b00ef10
TT
3984 if (!match_callback (idx))
3985 break;
3f563c84
PA
3986 prev = idx;
3987 }
3988 }
3989
3990 /* Above we use a type wider than idx's for 'prev', since 0 and
3991 (offset_type)-1 are both possible values. */
3992 static_assert (sizeof (prev) > sizeof (offset_type), "");
3993}
3994
c62446b1
PA
3995#if GDB_SELF_TEST
3996
3997namespace selftests { namespace dw2_expand_symtabs_matching {
3998
a3c5fafd
PA
3999/* A mock .gdb_index/.debug_names-like name index table, enough to
4000 exercise dw2_expand_symtabs_matching_symbol, which works with the
4001 mapped_index_base interface. Builds an index from the symbol list
4002 passed as parameter to the constructor. */
4003class mock_mapped_index : public mapped_index_base
c62446b1
PA
4004{
4005public:
a3c5fafd
PA
4006 mock_mapped_index (gdb::array_view<const char *> symbols)
4007 : m_symbol_table (symbols)
c62446b1
PA
4008 {}
4009
a3c5fafd 4010 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4011
a3c5fafd 4012 /* Return the number of names in the symbol table. */
632e107b 4013 size_t symbol_name_count () const override
c62446b1 4014 {
a3c5fafd 4015 return m_symbol_table.size ();
c62446b1
PA
4016 }
4017
a3c5fafd 4018 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4019 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4020 {
4021 return m_symbol_table[idx];
4022 }
c62446b1 4023
a3c5fafd
PA
4024private:
4025 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4026};
4027
4028/* Convenience function that converts a NULL pointer to a "<null>"
4029 string, to pass to print routines. */
4030
4031static const char *
4032string_or_null (const char *str)
4033{
4034 return str != NULL ? str : "<null>";
4035}
4036
4037/* Check if a lookup_name_info built from
4038 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4039 index. EXPECTED_LIST is the list of expected matches, in expected
4040 matching order. If no match expected, then an empty list is
4041 specified. Returns true on success. On failure prints a warning
4042 indicating the file:line that failed, and returns false. */
4043
4044static bool
4045check_match (const char *file, int line,
4046 mock_mapped_index &mock_index,
4047 const char *name, symbol_name_match_type match_type,
4048 bool completion_mode,
4049 std::initializer_list<const char *> expected_list)
4050{
4051 lookup_name_info lookup_name (name, match_type, completion_mode);
4052
4053 bool matched = true;
4054
4055 auto mismatch = [&] (const char *expected_str,
4056 const char *got)
4057 {
4058 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4059 "expected=\"%s\", got=\"%s\"\n"),
4060 file, line,
4061 (match_type == symbol_name_match_type::FULL
4062 ? "FULL" : "WILD"),
4063 name, string_or_null (expected_str), string_or_null (got));
4064 matched = false;
4065 };
4066
4067 auto expected_it = expected_list.begin ();
4068 auto expected_end = expected_list.end ();
4069
a3c5fafd 4070 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4071 NULL, ALL_DOMAIN,
4072 [&] (offset_type idx)
4073 {
a3c5fafd 4074 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4075 const char *expected_str
4076 = expected_it == expected_end ? NULL : *expected_it++;
4077
4078 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4079 mismatch (expected_str, matched_name);
3b00ef10 4080 return true;
c62446b1
PA
4081 });
4082
4083 const char *expected_str
4084 = expected_it == expected_end ? NULL : *expected_it++;
4085 if (expected_str != NULL)
4086 mismatch (expected_str, NULL);
4087
4088 return matched;
4089}
4090
4091/* The symbols added to the mock mapped_index for testing (in
4092 canonical form). */
4093static const char *test_symbols[] = {
4094 "function",
4095 "std::bar",
4096 "std::zfunction",
4097 "std::zfunction2",
4098 "w1::w2",
4099 "ns::foo<char*>",
4100 "ns::foo<int>",
4101 "ns::foo<long>",
a20714ff
PA
4102 "ns2::tmpl<int>::foo2",
4103 "(anonymous namespace)::A::B::C",
c62446b1 4104
e1ef7d7a
PA
4105 /* These are used to check that the increment-last-char in the
4106 matching algorithm for completion doesn't match "t1_fund" when
4107 completing "t1_func". */
4108 "t1_func",
4109 "t1_func1",
4110 "t1_fund",
4111 "t1_fund1",
4112
4113 /* A UTF-8 name with multi-byte sequences to make sure that
4114 cp-name-parser understands this as a single identifier ("função"
4115 is "function" in PT). */
4116 u8"u8função",
4117
4118 /* \377 (0xff) is Latin1 'ÿ'. */
4119 "yfunc\377",
4120
4121 /* \377 (0xff) is Latin1 'ÿ'. */
4122 "\377",
4123 "\377\377123",
4124
c62446b1
PA
4125 /* A name with all sorts of complications. Starts with "z" to make
4126 it easier for the completion tests below. */
4127#define Z_SYM_NAME \
4128 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4129 "::tuple<(anonymous namespace)::ui*, " \
4130 "std::default_delete<(anonymous namespace)::ui>, void>"
4131
4132 Z_SYM_NAME
4133};
4134
a3c5fafd
PA
4135/* Returns true if the mapped_index_base::find_name_component_bounds
4136 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4137 in completion mode. */
5c58de74
PA
4138
4139static bool
a3c5fafd 4140check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4141 const char *search_name,
4142 gdb::array_view<const char *> expected_syms)
4143{
4144 lookup_name_info lookup_name (search_name,
4145 symbol_name_match_type::FULL, true);
4146
3b00ef10
TT
4147 auto bounds = index.find_name_components_bounds (lookup_name,
4148 language_cplus);
5c58de74
PA
4149
4150 size_t distance = std::distance (bounds.first, bounds.second);
4151 if (distance != expected_syms.size ())
4152 return false;
4153
4154 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4155 {
4156 auto nc_elem = bounds.first + exp_elem;
4157 const char *qualified = index.symbol_name_at (nc_elem->idx);
4158 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4159 return false;
4160 }
4161
4162 return true;
4163}
4164
4165/* Test the lower-level mapped_index::find_name_component_bounds
4166 method. */
4167
c62446b1 4168static void
5c58de74
PA
4169test_mapped_index_find_name_component_bounds ()
4170{
4171 mock_mapped_index mock_index (test_symbols);
4172
a3c5fafd 4173 mock_index.build_name_components ();
5c58de74
PA
4174
4175 /* Test the lower-level mapped_index::find_name_component_bounds
4176 method in completion mode. */
4177 {
4178 static const char *expected_syms[] = {
4179 "t1_func",
4180 "t1_func1",
5c58de74
PA
4181 };
4182
a3c5fafd 4183 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4184 "t1_func", expected_syms));
4185 }
4186
4187 /* Check that the increment-last-char in the name matching algorithm
4188 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4189 {
4190 static const char *expected_syms1[] = {
4191 "\377",
4192 "\377\377123",
4193 };
a3c5fafd 4194 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4195 "\377", expected_syms1));
4196
4197 static const char *expected_syms2[] = {
4198 "\377\377123",
4199 };
a3c5fafd 4200 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4201 "\377\377", expected_syms2));
4202 }
4203}
4204
4205/* Test dw2_expand_symtabs_matching_symbol. */
4206
4207static void
4208test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4209{
4210 mock_mapped_index mock_index (test_symbols);
4211
4212 /* We let all tests run until the end even if some fails, for debug
4213 convenience. */
4214 bool any_mismatch = false;
4215
4216 /* Create the expected symbols list (an initializer_list). Needed
4217 because lists have commas, and we need to pass them to CHECK,
4218 which is a macro. */
4219#define EXPECT(...) { __VA_ARGS__ }
4220
4221 /* Wrapper for check_match that passes down the current
4222 __FILE__/__LINE__. */
4223#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4224 any_mismatch |= !check_match (__FILE__, __LINE__, \
4225 mock_index, \
4226 NAME, MATCH_TYPE, COMPLETION_MODE, \
4227 EXPECTED_LIST)
4228
4229 /* Identity checks. */
4230 for (const char *sym : test_symbols)
4231 {
4232 /* Should be able to match all existing symbols. */
4233 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4234 EXPECT (sym));
4235
4236 /* Should be able to match all existing symbols with
4237 parameters. */
4238 std::string with_params = std::string (sym) + "(int)";
4239 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4240 EXPECT (sym));
4241
4242 /* Should be able to match all existing symbols with
4243 parameters and qualifiers. */
4244 with_params = std::string (sym) + " ( int ) const";
4245 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4246 EXPECT (sym));
4247
4248 /* This should really find sym, but cp-name-parser.y doesn't
4249 know about lvalue/rvalue qualifiers yet. */
4250 with_params = std::string (sym) + " ( int ) &&";
4251 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4252 {});
4253 }
4254
e1ef7d7a
PA
4255 /* Check that the name matching algorithm for completion doesn't get
4256 confused with Latin1 'ÿ' / 0xff. */
4257 {
4258 static const char str[] = "\377";
4259 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4260 EXPECT ("\377", "\377\377123"));
4261 }
4262
4263 /* Check that the increment-last-char in the matching algorithm for
4264 completion doesn't match "t1_fund" when completing "t1_func". */
4265 {
4266 static const char str[] = "t1_func";
4267 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4268 EXPECT ("t1_func", "t1_func1"));
4269 }
4270
c62446b1
PA
4271 /* Check that completion mode works at each prefix of the expected
4272 symbol name. */
4273 {
4274 static const char str[] = "function(int)";
4275 size_t len = strlen (str);
4276 std::string lookup;
4277
4278 for (size_t i = 1; i < len; i++)
4279 {
4280 lookup.assign (str, i);
4281 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4282 EXPECT ("function"));
4283 }
4284 }
4285
4286 /* While "w" is a prefix of both components, the match function
4287 should still only be called once. */
4288 {
4289 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4290 EXPECT ("w1::w2"));
a20714ff
PA
4291 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4292 EXPECT ("w1::w2"));
c62446b1
PA
4293 }
4294
4295 /* Same, with a "complicated" symbol. */
4296 {
4297 static const char str[] = Z_SYM_NAME;
4298 size_t len = strlen (str);
4299 std::string lookup;
4300
4301 for (size_t i = 1; i < len; i++)
4302 {
4303 lookup.assign (str, i);
4304 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4305 EXPECT (Z_SYM_NAME));
4306 }
4307 }
4308
4309 /* In FULL mode, an incomplete symbol doesn't match. */
4310 {
4311 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4312 {});
4313 }
4314
4315 /* A complete symbol with parameters matches any overload, since the
4316 index has no overload info. */
4317 {
4318 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4319 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4320 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4321 EXPECT ("std::zfunction", "std::zfunction2"));
4322 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4323 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4324 }
4325
4326 /* Check that whitespace is ignored appropriately. A symbol with a
4327 template argument list. */
4328 {
4329 static const char expected[] = "ns::foo<int>";
4330 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4331 EXPECT (expected));
a20714ff
PA
4332 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4333 EXPECT (expected));
c62446b1
PA
4334 }
4335
4336 /* Check that whitespace is ignored appropriately. A symbol with a
4337 template argument list that includes a pointer. */
4338 {
4339 static const char expected[] = "ns::foo<char*>";
4340 /* Try both completion and non-completion modes. */
4341 static const bool completion_mode[2] = {false, true};
4342 for (size_t i = 0; i < 2; i++)
4343 {
4344 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4345 completion_mode[i], EXPECT (expected));
a20714ff
PA
4346 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4347 completion_mode[i], EXPECT (expected));
c62446b1
PA
4348
4349 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4350 completion_mode[i], EXPECT (expected));
a20714ff
PA
4351 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4352 completion_mode[i], EXPECT (expected));
c62446b1
PA
4353 }
4354 }
4355
4356 {
4357 /* Check method qualifiers are ignored. */
4358 static const char expected[] = "ns::foo<char*>";
4359 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4360 symbol_name_match_type::FULL, true, EXPECT (expected));
4361 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4362 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4363 CHECK_MATCH ("foo < char * > ( int ) const",
4364 symbol_name_match_type::WILD, true, EXPECT (expected));
4365 CHECK_MATCH ("foo < char * > ( int ) &&",
4366 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4367 }
4368
4369 /* Test lookup names that don't match anything. */
4370 {
a20714ff
PA
4371 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4372 {});
4373
c62446b1
PA
4374 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4375 {});
4376 }
4377
a20714ff
PA
4378 /* Some wild matching tests, exercising "(anonymous namespace)",
4379 which should not be confused with a parameter list. */
4380 {
4381 static const char *syms[] = {
4382 "A::B::C",
4383 "B::C",
4384 "C",
4385 "A :: B :: C ( int )",
4386 "B :: C ( int )",
4387 "C ( int )",
4388 };
4389
4390 for (const char *s : syms)
4391 {
4392 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4393 EXPECT ("(anonymous namespace)::A::B::C"));
4394 }
4395 }
4396
4397 {
4398 static const char expected[] = "ns2::tmpl<int>::foo2";
4399 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4400 EXPECT (expected));
4401 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4402 EXPECT (expected));
4403 }
4404
c62446b1
PA
4405 SELF_CHECK (!any_mismatch);
4406
4407#undef EXPECT
4408#undef CHECK_MATCH
4409}
4410
5c58de74
PA
4411static void
4412run_test ()
4413{
4414 test_mapped_index_find_name_component_bounds ();
4415 test_dw2_expand_symtabs_matching_symbol ();
4416}
4417
c62446b1
PA
4418}} // namespace selftests::dw2_expand_symtabs_matching
4419
4420#endif /* GDB_SELF_TEST */
4421
4b514bc8
JK
4422/* If FILE_MATCHER is NULL or if PER_CU has
4423 dwarf2_per_cu_quick_data::MARK set (see
4424 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4425 EXPANSION_NOTIFY on it. */
4426
4427static void
4428dw2_expand_symtabs_matching_one
4429 (struct dwarf2_per_cu_data *per_cu,
4430 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4431 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4432{
4433 if (file_matcher == NULL || per_cu->v.quick->mark)
4434 {
4435 bool symtab_was_null
4436 = (per_cu->v.quick->compunit_symtab == NULL);
4437
58f0c718 4438 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4439
4440 if (expansion_notify != NULL
4441 && symtab_was_null
4442 && per_cu->v.quick->compunit_symtab != NULL)
4443 expansion_notify (per_cu->v.quick->compunit_symtab);
4444 }
4445}
4446
3f563c84
PA
4447/* Helper for dw2_expand_matching symtabs. Called on each symbol
4448 matched, to expand corresponding CUs that were marked. IDX is the
4449 index of the symbol name that matched. */
4450
4451static void
4452dw2_expand_marked_cus
ed2dc618 4453 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4454 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4455 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4456 search_domain kind)
4457{
3f563c84
PA
4458 offset_type *vec, vec_len, vec_idx;
4459 bool global_seen = false;
ed2dc618 4460 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4461
61920122 4462 vec = (offset_type *) (index.constant_pool
f00a2de2 4463 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4464 vec_len = MAYBE_SWAP (vec[0]);
4465 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4466 {
61920122
PA
4467 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4468 /* This value is only valid for index versions >= 7. */
4469 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4470 gdb_index_symbol_kind symbol_kind =
4471 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4472 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4473 /* Only check the symbol attributes if they're present.
4474 Indices prior to version 7 don't record them,
4475 and indices >= 7 may elide them for certain symbols
4476 (gold does this). */
4477 int attrs_valid =
4478 (index.version >= 7
4479 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4480
4481 /* Work around gold/15646. */
4482 if (attrs_valid)
9291a0cd 4483 {
61920122
PA
4484 if (!is_static && global_seen)
4485 continue;
4486 if (!is_static)
4487 global_seen = true;
4488 }
3190f0c6 4489
61920122
PA
4490 /* Only check the symbol's kind if it has one. */
4491 if (attrs_valid)
4492 {
4493 switch (kind)
8943b874 4494 {
61920122
PA
4495 case VARIABLES_DOMAIN:
4496 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4497 continue;
4498 break;
4499 case FUNCTIONS_DOMAIN:
4500 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4501 continue;
61920122
PA
4502 break;
4503 case TYPES_DOMAIN:
4504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4505 continue;
4506 break;
59c35742
AB
4507 case MODULES_DOMAIN:
4508 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4509 continue;
4510 break;
61920122
PA
4511 default:
4512 break;
8943b874 4513 }
61920122 4514 }
8943b874 4515
61920122 4516 /* Don't crash on bad data. */
b76e467d 4517 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4518 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4519 {
b98664d3 4520 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4521 " [in module %s]"),
4522 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4523 continue;
4524 }
4525
ff4c9fec 4526 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4527 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4528 expansion_notify);
61920122
PA
4529 }
4530}
4531
4b514bc8
JK
4532/* If FILE_MATCHER is non-NULL, set all the
4533 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4534 that match FILE_MATCHER. */
4535
61920122 4536static void
4b514bc8 4537dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4538 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4539 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4540{
4b514bc8 4541 if (file_matcher == NULL)
61920122
PA
4542 return;
4543
4b514bc8
JK
4544 objfile *const objfile = dwarf2_per_objfile->objfile;
4545
4546 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4547 htab_eq_pointer,
4548 NULL, xcalloc, xfree));
4549 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4550 htab_eq_pointer,
4551 NULL, xcalloc, xfree));
61920122 4552
4b514bc8
JK
4553 /* The rule is CUs specify all the files, including those used by
4554 any TU, so there's no need to scan TUs here. */
61920122 4555
b76e467d 4556 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4557 {
927aa2e7
JK
4558 QUIT;
4559
4560 per_cu->v.quick->mark = 0;
4561
4562 /* We only need to look at symtabs not already expanded. */
4563 if (per_cu->v.quick->compunit_symtab)
4564 continue;
4565
b76e467d 4566 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4567 if (file_data == NULL)
4568 continue;
4569
4570 if (htab_find (visited_not_found.get (), file_data) != NULL)
4571 continue;
4572 else if (htab_find (visited_found.get (), file_data) != NULL)
4573 {
4574 per_cu->v.quick->mark = 1;
4575 continue;
4576 }
4577
b76e467d 4578 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4579 {
4580 const char *this_real_name;
4581
4582 if (file_matcher (file_data->file_names[j], false))
4583 {
4584 per_cu->v.quick->mark = 1;
4585 break;
4586 }
4587
4588 /* Before we invoke realpath, which can get expensive when many
4589 files are involved, do a quick comparison of the basenames. */
4590 if (!basenames_may_differ
4591 && !file_matcher (lbasename (file_data->file_names[j]),
4592 true))
4593 continue;
4594
4595 this_real_name = dw2_get_real_path (objfile, file_data, j);
4596 if (file_matcher (this_real_name, false))
4597 {
4598 per_cu->v.quick->mark = 1;
4599 break;
4600 }
4601 }
4602
b76e467d
SM
4603 void **slot = htab_find_slot (per_cu->v.quick->mark
4604 ? visited_found.get ()
4605 : visited_not_found.get (),
4606 file_data, INSERT);
927aa2e7
JK
4607 *slot = file_data;
4608 }
4609}
4610
4611static void
4612dw2_expand_symtabs_matching
4613 (struct objfile *objfile,
4614 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4615 const lookup_name_info &lookup_name,
4616 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4617 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4618 enum search_domain kind)
4619{
ed2dc618
SM
4620 struct dwarf2_per_objfile *dwarf2_per_objfile
4621 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4622
4623 /* index_table is NULL if OBJF_READNOW. */
4624 if (!dwarf2_per_objfile->index_table)
4625 return;
4626
ed2dc618 4627 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4628
4629 mapped_index &index = *dwarf2_per_objfile->index_table;
4630
4631 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4632 symbol_matcher,
4633 kind, [&] (offset_type idx)
4634 {
ed2dc618 4635 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4636 expansion_notify, kind);
3b00ef10 4637 return true;
927aa2e7
JK
4638 });
4639}
4640
4641/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4642 symtab. */
4643
4644static struct compunit_symtab *
4645recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4646 CORE_ADDR pc)
4647{
4648 int i;
4649
4650 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4651 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4652 return cust;
4653
4654 if (cust->includes == NULL)
4655 return NULL;
4656
4657 for (i = 0; cust->includes[i]; ++i)
4658 {
4659 struct compunit_symtab *s = cust->includes[i];
4660
4661 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4662 if (s != NULL)
4663 return s;
4664 }
4665
4666 return NULL;
4667}
4668
4669static struct compunit_symtab *
4670dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4671 struct bound_minimal_symbol msymbol,
4672 CORE_ADDR pc,
4673 struct obj_section *section,
4674 int warn_if_readin)
4675{
4676 struct dwarf2_per_cu_data *data;
4677 struct compunit_symtab *result;
4678
d320c2b5 4679 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4680 return NULL;
4681
b3b3bada 4682 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4683 data = (struct dwarf2_per_cu_data *) addrmap_find
4684 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4685 if (!data)
4686 return NULL;
4687
4688 if (warn_if_readin && data->v.quick->compunit_symtab)
4689 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4690 paddress (get_objfile_arch (objfile), pc));
4691
4692 result
58f0c718
TT
4693 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4694 false),
927aa2e7
JK
4695 pc);
4696 gdb_assert (result != NULL);
4697 return result;
4698}
4699
4700static void
4701dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4702 void *data, int need_fullname)
4703{
ed2dc618
SM
4704 struct dwarf2_per_objfile *dwarf2_per_objfile
4705 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4706
4707 if (!dwarf2_per_objfile->filenames_cache)
4708 {
4709 dwarf2_per_objfile->filenames_cache.emplace ();
4710
4711 htab_up visited (htab_create_alloc (10,
4712 htab_hash_pointer, htab_eq_pointer,
4713 NULL, xcalloc, xfree));
4714
4715 /* The rule is CUs specify all the files, including those used
4716 by any TU, so there's no need to scan TUs here. We can
4717 ignore file names coming from already-expanded CUs. */
4718
b76e467d 4719 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4720 {
927aa2e7
JK
4721 if (per_cu->v.quick->compunit_symtab)
4722 {
4723 void **slot = htab_find_slot (visited.get (),
4724 per_cu->v.quick->file_names,
4725 INSERT);
4726
4727 *slot = per_cu->v.quick->file_names;
4728 }
4729 }
4730
b76e467d 4731 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4732 {
927aa2e7
JK
4733 /* We only need to look at symtabs not already expanded. */
4734 if (per_cu->v.quick->compunit_symtab)
4735 continue;
4736
b76e467d 4737 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4738 if (file_data == NULL)
4739 continue;
4740
b76e467d 4741 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4742 if (*slot)
4743 {
4744 /* Already visited. */
4745 continue;
4746 }
4747 *slot = file_data;
4748
4749 for (int j = 0; j < file_data->num_file_names; ++j)
4750 {
4751 const char *filename = file_data->file_names[j];
4752 dwarf2_per_objfile->filenames_cache->seen (filename);
4753 }
4754 }
4755 }
4756
4757 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4758 {
4759 gdb::unique_xmalloc_ptr<char> this_real_name;
4760
4761 if (need_fullname)
4762 this_real_name = gdb_realpath (filename);
4763 (*fun) (filename, this_real_name.get (), data);
4764 });
4765}
4766
4767static int
4768dw2_has_symbols (struct objfile *objfile)
4769{
4770 return 1;
4771}
4772
4773const struct quick_symbol_functions dwarf2_gdb_index_functions =
4774{
4775 dw2_has_symbols,
4776 dw2_find_last_source_symtab,
4777 dw2_forget_cached_source_info,
4778 dw2_map_symtabs_matching_filename,
4779 dw2_lookup_symbol,
4780 dw2_print_stats,
4781 dw2_dump,
927aa2e7
JK
4782 dw2_expand_symtabs_for_function,
4783 dw2_expand_all_symtabs,
4784 dw2_expand_symtabs_with_fullname,
4785 dw2_map_matching_symbols,
4786 dw2_expand_symtabs_matching,
4787 dw2_find_pc_sect_compunit_symtab,
4788 NULL,
4789 dw2_map_symbol_filenames
4790};
4791
4792/* DWARF-5 debug_names reader. */
4793
4794/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4795static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4796
4797/* A helper function that reads the .debug_names section in SECTION
4798 and fills in MAP. FILENAME is the name of the file containing the
4799 section; it is used for error reporting.
4800
4801 Returns true if all went well, false otherwise. */
4802
4803static bool
4804read_debug_names_from_section (struct objfile *objfile,
4805 const char *filename,
4806 struct dwarf2_section_info *section,
4807 mapped_debug_names &map)
4808{
96b79293 4809 if (section->empty ())
927aa2e7
JK
4810 return false;
4811
4812 /* Older elfutils strip versions could keep the section in the main
4813 executable while splitting it for the separate debug info file. */
96b79293 4814 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4815 return false;
4816
96b79293 4817 section->read (objfile);
927aa2e7
JK
4818
4819 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4820
4821 const gdb_byte *addr = section->buffer;
4822
96b79293 4823 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4824
4825 unsigned int bytes_read;
4826 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4827 addr += bytes_read;
4828
4829 map.dwarf5_is_dwarf64 = bytes_read != 4;
4830 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4831 if (bytes_read + length != section->size)
4832 {
4833 /* There may be multiple per-CU indices. */
4834 warning (_("Section .debug_names in %s length %s does not match "
4835 "section length %s, ignoring .debug_names."),
4836 filename, plongest (bytes_read + length),
4837 pulongest (section->size));
4838 return false;
4839 }
4840
4841 /* The version number. */
4842 uint16_t version = read_2_bytes (abfd, addr);
4843 addr += 2;
4844 if (version != 5)
4845 {
4846 warning (_("Section .debug_names in %s has unsupported version %d, "
4847 "ignoring .debug_names."),
4848 filename, version);
4849 return false;
4850 }
4851
4852 /* Padding. */
4853 uint16_t padding = read_2_bytes (abfd, addr);
4854 addr += 2;
4855 if (padding != 0)
4856 {
4857 warning (_("Section .debug_names in %s has unsupported padding %d, "
4858 "ignoring .debug_names."),
4859 filename, padding);
4860 return false;
4861 }
4862
4863 /* comp_unit_count - The number of CUs in the CU list. */
4864 map.cu_count = read_4_bytes (abfd, addr);
4865 addr += 4;
4866
4867 /* local_type_unit_count - The number of TUs in the local TU
4868 list. */
4869 map.tu_count = read_4_bytes (abfd, addr);
4870 addr += 4;
4871
4872 /* foreign_type_unit_count - The number of TUs in the foreign TU
4873 list. */
4874 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4875 addr += 4;
4876 if (foreign_tu_count != 0)
4877 {
4878 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4879 "ignoring .debug_names."),
4880 filename, static_cast<unsigned long> (foreign_tu_count));
4881 return false;
4882 }
4883
4884 /* bucket_count - The number of hash buckets in the hash lookup
4885 table. */
4886 map.bucket_count = read_4_bytes (abfd, addr);
4887 addr += 4;
4888
4889 /* name_count - The number of unique names in the index. */
4890 map.name_count = read_4_bytes (abfd, addr);
4891 addr += 4;
4892
4893 /* abbrev_table_size - The size in bytes of the abbreviations
4894 table. */
4895 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4896 addr += 4;
4897
4898 /* augmentation_string_size - The size in bytes of the augmentation
4899 string. This value is rounded up to a multiple of 4. */
4900 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4901 addr += 4;
4902 map.augmentation_is_gdb = ((augmentation_string_size
4903 == sizeof (dwarf5_augmentation))
4904 && memcmp (addr, dwarf5_augmentation,
4905 sizeof (dwarf5_augmentation)) == 0);
4906 augmentation_string_size += (-augmentation_string_size) & 3;
4907 addr += augmentation_string_size;
4908
4909 /* List of CUs */
4910 map.cu_table_reordered = addr;
4911 addr += map.cu_count * map.offset_size;
4912
4913 /* List of Local TUs */
4914 map.tu_table_reordered = addr;
4915 addr += map.tu_count * map.offset_size;
4916
4917 /* Hash Lookup Table */
4918 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4919 addr += map.bucket_count * 4;
4920 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4921 addr += map.name_count * 4;
4922
4923 /* Name Table */
4924 map.name_table_string_offs_reordered = addr;
4925 addr += map.name_count * map.offset_size;
4926 map.name_table_entry_offs_reordered = addr;
4927 addr += map.name_count * map.offset_size;
4928
4929 const gdb_byte *abbrev_table_start = addr;
4930 for (;;)
4931 {
927aa2e7
JK
4932 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4933 addr += bytes_read;
4934 if (index_num == 0)
4935 break;
4936
4937 const auto insertpair
4938 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4939 if (!insertpair.second)
4940 {
4941 warning (_("Section .debug_names in %s has duplicate index %s, "
4942 "ignoring .debug_names."),
4943 filename, pulongest (index_num));
4944 return false;
4945 }
4946 mapped_debug_names::index_val &indexval = insertpair.first->second;
4947 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4948 addr += bytes_read;
4949
4950 for (;;)
4951 {
4952 mapped_debug_names::index_val::attr attr;
4953 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4954 addr += bytes_read;
4955 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4956 addr += bytes_read;
4957 if (attr.form == DW_FORM_implicit_const)
4958 {
4959 attr.implicit_const = read_signed_leb128 (abfd, addr,
4960 &bytes_read);
4961 addr += bytes_read;
4962 }
4963 if (attr.dw_idx == 0 && attr.form == 0)
4964 break;
4965 indexval.attr_vec.push_back (std::move (attr));
4966 }
4967 }
4968 if (addr != abbrev_table_start + abbrev_table_size)
4969 {
4970 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4971 "of size %s vs. written as %u, ignoring .debug_names."),
4972 filename, plongest (addr - abbrev_table_start),
4973 abbrev_table_size);
927aa2e7
JK
4974 return false;
4975 }
4976 map.entry_pool = addr;
4977
4978 return true;
4979}
4980
4981/* A helper for create_cus_from_debug_names that handles the MAP's CU
4982 list. */
4983
4984static void
ed2dc618 4985create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4986 const mapped_debug_names &map,
4987 dwarf2_section_info &section,
b76e467d 4988 bool is_dwz)
927aa2e7
JK
4989{
4990 sect_offset sect_off_prev;
4991 for (uint32_t i = 0; i <= map.cu_count; ++i)
4992 {
4993 sect_offset sect_off_next;
4994 if (i < map.cu_count)
4995 {
4996 sect_off_next
4997 = (sect_offset) (extract_unsigned_integer
4998 (map.cu_table_reordered + i * map.offset_size,
4999 map.offset_size,
5000 map.dwarf5_byte_order));
5001 }
5002 else
5003 sect_off_next = (sect_offset) section.size;
5004 if (i >= 1)
5005 {
5006 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5007 dwarf2_per_cu_data *per_cu
ed2dc618 5008 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5009 sect_off_prev, length);
b76e467d 5010 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5011 }
5012 sect_off_prev = sect_off_next;
5013 }
5014}
5015
5016/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5017 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5018
5019static void
ed2dc618 5020create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5021 const mapped_debug_names &map,
5022 const mapped_debug_names &dwz_map)
5023{
b76e467d
SM
5024 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5025 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5026
ed2dc618
SM
5027 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5028 dwarf2_per_objfile->info,
b76e467d 5029 false /* is_dwz */);
927aa2e7
JK
5030
5031 if (dwz_map.cu_count == 0)
5032 return;
5033
ed2dc618
SM
5034 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5035 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5036 true /* is_dwz */);
927aa2e7
JK
5037}
5038
5039/* Read .debug_names. If everything went ok, initialize the "quick"
5040 elements of all the CUs and return true. Otherwise, return false. */
5041
5042static bool
ed2dc618 5043dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5044{
22ca247e
TT
5045 std::unique_ptr<mapped_debug_names> map
5046 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5047 mapped_debug_names dwz_map (dwarf2_per_objfile);
5048 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5049
5050 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5051 &dwarf2_per_objfile->debug_names,
22ca247e 5052 *map))
927aa2e7
JK
5053 return false;
5054
5055 /* Don't use the index if it's empty. */
22ca247e 5056 if (map->name_count == 0)
927aa2e7
JK
5057 return false;
5058
5059 /* If there is a .dwz file, read it so we can get its CU list as
5060 well. */
ed2dc618 5061 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5062 if (dwz != NULL)
5063 {
5064 if (!read_debug_names_from_section (objfile,
00f93c44 5065 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5066 &dwz->debug_names, dwz_map))
5067 {
5068 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5069 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5070 return false;
5071 }
5072 }
5073
22ca247e 5074 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5075
22ca247e 5076 if (map->tu_count != 0)
927aa2e7
JK
5077 {
5078 /* We can only handle a single .debug_types when we have an
5079 index. */
fd5866f6 5080 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5081 return false;
5082
fd5866f6 5083 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5084
5085 create_signatured_type_table_from_debug_names
22ca247e 5086 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5087 }
5088
ed2dc618
SM
5089 create_addrmap_from_aranges (dwarf2_per_objfile,
5090 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5091
22ca247e 5092 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5093 dwarf2_per_objfile->using_index = 1;
5094 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5095 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5096
5097 return true;
5098}
5099
927aa2e7
JK
5100/* Type used to manage iterating over all CUs looking for a symbol for
5101 .debug_names. */
5102
5103class dw2_debug_names_iterator
5104{
5105public:
927aa2e7 5106 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5107 gdb::optional<block_enum> block_index,
5108 domain_enum domain,
927aa2e7 5109 const char *name)
2b79f376 5110 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5111 m_addr (find_vec_in_debug_names (map, name))
5112 {}
5113
5114 dw2_debug_names_iterator (const mapped_debug_names &map,
5115 search_domain search, uint32_t namei)
5116 : m_map (map),
5117 m_search (search),
5118 m_addr (find_vec_in_debug_names (map, namei))
5119 {}
5120
3b00ef10
TT
5121 dw2_debug_names_iterator (const mapped_debug_names &map,
5122 block_enum block_index, domain_enum domain,
5123 uint32_t namei)
5124 : m_map (map), m_block_index (block_index), m_domain (domain),
5125 m_addr (find_vec_in_debug_names (map, namei))
5126 {}
5127
927aa2e7
JK
5128 /* Return the next matching CU or NULL if there are no more. */
5129 dwarf2_per_cu_data *next ();
5130
5131private:
5132 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5133 const char *name);
5134 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5135 uint32_t namei);
5136
5137 /* The internalized form of .debug_names. */
5138 const mapped_debug_names &m_map;
5139
2b79f376
SM
5140 /* If set, only look for symbols that match that block. Valid values are
5141 GLOBAL_BLOCK and STATIC_BLOCK. */
5142 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5143
5144 /* The kind of symbol we're looking for. */
5145 const domain_enum m_domain = UNDEF_DOMAIN;
5146 const search_domain m_search = ALL_DOMAIN;
5147
5148 /* The list of CUs from the index entry of the symbol, or NULL if
5149 not found. */
5150 const gdb_byte *m_addr;
5151};
5152
5153const char *
5154mapped_debug_names::namei_to_name (uint32_t namei) const
5155{
5156 const ULONGEST namei_string_offs
5157 = extract_unsigned_integer ((name_table_string_offs_reordered
5158 + namei * offset_size),
5159 offset_size,
5160 dwarf5_byte_order);
4f44ae6c
TT
5161 return read_indirect_string_at_offset (dwarf2_per_objfile,
5162 namei_string_offs);
927aa2e7
JK
5163}
5164
5165/* Find a slot in .debug_names for the object named NAME. If NAME is
5166 found, return pointer to its pool data. If NAME cannot be found,
5167 return NULL. */
5168
5169const gdb_byte *
5170dw2_debug_names_iterator::find_vec_in_debug_names
5171 (const mapped_debug_names &map, const char *name)
5172{
5173 int (*cmp) (const char *, const char *);
5174
54ee4252 5175 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5176 if (current_language->la_language == language_cplus
5177 || current_language->la_language == language_fortran
5178 || current_language->la_language == language_d)
5179 {
5180 /* NAME is already canonical. Drop any qualifiers as
5181 .debug_names does not contain any. */
5182
5183 if (strchr (name, '(') != NULL)
5184 {
54ee4252 5185 without_params = cp_remove_params (name);
927aa2e7 5186 if (without_params != NULL)
54ee4252 5187 name = without_params.get ();
927aa2e7
JK
5188 }
5189 }
5190
5191 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5192
5193 const uint32_t full_hash = dwarf5_djb_hash (name);
5194 uint32_t namei
5195 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5196 (map.bucket_table_reordered
5197 + (full_hash % map.bucket_count)), 4,
5198 map.dwarf5_byte_order);
5199 if (namei == 0)
5200 return NULL;
5201 --namei;
5202 if (namei >= map.name_count)
5203 {
b98664d3 5204 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5205 "[in module %s]"),
5206 namei, map.name_count,
ed2dc618 5207 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5208 return NULL;
5209 }
5210
5211 for (;;)
5212 {
5213 const uint32_t namei_full_hash
5214 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5215 (map.hash_table_reordered + namei), 4,
5216 map.dwarf5_byte_order);
5217 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5218 return NULL;
5219
5220 if (full_hash == namei_full_hash)
5221 {
5222 const char *const namei_string = map.namei_to_name (namei);
5223
5224#if 0 /* An expensive sanity check. */
5225 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5226 {
b98664d3 5227 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5228 "[in module %s]"),
5229 namei, objfile_name (dwarf2_per_objfile->objfile));
5230 return NULL;
5231 }
5232#endif
5233
5234 if (cmp (namei_string, name) == 0)
5235 {
5236 const ULONGEST namei_entry_offs
5237 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5238 + namei * map.offset_size),
5239 map.offset_size, map.dwarf5_byte_order);
5240 return map.entry_pool + namei_entry_offs;
5241 }
5242 }
5243
5244 ++namei;
5245 if (namei >= map.name_count)
5246 return NULL;
5247 }
5248}
5249
5250const gdb_byte *
5251dw2_debug_names_iterator::find_vec_in_debug_names
5252 (const mapped_debug_names &map, uint32_t namei)
5253{
5254 if (namei >= map.name_count)
5255 {
b98664d3 5256 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5257 "[in module %s]"),
5258 namei, map.name_count,
ed2dc618 5259 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5260 return NULL;
5261 }
5262
5263 const ULONGEST namei_entry_offs
5264 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5265 + namei * map.offset_size),
5266 map.offset_size, map.dwarf5_byte_order);
5267 return map.entry_pool + namei_entry_offs;
5268}
5269
5270/* See dw2_debug_names_iterator. */
5271
5272dwarf2_per_cu_data *
5273dw2_debug_names_iterator::next ()
5274{
5275 if (m_addr == NULL)
5276 return NULL;
5277
ed2dc618
SM
5278 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5279 struct objfile *objfile = dwarf2_per_objfile->objfile;
5280 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5281
5282 again:
5283
5284 unsigned int bytes_read;
5285 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5286 m_addr += bytes_read;
5287 if (abbrev == 0)
5288 return NULL;
5289
5290 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5291 if (indexval_it == m_map.abbrev_map.cend ())
5292 {
b98664d3 5293 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5294 "[in module %s]"),
ed2dc618 5295 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5296 return NULL;
5297 }
5298 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5299 enum class symbol_linkage {
5300 unknown,
5301 static_,
5302 extern_,
23c13d42 5303 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5304 dwarf2_per_cu_data *per_cu = NULL;
5305 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5306 {
5307 ULONGEST ull;
5308 switch (attr.form)
5309 {
5310 case DW_FORM_implicit_const:
5311 ull = attr.implicit_const;
5312 break;
5313 case DW_FORM_flag_present:
5314 ull = 1;
5315 break;
5316 case DW_FORM_udata:
5317 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5318 m_addr += bytes_read;
5319 break;
5320 default:
b98664d3 5321 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5322 dwarf_form_name (attr.form),
ed2dc618 5323 objfile_name (objfile));
927aa2e7
JK
5324 return NULL;
5325 }
5326 switch (attr.dw_idx)
5327 {
5328 case DW_IDX_compile_unit:
5329 /* Don't crash on bad data. */
b76e467d 5330 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5331 {
b98664d3 5332 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5333 " [in module %s]"),
5334 pulongest (ull),
5335 objfile_name (dwarf2_per_objfile->objfile));
5336 continue;
5337 }
ff4c9fec 5338 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5339 break;
8af5c486
JK
5340 case DW_IDX_type_unit:
5341 /* Don't crash on bad data. */
b2bdb8cf 5342 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5343 {
b98664d3 5344 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5345 " [in module %s]"),
5346 pulongest (ull),
5347 objfile_name (dwarf2_per_objfile->objfile));
5348 continue;
5349 }
ff4c9fec 5350 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5351 break;
927aa2e7
JK
5352 case DW_IDX_GNU_internal:
5353 if (!m_map.augmentation_is_gdb)
5354 break;
23c13d42 5355 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5356 break;
5357 case DW_IDX_GNU_external:
5358 if (!m_map.augmentation_is_gdb)
5359 break;
23c13d42 5360 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5361 break;
5362 }
5363 }
5364
5365 /* Skip if already read in. */
5366 if (per_cu->v.quick->compunit_symtab)
5367 goto again;
5368
5369 /* Check static vs global. */
23c13d42 5370 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5371 {
2b79f376 5372 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5373 const bool symbol_is_static =
5374 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5375 if (want_static != symbol_is_static)
2b79f376 5376 goto again;
927aa2e7
JK
5377 }
5378
5379 /* Match dw2_symtab_iter_next, symbol_kind
5380 and debug_names::psymbol_tag. */
5381 switch (m_domain)
5382 {
5383 case VAR_DOMAIN:
5384 switch (indexval.dwarf_tag)
5385 {
5386 case DW_TAG_variable:
5387 case DW_TAG_subprogram:
5388 /* Some types are also in VAR_DOMAIN. */
5389 case DW_TAG_typedef:
5390 case DW_TAG_structure_type:
5391 break;
5392 default:
5393 goto again;
5394 }
5395 break;
5396 case STRUCT_DOMAIN:
5397 switch (indexval.dwarf_tag)
5398 {
5399 case DW_TAG_typedef:
5400 case DW_TAG_structure_type:
5401 break;
5402 default:
5403 goto again;
5404 }
5405 break;
5406 case LABEL_DOMAIN:
5407 switch (indexval.dwarf_tag)
5408 {
5409 case 0:
5410 case DW_TAG_variable:
5411 break;
5412 default:
5413 goto again;
5414 }
5415 break;
59c35742
AB
5416 case MODULE_DOMAIN:
5417 switch (indexval.dwarf_tag)
5418 {
5419 case DW_TAG_module:
5420 break;
5421 default:
5422 goto again;
5423 }
5424 break;
927aa2e7
JK
5425 default:
5426 break;
5427 }
5428
5429 /* Match dw2_expand_symtabs_matching, symbol_kind and
5430 debug_names::psymbol_tag. */
5431 switch (m_search)
4b514bc8 5432 {
927aa2e7
JK
5433 case VARIABLES_DOMAIN:
5434 switch (indexval.dwarf_tag)
4b514bc8 5435 {
927aa2e7
JK
5436 case DW_TAG_variable:
5437 break;
5438 default:
5439 goto again;
4b514bc8 5440 }
927aa2e7
JK
5441 break;
5442 case FUNCTIONS_DOMAIN:
5443 switch (indexval.dwarf_tag)
4b514bc8 5444 {
927aa2e7
JK
5445 case DW_TAG_subprogram:
5446 break;
5447 default:
5448 goto again;
4b514bc8 5449 }
927aa2e7
JK
5450 break;
5451 case TYPES_DOMAIN:
5452 switch (indexval.dwarf_tag)
5453 {
5454 case DW_TAG_typedef:
5455 case DW_TAG_structure_type:
5456 break;
5457 default:
5458 goto again;
5459 }
5460 break;
59c35742
AB
5461 case MODULES_DOMAIN:
5462 switch (indexval.dwarf_tag)
5463 {
5464 case DW_TAG_module:
5465 break;
5466 default:
5467 goto again;
5468 }
927aa2e7
JK
5469 default:
5470 break;
4b514bc8 5471 }
927aa2e7
JK
5472
5473 return per_cu;
4b514bc8 5474}
61920122 5475
927aa2e7 5476static struct compunit_symtab *
c7f839cb 5477dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5478 const char *name, domain_enum domain)
4b514bc8 5479{
ed2dc618
SM
5480 struct dwarf2_per_objfile *dwarf2_per_objfile
5481 = get_dwarf2_per_objfile (objfile);
61920122 5482
927aa2e7
JK
5483 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5484 if (!mapp)
61920122 5485 {
927aa2e7
JK
5486 /* index is NULL if OBJF_READNOW. */
5487 return NULL;
5488 }
5489 const auto &map = *mapp;
9291a0cd 5490
2b79f376 5491 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5492
927aa2e7
JK
5493 struct compunit_symtab *stab_best = NULL;
5494 struct dwarf2_per_cu_data *per_cu;
5495 while ((per_cu = iter.next ()) != NULL)
5496 {
5497 struct symbol *sym, *with_opaque = NULL;
58f0c718 5498 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5499 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5500 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5501
927aa2e7
JK
5502 sym = block_find_symbol (block, name, domain,
5503 block_find_non_opaque_type_preferred,
5504 &with_opaque);
9703b513 5505
927aa2e7
JK
5506 /* Some caution must be observed with overloaded functions and
5507 methods, since the index will not contain any overload
5508 information (but NAME might contain it). */
a3ec0bb1 5509
927aa2e7 5510 if (sym != NULL
987012b8 5511 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5512 return stab;
5513 if (with_opaque != NULL
987012b8 5514 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5515 stab_best = stab;
9703b513 5516
927aa2e7 5517 /* Keep looking through other CUs. */
9703b513
TT
5518 }
5519
927aa2e7 5520 return stab_best;
9703b513
TT
5521}
5522
927aa2e7
JK
5523/* This dumps minimal information about .debug_names. It is called
5524 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5525 uses this to verify that .debug_names has been loaded. */
9291a0cd 5526
927aa2e7
JK
5527static void
5528dw2_debug_names_dump (struct objfile *objfile)
5529{
ed2dc618
SM
5530 struct dwarf2_per_objfile *dwarf2_per_objfile
5531 = get_dwarf2_per_objfile (objfile);
5532
927aa2e7
JK
5533 gdb_assert (dwarf2_per_objfile->using_index);
5534 printf_filtered (".debug_names:");
5535 if (dwarf2_per_objfile->debug_names_table)
5536 printf_filtered (" exists\n");
5537 else
5538 printf_filtered (" faked for \"readnow\"\n");
5539 printf_filtered ("\n");
9291a0cd
TT
5540}
5541
9291a0cd 5542static void
927aa2e7
JK
5543dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5544 const char *func_name)
9291a0cd 5545{
ed2dc618
SM
5546 struct dwarf2_per_objfile *dwarf2_per_objfile
5547 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5548
927aa2e7
JK
5549 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5550 if (dwarf2_per_objfile->debug_names_table)
24c79950 5551 {
927aa2e7 5552 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5553
2b79f376 5554 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5555
927aa2e7
JK
5556 struct dwarf2_per_cu_data *per_cu;
5557 while ((per_cu = iter.next ()) != NULL)
58f0c718 5558 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5559 }
5560}
24c79950 5561
3b00ef10
TT
5562static void
5563dw2_debug_names_map_matching_symbols
5564 (struct objfile *objfile,
5565 const lookup_name_info &name, domain_enum domain,
5566 int global,
5567 gdb::function_view<symbol_found_callback_ftype> callback,
5568 symbol_compare_ftype *ordered_compare)
5569{
5570 struct dwarf2_per_objfile *dwarf2_per_objfile
5571 = get_dwarf2_per_objfile (objfile);
5572
5573 /* debug_names_table is NULL if OBJF_READNOW. */
5574 if (!dwarf2_per_objfile->debug_names_table)
5575 return;
5576
5577 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5578 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5579
5580 const char *match_name = name.ada ().lookup_name ().c_str ();
5581 auto matcher = [&] (const char *symname)
5582 {
5583 if (ordered_compare == nullptr)
5584 return true;
5585 return ordered_compare (symname, match_name) == 0;
5586 };
5587
5588 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5589 [&] (offset_type namei)
5590 {
5591 /* The name was matched, now expand corresponding CUs that were
5592 marked. */
5593 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5594
5595 struct dwarf2_per_cu_data *per_cu;
5596 while ((per_cu = iter.next ()) != NULL)
5597 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5598 return true;
5599 });
5600
5601 /* It's a shame we couldn't do this inside the
5602 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5603 that have already been expanded. Instead, this loop matches what
5604 the psymtab code does. */
5605 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5606 {
5607 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5608 if (cust != nullptr)
5609 {
5610 const struct block *block
5611 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5612 if (!iterate_over_symbols_terminated (block, name,
5613 domain, callback))
5614 break;
5615 }
5616 }
5617}
5618
927aa2e7
JK
5619static void
5620dw2_debug_names_expand_symtabs_matching
5621 (struct objfile *objfile,
5622 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5623 const lookup_name_info &lookup_name,
5624 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5625 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5626 enum search_domain kind)
5627{
ed2dc618
SM
5628 struct dwarf2_per_objfile *dwarf2_per_objfile
5629 = get_dwarf2_per_objfile (objfile);
9291a0cd 5630
927aa2e7
JK
5631 /* debug_names_table is NULL if OBJF_READNOW. */
5632 if (!dwarf2_per_objfile->debug_names_table)
5633 return;
9291a0cd 5634
ed2dc618 5635 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5636
44ed8f3e 5637 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5638
44ed8f3e
PA
5639 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5640 symbol_matcher,
5641 kind, [&] (offset_type namei)
927aa2e7 5642 {
927aa2e7
JK
5643 /* The name was matched, now expand corresponding CUs that were
5644 marked. */
5645 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5646
927aa2e7
JK
5647 struct dwarf2_per_cu_data *per_cu;
5648 while ((per_cu = iter.next ()) != NULL)
5649 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5650 expansion_notify);
3b00ef10 5651 return true;
44ed8f3e 5652 });
9291a0cd
TT
5653}
5654
927aa2e7 5655const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5656{
5657 dw2_has_symbols,
5658 dw2_find_last_source_symtab,
5659 dw2_forget_cached_source_info,
f8eba3c6 5660 dw2_map_symtabs_matching_filename,
927aa2e7 5661 dw2_debug_names_lookup_symbol,
9291a0cd 5662 dw2_print_stats,
927aa2e7 5663 dw2_debug_names_dump,
927aa2e7 5664 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5665 dw2_expand_all_symtabs,
652a8996 5666 dw2_expand_symtabs_with_fullname,
3b00ef10 5667 dw2_debug_names_map_matching_symbols,
927aa2e7 5668 dw2_debug_names_expand_symtabs_matching,
43f3e411 5669 dw2_find_pc_sect_compunit_symtab,
71a3c369 5670 NULL,
9291a0cd
TT
5671 dw2_map_symbol_filenames
5672};
5673
4485a1c1
SM
5674/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5675 to either a dwarf2_per_objfile or dwz_file object. */
5676
5677template <typename T>
5678static gdb::array_view<const gdb_byte>
5679get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5680{
5681 dwarf2_section_info *section = &section_owner->gdb_index;
5682
96b79293 5683 if (section->empty ())
4485a1c1
SM
5684 return {};
5685
5686 /* Older elfutils strip versions could keep the section in the main
5687 executable while splitting it for the separate debug info file. */
96b79293 5688 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5689 return {};
5690
96b79293 5691 section->read (obj);
4485a1c1 5692
8bebfcda
PA
5693 /* dwarf2_section_info::size is a bfd_size_type, while
5694 gdb::array_view works with size_t. On 32-bit hosts, with
5695 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5696 is 32-bit. So we need an explicit narrowing conversion here.
5697 This is fine, because it's impossible to allocate or mmap an
5698 array/buffer larger than what size_t can represent. */
5699 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5700}
5701
87d6a7aa
SM
5702/* Lookup the index cache for the contents of the index associated to
5703 DWARF2_OBJ. */
5704
5705static gdb::array_view<const gdb_byte>
5706get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5707{
5708 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5709 if (build_id == nullptr)
5710 return {};
5711
5712 return global_index_cache.lookup_gdb_index (build_id,
5713 &dwarf2_obj->index_cache_res);
5714}
5715
5716/* Same as the above, but for DWZ. */
5717
5718static gdb::array_view<const gdb_byte>
5719get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5720{
5721 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5722 if (build_id == nullptr)
5723 return {};
5724
5725 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5726}
5727
3c0aa29a 5728/* See symfile.h. */
9291a0cd 5729
3c0aa29a
PA
5730bool
5731dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5732{
ed2dc618
SM
5733 struct dwarf2_per_objfile *dwarf2_per_objfile
5734 = get_dwarf2_per_objfile (objfile);
5735
9291a0cd
TT
5736 /* If we're about to read full symbols, don't bother with the
5737 indices. In this case we also don't care if some other debug
5738 format is making psymtabs, because they are all about to be
5739 expanded anyway. */
5740 if ((objfile->flags & OBJF_READNOW))
5741 {
9291a0cd 5742 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5743 create_all_comp_units (dwarf2_per_objfile);
5744 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5745 dwarf2_per_objfile->quick_file_names_table
5746 = create_quick_file_names_table
5747 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5748
b76e467d 5749 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5750 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5751 {
ff4c9fec 5752 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5753
e254ef6a
DE
5754 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5755 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5756 }
5757
5758 /* Return 1 so that gdb sees the "quick" functions. However,
5759 these functions will be no-ops because we will have expanded
5760 all symtabs. */
3c0aa29a
PA
5761 *index_kind = dw_index_kind::GDB_INDEX;
5762 return true;
9291a0cd
TT
5763 }
5764
ed2dc618 5765 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5766 {
5767 *index_kind = dw_index_kind::DEBUG_NAMES;
5768 return true;
5769 }
927aa2e7 5770
4485a1c1
SM
5771 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5772 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5773 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5774 {
5775 *index_kind = dw_index_kind::GDB_INDEX;
5776 return true;
5777 }
9291a0cd 5778
87d6a7aa
SM
5779 /* ... otherwise, try to find the index in the index cache. */
5780 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5781 get_gdb_index_contents_from_cache,
5782 get_gdb_index_contents_from_cache_dwz))
5783 {
5784 global_index_cache.hit ();
5785 *index_kind = dw_index_kind::GDB_INDEX;
5786 return true;
5787 }
5788
5789 global_index_cache.miss ();
3c0aa29a 5790 return false;
9291a0cd
TT
5791}
5792
5793\f
5794
dce234bc
PP
5795/* Build a partial symbol table. */
5796
5797void
f29dff0a 5798dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5799{
ed2dc618
SM
5800 struct dwarf2_per_objfile *dwarf2_per_objfile
5801 = get_dwarf2_per_objfile (objfile);
c9bf0622 5802
6eee24ce 5803 init_psymbol_list (objfile, 1024);
c906108c 5804
a70b8144 5805 try
c9bf0622
TT
5806 {
5807 /* This isn't really ideal: all the data we allocate on the
5808 objfile's obstack is still uselessly kept around. However,
5809 freeing it seems unsafe. */
906768f9 5810 psymtab_discarder psymtabs (objfile);
ed2dc618 5811 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5812 psymtabs.keep ();
87d6a7aa
SM
5813
5814 /* (maybe) store an index in the cache. */
5815 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5816 }
230d2906 5817 catch (const gdb_exception_error &except)
492d29ea
PA
5818 {
5819 exception_print (gdb_stderr, except);
5820 }
c906108c 5821}
c906108c 5822
3b80fe9b
DE
5823/* Find the base address of the compilation unit for range lists and
5824 location lists. It will normally be specified by DW_AT_low_pc.
5825 In DWARF-3 draft 4, the base address could be overridden by
5826 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5827 compilation units with discontinuous ranges. */
5828
5829static void
5830dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5831{
5832 struct attribute *attr;
5833
5834 cu->base_known = 0;
5835 cu->base_address = 0;
5836
5837 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5838 if (attr != nullptr)
3b80fe9b 5839 {
cd6c91b4 5840 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5841 cu->base_known = 1;
5842 }
5843 else
5844 {
5845 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5846 if (attr != nullptr)
3b80fe9b 5847 {
cd6c91b4 5848 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5849 cu->base_known = 1;
5850 }
5851 }
5852}
5853
36586728
TT
5854/* Helper function that returns the proper abbrev section for
5855 THIS_CU. */
5856
5857static struct dwarf2_section_info *
5858get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5859{
5860 struct dwarf2_section_info *abbrev;
ed2dc618 5861 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5862
5863 if (this_cu->is_dwz)
ed2dc618 5864 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5865 else
5866 abbrev = &dwarf2_per_objfile->abbrev;
5867
5868 return abbrev;
5869}
5870
f4dc4d17
DE
5871/* Fetch the abbreviation table offset from a comp or type unit header. */
5872
5873static sect_offset
ed2dc618
SM
5874read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5875 struct dwarf2_section_info *section,
9c541725 5876 sect_offset sect_off)
f4dc4d17 5877{
96b79293 5878 bfd *abfd = section->get_bfd_owner ();
d521ce57 5879 const gdb_byte *info_ptr;
ac298888 5880 unsigned int initial_length_size, offset_size;
43988095 5881 uint16_t version;
f4dc4d17 5882
96b79293 5883 section->read (dwarf2_per_objfile->objfile);
9c541725 5884 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5885 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5886 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5887 info_ptr += initial_length_size;
5888
5889 version = read_2_bytes (abfd, info_ptr);
5890 info_ptr += 2;
5891 if (version >= 5)
5892 {
5893 /* Skip unit type and address size. */
5894 info_ptr += 2;
5895 }
5896
24aa364d 5897 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5898}
5899
b83470bf
TT
5900/* A partial symtab that is used only for include files. */
5901struct dwarf2_include_psymtab : public partial_symtab
5902{
5903 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5904 : partial_symtab (filename, objfile)
5905 {
5906 }
5907
5908 void read_symtab (struct objfile *objfile) override
5909 {
5910 expand_psymtab (objfile);
5911 }
5912
5913 void expand_psymtab (struct objfile *objfile) override
5914 {
5915 if (m_readin)
5916 return;
5917 /* It's an include file, no symbols to read for it.
5918 Everything is in the parent symtab. */
5919 read_dependencies (objfile);
5920 m_readin = true;
5921 }
5922
5923 bool readin_p () const override
5924 {
5925 return m_readin;
5926 }
5927
5928 struct compunit_symtab *get_compunit_symtab () const override
5929 {
5930 return nullptr;
5931 }
5932
5933private:
5934
5935 bool m_readin = false;
5936};
5937
aaa75496
JB
5938/* Allocate a new partial symtab for file named NAME and mark this new
5939 partial symtab as being an include of PST. */
5940
5941static void
891813be 5942dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5943 struct objfile *objfile)
5944{
b83470bf 5945 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 5946
fbd9ab74
JK
5947 if (!IS_ABSOLUTE_PATH (subpst->filename))
5948 {
5949 /* It shares objfile->objfile_obstack. */
5950 subpst->dirname = pst->dirname;
5951 }
5952
a9342b62 5953 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5954 subpst->dependencies[0] = pst;
5955 subpst->number_of_dependencies = 1;
aaa75496
JB
5956}
5957
5958/* Read the Line Number Program data and extract the list of files
5959 included by the source file represented by PST. Build an include
d85a05f0 5960 partial symtab for each of these included files. */
aaa75496
JB
5961
5962static void
5963dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5964 struct die_info *die,
891813be 5965 dwarf2_psymtab *pst)
aaa75496 5966{
fff8551c 5967 line_header_up lh;
d85a05f0 5968 struct attribute *attr;
aaa75496 5969
d85a05f0 5970 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5971 if (attr != nullptr)
9c541725 5972 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5973 if (lh == NULL)
5974 return; /* No linetable, so no includes. */
5975
79748972
TT
5976 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5977 that we pass in the raw text_low here; that is ok because we're
5978 only decoding the line table to make include partial symtabs, and
5979 so the addresses aren't really used. */
4ae976d1 5980 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5981 pst->raw_text_low (), 1);
aaa75496
JB
5982}
5983
348e048f 5984static hashval_t
52dc124a 5985hash_signatured_type (const void *item)
348e048f 5986{
9a3c8263
SM
5987 const struct signatured_type *sig_type
5988 = (const struct signatured_type *) item;
9a619af0 5989
348e048f 5990 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5991 return sig_type->signature;
348e048f
DE
5992}
5993
5994static int
52dc124a 5995eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5996{
9a3c8263
SM
5997 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5998 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5999
348e048f
DE
6000 return lhs->signature == rhs->signature;
6001}
6002
1fd400ff
TT
6003/* Allocate a hash table for signatured types. */
6004
b0b6a987 6005static htab_up
298e9637 6006allocate_signatured_type_table ()
1fd400ff 6007{
b0b6a987
TT
6008 return htab_up (htab_create_alloc (41,
6009 hash_signatured_type,
6010 eq_signatured_type,
6011 NULL, xcalloc, xfree));
1fd400ff
TT
6012}
6013
d467dd73 6014/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6015
6016static int
d467dd73 6017add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6018{
9a3c8263 6019 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6020 std::vector<signatured_type *> *all_type_units
6021 = (std::vector<signatured_type *> *) datum;
1fd400ff 6022
b2bdb8cf 6023 all_type_units->push_back (sigt);
1fd400ff
TT
6024
6025 return 1;
6026}
6027
78d4d2c5 6028/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6029 and fill them into TYPES_HTAB. It will process only type units,
6030 therefore DW_UT_type. */
c88ee1f0 6031
78d4d2c5 6032static void
ed2dc618
SM
6033create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6034 struct dwo_file *dwo_file,
b0b6a987 6035 dwarf2_section_info *section, htab_up &types_htab,
43988095 6036 rcuh_kind section_kind)
348e048f 6037{
3019eac3 6038 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6039 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6040 bfd *abfd;
6041 const gdb_byte *info_ptr, *end_ptr;
348e048f 6042
4bdcc0c1
DE
6043 abbrev_section = (dwo_file != NULL
6044 ? &dwo_file->sections.abbrev
6045 : &dwarf2_per_objfile->abbrev);
6046
b4f54984 6047 if (dwarf_read_debug)
43988095 6048 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6049 section->get_name (),
6050 abbrev_section->get_file_name ());
09406207 6051
96b79293 6052 section->read (objfile);
78d4d2c5 6053 info_ptr = section->buffer;
348e048f 6054
78d4d2c5
JK
6055 if (info_ptr == NULL)
6056 return;
348e048f 6057
78d4d2c5
JK
6058 /* We can't set abfd until now because the section may be empty or
6059 not present, in which case the bfd is unknown. */
96b79293 6060 abfd = section->get_bfd_owner ();
348e048f 6061
c0ab21c2
TT
6062 /* We don't use cutu_reader here because we don't need to read
6063 any dies: the signature is in the header. */
3019eac3 6064
78d4d2c5
JK
6065 end_ptr = info_ptr + section->size;
6066 while (info_ptr < end_ptr)
6067 {
78d4d2c5
JK
6068 struct signatured_type *sig_type;
6069 struct dwo_unit *dwo_tu;
6070 void **slot;
6071 const gdb_byte *ptr = info_ptr;
6072 struct comp_unit_head header;
6073 unsigned int length;
8b70b953 6074
9c541725 6075 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6076
a49dd8dd
JK
6077 /* Initialize it due to a false compiler warning. */
6078 header.signature = -1;
9c541725 6079 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6080
78d4d2c5
JK
6081 /* We need to read the type's signature in order to build the hash
6082 table, but we don't need anything else just yet. */
348e048f 6083
ed2dc618 6084 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6085 abbrev_section, ptr, section_kind);
348e048f 6086
4057dfde 6087 length = header.get_length ();
6caca83c 6088
78d4d2c5
JK
6089 /* Skip dummy type units. */
6090 if (ptr >= info_ptr + length
43988095
JK
6091 || peek_abbrev_code (abfd, ptr) == 0
6092 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6093 {
6094 info_ptr += length;
6095 continue;
6096 }
dee91e82 6097
78d4d2c5
JK
6098 if (types_htab == NULL)
6099 {
6100 if (dwo_file)
298e9637 6101 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6102 else
298e9637 6103 types_htab = allocate_signatured_type_table ();
78d4d2c5 6104 }
8b70b953 6105
78d4d2c5
JK
6106 if (dwo_file)
6107 {
6108 sig_type = NULL;
6109 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6110 struct dwo_unit);
6111 dwo_tu->dwo_file = dwo_file;
43988095 6112 dwo_tu->signature = header.signature;
9c541725 6113 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6114 dwo_tu->section = section;
9c541725 6115 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6116 dwo_tu->length = length;
6117 }
6118 else
6119 {
6120 /* N.B.: type_offset is not usable if this type uses a DWO file.
6121 The real type_offset is in the DWO file. */
6122 dwo_tu = NULL;
6123 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6124 struct signatured_type);
43988095 6125 sig_type->signature = header.signature;
9c541725 6126 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6127 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6128 sig_type->per_cu.is_debug_types = 1;
6129 sig_type->per_cu.section = section;
9c541725 6130 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6131 sig_type->per_cu.length = length;
6132 }
6133
b0b6a987 6134 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6135 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6136 INSERT);
6137 gdb_assert (slot != NULL);
6138 if (*slot != NULL)
6139 {
9c541725 6140 sect_offset dup_sect_off;
0349ea22 6141
3019eac3
DE
6142 if (dwo_file)
6143 {
78d4d2c5
JK
6144 const struct dwo_unit *dup_tu
6145 = (const struct dwo_unit *) *slot;
6146
9c541725 6147 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6148 }
6149 else
6150 {
78d4d2c5
JK
6151 const struct signatured_type *dup_tu
6152 = (const struct signatured_type *) *slot;
6153
9c541725 6154 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6155 }
8b70b953 6156
b98664d3 6157 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6158 " the entry at offset %s, signature %s"),
6159 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6160 hex_string (header.signature));
78d4d2c5
JK
6161 }
6162 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6163
78d4d2c5 6164 if (dwarf_read_debug > 1)
9d8780f0
SM
6165 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6166 sect_offset_str (sect_off),
43988095 6167 hex_string (header.signature));
3019eac3 6168
78d4d2c5
JK
6169 info_ptr += length;
6170 }
6171}
3019eac3 6172
78d4d2c5
JK
6173/* Create the hash table of all entries in the .debug_types
6174 (or .debug_types.dwo) section(s).
6175 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6176 otherwise it is NULL.
b3c8eb43 6177
78d4d2c5 6178 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6179
78d4d2c5 6180 Note: This function processes DWO files only, not DWP files. */
348e048f 6181
78d4d2c5 6182static void
ed2dc618
SM
6183create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6184 struct dwo_file *dwo_file,
fd5866f6 6185 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6186 htab_up &types_htab)
78d4d2c5 6187{
fd5866f6
SM
6188 for (dwarf2_section_info &section : type_sections)
6189 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6190 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6191}
6192
6193/* Create the hash table of all entries in the .debug_types section,
6194 and initialize all_type_units.
6195 The result is zero if there is an error (e.g. missing .debug_types section),
6196 otherwise non-zero. */
6197
6198static int
ed2dc618 6199create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6200{
b0b6a987 6201 htab_up types_htab;
3019eac3 6202
ed2dc618
SM
6203 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6204 &dwarf2_per_objfile->info, types_htab,
43988095 6205 rcuh_kind::COMPILE);
ed2dc618
SM
6206 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6207 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6208 if (types_htab == NULL)
6209 {
6210 dwarf2_per_objfile->signatured_types = NULL;
6211 return 0;
6212 }
6213
b0b6a987 6214 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6215
b2bdb8cf 6216 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6217 dwarf2_per_objfile->all_type_units.reserve
6218 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6219
b0b6a987
TT
6220 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6221 add_signatured_type_cu_to_table,
b2bdb8cf 6222 &dwarf2_per_objfile->all_type_units);
1fd400ff 6223
348e048f
DE
6224 return 1;
6225}
6226
6aa5f3a6
DE
6227/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6228 If SLOT is non-NULL, it is the entry to use in the hash table.
6229 Otherwise we find one. */
6230
6231static struct signatured_type *
ed2dc618
SM
6232add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6233 void **slot)
6aa5f3a6
DE
6234{
6235 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6236
b2bdb8cf
SM
6237 if (dwarf2_per_objfile->all_type_units.size ()
6238 == dwarf2_per_objfile->all_type_units.capacity ())
6239 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6240
b2bdb8cf
SM
6241 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6242 struct signatured_type);
6243
6244 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6245 sig_type->signature = sig;
6246 sig_type->per_cu.is_debug_types = 1;
6247 if (dwarf2_per_objfile->using_index)
6248 {
6249 sig_type->per_cu.v.quick =
6250 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6251 struct dwarf2_per_cu_quick_data);
6252 }
6253
6254 if (slot == NULL)
6255 {
b0b6a987 6256 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6257 sig_type, INSERT);
6258 }
6259 gdb_assert (*slot == NULL);
6260 *slot = sig_type;
6261 /* The rest of sig_type must be filled in by the caller. */
6262 return sig_type;
6263}
6264
a2ce51a0
DE
6265/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6266 Fill in SIG_ENTRY with DWO_ENTRY. */
6267
6268static void
ed2dc618 6269fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6270 struct signatured_type *sig_entry,
6271 struct dwo_unit *dwo_entry)
6272{
7ee85ab1 6273 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6274 gdb_assert (! sig_entry->per_cu.queued);
6275 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6276 if (dwarf2_per_objfile->using_index)
6277 {
6278 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6279 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6280 }
6281 else
6282 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6283 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6284 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6285 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6286 gdb_assert (sig_entry->dwo_unit == NULL);
6287
6288 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6289 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6290 sig_entry->per_cu.length = dwo_entry->length;
6291 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6292 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6293 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6294 sig_entry->dwo_unit = dwo_entry;
6295}
6296
6297/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6298 If we haven't read the TU yet, create the signatured_type data structure
6299 for a TU to be read in directly from a DWO file, bypassing the stub.
6300 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6301 using .gdb_index, then when reading a CU we want to stay in the DWO file
6302 containing that CU. Otherwise we could end up reading several other DWO
6303 files (due to comdat folding) to process the transitive closure of all the
6304 mentioned TUs, and that can be slow. The current DWO file will have every
6305 type signature that it needs.
a2ce51a0
DE
6306 We only do this for .gdb_index because in the psymtab case we already have
6307 to read all the DWOs to build the type unit groups. */
6308
6309static struct signatured_type *
6310lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6311{
518817b3
SM
6312 struct dwarf2_per_objfile *dwarf2_per_objfile
6313 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6314 struct dwo_file *dwo_file;
6315 struct dwo_unit find_dwo_entry, *dwo_entry;
6316 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6317 void **slot;
a2ce51a0
DE
6318
6319 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6320
6aa5f3a6
DE
6321 /* If TU skeletons have been removed then we may not have read in any
6322 TUs yet. */
6323 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6324 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6325
6326 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6327 Use the global signatured_types array to do our own comdat-folding
6328 of types. If this is the first time we're reading this TU, and
6329 the TU has an entry in .gdb_index, replace the recorded data from
6330 .gdb_index with this TU. */
a2ce51a0 6331
a2ce51a0 6332 find_sig_entry.signature = sig;
b0b6a987 6333 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6334 &find_sig_entry, INSERT);
9a3c8263 6335 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6336
6337 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6338 read. Don't reassign the global entry to point to this DWO if that's
6339 the case. Also note that if the TU is already being read, it may not
6340 have come from a DWO, the program may be a mix of Fission-compiled
6341 code and non-Fission-compiled code. */
6342
6343 /* Have we already tried to read this TU?
6344 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6345 needn't exist in the global table yet). */
6346 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6347 return sig_entry;
6348
6aa5f3a6
DE
6349 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6350 dwo_unit of the TU itself. */
6351 dwo_file = cu->dwo_unit->dwo_file;
6352
a2ce51a0
DE
6353 /* Ok, this is the first time we're reading this TU. */
6354 if (dwo_file->tus == NULL)
6355 return NULL;
6356 find_dwo_entry.signature = sig;
b0b6a987
TT
6357 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6358 &find_dwo_entry);
a2ce51a0
DE
6359 if (dwo_entry == NULL)
6360 return NULL;
6361
6aa5f3a6
DE
6362 /* If the global table doesn't have an entry for this TU, add one. */
6363 if (sig_entry == NULL)
ed2dc618 6364 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6365
ed2dc618 6366 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6367 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6368 return sig_entry;
6369}
6370
a2ce51a0
DE
6371/* Subroutine of lookup_signatured_type.
6372 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6373 then try the DWP file. If the TU stub (skeleton) has been removed then
6374 it won't be in .gdb_index. */
a2ce51a0
DE
6375
6376static struct signatured_type *
6377lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6378{
518817b3
SM
6379 struct dwarf2_per_objfile *dwarf2_per_objfile
6380 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6381 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6382 struct dwo_unit *dwo_entry;
6383 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6384 void **slot;
a2ce51a0
DE
6385
6386 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6387 gdb_assert (dwp_file != NULL);
6388
6aa5f3a6
DE
6389 /* If TU skeletons have been removed then we may not have read in any
6390 TUs yet. */
6391 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6392 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6393
6aa5f3a6 6394 find_sig_entry.signature = sig;
b0b6a987 6395 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6396 &find_sig_entry, INSERT);
9a3c8263 6397 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6398
6399 /* Have we already tried to read this TU?
6400 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6401 needn't exist in the global table yet). */
6402 if (sig_entry != NULL)
6403 return sig_entry;
6404
a2ce51a0
DE
6405 if (dwp_file->tus == NULL)
6406 return NULL;
ed2dc618 6407 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6408 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6409 if (dwo_entry == NULL)
6410 return NULL;
6411
ed2dc618
SM
6412 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6413 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6414
a2ce51a0
DE
6415 return sig_entry;
6416}
6417
380bca97 6418/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6419 Returns NULL if signature SIG is not present in the table.
6420 It is up to the caller to complain about this. */
348e048f
DE
6421
6422static struct signatured_type *
a2ce51a0 6423lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6424{
518817b3
SM
6425 struct dwarf2_per_objfile *dwarf2_per_objfile
6426 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6427
a2ce51a0
DE
6428 if (cu->dwo_unit
6429 && dwarf2_per_objfile->using_index)
6430 {
6431 /* We're in a DWO/DWP file, and we're using .gdb_index.
6432 These cases require special processing. */
ed2dc618 6433 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6434 return lookup_dwo_signatured_type (cu, sig);
6435 else
6436 return lookup_dwp_signatured_type (cu, sig);
6437 }
6438 else
6439 {
6440 struct signatured_type find_entry, *entry;
348e048f 6441
a2ce51a0
DE
6442 if (dwarf2_per_objfile->signatured_types == NULL)
6443 return NULL;
6444 find_entry.signature = sig;
9a3c8263 6445 entry = ((struct signatured_type *)
b0b6a987
TT
6446 htab_find (dwarf2_per_objfile->signatured_types.get (),
6447 &find_entry));
a2ce51a0
DE
6448 return entry;
6449 }
348e048f 6450}
18a8505e
AT
6451
6452/* Return the address base of the compile unit, which, if exists, is stored
6453 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6454static gdb::optional<ULONGEST>
6455lookup_addr_base (struct die_info *comp_unit_die)
6456{
6457 struct attribute *attr;
6458 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6459 if (attr == nullptr)
6460 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6461 if (attr == nullptr)
6462 return gdb::optional<ULONGEST> ();
6463 return DW_UNSND (attr);
6464}
6465
6466/* Return range lists base of the compile unit, which, if exists, is stored
6467 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6468static ULONGEST
6469lookup_ranges_base (struct die_info *comp_unit_die)
6470{
6471 struct attribute *attr;
6472 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6473 if (attr == nullptr)
6474 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6475 if (attr == nullptr)
6476 return 0;
6477 return DW_UNSND (attr);
6478}
6479
42e7ad6c 6480/* Low level DIE reading support. */
348e048f 6481
d85a05f0
DJ
6482/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6483
6484static void
6485init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6486 struct dwarf2_cu *cu,
3019eac3 6487 struct dwarf2_section_info *section,
685af9cd
TT
6488 struct dwo_file *dwo_file,
6489 struct abbrev_table *abbrev_table)
d85a05f0 6490{
fceca515 6491 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6492 reader->abfd = section->get_bfd_owner ();
d85a05f0 6493 reader->cu = cu;
3019eac3 6494 reader->dwo_file = dwo_file;
dee91e82
DE
6495 reader->die_section = section;
6496 reader->buffer = section->buffer;
f664829e 6497 reader->buffer_end = section->buffer + section->size;
685af9cd 6498 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6499}
6500
c0ab21c2 6501/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6502 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6503 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6504 already.
6505
6506 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6507 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6508 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6509 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6510 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6511 STUB_COMP_DIR may be non-NULL.
3e225074 6512 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6513 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6514 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6515 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6516 kept around for at least as long as *RESULT_READER.
6517
b0c7bfa9
DE
6518 The result is non-zero if a valid (non-dummy) DIE was found. */
6519
6520static int
6521read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6522 struct dwo_unit *dwo_unit,
b0c7bfa9 6523 struct die_info *stub_comp_unit_die,
a2ce51a0 6524 const char *stub_comp_dir,
b0c7bfa9 6525 struct die_reader_specs *result_reader,
d521ce57 6526 const gdb_byte **result_info_ptr,
b0c7bfa9 6527 struct die_info **result_comp_unit_die,
685af9cd 6528 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6529{
ed2dc618 6530 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6531 struct objfile *objfile = dwarf2_per_objfile->objfile;
6532 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6533 bfd *abfd;
d521ce57 6534 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6535 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6536 int i,num_extra_attrs;
6537 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6538 struct die_info *comp_unit_die;
6539
b0aeadb3
DE
6540 /* At most one of these may be provided. */
6541 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6542
b0c7bfa9
DE
6543 /* These attributes aren't processed until later:
6544 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6545 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6546 referenced later. However, these attributes are found in the stub
6547 which we won't have later. In order to not impose this complication
6548 on the rest of the code, we read them here and copy them to the
6549 DWO CU/TU die. */
b0c7bfa9
DE
6550
6551 stmt_list = NULL;
6552 low_pc = NULL;
6553 high_pc = NULL;
6554 ranges = NULL;
6555 comp_dir = NULL;
6556
6557 if (stub_comp_unit_die != NULL)
6558 {
6559 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6560 DWO file. */
6561 if (! this_cu->is_debug_types)
6562 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6563 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6564 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6565 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6566 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6567
18a8505e 6568 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6569
18a8505e
AT
6570 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6571 here (if needed). We need the value before we can process
6572 DW_AT_ranges. */
6573 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6574 }
a2ce51a0
DE
6575 else if (stub_comp_dir != NULL)
6576 {
6577 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6578 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6579 comp_dir->name = DW_AT_comp_dir;
6580 comp_dir->form = DW_FORM_string;
6581 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6582 DW_STRING (comp_dir) = stub_comp_dir;
6583 }
b0c7bfa9
DE
6584
6585 /* Set up for reading the DWO CU/TU. */
6586 cu->dwo_unit = dwo_unit;
685af9cd 6587 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6588 section->read (objfile);
6589 abfd = section->get_bfd_owner ();
9c541725
PA
6590 begin_info_ptr = info_ptr = (section->buffer
6591 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6592 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6593
6594 if (this_cu->is_debug_types)
6595 {
b0c7bfa9
DE
6596 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6597
ed2dc618
SM
6598 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6599 &cu->header, section,
b0c7bfa9 6600 dwo_abbrev_section,
43988095 6601 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6602 /* This is not an assert because it can be caused by bad debug info. */
43988095 6603 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6604 {
6605 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6606 " TU at offset %s [in module %s]"),
a2ce51a0 6607 hex_string (sig_type->signature),
43988095 6608 hex_string (cu->header.signature),
9d8780f0 6609 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6610 bfd_get_filename (abfd));
6611 }
9c541725 6612 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6613 /* For DWOs coming from DWP files, we don't know the CU length
6614 nor the type's offset in the TU until now. */
4057dfde 6615 dwo_unit->length = cu->header.get_length ();
9c541725 6616 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6617
6618 /* Establish the type offset that can be used to lookup the type.
6619 For DWO files, we don't know it until now. */
9c541725
PA
6620 sig_type->type_offset_in_section
6621 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6622 }
6623 else
6624 {
ed2dc618
SM
6625 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6626 &cu->header, section,
b0c7bfa9 6627 dwo_abbrev_section,
43988095 6628 info_ptr, rcuh_kind::COMPILE);
9c541725 6629 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6630 /* For DWOs coming from DWP files, we don't know the CU length
6631 until now. */
4057dfde 6632 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6633 }
6634
685af9cd 6635 *result_dwo_abbrev_table
86de1d91
TT
6636 = abbrev_table::read (objfile, dwo_abbrev_section,
6637 cu->header.abbrev_sect_off);
685af9cd
TT
6638 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6639 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6640
6641 /* Read in the die, but leave space to copy over the attributes
6642 from the stub. This has the benefit of simplifying the rest of
6643 the code - all the work to maintain the illusion of a single
6644 DW_TAG_{compile,type}_unit DIE is done here. */
6645 num_extra_attrs = ((stmt_list != NULL)
6646 + (low_pc != NULL)
6647 + (high_pc != NULL)
6648 + (ranges != NULL)
6649 + (comp_dir != NULL));
6650 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6651 num_extra_attrs);
b0c7bfa9
DE
6652
6653 /* Copy over the attributes from the stub to the DIE we just read in. */
6654 comp_unit_die = *result_comp_unit_die;
6655 i = comp_unit_die->num_attrs;
6656 if (stmt_list != NULL)
6657 comp_unit_die->attrs[i++] = *stmt_list;
6658 if (low_pc != NULL)
6659 comp_unit_die->attrs[i++] = *low_pc;
6660 if (high_pc != NULL)
6661 comp_unit_die->attrs[i++] = *high_pc;
6662 if (ranges != NULL)
6663 comp_unit_die->attrs[i++] = *ranges;
6664 if (comp_dir != NULL)
6665 comp_unit_die->attrs[i++] = *comp_dir;
6666 comp_unit_die->num_attrs += num_extra_attrs;
6667
b4f54984 6668 if (dwarf_die_debug)
bf6af496
DE
6669 {
6670 fprintf_unfiltered (gdb_stdlog,
6671 "Read die from %s@0x%x of %s:\n",
96b79293 6672 section->get_name (),
bf6af496
DE
6673 (unsigned) (begin_info_ptr - section->buffer),
6674 bfd_get_filename (abfd));
b4f54984 6675 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6676 }
6677
b0c7bfa9
DE
6678 /* Skip dummy compilation units. */
6679 if (info_ptr >= begin_info_ptr + dwo_unit->length
6680 || peek_abbrev_code (abfd, info_ptr) == 0)
6681 return 0;
6682
6683 *result_info_ptr = info_ptr;
6684 return 1;
6685}
6686
a084a2a6
AT
6687/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6688 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6689 signature is part of the header. */
6690static gdb::optional<ULONGEST>
6691lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6692{
6693 if (cu->header.version >= 5)
6694 return cu->header.signature;
6695 struct attribute *attr;
6696 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6697 if (attr == nullptr)
6698 return gdb::optional<ULONGEST> ();
6699 return DW_UNSND (attr);
6700}
6701
c0ab21c2 6702/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6703 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6704 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6705
6706static struct dwo_unit *
6707lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6708 struct die_info *comp_unit_die,
6709 const char *dwo_name)
b0c7bfa9
DE
6710{
6711 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6712 struct dwo_unit *dwo_unit;
c0ab21c2 6713 const char *comp_dir;
b0c7bfa9 6714
a2ce51a0
DE
6715 gdb_assert (cu != NULL);
6716
b0c7bfa9 6717 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6718 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6719 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6720
6721 if (this_cu->is_debug_types)
6722 {
6723 struct signatured_type *sig_type;
6724
6725 /* Since this_cu is the first member of struct signatured_type,
6726 we can go from a pointer to one to a pointer to the other. */
6727 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6728 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6729 }
6730 else
6731 {
a084a2a6
AT
6732 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6733 if (!signature.has_value ())
b0c7bfa9
DE
6734 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6735 " [in module %s]"),
e3b94546 6736 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6737 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6738 *signature);
b0c7bfa9
DE
6739 }
6740
b0c7bfa9
DE
6741 return dwo_unit;
6742}
6743
c0ab21c2 6744/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6745 See it for a description of the parameters.
fcd3b13d 6746 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6747
c0ab21c2
TT
6748void
6749cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6750 int use_existing_cu)
a2ce51a0 6751{
a2ce51a0 6752 struct signatured_type *sig_type;
a2ce51a0
DE
6753
6754 /* Verify we can do the following downcast, and that we have the
6755 data we need. */
6756 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6757 sig_type = (struct signatured_type *) this_cu;
6758 gdb_assert (sig_type->dwo_unit != NULL);
6759
6aa5f3a6
DE
6760 if (use_existing_cu && this_cu->cu != NULL)
6761 {
6762 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6763 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6764 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6765 }
6766 else
6767 {
6768 /* If !use_existing_cu, this_cu->cu must be NULL. */
6769 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6770 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6771 }
6772
6773 /* A future optimization, if needed, would be to use an existing
6774 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6775 could share abbrev tables. */
a2ce51a0
DE
6776
6777 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6778 NULL /* stub_comp_unit_die */,
6779 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6780 this, &info_ptr,
3e225074 6781 &comp_unit_die,
c0ab21c2 6782 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6783 {
6784 /* Dummy die. */
c0ab21c2 6785 dummy_p = true;
a2ce51a0 6786 }
a2ce51a0
DE
6787}
6788
fd820528 6789/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6790 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6791
f4dc4d17
DE
6792 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6793 Otherwise the table specified in the comp unit header is read in and used.
6794 This is an optimization for when we already have the abbrev table.
6795
dee91e82 6796 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6797 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6798
c0ab21c2
TT
6799cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6800 struct abbrev_table *abbrev_table,
6751ebae 6801 int use_existing_cu,
c0ab21c2
TT
6802 bool skip_partial)
6803 : die_reader_specs {},
6751ebae 6804 m_this_cu (this_cu)
c906108c 6805{
ed2dc618 6806 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6807 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6808 struct dwarf2_section_info *section = this_cu->section;
96b79293 6809 bfd *abfd = section->get_bfd_owner ();
dee91e82 6810 struct dwarf2_cu *cu;
c0ab21c2 6811 const gdb_byte *begin_info_ptr;
dee91e82 6812 struct signatured_type *sig_type = NULL;
4bdcc0c1 6813 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6814 /* Non-zero if CU currently points to a DWO file and we need to
6815 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6816 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6817 int rereading_dwo_cu = 0;
c906108c 6818
b4f54984 6819 if (dwarf_die_debug)
9d8780f0 6820 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6821 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6822 sect_offset_str (this_cu->sect_off));
09406207 6823
a2ce51a0
DE
6824 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6825 file (instead of going through the stub), short-circuit all of this. */
6826 if (this_cu->reading_dwo_directly)
6827 {
6828 /* Narrow down the scope of possibilities to have to understand. */
6829 gdb_assert (this_cu->is_debug_types);
6830 gdb_assert (abbrev_table == NULL);
6751ebae 6831 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6832 return;
6833 }
6834
dee91e82 6835 /* This is cheap if the section is already read in. */
96b79293 6836 section->read (objfile);
dee91e82 6837
9c541725 6838 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6839
6840 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6841
6842 if (use_existing_cu && this_cu->cu != NULL)
6843 {
6844 cu = this_cu->cu;
42e7ad6c
DE
6845 /* If this CU is from a DWO file we need to start over, we need to
6846 refetch the attributes from the skeleton CU.
6847 This could be optimized by retrieving those attributes from when we
6848 were here the first time: the previous comp_unit_die was stored in
6849 comp_unit_obstack. But there's no data yet that we need this
6850 optimization. */
6851 if (cu->dwo_unit != NULL)
6852 rereading_dwo_cu = 1;
dee91e82
DE
6853 }
6854 else
6855 {
6856 /* If !use_existing_cu, this_cu->cu must be NULL. */
6857 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6858 m_new_cu.reset (new dwarf2_cu (this_cu));
6859 cu = m_new_cu.get ();
42e7ad6c 6860 }
dee91e82 6861
b0c7bfa9 6862 /* Get the header. */
9c541725 6863 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6864 {
6865 /* We already have the header, there's no need to read it in again. */
9c541725 6866 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6867 }
6868 else
6869 {
3019eac3 6870 if (this_cu->is_debug_types)
dee91e82 6871 {
ed2dc618
SM
6872 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6873 &cu->header, section,
4bdcc0c1 6874 abbrev_section, info_ptr,
43988095 6875 rcuh_kind::TYPE);
dee91e82 6876
42e7ad6c
DE
6877 /* Since per_cu is the first member of struct signatured_type,
6878 we can go from a pointer to one to a pointer to the other. */
6879 sig_type = (struct signatured_type *) this_cu;
43988095 6880 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6881 gdb_assert (sig_type->type_offset_in_tu
6882 == cu->header.type_cu_offset_in_tu);
6883 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6884
42e7ad6c
DE
6885 /* LENGTH has not been set yet for type units if we're
6886 using .gdb_index. */
4057dfde 6887 this_cu->length = cu->header.get_length ();
3019eac3
DE
6888
6889 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6890 sig_type->type_offset_in_section =
6891 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6892
6893 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6894 }
6895 else
6896 {
ed2dc618
SM
6897 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6898 &cu->header, section,
4bdcc0c1 6899 abbrev_section,
43988095
JK
6900 info_ptr,
6901 rcuh_kind::COMPILE);
dee91e82 6902
9c541725 6903 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6904 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6905 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6906 }
6907 }
10b3939b 6908
6caca83c 6909 /* Skip dummy compilation units. */
dee91e82 6910 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6911 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6912 {
6913 dummy_p = true;
6914 return;
6915 }
6caca83c 6916
433df2d4
DE
6917 /* If we don't have them yet, read the abbrevs for this compilation unit.
6918 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6919 done. */
f4dc4d17 6920 if (abbrev_table != NULL)
685af9cd
TT
6921 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6922 else
f4dc4d17 6923 {
c0ab21c2 6924 m_abbrev_table_holder
86de1d91
TT
6925 = abbrev_table::read (objfile, abbrev_section,
6926 cu->header.abbrev_sect_off);
c0ab21c2 6927 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6928 }
af703f96 6929
dee91e82 6930 /* Read the top level CU/TU die. */
c0ab21c2 6931 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6932 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6933
58f0c718 6934 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6935 {
6936 dummy_p = true;
6937 return;
6938 }
58f0c718 6939
b0c7bfa9 6940 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6941 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6942 table from the DWO file and pass the ownership over to us. It will be
6943 referenced from READER, so we must make sure to free it after we're done
6944 with READER.
6945
b0c7bfa9
DE
6946 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6947 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6948 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6949 if (dwo_name != nullptr)
3019eac3 6950 {
3019eac3 6951 struct dwo_unit *dwo_unit;
b0c7bfa9 6952 struct die_info *dwo_comp_unit_die;
3019eac3 6953
3e225074 6954 if (comp_unit_die->has_children)
6a506a2d 6955 {
b98664d3 6956 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6957 " has children (offset %s) [in module %s]"),
6958 sect_offset_str (this_cu->sect_off),
6959 bfd_get_filename (abfd));
6a506a2d 6960 }
c0ab21c2 6961 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6962 if (dwo_unit != NULL)
3019eac3 6963 {
6a506a2d 6964 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6965 comp_unit_die, NULL,
c0ab21c2 6966 this, &info_ptr,
3e225074 6967 &dwo_comp_unit_die,
c0ab21c2 6968 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6969 {
6970 /* Dummy die. */
c0ab21c2 6971 dummy_p = true;
6a506a2d
DE
6972 return;
6973 }
6974 comp_unit_die = dwo_comp_unit_die;
6975 }
6976 else
6977 {
6978 /* Yikes, we couldn't find the rest of the DIE, we only have
6979 the stub. A complaint has already been logged. There's
6980 not much more we can do except pass on the stub DIE to
6981 die_reader_func. We don't want to throw an error on bad
6982 debug info. */
3019eac3
DE
6983 }
6984 }
c0ab21c2 6985}
3019eac3 6986
6751ebae
TT
6987void
6988cutu_reader::keep ()
c0ab21c2 6989{
b0c7bfa9 6990 /* Done, clean up. */
6751ebae
TT
6991 gdb_assert (!dummy_p);
6992 if (m_new_cu != NULL)
348e048f 6993 {
c0ab21c2
TT
6994 struct dwarf2_per_objfile *dwarf2_per_objfile
6995 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 6996 /* Link this CU into read_in_chain. */
c0ab21c2
TT
6997 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6998 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 6999 /* The chain owns it now. */
c0ab21c2 7000 m_new_cu.release ();
348e048f 7001 }
dee91e82
DE
7002}
7003
18a8505e
AT
7004/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7005 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7006 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7007
7008 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7009 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7010
7011 We fill in THIS_CU->length.
7012
dee91e82 7013 THIS_CU->cu is always freed when done.
3019eac3 7014 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7015 to care whether it refers to the "main" CU or the DWO CU.
7016
7017 When parent_cu is passed, it is used to provide a default value for
7018 str_offsets_base and addr_base from the parent. */
dee91e82 7019
c0ab21c2
TT
7020cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7021 struct dwarf2_cu *parent_cu,
7022 struct dwo_file *dwo_file)
7023 : die_reader_specs {},
7024 m_this_cu (this_cu)
dee91e82 7025{
ed2dc618 7026 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7027 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7028 struct dwarf2_section_info *section = this_cu->section;
96b79293 7029 bfd *abfd = section->get_bfd_owner ();
33e80786 7030 struct dwarf2_section_info *abbrev_section;
d521ce57 7031 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7032
b4f54984 7033 if (dwarf_die_debug)
9d8780f0 7034 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7035 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7036 sect_offset_str (this_cu->sect_off));
09406207 7037
dee91e82
DE
7038 gdb_assert (this_cu->cu == NULL);
7039
33e80786
DE
7040 abbrev_section = (dwo_file != NULL
7041 ? &dwo_file->sections.abbrev
7042 : get_abbrev_section_for_cu (this_cu));
7043
dee91e82 7044 /* This is cheap if the section is already read in. */
96b79293 7045 section->read (objfile);
dee91e82 7046
c0ab21c2 7047 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7048
9c541725 7049 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7050 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7051 &m_new_cu->header, section,
4bdcc0c1 7052 abbrev_section, info_ptr,
43988095
JK
7053 (this_cu->is_debug_types
7054 ? rcuh_kind::TYPE
7055 : rcuh_kind::COMPILE));
dee91e82 7056
18a8505e
AT
7057 if (parent_cu != nullptr)
7058 {
c0ab21c2
TT
7059 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7060 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7061 }
4057dfde 7062 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7063
7064 /* Skip dummy compilation units. */
7065 if (info_ptr >= begin_info_ptr + this_cu->length
7066 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7067 {
7068 dummy_p = true;
7069 return;
7070 }
72bf9492 7071
c0ab21c2 7072 m_abbrev_table_holder
86de1d91
TT
7073 = abbrev_table::read (objfile, abbrev_section,
7074 m_new_cu->header.abbrev_sect_off);
dee91e82 7075
c0ab21c2
TT
7076 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7077 m_abbrev_table_holder.get ());
3e225074 7078 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7079}
7080
0018ea6f
DE
7081\f
7082/* Type Unit Groups.
dee91e82 7083
0018ea6f
DE
7084 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7085 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7086 so that all types coming from the same compilation (.o file) are grouped
7087 together. A future step could be to put the types in the same symtab as
7088 the CU the types ultimately came from. */
ff013f42 7089
f4dc4d17
DE
7090static hashval_t
7091hash_type_unit_group (const void *item)
7092{
9a3c8263
SM
7093 const struct type_unit_group *tu_group
7094 = (const struct type_unit_group *) item;
f4dc4d17 7095
094b34ac 7096 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7097}
348e048f
DE
7098
7099static int
f4dc4d17 7100eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7101{
9a3c8263
SM
7102 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7103 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7104
094b34ac 7105 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7106}
348e048f 7107
f4dc4d17
DE
7108/* Allocate a hash table for type unit groups. */
7109
eaa5fa8b 7110static htab_up
298e9637 7111allocate_type_unit_groups_table ()
f4dc4d17 7112{
eaa5fa8b
TT
7113 return htab_up (htab_create_alloc (3,
7114 hash_type_unit_group,
7115 eq_type_unit_group,
7116 NULL, xcalloc, xfree));
f4dc4d17 7117}
dee91e82 7118
f4dc4d17
DE
7119/* Type units that don't have DW_AT_stmt_list are grouped into their own
7120 partial symtabs. We combine several TUs per psymtab to not let the size
7121 of any one psymtab grow too big. */
7122#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7123#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7124
094b34ac 7125/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7126 Create the type_unit_group object used to hold one or more TUs. */
7127
7128static struct type_unit_group *
094b34ac 7129create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7130{
518817b3
SM
7131 struct dwarf2_per_objfile *dwarf2_per_objfile
7132 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7133 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7134 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7135 struct type_unit_group *tu_group;
f4dc4d17
DE
7136
7137 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7138 struct type_unit_group);
094b34ac 7139 per_cu = &tu_group->per_cu;
518817b3 7140 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7141
094b34ac
DE
7142 if (dwarf2_per_objfile->using_index)
7143 {
7144 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7145 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7146 }
7147 else
7148 {
9c541725 7149 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7150 dwarf2_psymtab *pst;
528e1572 7151 std::string name;
094b34ac
DE
7152
7153 /* Give the symtab a useful name for debug purposes. */
7154 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7155 name = string_printf ("<type_units_%d>",
7156 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7157 else
528e1572 7158 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7159
528e1572 7160 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7161 pst->anonymous = true;
094b34ac 7162 }
f4dc4d17 7163
094b34ac 7164 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7165 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7166
7167 return tu_group;
7168}
7169
094b34ac
DE
7170/* Look up the type_unit_group for type unit CU, and create it if necessary.
7171 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7172
7173static struct type_unit_group *
ff39bb5e 7174get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7175{
518817b3
SM
7176 struct dwarf2_per_objfile *dwarf2_per_objfile
7177 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7178 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7179 struct type_unit_group *tu_group;
7180 void **slot;
7181 unsigned int line_offset;
7182 struct type_unit_group type_unit_group_for_lookup;
7183
7184 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7185 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7186
7187 /* Do we need to create a new group, or can we use an existing one? */
7188
7189 if (stmt_list)
7190 {
7191 line_offset = DW_UNSND (stmt_list);
7192 ++tu_stats->nr_symtab_sharers;
7193 }
7194 else
7195 {
7196 /* Ugh, no stmt_list. Rare, but we have to handle it.
7197 We can do various things here like create one group per TU or
7198 spread them over multiple groups to split up the expansion work.
7199 To avoid worst case scenarios (too many groups or too large groups)
7200 we, umm, group them in bunches. */
7201 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7202 | (tu_stats->nr_stmt_less_type_units
7203 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7204 ++tu_stats->nr_stmt_less_type_units;
7205 }
7206
094b34ac 7207 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7208 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7209 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7210 &type_unit_group_for_lookup, INSERT);
7211 if (*slot != NULL)
7212 {
9a3c8263 7213 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7214 gdb_assert (tu_group != NULL);
7215 }
7216 else
7217 {
9c541725 7218 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7219 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7220 *slot = tu_group;
7221 ++tu_stats->nr_symtabs;
7222 }
7223
7224 return tu_group;
7225}
0018ea6f
DE
7226\f
7227/* Partial symbol tables. */
7228
7229/* Create a psymtab named NAME and assign it to PER_CU.
7230
7231 The caller must fill in the following details:
7232 dirname, textlow, texthigh. */
7233
891813be 7234static dwarf2_psymtab *
0018ea6f
DE
7235create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7236{
e3b94546 7237 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7238 dwarf2_psymtab *pst;
0018ea6f 7239
891813be 7240 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7241
6d94535f 7242 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7243
7244 /* This is the glue that links PST into GDB's symbol API. */
891813be 7245 pst->per_cu_data = per_cu;
0018ea6f
DE
7246 per_cu->v.psymtab = pst;
7247
7248 return pst;
7249}
7250
c0ab21c2 7251/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7252
7253static void
7254process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7255 const gdb_byte *info_ptr,
0018ea6f 7256 struct die_info *comp_unit_die,
c0ab21c2 7257 enum language pretend_language)
0018ea6f
DE
7258{
7259 struct dwarf2_cu *cu = reader->cu;
518817b3 7260 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7261 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7262 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7263 CORE_ADDR baseaddr;
7264 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7265 dwarf2_psymtab *pst;
3a2b436a 7266 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7267 const char *filename;
0018ea6f 7268
0018ea6f
DE
7269 gdb_assert (! per_cu->is_debug_types);
7270
c0ab21c2 7271 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7272
0018ea6f 7273 /* Allocate a new partial symbol table structure. */
2e927613
TV
7274 gdb::unique_xmalloc_ptr<char> debug_filename;
7275 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7276 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7277 if (filename == NULL)
0018ea6f 7278 filename = "";
2e927613
TV
7279 else if (strcmp (filename, artificial) == 0)
7280 {
7281 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7282 sect_offset_str (per_cu->sect_off),
7283 (char *) NULL));
2e927613
TV
7284 filename = debug_filename.get ();
7285 }
0018ea6f
DE
7286
7287 pst = create_partial_symtab (per_cu, filename);
7288
7289 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7290 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7291
b3b3bada 7292 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7293
7294 dwarf2_find_base_address (comp_unit_die, cu);
7295
7296 /* Possibly set the default values of LOWPC and HIGHPC from
7297 `DW_AT_ranges'. */
3a2b436a
JK
7298 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7299 &best_highpc, cu, pst);
7300 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7301 {
7302 CORE_ADDR low
7303 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7304 - baseaddr);
7305 CORE_ADDR high
7306 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7307 - baseaddr - 1);
7308 /* Store the contiguous range if it is not empty; it can be
7309 empty for CUs with no code. */
d320c2b5
TT
7310 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7311 low, high, pst);
79748972 7312 }
0018ea6f
DE
7313
7314 /* Check if comp unit has_children.
7315 If so, read the rest of the partial symbols from this comp unit.
7316 If not, there's no more debug_info for this comp unit. */
3e225074 7317 if (comp_unit_die->has_children)
0018ea6f
DE
7318 {
7319 struct partial_die_info *first_die;
7320 CORE_ADDR lowpc, highpc;
7321
7322 lowpc = ((CORE_ADDR) -1);
7323 highpc = ((CORE_ADDR) 0);
7324
7325 first_die = load_partial_dies (reader, info_ptr, 1);
7326
7327 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7328 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7329
7330 /* If we didn't find a lowpc, set it to highpc to avoid
7331 complaints from `maint check'. */
7332 if (lowpc == ((CORE_ADDR) -1))
7333 lowpc = highpc;
7334
7335 /* If the compilation unit didn't have an explicit address range,
7336 then use the information extracted from its child dies. */
e385593e 7337 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7338 {
7339 best_lowpc = lowpc;
7340 best_highpc = highpc;
7341 }
7342 }
4ae976d1 7343 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7344 best_lowpc + baseaddr)
7345 - baseaddr);
4ae976d1 7346 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7347 best_highpc + baseaddr)
7348 - baseaddr);
0018ea6f 7349
8763cede 7350 end_psymtab_common (objfile, pst);
0018ea6f 7351
ae640021 7352 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7353 {
7354 int i;
ae640021 7355 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7356
7357 /* Fill in 'dependencies' here; we fill in 'users' in a
7358 post-pass. */
7359 pst->number_of_dependencies = len;
a9342b62
TT
7360 pst->dependencies
7361 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7362 for (i = 0; i < len; ++i)
7363 {
7364 pst->dependencies[i]
7365 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7366 }
0018ea6f 7367
ae640021 7368 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7369 }
7370
7371 /* Get the list of files included in the current compilation unit,
7372 and build a psymtab for each of them. */
7373 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7374
b4f54984 7375 if (dwarf_read_debug)
b926417a
TT
7376 fprintf_unfiltered (gdb_stdlog,
7377 "Psymtab for %s unit @%s: %s - %s"
7378 ", %d global, %d static syms\n",
7379 per_cu->is_debug_types ? "type" : "comp",
7380 sect_offset_str (per_cu->sect_off),
7381 paddress (gdbarch, pst->text_low (objfile)),
7382 paddress (gdbarch, pst->text_high (objfile)),
7383 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7384}
7385
7386/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7387 Process compilation unit THIS_CU for a psymtab. */
7388
7389static void
7390process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7391 bool want_partial_unit,
b93601f3 7392 enum language pretend_language)
0018ea6f
DE
7393{
7394 /* If this compilation unit was already read in, free the
7395 cached copy in order to read it in again. This is
7396 necessary because we skipped some symbols when we first
7397 read in the compilation unit (see load_partial_dies).
7398 This problem could be avoided, but the benefit is unclear. */
7399 if (this_cu->cu != NULL)
7400 free_one_cached_comp_unit (this_cu);
7401
6751ebae 7402 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7403
58990295
TV
7404 switch (reader.comp_unit_die->tag)
7405 {
7406 case DW_TAG_compile_unit:
7407 this_cu->unit_type = DW_UT_compile;
7408 break;
7409 case DW_TAG_partial_unit:
7410 this_cu->unit_type = DW_UT_partial;
7411 break;
7412 default:
7413 abort ();
7414 }
7415
c0ab21c2 7416 if (reader.dummy_p)
f1902523 7417 {
c0ab21c2 7418 /* Nothing. */
f1902523 7419 }
c0ab21c2 7420 else if (this_cu->is_debug_types)
3e225074
TT
7421 build_type_psymtabs_reader (&reader, reader.info_ptr,
7422 reader.comp_unit_die);
135f5437
TT
7423 else if (want_partial_unit
7424 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7425 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7426 reader.comp_unit_die,
c0ab21c2 7427 pretend_language);
0018ea6f 7428
58990295
TV
7429 this_cu->lang = this_cu->cu->language;
7430
0018ea6f 7431 /* Age out any secondary CUs. */
ed2dc618 7432 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7433}
f4dc4d17
DE
7434
7435/* Reader function for build_type_psymtabs. */
7436
7437static void
7438build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7439 const gdb_byte *info_ptr,
3e225074 7440 struct die_info *type_unit_die)
f4dc4d17 7441{
ed2dc618 7442 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7443 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7444 struct objfile *objfile = dwarf2_per_objfile->objfile;
7445 struct dwarf2_cu *cu = reader->cu;
7446 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7447 struct signatured_type *sig_type;
f4dc4d17
DE
7448 struct type_unit_group *tu_group;
7449 struct attribute *attr;
7450 struct partial_die_info *first_die;
7451 CORE_ADDR lowpc, highpc;
891813be 7452 dwarf2_psymtab *pst;
f4dc4d17 7453
0186c6a7
DE
7454 gdb_assert (per_cu->is_debug_types);
7455 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7456
3e225074 7457 if (! type_unit_die->has_children)
f4dc4d17
DE
7458 return;
7459
7460 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7461 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7462
df07e2c7 7463 if (tu_group->tus == nullptr)
a8b3b8e9 7464 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7465 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7466
7467 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7468 pst = create_partial_symtab (per_cu, "");
6d94535f 7469 pst->anonymous = true;
f4dc4d17
DE
7470
7471 first_die = load_partial_dies (reader, info_ptr, 1);
7472
7473 lowpc = (CORE_ADDR) -1;
7474 highpc = (CORE_ADDR) 0;
7475 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7476
8763cede 7477 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7478}
7479
73051182
DE
7480/* Struct used to sort TUs by their abbreviation table offset. */
7481
7482struct tu_abbrev_offset
7483{
b2bdb8cf
SM
7484 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7485 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7486 {}
7487
7488 signatured_type *sig_type;
73051182
DE
7489 sect_offset abbrev_offset;
7490};
7491
484cf504 7492/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7493
484cf504
TT
7494static bool
7495sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7496 const struct tu_abbrev_offset &b)
73051182 7497{
484cf504 7498 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7499}
7500
7501/* Efficiently read all the type units.
7502 This does the bulk of the work for build_type_psymtabs.
7503
7504 The efficiency is because we sort TUs by the abbrev table they use and
7505 only read each abbrev table once. In one program there are 200K TUs
7506 sharing 8K abbrev tables.
7507
7508 The main purpose of this function is to support building the
7509 dwarf2_per_objfile->type_unit_groups table.
7510 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7511 can collapse the search space by grouping them by stmt_list.
7512 The savings can be significant, in the same program from above the 200K TUs
7513 share 8K stmt_list tables.
7514
7515 FUNC is expected to call get_type_unit_group, which will create the
7516 struct type_unit_group if necessary and add it to
7517 dwarf2_per_objfile->type_unit_groups. */
7518
7519static void
ed2dc618 7520build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7521{
73051182 7522 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7523 abbrev_table_up abbrev_table;
73051182 7524 sect_offset abbrev_offset;
73051182
DE
7525
7526 /* It's up to the caller to not call us multiple times. */
7527 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7528
b2bdb8cf 7529 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7530 return;
7531
7532 /* TUs typically share abbrev tables, and there can be way more TUs than
7533 abbrev tables. Sort by abbrev table to reduce the number of times we
7534 read each abbrev table in.
7535 Alternatives are to punt or to maintain a cache of abbrev tables.
7536 This is simpler and efficient enough for now.
7537
7538 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7539 symtab to use). Typically TUs with the same abbrev offset have the same
7540 stmt_list value too so in practice this should work well.
7541
7542 The basic algorithm here is:
7543
7544 sort TUs by abbrev table
7545 for each TU with same abbrev table:
7546 read abbrev table if first user
7547 read TU top level DIE
7548 [IWBN if DWO skeletons had DW_AT_stmt_list]
7549 call FUNC */
7550
b4f54984 7551 if (dwarf_read_debug)
73051182
DE
7552 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7553
7554 /* Sort in a separate table to maintain the order of all_type_units
7555 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7556 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7557 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7558
7559 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7560 sorted_by_abbrev.emplace_back
7561 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7562 sig_type->per_cu.section,
7563 sig_type->per_cu.sect_off));
73051182 7564
484cf504
TT
7565 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7566 sort_tu_by_abbrev_offset);
73051182 7567
9c541725 7568 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7569
b2bdb8cf 7570 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7571 {
73051182
DE
7572 /* Switch to the next abbrev table if necessary. */
7573 if (abbrev_table == NULL
b2bdb8cf 7574 || tu.abbrev_offset != abbrev_offset)
73051182 7575 {
b2bdb8cf 7576 abbrev_offset = tu.abbrev_offset;
73051182 7577 abbrev_table =
86de1d91
TT
7578 abbrev_table::read (dwarf2_per_objfile->objfile,
7579 &dwarf2_per_objfile->abbrev,
7580 abbrev_offset);
73051182
DE
7581 ++tu_stats->nr_uniq_abbrev_tables;
7582 }
7583
c0ab21c2 7584 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7585 0, false);
c0ab21c2
TT
7586 if (!reader.dummy_p)
7587 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7588 reader.comp_unit_die);
73051182 7589 }
6aa5f3a6 7590}
73051182 7591
6aa5f3a6
DE
7592/* Print collected type unit statistics. */
7593
7594static void
ed2dc618 7595print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7596{
7597 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7598
7599 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7600 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7601 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7602 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7603 tu_stats->nr_uniq_abbrev_tables);
7604 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7605 tu_stats->nr_symtabs);
7606 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7607 tu_stats->nr_symtab_sharers);
7608 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7609 tu_stats->nr_stmt_less_type_units);
7610 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7611 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7612}
7613
f4dc4d17
DE
7614/* Traversal function for build_type_psymtabs. */
7615
7616static int
7617build_type_psymtab_dependencies (void **slot, void *info)
7618{
ed2dc618
SM
7619 struct dwarf2_per_objfile *dwarf2_per_objfile
7620 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7621 struct objfile *objfile = dwarf2_per_objfile->objfile;
7622 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7623 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7624 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7625 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7626 int i;
7627
7628 gdb_assert (len > 0);
197400e8 7629 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7630
7631 pst->number_of_dependencies = len;
a9342b62 7632 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7633 for (i = 0; i < len; ++i)
f4dc4d17 7634 {
df07e2c7 7635 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7636 gdb_assert (iter->per_cu.is_debug_types);
7637 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7638 iter->type_unit_group = tu_group;
f4dc4d17
DE
7639 }
7640
df07e2c7
AB
7641 delete tu_group->tus;
7642 tu_group->tus = nullptr;
348e048f
DE
7643
7644 return 1;
7645}
7646
7647/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7648 Build partial symbol tables for the .debug_types comp-units. */
7649
7650static void
ed2dc618 7651build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7652{
ed2dc618 7653 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7654 return;
7655
ed2dc618 7656 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7657}
f4dc4d17 7658
6aa5f3a6
DE
7659/* Traversal function for process_skeletonless_type_unit.
7660 Read a TU in a DWO file and build partial symbols for it. */
7661
7662static int
7663process_skeletonless_type_unit (void **slot, void *info)
7664{
7665 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7666 struct dwarf2_per_objfile *dwarf2_per_objfile
7667 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7668 struct signatured_type find_entry, *entry;
7669
7670 /* If this TU doesn't exist in the global table, add it and read it in. */
7671
7672 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7673 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7674
7675 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7676 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7677 &find_entry, INSERT);
6aa5f3a6
DE
7678 /* If we've already seen this type there's nothing to do. What's happening
7679 is we're doing our own version of comdat-folding here. */
7680 if (*slot != NULL)
7681 return 1;
7682
7683 /* This does the job that create_all_type_units would have done for
7684 this TU. */
ed2dc618
SM
7685 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7686 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7687 *slot = entry;
7688
7689 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7690 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7691 if (!reader.dummy_p)
7692 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7693 reader.comp_unit_die);
6aa5f3a6
DE
7694
7695 return 1;
7696}
7697
7698/* Traversal function for process_skeletonless_type_units. */
7699
7700static int
7701process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7702{
7703 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7704
7705 if (dwo_file->tus != NULL)
b0b6a987
TT
7706 htab_traverse_noresize (dwo_file->tus.get (),
7707 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7708
7709 return 1;
7710}
7711
7712/* Scan all TUs of DWO files, verifying we've processed them.
7713 This is needed in case a TU was emitted without its skeleton.
7714 Note: This can't be done until we know what all the DWO files are. */
7715
7716static void
ed2dc618 7717process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7718{
7719 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7720 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7721 && dwarf2_per_objfile->dwo_files != NULL)
7722 {
51ac9db5 7723 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7724 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7725 dwarf2_per_objfile);
6aa5f3a6 7726 }
348e048f
DE
7727}
7728
ed2dc618 7729/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7730
7731static void
ed2dc618 7732set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7733{
b76e467d 7734 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7735 {
891813be 7736 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7737
36586728
TT
7738 if (pst == NULL)
7739 continue;
7740
b76e467d 7741 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7742 {
7743 /* Set the 'user' field only if it is not already set. */
7744 if (pst->dependencies[j]->user == NULL)
7745 pst->dependencies[j]->user = pst;
7746 }
7747 }
7748}
7749
93311388
DE
7750/* Build the partial symbol table by doing a quick pass through the
7751 .debug_info and .debug_abbrev sections. */
72bf9492 7752
93311388 7753static void
ed2dc618 7754dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7755{
ed2dc618 7756 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7757
b4f54984 7758 if (dwarf_read_debug)
45cfd468
DE
7759 {
7760 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7761 objfile_name (objfile));
45cfd468
DE
7762 }
7763
76935768
TT
7764 scoped_restore restore_reading_psyms
7765 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7766 true);
98bfdba5 7767
96b79293 7768 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7769
93311388
DE
7770 /* Any cached compilation units will be linked by the per-objfile
7771 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7772 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7773
ed2dc618 7774 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7775
ed2dc618 7776 create_all_comp_units (dwarf2_per_objfile);
c906108c 7777
60606b2c
TT
7778 /* Create a temporary address map on a temporary obstack. We later
7779 copy this to the final obstack. */
8268c778 7780 auto_obstack temp_obstack;
791afaa2
TT
7781
7782 scoped_restore save_psymtabs_addrmap
d320c2b5 7783 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7784 addrmap_create_mutable (&temp_obstack));
72bf9492 7785
b76e467d 7786 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7787 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7788
6aa5f3a6 7789 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7790 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7791
7792 /* Now that all TUs have been processed we can fill in the dependencies. */
7793 if (dwarf2_per_objfile->type_unit_groups != NULL)
7794 {
eaa5fa8b 7795 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7796 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7797 }
7798
b4f54984 7799 if (dwarf_read_debug)
ed2dc618 7800 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7801
ed2dc618 7802 set_partial_user (dwarf2_per_objfile);
95554aad 7803
d320c2b5
TT
7804 objfile->partial_symtabs->psymtabs_addrmap
7805 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7806 objfile->partial_symtabs->obstack ());
791afaa2
TT
7807 /* At this point we want to keep the address map. */
7808 save_psymtabs_addrmap.release ();
ff013f42 7809
b4f54984 7810 if (dwarf_read_debug)
45cfd468 7811 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7812 objfile_name (objfile));
ae038cb0
DJ
7813}
7814
dee91e82
DE
7815/* Load the partial DIEs for a secondary CU into memory.
7816 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7817
dee91e82
DE
7818static void
7819load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7820{
6751ebae 7821 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7822
7823 if (!reader.dummy_p)
7824 {
7825 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7826 language_minimal);
7827
7828 /* Check if comp unit has_children.
7829 If so, read the rest of the partial symbols from this comp unit.
7830 If not, there's no more debug_info for this comp unit. */
3e225074 7831 if (reader.comp_unit_die->has_children)
c0ab21c2 7832 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7833
7834 reader.keep ();
c0ab21c2 7835 }
ae038cb0
DJ
7836}
7837
ae038cb0 7838static void
ed2dc618 7839read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7840 struct dwarf2_section_info *section,
f1902523 7841 struct dwarf2_section_info *abbrev_section,
b76e467d 7842 unsigned int is_dwz)
ae038cb0 7843{
d521ce57 7844 const gdb_byte *info_ptr;
ed2dc618 7845 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7846
b4f54984 7847 if (dwarf_read_debug)
bf6af496 7848 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7849 section->get_name (),
7850 section->get_file_name ());
bf6af496 7851
96b79293 7852 section->read (objfile);
ae038cb0 7853
36586728 7854 info_ptr = section->buffer;
6e70227d 7855
36586728 7856 while (info_ptr < section->buffer + section->size)
ae038cb0 7857 {
ae038cb0 7858 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7859
9c541725 7860 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7861
f1902523 7862 comp_unit_head cu_header;
ed2dc618
SM
7863 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7864 abbrev_section, info_ptr,
7865 rcuh_kind::COMPILE);
ae038cb0
DJ
7866
7867 /* Save the compilation unit for later lookup. */
f1902523
JK
7868 if (cu_header.unit_type != DW_UT_type)
7869 {
7870 this_cu = XOBNEW (&objfile->objfile_obstack,
7871 struct dwarf2_per_cu_data);
7872 memset (this_cu, 0, sizeof (*this_cu));
7873 }
7874 else
7875 {
7876 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7877 struct signatured_type);
7878 memset (sig_type, 0, sizeof (*sig_type));
7879 sig_type->signature = cu_header.signature;
7880 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7881 this_cu = &sig_type->per_cu;
7882 }
7883 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7884 this_cu->sect_off = sect_off;
f1902523 7885 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7886 this_cu->is_dwz = is_dwz;
e3b94546 7887 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7888 this_cu->section = section;
ae038cb0 7889
b76e467d 7890 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7891
7892 info_ptr = info_ptr + this_cu->length;
7893 }
36586728
TT
7894}
7895
7896/* Create a list of all compilation units in OBJFILE.
7897 This is only done for -readnow and building partial symtabs. */
7898
7899static void
ed2dc618 7900create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7901{
b76e467d 7902 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7903 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7904 &dwarf2_per_objfile->abbrev, 0);
36586728 7905
b76e467d 7906 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7907 if (dwz != NULL)
ed2dc618 7908 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7909 1);
c906108c
SS
7910}
7911
5734ee8b 7912/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7913 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7914 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7915 DW_AT_ranges). See the comments of add_partial_subprogram on how
7916 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7917
72bf9492
DJ
7918static void
7919scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7920 CORE_ADDR *highpc, int set_addrmap,
7921 struct dwarf2_cu *cu)
c906108c 7922{
72bf9492 7923 struct partial_die_info *pdi;
c906108c 7924
91c24f0a
DC
7925 /* Now, march along the PDI's, descending into ones which have
7926 interesting children but skipping the children of the other ones,
7927 until we reach the end of the compilation unit. */
c906108c 7928
72bf9492 7929 pdi = first_die;
91c24f0a 7930
72bf9492
DJ
7931 while (pdi != NULL)
7932 {
52356b79 7933 pdi->fixup (cu);
c906108c 7934
f55ee35c 7935 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7936 children, so we need to look at them. Ditto for anonymous
7937 enums. */
933c6fe4 7938
72bf9492 7939 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7940 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7941 || pdi->tag == DW_TAG_imported_unit
7942 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7943 {
72bf9492 7944 switch (pdi->tag)
c906108c
SS
7945 {
7946 case DW_TAG_subprogram:
b1dc1806 7947 case DW_TAG_inlined_subroutine:
cdc07690 7948 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7949 break;
72929c62 7950 case DW_TAG_constant:
c906108c
SS
7951 case DW_TAG_variable:
7952 case DW_TAG_typedef:
91c24f0a 7953 case DW_TAG_union_type:
72bf9492 7954 if (!pdi->is_declaration)
63d06c5c 7955 {
72bf9492 7956 add_partial_symbol (pdi, cu);
63d06c5c
DC
7957 }
7958 break;
c906108c 7959 case DW_TAG_class_type:
680b30c7 7960 case DW_TAG_interface_type:
c906108c 7961 case DW_TAG_structure_type:
72bf9492 7962 if (!pdi->is_declaration)
c906108c 7963 {
72bf9492 7964 add_partial_symbol (pdi, cu);
c906108c 7965 }
b7fee5a3
KS
7966 if ((cu->language == language_rust
7967 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7968 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7969 set_addrmap, cu);
c906108c 7970 break;
91c24f0a 7971 case DW_TAG_enumeration_type:
72bf9492
DJ
7972 if (!pdi->is_declaration)
7973 add_partial_enumeration (pdi, cu);
c906108c
SS
7974 break;
7975 case DW_TAG_base_type:
a02abb62 7976 case DW_TAG_subrange_type:
c906108c 7977 /* File scope base type definitions are added to the partial
c5aa993b 7978 symbol table. */
72bf9492 7979 add_partial_symbol (pdi, cu);
c906108c 7980 break;
d9fa45fe 7981 case DW_TAG_namespace:
cdc07690 7982 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7983 break;
5d7cb8df 7984 case DW_TAG_module:
59c35742
AB
7985 if (!pdi->is_declaration)
7986 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7987 break;
95554aad
TT
7988 case DW_TAG_imported_unit:
7989 {
7990 struct dwarf2_per_cu_data *per_cu;
7991
f4dc4d17
DE
7992 /* For now we don't handle imported units in type units. */
7993 if (cu->per_cu->is_debug_types)
7994 {
7995 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7996 " supported in type units [in module %s]"),
518817b3 7997 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
7998 }
7999
e3b94546
SM
8000 per_cu = dwarf2_find_containing_comp_unit
8001 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8002 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8003
8004 /* Go read the partial unit, if needed. */
8005 if (per_cu->v.psymtab == NULL)
135f5437 8006 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8007
ae640021 8008 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8009 }
8010 break;
74921315
KS
8011 case DW_TAG_imported_declaration:
8012 add_partial_symbol (pdi, cu);
8013 break;
c906108c
SS
8014 default:
8015 break;
8016 }
8017 }
8018
72bf9492
DJ
8019 /* If the die has a sibling, skip to the sibling. */
8020
8021 pdi = pdi->die_sibling;
8022 }
8023}
8024
8025/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8026
72bf9492 8027 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8028 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8029 Enumerators are an exception; they use the scope of their parent
8030 enumeration type, i.e. the name of the enumeration type is not
8031 prepended to the enumerator.
91c24f0a 8032
72bf9492
DJ
8033 There are two complexities. One is DW_AT_specification; in this
8034 case "parent" means the parent of the target of the specification,
8035 instead of the direct parent of the DIE. The other is compilers
8036 which do not emit DW_TAG_namespace; in this case we try to guess
8037 the fully qualified name of structure types from their members'
8038 linkage names. This must be done using the DIE's children rather
8039 than the children of any DW_AT_specification target. We only need
8040 to do this for structures at the top level, i.e. if the target of
8041 any DW_AT_specification (if any; otherwise the DIE itself) does not
8042 have a parent. */
8043
8044/* Compute the scope prefix associated with PDI's parent, in
8045 compilation unit CU. The result will be allocated on CU's
8046 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8047 field. NULL is returned if no prefix is necessary. */
15d034d0 8048static const char *
72bf9492
DJ
8049partial_die_parent_scope (struct partial_die_info *pdi,
8050 struct dwarf2_cu *cu)
8051{
15d034d0 8052 const char *grandparent_scope;
72bf9492 8053 struct partial_die_info *parent, *real_pdi;
91c24f0a 8054
72bf9492
DJ
8055 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8056 then this means the parent of the specification DIE. */
8057
8058 real_pdi = pdi;
72bf9492 8059 while (real_pdi->has_specification)
fb816e8b 8060 {
122cf0f2
AB
8061 auto res = find_partial_die (real_pdi->spec_offset,
8062 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8063 real_pdi = res.pdi;
8064 cu = res.cu;
8065 }
72bf9492
DJ
8066
8067 parent = real_pdi->die_parent;
8068 if (parent == NULL)
8069 return NULL;
8070
8071 if (parent->scope_set)
8072 return parent->scope;
8073
52356b79 8074 parent->fixup (cu);
72bf9492 8075
10b3939b 8076 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8077
acebe513
UW
8078 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8079 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8080 Work around this problem here. */
8081 if (cu->language == language_cplus
6e70227d 8082 && parent->tag == DW_TAG_namespace
acebe513
UW
8083 && strcmp (parent->name, "::") == 0
8084 && grandparent_scope == NULL)
8085 {
8086 parent->scope = NULL;
8087 parent->scope_set = 1;
8088 return NULL;
8089 }
8090
0a4b0913 8091 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8092 if (pdi->tag == DW_TAG_enumerator)
8093 /* Enumerators should not get the name of the enumeration as a prefix. */
8094 parent->scope = grandparent_scope;
8095 else if (parent->tag == DW_TAG_namespace
f55ee35c 8096 || parent->tag == DW_TAG_module
72bf9492
DJ
8097 || parent->tag == DW_TAG_structure_type
8098 || parent->tag == DW_TAG_class_type
680b30c7 8099 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8100 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8101 || parent->tag == DW_TAG_enumeration_type
8102 || (cu->language == language_fortran
8103 && parent->tag == DW_TAG_subprogram
8104 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8105 {
8106 if (grandparent_scope == NULL)
8107 parent->scope = parent->name;
8108 else
3e43a32a
MS
8109 parent->scope = typename_concat (&cu->comp_unit_obstack,
8110 grandparent_scope,
f55ee35c 8111 parent->name, 0, cu);
72bf9492 8112 }
72bf9492
DJ
8113 else
8114 {
8115 /* FIXME drow/2004-04-01: What should we be doing with
8116 function-local names? For partial symbols, we should probably be
8117 ignoring them. */
fa9c3fa0
TT
8118 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8119 dwarf_tag_name (parent->tag),
8120 sect_offset_str (pdi->sect_off));
72bf9492 8121 parent->scope = grandparent_scope;
c906108c
SS
8122 }
8123
72bf9492
DJ
8124 parent->scope_set = 1;
8125 return parent->scope;
8126}
8127
8128/* Return the fully scoped name associated with PDI, from compilation unit
8129 CU. The result will be allocated with malloc. */
4568ecf9 8130
43816ebc 8131static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8132partial_die_full_name (struct partial_die_info *pdi,
8133 struct dwarf2_cu *cu)
8134{
15d034d0 8135 const char *parent_scope;
72bf9492 8136
98bfdba5
PA
8137 /* If this is a template instantiation, we can not work out the
8138 template arguments from partial DIEs. So, unfortunately, we have
8139 to go through the full DIEs. At least any work we do building
8140 types here will be reused if full symbols are loaded later. */
8141 if (pdi->has_template_arguments)
8142 {
52356b79 8143 pdi->fixup (cu);
98bfdba5
PA
8144
8145 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8146 {
8147 struct die_info *die;
8148 struct attribute attr;
8149 struct dwarf2_cu *ref_cu = cu;
8150
b64f50a1 8151 /* DW_FORM_ref_addr is using section offset. */
b4069958 8152 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8153 attr.form = DW_FORM_ref_addr;
9c541725 8154 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8155 die = follow_die_ref (NULL, &attr, &ref_cu);
8156
43816ebc 8157 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8158 }
8159 }
8160
72bf9492
DJ
8161 parent_scope = partial_die_parent_scope (pdi, cu);
8162 if (parent_scope == NULL)
8163 return NULL;
8164 else
43816ebc
TT
8165 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8166 pdi->name, 0, cu));
c906108c
SS
8167}
8168
8169static void
72bf9492 8170add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8171{
518817b3
SM
8172 struct dwarf2_per_objfile *dwarf2_per_objfile
8173 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8174 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8175 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8176 CORE_ADDR addr = 0;
15d034d0 8177 const char *actual_name = NULL;
e142c38c
DJ
8178 CORE_ADDR baseaddr;
8179
b3b3bada 8180 baseaddr = objfile->text_section_offset ();
c906108c 8181
43816ebc
TT
8182 gdb::unique_xmalloc_ptr<char> built_actual_name
8183 = partial_die_full_name (pdi, cu);
15d034d0 8184 if (built_actual_name != NULL)
43816ebc 8185 actual_name = built_actual_name.get ();
63d06c5c 8186
72bf9492
DJ
8187 if (actual_name == NULL)
8188 actual_name = pdi->name;
8189
c906108c
SS
8190 switch (pdi->tag)
8191 {
b1dc1806 8192 case DW_TAG_inlined_subroutine:
c906108c 8193 case DW_TAG_subprogram:
79748972
TT
8194 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8195 - baseaddr);
0a4b0913
AB
8196 if (pdi->is_external
8197 || cu->language == language_ada
8198 || (cu->language == language_fortran
8199 && pdi->die_parent != NULL
8200 && pdi->die_parent->tag == DW_TAG_subprogram))
8201 {
8202 /* Normally, only "external" DIEs are part of the global scope.
8203 But in Ada and Fortran, we want to be able to access nested
8204 procedures globally. So all Ada and Fortran subprograms are
8205 stored in the global scope. */
31edb802 8206 add_psymbol_to_list (actual_name,
15d034d0 8207 built_actual_name != NULL,
f47fb265 8208 VAR_DOMAIN, LOC_BLOCK,
79748972 8209 SECT_OFF_TEXT (objfile),
75aedd27 8210 psymbol_placement::GLOBAL,
79748972
TT
8211 addr,
8212 cu->language, objfile);
c906108c
SS
8213 }
8214 else
8215 {
31edb802 8216 add_psymbol_to_list (actual_name,
15d034d0 8217 built_actual_name != NULL,
f47fb265 8218 VAR_DOMAIN, LOC_BLOCK,
79748972 8219 SECT_OFF_TEXT (objfile),
75aedd27 8220 psymbol_placement::STATIC,
1762568f 8221 addr, cu->language, objfile);
c906108c 8222 }
0c1b455e
TT
8223
8224 if (pdi->main_subprogram && actual_name != NULL)
8225 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8226 break;
72929c62 8227 case DW_TAG_constant:
31edb802 8228 add_psymbol_to_list (actual_name,
75aedd27
TT
8229 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8230 -1, (pdi->is_external
8231 ? psymbol_placement::GLOBAL
8232 : psymbol_placement::STATIC),
8233 0, cu->language, objfile);
72929c62 8234 break;
c906108c 8235 case DW_TAG_variable:
95554aad
TT
8236 if (pdi->d.locdesc)
8237 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8238
95554aad 8239 if (pdi->d.locdesc
caac4577
JG
8240 && addr == 0
8241 && !dwarf2_per_objfile->has_section_at_zero)
8242 {
8243 /* A global or static variable may also have been stripped
8244 out by the linker if unused, in which case its address
8245 will be nullified; do not add such variables into partial
8246 symbol table then. */
8247 }
8248 else if (pdi->is_external)
c906108c
SS
8249 {
8250 /* Global Variable.
8251 Don't enter into the minimal symbol tables as there is
8252 a minimal symbol table entry from the ELF symbols already.
8253 Enter into partial symbol table if it has a location
8254 descriptor or a type.
8255 If the location descriptor is missing, new_symbol will create
8256 a LOC_UNRESOLVED symbol, the address of the variable will then
8257 be determined from the minimal symbol table whenever the variable
8258 is referenced.
8259 The address for the partial symbol table entry is not
8260 used by GDB, but it comes in handy for debugging partial symbol
8261 table building. */
8262
95554aad 8263 if (pdi->d.locdesc || pdi->has_type)
31edb802 8264 add_psymbol_to_list (actual_name,
15d034d0 8265 built_actual_name != NULL,
f47fb265 8266 VAR_DOMAIN, LOC_STATIC,
79748972 8267 SECT_OFF_TEXT (objfile),
75aedd27 8268 psymbol_placement::GLOBAL,
79748972 8269 addr, cu->language, objfile);
c906108c
SS
8270 }
8271 else
8272 {
ff908ebf
AW
8273 int has_loc = pdi->d.locdesc != NULL;
8274
8275 /* Static Variable. Skip symbols whose value we cannot know (those
8276 without location descriptors or constant values). */
8277 if (!has_loc && !pdi->has_const_value)
43816ebc 8278 return;
ff908ebf 8279
31edb802 8280 add_psymbol_to_list (actual_name,
15d034d0 8281 built_actual_name != NULL,
f47fb265 8282 VAR_DOMAIN, LOC_STATIC,
79748972 8283 SECT_OFF_TEXT (objfile),
75aedd27 8284 psymbol_placement::STATIC,
79748972 8285 has_loc ? addr : 0,
f47fb265 8286 cu->language, objfile);
c906108c
SS
8287 }
8288 break;
8289 case DW_TAG_typedef:
8290 case DW_TAG_base_type:
a02abb62 8291 case DW_TAG_subrange_type:
31edb802 8292 add_psymbol_to_list (actual_name,
15d034d0 8293 built_actual_name != NULL,
79748972 8294 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8295 psymbol_placement::STATIC,
1762568f 8296 0, cu->language, objfile);
c906108c 8297 break;
74921315 8298 case DW_TAG_imported_declaration:
72bf9492 8299 case DW_TAG_namespace:
31edb802 8300 add_psymbol_to_list (actual_name,
15d034d0 8301 built_actual_name != NULL,
79748972 8302 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8303 psymbol_placement::GLOBAL,
1762568f 8304 0, cu->language, objfile);
72bf9492 8305 break;
530e8392 8306 case DW_TAG_module:
a5fd13a9
BH
8307 /* With Fortran 77 there might be a "BLOCK DATA" module
8308 available without any name. If so, we skip the module as it
8309 doesn't bring any value. */
8310 if (actual_name != nullptr)
31edb802 8311 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8312 built_actual_name != NULL,
8313 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8314 psymbol_placement::GLOBAL,
8315 0, cu->language, objfile);
530e8392 8316 break;
c906108c 8317 case DW_TAG_class_type:
680b30c7 8318 case DW_TAG_interface_type:
c906108c
SS
8319 case DW_TAG_structure_type:
8320 case DW_TAG_union_type:
8321 case DW_TAG_enumeration_type:
fa4028e9
JB
8322 /* Skip external references. The DWARF standard says in the section
8323 about "Structure, Union, and Class Type Entries": "An incomplete
8324 structure, union or class type is represented by a structure,
8325 union or class entry that does not have a byte size attribute
8326 and that has a DW_AT_declaration attribute." */
8327 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8328 return;
fa4028e9 8329
63d06c5c
DC
8330 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8331 static vs. global. */
31edb802 8332 add_psymbol_to_list (actual_name,
15d034d0 8333 built_actual_name != NULL,
79748972 8334 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8335 cu->language == language_cplus
75aedd27
TT
8336 ? psymbol_placement::GLOBAL
8337 : psymbol_placement::STATIC,
1762568f 8338 0, cu->language, objfile);
c906108c 8339
c906108c
SS
8340 break;
8341 case DW_TAG_enumerator:
31edb802 8342 add_psymbol_to_list (actual_name,
15d034d0 8343 built_actual_name != NULL,
79748972 8344 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8345 cu->language == language_cplus
75aedd27
TT
8346 ? psymbol_placement::GLOBAL
8347 : psymbol_placement::STATIC,
1762568f 8348 0, cu->language, objfile);
c906108c
SS
8349 break;
8350 default:
8351 break;
8352 }
8353}
8354
5c4e30ca
DC
8355/* Read a partial die corresponding to a namespace; also, add a symbol
8356 corresponding to that namespace to the symbol table. NAMESPACE is
8357 the name of the enclosing namespace. */
91c24f0a 8358
72bf9492
DJ
8359static void
8360add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8361 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8362 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8363{
72bf9492 8364 /* Add a symbol for the namespace. */
e7c27a73 8365
72bf9492 8366 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8367
8368 /* Now scan partial symbols in that namespace. */
8369
91c24f0a 8370 if (pdi->has_children)
cdc07690 8371 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8372}
8373
5d7cb8df
JK
8374/* Read a partial die corresponding to a Fortran module. */
8375
8376static void
8377add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8378 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8379{
530e8392
KB
8380 /* Add a symbol for the namespace. */
8381
8382 add_partial_symbol (pdi, cu);
8383
f55ee35c 8384 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8385
8386 if (pdi->has_children)
cdc07690 8387 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8388}
8389
b1dc1806
XR
8390/* Read a partial die corresponding to a subprogram or an inlined
8391 subprogram and create a partial symbol for that subprogram.
8392 When the CU language allows it, this routine also defines a partial
8393 symbol for each nested subprogram that this subprogram contains.
8394 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8395 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8396
cdc07690
YQ
8397 PDI may also be a lexical block, in which case we simply search
8398 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8399 Again, this is only performed when the CU language allows this
8400 type of definitions. */
8401
8402static void
8403add_partial_subprogram (struct partial_die_info *pdi,
8404 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8405 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8406{
b1dc1806 8407 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8408 {
8409 if (pdi->has_pc_info)
8410 {
8411 if (pdi->lowpc < *lowpc)
8412 *lowpc = pdi->lowpc;
8413 if (pdi->highpc > *highpc)
8414 *highpc = pdi->highpc;
cdc07690 8415 if (set_addrmap)
5734ee8b 8416 {
518817b3 8417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8418 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8419 CORE_ADDR baseaddr;
b926417a
TT
8420 CORE_ADDR this_highpc;
8421 CORE_ADDR this_lowpc;
5734ee8b 8422
b3b3bada 8423 baseaddr = objfile->text_section_offset ();
b926417a
TT
8424 this_lowpc
8425 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8426 pdi->lowpc + baseaddr)
8427 - baseaddr);
8428 this_highpc
8429 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8430 pdi->highpc + baseaddr)
8431 - baseaddr);
d320c2b5 8432 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8433 this_lowpc, this_highpc - 1,
9291a0cd 8434 cu->per_cu->v.psymtab);
5734ee8b 8435 }
481860b3
GB
8436 }
8437
8438 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8439 {
bc30ff58 8440 if (!pdi->is_declaration)
e8d05480
JB
8441 /* Ignore subprogram DIEs that do not have a name, they are
8442 illegal. Do not emit a complaint at this point, we will
8443 do so when we convert this psymtab into a symtab. */
8444 if (pdi->name)
8445 add_partial_symbol (pdi, cu);
bc30ff58
JB
8446 }
8447 }
6e70227d 8448
bc30ff58
JB
8449 if (! pdi->has_children)
8450 return;
8451
0a4b0913 8452 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8453 {
8454 pdi = pdi->die_child;
8455 while (pdi != NULL)
8456 {
52356b79 8457 pdi->fixup (cu);
bc30ff58 8458 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8459 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8460 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8461 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8462 pdi = pdi->die_sibling;
8463 }
8464 }
8465}
8466
91c24f0a
DC
8467/* Read a partial die corresponding to an enumeration type. */
8468
72bf9492
DJ
8469static void
8470add_partial_enumeration (struct partial_die_info *enum_pdi,
8471 struct dwarf2_cu *cu)
91c24f0a 8472{
72bf9492 8473 struct partial_die_info *pdi;
91c24f0a
DC
8474
8475 if (enum_pdi->name != NULL)
72bf9492
DJ
8476 add_partial_symbol (enum_pdi, cu);
8477
8478 pdi = enum_pdi->die_child;
8479 while (pdi)
91c24f0a 8480 {
72bf9492 8481 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8482 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8483 else
72bf9492
DJ
8484 add_partial_symbol (pdi, cu);
8485 pdi = pdi->die_sibling;
91c24f0a 8486 }
91c24f0a
DC
8487}
8488
6caca83c
CC
8489/* Return the initial uleb128 in the die at INFO_PTR. */
8490
8491static unsigned int
d521ce57 8492peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8493{
8494 unsigned int bytes_read;
8495
8496 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8497}
8498
685af9cd
TT
8499/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8500 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8501
4bb7a0a7
DJ
8502 Return the corresponding abbrev, or NULL if the number is zero (indicating
8503 an empty DIE). In either case *BYTES_READ will be set to the length of
8504 the initial number. */
8505
8506static struct abbrev_info *
685af9cd
TT
8507peek_die_abbrev (const die_reader_specs &reader,
8508 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8509{
685af9cd 8510 dwarf2_cu *cu = reader.cu;
518817b3 8511 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8512 unsigned int abbrev_number
8513 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8514
8515 if (abbrev_number == 0)
8516 return NULL;
8517
685af9cd 8518 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8519 if (!abbrev)
8520 {
422b9917 8521 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8522 " at offset %s [in module %s]"),
422b9917 8523 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8524 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8525 }
8526
8527 return abbrev;
8528}
8529
93311388
DE
8530/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8531 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8532 DIE. Any children of the skipped DIEs will also be skipped. */
8533
d521ce57
TT
8534static const gdb_byte *
8535skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8536{
4bb7a0a7
DJ
8537 while (1)
8538 {
685af9cd
TT
8539 unsigned int bytes_read;
8540 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8541
4bb7a0a7
DJ
8542 if (abbrev == NULL)
8543 return info_ptr + bytes_read;
8544 else
dee91e82 8545 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8546 }
8547}
8548
93311388
DE
8549/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8550 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8551 abbrev corresponding to that skipped uleb128 should be passed in
8552 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8553 children. */
8554
d521ce57
TT
8555static const gdb_byte *
8556skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8557 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8558{
8559 unsigned int bytes_read;
8560 struct attribute attr;
dee91e82
DE
8561 bfd *abfd = reader->abfd;
8562 struct dwarf2_cu *cu = reader->cu;
d521ce57 8563 const gdb_byte *buffer = reader->buffer;
f664829e 8564 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8565 unsigned int form, i;
8566
8567 for (i = 0; i < abbrev->num_attrs; i++)
8568 {
8569 /* The only abbrev we care about is DW_AT_sibling. */
8570 if (abbrev->attrs[i].name == DW_AT_sibling)
8571 {
18a8505e
AT
8572 bool ignored;
8573 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8574 &ignored);
4bb7a0a7 8575 if (attr.form == DW_FORM_ref_addr)
b98664d3 8576 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8577 else
b9502d3f 8578 {
9c541725
PA
8579 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8580 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8581
8582 if (sibling_ptr < info_ptr)
b98664d3 8583 complaint (_("DW_AT_sibling points backwards"));
22869d73 8584 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8585 reader->die_section->overflow_complaint ();
b9502d3f
WN
8586 else
8587 return sibling_ptr;
8588 }
4bb7a0a7
DJ
8589 }
8590
8591 /* If it isn't DW_AT_sibling, skip this attribute. */
8592 form = abbrev->attrs[i].form;
8593 skip_attribute:
8594 switch (form)
8595 {
4bb7a0a7 8596 case DW_FORM_ref_addr:
ae411497
TT
8597 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8598 and later it is offset sized. */
8599 if (cu->header.version == 2)
8600 info_ptr += cu->header.addr_size;
8601 else
8602 info_ptr += cu->header.offset_size;
8603 break;
36586728
TT
8604 case DW_FORM_GNU_ref_alt:
8605 info_ptr += cu->header.offset_size;
8606 break;
ae411497 8607 case DW_FORM_addr:
4bb7a0a7
DJ
8608 info_ptr += cu->header.addr_size;
8609 break;
8610 case DW_FORM_data1:
8611 case DW_FORM_ref1:
8612 case DW_FORM_flag:
8fe0f950 8613 case DW_FORM_strx1:
4bb7a0a7
DJ
8614 info_ptr += 1;
8615 break;
2dc7f7b3 8616 case DW_FORM_flag_present:
43988095 8617 case DW_FORM_implicit_const:
2dc7f7b3 8618 break;
4bb7a0a7
DJ
8619 case DW_FORM_data2:
8620 case DW_FORM_ref2:
8fe0f950 8621 case DW_FORM_strx2:
4bb7a0a7
DJ
8622 info_ptr += 2;
8623 break;
8fe0f950
AT
8624 case DW_FORM_strx3:
8625 info_ptr += 3;
8626 break;
4bb7a0a7
DJ
8627 case DW_FORM_data4:
8628 case DW_FORM_ref4:
8fe0f950 8629 case DW_FORM_strx4:
4bb7a0a7
DJ
8630 info_ptr += 4;
8631 break;
8632 case DW_FORM_data8:
8633 case DW_FORM_ref8:
55f1336d 8634 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8635 info_ptr += 8;
8636 break;
0224619f
JK
8637 case DW_FORM_data16:
8638 info_ptr += 16;
8639 break;
4bb7a0a7 8640 case DW_FORM_string:
9b1c24c8 8641 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8642 info_ptr += bytes_read;
8643 break;
2dc7f7b3 8644 case DW_FORM_sec_offset:
4bb7a0a7 8645 case DW_FORM_strp:
36586728 8646 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8647 info_ptr += cu->header.offset_size;
8648 break;
2dc7f7b3 8649 case DW_FORM_exprloc:
4bb7a0a7
DJ
8650 case DW_FORM_block:
8651 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8652 info_ptr += bytes_read;
8653 break;
8654 case DW_FORM_block1:
8655 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8656 break;
8657 case DW_FORM_block2:
8658 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8659 break;
8660 case DW_FORM_block4:
8661 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8662 break;
336d760d 8663 case DW_FORM_addrx:
cf532bd1 8664 case DW_FORM_strx:
4bb7a0a7
DJ
8665 case DW_FORM_sdata:
8666 case DW_FORM_udata:
8667 case DW_FORM_ref_udata:
3019eac3
DE
8668 case DW_FORM_GNU_addr_index:
8669 case DW_FORM_GNU_str_index:
18a8505e 8670 case DW_FORM_rnglistx:
d521ce57 8671 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8672 break;
8673 case DW_FORM_indirect:
8674 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8675 info_ptr += bytes_read;
8676 /* We need to continue parsing from here, so just go back to
8677 the top. */
8678 goto skip_attribute;
8679
8680 default:
3e43a32a
MS
8681 error (_("Dwarf Error: Cannot handle %s "
8682 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8683 dwarf_form_name (form),
8684 bfd_get_filename (abfd));
8685 }
8686 }
8687
8688 if (abbrev->has_children)
dee91e82 8689 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8690 else
8691 return info_ptr;
8692}
8693
93311388 8694/* Locate ORIG_PDI's sibling.
dee91e82 8695 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8696
d521ce57 8697static const gdb_byte *
dee91e82
DE
8698locate_pdi_sibling (const struct die_reader_specs *reader,
8699 struct partial_die_info *orig_pdi,
d521ce57 8700 const gdb_byte *info_ptr)
91c24f0a
DC
8701{
8702 /* Do we know the sibling already? */
72bf9492 8703
91c24f0a
DC
8704 if (orig_pdi->sibling)
8705 return orig_pdi->sibling;
8706
8707 /* Are there any children to deal with? */
8708
8709 if (!orig_pdi->has_children)
8710 return info_ptr;
8711
4bb7a0a7 8712 /* Skip the children the long way. */
91c24f0a 8713
dee91e82 8714 return skip_children (reader, info_ptr);
91c24f0a
DC
8715}
8716
257e7a09 8717/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8718 not NULL. */
c906108c 8719
891813be
TT
8720void
8721dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8722{
ed2dc618
SM
8723 struct dwarf2_per_objfile *dwarf2_per_objfile
8724 = get_dwarf2_per_objfile (objfile);
8725
077cbab2
TT
8726 gdb_assert (!readin);
8727 /* If this psymtab is constructed from a debug-only objfile, the
8728 has_section_at_zero flag will not necessarily be correct. We
8729 can get the correct value for this flag by looking at the data
8730 associated with the (presumably stripped) associated objfile. */
8731 if (objfile->separate_debug_objfile_backlink)
c906108c 8732 {
077cbab2
TT
8733 struct dwarf2_per_objfile *dpo_backlink
8734 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8735
077cbab2
TT
8736 dwarf2_per_objfile->has_section_at_zero
8737 = dpo_backlink->has_section_at_zero;
8738 }
98bfdba5 8739
8566b89b 8740 expand_psymtab (objfile);
95554aad 8741
ed2dc618 8742 process_cu_includes (dwarf2_per_objfile);
c906108c 8743}
9cdd5dbd
DE
8744\f
8745/* Reading in full CUs. */
c906108c 8746
10b3939b
DJ
8747/* Add PER_CU to the queue. */
8748
8749static void
95554aad
TT
8750queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8751 enum language pretend_language)
10b3939b 8752{
10b3939b 8753 per_cu->queued = 1;
39856def 8754 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8755}
8756
89e63ee4
DE
8757/* If PER_CU is not yet queued, add it to the queue.
8758 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8759 dependency.
0907af0c 8760 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8761 meaning either PER_CU is already queued or it is already loaded.
8762
8763 N.B. There is an invariant here that if a CU is queued then it is loaded.
8764 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8765
8766static int
89e63ee4 8767maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8768 struct dwarf2_per_cu_data *per_cu,
8769 enum language pretend_language)
8770{
8771 /* We may arrive here during partial symbol reading, if we need full
8772 DIEs to process an unusual case (e.g. template arguments). Do
8773 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8774 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8775 {
8776 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8777 return 1;
8778 return 0;
8779 }
8780
8781 /* Mark the dependence relation so that we don't flush PER_CU
8782 too early. */
89e63ee4
DE
8783 if (dependent_cu != NULL)
8784 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8785
8786 /* If it's already on the queue, we have nothing to do. */
8787 if (per_cu->queued)
8788 return 0;
8789
8790 /* If the compilation unit is already loaded, just mark it as
8791 used. */
8792 if (per_cu->cu != NULL)
8793 {
8794 per_cu->cu->last_used = 0;
8795 return 0;
8796 }
8797
8798 /* Add it to the queue. */
8799 queue_comp_unit (per_cu, pretend_language);
8800
8801 return 1;
8802}
8803
10b3939b
DJ
8804/* Process the queue. */
8805
8806static void
ed2dc618 8807process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8808{
b4f54984 8809 if (dwarf_read_debug)
45cfd468
DE
8810 {
8811 fprintf_unfiltered (gdb_stdlog,
8812 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8813 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8814 }
8815
03dd20cc
DJ
8816 /* The queue starts out with one item, but following a DIE reference
8817 may load a new CU, adding it to the end of the queue. */
39856def 8818 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8819 {
39856def
TT
8820 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8821
cc12ce38 8822 if ((dwarf2_per_objfile->using_index
39856def
TT
8823 ? !item.per_cu->v.quick->compunit_symtab
8824 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8825 /* Skip dummy CUs. */
39856def 8826 && item.per_cu->cu != NULL)
f4dc4d17 8827 {
39856def 8828 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8829 unsigned int debug_print_threshold;
247f5c4f 8830 char buf[100];
f4dc4d17 8831
247f5c4f 8832 if (per_cu->is_debug_types)
f4dc4d17 8833 {
247f5c4f
DE
8834 struct signatured_type *sig_type =
8835 (struct signatured_type *) per_cu;
8836
9d8780f0 8837 sprintf (buf, "TU %s at offset %s",
73be47f5 8838 hex_string (sig_type->signature),
9d8780f0 8839 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8840 /* There can be 100s of TUs.
8841 Only print them in verbose mode. */
8842 debug_print_threshold = 2;
f4dc4d17 8843 }
247f5c4f 8844 else
73be47f5 8845 {
9d8780f0
SM
8846 sprintf (buf, "CU at offset %s",
8847 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8848 debug_print_threshold = 1;
8849 }
247f5c4f 8850
b4f54984 8851 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8852 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8853
8854 if (per_cu->is_debug_types)
39856def 8855 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8856 else
39856def 8857 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8858
b4f54984 8859 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8860 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8861 }
10b3939b 8862
39856def
TT
8863 item.per_cu->queued = 0;
8864 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8865 }
8866
b4f54984 8867 if (dwarf_read_debug)
45cfd468
DE
8868 {
8869 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8870 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8871 }
10b3939b
DJ
8872}
8873
10b3939b
DJ
8874/* Read in full symbols for PST, and anything it depends on. */
8875
8566b89b
TT
8876void
8877dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8878{
8566b89b 8879 if (readin)
95554aad
TT
8880 return;
8881
0494dbec 8882 read_dependencies (objfile);
aaa75496 8883
b83470bf
TT
8884 dw2_do_instantiate_symtab (per_cu_data, false);
8885 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8886}
8887
dee91e82
DE
8888/* Trivial hash function for die_info: the hash value of a DIE
8889 is its offset in .debug_info for this objfile. */
10b3939b 8890
dee91e82
DE
8891static hashval_t
8892die_hash (const void *item)
10b3939b 8893{
9a3c8263 8894 const struct die_info *die = (const struct die_info *) item;
6502dd73 8895
9c541725 8896 return to_underlying (die->sect_off);
dee91e82 8897}
63d06c5c 8898
dee91e82
DE
8899/* Trivial comparison function for die_info structures: two DIEs
8900 are equal if they have the same offset. */
98bfdba5 8901
dee91e82
DE
8902static int
8903die_eq (const void *item_lhs, const void *item_rhs)
8904{
9a3c8263
SM
8905 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8906 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8907
9c541725 8908 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8909}
c906108c 8910
c0ab21c2 8911/* Load the DIEs associated with PER_CU into memory. */
c906108c 8912
dee91e82 8913static void
c0ab21c2
TT
8914load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8915 bool skip_partial,
8916 enum language pretend_language)
dee91e82 8917{
c0ab21c2
TT
8918 gdb_assert (! this_cu->is_debug_types);
8919
6751ebae 8920 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8921 if (reader.dummy_p)
8922 return;
8923
8924 struct dwarf2_cu *cu = reader.cu;
8925 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8926
dee91e82
DE
8927 gdb_assert (cu->die_hash == NULL);
8928 cu->die_hash =
8929 htab_create_alloc_ex (cu->header.length / 12,
8930 die_hash,
8931 die_eq,
8932 NULL,
8933 &cu->comp_unit_obstack,
8934 hashtab_obstack_allocate,
8935 dummy_obstack_deallocate);
e142c38c 8936
3e225074 8937 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8938 reader.comp_unit_die->child
8939 = read_die_and_siblings (&reader, reader.info_ptr,
8940 &info_ptr, reader.comp_unit_die);
8941 cu->dies = reader.comp_unit_die;
dee91e82 8942 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8943
8944 /* We try not to read any attributes in this function, because not
9cdd5dbd 8945 all CUs needed for references have been loaded yet, and symbol
10b3939b 8946 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8947 or we won't be able to build types correctly.
8948 Similarly, if we do not read the producer, we can not apply
8949 producer-specific interpretation. */
c0ab21c2 8950 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8951
8952 reader.keep ();
10b3939b
DJ
8953}
8954
3da10d80
KS
8955/* Add a DIE to the delayed physname list. */
8956
8957static void
8958add_to_method_list (struct type *type, int fnfield_index, int index,
8959 const char *name, struct die_info *die,
8960 struct dwarf2_cu *cu)
8961{
8962 struct delayed_method_info mi;
8963 mi.type = type;
8964 mi.fnfield_index = fnfield_index;
8965 mi.index = index;
8966 mi.name = name;
8967 mi.die = die;
c89b44cd 8968 cu->method_list.push_back (mi);
3da10d80
KS
8969}
8970
3693fdb3
PA
8971/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8972 "const" / "volatile". If so, decrements LEN by the length of the
8973 modifier and return true. Otherwise return false. */
8974
8975template<size_t N>
8976static bool
8977check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8978{
8979 size_t mod_len = sizeof (mod) - 1;
8980 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8981 {
8982 len -= mod_len;
8983 return true;
8984 }
8985 return false;
8986}
8987
3da10d80
KS
8988/* Compute the physnames of any methods on the CU's method list.
8989
8990 The computation of method physnames is delayed in order to avoid the
8991 (bad) condition that one of the method's formal parameters is of an as yet
8992 incomplete type. */
8993
8994static void
8995compute_delayed_physnames (struct dwarf2_cu *cu)
8996{
3693fdb3 8997 /* Only C++ delays computing physnames. */
c89b44cd 8998 if (cu->method_list.empty ())
3693fdb3
PA
8999 return;
9000 gdb_assert (cu->language == language_cplus);
9001
52941706 9002 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9003 {
1d06ead6 9004 const char *physname;
3da10d80 9005 struct fn_fieldlist *fn_flp
c89b44cd
TT
9006 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9007 physname = dwarf2_physname (mi.name, mi.die, cu);
9008 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9009 = physname ? physname : "";
3693fdb3
PA
9010
9011 /* Since there's no tag to indicate whether a method is a
9012 const/volatile overload, extract that information out of the
9013 demangled name. */
9014 if (physname != NULL)
9015 {
9016 size_t len = strlen (physname);
9017
9018 while (1)
9019 {
9020 if (physname[len] == ')') /* shortcut */
9021 break;
9022 else if (check_modifier (physname, len, " const"))
c89b44cd 9023 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9024 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9025 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9026 else
9027 break;
9028 }
9029 }
3da10d80 9030 }
c89b44cd
TT
9031
9032 /* The list is no longer needed. */
9033 cu->method_list.clear ();
3da10d80
KS
9034}
9035
a766d390
DE
9036/* Go objects should be embedded in a DW_TAG_module DIE,
9037 and it's not clear if/how imported objects will appear.
9038 To keep Go support simple until that's worked out,
9039 go back through what we've read and create something usable.
9040 We could do this while processing each DIE, and feels kinda cleaner,
9041 but that way is more invasive.
9042 This is to, for example, allow the user to type "p var" or "b main"
9043 without having to specify the package name, and allow lookups
9044 of module.object to work in contexts that use the expression
9045 parser. */
9046
9047static void
9048fixup_go_packaging (struct dwarf2_cu *cu)
9049{
421d1616 9050 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9051 struct pending *list;
9052 int i;
9053
c24bdb02 9054 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9055 list != NULL;
9056 list = list->next)
a766d390
DE
9057 {
9058 for (i = 0; i < list->nsyms; ++i)
9059 {
9060 struct symbol *sym = list->symbol[i];
9061
c1b5c1eb 9062 if (sym->language () == language_go
a766d390
DE
9063 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9064 {
421d1616
TT
9065 gdb::unique_xmalloc_ptr<char> this_package_name
9066 (go_symbol_package_name (sym));
a766d390
DE
9067
9068 if (this_package_name == NULL)
9069 continue;
9070 if (package_name == NULL)
421d1616 9071 package_name = std::move (this_package_name);
a766d390
DE
9072 else
9073 {
518817b3
SM
9074 struct objfile *objfile
9075 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9076 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9077 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9078 (symbol_symtab (sym) != NULL
9079 ? symtab_to_filename_for_display
9080 (symbol_symtab (sym))
e3b94546 9081 : objfile_name (objfile)),
421d1616 9082 this_package_name.get (), package_name.get ());
a766d390
DE
9083 }
9084 }
9085 }
9086 }
9087
9088 if (package_name != NULL)
9089 {
518817b3 9090 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9091 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9092 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9093 saved_package_name);
a766d390
DE
9094 struct symbol *sym;
9095
e623cf5d 9096 sym = allocate_symbol (objfile);
d3ecddab 9097 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9098 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9099 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9100 e.g., "main" finds the "main" module and not C's main(). */
9101 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9102 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9103 SYMBOL_TYPE (sym) = type;
9104
c24bdb02 9105 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9106 }
9107}
9108
c9317f21
TT
9109/* Allocate a fully-qualified name consisting of the two parts on the
9110 obstack. */
9111
9112static const char *
9113rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9114{
9115 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9116}
9117
9118/* A helper that allocates a struct discriminant_info to attach to a
9119 union type. */
9120
9121static struct discriminant_info *
9122alloc_discriminant_info (struct type *type, int discriminant_index,
9123 int default_index)
9124{
9125 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9126 gdb_assert (discriminant_index == -1
9127 || (discriminant_index >= 0
9128 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9129 gdb_assert (default_index == -1
c7b15a66 9130 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9131
9132 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9133
9134 struct discriminant_info *disc
9135 = ((struct discriminant_info *)
9136 TYPE_ZALLOC (type,
9137 offsetof (struct discriminant_info, discriminants)
9138 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9139 disc->default_index = default_index;
9140 disc->discriminant_index = discriminant_index;
9141
9142 struct dynamic_prop prop;
9143 prop.kind = PROP_UNDEFINED;
9144 prop.data.baton = disc;
9145
9146 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9147
9148 return disc;
9149}
9150
9151/* Some versions of rustc emitted enums in an unusual way.
9152
9153 Ordinary enums were emitted as unions. The first element of each
9154 structure in the union was named "RUST$ENUM$DISR". This element
9155 held the discriminant.
9156
9157 These versions of Rust also implemented the "non-zero"
9158 optimization. When the enum had two values, and one is empty and
9159 the other holds a pointer that cannot be zero, the pointer is used
9160 as the discriminant, with a zero value meaning the empty variant.
9161 Here, the union's first member is of the form
9162 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9163 where the fieldnos are the indices of the fields that should be
9164 traversed in order to find the field (which may be several fields deep)
9165 and the variantname is the name of the variant of the case when the
9166 field is zero.
9167
9168 This function recognizes whether TYPE is of one of these forms,
9169 and, if so, smashes it to be a variant type. */
9170
9171static void
9172quirk_rust_enum (struct type *type, struct objfile *objfile)
9173{
9174 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9175
9176 /* We don't need to deal with empty enums. */
9177 if (TYPE_NFIELDS (type) == 0)
9178 return;
9179
9180#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9181 if (TYPE_NFIELDS (type) == 1
9182 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9183 {
9184 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9185
9186 /* Decode the field name to find the offset of the
9187 discriminant. */
9188 ULONGEST bit_offset = 0;
9189 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9190 while (name[0] >= '0' && name[0] <= '9')
9191 {
9192 char *tail;
9193 unsigned long index = strtoul (name, &tail, 10);
9194 name = tail;
9195 if (*name != '$'
9196 || index >= TYPE_NFIELDS (field_type)
9197 || (TYPE_FIELD_LOC_KIND (field_type, index)
9198 != FIELD_LOC_KIND_BITPOS))
9199 {
b98664d3 9200 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9201 "[in module %s]"),
9202 TYPE_FIELD_NAME (type, 0),
9203 objfile_name (objfile));
9204 return;
9205 }
9206 ++name;
9207
9208 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9209 field_type = TYPE_FIELD_TYPE (field_type, index);
9210 }
9211
9212 /* Make a union to hold the variants. */
9213 struct type *union_type = alloc_type (objfile);
9214 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9215 TYPE_NFIELDS (union_type) = 3;
9216 TYPE_FIELDS (union_type)
9217 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9218 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9219 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9220
9221 /* Put the discriminant must at index 0. */
9222 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9223 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9224 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9225 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9226
9227 /* The order of fields doesn't really matter, so put the real
9228 field at index 1 and the data-less field at index 2. */
9229 struct discriminant_info *disc
9230 = alloc_discriminant_info (union_type, 0, 1);
9231 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9232 TYPE_FIELD_NAME (union_type, 1)
9233 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9234 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9235 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9236 TYPE_FIELD_NAME (union_type, 1));
9237
9238 const char *dataless_name
9239 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9240 name);
9241 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9242 dataless_name);
9243 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9244 /* NAME points into the original discriminant name, which
9245 already has the correct lifetime. */
9246 TYPE_FIELD_NAME (union_type, 2) = name;
9247 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9248 disc->discriminants[2] = 0;
9249
9250 /* Smash this type to be a structure type. We have to do this
9251 because the type has already been recorded. */
9252 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9253 TYPE_NFIELDS (type) = 1;
9254 TYPE_FIELDS (type)
9255 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9256
9257 /* Install the variant part. */
9258 TYPE_FIELD_TYPE (type, 0) = union_type;
9259 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9260 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9261 }
77c2dba3
TT
9262 /* A union with a single anonymous field is probably an old-style
9263 univariant enum. */
9264 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9265 {
c9317f21
TT
9266 /* Smash this type to be a structure type. We have to do this
9267 because the type has already been recorded. */
9268 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9269
9270 /* Make a union to hold the variants. */
9271 struct type *union_type = alloc_type (objfile);
9272 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9273 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9274 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9275 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9276 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9277
9278 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9279 const char *variant_name
9280 = rust_last_path_segment (TYPE_NAME (field_type));
9281 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9282 TYPE_NAME (field_type)
9283 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9284 TYPE_NAME (type), variant_name);
c9317f21
TT
9285
9286 /* Install the union in the outer struct type. */
9287 TYPE_NFIELDS (type) = 1;
9288 TYPE_FIELDS (type)
9289 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9290 TYPE_FIELD_TYPE (type, 0) = union_type;
9291 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9292 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9293
9294 alloc_discriminant_info (union_type, -1, 0);
9295 }
9296 else
9297 {
9298 struct type *disr_type = nullptr;
9299 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9300 {
9301 disr_type = TYPE_FIELD_TYPE (type, i);
9302
a037790e
TT
9303 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9304 {
9305 /* All fields of a true enum will be structs. */
9306 return;
9307 }
9308 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9309 {
9310 /* Could be data-less variant, so keep going. */
a037790e 9311 disr_type = nullptr;
c9317f21
TT
9312 }
9313 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9314 "RUST$ENUM$DISR") != 0)
9315 {
9316 /* Not a Rust enum. */
9317 return;
9318 }
9319 else
9320 {
9321 /* Found one. */
9322 break;
9323 }
9324 }
9325
9326 /* If we got here without a discriminant, then it's probably
9327 just a union. */
9328 if (disr_type == nullptr)
9329 return;
9330
9331 /* Smash this type to be a structure type. We have to do this
9332 because the type has already been recorded. */
9333 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9334
9335 /* Make a union to hold the variants. */
9336 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9337 struct type *union_type = alloc_type (objfile);
9338 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9339 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9340 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9341 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9342 TYPE_FIELDS (union_type)
9343 = (struct field *) TYPE_ZALLOC (union_type,
9344 (TYPE_NFIELDS (union_type)
9345 * sizeof (struct field)));
9346
9347 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9348 TYPE_NFIELDS (type) * sizeof (struct field));
9349
9350 /* Install the discriminant at index 0 in the union. */
9351 TYPE_FIELD (union_type, 0) = *disr_field;
9352 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9353 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9354
9355 /* Install the union in the outer struct type. */
9356 TYPE_FIELD_TYPE (type, 0) = union_type;
9357 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9358 TYPE_NFIELDS (type) = 1;
9359
9360 /* Set the size and offset of the union type. */
9361 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9362
9363 /* We need a way to find the correct discriminant given a
9364 variant name. For convenience we build a map here. */
9365 struct type *enum_type = FIELD_TYPE (*disr_field);
9366 std::unordered_map<std::string, ULONGEST> discriminant_map;
9367 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9368 {
9369 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9370 {
9371 const char *name
9372 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9373 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9374 }
9375 }
9376
9377 int n_fields = TYPE_NFIELDS (union_type);
9378 struct discriminant_info *disc
9379 = alloc_discriminant_info (union_type, 0, -1);
9380 /* Skip the discriminant here. */
9381 for (int i = 1; i < n_fields; ++i)
9382 {
9383 /* Find the final word in the name of this variant's type.
9384 That name can be used to look up the correct
9385 discriminant. */
9386 const char *variant_name
9387 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9388 i)));
9389
9390 auto iter = discriminant_map.find (variant_name);
9391 if (iter != discriminant_map.end ())
9392 disc->discriminants[i] = iter->second;
9393
bedda9ac 9394 /* Remove the discriminant field, if it exists. */
c9317f21 9395 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9396 if (TYPE_NFIELDS (sub_type) > 0)
9397 {
9398 --TYPE_NFIELDS (sub_type);
9399 ++TYPE_FIELDS (sub_type);
9400 }
c9317f21
TT
9401 TYPE_FIELD_NAME (union_type, i) = variant_name;
9402 TYPE_NAME (sub_type)
9403 = rust_fully_qualify (&objfile->objfile_obstack,
9404 TYPE_NAME (type), variant_name);
9405 }
9406 }
9407}
9408
9409/* Rewrite some Rust unions to be structures with variants parts. */
9410
9411static void
9412rust_union_quirks (struct dwarf2_cu *cu)
9413{
9414 gdb_assert (cu->language == language_rust);
52941706
SM
9415 for (type *type_ : cu->rust_unions)
9416 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9417 /* We don't need this any more. */
9418 cu->rust_unions.clear ();
c9317f21
TT
9419}
9420
95554aad
TT
9421/* Return the symtab for PER_CU. This works properly regardless of
9422 whether we're using the index or psymtabs. */
9423
43f3e411
DE
9424static struct compunit_symtab *
9425get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9426{
ed2dc618 9427 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9428 ? per_cu->v.quick->compunit_symtab
9429 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9430}
9431
9432/* A helper function for computing the list of all symbol tables
9433 included by PER_CU. */
9434
9435static void
4c39bc03 9436recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9437 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9438 struct dwarf2_per_cu_data *per_cu,
43f3e411 9439 struct compunit_symtab *immediate_parent)
95554aad
TT
9440{
9441 void **slot;
43f3e411 9442 struct compunit_symtab *cust;
95554aad
TT
9443
9444 slot = htab_find_slot (all_children, per_cu, INSERT);
9445 if (*slot != NULL)
9446 {
9447 /* This inclusion and its children have been processed. */
9448 return;
9449 }
9450
9451 *slot = per_cu;
9452 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9453 cust = get_compunit_symtab (per_cu);
9454 if (cust != NULL)
ec94af83
DE
9455 {
9456 /* If this is a type unit only add its symbol table if we haven't
9457 seen it yet (type unit per_cu's can share symtabs). */
9458 if (per_cu->is_debug_types)
9459 {
43f3e411 9460 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9461 if (*slot == NULL)
9462 {
43f3e411 9463 *slot = cust;
4c39bc03 9464 result->push_back (cust);
43f3e411
DE
9465 if (cust->user == NULL)
9466 cust->user = immediate_parent;
ec94af83
DE
9467 }
9468 }
9469 else
f9125b6c 9470 {
4c39bc03 9471 result->push_back (cust);
43f3e411
DE
9472 if (cust->user == NULL)
9473 cust->user = immediate_parent;
f9125b6c 9474 }
ec94af83 9475 }
95554aad 9476
ae640021
AB
9477 if (!per_cu->imported_symtabs_empty ())
9478 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9479 {
9480 recursively_compute_inclusions (result, all_children,
9481 all_type_symtabs, ptr, cust);
9482 }
95554aad
TT
9483}
9484
43f3e411 9485/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9486 PER_CU. */
9487
9488static void
43f3e411 9489compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9490{
f4dc4d17
DE
9491 gdb_assert (! per_cu->is_debug_types);
9492
ae640021 9493 if (!per_cu->imported_symtabs_empty ())
95554aad 9494 {
ae640021 9495 int len;
4c39bc03 9496 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9497 htab_t all_children, all_type_symtabs;
43f3e411 9498 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9499
9500 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9501 if (cust == NULL)
95554aad
TT
9502 return;
9503
9504 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9505 NULL, xcalloc, xfree);
ec94af83
DE
9506 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9507 NULL, xcalloc, xfree);
95554aad 9508
ae640021 9509 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9510 {
9511 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9512 all_type_symtabs, ptr, cust);
ec94af83 9513 }
95554aad 9514
ec94af83 9515 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9516 len = result_symtabs.size ();
43f3e411 9517 cust->includes
ed2dc618 9518 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9519 struct compunit_symtab *, len + 1);
4c39bc03
TT
9520 memcpy (cust->includes, result_symtabs.data (),
9521 len * sizeof (compunit_symtab *));
43f3e411 9522 cust->includes[len] = NULL;
95554aad 9523
95554aad 9524 htab_delete (all_children);
ec94af83 9525 htab_delete (all_type_symtabs);
95554aad
TT
9526 }
9527}
9528
9529/* Compute the 'includes' field for the symtabs of all the CUs we just
9530 read. */
9531
9532static void
ed2dc618 9533process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9534{
71b73764 9535 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9536 {
9537 if (! iter->is_debug_types)
43f3e411 9538 compute_compunit_symtab_includes (iter);
f4dc4d17 9539 }
95554aad 9540
c5d0225d 9541 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9542}
9543
9cdd5dbd 9544/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9545 already been loaded into memory. */
9546
9547static void
95554aad
TT
9548process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9549 enum language pretend_language)
10b3939b 9550{
10b3939b 9551 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9552 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9553 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9554 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9555 CORE_ADDR lowpc, highpc;
43f3e411 9556 struct compunit_symtab *cust;
10b3939b 9557 CORE_ADDR baseaddr;
4359dff1 9558 struct block *static_block;
3e29f34a 9559 CORE_ADDR addr;
10b3939b 9560
b3b3bada 9561 baseaddr = objfile->text_section_offset ();
10b3939b 9562
c89b44cd
TT
9563 /* Clear the list here in case something was left over. */
9564 cu->method_list.clear ();
10b3939b 9565
95554aad
TT
9566 cu->language = pretend_language;
9567 cu->language_defn = language_def (cu->language);
9568
c906108c 9569 /* Do line number decoding in read_file_scope () */
10b3939b 9570 process_die (cu->dies, cu);
c906108c 9571
a766d390
DE
9572 /* For now fudge the Go package. */
9573 if (cu->language == language_go)
9574 fixup_go_packaging (cu);
9575
5f48f8f3 9576 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9577 should be complete, and it should now be safe to compute all of the
9578 physnames. */
9579 compute_delayed_physnames (cu);
3da10d80 9580
c9317f21
TT
9581 if (cu->language == language_rust)
9582 rust_union_quirks (cu);
9583
fae299cd
DC
9584 /* Some compilers don't define a DW_AT_high_pc attribute for the
9585 compilation unit. If the DW_AT_high_pc is missing, synthesize
9586 it, by scanning the DIE's below the compilation unit. */
10b3939b 9587 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9588
3e29f34a 9589 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9590 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9591
9592 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9593 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9594 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9595 addrmap to help ensure it has an accurate map of pc values belonging to
9596 this comp unit. */
9597 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9598
c24bdb02 9599 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9600 SECT_OFF_TEXT (objfile),
9601 0);
c906108c 9602
43f3e411 9603 if (cust != NULL)
c906108c 9604 {
df15bd07 9605 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9606
8be455d7
JK
9607 /* Set symtab language to language from DW_AT_language. If the
9608 compilation is from a C file generated by language preprocessors, do
9609 not set the language if it was already deduced by start_subfile. */
43f3e411 9610 if (!(cu->language == language_c
40e3ad0e 9611 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9612 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9613
9614 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9615 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9616 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9617 there were bugs in prologue debug info, fixed later in GCC-4.5
9618 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9619
9620 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9621 needed, it would be wrong due to missing DW_AT_producer there.
9622
9623 Still one can confuse GDB by using non-standard GCC compilation
9624 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9625 */
ab260dad 9626 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9627 cust->locations_valid = 1;
e0d00bc7
JK
9628
9629 if (gcc_4_minor >= 5)
43f3e411 9630 cust->epilogue_unwind_valid = 1;
96408a79 9631
43f3e411 9632 cust->call_site_htab = cu->call_site_htab;
c906108c 9633 }
9291a0cd
TT
9634
9635 if (dwarf2_per_objfile->using_index)
43f3e411 9636 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9637 else
9638 {
891813be 9639 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9640 pst->compunit_symtab = cust;
6d94535f 9641 pst->readin = true;
9291a0cd 9642 }
c906108c 9643
95554aad 9644 /* Push it for inclusion processing later. */
c5d0225d 9645 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9646
9647 /* Not needed any more. */
c24bdb02 9648 cu->reset_builder ();
f4dc4d17 9649}
45cfd468 9650
f4dc4d17
DE
9651/* Generate full symbol information for type unit PER_CU, whose DIEs have
9652 already been loaded into memory. */
9653
9654static void
9655process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9656 enum language pretend_language)
9657{
9658 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9659 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9660 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9661 struct compunit_symtab *cust;
0186c6a7
DE
9662 struct signatured_type *sig_type;
9663
9664 gdb_assert (per_cu->is_debug_types);
9665 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9666
c89b44cd
TT
9667 /* Clear the list here in case something was left over. */
9668 cu->method_list.clear ();
f4dc4d17 9669
f4dc4d17
DE
9670 cu->language = pretend_language;
9671 cu->language_defn = language_def (cu->language);
9672
9673 /* The symbol tables are set up in read_type_unit_scope. */
9674 process_die (cu->dies, cu);
9675
9676 /* For now fudge the Go package. */
9677 if (cu->language == language_go)
9678 fixup_go_packaging (cu);
9679
5f48f8f3 9680 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9681 should be complete, and it should now be safe to compute all of the
9682 physnames. */
9683 compute_delayed_physnames (cu);
f4dc4d17 9684
c9317f21
TT
9685 if (cu->language == language_rust)
9686 rust_union_quirks (cu);
9687
f4dc4d17
DE
9688 /* TUs share symbol tables.
9689 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9690 of it with end_expandable_symtab. Otherwise, complete the addition of
9691 this TU's symbols to the existing symtab. */
43f3e411 9692 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9693 {
c24bdb02
KS
9694 buildsym_compunit *builder = cu->get_builder ();
9695 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9696 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9697
43f3e411 9698 if (cust != NULL)
f4dc4d17
DE
9699 {
9700 /* Set symtab language to language from DW_AT_language. If the
9701 compilation is from a C file generated by language preprocessors,
9702 do not set the language if it was already deduced by
9703 start_subfile. */
43f3e411
DE
9704 if (!(cu->language == language_c
9705 && COMPUNIT_FILETABS (cust)->language != language_c))
9706 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9707 }
9708 }
9709 else
9710 {
c24bdb02 9711 cu->get_builder ()->augment_type_symtab ();
43f3e411 9712 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9713 }
9714
9715 if (dwarf2_per_objfile->using_index)
43f3e411 9716 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9717 else
9718 {
891813be 9719 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9720 pst->compunit_symtab = cust;
6d94535f 9721 pst->readin = true;
45cfd468 9722 }
804d2729
TT
9723
9724 /* Not needed any more. */
c24bdb02 9725 cu->reset_builder ();
c906108c
SS
9726}
9727
95554aad
TT
9728/* Process an imported unit DIE. */
9729
9730static void
9731process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9732{
9733 struct attribute *attr;
9734
f4dc4d17
DE
9735 /* For now we don't handle imported units in type units. */
9736 if (cu->per_cu->is_debug_types)
9737 {
9738 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9739 " supported in type units [in module %s]"),
518817b3 9740 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9741 }
9742
95554aad
TT
9743 attr = dwarf2_attr (die, DW_AT_import, cu);
9744 if (attr != NULL)
9745 {
9c541725
PA
9746 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9747 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9748 dwarf2_per_cu_data *per_cu
e3b94546 9749 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9750 cu->per_cu->dwarf2_per_objfile);
95554aad 9751
58990295
TV
9752 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9753 into another compilation unit, at root level. Regard this as a hint,
9754 and ignore it. */
9755 if (die->parent && die->parent->parent == NULL
9756 && per_cu->unit_type == DW_UT_compile
9757 && per_cu->lang == language_cplus)
9758 return;
9759
69d751e3 9760 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9761 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9762 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9763
ae640021 9764 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9765 }
9766}
9767
4c8aa72d
PA
9768/* RAII object that represents a process_die scope: i.e.,
9769 starts/finishes processing a DIE. */
9770class process_die_scope
adde2bff 9771{
4c8aa72d
PA
9772public:
9773 process_die_scope (die_info *die, dwarf2_cu *cu)
9774 : m_die (die), m_cu (cu)
9775 {
9776 /* We should only be processing DIEs not already in process. */
9777 gdb_assert (!m_die->in_process);
9778 m_die->in_process = true;
9779 }
8c3cb9fa 9780
4c8aa72d
PA
9781 ~process_die_scope ()
9782 {
9783 m_die->in_process = false;
9784
9785 /* If we're done processing the DIE for the CU that owns the line
9786 header, we don't need the line header anymore. */
9787 if (m_cu->line_header_die_owner == m_die)
9788 {
9789 delete m_cu->line_header;
9790 m_cu->line_header = NULL;
9791 m_cu->line_header_die_owner = NULL;
9792 }
9793 }
9794
9795private:
9796 die_info *m_die;
9797 dwarf2_cu *m_cu;
9798};
adde2bff 9799
c906108c
SS
9800/* Process a die and its children. */
9801
9802static void
e7c27a73 9803process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9804{
4c8aa72d 9805 process_die_scope scope (die, cu);
adde2bff 9806
c906108c
SS
9807 switch (die->tag)
9808 {
9809 case DW_TAG_padding:
9810 break;
9811 case DW_TAG_compile_unit:
95554aad 9812 case DW_TAG_partial_unit:
e7c27a73 9813 read_file_scope (die, cu);
c906108c 9814 break;
348e048f
DE
9815 case DW_TAG_type_unit:
9816 read_type_unit_scope (die, cu);
9817 break;
c906108c 9818 case DW_TAG_subprogram:
0a4b0913
AB
9819 /* Nested subprograms in Fortran get a prefix. */
9820 if (cu->language == language_fortran
9821 && die->parent != NULL
9822 && die->parent->tag == DW_TAG_subprogram)
9823 cu->processing_has_namespace_info = true;
9824 /* Fall through. */
c906108c 9825 case DW_TAG_inlined_subroutine:
edb3359d 9826 read_func_scope (die, cu);
c906108c
SS
9827 break;
9828 case DW_TAG_lexical_block:
14898363
L
9829 case DW_TAG_try_block:
9830 case DW_TAG_catch_block:
e7c27a73 9831 read_lexical_block_scope (die, cu);
c906108c 9832 break;
216f72a1 9833 case DW_TAG_call_site:
96408a79
SA
9834 case DW_TAG_GNU_call_site:
9835 read_call_site_scope (die, cu);
9836 break;
c906108c 9837 case DW_TAG_class_type:
680b30c7 9838 case DW_TAG_interface_type:
c906108c
SS
9839 case DW_TAG_structure_type:
9840 case DW_TAG_union_type:
134d01f1 9841 process_structure_scope (die, cu);
c906108c
SS
9842 break;
9843 case DW_TAG_enumeration_type:
134d01f1 9844 process_enumeration_scope (die, cu);
c906108c 9845 break;
134d01f1 9846
f792889a
DJ
9847 /* These dies have a type, but processing them does not create
9848 a symbol or recurse to process the children. Therefore we can
9849 read them on-demand through read_type_die. */
c906108c 9850 case DW_TAG_subroutine_type:
72019c9c 9851 case DW_TAG_set_type:
c906108c 9852 case DW_TAG_array_type:
c906108c 9853 case DW_TAG_pointer_type:
c906108c 9854 case DW_TAG_ptr_to_member_type:
c906108c 9855 case DW_TAG_reference_type:
4297a3f0 9856 case DW_TAG_rvalue_reference_type:
c906108c 9857 case DW_TAG_string_type:
c906108c 9858 break;
134d01f1 9859
c906108c 9860 case DW_TAG_base_type:
a02abb62 9861 case DW_TAG_subrange_type:
cb249c71 9862 case DW_TAG_typedef:
134d01f1
DJ
9863 /* Add a typedef symbol for the type definition, if it has a
9864 DW_AT_name. */
f792889a 9865 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9866 break;
c906108c 9867 case DW_TAG_common_block:
e7c27a73 9868 read_common_block (die, cu);
c906108c
SS
9869 break;
9870 case DW_TAG_common_inclusion:
9871 break;
d9fa45fe 9872 case DW_TAG_namespace:
9068261f 9873 cu->processing_has_namespace_info = true;
e7c27a73 9874 read_namespace (die, cu);
d9fa45fe 9875 break;
5d7cb8df 9876 case DW_TAG_module:
9068261f 9877 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9878 read_module (die, cu);
9879 break;
d9fa45fe 9880 case DW_TAG_imported_declaration:
9068261f 9881 cu->processing_has_namespace_info = true;
74921315
KS
9882 if (read_namespace_alias (die, cu))
9883 break;
86a73007
TT
9884 /* The declaration is not a global namespace alias. */
9885 /* Fall through. */
d9fa45fe 9886 case DW_TAG_imported_module:
9068261f 9887 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9888 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9889 || cu->language != language_fortran))
b98664d3 9890 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9891 dwarf_tag_name (die->tag));
9892 read_import_statement (die, cu);
d9fa45fe 9893 break;
95554aad
TT
9894
9895 case DW_TAG_imported_unit:
9896 process_imported_unit_die (die, cu);
9897 break;
9898
71a3c369
TT
9899 case DW_TAG_variable:
9900 read_variable (die, cu);
9901 break;
9902
c906108c 9903 default:
e7c27a73 9904 new_symbol (die, NULL, cu);
c906108c
SS
9905 break;
9906 }
9907}
ca69b9e6
DE
9908\f
9909/* DWARF name computation. */
c906108c 9910
94af9270
KS
9911/* A helper function for dwarf2_compute_name which determines whether DIE
9912 needs to have the name of the scope prepended to the name listed in the
9913 die. */
9914
9915static int
9916die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9917{
1c809c68
TT
9918 struct attribute *attr;
9919
94af9270
KS
9920 switch (die->tag)
9921 {
9922 case DW_TAG_namespace:
9923 case DW_TAG_typedef:
9924 case DW_TAG_class_type:
9925 case DW_TAG_interface_type:
9926 case DW_TAG_structure_type:
9927 case DW_TAG_union_type:
9928 case DW_TAG_enumeration_type:
9929 case DW_TAG_enumerator:
9930 case DW_TAG_subprogram:
08a76f8a 9931 case DW_TAG_inlined_subroutine:
94af9270 9932 case DW_TAG_member:
74921315 9933 case DW_TAG_imported_declaration:
94af9270
KS
9934 return 1;
9935
9936 case DW_TAG_variable:
c2b0a229 9937 case DW_TAG_constant:
94af9270
KS
9938 /* We only need to prefix "globally" visible variables. These include
9939 any variable marked with DW_AT_external or any variable that
9940 lives in a namespace. [Variables in anonymous namespaces
9941 require prefixing, but they are not DW_AT_external.] */
9942
9943 if (dwarf2_attr (die, DW_AT_specification, cu))
9944 {
9945 struct dwarf2_cu *spec_cu = cu;
9a619af0 9946
94af9270
KS
9947 return die_needs_namespace (die_specification (die, &spec_cu),
9948 spec_cu);
9949 }
9950
1c809c68 9951 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9952 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9953 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9954 return 0;
9955 /* A variable in a lexical block of some kind does not need a
9956 namespace, even though in C++ such variables may be external
9957 and have a mangled name. */
9958 if (die->parent->tag == DW_TAG_lexical_block
9959 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9960 || die->parent->tag == DW_TAG_catch_block
9961 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9962 return 0;
9963 return 1;
94af9270
KS
9964
9965 default:
9966 return 0;
9967 }
9968}
9969
73b9be8b
KS
9970/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9971 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9972 defined for the given DIE. */
9973
9974static struct attribute *
9975dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9976{
9977 struct attribute *attr;
9978
9979 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9980 if (attr == NULL)
9981 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9982
9983 return attr;
9984}
9985
9986/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9987 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9988 defined for the given DIE. */
9989
9990static const char *
9991dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9992{
9993 const char *linkage_name;
9994
9995 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9996 if (linkage_name == NULL)
9997 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9998
9999 return linkage_name;
10000}
10001
94af9270 10002/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10003 compute the physname for the object, which include a method's:
9c37b5ae 10004 - formal parameters (C++),
a766d390 10005 - receiver type (Go),
a766d390
DE
10006
10007 The term "physname" is a bit confusing.
10008 For C++, for example, it is the demangled name.
10009 For Go, for example, it's the mangled name.
94af9270 10010
af6b7be1
JB
10011 For Ada, return the DIE's linkage name rather than the fully qualified
10012 name. PHYSNAME is ignored..
10013
94af9270
KS
10014 The result is allocated on the objfile_obstack and canonicalized. */
10015
10016static const char *
15d034d0
TT
10017dwarf2_compute_name (const char *name,
10018 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10019 int physname)
10020{
518817b3 10021 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10022
94af9270
KS
10023 if (name == NULL)
10024 name = dwarf2_name (die, cu);
10025
2ee7123e
DE
10026 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10027 but otherwise compute it by typename_concat inside GDB.
10028 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10029 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10030 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10031 will set the demangled name to the result of dwarf2_full_name, and it is
10032 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10033 if (cu->language == language_ada
10034 || (cu->language == language_fortran && physname))
10035 {
10036 /* For Ada unit, we prefer the linkage name over the name, as
10037 the former contains the exported name, which the user expects
10038 to be able to reference. Ideally, we want the user to be able
10039 to reference this entity using either natural or linkage name,
10040 but we haven't started looking at this enhancement yet. */
73b9be8b 10041 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10042
2ee7123e
DE
10043 if (linkage_name != NULL)
10044 return linkage_name;
f55ee35c
JK
10045 }
10046
94af9270
KS
10047 /* These are the only languages we know how to qualify names in. */
10048 if (name != NULL
9c37b5ae 10049 && (cu->language == language_cplus
c44af4eb
TT
10050 || cu->language == language_fortran || cu->language == language_d
10051 || cu->language == language_rust))
94af9270
KS
10052 {
10053 if (die_needs_namespace (die, cu))
10054 {
0d5cff50 10055 const char *prefix;
34a68019 10056 const char *canonical_name = NULL;
94af9270 10057
d7e74731
PA
10058 string_file buf;
10059
94af9270 10060 prefix = determine_prefix (die, cu);
94af9270
KS
10061 if (*prefix != '\0')
10062 {
43816ebc
TT
10063 gdb::unique_xmalloc_ptr<char> prefixed_name
10064 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10065
43816ebc 10066 buf.puts (prefixed_name.get ());
94af9270
KS
10067 }
10068 else
d7e74731 10069 buf.puts (name);
94af9270 10070
98bfdba5
PA
10071 /* Template parameters may be specified in the DIE's DW_AT_name, or
10072 as children with DW_TAG_template_type_param or
10073 DW_TAG_value_type_param. If the latter, add them to the name
10074 here. If the name already has template parameters, then
10075 skip this step; some versions of GCC emit both, and
10076 it is more efficient to use the pre-computed name.
10077
10078 Something to keep in mind about this process: it is very
10079 unlikely, or in some cases downright impossible, to produce
10080 something that will match the mangled name of a function.
10081 If the definition of the function has the same debug info,
10082 we should be able to match up with it anyway. But fallbacks
10083 using the minimal symbol, for instance to find a method
10084 implemented in a stripped copy of libstdc++, will not work.
10085 If we do not have debug info for the definition, we will have to
10086 match them up some other way.
10087
10088 When we do name matching there is a related problem with function
10089 templates; two instantiated function templates are allowed to
10090 differ only by their return types, which we do not add here. */
10091
10092 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10093 {
10094 struct attribute *attr;
10095 struct die_info *child;
10096 int first = 1;
10097
10098 die->building_fullname = 1;
10099
10100 for (child = die->child; child != NULL; child = child->sibling)
10101 {
10102 struct type *type;
12df843f 10103 LONGEST value;
d521ce57 10104 const gdb_byte *bytes;
98bfdba5
PA
10105 struct dwarf2_locexpr_baton *baton;
10106 struct value *v;
10107
10108 if (child->tag != DW_TAG_template_type_param
10109 && child->tag != DW_TAG_template_value_param)
10110 continue;
10111
10112 if (first)
10113 {
d7e74731 10114 buf.puts ("<");
98bfdba5
PA
10115 first = 0;
10116 }
10117 else
d7e74731 10118 buf.puts (", ");
98bfdba5
PA
10119
10120 attr = dwarf2_attr (child, DW_AT_type, cu);
10121 if (attr == NULL)
10122 {
b98664d3 10123 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10124 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10125 continue;
10126 }
10127 type = die_type (child, cu);
10128
10129 if (child->tag == DW_TAG_template_type_param)
10130 {
c1ec8cea
TT
10131 c_print_type (type, "", &buf, -1, 0, cu->language,
10132 &type_print_raw_options);
98bfdba5
PA
10133 continue;
10134 }
10135
10136 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10137 if (attr == NULL)
10138 {
b98664d3 10139 complaint (_("template parameter missing "
3e43a32a 10140 "DW_AT_const_value"));
d7e74731 10141 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10142 continue;
10143 }
10144
10145 dwarf2_const_value_attr (attr, type, name,
10146 &cu->comp_unit_obstack, cu,
10147 &value, &bytes, &baton);
10148
10149 if (TYPE_NOSIGN (type))
10150 /* GDB prints characters as NUMBER 'CHAR'. If that's
10151 changed, this can use value_print instead. */
d7e74731 10152 c_printchar (value, type, &buf);
98bfdba5
PA
10153 else
10154 {
10155 struct value_print_options opts;
10156
10157 if (baton != NULL)
10158 v = dwarf2_evaluate_loc_desc (type, NULL,
10159 baton->data,
10160 baton->size,
10161 baton->per_cu);
10162 else if (bytes != NULL)
10163 {
10164 v = allocate_value (type);
10165 memcpy (value_contents_writeable (v), bytes,
10166 TYPE_LENGTH (type));
10167 }
10168 else
10169 v = value_from_longest (type, value);
10170
3e43a32a
MS
10171 /* Specify decimal so that we do not depend on
10172 the radix. */
98bfdba5
PA
10173 get_formatted_print_options (&opts, 'd');
10174 opts.raw = 1;
d7e74731 10175 value_print (v, &buf, &opts);
98bfdba5 10176 release_value (v);
98bfdba5
PA
10177 }
10178 }
10179
10180 die->building_fullname = 0;
10181
10182 if (!first)
10183 {
10184 /* Close the argument list, with a space if necessary
10185 (nested templates). */
d7e74731
PA
10186 if (!buf.empty () && buf.string ().back () == '>')
10187 buf.puts (" >");
98bfdba5 10188 else
d7e74731 10189 buf.puts (">");
98bfdba5
PA
10190 }
10191 }
10192
9c37b5ae 10193 /* For C++ methods, append formal parameter type
94af9270 10194 information, if PHYSNAME. */
6e70227d 10195
94af9270 10196 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10197 && cu->language == language_cplus)
94af9270
KS
10198 {
10199 struct type *type = read_type_die (die, cu);
10200
d7e74731 10201 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10202 &type_print_raw_options);
94af9270 10203
9c37b5ae 10204 if (cu->language == language_cplus)
94af9270 10205 {
60430eff
DJ
10206 /* Assume that an artificial first parameter is
10207 "this", but do not crash if it is not. RealView
10208 marks unnamed (and thus unused) parameters as
10209 artificial; there is no way to differentiate
10210 the two cases. */
94af9270
KS
10211 if (TYPE_NFIELDS (type) > 0
10212 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10213 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10214 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10215 0))))
d7e74731 10216 buf.puts (" const");
94af9270
KS
10217 }
10218 }
10219
d7e74731 10220 const std::string &intermediate_name = buf.string ();
94af9270
KS
10221
10222 if (cu->language == language_cplus)
34a68019 10223 canonical_name
322a8516 10224 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10225 objfile);
34a68019
TT
10226
10227 /* If we only computed INTERMEDIATE_NAME, or if
10228 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10229 intern it. */
322a8516 10230 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10231 name = objfile->intern (intermediate_name);
34a68019
TT
10232 else
10233 name = canonical_name;
94af9270
KS
10234 }
10235 }
10236
10237 return name;
10238}
10239
0114d602
DJ
10240/* Return the fully qualified name of DIE, based on its DW_AT_name.
10241 If scope qualifiers are appropriate they will be added. The result
34a68019 10242 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10243 not have a name. NAME may either be from a previous call to
10244 dwarf2_name or NULL.
10245
9c37b5ae 10246 The output string will be canonicalized (if C++). */
0114d602
DJ
10247
10248static const char *
15d034d0 10249dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10250{
94af9270
KS
10251 return dwarf2_compute_name (name, die, cu, 0);
10252}
0114d602 10253
94af9270
KS
10254/* Construct a physname for the given DIE in CU. NAME may either be
10255 from a previous call to dwarf2_name or NULL. The result will be
10256 allocated on the objfile_objstack or NULL if the DIE does not have a
10257 name.
0114d602 10258
9c37b5ae 10259 The output string will be canonicalized (if C++). */
0114d602 10260
94af9270 10261static const char *
15d034d0 10262dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10263{
518817b3 10264 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10265 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10266 int need_copy = 1;
10267
10268 /* In this case dwarf2_compute_name is just a shortcut not building anything
10269 on its own. */
10270 if (!die_needs_namespace (die, cu))
10271 return dwarf2_compute_name (name, die, cu, 1);
10272
73b9be8b 10273 mangled = dw2_linkage_name (die, cu);
900e11f9 10274
e98c9e7c
TT
10275 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10276 See https://github.com/rust-lang/rust/issues/32925. */
10277 if (cu->language == language_rust && mangled != NULL
10278 && strchr (mangled, '{') != NULL)
10279 mangled = NULL;
10280
900e11f9
JK
10281 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10282 has computed. */
791afaa2 10283 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10284 if (mangled != NULL)
900e11f9 10285 {
900e11f9 10286
59cc4834
JB
10287 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10288 {
10289 /* Do nothing (do not demangle the symbol name). */
10290 }
10291 else if (cu->language == language_go)
a766d390 10292 {
5e2db402
TT
10293 /* This is a lie, but we already lie to the caller new_symbol.
10294 new_symbol assumes we return the mangled name.
a766d390 10295 This just undoes that lie until things are cleaned up. */
a766d390
DE
10296 }
10297 else
10298 {
0eb876f5
JB
10299 /* Use DMGL_RET_DROP for C++ template functions to suppress
10300 their return type. It is easier for GDB users to search
10301 for such functions as `name(params)' than `long name(params)'.
10302 In such case the minimal symbol names do not match the full
10303 symbol names but for template functions there is never a need
10304 to look up their definition from their declaration so
10305 the only disadvantage remains the minimal symbol variant
10306 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10307 demangled.reset (gdb_demangle (mangled,
10308 (DMGL_PARAMS | DMGL_ANSI
10309 | DMGL_RET_DROP)));
a766d390 10310 }
900e11f9 10311 if (demangled)
791afaa2 10312 canon = demangled.get ();
900e11f9
JK
10313 else
10314 {
10315 canon = mangled;
10316 need_copy = 0;
10317 }
10318 }
10319
10320 if (canon == NULL || check_physname)
10321 {
10322 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10323
10324 if (canon != NULL && strcmp (physname, canon) != 0)
10325 {
10326 /* It may not mean a bug in GDB. The compiler could also
10327 compute DW_AT_linkage_name incorrectly. But in such case
10328 GDB would need to be bug-to-bug compatible. */
10329
b98664d3 10330 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10331 "(from linkage <%s>) - DIE at %s [in module %s]"),
10332 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10333 objfile_name (objfile));
900e11f9
JK
10334
10335 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10336 is available here - over computed PHYSNAME. It is safer
10337 against both buggy GDB and buggy compilers. */
10338
10339 retval = canon;
10340 }
10341 else
10342 {
10343 retval = physname;
10344 need_copy = 0;
10345 }
10346 }
10347 else
10348 retval = canon;
10349
10350 if (need_copy)
be1e3d3e 10351 retval = objfile->intern (retval);
900e11f9 10352
900e11f9 10353 return retval;
0114d602
DJ
10354}
10355
74921315
KS
10356/* Inspect DIE in CU for a namespace alias. If one exists, record
10357 a new symbol for it.
10358
10359 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10360
10361static int
10362read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10363{
10364 struct attribute *attr;
10365
10366 /* If the die does not have a name, this is not a namespace
10367 alias. */
10368 attr = dwarf2_attr (die, DW_AT_name, cu);
10369 if (attr != NULL)
10370 {
10371 int num;
10372 struct die_info *d = die;
10373 struct dwarf2_cu *imported_cu = cu;
10374
10375 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10376 keep inspecting DIEs until we hit the underlying import. */
10377#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10378 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10379 {
10380 attr = dwarf2_attr (d, DW_AT_import, cu);
10381 if (attr == NULL)
10382 break;
10383
10384 d = follow_die_ref (d, attr, &imported_cu);
10385 if (d->tag != DW_TAG_imported_declaration)
10386 break;
10387 }
10388
10389 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10390 {
b98664d3 10391 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10392 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10393 return 0;
10394 }
10395
10396 if (attr != NULL)
10397 {
10398 struct type *type;
9c541725 10399 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10400
9c541725 10401 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10402 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10403 {
10404 /* This declaration is a global namespace alias. Add
10405 a symbol for it whose type is the aliased namespace. */
10406 new_symbol (die, type, cu);
10407 return 1;
10408 }
10409 }
10410 }
10411
10412 return 0;
10413}
10414
22cee43f 10415/* Return the using directives repository (global or local?) to use in the
804d2729 10416 current context for CU.
22cee43f
PMR
10417
10418 For Ada, imported declarations can materialize renamings, which *may* be
10419 global. However it is impossible (for now?) in DWARF to distinguish
10420 "external" imported declarations and "static" ones. As all imported
10421 declarations seem to be static in all other languages, make them all CU-wide
10422 global only in Ada. */
10423
10424static struct using_direct **
804d2729 10425using_directives (struct dwarf2_cu *cu)
22cee43f 10426{
c24bdb02
KS
10427 if (cu->language == language_ada
10428 && cu->get_builder ()->outermost_context_p ())
10429 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10430 else
c24bdb02 10431 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10432}
10433
27aa8d6a
SW
10434/* Read the import statement specified by the given die and record it. */
10435
10436static void
10437read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10438{
518817b3 10439 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10440 struct attribute *import_attr;
32019081 10441 struct die_info *imported_die, *child_die;
de4affc9 10442 struct dwarf2_cu *imported_cu;
27aa8d6a 10443 const char *imported_name;
794684b6 10444 const char *imported_name_prefix;
13387711
SW
10445 const char *canonical_name;
10446 const char *import_alias;
10447 const char *imported_declaration = NULL;
794684b6 10448 const char *import_prefix;
eb1e02fd 10449 std::vector<const char *> excludes;
13387711 10450
27aa8d6a
SW
10451 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10452 if (import_attr == NULL)
10453 {
b98664d3 10454 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10455 dwarf_tag_name (die->tag));
10456 return;
10457 }
10458
de4affc9
CC
10459 imported_cu = cu;
10460 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10461 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10462 if (imported_name == NULL)
10463 {
10464 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10465
10466 The import in the following code:
10467 namespace A
10468 {
10469 typedef int B;
10470 }
10471
10472 int main ()
10473 {
10474 using A::B;
10475 B b;
10476 return b;
10477 }
10478
10479 ...
10480 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10481 <52> DW_AT_decl_file : 1
10482 <53> DW_AT_decl_line : 6
10483 <54> DW_AT_import : <0x75>
10484 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10485 <59> DW_AT_name : B
10486 <5b> DW_AT_decl_file : 1
10487 <5c> DW_AT_decl_line : 2
10488 <5d> DW_AT_type : <0x6e>
10489 ...
10490 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10491 <76> DW_AT_byte_size : 4
10492 <77> DW_AT_encoding : 5 (signed)
10493
10494 imports the wrong die ( 0x75 instead of 0x58 ).
10495 This case will be ignored until the gcc bug is fixed. */
10496 return;
10497 }
10498
82856980
SW
10499 /* Figure out the local name after import. */
10500 import_alias = dwarf2_name (die, cu);
27aa8d6a 10501
794684b6
SW
10502 /* Figure out where the statement is being imported to. */
10503 import_prefix = determine_prefix (die, cu);
10504
10505 /* Figure out what the scope of the imported die is and prepend it
10506 to the name of the imported die. */
de4affc9 10507 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10508
f55ee35c
JK
10509 if (imported_die->tag != DW_TAG_namespace
10510 && imported_die->tag != DW_TAG_module)
794684b6 10511 {
13387711
SW
10512 imported_declaration = imported_name;
10513 canonical_name = imported_name_prefix;
794684b6 10514 }
13387711 10515 else if (strlen (imported_name_prefix) > 0)
12aaed36 10516 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10517 imported_name_prefix,
10518 (cu->language == language_d ? "." : "::"),
10519 imported_name, (char *) NULL);
13387711
SW
10520 else
10521 canonical_name = imported_name;
794684b6 10522
32019081
JK
10523 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10524 for (child_die = die->child; child_die && child_die->tag;
10525 child_die = sibling_die (child_die))
10526 {
10527 /* DWARF-4: A Fortran use statement with a “rename list” may be
10528 represented by an imported module entry with an import attribute
10529 referring to the module and owned entries corresponding to those
10530 entities that are renamed as part of being imported. */
10531
10532 if (child_die->tag != DW_TAG_imported_declaration)
10533 {
b98664d3 10534 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10535 "- DIE at %s [in module %s]"),
10536 sect_offset_str (child_die->sect_off),
10537 objfile_name (objfile));
32019081
JK
10538 continue;
10539 }
10540
10541 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10542 if (import_attr == NULL)
10543 {
b98664d3 10544 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10545 dwarf_tag_name (child_die->tag));
10546 continue;
10547 }
10548
10549 imported_cu = cu;
10550 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10551 &imported_cu);
10552 imported_name = dwarf2_name (imported_die, imported_cu);
10553 if (imported_name == NULL)
10554 {
b98664d3 10555 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10556 "imported name - DIE at %s [in module %s]"),
10557 sect_offset_str (child_die->sect_off),
10558 objfile_name (objfile));
32019081
JK
10559 continue;
10560 }
10561
eb1e02fd 10562 excludes.push_back (imported_name);
32019081
JK
10563
10564 process_die (child_die, cu);
10565 }
10566
804d2729 10567 add_using_directive (using_directives (cu),
22cee43f
PMR
10568 import_prefix,
10569 canonical_name,
10570 import_alias,
10571 imported_declaration,
10572 excludes,
10573 0,
10574 &objfile->objfile_obstack);
27aa8d6a
SW
10575}
10576
5230b05a
WT
10577/* ICC<14 does not output the required DW_AT_declaration on incomplete
10578 types, but gives them a size of zero. Starting with version 14,
10579 ICC is compatible with GCC. */
10580
9068261f 10581static bool
5230b05a
WT
10582producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10583{
10584 if (!cu->checked_producer)
10585 check_producer (cu);
10586
10587 return cu->producer_is_icc_lt_14;
10588}
10589
eb77c9df
AB
10590/* ICC generates a DW_AT_type for C void functions. This was observed on
10591 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10592 which says that void functions should not have a DW_AT_type. */
10593
10594static bool
10595producer_is_icc (struct dwarf2_cu *cu)
10596{
10597 if (!cu->checked_producer)
10598 check_producer (cu);
10599
10600 return cu->producer_is_icc;
10601}
10602
1b80a9fa
JK
10603/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10604 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10605 this, it was first present in GCC release 4.3.0. */
10606
9068261f 10607static bool
1b80a9fa
JK
10608producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10609{
10610 if (!cu->checked_producer)
10611 check_producer (cu);
10612
10613 return cu->producer_is_gcc_lt_4_3;
10614}
10615
d721ba37
PA
10616static file_and_directory
10617find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10618{
d721ba37
PA
10619 file_and_directory res;
10620
9291a0cd
TT
10621 /* Find the filename. Do not use dwarf2_name here, since the filename
10622 is not a source language identifier. */
d721ba37
PA
10623 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10624 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10625
d721ba37
PA
10626 if (res.comp_dir == NULL
10627 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10628 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10629 {
d721ba37
PA
10630 res.comp_dir_storage = ldirname (res.name);
10631 if (!res.comp_dir_storage.empty ())
10632 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10633 }
d721ba37 10634 if (res.comp_dir != NULL)
9291a0cd
TT
10635 {
10636 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10637 directory, get rid of it. */
d721ba37 10638 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10639
d721ba37
PA
10640 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10641 res.comp_dir = cp + 1;
9291a0cd
TT
10642 }
10643
d721ba37
PA
10644 if (res.name == NULL)
10645 res.name = "<unknown>";
10646
10647 return res;
9291a0cd
TT
10648}
10649
f4dc4d17
DE
10650/* Handle DW_AT_stmt_list for a compilation unit.
10651 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10652 COMP_DIR is the compilation directory. LOWPC is passed to
10653 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10654
10655static void
10656handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10657 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10658{
518817b3
SM
10659 struct dwarf2_per_objfile *dwarf2_per_objfile
10660 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10661 struct attribute *attr;
527f3840
JK
10662 struct line_header line_header_local;
10663 hashval_t line_header_local_hash;
527f3840
JK
10664 void **slot;
10665 int decode_mapping;
2ab95328 10666
f4dc4d17
DE
10667 gdb_assert (! cu->per_cu->is_debug_types);
10668
2ab95328 10669 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10670 if (attr == NULL)
10671 return;
10672
9c541725 10673 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10674
10675 /* The line header hash table is only created if needed (it exists to
10676 prevent redundant reading of the line table for partial_units).
10677 If we're given a partial_unit, we'll need it. If we're given a
10678 compile_unit, then use the line header hash table if it's already
10679 created, but don't create one just yet. */
10680
10681 if (dwarf2_per_objfile->line_header_hash == NULL
10682 && die->tag == DW_TAG_partial_unit)
2ab95328 10683 {
527f3840 10684 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10685 .reset (htab_create_alloc (127, line_header_hash_voidp,
10686 line_header_eq_voidp,
10687 free_line_header_voidp,
10688 xcalloc, xfree));
527f3840 10689 }
2ab95328 10690
9c541725 10691 line_header_local.sect_off = line_offset;
527f3840
JK
10692 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10693 line_header_local_hash = line_header_hash (&line_header_local);
10694 if (dwarf2_per_objfile->line_header_hash != NULL)
10695 {
d15acc42 10696 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10697 &line_header_local,
10698 line_header_local_hash, NO_INSERT);
10699
10700 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10701 is not present in *SLOT (since if there is something in *SLOT then
10702 it will be for a partial_unit). */
10703 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10704 {
527f3840 10705 gdb_assert (*slot != NULL);
9a3c8263 10706 cu->line_header = (struct line_header *) *slot;
527f3840 10707 return;
dee91e82 10708 }
2ab95328 10709 }
527f3840
JK
10710
10711 /* dwarf_decode_line_header does not yet provide sufficient information.
10712 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10713 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10714 if (lh == NULL)
527f3840 10715 return;
4c8aa72d
PA
10716
10717 cu->line_header = lh.release ();
10718 cu->line_header_die_owner = die;
527f3840
JK
10719
10720 if (dwarf2_per_objfile->line_header_hash == NULL)
10721 slot = NULL;
10722 else
10723 {
d15acc42 10724 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10725 &line_header_local,
10726 line_header_local_hash, INSERT);
10727 gdb_assert (slot != NULL);
10728 }
10729 if (slot != NULL && *slot == NULL)
10730 {
10731 /* This newly decoded line number information unit will be owned
10732 by line_header_hash hash table. */
10733 *slot = cu->line_header;
4c8aa72d 10734 cu->line_header_die_owner = NULL;
527f3840
JK
10735 }
10736 else
10737 {
10738 /* We cannot free any current entry in (*slot) as that struct line_header
10739 may be already used by multiple CUs. Create only temporary decoded
10740 line_header for this CU - it may happen at most once for each line
10741 number information unit. And if we're not using line_header_hash
10742 then this is what we want as well. */
10743 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10744 }
10745 decode_mapping = (die->tag != DW_TAG_partial_unit);
10746 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10747 decode_mapping);
fff8551c 10748
2ab95328
TT
10749}
10750
95554aad 10751/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10752
c906108c 10753static void
e7c27a73 10754read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10755{
518817b3
SM
10756 struct dwarf2_per_objfile *dwarf2_per_objfile
10757 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10758 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10759 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10760 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10761 CORE_ADDR highpc = ((CORE_ADDR) 0);
10762 struct attribute *attr;
c906108c 10763 struct die_info *child_die;
e142c38c 10764 CORE_ADDR baseaddr;
6e70227d 10765
380618d6 10766 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10767 baseaddr = objfile->text_section_offset ();
c906108c 10768
fae299cd 10769 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10770
10771 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10772 from finish_block. */
2acceee2 10773 if (lowpc == ((CORE_ADDR) -1))
c906108c 10774 lowpc = highpc;
3e29f34a 10775 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10776
d721ba37 10777 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10778
f4b8a18d
KW
10779 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10780 standardised yet. As a workaround for the language detection we fall
10781 back to the DW_AT_producer string. */
10782 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10783 cu->language = language_opencl;
10784
3019eac3
DE
10785 /* Similar hack for Go. */
10786 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10787 set_cu_language (DW_LANG_Go, cu);
10788
c24bdb02 10789 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10790
10791 /* Decode line number information if present. We do this before
10792 processing child DIEs, so that the line header table is available
10793 for DW_AT_decl_file. */
d721ba37 10794 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10795
10796 /* Process all dies in compilation unit. */
10797 if (die->child != NULL)
10798 {
10799 child_die = die->child;
10800 while (child_die && child_die->tag)
10801 {
10802 process_die (child_die, cu);
10803 child_die = sibling_die (child_die);
10804 }
10805 }
10806
10807 /* Decode macro information, if present. Dwarf 2 macro information
10808 refers to information in the line number info statement program
10809 header, so we can only read it if we've read the header
10810 successfully. */
0af92d60
JK
10811 attr = dwarf2_attr (die, DW_AT_macros, cu);
10812 if (attr == NULL)
10813 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10814 if (attr && cu->line_header)
10815 {
10816 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10817 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10818
43f3e411 10819 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10820 }
10821 else
10822 {
10823 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10824 if (attr && cu->line_header)
10825 {
10826 unsigned int macro_offset = DW_UNSND (attr);
10827
43f3e411 10828 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10829 }
10830 }
3019eac3
DE
10831}
10832
c24bdb02
KS
10833void
10834dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10835{
f4dc4d17
DE
10836 struct type_unit_group *tu_group;
10837 int first_time;
3019eac3 10838 struct attribute *attr;
9c541725 10839 unsigned int i;
0186c6a7 10840 struct signatured_type *sig_type;
3019eac3 10841
f4dc4d17 10842 gdb_assert (per_cu->is_debug_types);
0186c6a7 10843 sig_type = (struct signatured_type *) per_cu;
3019eac3 10844
c24bdb02 10845 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10846
f4dc4d17 10847 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10848 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10849 if (sig_type->type_unit_group == NULL)
c24bdb02 10850 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10851 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10852
10853 /* If we've already processed this stmt_list there's no real need to
10854 do it again, we could fake it and just recreate the part we need
10855 (file name,index -> symtab mapping). If data shows this optimization
10856 is useful we can do it then. */
43f3e411 10857 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10858
10859 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10860 debug info. */
fff8551c 10861 line_header_up lh;
f4dc4d17 10862 if (attr != NULL)
3019eac3 10863 {
9c541725 10864 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10865 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10866 }
10867 if (lh == NULL)
10868 {
10869 if (first_time)
c24bdb02 10870 start_symtab ("", NULL, 0);
f4dc4d17
DE
10871 else
10872 {
10873 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10874 gdb_assert (m_builder == nullptr);
804d2729 10875 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10876 m_builder.reset (new struct buildsym_compunit
10877 (COMPUNIT_OBJFILE (cust), "",
10878 COMPUNIT_DIRNAME (cust),
10879 compunit_language (cust),
10880 0, cust));
f4dc4d17 10881 }
f4dc4d17 10882 return;
3019eac3
DE
10883 }
10884
c24bdb02
KS
10885 line_header = lh.release ();
10886 line_header_die_owner = die;
3019eac3 10887
f4dc4d17
DE
10888 if (first_time)
10889 {
c24bdb02 10890 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10891
1fd60fc0
DE
10892 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10893 still initializing it, and our caller (a few levels up)
10894 process_full_type_unit still needs to know if this is the first
10895 time. */
10896
4ac93832
TT
10897 tu_group->symtabs
10898 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10899 struct symtab *, line_header->file_names_size ());
3019eac3 10900
7ba99d21
AT
10901 auto &file_names = line_header->file_names ();
10902 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10903 {
7ba99d21 10904 file_entry &fe = file_names[i];
c24bdb02
KS
10905 dwarf2_start_subfile (this, fe.name,
10906 fe.include_dir (line_header));
10907 buildsym_compunit *b = get_builder ();
10908 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10909 {
4c8aa72d
PA
10910 /* NOTE: start_subfile will recognize when it's been
10911 passed a file it has already seen. So we can't
10912 assume there's a simple mapping from
10913 cu->line_header->file_names to subfiles, plus
10914 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10915 b->get_current_subfile ()->symtab
10916 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10917 }
10918
c24bdb02 10919 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10920 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10921 }
10922 }
10923 else
3019eac3 10924 {
c24bdb02 10925 gdb_assert (m_builder == nullptr);
804d2729 10926 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10927 m_builder.reset (new struct buildsym_compunit
10928 (COMPUNIT_OBJFILE (cust), "",
10929 COMPUNIT_DIRNAME (cust),
10930 compunit_language (cust),
10931 0, cust));
f4dc4d17 10932
7ba99d21
AT
10933 auto &file_names = line_header->file_names ();
10934 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10935 {
7ba99d21 10936 file_entry &fe = file_names[i];
4c8aa72d 10937 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10938 }
3019eac3
DE
10939 }
10940
f4dc4d17
DE
10941 /* The main symtab is allocated last. Type units don't have DW_AT_name
10942 so they don't have a "real" (so to speak) symtab anyway.
10943 There is later code that will assign the main symtab to all symbols
10944 that don't have one. We need to handle the case of a symbol with a
10945 missing symtab (DW_AT_decl_file) anyway. */
10946}
3019eac3 10947
f4dc4d17
DE
10948/* Process DW_TAG_type_unit.
10949 For TUs we want to skip the first top level sibling if it's not the
10950 actual type being defined by this TU. In this case the first top
10951 level sibling is there to provide context only. */
3019eac3 10952
f4dc4d17
DE
10953static void
10954read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10955{
10956 struct die_info *child_die;
3019eac3 10957
f4dc4d17
DE
10958 prepare_one_comp_unit (cu, die, language_minimal);
10959
10960 /* Initialize (or reinitialize) the machinery for building symtabs.
10961 We do this before processing child DIEs, so that the line header table
10962 is available for DW_AT_decl_file. */
c24bdb02 10963 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10964
10965 if (die->child != NULL)
10966 {
10967 child_die = die->child;
10968 while (child_die && child_die->tag)
10969 {
10970 process_die (child_die, cu);
10971 child_die = sibling_die (child_die);
10972 }
10973 }
3019eac3
DE
10974}
10975\f
80626a55
DE
10976/* DWO/DWP files.
10977
10978 http://gcc.gnu.org/wiki/DebugFission
10979 http://gcc.gnu.org/wiki/DebugFissionDWP
10980
10981 To simplify handling of both DWO files ("object" files with the DWARF info)
10982 and DWP files (a file with the DWOs packaged up into one file), we treat
10983 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10984
10985static hashval_t
10986hash_dwo_file (const void *item)
10987{
9a3c8263 10988 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10989 hashval_t hash;
3019eac3 10990
a2ce51a0
DE
10991 hash = htab_hash_string (dwo_file->dwo_name);
10992 if (dwo_file->comp_dir != NULL)
10993 hash += htab_hash_string (dwo_file->comp_dir);
10994 return hash;
3019eac3
DE
10995}
10996
10997static int
10998eq_dwo_file (const void *item_lhs, const void *item_rhs)
10999{
9a3c8263
SM
11000 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11001 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11002
a2ce51a0
DE
11003 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11004 return 0;
11005 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11006 return lhs->comp_dir == rhs->comp_dir;
11007 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11008}
11009
11010/* Allocate a hash table for DWO files. */
11011
51ac9db5 11012static htab_up
298e9637 11013allocate_dwo_file_hash_table ()
3019eac3 11014{
51ac9db5
SM
11015 auto delete_dwo_file = [] (void *item)
11016 {
11017 struct dwo_file *dwo_file = (struct dwo_file *) item;
11018
11019 delete dwo_file;
11020 };
11021
bc68fb19
TT
11022 return htab_up (htab_create_alloc (41,
11023 hash_dwo_file,
11024 eq_dwo_file,
11025 delete_dwo_file,
11026 xcalloc, xfree));
3019eac3
DE
11027}
11028
80626a55
DE
11029/* Lookup DWO file DWO_NAME. */
11030
11031static void **
ed2dc618
SM
11032lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11033 const char *dwo_name,
11034 const char *comp_dir)
80626a55
DE
11035{
11036 struct dwo_file find_entry;
11037 void **slot;
11038
11039 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11040 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11041
0ac5b59e
DE
11042 find_entry.dwo_name = dwo_name;
11043 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11044 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11045 INSERT);
80626a55
DE
11046
11047 return slot;
11048}
11049
3019eac3
DE
11050static hashval_t
11051hash_dwo_unit (const void *item)
11052{
9a3c8263 11053 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11054
11055 /* This drops the top 32 bits of the id, but is ok for a hash. */
11056 return dwo_unit->signature;
11057}
11058
11059static int
11060eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11061{
9a3c8263
SM
11062 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11063 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11064
11065 /* The signature is assumed to be unique within the DWO file.
11066 So while object file CU dwo_id's always have the value zero,
11067 that's OK, assuming each object file DWO file has only one CU,
11068 and that's the rule for now. */
11069 return lhs->signature == rhs->signature;
11070}
11071
11072/* Allocate a hash table for DWO CUs,TUs.
11073 There is one of these tables for each of CUs,TUs for each DWO file. */
11074
b0b6a987 11075static htab_up
298e9637 11076allocate_dwo_unit_table ()
3019eac3
DE
11077{
11078 /* Start out with a pretty small number.
11079 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11080 return htab_up (htab_create_alloc (3,
11081 hash_dwo_unit,
11082 eq_dwo_unit,
11083 NULL, xcalloc, xfree));
3019eac3
DE
11084}
11085
19c3d4c9 11086/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11087
11088static void
19c3d4c9
DE
11089create_dwo_cu_reader (const struct die_reader_specs *reader,
11090 const gdb_byte *info_ptr,
11091 struct die_info *comp_unit_die,
c0ab21c2
TT
11092 struct dwo_file *dwo_file,
11093 struct dwo_unit *dwo_unit)
3019eac3
DE
11094{
11095 struct dwarf2_cu *cu = reader->cu;
9c541725 11096 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11097 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11098
a084a2a6
AT
11099 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11100 if (!signature.has_value ())
3019eac3 11101 {
b98664d3 11102 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11103 " its dwo_id [in module %s]"),
9d8780f0 11104 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11105 return;
11106 }
11107
3019eac3 11108 dwo_unit->dwo_file = dwo_file;
a084a2a6 11109 dwo_unit->signature = *signature;
8a0459fd 11110 dwo_unit->section = section;
9c541725 11111 dwo_unit->sect_off = sect_off;
3019eac3
DE
11112 dwo_unit->length = cu->per_cu->length;
11113
b4f54984 11114 if (dwarf_read_debug)
9d8780f0
SM
11115 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11116 sect_offset_str (sect_off),
9c541725 11117 hex_string (dwo_unit->signature));
3019eac3
DE
11118}
11119
33c5cd75 11120/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11121 Note: This function processes DWO files only, not DWP files. */
3019eac3 11122
33c5cd75 11123static void
ed2dc618 11124create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11125 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11126 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11127{
11128 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11129 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11130
96b79293 11131 section.read (objfile);
33c5cd75 11132 info_ptr = section.buffer;
3019eac3
DE
11133
11134 if (info_ptr == NULL)
33c5cd75 11135 return;
3019eac3 11136
b4f54984 11137 if (dwarf_read_debug)
19c3d4c9
DE
11138 {
11139 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11140 section.get_name (),
11141 section.get_file_name ());
19c3d4c9 11142 }
3019eac3 11143
33c5cd75 11144 end_ptr = info_ptr + section.size;
3019eac3
DE
11145 while (info_ptr < end_ptr)
11146 {
11147 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11148 struct dwo_unit read_unit {};
33c5cd75
DB
11149 struct dwo_unit *dwo_unit;
11150 void **slot;
11151 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11152
11153 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11154 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11155 per_cu.is_debug_types = 0;
33c5cd75
DB
11156 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11157 per_cu.section = &section;
11158
c0ab21c2
TT
11159 cutu_reader reader (&per_cu, cu, &dwo_file);
11160 if (!reader.dummy_p)
11161 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11162 &dwo_file, &read_unit);
33c5cd75
DB
11163 info_ptr += per_cu.length;
11164
11165 // If the unit could not be parsed, skip it.
c0ab21c2 11166 if (read_unit.dwo_file == NULL)
33c5cd75 11167 continue;
3019eac3 11168
33c5cd75 11169 if (cus_htab == NULL)
298e9637 11170 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11171
33c5cd75 11172 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11173 *dwo_unit = read_unit;
b0b6a987 11174 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11175 gdb_assert (slot != NULL);
11176 if (*slot != NULL)
19c3d4c9 11177 {
33c5cd75
DB
11178 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11179 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11180
b98664d3 11181 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11182 " the entry at offset %s, signature %s"),
11183 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11184 hex_string (dwo_unit->signature));
19c3d4c9 11185 }
33c5cd75 11186 *slot = (void *)dwo_unit;
3019eac3 11187 }
3019eac3
DE
11188}
11189
80626a55
DE
11190/* DWP file .debug_{cu,tu}_index section format:
11191 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11192
d2415c6c
DE
11193 DWP Version 1:
11194
80626a55
DE
11195 Both index sections have the same format, and serve to map a 64-bit
11196 signature to a set of section numbers. Each section begins with a header,
11197 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11198 indexes, and a pool of 32-bit section numbers. The index sections will be
11199 aligned at 8-byte boundaries in the file.
11200
d2415c6c
DE
11201 The index section header consists of:
11202
11203 V, 32 bit version number
11204 -, 32 bits unused
11205 N, 32 bit number of compilation units or type units in the index
11206 M, 32 bit number of slots in the hash table
80626a55 11207
d2415c6c 11208 Numbers are recorded using the byte order of the application binary.
80626a55 11209
d2415c6c
DE
11210 The hash table begins at offset 16 in the section, and consists of an array
11211 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11212 order of the application binary). Unused slots in the hash table are 0.
11213 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11214
d2415c6c
DE
11215 The parallel table begins immediately after the hash table
11216 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11217 array of 32-bit indexes (using the byte order of the application binary),
11218 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11219 table contains a 32-bit index into the pool of section numbers. For unused
11220 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11221
73869dc2
DE
11222 The pool of section numbers begins immediately following the hash table
11223 (at offset 16 + 12 * M from the beginning of the section). The pool of
11224 section numbers consists of an array of 32-bit words (using the byte order
11225 of the application binary). Each item in the array is indexed starting
11226 from 0. The hash table entry provides the index of the first section
11227 number in the set. Additional section numbers in the set follow, and the
11228 set is terminated by a 0 entry (section number 0 is not used in ELF).
11229
11230 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11231 section must be the first entry in the set, and the .debug_abbrev.dwo must
11232 be the second entry. Other members of the set may follow in any order.
11233
11234 ---
11235
11236 DWP Version 2:
11237
11238 DWP Version 2 combines all the .debug_info, etc. sections into one,
11239 and the entries in the index tables are now offsets into these sections.
11240 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11241 section.
11242
11243 Index Section Contents:
11244 Header
11245 Hash Table of Signatures dwp_hash_table.hash_table
11246 Parallel Table of Indices dwp_hash_table.unit_table
11247 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11248 Table of Section Sizes dwp_hash_table.v2.sizes
11249
11250 The index section header consists of:
11251
11252 V, 32 bit version number
11253 L, 32 bit number of columns in the table of section offsets
11254 N, 32 bit number of compilation units or type units in the index
11255 M, 32 bit number of slots in the hash table
11256
11257 Numbers are recorded using the byte order of the application binary.
11258
11259 The hash table has the same format as version 1.
11260 The parallel table of indices has the same format as version 1,
11261 except that the entries are origin-1 indices into the table of sections
11262 offsets and the table of section sizes.
11263
11264 The table of offsets begins immediately following the parallel table
11265 (at offset 16 + 12 * M from the beginning of the section). The table is
11266 a two-dimensional array of 32-bit words (using the byte order of the
11267 application binary), with L columns and N+1 rows, in row-major order.
11268 Each row in the array is indexed starting from 0. The first row provides
11269 a key to the remaining rows: each column in this row provides an identifier
11270 for a debug section, and the offsets in the same column of subsequent rows
11271 refer to that section. The section identifiers are:
11272
11273 DW_SECT_INFO 1 .debug_info.dwo
11274 DW_SECT_TYPES 2 .debug_types.dwo
11275 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11276 DW_SECT_LINE 4 .debug_line.dwo
11277 DW_SECT_LOC 5 .debug_loc.dwo
11278 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11279 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11280 DW_SECT_MACRO 8 .debug_macro.dwo
11281
11282 The offsets provided by the CU and TU index sections are the base offsets
11283 for the contributions made by each CU or TU to the corresponding section
11284 in the package file. Each CU and TU header contains an abbrev_offset
11285 field, used to find the abbreviations table for that CU or TU within the
11286 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11287 be interpreted as relative to the base offset given in the index section.
11288 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11289 should be interpreted as relative to the base offset for .debug_line.dwo,
11290 and offsets into other debug sections obtained from DWARF attributes should
11291 also be interpreted as relative to the corresponding base offset.
11292
11293 The table of sizes begins immediately following the table of offsets.
11294 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11295 with L columns and N rows, in row-major order. Each row in the array is
11296 indexed starting from 1 (row 0 is shared by the two tables).
11297
11298 ---
11299
11300 Hash table lookup is handled the same in version 1 and 2:
11301
11302 We assume that N and M will not exceed 2^32 - 1.
11303 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11304
d2415c6c
DE
11305 Given a 64-bit compilation unit signature or a type signature S, an entry
11306 in the hash table is located as follows:
80626a55 11307
d2415c6c
DE
11308 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11309 the low-order k bits all set to 1.
80626a55 11310
d2415c6c 11311 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11312
d2415c6c
DE
11313 3) If the hash table entry at index H matches the signature, use that
11314 entry. If the hash table entry at index H is unused (all zeroes),
11315 terminate the search: the signature is not present in the table.
80626a55 11316
d2415c6c 11317 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11318
d2415c6c 11319 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11320 to stop at an unused slot or find the match. */
80626a55
DE
11321
11322/* Create a hash table to map DWO IDs to their CU/TU entry in
11323 .debug_{info,types}.dwo in DWP_FILE.
11324 Returns NULL if there isn't one.
11325 Note: This function processes DWP files only, not DWO files. */
11326
11327static struct dwp_hash_table *
ed2dc618
SM
11328create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11329 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11330{
11331 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11332 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11333 const gdb_byte *index_ptr, *index_end;
80626a55 11334 struct dwarf2_section_info *index;
73869dc2 11335 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11336 struct dwp_hash_table *htab;
11337
11338 if (is_debug_types)
11339 index = &dwp_file->sections.tu_index;
11340 else
11341 index = &dwp_file->sections.cu_index;
11342
96b79293 11343 if (index->empty ())
80626a55 11344 return NULL;
96b79293 11345 index->read (objfile);
80626a55
DE
11346
11347 index_ptr = index->buffer;
11348 index_end = index_ptr + index->size;
11349
11350 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11351 index_ptr += 4;
11352 if (version == 2)
11353 nr_columns = read_4_bytes (dbfd, index_ptr);
11354 else
11355 nr_columns = 0;
11356 index_ptr += 4;
80626a55
DE
11357 nr_units = read_4_bytes (dbfd, index_ptr);
11358 index_ptr += 4;
11359 nr_slots = read_4_bytes (dbfd, index_ptr);
11360 index_ptr += 4;
11361
73869dc2 11362 if (version != 1 && version != 2)
80626a55 11363 {
21aa081e 11364 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11365 " [in module %s]"),
21aa081e 11366 pulongest (version), dwp_file->name);
80626a55
DE
11367 }
11368 if (nr_slots != (nr_slots & -nr_slots))
11369 {
21aa081e 11370 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11371 " is not power of 2 [in module %s]"),
21aa081e 11372 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11373 }
11374
11375 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11376 htab->version = version;
11377 htab->nr_columns = nr_columns;
80626a55
DE
11378 htab->nr_units = nr_units;
11379 htab->nr_slots = nr_slots;
11380 htab->hash_table = index_ptr;
11381 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11382
11383 /* Exit early if the table is empty. */
11384 if (nr_slots == 0 || nr_units == 0
11385 || (version == 2 && nr_columns == 0))
11386 {
11387 /* All must be zero. */
11388 if (nr_slots != 0 || nr_units != 0
11389 || (version == 2 && nr_columns != 0))
11390 {
b98664d3 11391 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11392 " all zero [in modules %s]"),
11393 dwp_file->name);
11394 }
11395 return htab;
11396 }
11397
11398 if (version == 1)
11399 {
11400 htab->section_pool.v1.indices =
11401 htab->unit_table + sizeof (uint32_t) * nr_slots;
11402 /* It's harder to decide whether the section is too small in v1.
11403 V1 is deprecated anyway so we punt. */
11404 }
11405 else
11406 {
11407 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11408 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11409 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11410 /* Reverse map for error checking. */
11411 int ids_seen[DW_SECT_MAX + 1];
11412 int i;
11413
11414 if (nr_columns < 2)
11415 {
11416 error (_("Dwarf Error: bad DWP hash table, too few columns"
11417 " in section table [in module %s]"),
11418 dwp_file->name);
11419 }
11420 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11421 {
11422 error (_("Dwarf Error: bad DWP hash table, too many columns"
11423 " in section table [in module %s]"),
11424 dwp_file->name);
11425 }
04fd5eed
GB
11426 memset (ids, 255, sizeof_ids);
11427 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11428 for (i = 0; i < nr_columns; ++i)
11429 {
11430 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11431
11432 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11433 {
11434 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11435 " in section table [in module %s]"),
11436 id, dwp_file->name);
11437 }
11438 if (ids_seen[id] != -1)
11439 {
11440 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11441 " id %d in section table [in module %s]"),
11442 id, dwp_file->name);
11443 }
11444 ids_seen[id] = i;
11445 ids[i] = id;
11446 }
11447 /* Must have exactly one info or types section. */
11448 if (((ids_seen[DW_SECT_INFO] != -1)
11449 + (ids_seen[DW_SECT_TYPES] != -1))
11450 != 1)
11451 {
11452 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11453 " DWO info/types section [in module %s]"),
11454 dwp_file->name);
11455 }
11456 /* Must have an abbrev section. */
11457 if (ids_seen[DW_SECT_ABBREV] == -1)
11458 {
11459 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11460 " section [in module %s]"),
11461 dwp_file->name);
11462 }
11463 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11464 htab->section_pool.v2.sizes =
11465 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11466 * nr_units * nr_columns);
11467 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11468 * nr_units * nr_columns))
11469 > index_end)
11470 {
11471 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11472 " [in module %s]"),
11473 dwp_file->name);
11474 }
11475 }
80626a55
DE
11476
11477 return htab;
11478}
11479
11480/* Update SECTIONS with the data from SECTP.
11481
11482 This function is like the other "locate" section routines that are
11483 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11484 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11485
11486 The result is non-zero for success, or zero if an error was found. */
11487
11488static int
73869dc2
DE
11489locate_v1_virtual_dwo_sections (asection *sectp,
11490 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11491{
11492 const struct dwop_section_names *names = &dwop_section_names;
11493
11494 if (section_is_p (sectp->name, &names->abbrev_dwo))
11495 {
11496 /* There can be only one. */
049412e3 11497 if (sections->abbrev.s.section != NULL)
80626a55 11498 return 0;
049412e3 11499 sections->abbrev.s.section = sectp;
fd361982 11500 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11501 }
11502 else if (section_is_p (sectp->name, &names->info_dwo)
11503 || section_is_p (sectp->name, &names->types_dwo))
11504 {
11505 /* There can be only one. */
049412e3 11506 if (sections->info_or_types.s.section != NULL)
80626a55 11507 return 0;
049412e3 11508 sections->info_or_types.s.section = sectp;
fd361982 11509 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11510 }
11511 else if (section_is_p (sectp->name, &names->line_dwo))
11512 {
11513 /* There can be only one. */
049412e3 11514 if (sections->line.s.section != NULL)
80626a55 11515 return 0;
049412e3 11516 sections->line.s.section = sectp;
fd361982 11517 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11518 }
11519 else if (section_is_p (sectp->name, &names->loc_dwo))
11520 {
11521 /* There can be only one. */
049412e3 11522 if (sections->loc.s.section != NULL)
80626a55 11523 return 0;
049412e3 11524 sections->loc.s.section = sectp;
fd361982 11525 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11526 }
11527 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11528 {
11529 /* There can be only one. */
049412e3 11530 if (sections->macinfo.s.section != NULL)
80626a55 11531 return 0;
049412e3 11532 sections->macinfo.s.section = sectp;
fd361982 11533 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11534 }
11535 else if (section_is_p (sectp->name, &names->macro_dwo))
11536 {
11537 /* There can be only one. */
049412e3 11538 if (sections->macro.s.section != NULL)
80626a55 11539 return 0;
049412e3 11540 sections->macro.s.section = sectp;
fd361982 11541 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11542 }
11543 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11544 {
11545 /* There can be only one. */
049412e3 11546 if (sections->str_offsets.s.section != NULL)
80626a55 11547 return 0;
049412e3 11548 sections->str_offsets.s.section = sectp;
fd361982 11549 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11550 }
11551 else
11552 {
11553 /* No other kind of section is valid. */
11554 return 0;
11555 }
11556
11557 return 1;
11558}
11559
73869dc2
DE
11560/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11561 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11562 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11563 This is for DWP version 1 files. */
80626a55
DE
11564
11565static struct dwo_unit *
ed2dc618
SM
11566create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11567 struct dwp_file *dwp_file,
73869dc2
DE
11568 uint32_t unit_index,
11569 const char *comp_dir,
11570 ULONGEST signature, int is_debug_types)
80626a55
DE
11571{
11572 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11573 const struct dwp_hash_table *dwp_htab =
11574 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11575 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11576 const char *kind = is_debug_types ? "TU" : "CU";
11577 struct dwo_file *dwo_file;
11578 struct dwo_unit *dwo_unit;
73869dc2 11579 struct virtual_v1_dwo_sections sections;
80626a55 11580 void **dwo_file_slot;
80626a55
DE
11581 int i;
11582
73869dc2
DE
11583 gdb_assert (dwp_file->version == 1);
11584
b4f54984 11585 if (dwarf_read_debug)
80626a55 11586 {
73869dc2 11587 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11588 kind,
73869dc2 11589 pulongest (unit_index), hex_string (signature),
80626a55
DE
11590 dwp_file->name);
11591 }
11592
19ac8c2e 11593 /* Fetch the sections of this DWO unit.
80626a55
DE
11594 Put a limit on the number of sections we look for so that bad data
11595 doesn't cause us to loop forever. */
11596
73869dc2 11597#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11598 (1 /* .debug_info or .debug_types */ \
11599 + 1 /* .debug_abbrev */ \
11600 + 1 /* .debug_line */ \
11601 + 1 /* .debug_loc */ \
11602 + 1 /* .debug_str_offsets */ \
19ac8c2e 11603 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11604 + 1 /* trailing zero */)
11605
11606 memset (&sections, 0, sizeof (sections));
80626a55 11607
73869dc2 11608 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11609 {
11610 asection *sectp;
11611 uint32_t section_nr =
11612 read_4_bytes (dbfd,
73869dc2
DE
11613 dwp_htab->section_pool.v1.indices
11614 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11615
11616 if (section_nr == 0)
11617 break;
11618 if (section_nr >= dwp_file->num_sections)
11619 {
11620 error (_("Dwarf Error: bad DWP hash table, section number too large"
11621 " [in module %s]"),
11622 dwp_file->name);
11623 }
11624
11625 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11626 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11627 {
11628 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11629 " [in module %s]"),
11630 dwp_file->name);
11631 }
11632 }
11633
11634 if (i < 2
96b79293
TT
11635 || sections.info_or_types.empty ()
11636 || sections.abbrev.empty ())
80626a55
DE
11637 {
11638 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11639 " [in module %s]"),
11640 dwp_file->name);
11641 }
73869dc2 11642 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11643 {
11644 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11645 " [in module %s]"),
11646 dwp_file->name);
11647 }
11648
11649 /* It's easier for the rest of the code if we fake a struct dwo_file and
11650 have dwo_unit "live" in that. At least for now.
11651
11652 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11653 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11654 file, we can combine them back into a virtual DWO file to save space
11655 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11656 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11657
791afaa2
TT
11658 std::string virtual_dwo_name =
11659 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11660 sections.abbrev.get_id (),
11661 sections.line.get_id (),
11662 sections.loc.get_id (),
11663 sections.str_offsets.get_id ());
80626a55 11664 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11665 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11666 virtual_dwo_name.c_str (),
11667 comp_dir);
80626a55
DE
11668 /* Create one if necessary. */
11669 if (*dwo_file_slot == NULL)
11670 {
b4f54984 11671 if (dwarf_read_debug)
80626a55
DE
11672 {
11673 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11674 virtual_dwo_name.c_str ());
80626a55 11675 }
51ac9db5 11676 dwo_file = new struct dwo_file;
be1e3d3e 11677 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
0ac5b59e 11678 dwo_file->comp_dir = comp_dir;
80626a55
DE
11679 dwo_file->sections.abbrev = sections.abbrev;
11680 dwo_file->sections.line = sections.line;
11681 dwo_file->sections.loc = sections.loc;
11682 dwo_file->sections.macinfo = sections.macinfo;
11683 dwo_file->sections.macro = sections.macro;
11684 dwo_file->sections.str_offsets = sections.str_offsets;
11685 /* The "str" section is global to the entire DWP file. */
11686 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11687 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11688 there's no need to record it in dwo_file.
11689 Also, we can't simply record type sections in dwo_file because
11690 we record a pointer into the vector in dwo_unit. As we collect more
11691 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11692 for it, invalidating all copies of pointers into the previous
11693 contents. */
80626a55
DE
11694 *dwo_file_slot = dwo_file;
11695 }
11696 else
11697 {
b4f54984 11698 if (dwarf_read_debug)
80626a55
DE
11699 {
11700 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11701 virtual_dwo_name.c_str ());
80626a55 11702 }
9a3c8263 11703 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11704 }
80626a55
DE
11705
11706 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11707 dwo_unit->dwo_file = dwo_file;
11708 dwo_unit->signature = signature;
8d749320
SM
11709 dwo_unit->section =
11710 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11711 *dwo_unit->section = sections.info_or_types;
57d63ce2 11712 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11713
11714 return dwo_unit;
11715}
11716
73869dc2
DE
11717/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11718 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11719 piece within that section used by a TU/CU, return a virtual section
11720 of just that piece. */
11721
11722static struct dwarf2_section_info
ed2dc618
SM
11723create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11724 struct dwarf2_section_info *section,
73869dc2
DE
11725 bfd_size_type offset, bfd_size_type size)
11726{
11727 struct dwarf2_section_info result;
11728 asection *sectp;
11729
11730 gdb_assert (section != NULL);
11731 gdb_assert (!section->is_virtual);
11732
11733 memset (&result, 0, sizeof (result));
11734 result.s.containing_section = section;
dc4ccb6f 11735 result.is_virtual = true;
73869dc2
DE
11736
11737 if (size == 0)
11738 return result;
11739
96b79293 11740 sectp = section->get_bfd_section ();
73869dc2
DE
11741
11742 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11743 bounds of the real section. This is a pretty-rare event, so just
11744 flag an error (easier) instead of a warning and trying to cope. */
11745 if (sectp == NULL
fd361982 11746 || offset + size > bfd_section_size (sectp))
73869dc2 11747 {
73869dc2
DE
11748 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11749 " in section %s [in module %s]"),
fd361982 11750 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11751 objfile_name (dwarf2_per_objfile->objfile));
11752 }
11753
11754 result.virtual_offset = offset;
11755 result.size = size;
11756 return result;
11757}
11758
11759/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11760 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11761 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11762 This is for DWP version 2 files. */
11763
11764static struct dwo_unit *
ed2dc618
SM
11765create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11766 struct dwp_file *dwp_file,
73869dc2
DE
11767 uint32_t unit_index,
11768 const char *comp_dir,
11769 ULONGEST signature, int is_debug_types)
11770{
11771 struct objfile *objfile = dwarf2_per_objfile->objfile;
11772 const struct dwp_hash_table *dwp_htab =
11773 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11774 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11775 const char *kind = is_debug_types ? "TU" : "CU";
11776 struct dwo_file *dwo_file;
11777 struct dwo_unit *dwo_unit;
11778 struct virtual_v2_dwo_sections sections;
11779 void **dwo_file_slot;
73869dc2
DE
11780 int i;
11781
11782 gdb_assert (dwp_file->version == 2);
11783
b4f54984 11784 if (dwarf_read_debug)
73869dc2
DE
11785 {
11786 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11787 kind,
11788 pulongest (unit_index), hex_string (signature),
11789 dwp_file->name);
11790 }
11791
11792 /* Fetch the section offsets of this DWO unit. */
11793
11794 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11795
11796 for (i = 0; i < dwp_htab->nr_columns; ++i)
11797 {
11798 uint32_t offset = read_4_bytes (dbfd,
11799 dwp_htab->section_pool.v2.offsets
11800 + (((unit_index - 1) * dwp_htab->nr_columns
11801 + i)
11802 * sizeof (uint32_t)));
11803 uint32_t size = read_4_bytes (dbfd,
11804 dwp_htab->section_pool.v2.sizes
11805 + (((unit_index - 1) * dwp_htab->nr_columns
11806 + i)
11807 * sizeof (uint32_t)));
11808
11809 switch (dwp_htab->section_pool.v2.section_ids[i])
11810 {
11811 case DW_SECT_INFO:
11812 case DW_SECT_TYPES:
11813 sections.info_or_types_offset = offset;
11814 sections.info_or_types_size = size;
11815 break;
11816 case DW_SECT_ABBREV:
11817 sections.abbrev_offset = offset;
11818 sections.abbrev_size = size;
11819 break;
11820 case DW_SECT_LINE:
11821 sections.line_offset = offset;
11822 sections.line_size = size;
11823 break;
11824 case DW_SECT_LOC:
11825 sections.loc_offset = offset;
11826 sections.loc_size = size;
11827 break;
11828 case DW_SECT_STR_OFFSETS:
11829 sections.str_offsets_offset = offset;
11830 sections.str_offsets_size = size;
11831 break;
11832 case DW_SECT_MACINFO:
11833 sections.macinfo_offset = offset;
11834 sections.macinfo_size = size;
11835 break;
11836 case DW_SECT_MACRO:
11837 sections.macro_offset = offset;
11838 sections.macro_size = size;
11839 break;
11840 }
11841 }
11842
11843 /* It's easier for the rest of the code if we fake a struct dwo_file and
11844 have dwo_unit "live" in that. At least for now.
11845
11846 The DWP file can be made up of a random collection of CUs and TUs.
11847 However, for each CU + set of TUs that came from the same original DWO
11848 file, we can combine them back into a virtual DWO file to save space
11849 (fewer struct dwo_file objects to allocate). Remember that for really
11850 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11851
791afaa2
TT
11852 std::string virtual_dwo_name =
11853 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11854 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11855 (long) (sections.line_size ? sections.line_offset : 0),
11856 (long) (sections.loc_size ? sections.loc_offset : 0),
11857 (long) (sections.str_offsets_size
11858 ? sections.str_offsets_offset : 0));
73869dc2 11859 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11860 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11861 virtual_dwo_name.c_str (),
11862 comp_dir);
73869dc2
DE
11863 /* Create one if necessary. */
11864 if (*dwo_file_slot == NULL)
11865 {
b4f54984 11866 if (dwarf_read_debug)
73869dc2
DE
11867 {
11868 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11869 virtual_dwo_name.c_str ());
73869dc2 11870 }
51ac9db5 11871 dwo_file = new struct dwo_file;
be1e3d3e 11872 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
73869dc2
DE
11873 dwo_file->comp_dir = comp_dir;
11874 dwo_file->sections.abbrev =
ed2dc618 11875 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11876 sections.abbrev_offset, sections.abbrev_size);
11877 dwo_file->sections.line =
ed2dc618 11878 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11879 sections.line_offset, sections.line_size);
11880 dwo_file->sections.loc =
ed2dc618 11881 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11882 sections.loc_offset, sections.loc_size);
11883 dwo_file->sections.macinfo =
ed2dc618 11884 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11885 sections.macinfo_offset, sections.macinfo_size);
11886 dwo_file->sections.macro =
ed2dc618 11887 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11888 sections.macro_offset, sections.macro_size);
11889 dwo_file->sections.str_offsets =
ed2dc618
SM
11890 create_dwp_v2_section (dwarf2_per_objfile,
11891 &dwp_file->sections.str_offsets,
73869dc2
DE
11892 sections.str_offsets_offset,
11893 sections.str_offsets_size);
11894 /* The "str" section is global to the entire DWP file. */
11895 dwo_file->sections.str = dwp_file->sections.str;
11896 /* The info or types section is assigned below to dwo_unit,
11897 there's no need to record it in dwo_file.
11898 Also, we can't simply record type sections in dwo_file because
11899 we record a pointer into the vector in dwo_unit. As we collect more
11900 types we'll grow the vector and eventually have to reallocate space
11901 for it, invalidating all copies of pointers into the previous
11902 contents. */
11903 *dwo_file_slot = dwo_file;
11904 }
11905 else
11906 {
b4f54984 11907 if (dwarf_read_debug)
73869dc2
DE
11908 {
11909 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11910 virtual_dwo_name.c_str ());
73869dc2 11911 }
9a3c8263 11912 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11913 }
73869dc2
DE
11914
11915 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11916 dwo_unit->dwo_file = dwo_file;
11917 dwo_unit->signature = signature;
8d749320
SM
11918 dwo_unit->section =
11919 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11920 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11921 is_debug_types
73869dc2
DE
11922 ? &dwp_file->sections.types
11923 : &dwp_file->sections.info,
11924 sections.info_or_types_offset,
11925 sections.info_or_types_size);
11926 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11927
11928 return dwo_unit;
11929}
11930
57d63ce2
DE
11931/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11932 Returns NULL if the signature isn't found. */
80626a55
DE
11933
11934static struct dwo_unit *
ed2dc618
SM
11935lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11936 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11937 ULONGEST signature, int is_debug_types)
80626a55 11938{
57d63ce2
DE
11939 const struct dwp_hash_table *dwp_htab =
11940 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11941 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11942 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11943 uint32_t hash = signature & mask;
11944 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11945 unsigned int i;
11946 void **slot;
870f88f7 11947 struct dwo_unit find_dwo_cu;
80626a55
DE
11948
11949 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11950 find_dwo_cu.signature = signature;
19ac8c2e 11951 slot = htab_find_slot (is_debug_types
48b490f2
TT
11952 ? dwp_file->loaded_tus.get ()
11953 : dwp_file->loaded_cus.get (),
19ac8c2e 11954 &find_dwo_cu, INSERT);
80626a55
DE
11955
11956 if (*slot != NULL)
9a3c8263 11957 return (struct dwo_unit *) *slot;
80626a55
DE
11958
11959 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11960 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11961 {
11962 ULONGEST signature_in_table;
11963
11964 signature_in_table =
57d63ce2 11965 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11966 if (signature_in_table == signature)
11967 {
57d63ce2
DE
11968 uint32_t unit_index =
11969 read_4_bytes (dbfd,
11970 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11971
73869dc2
DE
11972 if (dwp_file->version == 1)
11973 {
ed2dc618
SM
11974 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11975 dwp_file, unit_index,
73869dc2
DE
11976 comp_dir, signature,
11977 is_debug_types);
11978 }
11979 else
11980 {
ed2dc618
SM
11981 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11982 dwp_file, unit_index,
73869dc2
DE
11983 comp_dir, signature,
11984 is_debug_types);
11985 }
9a3c8263 11986 return (struct dwo_unit *) *slot;
80626a55
DE
11987 }
11988 if (signature_in_table == 0)
11989 return NULL;
11990 hash = (hash + hash2) & mask;
11991 }
11992
11993 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11994 " [in module %s]"),
11995 dwp_file->name);
11996}
11997
ab5088bf 11998/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11999 Open the file specified by FILE_NAME and hand it off to BFD for
12000 preliminary analysis. Return a newly initialized bfd *, which
12001 includes a canonicalized copy of FILE_NAME.
80626a55 12002 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12003 SEARCH_CWD is true if the current directory is to be searched.
12004 It will be searched before debug-file-directory.
13aaf454
DE
12005 If successful, the file is added to the bfd include table of the
12006 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12007 If unable to find/open the file, return NULL.
3019eac3
DE
12008 NOTE: This function is derived from symfile_bfd_open. */
12009
192b62ce 12010static gdb_bfd_ref_ptr
ed2dc618
SM
12011try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12012 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12013{
24b9144d 12014 int desc;
9c02c129
DE
12015 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12016 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12017 to debug_file_directory. */
e0cc99a6 12018 const char *search_path;
9c02c129
DE
12019 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12020
e0cc99a6 12021 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12022 if (search_cwd)
12023 {
12024 if (*debug_file_directory != '\0')
e0cc99a6
TT
12025 {
12026 search_path_holder.reset (concat (".", dirname_separator_string,
12027 debug_file_directory,
12028 (char *) NULL));
12029 search_path = search_path_holder.get ();
12030 }
6ac97d4c 12031 else
e0cc99a6 12032 search_path = ".";
6ac97d4c 12033 }
9c02c129 12034 else
e0cc99a6 12035 search_path = debug_file_directory;
3019eac3 12036
24b9144d 12037 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12038 if (is_dwp)
12039 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12040
12041 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12042 desc = openp (search_path, flags, file_name,
3019eac3
DE
12043 O_RDONLY | O_BINARY, &absolute_name);
12044 if (desc < 0)
12045 return NULL;
12046
e0cc99a6
TT
12047 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12048 gnutarget, desc));
9c02c129
DE
12049 if (sym_bfd == NULL)
12050 return NULL;
192b62ce 12051 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12052
192b62ce
TT
12053 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12054 return NULL;
3019eac3 12055
13aaf454
DE
12056 /* Success. Record the bfd as having been included by the objfile's bfd.
12057 This is important because things like demangled_names_hash lives in the
12058 objfile's per_bfd space and may have references to things like symbol
12059 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12060 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12061
3019eac3
DE
12062 return sym_bfd;
12063}
12064
ab5088bf 12065/* Try to open DWO file FILE_NAME.
3019eac3
DE
12066 COMP_DIR is the DW_AT_comp_dir attribute.
12067 The result is the bfd handle of the file.
12068 If there is a problem finding or opening the file, return NULL.
12069 Upon success, the canonicalized path of the file is stored in the bfd,
12070 same as symfile_bfd_open. */
12071
192b62ce 12072static gdb_bfd_ref_ptr
ed2dc618
SM
12073open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12074 const char *file_name, const char *comp_dir)
3019eac3 12075{
80626a55 12076 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12077 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12078 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12079
12080 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12081
12082 if (comp_dir != NULL)
12083 {
43816ebc
TT
12084 gdb::unique_xmalloc_ptr<char> path_to_try
12085 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12086
12087 /* NOTE: If comp_dir is a relative path, this will also try the
12088 search path, which seems useful. */
ed2dc618 12089 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12090 path_to_try.get (),
ed2dc618 12091 0 /*is_dwp*/,
192b62ce 12092 1 /*search_cwd*/));
3019eac3
DE
12093 if (abfd != NULL)
12094 return abfd;
12095 }
12096
12097 /* That didn't work, try debug-file-directory, which, despite its name,
12098 is a list of paths. */
12099
12100 if (*debug_file_directory == '\0')
12101 return NULL;
12102
ed2dc618
SM
12103 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12104 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12105}
12106
80626a55
DE
12107/* This function is mapped across the sections and remembers the offset and
12108 size of each of the DWO debugging sections we are interested in. */
12109
12110static void
12111dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12112{
9a3c8263 12113 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12114 const struct dwop_section_names *names = &dwop_section_names;
12115
12116 if (section_is_p (sectp->name, &names->abbrev_dwo))
12117 {
049412e3 12118 dwo_sections->abbrev.s.section = sectp;
fd361982 12119 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12120 }
12121 else if (section_is_p (sectp->name, &names->info_dwo))
12122 {
049412e3 12123 dwo_sections->info.s.section = sectp;
fd361982 12124 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12125 }
12126 else if (section_is_p (sectp->name, &names->line_dwo))
12127 {
049412e3 12128 dwo_sections->line.s.section = sectp;
fd361982 12129 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12130 }
12131 else if (section_is_p (sectp->name, &names->loc_dwo))
12132 {
049412e3 12133 dwo_sections->loc.s.section = sectp;
fd361982 12134 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12135 }
12136 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12137 {
049412e3 12138 dwo_sections->macinfo.s.section = sectp;
fd361982 12139 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12140 }
12141 else if (section_is_p (sectp->name, &names->macro_dwo))
12142 {
049412e3 12143 dwo_sections->macro.s.section = sectp;
fd361982 12144 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12145 }
12146 else if (section_is_p (sectp->name, &names->str_dwo))
12147 {
049412e3 12148 dwo_sections->str.s.section = sectp;
fd361982 12149 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12150 }
12151 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12152 {
049412e3 12153 dwo_sections->str_offsets.s.section = sectp;
fd361982 12154 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12155 }
12156 else if (section_is_p (sectp->name, &names->types_dwo))
12157 {
12158 struct dwarf2_section_info type_section;
12159
12160 memset (&type_section, 0, sizeof (type_section));
049412e3 12161 type_section.s.section = sectp;
fd361982 12162 type_section.size = bfd_section_size (sectp);
fd5866f6 12163 dwo_sections->types.push_back (type_section);
80626a55
DE
12164 }
12165}
12166
ab5088bf 12167/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12168 by PER_CU. This is for the non-DWP case.
80626a55 12169 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12170
12171static struct dwo_file *
0ac5b59e
DE
12172open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12173 const char *dwo_name, const char *comp_dir)
3019eac3 12174{
ed2dc618 12175 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12176
fb1eb2f9 12177 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12178 if (dbfd == NULL)
12179 {
b4f54984 12180 if (dwarf_read_debug)
80626a55
DE
12181 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12182 return NULL;
12183 }
263db9a1 12184
51ac9db5 12185 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12186 dwo_file->dwo_name = dwo_name;
12187 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12188 dwo_file->dbfd = std::move (dbfd);
3019eac3 12189
fb1eb2f9 12190 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12191 &dwo_file->sections);
3019eac3 12192
18a8505e
AT
12193 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12194 dwo_file->sections.info, dwo_file->cus);
3019eac3 12195
263db9a1 12196 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12197 dwo_file->sections.types, dwo_file->tus);
3019eac3 12198
b4f54984 12199 if (dwarf_read_debug)
80626a55
DE
12200 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12201
263db9a1 12202 return dwo_file.release ();
3019eac3
DE
12203}
12204
80626a55 12205/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12206 size of each of the DWP debugging sections common to version 1 and 2 that
12207 we are interested in. */
3019eac3 12208
80626a55 12209static void
73869dc2
DE
12210dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12211 void *dwp_file_ptr)
3019eac3 12212{
9a3c8263 12213 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12214 const struct dwop_section_names *names = &dwop_section_names;
12215 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12216
80626a55 12217 /* Record the ELF section number for later lookup: this is what the
73869dc2 12218 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12219 gdb_assert (elf_section_nr < dwp_file->num_sections);
12220 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12221
80626a55
DE
12222 /* Look for specific sections that we need. */
12223 if (section_is_p (sectp->name, &names->str_dwo))
12224 {
049412e3 12225 dwp_file->sections.str.s.section = sectp;
fd361982 12226 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12227 }
12228 else if (section_is_p (sectp->name, &names->cu_index))
12229 {
049412e3 12230 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12231 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12232 }
12233 else if (section_is_p (sectp->name, &names->tu_index))
12234 {
049412e3 12235 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12236 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12237 }
12238}
3019eac3 12239
73869dc2
DE
12240/* This function is mapped across the sections and remembers the offset and
12241 size of each of the DWP version 2 debugging sections that we are interested
12242 in. This is split into a separate function because we don't know if we
12243 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12244
12245static void
12246dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12247{
9a3c8263 12248 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12249 const struct dwop_section_names *names = &dwop_section_names;
12250 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12251
12252 /* Record the ELF section number for later lookup: this is what the
12253 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12254 gdb_assert (elf_section_nr < dwp_file->num_sections);
12255 dwp_file->elf_sections[elf_section_nr] = sectp;
12256
12257 /* Look for specific sections that we need. */
12258 if (section_is_p (sectp->name, &names->abbrev_dwo))
12259 {
049412e3 12260 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12261 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12262 }
12263 else if (section_is_p (sectp->name, &names->info_dwo))
12264 {
049412e3 12265 dwp_file->sections.info.s.section = sectp;
fd361982 12266 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12267 }
12268 else if (section_is_p (sectp->name, &names->line_dwo))
12269 {
049412e3 12270 dwp_file->sections.line.s.section = sectp;
fd361982 12271 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12272 }
12273 else if (section_is_p (sectp->name, &names->loc_dwo))
12274 {
049412e3 12275 dwp_file->sections.loc.s.section = sectp;
fd361982 12276 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12277 }
12278 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12279 {
049412e3 12280 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12281 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12282 }
12283 else if (section_is_p (sectp->name, &names->macro_dwo))
12284 {
049412e3 12285 dwp_file->sections.macro.s.section = sectp;
fd361982 12286 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12287 }
12288 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12289 {
049412e3 12290 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12291 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12292 }
12293 else if (section_is_p (sectp->name, &names->types_dwo))
12294 {
049412e3 12295 dwp_file->sections.types.s.section = sectp;
fd361982 12296 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12297 }
12298}
12299
80626a55 12300/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12301
80626a55
DE
12302static hashval_t
12303hash_dwp_loaded_cutus (const void *item)
12304{
9a3c8263 12305 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12306
80626a55
DE
12307 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12308 return dwo_unit->signature;
3019eac3
DE
12309}
12310
80626a55 12311/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12312
80626a55
DE
12313static int
12314eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12315{
9a3c8263
SM
12316 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12317 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12318
80626a55
DE
12319 return dua->signature == dub->signature;
12320}
3019eac3 12321
80626a55 12322/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12323
48b490f2 12324static htab_up
298e9637 12325allocate_dwp_loaded_cutus_table ()
80626a55 12326{
48b490f2
TT
12327 return htab_up (htab_create_alloc (3,
12328 hash_dwp_loaded_cutus,
12329 eq_dwp_loaded_cutus,
12330 NULL, xcalloc, xfree));
80626a55 12331}
3019eac3 12332
ab5088bf
DE
12333/* Try to open DWP file FILE_NAME.
12334 The result is the bfd handle of the file.
12335 If there is a problem finding or opening the file, return NULL.
12336 Upon success, the canonicalized path of the file is stored in the bfd,
12337 same as symfile_bfd_open. */
12338
192b62ce 12339static gdb_bfd_ref_ptr
ed2dc618
SM
12340open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12341 const char *file_name)
ab5088bf 12342{
ed2dc618
SM
12343 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12344 1 /*is_dwp*/,
192b62ce 12345 1 /*search_cwd*/));
6ac97d4c
DE
12346 if (abfd != NULL)
12347 return abfd;
12348
12349 /* Work around upstream bug 15652.
12350 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12351 [Whether that's a "bug" is debatable, but it is getting in our way.]
12352 We have no real idea where the dwp file is, because gdb's realpath-ing
12353 of the executable's path may have discarded the needed info.
12354 [IWBN if the dwp file name was recorded in the executable, akin to
12355 .gnu_debuglink, but that doesn't exist yet.]
12356 Strip the directory from FILE_NAME and search again. */
12357 if (*debug_file_directory != '\0')
12358 {
12359 /* Don't implicitly search the current directory here.
12360 If the user wants to search "." to handle this case,
12361 it must be added to debug-file-directory. */
ed2dc618
SM
12362 return try_open_dwop_file (dwarf2_per_objfile,
12363 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12364 0 /*search_cwd*/);
12365 }
12366
12367 return NULL;
ab5088bf
DE
12368}
12369
80626a55
DE
12370/* Initialize the use of the DWP file for the current objfile.
12371 By convention the name of the DWP file is ${objfile}.dwp.
12372 The result is NULL if it can't be found. */
a766d390 12373
400174b1 12374static std::unique_ptr<struct dwp_file>
ed2dc618 12375open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12376{
12377 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12378
82bf32bc
JK
12379 /* Try to find first .dwp for the binary file before any symbolic links
12380 resolving. */
6c447423
DE
12381
12382 /* If the objfile is a debug file, find the name of the real binary
12383 file and get the name of dwp file from there. */
d721ba37 12384 std::string dwp_name;
6c447423
DE
12385 if (objfile->separate_debug_objfile_backlink != NULL)
12386 {
12387 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12388 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12389
d721ba37 12390 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12391 }
12392 else
d721ba37
PA
12393 dwp_name = objfile->original_name;
12394
12395 dwp_name += ".dwp";
80626a55 12396
ed2dc618 12397 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12398 if (dbfd == NULL
12399 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12400 {
12401 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12402 dwp_name = objfile_name (objfile);
12403 dwp_name += ".dwp";
ed2dc618 12404 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12405 }
12406
80626a55
DE
12407 if (dbfd == NULL)
12408 {
b4f54984 12409 if (dwarf_read_debug)
d721ba37 12410 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12411 return std::unique_ptr<dwp_file> ();
3019eac3 12412 }
400174b1
TT
12413
12414 const char *name = bfd_get_filename (dbfd.get ());
12415 std::unique_ptr<struct dwp_file> dwp_file
12416 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12417
0a0f4c01 12418 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12419 dwp_file->elf_sections =
12420 OBSTACK_CALLOC (&objfile->objfile_obstack,
12421 dwp_file->num_sections, asection *);
12422
400174b1
TT
12423 bfd_map_over_sections (dwp_file->dbfd.get (),
12424 dwarf2_locate_common_dwp_sections,
12425 dwp_file.get ());
80626a55 12426
400174b1
TT
12427 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12428 0);
80626a55 12429
400174b1
TT
12430 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12431 1);
80626a55 12432
73869dc2 12433 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12434 if (dwp_file->cus && dwp_file->tus
12435 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12436 {
12437 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12438 pretty bizarre. We use pulongest here because that's the established
4d65956b 12439 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12440 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12441 " TU version %s [in DWP file %s]"),
12442 pulongest (dwp_file->cus->version),
d721ba37 12443 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12444 }
08302ed2
DE
12445
12446 if (dwp_file->cus)
12447 dwp_file->version = dwp_file->cus->version;
12448 else if (dwp_file->tus)
12449 dwp_file->version = dwp_file->tus->version;
12450 else
12451 dwp_file->version = 2;
73869dc2
DE
12452
12453 if (dwp_file->version == 2)
400174b1
TT
12454 bfd_map_over_sections (dwp_file->dbfd.get (),
12455 dwarf2_locate_v2_dwp_sections,
12456 dwp_file.get ());
73869dc2 12457
298e9637
SM
12458 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12459 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12460
b4f54984 12461 if (dwarf_read_debug)
80626a55
DE
12462 {
12463 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12464 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12465 " %s CUs, %s TUs\n",
12466 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12467 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12468 }
12469
12470 return dwp_file;
3019eac3 12471}
c906108c 12472
ab5088bf
DE
12473/* Wrapper around open_and_init_dwp_file, only open it once. */
12474
12475static struct dwp_file *
ed2dc618 12476get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12477{
12478 if (! dwarf2_per_objfile->dwp_checked)
12479 {
ed2dc618
SM
12480 dwarf2_per_objfile->dwp_file
12481 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12482 dwarf2_per_objfile->dwp_checked = 1;
12483 }
400174b1 12484 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12485}
12486
80626a55
DE
12487/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12488 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12489 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12490 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12491 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12492
12493 This is called, for example, when wanting to read a variable with a
12494 complex location. Therefore we don't want to do file i/o for every call.
12495 Therefore we don't want to look for a DWO file on every call.
12496 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12497 then we check if we've already seen DWO_NAME, and only THEN do we check
12498 for a DWO file.
12499
1c658ad5 12500 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12501 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12502
3019eac3 12503static struct dwo_unit *
80626a55
DE
12504lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12505 const char *dwo_name, const char *comp_dir,
12506 ULONGEST signature, int is_debug_types)
3019eac3 12507{
ed2dc618 12508 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12509 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12510 const char *kind = is_debug_types ? "TU" : "CU";
12511 void **dwo_file_slot;
3019eac3 12512 struct dwo_file *dwo_file;
80626a55 12513 struct dwp_file *dwp_file;
cb1df416 12514
6a506a2d
DE
12515 /* First see if there's a DWP file.
12516 If we have a DWP file but didn't find the DWO inside it, don't
12517 look for the original DWO file. It makes gdb behave differently
12518 depending on whether one is debugging in the build tree. */
cf2c3c16 12519
ed2dc618 12520 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12521 if (dwp_file != NULL)
cf2c3c16 12522 {
80626a55
DE
12523 const struct dwp_hash_table *dwp_htab =
12524 is_debug_types ? dwp_file->tus : dwp_file->cus;
12525
12526 if (dwp_htab != NULL)
12527 {
12528 struct dwo_unit *dwo_cutu =
ed2dc618 12529 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12530 signature, is_debug_types);
80626a55
DE
12531
12532 if (dwo_cutu != NULL)
12533 {
b4f54984 12534 if (dwarf_read_debug)
80626a55
DE
12535 {
12536 fprintf_unfiltered (gdb_stdlog,
12537 "Virtual DWO %s %s found: @%s\n",
12538 kind, hex_string (signature),
12539 host_address_to_string (dwo_cutu));
12540 }
12541 return dwo_cutu;
12542 }
12543 }
12544 }
6a506a2d 12545 else
80626a55 12546 {
6a506a2d 12547 /* No DWP file, look for the DWO file. */
80626a55 12548
ed2dc618
SM
12549 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12550 dwo_name, comp_dir);
6a506a2d 12551 if (*dwo_file_slot == NULL)
80626a55 12552 {
6a506a2d
DE
12553 /* Read in the file and build a table of the CUs/TUs it contains. */
12554 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12555 }
6a506a2d 12556 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12557 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12558
6a506a2d 12559 if (dwo_file != NULL)
19c3d4c9 12560 {
6a506a2d
DE
12561 struct dwo_unit *dwo_cutu = NULL;
12562
12563 if (is_debug_types && dwo_file->tus)
12564 {
12565 struct dwo_unit find_dwo_cutu;
12566
12567 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12568 find_dwo_cutu.signature = signature;
9a3c8263 12569 dwo_cutu
b0b6a987
TT
12570 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12571 &find_dwo_cutu);
6a506a2d 12572 }
33c5cd75 12573 else if (!is_debug_types && dwo_file->cus)
80626a55 12574 {
33c5cd75
DB
12575 struct dwo_unit find_dwo_cutu;
12576
12577 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12578 find_dwo_cutu.signature = signature;
b0b6a987 12579 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12580 &find_dwo_cutu);
6a506a2d
DE
12581 }
12582
12583 if (dwo_cutu != NULL)
12584 {
b4f54984 12585 if (dwarf_read_debug)
6a506a2d
DE
12586 {
12587 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12588 kind, dwo_name, hex_string (signature),
12589 host_address_to_string (dwo_cutu));
12590 }
12591 return dwo_cutu;
80626a55
DE
12592 }
12593 }
2e276125 12594 }
9cdd5dbd 12595
80626a55
DE
12596 /* We didn't find it. This could mean a dwo_id mismatch, or
12597 someone deleted the DWO/DWP file, or the search path isn't set up
12598 correctly to find the file. */
12599
b4f54984 12600 if (dwarf_read_debug)
80626a55
DE
12601 {
12602 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12603 kind, dwo_name, hex_string (signature));
12604 }
3019eac3 12605
6656a72d
DE
12606 /* This is a warning and not a complaint because it can be caused by
12607 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12608 {
12609 /* Print the name of the DWP file if we looked there, helps the user
12610 better diagnose the problem. */
791afaa2 12611 std::string dwp_text;
43942612
DE
12612
12613 if (dwp_file != NULL)
791afaa2
TT
12614 dwp_text = string_printf (" [in DWP file %s]",
12615 lbasename (dwp_file->name));
43942612 12616
9d8780f0 12617 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12618 " [in module %s]"),
12619 kind, dwo_name, hex_string (signature),
791afaa2 12620 dwp_text.c_str (),
43942612 12621 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12622 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12623 }
3019eac3 12624 return NULL;
5fb290d7
DJ
12625}
12626
80626a55
DE
12627/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12628 See lookup_dwo_cutu_unit for details. */
12629
12630static struct dwo_unit *
12631lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12632 const char *dwo_name, const char *comp_dir,
12633 ULONGEST signature)
12634{
12635 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12636}
12637
12638/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12639 See lookup_dwo_cutu_unit for details. */
12640
12641static struct dwo_unit *
12642lookup_dwo_type_unit (struct signatured_type *this_tu,
12643 const char *dwo_name, const char *comp_dir)
12644{
12645 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12646}
12647
89e63ee4
DE
12648/* Traversal function for queue_and_load_all_dwo_tus. */
12649
12650static int
12651queue_and_load_dwo_tu (void **slot, void *info)
12652{
12653 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12654 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12655 ULONGEST signature = dwo_unit->signature;
12656 struct signatured_type *sig_type =
12657 lookup_dwo_signatured_type (per_cu->cu, signature);
12658
12659 if (sig_type != NULL)
12660 {
12661 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12662
12663 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12664 a real dependency of PER_CU on SIG_TYPE. That is detected later
12665 while processing PER_CU. */
12666 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12667 load_full_type_unit (sig_cu);
ae640021 12668 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12669 }
12670
12671 return 1;
12672}
12673
12674/* Queue all TUs contained in the DWO of PER_CU to be read in.
12675 The DWO may have the only definition of the type, though it may not be
12676 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12677 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12678
12679static void
12680queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12681{
12682 struct dwo_unit *dwo_unit;
12683 struct dwo_file *dwo_file;
12684
12685 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12686 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12687 gdb_assert (per_cu->cu != NULL);
12688
12689 dwo_unit = per_cu->cu->dwo_unit;
12690 gdb_assert (dwo_unit != NULL);
12691
12692 dwo_file = dwo_unit->dwo_file;
12693 if (dwo_file->tus != NULL)
b0b6a987
TT
12694 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12695 per_cu);
89e63ee4
DE
12696}
12697
3019eac3 12698/* Read in various DIEs. */
348e048f 12699
d389af10 12700/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12701 Inherit only the children of the DW_AT_abstract_origin DIE not being
12702 already referenced by DW_AT_abstract_origin from the children of the
12703 current DIE. */
d389af10
JK
12704
12705static void
12706inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12707{
12708 struct die_info *child_die;
791afaa2 12709 sect_offset *offsetp;
d389af10
JK
12710 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12711 struct die_info *origin_die;
12712 /* Iterator of the ORIGIN_DIE children. */
12713 struct die_info *origin_child_die;
d389af10 12714 struct attribute *attr;
cd02d79d
PA
12715 struct dwarf2_cu *origin_cu;
12716 struct pending **origin_previous_list_in_scope;
d389af10
JK
12717
12718 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12719 if (!attr)
12720 return;
12721
cd02d79d
PA
12722 /* Note that following die references may follow to a die in a
12723 different cu. */
12724
12725 origin_cu = cu;
12726 origin_die = follow_die_ref (die, attr, &origin_cu);
12727
12728 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12729 symbols in. */
12730 origin_previous_list_in_scope = origin_cu->list_in_scope;
12731 origin_cu->list_in_scope = cu->list_in_scope;
12732
edb3359d
DJ
12733 if (die->tag != origin_die->tag
12734 && !(die->tag == DW_TAG_inlined_subroutine
12735 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12736 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12737 sect_offset_str (die->sect_off),
12738 sect_offset_str (origin_die->sect_off));
d389af10 12739
791afaa2 12740 std::vector<sect_offset> offsets;
d389af10 12741
3ea89b92
PMR
12742 for (child_die = die->child;
12743 child_die && child_die->tag;
12744 child_die = sibling_die (child_die))
12745 {
12746 struct die_info *child_origin_die;
12747 struct dwarf2_cu *child_origin_cu;
12748
12749 /* We are trying to process concrete instance entries:
216f72a1 12750 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12751 it's not relevant to our analysis here. i.e. detecting DIEs that are
12752 present in the abstract instance but not referenced in the concrete
12753 one. */
216f72a1
JK
12754 if (child_die->tag == DW_TAG_call_site
12755 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12756 continue;
12757
c38f313d
DJ
12758 /* For each CHILD_DIE, find the corresponding child of
12759 ORIGIN_DIE. If there is more than one layer of
12760 DW_AT_abstract_origin, follow them all; there shouldn't be,
12761 but GCC versions at least through 4.4 generate this (GCC PR
12762 40573). */
3ea89b92
PMR
12763 child_origin_die = child_die;
12764 child_origin_cu = cu;
c38f313d
DJ
12765 while (1)
12766 {
cd02d79d
PA
12767 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12768 child_origin_cu);
c38f313d
DJ
12769 if (attr == NULL)
12770 break;
cd02d79d
PA
12771 child_origin_die = follow_die_ref (child_origin_die, attr,
12772 &child_origin_cu);
c38f313d
DJ
12773 }
12774
d389af10
JK
12775 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12776 counterpart may exist. */
c38f313d 12777 if (child_origin_die != child_die)
d389af10 12778 {
edb3359d
DJ
12779 if (child_die->tag != child_origin_die->tag
12780 && !(child_die->tag == DW_TAG_inlined_subroutine
12781 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12782 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12783 "different tags"),
9d8780f0
SM
12784 sect_offset_str (child_die->sect_off),
12785 sect_offset_str (child_origin_die->sect_off));
c38f313d 12786 if (child_origin_die->parent != origin_die)
b98664d3 12787 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12788 "different parents"),
9d8780f0
SM
12789 sect_offset_str (child_die->sect_off),
12790 sect_offset_str (child_origin_die->sect_off));
c38f313d 12791 else
791afaa2 12792 offsets.push_back (child_origin_die->sect_off);
d389af10 12793 }
d389af10 12794 }
791afaa2
TT
12795 std::sort (offsets.begin (), offsets.end ());
12796 sect_offset *offsets_end = offsets.data () + offsets.size ();
12797 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12798 if (offsetp[-1] == *offsetp)
b98664d3 12799 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12800 "to DIE %s as their abstract origin"),
12801 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12802
791afaa2 12803 offsetp = offsets.data ();
d389af10
JK
12804 origin_child_die = origin_die->child;
12805 while (origin_child_die && origin_child_die->tag)
12806 {
12807 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12808 while (offsetp < offsets_end
9c541725 12809 && *offsetp < origin_child_die->sect_off)
d389af10 12810 offsetp++;
b64f50a1 12811 if (offsetp >= offsets_end
9c541725 12812 || *offsetp > origin_child_die->sect_off)
d389af10 12813 {
adde2bff
DE
12814 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12815 Check whether we're already processing ORIGIN_CHILD_DIE.
12816 This can happen with mutually referenced abstract_origins.
12817 PR 16581. */
12818 if (!origin_child_die->in_process)
12819 process_die (origin_child_die, origin_cu);
d389af10
JK
12820 }
12821 origin_child_die = sibling_die (origin_child_die);
12822 }
cd02d79d 12823 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12824
12825 if (cu != origin_cu)
12826 compute_delayed_physnames (origin_cu);
d389af10
JK
12827}
12828
c906108c 12829static void
e7c27a73 12830read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12831{
518817b3 12832 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12833 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12834 struct context_stack *newobj;
c906108c
SS
12835 CORE_ADDR lowpc;
12836 CORE_ADDR highpc;
12837 struct die_info *child_die;
edb3359d 12838 struct attribute *attr, *call_line, *call_file;
15d034d0 12839 const char *name;
e142c38c 12840 CORE_ADDR baseaddr;
801e3a5b 12841 struct block *block;
edb3359d 12842 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12843 std::vector<struct symbol *> template_args;
34eaf542 12844 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12845
12846 if (inlined_func)
12847 {
12848 /* If we do not have call site information, we can't show the
12849 caller of this inlined function. That's too confusing, so
12850 only use the scope for local variables. */
12851 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12852 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12853 if (call_line == NULL || call_file == NULL)
12854 {
12855 read_lexical_block_scope (die, cu);
12856 return;
12857 }
12858 }
c906108c 12859
b3b3bada 12860 baseaddr = objfile->text_section_offset ();
e142c38c 12861
94af9270 12862 name = dwarf2_name (die, cu);
c906108c 12863
e8d05480
JB
12864 /* Ignore functions with missing or empty names. These are actually
12865 illegal according to the DWARF standard. */
12866 if (name == NULL)
12867 {
b98664d3 12868 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12869 sect_offset_str (die->sect_off));
e8d05480
JB
12870 return;
12871 }
12872
12873 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12874 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12875 <= PC_BOUNDS_INVALID)
e8d05480 12876 {
ae4d0c03
PM
12877 attr = dwarf2_attr (die, DW_AT_external, cu);
12878 if (!attr || !DW_UNSND (attr))
b98664d3 12879 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12880 "for subprogram DIE at %s"),
12881 sect_offset_str (die->sect_off));
e8d05480
JB
12882 return;
12883 }
c906108c 12884
3e29f34a
MR
12885 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12886 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12887
34eaf542
TT
12888 /* If we have any template arguments, then we must allocate a
12889 different sort of symbol. */
12890 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12891 {
12892 if (child_die->tag == DW_TAG_template_type_param
12893 || child_die->tag == DW_TAG_template_value_param)
12894 {
e623cf5d 12895 templ_func = allocate_template_symbol (objfile);
cf724bc9 12896 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12897 break;
12898 }
12899 }
12900
c24bdb02 12901 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12902 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12903 (struct symbol *) templ_func);
4c2df51b 12904
81873cc8 12905 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12906 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12907 cu->language);
12908
4cecd739
DJ
12909 /* If there is a location expression for DW_AT_frame_base, record
12910 it. */
e142c38c 12911 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12912 if (attr != nullptr)
fe978cb0 12913 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12914
63e43d3a
PMR
12915 /* If there is a location for the static link, record it. */
12916 newobj->static_link = NULL;
12917 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12918 if (attr != nullptr)
63e43d3a 12919 {
224c3ddb
SM
12920 newobj->static_link
12921 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12922 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12923 cu->per_cu->addr_type ());
63e43d3a
PMR
12924 }
12925
c24bdb02 12926 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12927
639d11d3 12928 if (die->child != NULL)
c906108c 12929 {
639d11d3 12930 child_die = die->child;
c906108c
SS
12931 while (child_die && child_die->tag)
12932 {
34eaf542
TT
12933 if (child_die->tag == DW_TAG_template_type_param
12934 || child_die->tag == DW_TAG_template_value_param)
12935 {
12936 struct symbol *arg = new_symbol (child_die, NULL, cu);
12937
f1078f66 12938 if (arg != NULL)
2f4732b0 12939 template_args.push_back (arg);
34eaf542
TT
12940 }
12941 else
12942 process_die (child_die, cu);
c906108c
SS
12943 child_die = sibling_die (child_die);
12944 }
12945 }
12946
d389af10
JK
12947 inherit_abstract_dies (die, cu);
12948
4a811a97
UW
12949 /* If we have a DW_AT_specification, we might need to import using
12950 directives from the context of the specification DIE. See the
12951 comment in determine_prefix. */
12952 if (cu->language == language_cplus
12953 && dwarf2_attr (die, DW_AT_specification, cu))
12954 {
12955 struct dwarf2_cu *spec_cu = cu;
12956 struct die_info *spec_die = die_specification (die, &spec_cu);
12957
12958 while (spec_die)
12959 {
12960 child_die = spec_die->child;
12961 while (child_die && child_die->tag)
12962 {
12963 if (child_die->tag == DW_TAG_imported_module)
12964 process_die (child_die, spec_cu);
12965 child_die = sibling_die (child_die);
12966 }
12967
12968 /* In some cases, GCC generates specification DIEs that
12969 themselves contain DW_AT_specification attributes. */
12970 spec_die = die_specification (spec_die, &spec_cu);
12971 }
12972 }
12973
c24bdb02 12974 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12975 /* Make a block for the local symbols within. */
c24bdb02 12976 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12977 cstk.static_link, lowpc, highpc);
801e3a5b 12978
df8a16a1 12979 /* For C++, set the block's scope. */
45280282
IB
12980 if ((cu->language == language_cplus
12981 || cu->language == language_fortran
c44af4eb
TT
12982 || cu->language == language_d
12983 || cu->language == language_rust)
4d4ec4e5 12984 && cu->processing_has_namespace_info)
195a3f6c
TT
12985 block_set_scope (block, determine_prefix (die, cu),
12986 &objfile->objfile_obstack);
df8a16a1 12987
801e3a5b
JB
12988 /* If we have address ranges, record them. */
12989 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12990
a60f3166 12991 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12992
34eaf542 12993 /* Attach template arguments to function. */
2f4732b0 12994 if (!template_args.empty ())
34eaf542
TT
12995 {
12996 gdb_assert (templ_func != NULL);
12997
2f4732b0 12998 templ_func->n_template_arguments = template_args.size ();
34eaf542 12999 templ_func->template_arguments
8d749320
SM
13000 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13001 templ_func->n_template_arguments);
34eaf542 13002 memcpy (templ_func->template_arguments,
2f4732b0 13003 template_args.data (),
34eaf542 13004 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13005
13006 /* Make sure that the symtab is set on the new symbols. Even
13007 though they don't appear in this symtab directly, other parts
13008 of gdb assume that symbols do, and this is reasonably
13009 true. */
8634679f 13010 for (symbol *sym : template_args)
3e1d3d8c 13011 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13012 }
13013
208d8187
JB
13014 /* In C++, we can have functions nested inside functions (e.g., when
13015 a function declares a class that has methods). This means that
13016 when we finish processing a function scope, we may need to go
13017 back to building a containing block's symbol lists. */
c24bdb02
KS
13018 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13019 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13020
921e78cf
JB
13021 /* If we've finished processing a top-level function, subsequent
13022 symbols go in the file symbol list. */
c24bdb02
KS
13023 if (cu->get_builder ()->outermost_context_p ())
13024 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13025}
13026
13027/* Process all the DIES contained within a lexical block scope. Start
13028 a new scope, process the dies, and then close the scope. */
13029
13030static void
e7c27a73 13031read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13032{
518817b3 13033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13034 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13035 CORE_ADDR lowpc, highpc;
13036 struct die_info *child_die;
e142c38c
DJ
13037 CORE_ADDR baseaddr;
13038
b3b3bada 13039 baseaddr = objfile->text_section_offset ();
c906108c
SS
13040
13041 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13042 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13043 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13044 be nasty. Might be easier to properly extend generic blocks to
af34e669 13045 describe ranges. */
e385593e
JK
13046 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13047 {
13048 case PC_BOUNDS_NOT_PRESENT:
13049 /* DW_TAG_lexical_block has no attributes, process its children as if
13050 there was no wrapping by that DW_TAG_lexical_block.
13051 GCC does no longer produces such DWARF since GCC r224161. */
13052 for (child_die = die->child;
13053 child_die != NULL && child_die->tag;
13054 child_die = sibling_die (child_die))
13055 process_die (child_die, cu);
13056 return;
13057 case PC_BOUNDS_INVALID:
13058 return;
13059 }
3e29f34a
MR
13060 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13061 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13062
c24bdb02 13063 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13064 if (die->child != NULL)
c906108c 13065 {
639d11d3 13066 child_die = die->child;
c906108c
SS
13067 while (child_die && child_die->tag)
13068 {
e7c27a73 13069 process_die (child_die, cu);
c906108c
SS
13070 child_die = sibling_die (child_die);
13071 }
13072 }
3ea89b92 13073 inherit_abstract_dies (die, cu);
c24bdb02 13074 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13075
c24bdb02
KS
13076 if (*cu->get_builder ()->get_local_symbols () != NULL
13077 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13078 {
801e3a5b 13079 struct block *block
c24bdb02 13080 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13081 cstk.start_addr, highpc);
801e3a5b
JB
13082
13083 /* Note that recording ranges after traversing children, as we
13084 do here, means that recording a parent's ranges entails
13085 walking across all its children's ranges as they appear in
13086 the address map, which is quadratic behavior.
13087
13088 It would be nicer to record the parent's ranges before
13089 traversing its children, simply overriding whatever you find
13090 there. But since we don't even decide whether to create a
13091 block until after we've traversed its children, that's hard
13092 to do. */
13093 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13094 }
c24bdb02
KS
13095 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13096 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13097}
13098
216f72a1 13099/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13100
13101static void
13102read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13103{
518817b3 13104 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13105 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13106 CORE_ADDR pc, baseaddr;
13107 struct attribute *attr;
13108 struct call_site *call_site, call_site_local;
13109 void **slot;
13110 int nparams;
13111 struct die_info *child_die;
13112
b3b3bada 13113 baseaddr = objfile->text_section_offset ();
96408a79 13114
216f72a1
JK
13115 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13116 if (attr == NULL)
13117 {
13118 /* This was a pre-DWARF-5 GNU extension alias
13119 for DW_AT_call_return_pc. */
13120 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13121 }
96408a79
SA
13122 if (!attr)
13123 {
b98664d3 13124 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13125 "DIE %s [in module %s]"),
13126 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13127 return;
13128 }
cd6c91b4 13129 pc = attr->value_as_address () + baseaddr;
3e29f34a 13130 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13131
13132 if (cu->call_site_htab == NULL)
13133 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13134 NULL, &objfile->objfile_obstack,
13135 hashtab_obstack_allocate, NULL);
13136 call_site_local.pc = pc;
13137 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13138 if (*slot != NULL)
13139 {
b98664d3 13140 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13141 "DIE %s [in module %s]"),
13142 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13143 objfile_name (objfile));
96408a79
SA
13144 return;
13145 }
13146
13147 /* Count parameters at the caller. */
13148
13149 nparams = 0;
13150 for (child_die = die->child; child_die && child_die->tag;
13151 child_die = sibling_die (child_die))
13152 {
216f72a1
JK
13153 if (child_die->tag != DW_TAG_call_site_parameter
13154 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13155 {
b98664d3 13156 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13157 "DW_TAG_call_site child DIE %s [in module %s]"),
13158 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13159 objfile_name (objfile));
96408a79
SA
13160 continue;
13161 }
13162
13163 nparams++;
13164 }
13165
224c3ddb
SM
13166 call_site
13167 = ((struct call_site *)
13168 obstack_alloc (&objfile->objfile_obstack,
13169 sizeof (*call_site)
13170 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13171 *slot = call_site;
13172 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13173 call_site->pc = pc;
13174
216f72a1
JK
13175 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13176 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13177 {
13178 struct die_info *func_die;
13179
13180 /* Skip also over DW_TAG_inlined_subroutine. */
13181 for (func_die = die->parent;
13182 func_die && func_die->tag != DW_TAG_subprogram
13183 && func_die->tag != DW_TAG_subroutine_type;
13184 func_die = func_die->parent);
13185
216f72a1
JK
13186 /* DW_AT_call_all_calls is a superset
13187 of DW_AT_call_all_tail_calls. */
96408a79 13188 if (func_die
216f72a1 13189 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13190 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13191 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13192 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13193 {
13194 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13195 not complete. But keep CALL_SITE for look ups via call_site_htab,
13196 both the initial caller containing the real return address PC and
13197 the final callee containing the current PC of a chain of tail
13198 calls do not need to have the tail call list complete. But any
13199 function candidate for a virtual tail call frame searched via
13200 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13201 determined unambiguously. */
13202 }
13203 else
13204 {
13205 struct type *func_type = NULL;
13206
13207 if (func_die)
13208 func_type = get_die_type (func_die, cu);
13209 if (func_type != NULL)
13210 {
13211 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13212
13213 /* Enlist this call site to the function. */
13214 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13215 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13216 }
13217 else
b98664d3 13218 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13219 "DIE %s [in module %s]"),
13220 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13221 }
13222 }
13223
216f72a1
JK
13224 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13225 if (attr == NULL)
13226 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13227 if (attr == NULL)
13228 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13229 if (attr == NULL)
216f72a1
JK
13230 {
13231 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13232 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13233 }
96408a79 13234 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13235 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13236 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13237 else if (attr->form_is_block ())
96408a79
SA
13238 {
13239 struct dwarf2_locexpr_baton *dlbaton;
13240
8d749320 13241 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13242 dlbaton->data = DW_BLOCK (attr)->data;
13243 dlbaton->size = DW_BLOCK (attr)->size;
13244 dlbaton->per_cu = cu->per_cu;
13245
13246 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13247 }
cd6c91b4 13248 else if (attr->form_is_ref ())
96408a79 13249 {
96408a79
SA
13250 struct dwarf2_cu *target_cu = cu;
13251 struct die_info *target_die;
13252
ac9ec31b 13253 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13254 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13255 if (die_is_declaration (target_die, target_cu))
13256 {
7d45c7c3 13257 const char *target_physname;
9112db09
JK
13258
13259 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13260 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13261 if (target_physname == NULL)
9112db09 13262 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13263 if (target_physname == NULL)
b98664d3 13264 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13265 "physname, for referencing DIE %s [in module %s]"),
13266 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13267 else
7d455152 13268 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13269 }
13270 else
13271 {
13272 CORE_ADDR lowpc;
13273
13274 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13275 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13276 <= PC_BOUNDS_INVALID)
b98664d3 13277 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13278 "low pc, for referencing DIE %s [in module %s]"),
13279 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13280 else
3e29f34a
MR
13281 {
13282 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13283 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13284 }
96408a79
SA
13285 }
13286 }
13287 else
b98664d3 13288 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13289 "block nor reference, for DIE %s [in module %s]"),
13290 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13291
13292 call_site->per_cu = cu->per_cu;
13293
13294 for (child_die = die->child;
13295 child_die && child_die->tag;
13296 child_die = sibling_die (child_die))
13297 {
96408a79 13298 struct call_site_parameter *parameter;
1788b2d3 13299 struct attribute *loc, *origin;
96408a79 13300
216f72a1
JK
13301 if (child_die->tag != DW_TAG_call_site_parameter
13302 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13303 {
13304 /* Already printed the complaint above. */
13305 continue;
13306 }
13307
13308 gdb_assert (call_site->parameter_count < nparams);
13309 parameter = &call_site->parameter[call_site->parameter_count];
13310
1788b2d3
JK
13311 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13312 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13313 register is contained in DW_AT_call_value. */
96408a79 13314
24c5c679 13315 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13316 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13317 if (origin == NULL)
13318 {
13319 /* This was a pre-DWARF-5 GNU extension alias
13320 for DW_AT_call_parameter. */
13321 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13322 }
cd6c91b4 13323 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13324 {
1788b2d3 13325 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13326
13327 sect_offset sect_off
13328 = (sect_offset) dwarf2_get_ref_die_offset (origin);
4057dfde 13329 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13330 {
13331 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13332 binding can be done only inside one CU. Such referenced DIE
13333 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13334 complaint (_("DW_AT_call_parameter offset is not in CU 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));
d76b7dbc
JK
13338 continue;
13339 }
9c541725
PA
13340 parameter->u.param_cu_off
13341 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13342 }
4fc6c0d5 13343 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13344 {
b98664d3 13345 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13346 "DW_TAG_call_site child DIE %s [in module %s]"),
13347 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13348 continue;
13349 }
24c5c679 13350 else
96408a79 13351 {
24c5c679
JK
13352 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13353 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13354 if (parameter->u.dwarf_reg != -1)
13355 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13356 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13357 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13358 &parameter->u.fb_offset))
13359 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13360 else
13361 {
b98664d3 13362 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13363 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13364 "DW_TAG_call_site child DIE %s "
24c5c679 13365 "[in module %s]"),
9d8780f0 13366 sect_offset_str (child_die->sect_off),
9c541725 13367 objfile_name (objfile));
24c5c679
JK
13368 continue;
13369 }
96408a79
SA
13370 }
13371
216f72a1
JK
13372 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13373 if (attr == NULL)
13374 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13375 if (attr == NULL || !attr->form_is_block ())
96408a79 13376 {
b98664d3 13377 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13378 "DW_TAG_call_site child DIE %s [in module %s]"),
13379 sect_offset_str (child_die->sect_off),
9c541725 13380 objfile_name (objfile));
96408a79
SA
13381 continue;
13382 }
13383 parameter->value = DW_BLOCK (attr)->data;
13384 parameter->value_size = DW_BLOCK (attr)->size;
13385
13386 /* Parameters are not pre-cleared by memset above. */
13387 parameter->data_value = NULL;
13388 parameter->data_value_size = 0;
13389 call_site->parameter_count++;
13390
216f72a1
JK
13391 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13392 if (attr == NULL)
13393 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13394 if (attr != nullptr)
96408a79 13395 {
4fc6c0d5 13396 if (!attr->form_is_block ())
b98664d3 13397 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13398 "DW_TAG_call_site child DIE %s [in module %s]"),
13399 sect_offset_str (child_die->sect_off),
9c541725 13400 objfile_name (objfile));
96408a79
SA
13401 else
13402 {
13403 parameter->data_value = DW_BLOCK (attr)->data;
13404 parameter->data_value_size = DW_BLOCK (attr)->size;
13405 }
13406 }
13407 }
13408}
13409
71a3c369
TT
13410/* Helper function for read_variable. If DIE represents a virtual
13411 table, then return the type of the concrete object that is
13412 associated with the virtual table. Otherwise, return NULL. */
13413
13414static struct type *
13415rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13416{
13417 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13418 if (attr == NULL)
13419 return NULL;
13420
13421 /* Find the type DIE. */
13422 struct die_info *type_die = NULL;
13423 struct dwarf2_cu *type_cu = cu;
13424
cd6c91b4 13425 if (attr->form_is_ref ())
71a3c369
TT
13426 type_die = follow_die_ref (die, attr, &type_cu);
13427 if (type_die == NULL)
13428 return NULL;
13429
13430 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13431 return NULL;
13432 return die_containing_type (type_die, type_cu);
13433}
13434
13435/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13436
13437static void
13438read_variable (struct die_info *die, struct dwarf2_cu *cu)
13439{
13440 struct rust_vtable_symbol *storage = NULL;
13441
13442 if (cu->language == language_rust)
13443 {
13444 struct type *containing_type = rust_containing_type (die, cu);
13445
13446 if (containing_type != NULL)
13447 {
518817b3 13448 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13449
468c0cbb 13450 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13451 initialize_objfile_symbol (storage);
13452 storage->concrete_type = containing_type;
cf724bc9 13453 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13454 }
13455 }
13456
e4a62c65
TV
13457 struct symbol *res = new_symbol (die, NULL, cu, storage);
13458 struct attribute *abstract_origin
13459 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13460 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13461 if (res == NULL && loc && abstract_origin)
13462 {
13463 /* We have a variable without a name, but with a location and an abstract
13464 origin. This may be a concrete instance of an abstract variable
13465 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13466 later. */
13467 struct dwarf2_cu *origin_cu = cu;
13468 struct die_info *origin_die
13469 = follow_die_ref (die, abstract_origin, &origin_cu);
13470 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13471 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13472 }
71a3c369
TT
13473}
13474
43988095
JK
13475/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13476 reading .debug_rnglists.
13477 Callback's type should be:
13478 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13479 Return true if the attributes are present and valid, otherwise,
13480 return false. */
13481
13482template <typename Callback>
13483static bool
13484dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13485 Callback &&callback)
13486{
ed2dc618 13487 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13488 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13489 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13490 bfd *obfd = objfile->obfd;
43988095
JK
13491 /* Base address selection entry. */
13492 CORE_ADDR base;
13493 int found_base;
43988095 13494 const gdb_byte *buffer;
43988095
JK
13495 CORE_ADDR baseaddr;
13496 bool overflow = false;
13497
13498 found_base = cu->base_known;
13499 base = cu->base_address;
13500
96b79293 13501 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13502 if (offset >= dwarf2_per_objfile->rnglists.size)
13503 {
b98664d3 13504 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13505 offset);
13506 return false;
13507 }
13508 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13509
b3b3bada 13510 baseaddr = objfile->text_section_offset ();
43988095
JK
13511
13512 while (1)
13513 {
7814882a
JK
13514 /* Initialize it due to a false compiler warning. */
13515 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13516 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13517 + dwarf2_per_objfile->rnglists.size);
13518 unsigned int bytes_read;
13519
13520 if (buffer == buf_end)
13521 {
13522 overflow = true;
13523 break;
13524 }
13525 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13526 switch (rlet)
13527 {
13528 case DW_RLE_end_of_list:
13529 break;
13530 case DW_RLE_base_address:
13531 if (buffer + cu->header.addr_size > buf_end)
13532 {
13533 overflow = true;
13534 break;
13535 }
c8a7a66f 13536 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13537 found_base = 1;
13538 buffer += bytes_read;
13539 break;
13540 case DW_RLE_start_length:
13541 if (buffer + cu->header.addr_size > buf_end)
13542 {
13543 overflow = true;
13544 break;
13545 }
c8a7a66f
TT
13546 range_beginning = cu->header.read_address (obfd, buffer,
13547 &bytes_read);
43988095
JK
13548 buffer += bytes_read;
13549 range_end = (range_beginning
13550 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13551 buffer += bytes_read;
13552 if (buffer > buf_end)
13553 {
13554 overflow = true;
13555 break;
13556 }
13557 break;
13558 case DW_RLE_offset_pair:
13559 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13560 buffer += bytes_read;
13561 if (buffer > buf_end)
13562 {
13563 overflow = true;
13564 break;
13565 }
13566 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13567 buffer += bytes_read;
13568 if (buffer > buf_end)
13569 {
13570 overflow = true;
13571 break;
13572 }
13573 break;
13574 case DW_RLE_start_end:
13575 if (buffer + 2 * cu->header.addr_size > buf_end)
13576 {
13577 overflow = true;
13578 break;
13579 }
c8a7a66f
TT
13580 range_beginning = cu->header.read_address (obfd, buffer,
13581 &bytes_read);
43988095 13582 buffer += bytes_read;
c8a7a66f 13583 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13584 buffer += bytes_read;
13585 break;
13586 default:
b98664d3 13587 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13588 return false;
13589 }
13590 if (rlet == DW_RLE_end_of_list || overflow)
13591 break;
13592 if (rlet == DW_RLE_base_address)
13593 continue;
13594
13595 if (!found_base)
13596 {
13597 /* We have no valid base address for the ranges
13598 data. */
b98664d3 13599 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13600 return false;
13601 }
13602
13603 if (range_beginning > range_end)
13604 {
13605 /* Inverted range entries are invalid. */
b98664d3 13606 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13607 return false;
13608 }
13609
13610 /* Empty range entries have no effect. */
13611 if (range_beginning == range_end)
13612 continue;
13613
13614 range_beginning += base;
13615 range_end += base;
13616
13617 /* A not-uncommon case of bad debug info.
13618 Don't pollute the addrmap with bad data. */
13619 if (range_beginning + baseaddr == 0
13620 && !dwarf2_per_objfile->has_section_at_zero)
13621 {
b98664d3 13622 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13623 " [in module %s]"), objfile_name (objfile));
13624 continue;
13625 }
13626
13627 callback (range_beginning, range_end);
13628 }
13629
13630 if (overflow)
13631 {
b98664d3 13632 complaint (_("Offset %d is not terminated "
43988095
JK
13633 "for DW_AT_ranges attribute"),
13634 offset);
13635 return false;
13636 }
13637
13638 return true;
13639}
13640
13641/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13642 Callback's type should be:
13643 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13644 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13645
43988095 13646template <typename Callback>
43039443 13647static int
5f46c5a5 13648dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13649 Callback &&callback)
43039443 13650{
ed2dc618 13651 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13652 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13653 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13654 struct comp_unit_head *cu_header = &cu->header;
13655 bfd *obfd = objfile->obfd;
13656 unsigned int addr_size = cu_header->addr_size;
13657 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13658 /* Base address selection entry. */
13659 CORE_ADDR base;
13660 int found_base;
13661 unsigned int dummy;
d521ce57 13662 const gdb_byte *buffer;
ff013f42 13663 CORE_ADDR baseaddr;
43039443 13664
43988095
JK
13665 if (cu_header->version >= 5)
13666 return dwarf2_rnglists_process (offset, cu, callback);
13667
d00adf39
DE
13668 found_base = cu->base_known;
13669 base = cu->base_address;
43039443 13670
96b79293 13671 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13672 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13673 {
b98664d3 13674 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13675 offset);
13676 return 0;
13677 }
dce234bc 13678 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13679
b3b3bada 13680 baseaddr = objfile->text_section_offset ();
ff013f42 13681
43039443
JK
13682 while (1)
13683 {
13684 CORE_ADDR range_beginning, range_end;
13685
c8a7a66f 13686 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13687 buffer += addr_size;
c8a7a66f 13688 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13689 buffer += addr_size;
13690 offset += 2 * addr_size;
13691
13692 /* An end of list marker is a pair of zero addresses. */
13693 if (range_beginning == 0 && range_end == 0)
13694 /* Found the end of list entry. */
13695 break;
13696
13697 /* Each base address selection entry is a pair of 2 values.
13698 The first is the largest possible address, the second is
13699 the base address. Check for a base address here. */
13700 if ((range_beginning & mask) == mask)
13701 {
28d2bfb9
AB
13702 /* If we found the largest possible address, then we already
13703 have the base address in range_end. */
13704 base = range_end;
43039443
JK
13705 found_base = 1;
13706 continue;
13707 }
13708
13709 if (!found_base)
13710 {
13711 /* We have no valid base address for the ranges
13712 data. */
b98664d3 13713 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13714 return 0;
13715 }
13716
9277c30c
UW
13717 if (range_beginning > range_end)
13718 {
13719 /* Inverted range entries are invalid. */
b98664d3 13720 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13721 return 0;
13722 }
13723
13724 /* Empty range entries have no effect. */
13725 if (range_beginning == range_end)
13726 continue;
13727
43039443
JK
13728 range_beginning += base;
13729 range_end += base;
13730
01093045
DE
13731 /* A not-uncommon case of bad debug info.
13732 Don't pollute the addrmap with bad data. */
13733 if (range_beginning + baseaddr == 0
13734 && !dwarf2_per_objfile->has_section_at_zero)
13735 {
b98664d3 13736 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13737 " [in module %s]"), objfile_name (objfile));
01093045
DE
13738 continue;
13739 }
13740
5f46c5a5
JK
13741 callback (range_beginning, range_end);
13742 }
13743
13744 return 1;
13745}
13746
13747/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13748 Return 1 if the attributes are present and valid, otherwise, return 0.
13749 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13750
13751static int
13752dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13753 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13754 dwarf2_psymtab *ranges_pst)
5f46c5a5 13755{
518817b3 13756 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13758 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13759 int low_set = 0;
13760 CORE_ADDR low = 0;
13761 CORE_ADDR high = 0;
13762 int retval;
13763
13764 retval = dwarf2_ranges_process (offset, cu,
13765 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13766 {
9277c30c 13767 if (ranges_pst != NULL)
3e29f34a
MR
13768 {
13769 CORE_ADDR lowpc;
13770 CORE_ADDR highpc;
13771
79748972
TT
13772 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13773 range_beginning + baseaddr)
13774 - baseaddr);
13775 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13776 range_end + baseaddr)
13777 - baseaddr);
d320c2b5
TT
13778 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13779 lowpc, highpc - 1, ranges_pst);
3e29f34a 13780 }
ff013f42 13781
43039443
JK
13782 /* FIXME: This is recording everything as a low-high
13783 segment of consecutive addresses. We should have a
13784 data structure for discontiguous block ranges
13785 instead. */
13786 if (! low_set)
13787 {
13788 low = range_beginning;
13789 high = range_end;
13790 low_set = 1;
13791 }
13792 else
13793 {
13794 if (range_beginning < low)
13795 low = range_beginning;
13796 if (range_end > high)
13797 high = range_end;
13798 }
5f46c5a5
JK
13799 });
13800 if (!retval)
13801 return 0;
43039443
JK
13802
13803 if (! low_set)
13804 /* If the first entry is an end-of-list marker, the range
13805 describes an empty scope, i.e. no instructions. */
13806 return 0;
13807
13808 if (low_return)
13809 *low_return = low;
13810 if (high_return)
13811 *high_return = high;
13812 return 1;
13813}
13814
3a2b436a
JK
13815/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13816 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13817 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13818
3a2b436a 13819static enum pc_bounds_kind
af34e669 13820dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13821 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13822 dwarf2_psymtab *pst)
c906108c 13823{
518817b3
SM
13824 struct dwarf2_per_objfile *dwarf2_per_objfile
13825 = cu->per_cu->dwarf2_per_objfile;
c906108c 13826 struct attribute *attr;
91da1414 13827 struct attribute *attr_high;
af34e669
DJ
13828 CORE_ADDR low = 0;
13829 CORE_ADDR high = 0;
e385593e 13830 enum pc_bounds_kind ret;
c906108c 13831
91da1414
MW
13832 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13833 if (attr_high)
af34e669 13834 {
e142c38c 13835 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13836 if (attr != nullptr)
91da1414 13837 {
cd6c91b4
TT
13838 low = attr->value_as_address ();
13839 high = attr_high->value_as_address ();
13840 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13841 high += low;
91da1414 13842 }
af34e669
DJ
13843 else
13844 /* Found high w/o low attribute. */
e385593e 13845 return PC_BOUNDS_INVALID;
af34e669
DJ
13846
13847 /* Found consecutive range of addresses. */
3a2b436a 13848 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13849 }
c906108c 13850 else
af34e669 13851 {
e142c38c 13852 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13853 if (attr != NULL)
13854 {
18a8505e 13855 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13856 We take advantage of the fact that DW_AT_ranges does not appear
13857 in DW_TAG_compile_unit of DWO files. */
13858 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13859 unsigned int ranges_offset = (DW_UNSND (attr)
13860 + (need_ranges_base
13861 ? cu->ranges_base
13862 : 0));
2e3cf129 13863
af34e669 13864 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13865 .debug_ranges section. */
2e3cf129 13866 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13867 return PC_BOUNDS_INVALID;
43039443 13868 /* Found discontinuous range of addresses. */
3a2b436a 13869 ret = PC_BOUNDS_RANGES;
af34e669 13870 }
e385593e
JK
13871 else
13872 return PC_BOUNDS_NOT_PRESENT;
af34e669 13873 }
c906108c 13874
48fbe735 13875 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13876 if (high <= low)
e385593e 13877 return PC_BOUNDS_INVALID;
c906108c
SS
13878
13879 /* When using the GNU linker, .gnu.linkonce. sections are used to
13880 eliminate duplicate copies of functions and vtables and such.
13881 The linker will arbitrarily choose one and discard the others.
13882 The AT_*_pc values for such functions refer to local labels in
13883 these sections. If the section from that file was discarded, the
13884 labels are not in the output, so the relocs get a value of 0.
13885 If this is a discarded function, mark the pc bounds as invalid,
13886 so that GDB will ignore it. */
72dca2f5 13887 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13888 return PC_BOUNDS_INVALID;
c906108c
SS
13889
13890 *lowpc = low;
96408a79
SA
13891 if (highpc)
13892 *highpc = high;
af34e669 13893 return ret;
c906108c
SS
13894}
13895
b084d499
JB
13896/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13897 its low and high PC addresses. Do nothing if these addresses could not
13898 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13899 and HIGHPC to the high address if greater than HIGHPC. */
13900
13901static void
13902dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13903 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13904 struct dwarf2_cu *cu)
13905{
13906 CORE_ADDR low, high;
13907 struct die_info *child = die->child;
13908
e385593e 13909 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13910 {
325fac50
PA
13911 *lowpc = std::min (*lowpc, low);
13912 *highpc = std::max (*highpc, high);
b084d499
JB
13913 }
13914
13915 /* If the language does not allow nested subprograms (either inside
13916 subprograms or lexical blocks), we're done. */
13917 if (cu->language != language_ada)
13918 return;
6e70227d 13919
b084d499
JB
13920 /* Check all the children of the given DIE. If it contains nested
13921 subprograms, then check their pc bounds. Likewise, we need to
13922 check lexical blocks as well, as they may also contain subprogram
13923 definitions. */
13924 while (child && child->tag)
13925 {
13926 if (child->tag == DW_TAG_subprogram
13927 || child->tag == DW_TAG_lexical_block)
13928 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13929 child = sibling_die (child);
13930 }
13931}
13932
fae299cd
DC
13933/* Get the low and high pc's represented by the scope DIE, and store
13934 them in *LOWPC and *HIGHPC. If the correct values can't be
13935 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13936
13937static void
13938get_scope_pc_bounds (struct die_info *die,
13939 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13940 struct dwarf2_cu *cu)
13941{
13942 CORE_ADDR best_low = (CORE_ADDR) -1;
13943 CORE_ADDR best_high = (CORE_ADDR) 0;
13944 CORE_ADDR current_low, current_high;
13945
3a2b436a 13946 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13947 >= PC_BOUNDS_RANGES)
fae299cd
DC
13948 {
13949 best_low = current_low;
13950 best_high = current_high;
13951 }
13952 else
13953 {
13954 struct die_info *child = die->child;
13955
13956 while (child && child->tag)
13957 {
13958 switch (child->tag) {
13959 case DW_TAG_subprogram:
b084d499 13960 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13961 break;
13962 case DW_TAG_namespace:
f55ee35c 13963 case DW_TAG_module:
fae299cd
DC
13964 /* FIXME: carlton/2004-01-16: Should we do this for
13965 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13966 that current GCC's always emit the DIEs corresponding
13967 to definitions of methods of classes as children of a
13968 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13969 the DIEs giving the declarations, which could be
13970 anywhere). But I don't see any reason why the
13971 standards says that they have to be there. */
13972 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13973
13974 if (current_low != ((CORE_ADDR) -1))
13975 {
325fac50
PA
13976 best_low = std::min (best_low, current_low);
13977 best_high = std::max (best_high, current_high);
fae299cd
DC
13978 }
13979 break;
13980 default:
0963b4bd 13981 /* Ignore. */
fae299cd
DC
13982 break;
13983 }
13984
13985 child = sibling_die (child);
13986 }
13987 }
13988
13989 *lowpc = best_low;
13990 *highpc = best_high;
13991}
13992
801e3a5b
JB
13993/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13994 in DIE. */
380bca97 13995
801e3a5b
JB
13996static void
13997dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13998 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13999{
518817b3 14000 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14001 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14002 struct attribute *attr;
91da1414 14003 struct attribute *attr_high;
801e3a5b 14004
91da1414
MW
14005 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14006 if (attr_high)
801e3a5b 14007 {
801e3a5b 14008 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14009 if (attr != nullptr)
801e3a5b 14010 {
cd6c91b4
TT
14011 CORE_ADDR low = attr->value_as_address ();
14012 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14013
cd6c91b4 14014 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14015 high += low;
9a619af0 14016
3e29f34a
MR
14017 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14018 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14019 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14020 }
14021 }
14022
14023 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14024 if (attr != nullptr)
801e3a5b 14025 {
18a8505e 14026 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14027 We take advantage of the fact that DW_AT_ranges does not appear
14028 in DW_TAG_compile_unit of DWO files. */
14029 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14030
14031 /* The value of the DW_AT_ranges attribute is the offset of the
14032 address range list in the .debug_ranges section. */
ab435259
DE
14033 unsigned long offset = (DW_UNSND (attr)
14034 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14035
2d5f09ec 14036 std::vector<blockrange> blockvec;
5f46c5a5
JK
14037 dwarf2_ranges_process (offset, cu,
14038 [&] (CORE_ADDR start, CORE_ADDR end)
14039 {
58fdfd2c
JK
14040 start += baseaddr;
14041 end += baseaddr;
5f46c5a5
JK
14042 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14043 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14044 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14045 blockvec.emplace_back (start, end);
5f46c5a5 14046 });
2d5f09ec
KB
14047
14048 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14049 }
14050}
14051
685b1105
JK
14052/* Check whether the producer field indicates either of GCC < 4.6, or the
14053 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14054
685b1105
JK
14055static void
14056check_producer (struct dwarf2_cu *cu)
60d5a603 14057{
38360086 14058 int major, minor;
60d5a603
JK
14059
14060 if (cu->producer == NULL)
14061 {
14062 /* For unknown compilers expect their behavior is DWARF version
14063 compliant.
14064
14065 GCC started to support .debug_types sections by -gdwarf-4 since
14066 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14067 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14068 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14069 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14070 }
b1ffba5a 14071 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14072 {
38360086
MW
14073 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14074 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14075 }
5230b05a 14076 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14077 {
14078 cu->producer_is_icc = true;
14079 cu->producer_is_icc_lt_14 = major < 14;
14080 }
c258c396
JD
14081 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14082 cu->producer_is_codewarrior = true;
685b1105
JK
14083 else
14084 {
14085 /* For other non-GCC compilers, expect their behavior is DWARF version
14086 compliant. */
60d5a603
JK
14087 }
14088
9068261f 14089 cu->checked_producer = true;
685b1105 14090}
ba919b58 14091
685b1105
JK
14092/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14093 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14094 during 4.6.0 experimental. */
14095
9068261f 14096static bool
685b1105
JK
14097producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14098{
14099 if (!cu->checked_producer)
14100 check_producer (cu);
14101
14102 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14103}
14104
c258c396
JD
14105
14106/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14107 with incorrect is_stmt attributes. */
14108
14109static bool
14110producer_is_codewarrior (struct dwarf2_cu *cu)
14111{
14112 if (!cu->checked_producer)
14113 check_producer (cu);
14114
14115 return cu->producer_is_codewarrior;
14116}
14117
405feb71 14118/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14119 DW_AT_accessibility. */
14120
14121static enum dwarf_access_attribute
14122dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14123{
14124 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14125 {
14126 /* The default DWARF 2 accessibility for members is public, the default
14127 accessibility for inheritance is private. */
14128
14129 if (die->tag != DW_TAG_inheritance)
14130 return DW_ACCESS_public;
14131 else
14132 return DW_ACCESS_private;
14133 }
14134 else
14135 {
14136 /* DWARF 3+ defines the default accessibility a different way. The same
14137 rules apply now for DW_TAG_inheritance as for the members and it only
14138 depends on the container kind. */
14139
14140 if (die->parent->tag == DW_TAG_class_type)
14141 return DW_ACCESS_private;
14142 else
14143 return DW_ACCESS_public;
14144 }
14145}
14146
74ac6d43
TT
14147/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14148 offset. If the attribute was not found return 0, otherwise return
14149 1. If it was found but could not properly be handled, set *OFFSET
14150 to 0. */
14151
14152static int
14153handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14154 LONGEST *offset)
14155{
14156 struct attribute *attr;
14157
14158 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14159 if (attr != NULL)
14160 {
14161 *offset = 0;
14162
14163 /* Note that we do not check for a section offset first here.
14164 This is because DW_AT_data_member_location is new in DWARF 4,
14165 so if we see it, we can assume that a constant form is really
14166 a constant and not a section offset. */
cd6c91b4 14167 if (attr->form_is_constant ())
74ac6d43 14168 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14169 else if (attr->form_is_section_offset ())
74ac6d43 14170 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14171 else if (attr->form_is_block ())
74ac6d43
TT
14172 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14173 else
14174 dwarf2_complex_location_expr_complaint ();
14175
14176 return 1;
14177 }
14178
14179 return 0;
14180}
14181
c906108c
SS
14182/* Add an aggregate field to the field list. */
14183
14184static void
107d2387 14185dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14186 struct dwarf2_cu *cu)
6e70227d 14187{
518817b3 14188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14189 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14190 struct nextfield *new_field;
14191 struct attribute *attr;
14192 struct field *fp;
15d034d0 14193 const char *fieldname = "";
c906108c 14194
7d0ccb61
DJ
14195 if (die->tag == DW_TAG_inheritance)
14196 {
be2daae6
TT
14197 fip->baseclasses.emplace_back ();
14198 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14199 }
14200 else
14201 {
be2daae6
TT
14202 fip->fields.emplace_back ();
14203 new_field = &fip->fields.back ();
7d0ccb61 14204 }
be2daae6 14205
e142c38c 14206 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14207 if (attr != nullptr)
c906108c 14208 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14209 else
14210 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14211 if (new_field->accessibility != DW_ACCESS_public)
14212 fip->non_public_fields = 1;
60d5a603 14213
e142c38c 14214 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14215 if (attr != nullptr)
c906108c 14216 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14217 else
14218 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14219
14220 fp = &new_field->field;
a9a9bd0f 14221
e142c38c 14222 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14223 {
74ac6d43
TT
14224 LONGEST offset;
14225
a9a9bd0f 14226 /* Data member other than a C++ static data member. */
6e70227d 14227
c906108c 14228 /* Get type of field. */
e7c27a73 14229 fp->type = die_type (die, cu);
c906108c 14230
d6a843b5 14231 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14232
c906108c 14233 /* Get bit size of field (zero if none). */
e142c38c 14234 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14235 if (attr != nullptr)
c906108c
SS
14236 {
14237 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14238 }
14239 else
14240 {
14241 FIELD_BITSIZE (*fp) = 0;
14242 }
14243
14244 /* Get bit offset of field. */
74ac6d43
TT
14245 if (handle_data_member_location (die, cu, &offset))
14246 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14247 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14248 if (attr != nullptr)
c906108c 14249 {
d5a22e77 14250 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14251 {
14252 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14253 additional bit offset from the MSB of the containing
14254 anonymous object to the MSB of the field. We don't
14255 have to do anything special since we don't need to
14256 know the size of the anonymous object. */
f41f5e61 14257 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14258 }
14259 else
14260 {
14261 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14262 MSB of the anonymous object, subtract off the number of
14263 bits from the MSB of the field to the MSB of the
14264 object, and then subtract off the number of bits of
14265 the field itself. The result is the bit offset of
14266 the LSB of the field. */
c906108c
SS
14267 int anonymous_size;
14268 int bit_offset = DW_UNSND (attr);
14269
e142c38c 14270 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14271 if (attr != nullptr)
c906108c
SS
14272 {
14273 /* The size of the anonymous object containing
14274 the bit field is explicit, so use the
14275 indicated size (in bytes). */
14276 anonymous_size = DW_UNSND (attr);
14277 }
14278 else
14279 {
14280 /* The size of the anonymous object containing
14281 the bit field must be inferred from the type
14282 attribute of the data member containing the
14283 bit field. */
14284 anonymous_size = TYPE_LENGTH (fp->type);
14285 }
f41f5e61
PA
14286 SET_FIELD_BITPOS (*fp,
14287 (FIELD_BITPOS (*fp)
14288 + anonymous_size * bits_per_byte
14289 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14290 }
14291 }
da5b30da
AA
14292 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14293 if (attr != NULL)
14294 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14295 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14296
14297 /* Get name of field. */
39cbfefa
DJ
14298 fieldname = dwarf2_name (die, cu);
14299 if (fieldname == NULL)
14300 fieldname = "";
d8151005
DJ
14301
14302 /* The name is already allocated along with this objfile, so we don't
14303 need to duplicate it for the type. */
14304 fp->name = fieldname;
c906108c
SS
14305
14306 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14307 pointer or virtual base class pointer) to private. */
e142c38c 14308 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14309 {
d48cc9dd 14310 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14311 new_field->accessibility = DW_ACCESS_private;
14312 fip->non_public_fields = 1;
14313 }
14314 }
a9a9bd0f 14315 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14316 {
a9a9bd0f
DC
14317 /* C++ static member. */
14318
14319 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14320 is a declaration, but all versions of G++ as of this writing
14321 (so through at least 3.2.1) incorrectly generate
14322 DW_TAG_variable tags. */
6e70227d 14323
ff355380 14324 const char *physname;
c906108c 14325
a9a9bd0f 14326 /* Get name of field. */
39cbfefa
DJ
14327 fieldname = dwarf2_name (die, cu);
14328 if (fieldname == NULL)
c906108c
SS
14329 return;
14330
254e6b9e 14331 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14332 if (attr
14333 /* Only create a symbol if this is an external value.
14334 new_symbol checks this and puts the value in the global symbol
14335 table, which we want. If it is not external, new_symbol
14336 will try to put the value in cu->list_in_scope which is wrong. */
14337 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14338 {
14339 /* A static const member, not much different than an enum as far as
14340 we're concerned, except that we can support more types. */
14341 new_symbol (die, NULL, cu);
14342 }
14343
2df3850c 14344 /* Get physical name. */
ff355380 14345 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14346
d8151005
DJ
14347 /* The name is already allocated along with this objfile, so we don't
14348 need to duplicate it for the type. */
14349 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14350 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14351 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14352 }
14353 else if (die->tag == DW_TAG_inheritance)
14354 {
74ac6d43 14355 LONGEST offset;
d4b96c9a 14356
74ac6d43
TT
14357 /* C++ base class field. */
14358 if (handle_data_member_location (die, cu, &offset))
14359 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14360 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14361 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14362 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14363 }
2ddeaf8a
TT
14364 else if (die->tag == DW_TAG_variant_part)
14365 {
14366 /* process_structure_scope will treat this DIE as a union. */
14367 process_structure_scope (die, cu);
14368
14369 /* The variant part is relative to the start of the enclosing
14370 structure. */
14371 SET_FIELD_BITPOS (*fp, 0);
14372 fp->type = get_die_type (die, cu);
14373 fp->artificial = 1;
14374 fp->name = "<<variant>>";
c8c81635
TT
14375
14376 /* Normally a DW_TAG_variant_part won't have a size, but our
14377 representation requires one, so set it to the maximum of the
489dbda6
TT
14378 child sizes, being sure to account for the offset at which
14379 each child is seen. */
c8c81635
TT
14380 if (TYPE_LENGTH (fp->type) == 0)
14381 {
14382 unsigned max = 0;
14383 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14384 {
14385 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14386 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14387 if (len > max)
14388 max = len;
14389 }
c8c81635
TT
14390 TYPE_LENGTH (fp->type) = max;
14391 }
2ddeaf8a
TT
14392 }
14393 else
14394 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14395}
14396
883fd55a
KS
14397/* Can the type given by DIE define another type? */
14398
14399static bool
14400type_can_define_types (const struct die_info *die)
14401{
14402 switch (die->tag)
14403 {
14404 case DW_TAG_typedef:
14405 case DW_TAG_class_type:
14406 case DW_TAG_structure_type:
14407 case DW_TAG_union_type:
14408 case DW_TAG_enumeration_type:
14409 return true;
14410
14411 default:
14412 return false;
14413 }
14414}
14415
14416/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14417
14418static void
883fd55a
KS
14419dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14420 struct dwarf2_cu *cu)
6e70227d 14421{
be2daae6
TT
14422 struct decl_field fp;
14423 memset (&fp, 0, sizeof (fp));
98751a41 14424
883fd55a 14425 gdb_assert (type_can_define_types (die));
98751a41 14426
883fd55a 14427 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14428 fp.name = dwarf2_name (die, cu);
14429 fp.type = read_type_die (die, cu);
98751a41 14430
c191a687
KS
14431 /* Save accessibility. */
14432 enum dwarf_access_attribute accessibility;
14433 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14434 if (attr != NULL)
14435 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14436 else
14437 accessibility = dwarf2_default_access_attribute (die, cu);
14438 switch (accessibility)
14439 {
14440 case DW_ACCESS_public:
14441 /* The assumed value if neither private nor protected. */
14442 break;
14443 case DW_ACCESS_private:
be2daae6 14444 fp.is_private = 1;
c191a687
KS
14445 break;
14446 case DW_ACCESS_protected:
be2daae6 14447 fp.is_protected = 1;
c191a687
KS
14448 break;
14449 default:
b98664d3 14450 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14451 }
14452
883fd55a 14453 if (die->tag == DW_TAG_typedef)
be2daae6 14454 fip->typedef_field_list.push_back (fp);
883fd55a 14455 else
be2daae6 14456 fip->nested_types_list.push_back (fp);
98751a41
JK
14457}
14458
c906108c
SS
14459/* Create the vector of fields, and attach it to the type. */
14460
14461static void
fba45db2 14462dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14463 struct dwarf2_cu *cu)
c906108c 14464{
317f7127 14465 int nfields = fip->nfields ();
c906108c
SS
14466
14467 /* Record the field count, allocate space for the array of fields,
14468 and create blank accessibility bitfields if necessary. */
14469 TYPE_NFIELDS (type) = nfields;
14470 TYPE_FIELDS (type) = (struct field *)
be2daae6 14471 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14472
b4ba55a1 14473 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14474 {
14475 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14476
14477 TYPE_FIELD_PRIVATE_BITS (type) =
14478 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14479 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14480
14481 TYPE_FIELD_PROTECTED_BITS (type) =
14482 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14483 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14484
774b6a14
TT
14485 TYPE_FIELD_IGNORE_BITS (type) =
14486 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14487 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14488 }
14489
14490 /* If the type has baseclasses, allocate and clear a bit vector for
14491 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14492 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14493 {
be2daae6 14494 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14495 unsigned char *pointer;
c906108c
SS
14496
14497 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14498 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14499 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14500 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14501 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14502 }
14503
2ddeaf8a
TT
14504 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14505 {
14506 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14507
be2daae6 14508 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14509 {
be2daae6
TT
14510 struct nextfield &field = fip->fields[index];
14511
14512 if (field.variant.is_discriminant)
2ddeaf8a 14513 di->discriminant_index = index;
be2daae6 14514 else if (field.variant.default_branch)
2ddeaf8a
TT
14515 di->default_index = index;
14516 else
be2daae6 14517 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14518 }
14519 }
14520
be2daae6
TT
14521 /* Copy the saved-up fields into the field vector. */
14522 for (int i = 0; i < nfields; ++i)
c906108c 14523 {
be2daae6
TT
14524 struct nextfield &field
14525 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14526 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14527
be2daae6
TT
14528 TYPE_FIELD (type, i) = field.field;
14529 switch (field.accessibility)
c906108c 14530 {
c5aa993b 14531 case DW_ACCESS_private:
b4ba55a1 14532 if (cu->language != language_ada)
be2daae6 14533 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14534 break;
c906108c 14535
c5aa993b 14536 case DW_ACCESS_protected:
b4ba55a1 14537 if (cu->language != language_ada)
be2daae6 14538 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14539 break;
c906108c 14540
c5aa993b
JM
14541 case DW_ACCESS_public:
14542 break;
c906108c 14543
c5aa993b
JM
14544 default:
14545 /* Unknown accessibility. Complain and treat it as public. */
14546 {
b98664d3 14547 complaint (_("unsupported accessibility %d"),
be2daae6 14548 field.accessibility);
c5aa993b
JM
14549 }
14550 break;
c906108c 14551 }
be2daae6 14552 if (i < fip->baseclasses.size ())
c906108c 14553 {
be2daae6 14554 switch (field.virtuality)
c906108c 14555 {
c5aa993b
JM
14556 case DW_VIRTUALITY_virtual:
14557 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14558 if (cu->language == language_ada)
a73c6dcd 14559 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14560 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14561 break;
c906108c
SS
14562 }
14563 }
c906108c
SS
14564 }
14565}
14566
7d27a96d
TT
14567/* Return true if this member function is a constructor, false
14568 otherwise. */
14569
14570static int
14571dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14572{
14573 const char *fieldname;
fe978cb0 14574 const char *type_name;
7d27a96d
TT
14575 int len;
14576
14577 if (die->parent == NULL)
14578 return 0;
14579
14580 if (die->parent->tag != DW_TAG_structure_type
14581 && die->parent->tag != DW_TAG_union_type
14582 && die->parent->tag != DW_TAG_class_type)
14583 return 0;
14584
14585 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14586 type_name = dwarf2_name (die->parent, cu);
14587 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14588 return 0;
14589
14590 len = strlen (fieldname);
fe978cb0
PA
14591 return (strncmp (fieldname, type_name, len) == 0
14592 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14593}
14594
e35000a7
TBA
14595/* Check if the given VALUE is a recognized enum
14596 dwarf_defaulted_attribute constant according to DWARF5 spec,
14597 Table 7.24. */
14598
14599static bool
14600is_valid_DW_AT_defaulted (ULONGEST value)
14601{
14602 switch (value)
14603 {
14604 case DW_DEFAULTED_no:
14605 case DW_DEFAULTED_in_class:
14606 case DW_DEFAULTED_out_of_class:
14607 return true;
14608 }
14609
3142e908 14610 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14611 return false;
14612}
14613
c906108c
SS
14614/* Add a member function to the proper fieldlist. */
14615
14616static void
107d2387 14617dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14618 struct type *type, struct dwarf2_cu *cu)
c906108c 14619{
518817b3 14620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14621 struct attribute *attr;
c906108c 14622 int i;
be2daae6 14623 struct fnfieldlist *flp = nullptr;
c906108c 14624 struct fn_field *fnp;
15d034d0 14625 const char *fieldname;
f792889a 14626 struct type *this_type;
60d5a603 14627 enum dwarf_access_attribute accessibility;
c906108c 14628
b4ba55a1 14629 if (cu->language == language_ada)
a73c6dcd 14630 error (_("unexpected member function in Ada type"));
b4ba55a1 14631
2df3850c 14632 /* Get name of member function. */
39cbfefa
DJ
14633 fieldname = dwarf2_name (die, cu);
14634 if (fieldname == NULL)
2df3850c 14635 return;
c906108c 14636
c906108c 14637 /* Look up member function name in fieldlist. */
be2daae6 14638 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14639 {
27bfe10e 14640 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14641 {
14642 flp = &fip->fnfieldlists[i];
14643 break;
14644 }
c906108c
SS
14645 }
14646
be2daae6
TT
14647 /* Create a new fnfieldlist if necessary. */
14648 if (flp == nullptr)
c906108c 14649 {
be2daae6
TT
14650 fip->fnfieldlists.emplace_back ();
14651 flp = &fip->fnfieldlists.back ();
c906108c 14652 flp->name = fieldname;
be2daae6 14653 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14654 }
14655
be2daae6
TT
14656 /* Create a new member function field and add it to the vector of
14657 fnfieldlists. */
14658 flp->fnfields.emplace_back ();
14659 fnp = &flp->fnfields.back ();
3da10d80
KS
14660
14661 /* Delay processing of the physname until later. */
9c37b5ae 14662 if (cu->language == language_cplus)
be2daae6
TT
14663 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14664 die, cu);
3da10d80
KS
14665 else
14666 {
1d06ead6 14667 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14668 fnp->physname = physname ? physname : "";
14669 }
14670
c906108c 14671 fnp->type = alloc_type (objfile);
f792889a
DJ
14672 this_type = read_type_die (die, cu);
14673 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14674 {
f792889a 14675 int nparams = TYPE_NFIELDS (this_type);
c906108c 14676
f792889a 14677 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14678 of the method itself (TYPE_CODE_METHOD). */
14679 smash_to_method_type (fnp->type, type,
f792889a
DJ
14680 TYPE_TARGET_TYPE (this_type),
14681 TYPE_FIELDS (this_type),
14682 TYPE_NFIELDS (this_type),
14683 TYPE_VARARGS (this_type));
c906108c
SS
14684
14685 /* Handle static member functions.
c5aa993b 14686 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14687 member functions. G++ helps GDB by marking the first
14688 parameter for non-static member functions (which is the this
14689 pointer) as artificial. We obtain this information from
14690 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14691 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14692 fnp->voffset = VOFFSET_STATIC;
14693 }
14694 else
b98664d3 14695 complaint (_("member function type missing for '%s'"),
3da10d80 14696 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14697
14698 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14699 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14700 fnp->fcontext = die_containing_type (die, cu);
c906108c 14701
3e43a32a
MS
14702 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14703 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14704
14705 /* Get accessibility. */
e142c38c 14706 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14707 if (attr != nullptr)
aead7601 14708 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14709 else
14710 accessibility = dwarf2_default_access_attribute (die, cu);
14711 switch (accessibility)
c906108c 14712 {
60d5a603
JK
14713 case DW_ACCESS_private:
14714 fnp->is_private = 1;
14715 break;
14716 case DW_ACCESS_protected:
14717 fnp->is_protected = 1;
14718 break;
c906108c
SS
14719 }
14720
b02dede2 14721 /* Check for artificial methods. */
e142c38c 14722 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14723 if (attr && DW_UNSND (attr) != 0)
14724 fnp->is_artificial = 1;
14725
e35000a7
TBA
14726 /* Check for defaulted methods. */
14727 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14728 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14729 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14730
14731 /* Check for deleted methods. */
14732 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14733 if (attr != nullptr && DW_UNSND (attr) != 0)
14734 fnp->is_deleted = 1;
14735
7d27a96d
TT
14736 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14737
0d564a31 14738 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14739 function. For older versions of GCC, this is an offset in the
14740 appropriate virtual table, as specified by DW_AT_containing_type.
14741 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14742 to the object address. */
14743
e142c38c 14744 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14745 if (attr != nullptr)
8e19ed76 14746 {
4fc6c0d5 14747 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14748 {
aec5aa8b
TT
14749 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14750 {
14751 /* Old-style GCC. */
14752 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14753 }
14754 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14755 || (DW_BLOCK (attr)->size > 1
14756 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14757 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14758 {
aec5aa8b
TT
14759 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14760 if ((fnp->voffset % cu->header.addr_size) != 0)
14761 dwarf2_complex_location_expr_complaint ();
14762 else
14763 fnp->voffset /= cu->header.addr_size;
14764 fnp->voffset += 2;
14765 }
14766 else
14767 dwarf2_complex_location_expr_complaint ();
14768
14769 if (!fnp->fcontext)
7e993ebf
KS
14770 {
14771 /* If there is no `this' field and no DW_AT_containing_type,
14772 we cannot actually find a base class context for the
14773 vtable! */
14774 if (TYPE_NFIELDS (this_type) == 0
14775 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14776 {
b98664d3 14777 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14778 "function \"%s\" (offset %s)"),
14779 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14780 }
14781 else
14782 {
14783 fnp->fcontext
14784 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14785 }
14786 }
aec5aa8b 14787 }
cd6c91b4 14788 else if (attr->form_is_section_offset ())
8e19ed76 14789 {
4d3c2250 14790 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14791 }
14792 else
14793 {
4d3c2250
KB
14794 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14795 fieldname);
8e19ed76 14796 }
0d564a31 14797 }
d48cc9dd
DJ
14798 else
14799 {
14800 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14801 if (attr && DW_UNSND (attr))
14802 {
14803 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14804 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14805 "but the vtable offset is not specified"),
9d8780f0 14806 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14807 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14808 TYPE_CPLUS_DYNAMIC (type) = 1;
14809 }
14810 }
c906108c
SS
14811}
14812
14813/* Create the vector of member function fields, and attach it to the type. */
14814
14815static void
fba45db2 14816dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14817 struct dwarf2_cu *cu)
c906108c 14818{
b4ba55a1 14819 if (cu->language == language_ada)
a73c6dcd 14820 error (_("unexpected member functions in Ada type"));
b4ba55a1 14821
c906108c
SS
14822 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14823 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14824 TYPE_ALLOC (type,
14825 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14826
be2daae6 14827 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14828 {
be2daae6 14829 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14830 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14831
be2daae6
TT
14832 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14833 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14834 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14835 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14836
14837 for (int k = 0; k < nf.fnfields.size (); ++k)
14838 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14839 }
14840
be2daae6 14841 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14842}
14843
1168df01
JB
14844/* Returns non-zero if NAME is the name of a vtable member in CU's
14845 language, zero otherwise. */
14846static int
14847is_vtable_name (const char *name, struct dwarf2_cu *cu)
14848{
14849 static const char vptr[] = "_vptr";
14850
9c37b5ae
TT
14851 /* Look for the C++ form of the vtable. */
14852 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14853 return 1;
14854
14855 return 0;
14856}
14857
c0dd20ea 14858/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14859 functions, with the ABI-specified layout. If TYPE describes
14860 such a structure, smash it into a member function type.
61049d3b
DJ
14861
14862 GCC shouldn't do this; it should just output pointer to member DIEs.
14863 This is GCC PR debug/28767. */
c0dd20ea 14864
0b92b5bb
TT
14865static void
14866quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14867{
09e2d7c7 14868 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14869
14870 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14871 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14872 return;
c0dd20ea
DJ
14873
14874 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14875 if (TYPE_FIELD_NAME (type, 0) == NULL
14876 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14877 || TYPE_FIELD_NAME (type, 1) == NULL
14878 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14879 return;
c0dd20ea
DJ
14880
14881 /* Find the type of the method. */
0b92b5bb 14882 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14883 if (pfn_type == NULL
14884 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14885 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14886 return;
c0dd20ea
DJ
14887
14888 /* Look for the "this" argument. */
14889 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14890 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14891 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14892 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14893 return;
c0dd20ea 14894
09e2d7c7 14895 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14896 new_type = alloc_type (objfile);
09e2d7c7 14897 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14898 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14899 TYPE_VARARGS (pfn_type));
0b92b5bb 14900 smash_to_methodptr_type (type, new_type);
c0dd20ea 14901}
1168df01 14902
2b4424c3
TT
14903/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14904 appropriate error checking and issuing complaints if there is a
14905 problem. */
14906
14907static ULONGEST
14908get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14909{
14910 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14911
14912 if (attr == nullptr)
14913 return 0;
14914
cd6c91b4 14915 if (!attr->form_is_constant ())
2b4424c3 14916 {
b98664d3 14917 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14918 " - DIE at %s [in module %s]"),
14919 sect_offset_str (die->sect_off),
14920 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14921 return 0;
14922 }
14923
14924 ULONGEST align;
14925 if (attr->form == DW_FORM_sdata)
14926 {
14927 LONGEST val = DW_SND (attr);
14928 if (val < 0)
14929 {
b98664d3 14930 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14931 " - DIE at %s [in module %s]"),
14932 sect_offset_str (die->sect_off),
14933 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14934 return 0;
14935 }
14936 align = val;
14937 }
14938 else
14939 align = DW_UNSND (attr);
14940
14941 if (align == 0)
14942 {
b98664d3 14943 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14944 " - DIE at %s [in module %s]"),
14945 sect_offset_str (die->sect_off),
14946 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14947 return 0;
14948 }
14949 if ((align & (align - 1)) != 0)
14950 {
b98664d3 14951 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14952 " - DIE at %s [in module %s]"),
14953 sect_offset_str (die->sect_off),
14954 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14955 return 0;
14956 }
14957
14958 return align;
14959}
14960
14961/* If the DIE has a DW_AT_alignment attribute, use its value to set
14962 the alignment for TYPE. */
14963
14964static void
14965maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14966 struct type *type)
14967{
14968 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14969 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14970 " - DIE at %s [in module %s]"),
14971 sect_offset_str (die->sect_off),
14972 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14973}
685b1105 14974
e35000a7
TBA
14975/* Check if the given VALUE is a valid enum dwarf_calling_convention
14976 constant for a type, according to DWARF5 spec, Table 5.5. */
14977
14978static bool
14979is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14980{
14981 switch (value)
14982 {
14983 case DW_CC_normal:
14984 case DW_CC_pass_by_reference:
14985 case DW_CC_pass_by_value:
14986 return true;
14987
14988 default:
14989 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14990 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14991 return false;
14992 }
14993}
14994
d0922fcf
TBA
14995/* Check if the given VALUE is a valid enum dwarf_calling_convention
14996 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14997 also according to GNU-specific values (see include/dwarf2.h). */
14998
14999static bool
15000is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15001{
15002 switch (value)
15003 {
15004 case DW_CC_normal:
15005 case DW_CC_program:
15006 case DW_CC_nocall:
15007 return true;
15008
15009 case DW_CC_GNU_renesas_sh:
15010 case DW_CC_GNU_borland_fastcall_i386:
15011 case DW_CC_GDB_IBM_OpenCL:
15012 return true;
15013
15014 default:
15015 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15016 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15017 return false;
15018 }
15019}
15020
c906108c 15021/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15022 (definition) to create a type for the structure or union. Fill in
15023 the type's name and general properties; the members will not be
83655187
DE
15024 processed until process_structure_scope. A symbol table entry for
15025 the type will also not be done until process_structure_scope (assuming
15026 the type has a name).
c906108c 15027
c767944b
DJ
15028 NOTE: we need to call these functions regardless of whether or not the
15029 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15030 structure or union. This gets the type entered into our set of
83655187 15031 user defined types. */
c906108c 15032
f792889a 15033static struct type *
134d01f1 15034read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15035{
518817b3 15036 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15037 struct type *type;
15038 struct attribute *attr;
15d034d0 15039 const char *name;
c906108c 15040
348e048f
DE
15041 /* If the definition of this type lives in .debug_types, read that type.
15042 Don't follow DW_AT_specification though, that will take us back up
15043 the chain and we want to go down. */
45e58e77 15044 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15045 if (attr != nullptr)
348e048f 15046 {
ac9ec31b 15047 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15048
ac9ec31b 15049 /* The type's CU may not be the same as CU.
02142a6c 15050 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15051 return set_die_type (die, type, cu);
15052 }
15053
c0dd20ea 15054 type = alloc_type (objfile);
c906108c 15055 INIT_CPLUS_SPECIFIC (type);
93311388 15056
39cbfefa
DJ
15057 name = dwarf2_name (die, cu);
15058 if (name != NULL)
c906108c 15059 {
987504bb 15060 if (cu->language == language_cplus
c44af4eb
TT
15061 || cu->language == language_d
15062 || cu->language == language_rust)
63d06c5c 15063 {
15d034d0 15064 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15065
15066 /* dwarf2_full_name might have already finished building the DIE's
15067 type. If so, there is no need to continue. */
15068 if (get_die_type (die, cu) != NULL)
15069 return get_die_type (die, cu);
15070
e86ca25f 15071 TYPE_NAME (type) = full_name;
63d06c5c
DC
15072 }
15073 else
15074 {
d8151005
DJ
15075 /* The name is already allocated along with this objfile, so
15076 we don't need to duplicate it for the type. */
e86ca25f 15077 TYPE_NAME (type) = name;
63d06c5c 15078 }
c906108c
SS
15079 }
15080
15081 if (die->tag == DW_TAG_structure_type)
15082 {
15083 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15084 }
15085 else if (die->tag == DW_TAG_union_type)
15086 {
15087 TYPE_CODE (type) = TYPE_CODE_UNION;
15088 }
2ddeaf8a
TT
15089 else if (die->tag == DW_TAG_variant_part)
15090 {
15091 TYPE_CODE (type) = TYPE_CODE_UNION;
15092 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15093 }
c906108c
SS
15094 else
15095 {
4753d33b 15096 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15097 }
15098
0cc2414c
TT
15099 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15100 TYPE_DECLARED_CLASS (type) = 1;
15101
e35000a7
TBA
15102 /* Store the calling convention in the type if it's available in
15103 the die. Otherwise the calling convention remains set to
15104 the default value DW_CC_normal. */
15105 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15106 if (attr != nullptr
15107 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15108 {
15109 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15110 TYPE_CPLUS_CALLING_CONVENTION (type)
15111 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15112 }
15113
e142c38c 15114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15115 if (attr != nullptr)
c906108c 15116 {
cd6c91b4 15117 if (attr->form_is_constant ())
155bfbd3
JB
15118 TYPE_LENGTH (type) = DW_UNSND (attr);
15119 else
15120 {
15121 /* For the moment, dynamic type sizes are not supported
15122 by GDB's struct type. The actual size is determined
15123 on-demand when resolving the type of a given object,
15124 so set the type's length to zero for now. Otherwise,
15125 we record an expression as the length, and that expression
15126 could lead to a very large value, which could eventually
15127 lead to us trying to allocate that much memory when creating
15128 a value of that type. */
15129 TYPE_LENGTH (type) = 0;
15130 }
c906108c
SS
15131 }
15132 else
15133 {
15134 TYPE_LENGTH (type) = 0;
15135 }
15136
2b4424c3
TT
15137 maybe_set_alignment (cu, die, type);
15138
5230b05a 15139 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15140 {
5230b05a
WT
15141 /* ICC<14 does not output the required DW_AT_declaration on
15142 incomplete types, but gives them a size of zero. */
422b1cb0 15143 TYPE_STUB (type) = 1;
685b1105
JK
15144 }
15145 else
15146 TYPE_STUB_SUPPORTED (type) = 1;
15147
dc718098 15148 if (die_is_declaration (die, cu))
876cecd0 15149 TYPE_STUB (type) = 1;
a6c727b2
DJ
15150 else if (attr == NULL && die->child == NULL
15151 && producer_is_realview (cu->producer))
15152 /* RealView does not output the required DW_AT_declaration
15153 on incomplete types. */
15154 TYPE_STUB (type) = 1;
dc718098 15155
c906108c
SS
15156 /* We need to add the type field to the die immediately so we don't
15157 infinitely recurse when dealing with pointers to the structure
0963b4bd 15158 type within the structure itself. */
1c379e20 15159 set_die_type (die, type, cu);
c906108c 15160
7e314c57
JK
15161 /* set_die_type should be already done. */
15162 set_descriptive_type (type, die, cu);
15163
c767944b
DJ
15164 return type;
15165}
15166
2ddeaf8a
TT
15167/* A helper for process_structure_scope that handles a single member
15168 DIE. */
15169
15170static void
15171handle_struct_member_die (struct die_info *child_die, struct type *type,
15172 struct field_info *fi,
15173 std::vector<struct symbol *> *template_args,
15174 struct dwarf2_cu *cu)
15175{
15176 if (child_die->tag == DW_TAG_member
15177 || child_die->tag == DW_TAG_variable
15178 || child_die->tag == DW_TAG_variant_part)
15179 {
15180 /* NOTE: carlton/2002-11-05: A C++ static data member
15181 should be a DW_TAG_member that is a declaration, but
15182 all versions of G++ as of this writing (so through at
15183 least 3.2.1) incorrectly generate DW_TAG_variable
15184 tags for them instead. */
15185 dwarf2_add_field (fi, child_die, cu);
15186 }
15187 else if (child_die->tag == DW_TAG_subprogram)
15188 {
15189 /* Rust doesn't have member functions in the C++ sense.
15190 However, it does emit ordinary functions as children
15191 of a struct DIE. */
15192 if (cu->language == language_rust)
15193 read_func_scope (child_die, cu);
15194 else
15195 {
15196 /* C++ member function. */
15197 dwarf2_add_member_fn (fi, child_die, type, cu);
15198 }
15199 }
15200 else if (child_die->tag == DW_TAG_inheritance)
15201 {
15202 /* C++ base class field. */
15203 dwarf2_add_field (fi, child_die, cu);
15204 }
15205 else if (type_can_define_types (child_die))
15206 dwarf2_add_type_defn (fi, child_die, cu);
15207 else if (child_die->tag == DW_TAG_template_type_param
15208 || child_die->tag == DW_TAG_template_value_param)
15209 {
15210 struct symbol *arg = new_symbol (child_die, NULL, cu);
15211
15212 if (arg != NULL)
15213 template_args->push_back (arg);
15214 }
15215 else if (child_die->tag == DW_TAG_variant)
15216 {
15217 /* In a variant we want to get the discriminant and also add a
15218 field for our sole member child. */
15219 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15220
bde09ab7 15221 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15222 variant_child != NULL;
15223 variant_child = sibling_die (variant_child))
15224 {
15225 if (variant_child->tag == DW_TAG_member)
15226 {
15227 handle_struct_member_die (variant_child, type, fi,
15228 template_args, cu);
15229 /* Only handle the one. */
15230 break;
15231 }
15232 }
15233
15234 /* We don't handle this but we might as well report it if we see
15235 it. */
15236 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15237 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15238 " - DIE at %s [in module %s]"),
15239 sect_offset_str (child_die->sect_off),
15240 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15241
15242 /* The first field was just added, so we can stash the
15243 discriminant there. */
be2daae6 15244 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15245 if (discr == NULL)
be2daae6 15246 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15247 else
be2daae6 15248 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15249 }
15250}
15251
c767944b
DJ
15252/* Finish creating a structure or union type, including filling in
15253 its members and creating a symbol for it. */
15254
15255static void
15256process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15257{
518817b3 15258 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15259 struct die_info *child_die;
c767944b
DJ
15260 struct type *type;
15261
15262 type = get_die_type (die, cu);
15263 if (type == NULL)
15264 type = read_structure_type (die, cu);
15265
2ddeaf8a
TT
15266 /* When reading a DW_TAG_variant_part, we need to notice when we
15267 read the discriminant member, so we can record it later in the
15268 discriminant_info. */
15269 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15270 sect_offset discr_offset {};
3e1d3d8c 15271 bool has_template_parameters = false;
2ddeaf8a
TT
15272
15273 if (is_variant_part)
15274 {
15275 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15276 if (discr == NULL)
15277 {
15278 /* Maybe it's a univariant form, an extension we support.
15279 In this case arrange not to check the offset. */
15280 is_variant_part = false;
15281 }
cd6c91b4 15282 else if (discr->form_is_ref ())
2ddeaf8a
TT
15283 {
15284 struct dwarf2_cu *target_cu = cu;
15285 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15286
15287 discr_offset = target_die->sect_off;
15288 }
15289 else
15290 {
b98664d3 15291 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15292 " - DIE at %s [in module %s]"),
15293 sect_offset_str (die->sect_off),
15294 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15295 is_variant_part = false;
15296 }
15297 }
15298
e142c38c 15299 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15300 {
15301 struct field_info fi;
2f4732b0 15302 std::vector<struct symbol *> template_args;
c906108c 15303
639d11d3 15304 child_die = die->child;
c906108c
SS
15305
15306 while (child_die && child_die->tag)
15307 {
2ddeaf8a 15308 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15309
2ddeaf8a 15310 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15311 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15312
c906108c
SS
15313 child_die = sibling_die (child_die);
15314 }
15315
34eaf542 15316 /* Attach template arguments to type. */
2f4732b0 15317 if (!template_args.empty ())
34eaf542 15318 {
3e1d3d8c 15319 has_template_parameters = true;
34eaf542 15320 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15321 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15322 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15323 = XOBNEWVEC (&objfile->objfile_obstack,
15324 struct symbol *,
15325 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15326 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15327 template_args.data (),
34eaf542
TT
15328 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15329 * sizeof (struct symbol *)));
34eaf542
TT
15330 }
15331
c906108c 15332 /* Attach fields and member functions to the type. */
317f7127 15333 if (fi.nfields () > 0)
e7c27a73 15334 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15335 if (!fi.fnfieldlists.empty ())
c906108c 15336 {
e7c27a73 15337 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15338
c5aa993b 15339 /* Get the type which refers to the base class (possibly this
c906108c 15340 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15341 class from the DW_AT_containing_type attribute. This use of
15342 DW_AT_containing_type is a GNU extension. */
c906108c 15343
e142c38c 15344 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15345 {
e7c27a73 15346 struct type *t = die_containing_type (die, cu);
c906108c 15347
ae6ae975 15348 set_type_vptr_basetype (type, t);
c906108c
SS
15349 if (type == t)
15350 {
c906108c
SS
15351 int i;
15352
15353 /* Our own class provides vtbl ptr. */
15354 for (i = TYPE_NFIELDS (t) - 1;
15355 i >= TYPE_N_BASECLASSES (t);
15356 --i)
15357 {
0d5cff50 15358 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15359
1168df01 15360 if (is_vtable_name (fieldname, cu))
c906108c 15361 {
ae6ae975 15362 set_type_vptr_fieldno (type, i);
c906108c
SS
15363 break;
15364 }
15365 }
15366
15367 /* Complain if virtual function table field not found. */
15368 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15369 complaint (_("virtual function table pointer "
3e43a32a 15370 "not found when defining class '%s'"),
e86ca25f 15371 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15372 }
15373 else
15374 {
ae6ae975 15375 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15376 }
15377 }
f6235d4c 15378 else if (cu->producer
61012eef 15379 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15380 {
15381 /* The IBM XLC compiler does not provide direct indication
15382 of the containing type, but the vtable pointer is
15383 always named __vfp. */
15384
15385 int i;
15386
15387 for (i = TYPE_NFIELDS (type) - 1;
15388 i >= TYPE_N_BASECLASSES (type);
15389 --i)
15390 {
15391 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15392 {
ae6ae975
DE
15393 set_type_vptr_fieldno (type, i);
15394 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15395 break;
15396 }
15397 }
15398 }
c906108c 15399 }
98751a41
JK
15400
15401 /* Copy fi.typedef_field_list linked list elements content into the
15402 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15403 if (!fi.typedef_field_list.empty ())
98751a41 15404 {
be2daae6 15405 int count = fi.typedef_field_list.size ();
98751a41 15406
a0d7a4ff 15407 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15408 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15409 = ((struct decl_field *)
be2daae6
TT
15410 TYPE_ALLOC (type,
15411 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15412 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15413
be2daae6
TT
15414 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15415 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15416 }
c767944b 15417
883fd55a
KS
15418 /* Copy fi.nested_types_list linked list elements content into the
15419 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15420 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15421 {
be2daae6 15422 int count = fi.nested_types_list.size ();
883fd55a
KS
15423
15424 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15425 TYPE_NESTED_TYPES_ARRAY (type)
15426 = ((struct decl_field *)
be2daae6
TT
15427 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15428 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15429
be2daae6
TT
15430 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15431 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15432 }
c906108c 15433 }
63d06c5c 15434
bb5ed363 15435 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15436 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15437 cu->rust_unions.push_back (type);
0b92b5bb 15438
90aeadfc
DC
15439 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15440 snapshots) has been known to create a die giving a declaration
15441 for a class that has, as a child, a die giving a definition for a
15442 nested class. So we have to process our children even if the
15443 current die is a declaration. Normally, of course, a declaration
15444 won't have any children at all. */
134d01f1 15445
ca040673
DE
15446 child_die = die->child;
15447
90aeadfc
DC
15448 while (child_die != NULL && child_die->tag)
15449 {
15450 if (child_die->tag == DW_TAG_member
15451 || child_die->tag == DW_TAG_variable
34eaf542
TT
15452 || child_die->tag == DW_TAG_inheritance
15453 || child_die->tag == DW_TAG_template_value_param
15454 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15455 {
90aeadfc 15456 /* Do nothing. */
134d01f1 15457 }
90aeadfc
DC
15458 else
15459 process_die (child_die, cu);
134d01f1 15460
90aeadfc 15461 child_die = sibling_die (child_die);
134d01f1
DJ
15462 }
15463
fa4028e9
JB
15464 /* Do not consider external references. According to the DWARF standard,
15465 these DIEs are identified by the fact that they have no byte_size
15466 attribute, and a declaration attribute. */
15467 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15468 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15469 {
15470 struct symbol *sym = new_symbol (die, type, cu);
15471
15472 if (has_template_parameters)
15473 {
a776957c
TT
15474 struct symtab *symtab;
15475 if (sym != nullptr)
15476 symtab = symbol_symtab (sym);
15477 else if (cu->line_header != nullptr)
15478 {
15479 /* Any related symtab will do. */
15480 symtab
7ba99d21 15481 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15482 }
15483 else
15484 {
15485 symtab = nullptr;
15486 complaint (_("could not find suitable "
15487 "symtab for template parameter"
15488 " - DIE at %s [in module %s]"),
15489 sect_offset_str (die->sect_off),
15490 objfile_name (objfile));
15491 }
15492
15493 if (symtab != nullptr)
15494 {
15495 /* Make sure that the symtab is set on the new symbols.
15496 Even though they don't appear in this symtab directly,
15497 other parts of gdb assume that symbols do, and this is
15498 reasonably true. */
15499 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15500 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15501 }
3e1d3d8c
TT
15502 }
15503 }
134d01f1
DJ
15504}
15505
55426c9d
JB
15506/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15507 update TYPE using some information only available in DIE's children. */
15508
15509static void
15510update_enumeration_type_from_children (struct die_info *die,
15511 struct type *type,
15512 struct dwarf2_cu *cu)
15513{
60f7655a 15514 struct die_info *child_die;
55426c9d
JB
15515 int unsigned_enum = 1;
15516 int flag_enum = 1;
55426c9d 15517
8268c778 15518 auto_obstack obstack;
55426c9d 15519
60f7655a
DE
15520 for (child_die = die->child;
15521 child_die != NULL && child_die->tag;
15522 child_die = sibling_die (child_die))
55426c9d
JB
15523 {
15524 struct attribute *attr;
15525 LONGEST value;
15526 const gdb_byte *bytes;
15527 struct dwarf2_locexpr_baton *baton;
15528 const char *name;
60f7655a 15529
55426c9d
JB
15530 if (child_die->tag != DW_TAG_enumerator)
15531 continue;
15532
15533 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15534 if (attr == NULL)
15535 continue;
15536
15537 name = dwarf2_name (child_die, cu);
15538 if (name == NULL)
15539 name = "<anonymous enumerator>";
15540
15541 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15542 &value, &bytes, &baton);
15543 if (value < 0)
15544 {
15545 unsigned_enum = 0;
15546 flag_enum = 0;
15547 }
55426c9d 15548 else
edd45eb0
SM
15549 {
15550 if (count_one_bits_ll (value) >= 2)
15551 flag_enum = 0;
edd45eb0 15552 }
55426c9d
JB
15553
15554 /* If we already know that the enum type is neither unsigned, nor
15555 a flag type, no need to look at the rest of the enumerates. */
15556 if (!unsigned_enum && !flag_enum)
15557 break;
55426c9d
JB
15558 }
15559
15560 if (unsigned_enum)
15561 TYPE_UNSIGNED (type) = 1;
15562 if (flag_enum)
15563 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15564}
15565
134d01f1
DJ
15566/* Given a DW_AT_enumeration_type die, set its type. We do not
15567 complete the type's fields yet, or create any symbols. */
c906108c 15568
f792889a 15569static struct type *
134d01f1 15570read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15571{
518817b3 15572 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15573 struct type *type;
c906108c 15574 struct attribute *attr;
0114d602 15575 const char *name;
134d01f1 15576
348e048f
DE
15577 /* If the definition of this type lives in .debug_types, read that type.
15578 Don't follow DW_AT_specification though, that will take us back up
15579 the chain and we want to go down. */
45e58e77 15580 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15581 if (attr != nullptr)
348e048f 15582 {
ac9ec31b 15583 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15584
ac9ec31b 15585 /* The type's CU may not be the same as CU.
02142a6c 15586 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15587 return set_die_type (die, type, cu);
15588 }
15589
c906108c
SS
15590 type = alloc_type (objfile);
15591
15592 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15593 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15594 if (name != NULL)
e86ca25f 15595 TYPE_NAME (type) = name;
c906108c 15596
0626fc76
TT
15597 attr = dwarf2_attr (die, DW_AT_type, cu);
15598 if (attr != NULL)
15599 {
15600 struct type *underlying_type = die_type (die, cu);
15601
15602 TYPE_TARGET_TYPE (type) = underlying_type;
15603 }
15604
e142c38c 15605 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15606 if (attr != nullptr)
c906108c
SS
15607 {
15608 TYPE_LENGTH (type) = DW_UNSND (attr);
15609 }
15610 else
15611 {
15612 TYPE_LENGTH (type) = 0;
15613 }
15614
2b4424c3
TT
15615 maybe_set_alignment (cu, die, type);
15616
137033e9
JB
15617 /* The enumeration DIE can be incomplete. In Ada, any type can be
15618 declared as private in the package spec, and then defined only
15619 inside the package body. Such types are known as Taft Amendment
15620 Types. When another package uses such a type, an incomplete DIE
15621 may be generated by the compiler. */
02eb380e 15622 if (die_is_declaration (die, cu))
876cecd0 15623 TYPE_STUB (type) = 1;
02eb380e 15624
0626fc76
TT
15625 /* Finish the creation of this type by using the enum's children.
15626 We must call this even when the underlying type has been provided
15627 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15628 update_enumeration_type_from_children (die, type, cu);
15629
0626fc76
TT
15630 /* If this type has an underlying type that is not a stub, then we
15631 may use its attributes. We always use the "unsigned" attribute
15632 in this situation, because ordinarily we guess whether the type
15633 is unsigned -- but the guess can be wrong and the underlying type
15634 can tell us the reality. However, we defer to a local size
15635 attribute if one exists, because this lets the compiler override
15636 the underlying type if needed. */
15637 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15638 {
15639 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15640 if (TYPE_LENGTH (type) == 0)
15641 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15642 if (TYPE_RAW_ALIGN (type) == 0
15643 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15644 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15645 }
15646
3d567982
TT
15647 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15648
f792889a 15649 return set_die_type (die, type, cu);
134d01f1
DJ
15650}
15651
15652/* Given a pointer to a die which begins an enumeration, process all
15653 the dies that define the members of the enumeration, and create the
15654 symbol for the enumeration type.
15655
15656 NOTE: We reverse the order of the element list. */
15657
15658static void
15659process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15660{
f792889a 15661 struct type *this_type;
134d01f1 15662
f792889a
DJ
15663 this_type = get_die_type (die, cu);
15664 if (this_type == NULL)
15665 this_type = read_enumeration_type (die, cu);
9dc481d3 15666
639d11d3 15667 if (die->child != NULL)
c906108c 15668 {
9dc481d3
DE
15669 struct die_info *child_die;
15670 struct symbol *sym;
43816ebc 15671 std::vector<struct field> fields;
15d034d0 15672 const char *name;
9dc481d3 15673
639d11d3 15674 child_die = die->child;
c906108c
SS
15675 while (child_die && child_die->tag)
15676 {
15677 if (child_die->tag != DW_TAG_enumerator)
15678 {
e7c27a73 15679 process_die (child_die, cu);
c906108c
SS
15680 }
15681 else
15682 {
39cbfefa
DJ
15683 name = dwarf2_name (child_die, cu);
15684 if (name)
c906108c 15685 {
f792889a 15686 sym = new_symbol (child_die, this_type, cu);
c906108c 15687
43816ebc
TT
15688 fields.emplace_back ();
15689 struct field &field = fields.back ();
c906108c 15690
43816ebc
TT
15691 FIELD_NAME (field) = sym->linkage_name ();
15692 FIELD_TYPE (field) = NULL;
15693 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15694 FIELD_BITSIZE (field) = 0;
c906108c
SS
15695 }
15696 }
15697
15698 child_die = sibling_die (child_die);
15699 }
15700
43816ebc 15701 if (!fields.empty ())
c906108c 15702 {
43816ebc 15703 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15704 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15705 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15706 memcpy (TYPE_FIELDS (this_type), fields.data (),
15707 sizeof (struct field) * fields.size ());
c906108c 15708 }
c906108c 15709 }
134d01f1 15710
6c83ed52
TT
15711 /* If we are reading an enum from a .debug_types unit, and the enum
15712 is a declaration, and the enum is not the signatured type in the
15713 unit, then we do not want to add a symbol for it. Adding a
15714 symbol would in some cases obscure the true definition of the
15715 enum, giving users an incomplete type when the definition is
15716 actually available. Note that we do not want to do this for all
15717 enums which are just declarations, because C++0x allows forward
15718 enum declarations. */
3019eac3 15719 if (cu->per_cu->is_debug_types
6c83ed52
TT
15720 && die_is_declaration (die, cu))
15721 {
52dc124a 15722 struct signatured_type *sig_type;
6c83ed52 15723
c0f78cd4 15724 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15725 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15726 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15727 return;
15728 }
15729
f792889a 15730 new_symbol (die, this_type, cu);
c906108c
SS
15731}
15732
15733/* Extract all information from a DW_TAG_array_type DIE and put it in
15734 the DIE's type field. For now, this only handles one dimensional
15735 arrays. */
15736
f792889a 15737static struct type *
e7c27a73 15738read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15739{
518817b3 15740 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15741 struct die_info *child_die;
7e314c57 15742 struct type *type;
c906108c 15743 struct type *element_type, *range_type, *index_type;
c906108c 15744 struct attribute *attr;
15d034d0 15745 const char *name;
a405673c 15746 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15747 unsigned int bit_stride = 0;
c906108c 15748
e7c27a73 15749 element_type = die_type (die, cu);
c906108c 15750
7e314c57
JK
15751 /* The die_type call above may have already set the type for this DIE. */
15752 type = get_die_type (die, cu);
15753 if (type)
15754 return type;
15755
dc53a7ad
JB
15756 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15757 if (attr != NULL)
a405673c
JB
15758 {
15759 int stride_ok;
09ba997f 15760 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15761
15762 byte_stride_prop
15763 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15764 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15765 prop_type);
a405673c
JB
15766 if (!stride_ok)
15767 {
b98664d3 15768 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15769 " - DIE at %s [in module %s]"),
15770 sect_offset_str (die->sect_off),
518817b3 15771 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15772 /* Ignore this attribute. We will likely not be able to print
15773 arrays of this type correctly, but there is little we can do
15774 to help if we cannot read the attribute's value. */
15775 byte_stride_prop = NULL;
15776 }
15777 }
dc53a7ad
JB
15778
15779 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15780 if (attr != NULL)
15781 bit_stride = DW_UNSND (attr);
15782
c906108c
SS
15783 /* Irix 6.2 native cc creates array types without children for
15784 arrays with unspecified length. */
639d11d3 15785 if (die->child == NULL)
c906108c 15786 {
46bf5051 15787 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15788 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15789 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15790 byte_stride_prop, bit_stride);
f792889a 15791 return set_die_type (die, type, cu);
c906108c
SS
15792 }
15793
791afaa2 15794 std::vector<struct type *> range_types;
639d11d3 15795 child_die = die->child;
c906108c
SS
15796 while (child_die && child_die->tag)
15797 {
15798 if (child_die->tag == DW_TAG_subrange_type)
15799 {
f792889a 15800 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15801
f792889a 15802 if (child_type != NULL)
a02abb62 15803 {
0963b4bd
MS
15804 /* The range type was succesfully read. Save it for the
15805 array type creation. */
791afaa2 15806 range_types.push_back (child_type);
a02abb62 15807 }
c906108c
SS
15808 }
15809 child_die = sibling_die (child_die);
15810 }
15811
15812 /* Dwarf2 dimensions are output from left to right, create the
15813 necessary array types in backwards order. */
7ca2d3a3 15814
c906108c 15815 type = element_type;
7ca2d3a3
DL
15816
15817 if (read_array_order (die, cu) == DW_ORD_col_major)
15818 {
15819 int i = 0;
9a619af0 15820
791afaa2 15821 while (i < range_types.size ())
dc53a7ad 15822 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15823 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15824 }
15825 else
15826 {
791afaa2 15827 size_t ndim = range_types.size ();
7ca2d3a3 15828 while (ndim-- > 0)
dc53a7ad 15829 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15830 byte_stride_prop, bit_stride);
7ca2d3a3 15831 }
c906108c 15832
f5f8a009
EZ
15833 /* Understand Dwarf2 support for vector types (like they occur on
15834 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15835 array type. This is not part of the Dwarf2/3 standard yet, but a
15836 custom vendor extension. The main difference between a regular
15837 array and the vector variant is that vectors are passed by value
15838 to functions. */
e142c38c 15839 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15840 if (attr != nullptr)
ea37ba09 15841 make_vector_type (type);
f5f8a009 15842
dbc98a8b
KW
15843 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15844 implementation may choose to implement triple vectors using this
15845 attribute. */
15846 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15847 if (attr != nullptr)
dbc98a8b
KW
15848 {
15849 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15850 TYPE_LENGTH (type) = DW_UNSND (attr);
15851 else
b98664d3 15852 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15853 "than the total size of elements"));
dbc98a8b
KW
15854 }
15855
39cbfefa
DJ
15856 name = dwarf2_name (die, cu);
15857 if (name)
15858 TYPE_NAME (type) = name;
6e70227d 15859
2b4424c3
TT
15860 maybe_set_alignment (cu, die, type);
15861
0963b4bd 15862 /* Install the type in the die. */
7e314c57
JK
15863 set_die_type (die, type, cu);
15864
15865 /* set_die_type should be already done. */
b4ba55a1
JB
15866 set_descriptive_type (type, die, cu);
15867
7e314c57 15868 return type;
c906108c
SS
15869}
15870
7ca2d3a3 15871static enum dwarf_array_dim_ordering
6e70227d 15872read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15873{
15874 struct attribute *attr;
15875
15876 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15877
435d3d88 15878 if (attr != nullptr)
aead7601 15879 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15880
0963b4bd
MS
15881 /* GNU F77 is a special case, as at 08/2004 array type info is the
15882 opposite order to the dwarf2 specification, but data is still
15883 laid out as per normal fortran.
7ca2d3a3 15884
0963b4bd
MS
15885 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15886 version checking. */
7ca2d3a3 15887
905e0470
PM
15888 if (cu->language == language_fortran
15889 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15890 {
15891 return DW_ORD_row_major;
15892 }
15893
6e70227d 15894 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15895 {
15896 case array_column_major:
15897 return DW_ORD_col_major;
15898 case array_row_major:
15899 default:
15900 return DW_ORD_row_major;
15901 };
15902}
15903
72019c9c 15904/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15905 the DIE's type field. */
72019c9c 15906
f792889a 15907static struct type *
72019c9c
GM
15908read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15909{
7e314c57
JK
15910 struct type *domain_type, *set_type;
15911 struct attribute *attr;
f792889a 15912
7e314c57
JK
15913 domain_type = die_type (die, cu);
15914
15915 /* The die_type call above may have already set the type for this DIE. */
15916 set_type = get_die_type (die, cu);
15917 if (set_type)
15918 return set_type;
15919
15920 set_type = create_set_type (NULL, domain_type);
15921
15922 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15923 if (attr != nullptr)
d09039dd 15924 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15925
2b4424c3
TT
15926 maybe_set_alignment (cu, die, set_type);
15927
f792889a 15928 return set_die_type (die, set_type, cu);
72019c9c 15929}
7ca2d3a3 15930
0971de02
TT
15931/* A helper for read_common_block that creates a locexpr baton.
15932 SYM is the symbol which we are marking as computed.
15933 COMMON_DIE is the DIE for the common block.
15934 COMMON_LOC is the location expression attribute for the common
15935 block itself.
15936 MEMBER_LOC is the location expression attribute for the particular
15937 member of the common block that we are processing.
15938 CU is the CU from which the above come. */
15939
15940static void
15941mark_common_block_symbol_computed (struct symbol *sym,
15942 struct die_info *common_die,
15943 struct attribute *common_loc,
15944 struct attribute *member_loc,
15945 struct dwarf2_cu *cu)
15946{
518817b3
SM
15947 struct dwarf2_per_objfile *dwarf2_per_objfile
15948 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15949 struct objfile *objfile = dwarf2_per_objfile->objfile;
15950 struct dwarf2_locexpr_baton *baton;
15951 gdb_byte *ptr;
15952 unsigned int cu_off;
15953 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15954 LONGEST offset = 0;
15955
15956 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15957 gdb_assert (common_loc->form_is_block ());
15958 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15959 || member_loc->form_is_constant ());
0971de02 15960
8d749320 15961 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15962 baton->per_cu = cu->per_cu;
15963 gdb_assert (baton->per_cu);
15964
15965 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15966
cd6c91b4 15967 if (member_loc->form_is_constant ())
0971de02
TT
15968 {
15969 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15970 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15971 }
15972 else
15973 baton->size += DW_BLOCK (member_loc)->size;
15974
224c3ddb 15975 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15976 baton->data = ptr;
15977
15978 *ptr++ = DW_OP_call4;
9c541725 15979 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15980 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15981 ptr += 4;
15982
cd6c91b4 15983 if (member_loc->form_is_constant ())
0971de02
TT
15984 {
15985 *ptr++ = DW_OP_addr;
15986 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15987 ptr += cu->header.addr_size;
15988 }
15989 else
15990 {
15991 /* We have to copy the data here, because DW_OP_call4 will only
15992 use a DW_AT_location attribute. */
15993 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15994 ptr += DW_BLOCK (member_loc)->size;
15995 }
15996
15997 *ptr++ = DW_OP_plus;
15998 gdb_assert (ptr - baton->data == baton->size);
15999
0971de02 16000 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16001 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16002}
16003
4357ac6c
TT
16004/* Create appropriate locally-scoped variables for all the
16005 DW_TAG_common_block entries. Also create a struct common_block
16006 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16007 is used to separate the common blocks name namespace from regular
4357ac6c 16008 variable names. */
c906108c
SS
16009
16010static void
e7c27a73 16011read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16012{
0971de02
TT
16013 struct attribute *attr;
16014
16015 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16016 if (attr != nullptr)
0971de02
TT
16017 {
16018 /* Support the .debug_loc offsets. */
4fc6c0d5 16019 if (attr->form_is_block ())
0971de02
TT
16020 {
16021 /* Ok. */
16022 }
cd6c91b4 16023 else if (attr->form_is_section_offset ())
0971de02
TT
16024 {
16025 dwarf2_complex_location_expr_complaint ();
16026 attr = NULL;
16027 }
16028 else
16029 {
16030 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16031 "common block member");
16032 attr = NULL;
16033 }
16034 }
16035
639d11d3 16036 if (die->child != NULL)
c906108c 16037 {
518817b3 16038 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16039 struct die_info *child_die;
16040 size_t n_entries = 0, size;
16041 struct common_block *common_block;
16042 struct symbol *sym;
74ac6d43 16043
4357ac6c
TT
16044 for (child_die = die->child;
16045 child_die && child_die->tag;
16046 child_die = sibling_die (child_die))
16047 ++n_entries;
16048
16049 size = (sizeof (struct common_block)
16050 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16051 common_block
16052 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16053 size);
4357ac6c
TT
16054 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16055 common_block->n_entries = 0;
16056
16057 for (child_die = die->child;
16058 child_die && child_die->tag;
16059 child_die = sibling_die (child_die))
16060 {
16061 /* Create the symbol in the DW_TAG_common_block block in the current
16062 symbol scope. */
e7c27a73 16063 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16064 if (sym != NULL)
16065 {
16066 struct attribute *member_loc;
16067
16068 common_block->contents[common_block->n_entries++] = sym;
16069
16070 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16071 cu);
16072 if (member_loc)
16073 {
16074 /* GDB has handled this for a long time, but it is
16075 not specified by DWARF. It seems to have been
16076 emitted by gfortran at least as recently as:
16077 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16078 complaint (_("Variable in common block has "
0971de02 16079 "DW_AT_data_member_location "
9d8780f0
SM
16080 "- DIE at %s [in module %s]"),
16081 sect_offset_str (child_die->sect_off),
518817b3 16082 objfile_name (objfile));
0971de02 16083
cd6c91b4 16084 if (member_loc->form_is_section_offset ())
0971de02 16085 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16086 else if (member_loc->form_is_constant ()
4fc6c0d5 16087 || member_loc->form_is_block ())
0971de02 16088 {
435d3d88 16089 if (attr != nullptr)
0971de02
TT
16090 mark_common_block_symbol_computed (sym, die, attr,
16091 member_loc, cu);
16092 }
16093 else
16094 dwarf2_complex_location_expr_complaint ();
16095 }
16096 }
c906108c 16097 }
4357ac6c
TT
16098
16099 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16100 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16101 }
16102}
16103
0114d602 16104/* Create a type for a C++ namespace. */
d9fa45fe 16105
0114d602
DJ
16106static struct type *
16107read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16108{
518817b3 16109 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16110 const char *previous_prefix, *name;
9219021c 16111 int is_anonymous;
0114d602
DJ
16112 struct type *type;
16113
16114 /* For extensions, reuse the type of the original namespace. */
16115 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16116 {
16117 struct die_info *ext_die;
16118 struct dwarf2_cu *ext_cu = cu;
9a619af0 16119
0114d602
DJ
16120 ext_die = dwarf2_extension (die, &ext_cu);
16121 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16122
16123 /* EXT_CU may not be the same as CU.
02142a6c 16124 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16125 return set_die_type (die, type, cu);
16126 }
9219021c 16127
e142c38c 16128 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16129
16130 /* Now build the name of the current namespace. */
16131
0114d602
DJ
16132 previous_prefix = determine_prefix (die, cu);
16133 if (previous_prefix[0] != '\0')
16134 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16135 previous_prefix, name, 0, cu);
0114d602
DJ
16136
16137 /* Create the type. */
19f392bc 16138 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16139
60531b24 16140 return set_die_type (die, type, cu);
0114d602
DJ
16141}
16142
22cee43f 16143/* Read a namespace scope. */
0114d602
DJ
16144
16145static void
16146read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16147{
518817b3 16148 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16149 int is_anonymous;
9219021c 16150
5c4e30ca
DC
16151 /* Add a symbol associated to this if we haven't seen the namespace
16152 before. Also, add a using directive if it's an anonymous
16153 namespace. */
9219021c 16154
f2f0e013 16155 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16156 {
16157 struct type *type;
16158
0114d602 16159 type = read_type_die (die, cu);
e7c27a73 16160 new_symbol (die, type, cu);
5c4e30ca 16161
e8e80198 16162 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16163 if (is_anonymous)
0114d602
DJ
16164 {
16165 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16166
eb1e02fd 16167 std::vector<const char *> excludes;
804d2729 16168 add_using_directive (using_directives (cu),
22cee43f 16169 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16170 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16171 }
5c4e30ca 16172 }
9219021c 16173
639d11d3 16174 if (die->child != NULL)
d9fa45fe 16175 {
639d11d3 16176 struct die_info *child_die = die->child;
6e70227d 16177
d9fa45fe
DC
16178 while (child_die && child_die->tag)
16179 {
e7c27a73 16180 process_die (child_die, cu);
d9fa45fe
DC
16181 child_die = sibling_die (child_die);
16182 }
16183 }
38d518c9
EZ
16184}
16185
f55ee35c
JK
16186/* Read a Fortran module as type. This DIE can be only a declaration used for
16187 imported module. Still we need that type as local Fortran "use ... only"
16188 declaration imports depend on the created type in determine_prefix. */
16189
16190static struct type *
16191read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16192{
518817b3 16193 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16194 const char *module_name;
f55ee35c
JK
16195 struct type *type;
16196
16197 module_name = dwarf2_name (die, cu);
19f392bc 16198 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16199
f55ee35c
JK
16200 return set_die_type (die, type, cu);
16201}
16202
5d7cb8df
JK
16203/* Read a Fortran module. */
16204
16205static void
16206read_module (struct die_info *die, struct dwarf2_cu *cu)
16207{
16208 struct die_info *child_die = die->child;
530e8392
KB
16209 struct type *type;
16210
16211 type = read_type_die (die, cu);
16212 new_symbol (die, type, cu);
5d7cb8df 16213
5d7cb8df
JK
16214 while (child_die && child_die->tag)
16215 {
16216 process_die (child_die, cu);
16217 child_die = sibling_die (child_die);
16218 }
16219}
16220
38d518c9
EZ
16221/* Return the name of the namespace represented by DIE. Set
16222 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16223 namespace. */
16224
16225static const char *
e142c38c 16226namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16227{
16228 struct die_info *current_die;
16229 const char *name = NULL;
16230
16231 /* Loop through the extensions until we find a name. */
16232
16233 for (current_die = die;
16234 current_die != NULL;
f2f0e013 16235 current_die = dwarf2_extension (die, &cu))
38d518c9 16236 {
96553a0c
DE
16237 /* We don't use dwarf2_name here so that we can detect the absence
16238 of a name -> anonymous namespace. */
7d45c7c3 16239 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16240
38d518c9
EZ
16241 if (name != NULL)
16242 break;
16243 }
16244
16245 /* Is it an anonymous namespace? */
16246
16247 *is_anonymous = (name == NULL);
16248 if (*is_anonymous)
2b1dbab0 16249 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16250
16251 return name;
d9fa45fe
DC
16252}
16253
c906108c
SS
16254/* Extract all information from a DW_TAG_pointer_type DIE and add to
16255 the user defined type vector. */
16256
f792889a 16257static struct type *
e7c27a73 16258read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16259{
518817b3
SM
16260 struct gdbarch *gdbarch
16261 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16262 struct comp_unit_head *cu_header = &cu->header;
c906108c 16263 struct type *type;
8b2dbe47
KB
16264 struct attribute *attr_byte_size;
16265 struct attribute *attr_address_class;
16266 int byte_size, addr_class;
7e314c57
JK
16267 struct type *target_type;
16268
16269 target_type = die_type (die, cu);
c906108c 16270
7e314c57
JK
16271 /* The die_type call above may have already set the type for this DIE. */
16272 type = get_die_type (die, cu);
16273 if (type)
16274 return type;
16275
16276 type = lookup_pointer_type (target_type);
8b2dbe47 16277
e142c38c 16278 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16279 if (attr_byte_size)
16280 byte_size = DW_UNSND (attr_byte_size);
c906108c 16281 else
8b2dbe47
KB
16282 byte_size = cu_header->addr_size;
16283
e142c38c 16284 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16285 if (attr_address_class)
16286 addr_class = DW_UNSND (attr_address_class);
16287 else
16288 addr_class = DW_ADDR_none;
16289
2b4424c3
TT
16290 ULONGEST alignment = get_alignment (cu, die);
16291
16292 /* If the pointer size, alignment, or address class is different
16293 than the default, create a type variant marked as such and set
16294 the length accordingly. */
16295 if (TYPE_LENGTH (type) != byte_size
16296 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16297 && alignment != TYPE_RAW_ALIGN (type))
16298 || addr_class != DW_ADDR_none)
c906108c 16299 {
5e2b427d 16300 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16301 {
16302 int type_flags;
16303
849957d9 16304 type_flags = gdbarch_address_class_type_flags
5e2b427d 16305 (gdbarch, byte_size, addr_class);
876cecd0
TT
16306 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16307 == 0);
8b2dbe47
KB
16308 type = make_type_with_address_space (type, type_flags);
16309 }
16310 else if (TYPE_LENGTH (type) != byte_size)
16311 {
b98664d3 16312 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16313 }
2b4424c3
TT
16314 else if (TYPE_RAW_ALIGN (type) != alignment)
16315 {
b98664d3 16316 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16317 " - DIE at %s [in module %s]"),
16318 sect_offset_str (die->sect_off),
16319 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16320 }
6e70227d 16321 else
9a619af0
MS
16322 {
16323 /* Should we also complain about unhandled address classes? */
16324 }
c906108c 16325 }
8b2dbe47
KB
16326
16327 TYPE_LENGTH (type) = byte_size;
2b4424c3 16328 set_type_align (type, alignment);
f792889a 16329 return set_die_type (die, type, cu);
c906108c
SS
16330}
16331
16332/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16333 the user defined type vector. */
16334
f792889a 16335static struct type *
e7c27a73 16336read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16337{
16338 struct type *type;
16339 struct type *to_type;
16340 struct type *domain;
16341
e7c27a73
DJ
16342 to_type = die_type (die, cu);
16343 domain = die_containing_type (die, cu);
0d5de010 16344
7e314c57
JK
16345 /* The calls above may have already set the type for this DIE. */
16346 type = get_die_type (die, cu);
16347 if (type)
16348 return type;
16349
0d5de010
DJ
16350 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16351 type = lookup_methodptr_type (to_type);
7078baeb
TT
16352 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16353 {
518817b3
SM
16354 struct type *new_type
16355 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16356
16357 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16358 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16359 TYPE_VARARGS (to_type));
16360 type = lookup_methodptr_type (new_type);
16361 }
0d5de010
DJ
16362 else
16363 type = lookup_memberptr_type (to_type, domain);
c906108c 16364
f792889a 16365 return set_die_type (die, type, cu);
c906108c
SS
16366}
16367
4297a3f0 16368/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16369 the user defined type vector. */
16370
f792889a 16371static struct type *
4297a3f0
AV
16372read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16373 enum type_code refcode)
c906108c 16374{
e7c27a73 16375 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16376 struct type *type, *target_type;
c906108c
SS
16377 struct attribute *attr;
16378
4297a3f0
AV
16379 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16380
7e314c57
JK
16381 target_type = die_type (die, cu);
16382
16383 /* The die_type call above may have already set the type for this DIE. */
16384 type = get_die_type (die, cu);
16385 if (type)
16386 return type;
16387
4297a3f0 16388 type = lookup_reference_type (target_type, refcode);
e142c38c 16389 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16390 if (attr != nullptr)
c906108c
SS
16391 {
16392 TYPE_LENGTH (type) = DW_UNSND (attr);
16393 }
16394 else
16395 {
107d2387 16396 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16397 }
2b4424c3 16398 maybe_set_alignment (cu, die, type);
f792889a 16399 return set_die_type (die, type, cu);
c906108c
SS
16400}
16401
cf363f18
MW
16402/* Add the given cv-qualifiers to the element type of the array. GCC
16403 outputs DWARF type qualifiers that apply to an array, not the
16404 element type. But GDB relies on the array element type to carry
16405 the cv-qualifiers. This mimics section 6.7.3 of the C99
16406 specification. */
16407
16408static struct type *
16409add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16410 struct type *base_type, int cnst, int voltl)
16411{
16412 struct type *el_type, *inner_array;
16413
16414 base_type = copy_type (base_type);
16415 inner_array = base_type;
16416
16417 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16418 {
16419 TYPE_TARGET_TYPE (inner_array) =
16420 copy_type (TYPE_TARGET_TYPE (inner_array));
16421 inner_array = TYPE_TARGET_TYPE (inner_array);
16422 }
16423
16424 el_type = TYPE_TARGET_TYPE (inner_array);
16425 cnst |= TYPE_CONST (el_type);
16426 voltl |= TYPE_VOLATILE (el_type);
16427 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16428
16429 return set_die_type (die, base_type, cu);
16430}
16431
f792889a 16432static struct type *
e7c27a73 16433read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16434{
f792889a 16435 struct type *base_type, *cv_type;
c906108c 16436
e7c27a73 16437 base_type = die_type (die, cu);
7e314c57
JK
16438
16439 /* The die_type call above may have already set the type for this DIE. */
16440 cv_type = get_die_type (die, cu);
16441 if (cv_type)
16442 return cv_type;
16443
2f608a3a
KW
16444 /* In case the const qualifier is applied to an array type, the element type
16445 is so qualified, not the array type (section 6.7.3 of C99). */
16446 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16447 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16448
f792889a
DJ
16449 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16450 return set_die_type (die, cv_type, cu);
c906108c
SS
16451}
16452
f792889a 16453static struct type *
e7c27a73 16454read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16455{
f792889a 16456 struct type *base_type, *cv_type;
c906108c 16457
e7c27a73 16458 base_type = die_type (die, cu);
7e314c57
JK
16459
16460 /* The die_type call above may have already set the type for this DIE. */
16461 cv_type = get_die_type (die, cu);
16462 if (cv_type)
16463 return cv_type;
16464
cf363f18
MW
16465 /* In case the volatile qualifier is applied to an array type, the
16466 element type is so qualified, not the array type (section 6.7.3
16467 of C99). */
16468 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16469 return add_array_cv_type (die, cu, base_type, 0, 1);
16470
f792889a
DJ
16471 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16472 return set_die_type (die, cv_type, cu);
c906108c
SS
16473}
16474
06d66ee9
TT
16475/* Handle DW_TAG_restrict_type. */
16476
16477static struct type *
16478read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16479{
16480 struct type *base_type, *cv_type;
16481
16482 base_type = die_type (die, cu);
16483
16484 /* The die_type call above may have already set the type for this DIE. */
16485 cv_type = get_die_type (die, cu);
16486 if (cv_type)
16487 return cv_type;
16488
16489 cv_type = make_restrict_type (base_type);
16490 return set_die_type (die, cv_type, cu);
16491}
16492
a2c2acaf
MW
16493/* Handle DW_TAG_atomic_type. */
16494
16495static struct type *
16496read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16497{
16498 struct type *base_type, *cv_type;
16499
16500 base_type = die_type (die, cu);
16501
16502 /* The die_type call above may have already set the type for this DIE. */
16503 cv_type = get_die_type (die, cu);
16504 if (cv_type)
16505 return cv_type;
16506
16507 cv_type = make_atomic_type (base_type);
16508 return set_die_type (die, cv_type, cu);
16509}
16510
c906108c
SS
16511/* Extract all information from a DW_TAG_string_type DIE and add to
16512 the user defined type vector. It isn't really a user defined type,
16513 but it behaves like one, with other DIE's using an AT_user_def_type
16514 attribute to reference it. */
16515
f792889a 16516static struct type *
e7c27a73 16517read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16518{
518817b3 16519 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16521 struct type *type, *range_type, *index_type, *char_type;
16522 struct attribute *attr;
216a7e6b
AB
16523 struct dynamic_prop prop;
16524 bool length_is_constant = true;
16525 LONGEST length;
16526
16527 /* There are a couple of places where bit sizes might be made use of
16528 when parsing a DW_TAG_string_type, however, no producer that we know
16529 of make use of these. Handling bit sizes that are a multiple of the
16530 byte size is easy enough, but what about other bit sizes? Lets deal
16531 with that problem when we have to. Warn about these attributes being
16532 unsupported, then parse the type and ignore them like we always
16533 have. */
16534 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16535 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16536 {
16537 static bool warning_printed = false;
16538 if (!warning_printed)
16539 {
16540 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16541 "currently supported on DW_TAG_string_type."));
16542 warning_printed = true;
16543 }
16544 }
c906108c 16545
e142c38c 16546 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16547 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16548 {
16549 /* The string length describes the location at which the length of
16550 the string can be found. The size of the length field can be
16551 specified with one of the attributes below. */
16552 struct type *prop_type;
16553 struct attribute *len
16554 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16555 if (len == nullptr)
16556 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16557 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16558 {
16559 /* Pass 0 as the default as we know this attribute is constant
16560 and the default value will not be returned. */
16561 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
09ba997f 16562 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16563 }
16564 else
16565 {
16566 /* If the size is not specified then we assume it is the size of
16567 an address on this target. */
09ba997f 16568 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16569 }
16570
16571 /* Convert the attribute into a dynamic property. */
16572 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16573 length = 1;
16574 else
16575 length_is_constant = false;
16576 }
16577 else if (attr != nullptr)
16578 {
16579 /* This DW_AT_string_length just contains the length with no
16580 indirection. There's no need to create a dynamic property in this
16581 case. Pass 0 for the default value as we know it will not be
16582 returned in this case. */
16583 length = dwarf2_get_attr_constant_value (attr, 0);
16584 }
16585 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16586 {
216a7e6b
AB
16587 /* We don't currently support non-constant byte sizes for strings. */
16588 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
16589 }
16590 else
16591 {
216a7e6b
AB
16592 /* Use 1 as a fallback length if we have nothing else. */
16593 length = 1;
c906108c 16594 }
6ccb9162 16595
46bf5051 16596 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16597 if (length_is_constant)
16598 range_type = create_static_range_type (NULL, index_type, 1, length);
16599 else
16600 {
16601 struct dynamic_prop low_bound;
16602
16603 low_bound.kind = PROP_CONST;
16604 low_bound.data.const_val = 1;
16605 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16606 }
3b7538c0
UW
16607 char_type = language_string_char_type (cu->language_defn, gdbarch);
16608 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16609
f792889a 16610 return set_die_type (die, type, cu);
c906108c
SS
16611}
16612
4d804846
JB
16613/* Assuming that DIE corresponds to a function, returns nonzero
16614 if the function is prototyped. */
16615
16616static int
16617prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16618{
16619 struct attribute *attr;
16620
16621 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16622 if (attr && (DW_UNSND (attr) != 0))
16623 return 1;
16624
16625 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16626 is only meaningful for C, but the concept also extends to other
4d804846
JB
16627 languages that allow unprototyped functions (Eg: Objective C).
16628 For all other languages, assume that functions are always
16629 prototyped. */
16630 if (cu->language != language_c
16631 && cu->language != language_objc
16632 && cu->language != language_opencl)
16633 return 1;
16634
16635 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16636 prototyped and unprototyped functions; default to prototyped,
16637 since that is more common in modern code (and RealView warns
16638 about unprototyped functions). */
16639 if (producer_is_realview (cu->producer))
16640 return 1;
16641
16642 return 0;
16643}
16644
c906108c
SS
16645/* Handle DIES due to C code like:
16646
16647 struct foo
c5aa993b
JM
16648 {
16649 int (*funcp)(int a, long l);
16650 int b;
16651 };
c906108c 16652
0963b4bd 16653 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16654
f792889a 16655static struct type *
e7c27a73 16656read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16657{
518817b3 16658 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16659 struct type *type; /* Type that this function returns. */
16660 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16661 struct attribute *attr;
16662
e7c27a73 16663 type = die_type (die, cu);
7e314c57
JK
16664
16665 /* The die_type call above may have already set the type for this DIE. */
16666 ftype = get_die_type (die, cu);
16667 if (ftype)
16668 return ftype;
16669
0c8b41f1 16670 ftype = lookup_function_type (type);
c906108c 16671
4d804846 16672 if (prototyped_function_p (die, cu))
a6c727b2 16673 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16674
c055b101
CV
16675 /* Store the calling convention in the type if it's available in
16676 the subroutine die. Otherwise set the calling convention to
16677 the default value DW_CC_normal. */
16678 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16679 if (attr != nullptr
16680 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16681 TYPE_CALLING_CONVENTION (ftype)
16682 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16683 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16684 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16685 else
16686 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16687
743649fd
MW
16688 /* Record whether the function returns normally to its caller or not
16689 if the DWARF producer set that information. */
16690 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16691 if (attr && (DW_UNSND (attr) != 0))
16692 TYPE_NO_RETURN (ftype) = 1;
16693
76c10ea2
GM
16694 /* We need to add the subroutine type to the die immediately so
16695 we don't infinitely recurse when dealing with parameters
0963b4bd 16696 declared as the same subroutine type. */
76c10ea2 16697 set_die_type (die, ftype, cu);
6e70227d 16698
639d11d3 16699 if (die->child != NULL)
c906108c 16700 {
bb5ed363 16701 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16702 struct die_info *child_die;
8072405b 16703 int nparams, iparams;
c906108c
SS
16704
16705 /* Count the number of parameters.
16706 FIXME: GDB currently ignores vararg functions, but knows about
16707 vararg member functions. */
8072405b 16708 nparams = 0;
639d11d3 16709 child_die = die->child;
c906108c
SS
16710 while (child_die && child_die->tag)
16711 {
16712 if (child_die->tag == DW_TAG_formal_parameter)
16713 nparams++;
16714 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16715 TYPE_VARARGS (ftype) = 1;
c906108c
SS
16716 child_die = sibling_die (child_die);
16717 }
16718
16719 /* Allocate storage for parameters and fill them in. */
16720 TYPE_NFIELDS (ftype) = nparams;
16721 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16722 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16723
8072405b
JK
16724 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16725 even if we error out during the parameters reading below. */
16726 for (iparams = 0; iparams < nparams; iparams++)
16727 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16728
16729 iparams = 0;
639d11d3 16730 child_die = die->child;
c906108c
SS
16731 while (child_die && child_die->tag)
16732 {
16733 if (child_die->tag == DW_TAG_formal_parameter)
16734 {
3ce3b1ba
PA
16735 struct type *arg_type;
16736
16737 /* DWARF version 2 has no clean way to discern C++
16738 static and non-static member functions. G++ helps
16739 GDB by marking the first parameter for non-static
16740 member functions (which is the this pointer) as
16741 artificial. We pass this information to
16742 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16743
16744 DWARF version 3 added DW_AT_object_pointer, which GCC
16745 4.5 does not yet generate. */
e142c38c 16746 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16747 if (attr != nullptr)
c906108c
SS
16748 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16749 else
9c37b5ae 16750 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16751 arg_type = die_type (child_die, cu);
16752
16753 /* RealView does not mark THIS as const, which the testsuite
16754 expects. GCC marks THIS as const in method definitions,
16755 but not in the class specifications (GCC PR 43053). */
16756 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16757 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16758 {
16759 int is_this = 0;
16760 struct dwarf2_cu *arg_cu = cu;
16761 const char *name = dwarf2_name (child_die, cu);
16762
16763 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16764 if (attr != nullptr)
3ce3b1ba
PA
16765 {
16766 /* If the compiler emits this, use it. */
16767 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16768 is_this = 1;
16769 }
16770 else if (name && strcmp (name, "this") == 0)
16771 /* Function definitions will have the argument names. */
16772 is_this = 1;
16773 else if (name == NULL && iparams == 0)
16774 /* Declarations may not have the names, so like
16775 elsewhere in GDB, assume an artificial first
16776 argument is "this". */
16777 is_this = 1;
16778
16779 if (is_this)
16780 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16781 arg_type, 0);
16782 }
16783
16784 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16785 iparams++;
16786 }
16787 child_die = sibling_die (child_die);
16788 }
16789 }
16790
76c10ea2 16791 return ftype;
c906108c
SS
16792}
16793
f792889a 16794static struct type *
e7c27a73 16795read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16796{
518817b3 16797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16798 const char *name = NULL;
3c8e0968 16799 struct type *this_type, *target_type;
c906108c 16800
94af9270 16801 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16802 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16803 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16804 set_die_type (die, this_type, cu);
3c8e0968
DE
16805 target_type = die_type (die, cu);
16806 if (target_type != this_type)
16807 TYPE_TARGET_TYPE (this_type) = target_type;
16808 else
16809 {
16810 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16811 spec and cause infinite loops in GDB. */
b98664d3 16812 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16813 "- DIE at %s [in module %s]"),
16814 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16815 TYPE_TARGET_TYPE (this_type) = NULL;
16816 }
e4003a34
TV
16817 if (name == NULL)
16818 {
16819 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16820 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16821 Handle these by just returning the target type, rather than
16822 constructing an anonymous typedef type and trying to handle this
16823 elsewhere. */
16824 set_die_type (die, target_type, cu);
16825 return target_type;
16826 }
f792889a 16827 return this_type;
c906108c
SS
16828}
16829
9b790ce7
UW
16830/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16831 (which may be different from NAME) to the architecture back-end to allow
16832 it to guess the correct format if necessary. */
16833
16834static struct type *
16835dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16836 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16837{
16838 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16839 const struct floatformat **format;
16840 struct type *type;
16841
16842 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16843 if (format)
103a685e 16844 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16845 else
77b7c781 16846 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16847
16848 return type;
16849}
16850
eb77c9df
AB
16851/* Allocate an integer type of size BITS and name NAME. */
16852
16853static struct type *
16854dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16855 int bits, int unsigned_p, const char *name)
16856{
16857 struct type *type;
16858
16859 /* Versions of Intel's C Compiler generate an integer type called "void"
16860 instead of using DW_TAG_unspecified_type. This has been seen on
16861 at least versions 14, 17, and 18. */
35ee2dc2
AB
16862 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16863 && strcmp (name, "void") == 0)
eb77c9df
AB
16864 type = objfile_type (objfile)->builtin_void;
16865 else
16866 type = init_integer_type (objfile, bits, unsigned_p, name);
16867
16868 return type;
16869}
16870
8bdc1658
AB
16871/* Initialise and return a floating point type of size BITS suitable for
16872 use as a component of a complex number. The NAME_HINT is passed through
16873 when initialising the floating point type and is the name of the complex
16874 type.
16875
16876 As DWARF doesn't currently provide an explicit name for the components
16877 of a complex number, but it can be helpful to have these components
16878 named, we try to select a suitable name based on the size of the
16879 component. */
16880static struct type *
16881dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16882 struct objfile *objfile,
103a685e
TT
16883 int bits, const char *name_hint,
16884 enum bfd_endian byte_order)
8bdc1658
AB
16885{
16886 gdbarch *gdbarch = get_objfile_arch (objfile);
16887 struct type *tt = nullptr;
16888
35add35e
AB
16889 /* Try to find a suitable floating point builtin type of size BITS.
16890 We're going to use the name of this type as the name for the complex
16891 target type that we are about to create. */
1db455a7 16892 switch (cu->language)
8bdc1658 16893 {
1db455a7
AB
16894 case language_fortran:
16895 switch (bits)
16896 {
16897 case 32:
16898 tt = builtin_f_type (gdbarch)->builtin_real;
16899 break;
16900 case 64:
16901 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16902 break;
16903 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16904 case 128:
16905 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16906 break;
16907 }
8bdc1658 16908 break;
1db455a7
AB
16909 default:
16910 switch (bits)
16911 {
16912 case 32:
16913 tt = builtin_type (gdbarch)->builtin_float;
16914 break;
16915 case 64:
16916 tt = builtin_type (gdbarch)->builtin_double;
16917 break;
16918 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16919 case 128:
16920 tt = builtin_type (gdbarch)->builtin_long_double;
16921 break;
16922 }
8bdc1658
AB
16923 break;
16924 }
16925
35add35e
AB
16926 /* If the type we found doesn't match the size we were looking for, then
16927 pretend we didn't find a type at all, the complex target type we
16928 create will then be nameless. */
a12e5744 16929 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16930 tt = nullptr;
16931
8bdc1658 16932 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16933 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16934}
16935
c906108c
SS
16936/* Find a representation of a given base type and install
16937 it in the TYPE field of the die. */
16938
f792889a 16939static struct type *
e7c27a73 16940read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16941{
518817b3 16942 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16943 struct type *type;
16944 struct attribute *attr;
19f392bc 16945 int encoding = 0, bits = 0;
15d034d0 16946 const char *name;
34877895 16947 gdbarch *arch;
c906108c 16948
e142c38c 16949 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16950 if (attr != nullptr)
34877895 16951 encoding = DW_UNSND (attr);
e142c38c 16952 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16953 if (attr != nullptr)
34877895 16954 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16955 name = dwarf2_name (die, cu);
6ccb9162 16956 if (!name)
34877895 16957 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16958
16959 arch = get_objfile_arch (objfile);
16960 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16961
34877895
PJ
16962 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16963 if (attr)
103a685e
TT
16964 {
16965 int endianity = DW_UNSND (attr);
16966
16967 switch (endianity)
16968 {
16969 case DW_END_big:
16970 byte_order = BFD_ENDIAN_BIG;
16971 break;
16972 case DW_END_little:
16973 byte_order = BFD_ENDIAN_LITTLE;
16974 break;
16975 default:
16976 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16977 break;
16978 }
16979 }
6ccb9162
UW
16980
16981 switch (encoding)
c906108c 16982 {
6ccb9162
UW
16983 case DW_ATE_address:
16984 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16985 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16986 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16987 break;
16988 case DW_ATE_boolean:
19f392bc 16989 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16990 break;
16991 case DW_ATE_complex_float:
103a685e
TT
16992 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16993 byte_order);
19f392bc 16994 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16995 break;
16996 case DW_ATE_decimal_float:
19f392bc 16997 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16998 break;
16999 case DW_ATE_float:
103a685e 17000 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17001 break;
17002 case DW_ATE_signed:
eb77c9df 17003 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17004 break;
17005 case DW_ATE_unsigned:
3b2b8fea
TT
17006 if (cu->language == language_fortran
17007 && name
61012eef 17008 && startswith (name, "character("))
19f392bc
UW
17009 type = init_character_type (objfile, bits, 1, name);
17010 else
eb77c9df 17011 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17012 break;
17013 case DW_ATE_signed_char:
6e70227d 17014 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17015 || cu->language == language_pascal
17016 || cu->language == language_fortran)
19f392bc
UW
17017 type = init_character_type (objfile, bits, 0, name);
17018 else
eb77c9df 17019 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17020 break;
17021 case DW_ATE_unsigned_char:
868a0084 17022 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17023 || cu->language == language_pascal
c44af4eb
TT
17024 || cu->language == language_fortran
17025 || cu->language == language_rust)
19f392bc
UW
17026 type = init_character_type (objfile, bits, 1, name);
17027 else
eb77c9df 17028 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17029 break;
75079b2b 17030 case DW_ATE_UTF:
53e710ac 17031 {
53e710ac
PA
17032 if (bits == 16)
17033 type = builtin_type (arch)->builtin_char16;
17034 else if (bits == 32)
17035 type = builtin_type (arch)->builtin_char32;
17036 else
17037 {
b98664d3 17038 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17039 bits);
eb77c9df 17040 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17041 }
17042 return set_die_type (die, type, cu);
17043 }
75079b2b
TT
17044 break;
17045
6ccb9162 17046 default:
b98664d3 17047 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17048 dwarf_type_encoding_name (encoding));
77b7c781 17049 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17050 break;
c906108c 17051 }
6ccb9162 17052
0114d602 17053 if (name && strcmp (name, "char") == 0)
876cecd0 17054 TYPE_NOSIGN (type) = 1;
0114d602 17055
2b4424c3
TT
17056 maybe_set_alignment (cu, die, type);
17057
103a685e 17058 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17059
f792889a 17060 return set_die_type (die, type, cu);
c906108c
SS
17061}
17062
80180f79
SA
17063/* Parse dwarf attribute if it's a block, reference or constant and put the
17064 resulting value of the attribute into struct bound_prop.
17065 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17066
17067static int
17068attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17069 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17070 struct type *default_type)
80180f79
SA
17071{
17072 struct dwarf2_property_baton *baton;
518817b3
SM
17073 struct obstack *obstack
17074 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17075
9a49df9d
AB
17076 gdb_assert (default_type != NULL);
17077
80180f79
SA
17078 if (attr == NULL || prop == NULL)
17079 return 0;
17080
4fc6c0d5 17081 if (attr->form_is_block ())
80180f79 17082 {
8d749320 17083 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17084 baton->property_type = default_type;
80180f79
SA
17085 baton->locexpr.per_cu = cu->per_cu;
17086 baton->locexpr.size = DW_BLOCK (attr)->size;
17087 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17088 switch (attr->name)
17089 {
17090 case DW_AT_string_length:
17091 baton->locexpr.is_reference = true;
17092 break;
17093 default:
17094 baton->locexpr.is_reference = false;
17095 break;
17096 }
80180f79
SA
17097 prop->data.baton = baton;
17098 prop->kind = PROP_LOCEXPR;
17099 gdb_assert (prop->data.baton != NULL);
17100 }
cd6c91b4 17101 else if (attr->form_is_ref ())
80180f79
SA
17102 {
17103 struct dwarf2_cu *target_cu = cu;
17104 struct die_info *target_die;
17105 struct attribute *target_attr;
17106
17107 target_die = follow_die_ref (die, attr, &target_cu);
17108 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17109 if (target_attr == NULL)
17110 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17111 target_cu);
80180f79
SA
17112 if (target_attr == NULL)
17113 return 0;
17114
df25ebbd 17115 switch (target_attr->name)
80180f79 17116 {
df25ebbd 17117 case DW_AT_location:
cd6c91b4 17118 if (target_attr->form_is_section_offset ())
df25ebbd 17119 {
8d749320 17120 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17121 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17122 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17123 prop->data.baton = baton;
17124 prop->kind = PROP_LOCLIST;
17125 gdb_assert (prop->data.baton != NULL);
17126 }
4fc6c0d5 17127 else if (target_attr->form_is_block ())
df25ebbd 17128 {
8d749320 17129 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17130 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17131 baton->locexpr.per_cu = cu->per_cu;
17132 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17133 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17134 baton->locexpr.is_reference = true;
df25ebbd
JB
17135 prop->data.baton = baton;
17136 prop->kind = PROP_LOCEXPR;
17137 gdb_assert (prop->data.baton != NULL);
17138 }
17139 else
17140 {
17141 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17142 "dynamic property");
17143 return 0;
17144 }
17145 break;
17146 case DW_AT_data_member_location:
17147 {
17148 LONGEST offset;
17149
17150 if (!handle_data_member_location (target_die, target_cu,
17151 &offset))
17152 return 0;
17153
8d749320 17154 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17155 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17156 target_cu);
df25ebbd
JB
17157 baton->offset_info.offset = offset;
17158 baton->offset_info.type = die_type (target_die, target_cu);
17159 prop->data.baton = baton;
17160 prop->kind = PROP_ADDR_OFFSET;
17161 break;
17162 }
80180f79
SA
17163 }
17164 }
cd6c91b4 17165 else if (attr->form_is_constant ())
80180f79
SA
17166 {
17167 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17168 prop->kind = PROP_CONST;
17169 }
17170 else
17171 {
17172 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17173 dwarf2_name (die, cu));
17174 return 0;
17175 }
17176
17177 return 1;
17178}
17179
09ba997f 17180/* See read.h. */
9a49df9d 17181
09ba997f
TT
17182struct type *
17183dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17184{
09ba997f 17185 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17186 struct type *int_type;
17187
17188 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17189#define TRY_TYPE(F) \
17190 int_type = (unsigned_p \
17191 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17192 : objfile_type (objfile)->builtin_ ## F); \
17193 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17194 return int_type
17195
17196 TRY_TYPE (char);
17197 TRY_TYPE (short);
17198 TRY_TYPE (int);
17199 TRY_TYPE (long);
17200 TRY_TYPE (long_long);
17201
17202#undef TRY_TYPE
17203
17204 gdb_assert_not_reached ("unable to find suitable integer type");
17205}
17206
09ba997f 17207/* See read.h. */
11a8b164 17208
09ba997f
TT
17209struct type *
17210dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17211{
09ba997f
TT
17212 int addr_size = this->addr_size ();
17213 return int_type (addr_size, unsigned_p);
11a8b164
AB
17214}
17215
b86352cf
AB
17216/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17217 present (which is valid) then compute the default type based on the
17218 compilation units address size. */
17219
17220static struct type *
17221read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17222{
17223 struct type *index_type = die_type (die, cu);
17224
17225 /* Dwarf-2 specifications explicitly allows to create subrange types
17226 without specifying a base type.
17227 In that case, the base type must be set to the type of
17228 the lower bound, upper bound or count, in that order, if any of these
17229 three attributes references an object that has a type.
17230 If no base type is found, the Dwarf-2 specifications say that
17231 a signed integer type of size equal to the size of an address should
17232 be used.
17233 For the following C code: `extern char gdb_int [];'
17234 GCC produces an empty range DIE.
17235 FIXME: muller/2010-05-28: Possible references to object for low bound,
17236 high bound or count are not yet handled by this code. */
17237 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17238 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17239
17240 return index_type;
17241}
17242
a02abb62
JB
17243/* Read the given DW_AT_subrange DIE. */
17244
f792889a 17245static struct type *
a02abb62
JB
17246read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17247{
4c9ad8c2 17248 struct type *base_type, *orig_base_type;
a02abb62
JB
17249 struct type *range_type;
17250 struct attribute *attr;
729efb13 17251 struct dynamic_prop low, high;
4fae6e18 17252 int low_default_is_valid;
c451ebe5 17253 int high_bound_is_count = 0;
15d034d0 17254 const char *name;
d359392f 17255 ULONGEST negative_mask;
e77813c8 17256
b86352cf
AB
17257 orig_base_type = read_subrange_index_type (die, cu);
17258
4c9ad8c2
TT
17259 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17260 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17261 creating the range type, but we use the result of check_typedef
17262 when examining properties of the type. */
17263 base_type = check_typedef (orig_base_type);
a02abb62 17264
7e314c57
JK
17265 /* The die_type call above may have already set the type for this DIE. */
17266 range_type = get_die_type (die, cu);
17267 if (range_type)
17268 return range_type;
17269
729efb13
SA
17270 low.kind = PROP_CONST;
17271 high.kind = PROP_CONST;
17272 high.data.const_val = 0;
17273
4fae6e18
JK
17274 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17275 omitting DW_AT_lower_bound. */
17276 switch (cu->language)
6e70227d 17277 {
4fae6e18
JK
17278 case language_c:
17279 case language_cplus:
729efb13 17280 low.data.const_val = 0;
4fae6e18
JK
17281 low_default_is_valid = 1;
17282 break;
17283 case language_fortran:
729efb13 17284 low.data.const_val = 1;
4fae6e18
JK
17285 low_default_is_valid = 1;
17286 break;
17287 case language_d:
4fae6e18 17288 case language_objc:
c44af4eb 17289 case language_rust:
729efb13 17290 low.data.const_val = 0;
4fae6e18
JK
17291 low_default_is_valid = (cu->header.version >= 4);
17292 break;
17293 case language_ada:
17294 case language_m2:
17295 case language_pascal:
729efb13 17296 low.data.const_val = 1;
4fae6e18
JK
17297 low_default_is_valid = (cu->header.version >= 4);
17298 break;
17299 default:
729efb13 17300 low.data.const_val = 0;
4fae6e18
JK
17301 low_default_is_valid = 0;
17302 break;
a02abb62
JB
17303 }
17304
e142c38c 17305 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17306 if (attr != nullptr)
9a49df9d 17307 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17308 else if (!low_default_is_valid)
b98664d3 17309 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17310 "- DIE at %s [in module %s]"),
17311 sect_offset_str (die->sect_off),
518817b3 17312 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17313
506f5c41
TV
17314 struct attribute *attr_ub, *attr_count;
17315 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17316 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17317 {
506f5c41 17318 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17319 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17320 {
c451ebe5
SA
17321 /* If bounds are constant do the final calculation here. */
17322 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17323 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17324 else
17325 high_bound_is_count = 1;
c2ff108b 17326 }
506f5c41
TV
17327 else
17328 {
17329 if (attr_ub != NULL)
17330 complaint (_("Unresolved DW_AT_upper_bound "
17331 "- DIE at %s [in module %s]"),
17332 sect_offset_str (die->sect_off),
17333 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17334 if (attr_count != NULL)
17335 complaint (_("Unresolved DW_AT_count "
17336 "- DIE at %s [in module %s]"),
17337 sect_offset_str (die->sect_off),
17338 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17339 }
e77813c8 17340 }
a02abb62 17341
4e962e74
TT
17342 LONGEST bias = 0;
17343 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17344 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17345 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17346
dbb9c2b1
JB
17347 /* Normally, the DWARF producers are expected to use a signed
17348 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17349 But this is unfortunately not always the case, as witnessed
17350 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17351 is used instead. To work around that ambiguity, we treat
17352 the bounds as signed, and thus sign-extend their values, when
17353 the base type is signed. */
6e70227d 17354 negative_mask =
d359392f 17355 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17356 if (low.kind == PROP_CONST
17357 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17358 low.data.const_val |= negative_mask;
17359 if (high.kind == PROP_CONST
17360 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17361 high.data.const_val |= negative_mask;
43bbcdc2 17362
5bbd8269
AB
17363 /* Check for bit and byte strides. */
17364 struct dynamic_prop byte_stride_prop;
17365 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17366 if (attr_byte_stride != nullptr)
17367 {
09ba997f 17368 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17369 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17370 prop_type);
17371 }
17372
17373 struct dynamic_prop bit_stride_prop;
17374 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17375 if (attr_bit_stride != nullptr)
17376 {
17377 /* It only makes sense to have either a bit or byte stride. */
17378 if (attr_byte_stride != nullptr)
17379 {
17380 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17381 "- DIE at %s [in module %s]"),
17382 sect_offset_str (die->sect_off),
17383 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17384 attr_bit_stride = nullptr;
17385 }
17386 else
17387 {
09ba997f 17388 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17389 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17390 prop_type);
17391 }
17392 }
17393
17394 if (attr_byte_stride != nullptr
17395 || attr_bit_stride != nullptr)
17396 {
17397 bool byte_stride_p = (attr_byte_stride != nullptr);
17398 struct dynamic_prop *stride
17399 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17400
17401 range_type
17402 = create_range_type_with_stride (NULL, orig_base_type, &low,
17403 &high, bias, stride, byte_stride_p);
17404 }
17405 else
17406 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17407
c451ebe5
SA
17408 if (high_bound_is_count)
17409 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17410
c2ff108b
JK
17411 /* Ada expects an empty array on no boundary attributes. */
17412 if (attr == NULL && cu->language != language_ada)
729efb13 17413 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17414
39cbfefa
DJ
17415 name = dwarf2_name (die, cu);
17416 if (name)
17417 TYPE_NAME (range_type) = name;
6e70227d 17418
e142c38c 17419 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17420 if (attr != nullptr)
a02abb62
JB
17421 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17422
2b4424c3
TT
17423 maybe_set_alignment (cu, die, range_type);
17424
7e314c57
JK
17425 set_die_type (die, range_type, cu);
17426
17427 /* set_die_type should be already done. */
b4ba55a1
JB
17428 set_descriptive_type (range_type, die, cu);
17429
7e314c57 17430 return range_type;
a02abb62 17431}
6e70227d 17432
f792889a 17433static struct type *
81a17f79
JB
17434read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17435{
17436 struct type *type;
81a17f79 17437
518817b3
SM
17438 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17439 NULL);
0114d602 17440 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17441
74a2f8ff 17442 /* In Ada, an unspecified type is typically used when the description
85102364 17443 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17444 such a type, we treat it as a stub, and try to resolve it later on,
17445 when needed. */
17446 if (cu->language == language_ada)
17447 TYPE_STUB (type) = 1;
17448
f792889a 17449 return set_die_type (die, type, cu);
81a17f79 17450}
a02abb62 17451
639d11d3
DC
17452/* Read a single die and all its descendents. Set the die's sibling
17453 field to NULL; set other fields in the die correctly, and set all
17454 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17455 location of the info_ptr after reading all of those dies. PARENT
17456 is the parent of the die in question. */
17457
17458static struct die_info *
dee91e82 17459read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17460 const gdb_byte *info_ptr,
17461 const gdb_byte **new_info_ptr,
dee91e82 17462 struct die_info *parent)
639d11d3
DC
17463{
17464 struct die_info *die;
d521ce57 17465 const gdb_byte *cur_ptr;
639d11d3 17466
3e225074 17467 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17468 if (die == NULL)
17469 {
17470 *new_info_ptr = cur_ptr;
17471 return NULL;
17472 }
93311388 17473 store_in_ref_table (die, reader->cu);
639d11d3 17474
3e225074 17475 if (die->has_children)
bf6af496 17476 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17477 else
17478 {
17479 die->child = NULL;
17480 *new_info_ptr = cur_ptr;
17481 }
17482
17483 die->sibling = NULL;
17484 die->parent = parent;
17485 return die;
17486}
17487
17488/* Read a die, all of its descendents, and all of its siblings; set
17489 all of the fields of all of the dies correctly. Arguments are as
17490 in read_die_and_children. */
17491
17492static struct die_info *
bf6af496 17493read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17494 const gdb_byte *info_ptr,
17495 const gdb_byte **new_info_ptr,
bf6af496 17496 struct die_info *parent)
639d11d3
DC
17497{
17498 struct die_info *first_die, *last_sibling;
d521ce57 17499 const gdb_byte *cur_ptr;
639d11d3 17500
c906108c 17501 cur_ptr = info_ptr;
639d11d3
DC
17502 first_die = last_sibling = NULL;
17503
17504 while (1)
c906108c 17505 {
639d11d3 17506 struct die_info *die
dee91e82 17507 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17508
1d325ec1 17509 if (die == NULL)
c906108c 17510 {
639d11d3
DC
17511 *new_info_ptr = cur_ptr;
17512 return first_die;
c906108c 17513 }
1d325ec1
DJ
17514
17515 if (!first_die)
17516 first_die = die;
c906108c 17517 else
1d325ec1
DJ
17518 last_sibling->sibling = die;
17519
17520 last_sibling = die;
c906108c 17521 }
c906108c
SS
17522}
17523
bf6af496
DE
17524/* Read a die, all of its descendents, and all of its siblings; set
17525 all of the fields of all of the dies correctly. Arguments are as
17526 in read_die_and_children.
17527 This the main entry point for reading a DIE and all its children. */
17528
17529static struct die_info *
17530read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17531 const gdb_byte *info_ptr,
17532 const gdb_byte **new_info_ptr,
bf6af496
DE
17533 struct die_info *parent)
17534{
17535 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17536 new_info_ptr, parent);
17537
b4f54984 17538 if (dwarf_die_debug)
bf6af496
DE
17539 {
17540 fprintf_unfiltered (gdb_stdlog,
17541 "Read die from %s@0x%x of %s:\n",
96b79293 17542 reader->die_section->get_name (),
bf6af496
DE
17543 (unsigned) (info_ptr - reader->die_section->buffer),
17544 bfd_get_filename (reader->abfd));
b4f54984 17545 dump_die (die, dwarf_die_debug);
bf6af496
DE
17546 }
17547
17548 return die;
17549}
17550
3019eac3
DE
17551/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17552 attributes.
17553 The caller is responsible for filling in the extra attributes
17554 and updating (*DIEP)->num_attrs.
17555 Set DIEP to point to a newly allocated die with its information,
3e225074 17556 except for its child, sibling, and parent fields. */
93311388 17557
d521ce57 17558static const gdb_byte *
3019eac3 17559read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17560 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17561 int num_extra_attrs)
93311388 17562{
b64f50a1 17563 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17564 struct abbrev_info *abbrev;
17565 struct die_info *die;
17566 struct dwarf2_cu *cu = reader->cu;
17567 bfd *abfd = reader->abfd;
17568
9c541725 17569 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17570 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17571 info_ptr += bytes_read;
17572 if (!abbrev_number)
17573 {
17574 *diep = NULL;
93311388
DE
17575 return info_ptr;
17576 }
17577
685af9cd 17578 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17579 if (!abbrev)
348e048f
DE
17580 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17581 abbrev_number,
17582 bfd_get_filename (abfd));
17583
3019eac3 17584 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17585 die->sect_off = sect_off;
93311388
DE
17586 die->tag = abbrev->tag;
17587 die->abbrev = abbrev_number;
3e225074 17588 die->has_children = abbrev->has_children;
93311388 17589
3019eac3
DE
17590 /* Make the result usable.
17591 The caller needs to update num_attrs after adding the extra
17592 attributes. */
93311388
DE
17593 die->num_attrs = abbrev->num_attrs;
17594
18a8505e 17595 std::vector<int> indexes_that_need_reprocess;
93311388 17596 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17597 {
17598 bool need_reprocess;
17599 info_ptr =
17600 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17601 info_ptr, &need_reprocess);
17602 if (need_reprocess)
17603 indexes_that_need_reprocess.push_back (i);
17604 }
17605
17606 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17607 if (attr != nullptr)
17608 cu->str_offsets_base = DW_UNSND (attr);
93311388 17609
18a8505e
AT
17610 auto maybe_addr_base = lookup_addr_base(die);
17611 if (maybe_addr_base.has_value ())
17612 cu->addr_base = *maybe_addr_base;
17613 for (int index : indexes_that_need_reprocess)
17614 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17615 *diep = die;
93311388
DE
17616 return info_ptr;
17617}
17618
3019eac3
DE
17619/* Read a die and all its attributes.
17620 Set DIEP to point to a newly allocated die with its information,
3e225074 17621 except for its child, sibling, and parent fields. */
3019eac3 17622
d521ce57 17623static const gdb_byte *
3019eac3 17624read_full_die (const struct die_reader_specs *reader,
3e225074 17625 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17626{
d521ce57 17627 const gdb_byte *result;
bf6af496 17628
3e225074 17629 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17630
b4f54984 17631 if (dwarf_die_debug)
bf6af496
DE
17632 {
17633 fprintf_unfiltered (gdb_stdlog,
17634 "Read die from %s@0x%x of %s:\n",
96b79293 17635 reader->die_section->get_name (),
bf6af496
DE
17636 (unsigned) (info_ptr - reader->die_section->buffer),
17637 bfd_get_filename (reader->abfd));
b4f54984 17638 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17639 }
17640
17641 return result;
3019eac3 17642}
433df2d4 17643\f
c906108c 17644
72bf9492
DJ
17645/* Returns nonzero if TAG represents a type that we might generate a partial
17646 symbol for. */
17647
17648static int
17649is_type_tag_for_partial (int tag)
17650{
17651 switch (tag)
17652 {
17653#if 0
17654 /* Some types that would be reasonable to generate partial symbols for,
17655 that we don't at present. */
17656 case DW_TAG_array_type:
17657 case DW_TAG_file_type:
17658 case DW_TAG_ptr_to_member_type:
17659 case DW_TAG_set_type:
17660 case DW_TAG_string_type:
17661 case DW_TAG_subroutine_type:
17662#endif
17663 case DW_TAG_base_type:
17664 case DW_TAG_class_type:
680b30c7 17665 case DW_TAG_interface_type:
72bf9492
DJ
17666 case DW_TAG_enumeration_type:
17667 case DW_TAG_structure_type:
17668 case DW_TAG_subrange_type:
17669 case DW_TAG_typedef:
17670 case DW_TAG_union_type:
17671 return 1;
17672 default:
17673 return 0;
17674 }
17675}
17676
17677/* Load all DIEs that are interesting for partial symbols into memory. */
17678
17679static struct partial_die_info *
dee91e82 17680load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17681 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17682{
dee91e82 17683 struct dwarf2_cu *cu = reader->cu;
518817b3 17684 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17685 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17686 unsigned int bytes_read;
5afb4e99 17687 unsigned int load_all = 0;
72bf9492
DJ
17688 int nesting_level = 1;
17689
17690 parent_die = NULL;
17691 last_die = NULL;
17692
7adf1e79
DE
17693 gdb_assert (cu->per_cu != NULL);
17694 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17695 load_all = 1;
17696
72bf9492
DJ
17697 cu->partial_dies
17698 = htab_create_alloc_ex (cu->header.length / 12,
17699 partial_die_hash,
17700 partial_die_eq,
17701 NULL,
17702 &cu->comp_unit_obstack,
17703 hashtab_obstack_allocate,
17704 dummy_obstack_deallocate);
17705
72bf9492
DJ
17706 while (1)
17707 {
685af9cd 17708 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17709
17710 /* A NULL abbrev means the end of a series of children. */
17711 if (abbrev == NULL)
17712 {
17713 if (--nesting_level == 0)
cd9983dd
YQ
17714 return first_die;
17715
72bf9492
DJ
17716 info_ptr += bytes_read;
17717 last_die = parent_die;
17718 parent_die = parent_die->die_parent;
17719 continue;
17720 }
17721
98bfdba5
PA
17722 /* Check for template arguments. We never save these; if
17723 they're seen, we just mark the parent, and go on our way. */
17724 if (parent_die != NULL
17725 && cu->language == language_cplus
17726 && (abbrev->tag == DW_TAG_template_type_param
17727 || abbrev->tag == DW_TAG_template_value_param))
17728 {
17729 parent_die->has_template_arguments = 1;
17730
17731 if (!load_all)
17732 {
17733 /* We don't need a partial DIE for the template argument. */
dee91e82 17734 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17735 continue;
17736 }
17737 }
17738
0d99eb77 17739 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17740 Skip their other children. */
17741 if (!load_all
17742 && cu->language == language_cplus
17743 && parent_die != NULL
17744 && parent_die->tag == DW_TAG_subprogram)
17745 {
dee91e82 17746 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17747 continue;
17748 }
17749
5afb4e99
DJ
17750 /* Check whether this DIE is interesting enough to save. Normally
17751 we would not be interested in members here, but there may be
17752 later variables referencing them via DW_AT_specification (for
17753 static members). */
17754 if (!load_all
17755 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17756 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17757 && abbrev->tag != DW_TAG_enumerator
17758 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17759 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17760 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17761 && abbrev->tag != DW_TAG_variable
5afb4e99 17762 && abbrev->tag != DW_TAG_namespace
f55ee35c 17763 && abbrev->tag != DW_TAG_module
95554aad 17764 && abbrev->tag != DW_TAG_member
74921315
KS
17765 && abbrev->tag != DW_TAG_imported_unit
17766 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17767 {
17768 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17769 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17770 continue;
17771 }
17772
6f06d47b
YQ
17773 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17774 abbrev);
cd9983dd 17775
48fbe735 17776 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17777
17778 /* This two-pass algorithm for processing partial symbols has a
17779 high cost in cache pressure. Thus, handle some simple cases
17780 here which cover the majority of C partial symbols. DIEs
17781 which neither have specification tags in them, nor could have
17782 specification tags elsewhere pointing at them, can simply be
17783 processed and discarded.
17784
17785 This segment is also optional; scan_partial_symbols and
17786 add_partial_symbol will handle these DIEs if we chain
17787 them in normally. When compilers which do not emit large
17788 quantities of duplicate debug information are more common,
17789 this code can probably be removed. */
17790
17791 /* Any complete simple types at the top level (pretty much all
17792 of them, for a language without namespaces), can be processed
17793 directly. */
17794 if (parent_die == NULL
cd9983dd
YQ
17795 && pdi.has_specification == 0
17796 && pdi.is_declaration == 0
17797 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17798 || pdi.tag == DW_TAG_base_type
17799 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17800 {
cd9983dd 17801 if (building_psymtab && pdi.name != NULL)
31edb802 17802 add_psymbol_to_list (pdi.name, false,
79748972 17803 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17804 psymbol_placement::STATIC,
1762568f 17805 0, cu->language, objfile);
cd9983dd 17806 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17807 continue;
17808 }
17809
d8228535
JK
17810 /* The exception for DW_TAG_typedef with has_children above is
17811 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17812 type_name_or_error will error on such types later.
d8228535
JK
17813
17814 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17815 it could not find the child DIEs referenced later, this is checked
17816 above. In correct DWARF DW_TAG_typedef should have no children. */
17817
cd9983dd 17818 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17819 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17820 "- DIE at %s [in module %s]"),
cd9983dd 17821 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17822
72bf9492
DJ
17823 /* If we're at the second level, and we're an enumerator, and
17824 our parent has no specification (meaning possibly lives in a
17825 namespace elsewhere), then we can add the partial symbol now
17826 instead of queueing it. */
cd9983dd 17827 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17828 && parent_die != NULL
17829 && parent_die->die_parent == NULL
17830 && parent_die->tag == DW_TAG_enumeration_type
17831 && parent_die->has_specification == 0)
17832 {
cd9983dd 17833 if (pdi.name == NULL)
b98664d3 17834 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17835 else if (building_psymtab)
31edb802 17836 add_psymbol_to_list (pdi.name, false,
79748972 17837 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17838 cu->language == language_cplus
75aedd27
TT
17839 ? psymbol_placement::GLOBAL
17840 : psymbol_placement::STATIC,
1762568f 17841 0, cu->language, objfile);
72bf9492 17842
cd9983dd 17843 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17844 continue;
17845 }
17846
cd9983dd 17847 struct partial_die_info *part_die
6f06d47b 17848 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17849
72bf9492
DJ
17850 /* We'll save this DIE so link it in. */
17851 part_die->die_parent = parent_die;
17852 part_die->die_sibling = NULL;
17853 part_die->die_child = NULL;
17854
17855 if (last_die && last_die == parent_die)
17856 last_die->die_child = part_die;
17857 else if (last_die)
17858 last_die->die_sibling = part_die;
17859
17860 last_die = part_die;
17861
17862 if (first_die == NULL)
17863 first_die = part_die;
17864
17865 /* Maybe add the DIE to the hash table. Not all DIEs that we
17866 find interesting need to be in the hash table, because we
17867 also have the parent/sibling/child chains; only those that we
17868 might refer to by offset later during partial symbol reading.
17869
17870 For now this means things that might have be the target of a
17871 DW_AT_specification, DW_AT_abstract_origin, or
17872 DW_AT_extension. DW_AT_extension will refer only to
17873 namespaces; DW_AT_abstract_origin refers to functions (and
17874 many things under the function DIE, but we do not recurse
17875 into function DIEs during partial symbol reading) and
17876 possibly variables as well; DW_AT_specification refers to
17877 declarations. Declarations ought to have the DW_AT_declaration
17878 flag. It happens that GCC forgets to put it in sometimes, but
17879 only for functions, not for types.
17880
17881 Adding more things than necessary to the hash table is harmless
17882 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17883 wasted time in find_partial_die, when we reread the compilation
17884 unit with load_all_dies set. */
72bf9492 17885
5afb4e99 17886 if (load_all
72929c62 17887 || abbrev->tag == DW_TAG_constant
5afb4e99 17888 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17889 || abbrev->tag == DW_TAG_variable
17890 || abbrev->tag == DW_TAG_namespace
17891 || part_die->is_declaration)
17892 {
17893 void **slot;
17894
17895 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17896 to_underlying (part_die->sect_off),
17897 INSERT);
72bf9492
DJ
17898 *slot = part_die;
17899 }
17900
72bf9492 17901 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17902 we have no reason to follow the children of structures; for other
98bfdba5
PA
17903 languages we have to, so that we can get at method physnames
17904 to infer fully qualified class names, for DW_AT_specification,
17905 and for C++ template arguments. For C++, we also look one level
17906 inside functions to find template arguments (if the name of the
17907 function does not already contain the template arguments).
bc30ff58 17908
0a4b0913
AB
17909 For Ada and Fortran, we need to scan the children of subprograms
17910 and lexical blocks as well because these languages allow the
17911 definition of nested entities that could be interesting for the
17912 debugger, such as nested subprograms for instance. */
72bf9492 17913 if (last_die->has_children
5afb4e99
DJ
17914 && (load_all
17915 || last_die->tag == DW_TAG_namespace
f55ee35c 17916 || last_die->tag == DW_TAG_module
72bf9492 17917 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17918 || (cu->language == language_cplus
17919 && last_die->tag == DW_TAG_subprogram
17920 && (last_die->name == NULL
17921 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17922 || (cu->language != language_c
17923 && (last_die->tag == DW_TAG_class_type
680b30c7 17924 || last_die->tag == DW_TAG_interface_type
72bf9492 17925 || last_die->tag == DW_TAG_structure_type
bc30ff58 17926 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17927 || ((cu->language == language_ada
17928 || cu->language == language_fortran)
bc30ff58
JB
17929 && (last_die->tag == DW_TAG_subprogram
17930 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17931 {
17932 nesting_level++;
17933 parent_die = last_die;
17934 continue;
17935 }
17936
17937 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17938 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17939
17940 /* Back to the top, do it again. */
17941 }
17942}
17943
6f06d47b
YQ
17944partial_die_info::partial_die_info (sect_offset sect_off_,
17945 struct abbrev_info *abbrev)
17946 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17947{
17948}
17949
35cc7ed7
YQ
17950/* Read a minimal amount of information into the minimal die structure.
17951 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17952
48fbe735
YQ
17953const gdb_byte *
17954partial_die_info::read (const struct die_reader_specs *reader,
17955 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17956{
dee91e82 17957 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17958 struct dwarf2_per_objfile *dwarf2_per_objfile
17959 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17960 unsigned int i;
c5aa993b 17961 int has_low_pc_attr = 0;
c906108c 17962 int has_high_pc_attr = 0;
91da1414 17963 int high_pc_relative = 0;
c906108c 17964
18a8505e 17965 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 17966 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17967 {
18a8505e
AT
17968 bool need_reprocess;
17969 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17970 info_ptr, &need_reprocess);
17971 /* String and address offsets that need to do the reprocessing have
17972 already been read at this point, so there is no need to wait until
17973 the loop terminates to do the reprocessing. */
17974 if (need_reprocess)
17975 read_attribute_reprocess (reader, &attr_vec[i]);
17976 attribute &attr = attr_vec[i];
c906108c 17977 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17978 partial symbol table. */
c906108c
SS
17979 switch (attr.name)
17980 {
17981 case DW_AT_name:
48fbe735 17982 switch (tag)
71c25dea
TT
17983 {
17984 case DW_TAG_compile_unit:
95554aad 17985 case DW_TAG_partial_unit:
348e048f 17986 case DW_TAG_type_unit:
71c25dea
TT
17987 /* Compilation units have a DW_AT_name that is a filename, not
17988 a source language identifier. */
17989 case DW_TAG_enumeration_type:
17990 case DW_TAG_enumerator:
17991 /* These tags always have simple identifiers already; no need
17992 to canonicalize them. */
48fbe735 17993 name = DW_STRING (&attr);
71c25dea
TT
17994 break;
17995 default:
48fbe735
YQ
17996 {
17997 struct objfile *objfile = dwarf2_per_objfile->objfile;
17998
17999 name
be1e3d3e 18000 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18001 }
71c25dea
TT
18002 break;
18003 }
c906108c 18004 break;
31ef98ae 18005 case DW_AT_linkage_name:
c906108c 18006 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18007 /* Note that both forms of linkage name might appear. We
18008 assume they will be the same, and we only store the last
18009 one we see. */
48fbe735 18010 linkage_name = DW_STRING (&attr);
c906108c
SS
18011 break;
18012 case DW_AT_low_pc:
18013 has_low_pc_attr = 1;
cd6c91b4 18014 lowpc = attr.value_as_address ();
c906108c
SS
18015 break;
18016 case DW_AT_high_pc:
18017 has_high_pc_attr = 1;
cd6c91b4
TT
18018 highpc = attr.value_as_address ();
18019 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18020 high_pc_relative = 1;
c906108c
SS
18021 break;
18022 case DW_AT_location:
0963b4bd 18023 /* Support the .debug_loc offsets. */
4fc6c0d5 18024 if (attr.form_is_block ())
8e19ed76 18025 {
48fbe735 18026 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18027 }
cd6c91b4 18028 else if (attr.form_is_section_offset ())
8e19ed76 18029 {
4d3c2250 18030 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18031 }
18032 else
18033 {
4d3c2250
KB
18034 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18035 "partial symbol information");
8e19ed76 18036 }
c906108c 18037 break;
c906108c 18038 case DW_AT_external:
48fbe735 18039 is_external = DW_UNSND (&attr);
c906108c
SS
18040 break;
18041 case DW_AT_declaration:
48fbe735 18042 is_declaration = DW_UNSND (&attr);
c906108c
SS
18043 break;
18044 case DW_AT_type:
48fbe735 18045 has_type = 1;
c906108c
SS
18046 break;
18047 case DW_AT_abstract_origin:
18048 case DW_AT_specification:
72bf9492 18049 case DW_AT_extension:
48fbe735
YQ
18050 has_specification = 1;
18051 spec_offset = dwarf2_get_ref_die_offset (&attr);
18052 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18053 || cu->per_cu->is_dwz);
c906108c
SS
18054 break;
18055 case DW_AT_sibling:
18056 /* Ignore absolute siblings, they might point outside of
18057 the current compile unit. */
18058 if (attr.form == DW_FORM_ref_addr)
b98664d3 18059 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18060 else
b9502d3f 18061 {
48fbe735 18062 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18063 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18064 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18065
18066 if (sibling_ptr < info_ptr)
b98664d3 18067 complaint (_("DW_AT_sibling points backwards"));
22869d73 18068 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18069 reader->die_section->overflow_complaint ();
b9502d3f 18070 else
48fbe735 18071 sibling = sibling_ptr;
b9502d3f 18072 }
c906108c 18073 break;
fa4028e9 18074 case DW_AT_byte_size:
48fbe735 18075 has_byte_size = 1;
fa4028e9 18076 break;
ff908ebf 18077 case DW_AT_const_value:
48fbe735 18078 has_const_value = 1;
ff908ebf 18079 break;
68511cec
CES
18080 case DW_AT_calling_convention:
18081 /* DWARF doesn't provide a way to identify a program's source-level
18082 entry point. DW_AT_calling_convention attributes are only meant
18083 to describe functions' calling conventions.
18084
18085 However, because it's a necessary piece of information in
0c1b455e
TT
18086 Fortran, and before DWARF 4 DW_CC_program was the only
18087 piece of debugging information whose definition refers to
18088 a 'main program' at all, several compilers marked Fortran
18089 main programs with DW_CC_program --- even when those
18090 functions use the standard calling conventions.
18091
18092 Although DWARF now specifies a way to provide this
18093 information, we support this practice for backward
18094 compatibility. */
68511cec 18095 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18096 && cu->language == language_fortran)
48fbe735 18097 main_subprogram = 1;
68511cec 18098 break;
481860b3
GB
18099 case DW_AT_inline:
18100 if (DW_UNSND (&attr) == DW_INL_inlined
18101 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18102 may_be_inlined = 1;
481860b3 18103 break;
95554aad
TT
18104
18105 case DW_AT_import:
48fbe735 18106 if (tag == DW_TAG_imported_unit)
36586728 18107 {
48fbe735
YQ
18108 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18109 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18110 || cu->per_cu->is_dwz);
18111 }
95554aad
TT
18112 break;
18113
0c1b455e 18114 case DW_AT_main_subprogram:
48fbe735 18115 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18116 break;
18117
05caa1d2
TT
18118 case DW_AT_ranges:
18119 {
18120 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18121 but that requires a full DIE, so instead we just
18122 reimplement it. */
18123 int need_ranges_base = tag != DW_TAG_compile_unit;
18124 unsigned int ranges_offset = (DW_UNSND (&attr)
18125 + (need_ranges_base
18126 ? cu->ranges_base
18127 : 0));
18128
18129 /* Value of the DW_AT_ranges attribute is the offset in the
18130 .debug_ranges section. */
18131 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18132 nullptr))
18133 has_pc_info = 1;
18134 }
18135 break;
18136
c906108c
SS
18137 default:
18138 break;
18139 }
18140 }
18141
10d06d82
TT
18142 /* For Ada, if both the name and the linkage name appear, we prefer
18143 the latter. This lets "catch exception" work better, regardless
18144 of the order in which the name and linkage name were emitted.
18145 Really, though, this is just a workaround for the fact that gdb
18146 doesn't store both the name and the linkage name. */
18147 if (cu->language == language_ada && linkage_name != nullptr)
18148 name = linkage_name;
18149
91da1414 18150 if (high_pc_relative)
48fbe735 18151 highpc += lowpc;
91da1414 18152
9373cf26
JK
18153 if (has_low_pc_attr && has_high_pc_attr)
18154 {
18155 /* When using the GNU linker, .gnu.linkonce. sections are used to
18156 eliminate duplicate copies of functions and vtables and such.
18157 The linker will arbitrarily choose one and discard the others.
18158 The AT_*_pc values for such functions refer to local labels in
18159 these sections. If the section from that file was discarded, the
18160 labels are not in the output, so the relocs get a value of 0.
18161 If this is a discarded function, mark the pc bounds as invalid,
18162 so that GDB will ignore it. */
48fbe735 18163 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18164 {
48fbe735 18165 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18166 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18167
b98664d3 18168 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18169 "for DIE at %s [in module %s]"),
48fbe735
YQ
18170 paddress (gdbarch, lowpc),
18171 sect_offset_str (sect_off),
9d8780f0 18172 objfile_name (objfile));
9373cf26
JK
18173 }
18174 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18175 else if (lowpc >= highpc)
9373cf26 18176 {
48fbe735 18177 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18178 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18179
b98664d3 18180 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18181 "for DIE at %s [in module %s]"),
48fbe735
YQ
18182 paddress (gdbarch, lowpc),
18183 paddress (gdbarch, highpc),
18184 sect_offset_str (sect_off),
9c541725 18185 objfile_name (objfile));
9373cf26
JK
18186 }
18187 else
48fbe735 18188 has_pc_info = 1;
9373cf26 18189 }
85cbf3d3 18190
c906108c
SS
18191 return info_ptr;
18192}
18193
72bf9492
DJ
18194/* Find a cached partial DIE at OFFSET in CU. */
18195
d590ff25
YQ
18196struct partial_die_info *
18197dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18198{
18199 struct partial_die_info *lookup_die = NULL;
6f06d47b 18200 struct partial_die_info part_die (sect_off);
72bf9492 18201
9a3c8263 18202 lookup_die = ((struct partial_die_info *)
d590ff25 18203 htab_find_with_hash (partial_dies, &part_die,
9c541725 18204 to_underlying (sect_off)));
72bf9492 18205
72bf9492
DJ
18206 return lookup_die;
18207}
18208
348e048f
DE
18209/* Find a partial DIE at OFFSET, which may or may not be in CU,
18210 except in the case of .debug_types DIEs which do not reference
18211 outside their CU (they do however referencing other types via
55f1336d 18212 DW_FORM_ref_sig8). */
72bf9492 18213
122cf0f2 18214static const struct cu_partial_die_info
9c541725 18215find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18216{
518817b3
SM
18217 struct dwarf2_per_objfile *dwarf2_per_objfile
18218 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18219 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18220 struct dwarf2_per_cu_data *per_cu = NULL;
18221 struct partial_die_info *pd = NULL;
72bf9492 18222
36586728 18223 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18224 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18225 {
d590ff25 18226 pd = cu->find_partial_die (sect_off);
5afb4e99 18227 if (pd != NULL)
fb816e8b 18228 return { cu, pd };
0d99eb77
DE
18229 /* We missed recording what we needed.
18230 Load all dies and try again. */
18231 per_cu = cu->per_cu;
5afb4e99 18232 }
0d99eb77
DE
18233 else
18234 {
18235 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18236 if (cu->per_cu->is_debug_types)
0d99eb77 18237 {
9d8780f0
SM
18238 error (_("Dwarf Error: Type Unit at offset %s contains"
18239 " external reference to offset %s [in module %s].\n"),
18240 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18241 bfd_get_filename (objfile->obfd));
18242 }
9c541725 18243 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18244 dwarf2_per_objfile);
72bf9492 18245
0d99eb77
DE
18246 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18247 load_partial_comp_unit (per_cu);
ae038cb0 18248
0d99eb77 18249 per_cu->cu->last_used = 0;
d590ff25 18250 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18251 }
5afb4e99 18252
dee91e82
DE
18253 /* If we didn't find it, and not all dies have been loaded,
18254 load them all and try again. */
18255
5afb4e99
DJ
18256 if (pd == NULL && per_cu->load_all_dies == 0)
18257 {
5afb4e99 18258 per_cu->load_all_dies = 1;
fd820528
DE
18259
18260 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18261 THIS_CU->cu may already be in use. So we can't just free it and
18262 replace its DIEs with the ones we read in. Instead, we leave those
18263 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18264 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18265 set. */
dee91e82 18266 load_partial_comp_unit (per_cu);
5afb4e99 18267
d590ff25 18268 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18269 }
18270
18271 if (pd == NULL)
18272 internal_error (__FILE__, __LINE__,
9d8780f0 18273 _("could not find partial DIE %s "
3e43a32a 18274 "in cache [from module %s]\n"),
9d8780f0 18275 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18276 return { per_cu->cu, pd };
72bf9492
DJ
18277}
18278
abc72ce4
DE
18279/* See if we can figure out if the class lives in a namespace. We do
18280 this by looking for a member function; its demangled name will
18281 contain namespace info, if there is any. */
18282
18283static void
18284guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18285 struct dwarf2_cu *cu)
18286{
18287 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18288 what template types look like, because the demangler
18289 frequently doesn't give the same name as the debug info. We
18290 could fix this by only using the demangled name to get the
18291 prefix (but see comment in read_structure_type). */
18292
18293 struct partial_die_info *real_pdi;
18294 struct partial_die_info *child_pdi;
18295
18296 /* If this DIE (this DIE's specification, if any) has a parent, then
18297 we should not do this. We'll prepend the parent's fully qualified
18298 name when we create the partial symbol. */
18299
18300 real_pdi = struct_pdi;
18301 while (real_pdi->has_specification)
fb816e8b 18302 {
122cf0f2
AB
18303 auto res = find_partial_die (real_pdi->spec_offset,
18304 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18305 real_pdi = res.pdi;
18306 cu = res.cu;
18307 }
abc72ce4
DE
18308
18309 if (real_pdi->die_parent != NULL)
18310 return;
18311
18312 for (child_pdi = struct_pdi->die_child;
18313 child_pdi != NULL;
18314 child_pdi = child_pdi->die_sibling)
18315 {
18316 if (child_pdi->tag == DW_TAG_subprogram
18317 && child_pdi->linkage_name != NULL)
18318 {
43816ebc
TT
18319 gdb::unique_xmalloc_ptr<char> actual_class_name
18320 (language_class_name_from_physname (cu->language_defn,
18321 child_pdi->linkage_name));
abc72ce4
DE
18322 if (actual_class_name != NULL)
18323 {
518817b3 18324 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18325 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18326 }
18327 break;
18328 }
18329 }
18330}
18331
52356b79
YQ
18332void
18333partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18334{
abc72ce4
DE
18335 /* Once we've fixed up a die, there's no point in doing so again.
18336 This also avoids a memory leak if we were to call
18337 guess_partial_die_structure_name multiple times. */
52356b79 18338 if (fixup_called)
abc72ce4
DE
18339 return;
18340
72bf9492
DJ
18341 /* If we found a reference attribute and the DIE has no name, try
18342 to find a name in the referred to DIE. */
18343
52356b79 18344 if (name == NULL && has_specification)
72bf9492
DJ
18345 {
18346 struct partial_die_info *spec_die;
72bf9492 18347
122cf0f2 18348 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18349 spec_die = res.pdi;
18350 cu = res.cu;
72bf9492 18351
52356b79 18352 spec_die->fixup (cu);
72bf9492
DJ
18353
18354 if (spec_die->name)
18355 {
52356b79 18356 name = spec_die->name;
72bf9492
DJ
18357
18358 /* Copy DW_AT_external attribute if it is set. */
18359 if (spec_die->is_external)
52356b79 18360 is_external = spec_die->is_external;
72bf9492
DJ
18361 }
18362 }
18363
18364 /* Set default names for some unnamed DIEs. */
72bf9492 18365
52356b79
YQ
18366 if (name == NULL && tag == DW_TAG_namespace)
18367 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18368
abc72ce4
DE
18369 /* If there is no parent die to provide a namespace, and there are
18370 children, see if we can determine the namespace from their linkage
122d1940 18371 name. */
abc72ce4 18372 if (cu->language == language_cplus
fd5866f6 18373 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18374 && die_parent == NULL
18375 && has_children
18376 && (tag == DW_TAG_class_type
18377 || tag == DW_TAG_structure_type
18378 || tag == DW_TAG_union_type))
18379 guess_partial_die_structure_name (this, cu);
abc72ce4 18380
53832f31
TT
18381 /* GCC might emit a nameless struct or union that has a linkage
18382 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18383 if (name == NULL
18384 && (tag == DW_TAG_class_type
18385 || tag == DW_TAG_interface_type
18386 || tag == DW_TAG_structure_type
18387 || tag == DW_TAG_union_type)
18388 && linkage_name != NULL)
53832f31 18389 {
43816ebc
TT
18390 gdb::unique_xmalloc_ptr<char> demangled
18391 (gdb_demangle (linkage_name, DMGL_TYPES));
18392 if (demangled != nullptr)
53832f31 18393 {
96408a79
SA
18394 const char *base;
18395
18396 /* Strip any leading namespaces/classes, keep only the base name.
18397 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18398 base = strrchr (demangled.get (), ':');
18399 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18400 base++;
18401 else
43816ebc 18402 base = demangled.get ();
96408a79 18403
518817b3 18404 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18405 name = objfile->intern (base);
53832f31
TT
18406 }
18407 }
18408
52356b79 18409 fixup_called = 1;
72bf9492
DJ
18410}
18411
18a8505e
AT
18412/* Process the attributes that had to be skipped in the first round. These
18413 attributes are the ones that need str_offsets_base or addr_base attributes.
18414 They could not have been processed in the first round, because at the time
18415 the values of str_offsets_base or addr_base may not have been known. */
18416void read_attribute_reprocess (const struct die_reader_specs *reader,
18417 struct attribute *attr)
18418{
18419 struct dwarf2_cu *cu = reader->cu;
18420 switch (attr->form)
18421 {
18422 case DW_FORM_addrx:
18423 case DW_FORM_GNU_addr_index:
18424 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18425 break;
18426 case DW_FORM_strx:
18427 case DW_FORM_strx1:
18428 case DW_FORM_strx2:
18429 case DW_FORM_strx3:
18430 case DW_FORM_strx4:
18431 case DW_FORM_GNU_str_index:
18432 {
18433 unsigned int str_index = DW_UNSND (attr);
18434 if (reader->dwo_file != NULL)
18435 {
18436 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18437 DW_STRING_IS_CANONICAL (attr) = 0;
18438 }
18439 else
18440 {
18441 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18442 DW_STRING_IS_CANONICAL (attr) = 0;
18443 }
18444 break;
18445 }
18446 default:
18447 gdb_assert_not_reached (_("Unexpected DWARF form."));
18448 }
18449}
18450
a8329558 18451/* Read an attribute value described by an attribute form. */
c906108c 18452
d521ce57 18453static const gdb_byte *
dee91e82
DE
18454read_attribute_value (const struct die_reader_specs *reader,
18455 struct attribute *attr, unsigned form,
18a8505e
AT
18456 LONGEST implicit_const, const gdb_byte *info_ptr,
18457 bool *need_reprocess)
c906108c 18458{
dee91e82 18459 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18460 struct dwarf2_per_objfile *dwarf2_per_objfile
18461 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18462 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18464 bfd *abfd = reader->abfd;
e7c27a73 18465 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18466 unsigned int bytes_read;
18467 struct dwarf_block *blk;
18a8505e 18468 *need_reprocess = false;
c906108c 18469
aead7601 18470 attr->form = (enum dwarf_form) form;
a8329558 18471 switch (form)
c906108c 18472 {
c906108c 18473 case DW_FORM_ref_addr:
ae411497 18474 if (cu->header.version == 2)
c8a7a66f
TT
18475 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18476 &bytes_read);
ae411497 18477 else
8266302d
TT
18478 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18479 &bytes_read);
ae411497
TT
18480 info_ptr += bytes_read;
18481 break;
36586728 18482 case DW_FORM_GNU_ref_alt:
8266302d 18483 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18484 info_ptr += bytes_read;
18485 break;
ae411497 18486 case DW_FORM_addr:
c8a7a66f 18487 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18488 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18489 info_ptr += bytes_read;
c906108c
SS
18490 break;
18491 case DW_FORM_block2:
7b5a2f43 18492 blk = dwarf_alloc_block (cu);
c906108c
SS
18493 blk->size = read_2_bytes (abfd, info_ptr);
18494 info_ptr += 2;
18495 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18496 info_ptr += blk->size;
18497 DW_BLOCK (attr) = blk;
18498 break;
18499 case DW_FORM_block4:
7b5a2f43 18500 blk = dwarf_alloc_block (cu);
c906108c
SS
18501 blk->size = read_4_bytes (abfd, info_ptr);
18502 info_ptr += 4;
18503 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18504 info_ptr += blk->size;
18505 DW_BLOCK (attr) = blk;
18506 break;
18507 case DW_FORM_data2:
18508 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18509 info_ptr += 2;
18510 break;
18511 case DW_FORM_data4:
18512 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18513 info_ptr += 4;
18514 break;
18515 case DW_FORM_data8:
18516 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18517 info_ptr += 8;
18518 break;
0224619f
JK
18519 case DW_FORM_data16:
18520 blk = dwarf_alloc_block (cu);
18521 blk->size = 16;
18522 blk->data = read_n_bytes (abfd, info_ptr, 16);
18523 info_ptr += 16;
18524 DW_BLOCK (attr) = blk;
18525 break;
2dc7f7b3 18526 case DW_FORM_sec_offset:
8266302d 18527 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18528 info_ptr += bytes_read;
18529 break;
c906108c 18530 case DW_FORM_string:
9b1c24c8 18531 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18532 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18533 info_ptr += bytes_read;
18534 break;
4bdf3d34 18535 case DW_FORM_strp:
36586728
TT
18536 if (!cu->per_cu->is_dwz)
18537 {
ed2dc618
SM
18538 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18539 abfd, info_ptr, cu_header,
36586728
TT
18540 &bytes_read);
18541 DW_STRING_IS_CANONICAL (attr) = 0;
18542 info_ptr += bytes_read;
18543 break;
18544 }
18545 /* FALLTHROUGH */
43988095
JK
18546 case DW_FORM_line_strp:
18547 if (!cu->per_cu->is_dwz)
18548 {
86c0bb4c
TT
18549 DW_STRING (attr)
18550 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18551 &bytes_read);
43988095
JK
18552 DW_STRING_IS_CANONICAL (attr) = 0;
18553 info_ptr += bytes_read;
18554 break;
18555 }
18556 /* FALLTHROUGH */
36586728
TT
18557 case DW_FORM_GNU_strp_alt:
18558 {
ed2dc618 18559 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18560 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18561 &bytes_read);
36586728 18562
0314b390 18563 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
18564 DW_STRING_IS_CANONICAL (attr) = 0;
18565 info_ptr += bytes_read;
18566 }
4bdf3d34 18567 break;
2dc7f7b3 18568 case DW_FORM_exprloc:
c906108c 18569 case DW_FORM_block:
7b5a2f43 18570 blk = dwarf_alloc_block (cu);
c906108c
SS
18571 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18572 info_ptr += bytes_read;
18573 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18574 info_ptr += blk->size;
18575 DW_BLOCK (attr) = blk;
18576 break;
18577 case DW_FORM_block1:
7b5a2f43 18578 blk = dwarf_alloc_block (cu);
c906108c
SS
18579 blk->size = read_1_byte (abfd, info_ptr);
18580 info_ptr += 1;
18581 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18582 info_ptr += blk->size;
18583 DW_BLOCK (attr) = blk;
18584 break;
18585 case DW_FORM_data1:
18586 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18587 info_ptr += 1;
18588 break;
18589 case DW_FORM_flag:
18590 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18591 info_ptr += 1;
18592 break;
2dc7f7b3
TT
18593 case DW_FORM_flag_present:
18594 DW_UNSND (attr) = 1;
18595 break;
c906108c
SS
18596 case DW_FORM_sdata:
18597 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18598 info_ptr += bytes_read;
18599 break;
18600 case DW_FORM_udata:
18a8505e 18601 case DW_FORM_rnglistx:
c906108c
SS
18602 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18603 info_ptr += bytes_read;
18604 break;
18605 case DW_FORM_ref1:
9c541725 18606 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18607 + read_1_byte (abfd, info_ptr));
c906108c
SS
18608 info_ptr += 1;
18609 break;
18610 case DW_FORM_ref2:
9c541725 18611 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18612 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18613 info_ptr += 2;
18614 break;
18615 case DW_FORM_ref4:
9c541725 18616 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18617 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18618 info_ptr += 4;
18619 break;
613e1657 18620 case DW_FORM_ref8:
9c541725 18621 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18622 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18623 info_ptr += 8;
18624 break;
55f1336d 18625 case DW_FORM_ref_sig8:
ac9ec31b 18626 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18627 info_ptr += 8;
18628 break;
c906108c 18629 case DW_FORM_ref_udata:
9c541725 18630 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18631 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18632 info_ptr += bytes_read;
18633 break;
c906108c 18634 case DW_FORM_indirect:
a8329558
KW
18635 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18636 info_ptr += bytes_read;
43988095
JK
18637 if (form == DW_FORM_implicit_const)
18638 {
18639 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18640 info_ptr += bytes_read;
18641 }
18642 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18643 info_ptr, need_reprocess);
43988095
JK
18644 break;
18645 case DW_FORM_implicit_const:
18646 DW_SND (attr) = implicit_const;
a8329558 18647 break;
336d760d 18648 case DW_FORM_addrx:
3019eac3 18649 case DW_FORM_GNU_addr_index:
18a8505e
AT
18650 *need_reprocess = true;
18651 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18652 info_ptr += bytes_read;
18653 break;
cf532bd1 18654 case DW_FORM_strx:
15f18d14
AT
18655 case DW_FORM_strx1:
18656 case DW_FORM_strx2:
18657 case DW_FORM_strx3:
18658 case DW_FORM_strx4:
3019eac3 18659 case DW_FORM_GNU_str_index:
3019eac3 18660 {
15f18d14
AT
18661 ULONGEST str_index;
18662 if (form == DW_FORM_strx1)
18663 {
18664 str_index = read_1_byte (abfd, info_ptr);
18665 info_ptr += 1;
18666 }
18667 else if (form == DW_FORM_strx2)
18668 {
18669 str_index = read_2_bytes (abfd, info_ptr);
18670 info_ptr += 2;
18671 }
18672 else if (form == DW_FORM_strx3)
18673 {
18674 str_index = read_3_bytes (abfd, info_ptr);
18675 info_ptr += 3;
18676 }
18677 else if (form == DW_FORM_strx4)
18678 {
18679 str_index = read_4_bytes (abfd, info_ptr);
18680 info_ptr += 4;
18681 }
18682 else
18683 {
18684 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18685 info_ptr += bytes_read;
18686 }
18a8505e
AT
18687 *need_reprocess = true;
18688 DW_UNSND (attr) = str_index;
18689 }
3019eac3 18690 break;
c906108c 18691 default:
8a3fe4f8 18692 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18693 dwarf_form_name (form),
18694 bfd_get_filename (abfd));
c906108c 18695 }
28e94949 18696
36586728 18697 /* Super hack. */
cd6c91b4 18698 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18699 attr->form = DW_FORM_GNU_ref_alt;
18700
28e94949
JB
18701 /* We have seen instances where the compiler tried to emit a byte
18702 size attribute of -1 which ended up being encoded as an unsigned
18703 0xffffffff. Although 0xffffffff is technically a valid size value,
18704 an object of this size seems pretty unlikely so we can relatively
18705 safely treat these cases as if the size attribute was invalid and
18706 treat them as zero by default. */
18707 if (attr->name == DW_AT_byte_size
18708 && form == DW_FORM_data4
18709 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18710 {
18711 complaint
b98664d3 18712 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18713 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18714 DW_UNSND (attr) = 0;
18715 }
28e94949 18716
c906108c
SS
18717 return info_ptr;
18718}
18719
a8329558
KW
18720/* Read an attribute described by an abbreviated attribute. */
18721
d521ce57 18722static const gdb_byte *
dee91e82
DE
18723read_attribute (const struct die_reader_specs *reader,
18724 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18725 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18726{
18727 attr->name = abbrev->name;
43988095 18728 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18729 abbrev->implicit_const, info_ptr,
18730 need_reprocess);
a8329558
KW
18731}
18732
c764a876
DE
18733/* Cover function for read_initial_length.
18734 Returns the length of the object at BUF, and stores the size of the
18735 initial length in *BYTES_READ and stores the size that offsets will be in
18736 *OFFSET_SIZE.
18737 If the initial length size is not equivalent to that specified in
18738 CU_HEADER then issue a complaint.
18739 This is useful when reading non-comp-unit headers. */
dd373385 18740
c764a876 18741static LONGEST
d521ce57 18742read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18743 const struct comp_unit_head *cu_header,
18744 unsigned int *bytes_read,
18745 unsigned int *offset_size)
18746{
18747 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18748
18749 gdb_assert (cu_header->initial_length_size == 4
18750 || cu_header->initial_length_size == 8
18751 || cu_header->initial_length_size == 12);
18752
18753 if (cu_header->initial_length_size != *bytes_read)
b98664d3 18754 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18755
c764a876 18756 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18757 return length;
613e1657
KB
18758}
18759
43988095
JK
18760/* Return pointer to string at .debug_str offset STR_OFFSET. */
18761
18762static const char *
ed2dc618 18763read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 18764 LONGEST str_offset)
43988095 18765{
4f44ae6c
TT
18766 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18767 str_offset, "DW_FORM_strp");
c906108c
SS
18768}
18769
43988095
JK
18770/* Return pointer to string at .debug_str offset as read from BUF.
18771 BUF is assumed to be in a compilation unit described by CU_HEADER.
18772 Return *BYTES_READ_PTR count of bytes read from BUF. */
18773
d521ce57 18774static const char *
ed2dc618
SM
18775read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18776 const gdb_byte *buf,
cf2c3c16
TT
18777 const struct comp_unit_head *cu_header,
18778 unsigned int *bytes_read_ptr)
18779{
8266302d 18780 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18781
4f44ae6c 18782 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
18783}
18784
86c0bb4c 18785/* See read.h. */
43988095 18786
86c0bb4c
TT
18787const char *
18788dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
43988095
JK
18789 const struct comp_unit_head *cu_header,
18790 unsigned int *bytes_read_ptr)
18791{
86c0bb4c 18792 bfd *abfd = objfile->obfd;
8266302d 18793 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18794
86c0bb4c 18795 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
18796}
18797
3019eac3 18798/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18799 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18800 ADDR_SIZE is the size of addresses from the CU header. */
18801
18802static CORE_ADDR
ed2dc618 18803read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18804 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18805 int addr_size)
3019eac3
DE
18806{
18807 struct objfile *objfile = dwarf2_per_objfile->objfile;
18808 bfd *abfd = objfile->obfd;
18809 const gdb_byte *info_ptr;
18a8505e 18810 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18811
96b79293 18812 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18813 if (dwarf2_per_objfile->addr.buffer == NULL)
18814 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18815 objfile_name (objfile));
18a8505e
AT
18816 if (addr_base_or_zero + addr_index * addr_size
18817 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18818 error (_("DW_FORM_addr_index pointing outside of "
18819 ".debug_addr section [in module %s]"),
4262abfb 18820 objfile_name (objfile));
3019eac3 18821 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18822 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18823 if (addr_size == 4)
18824 return bfd_get_32 (abfd, info_ptr);
18825 else
18826 return bfd_get_64 (abfd, info_ptr);
18827}
18828
18829/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18830
18831static CORE_ADDR
18832read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18833{
518817b3
SM
18834 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18835 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18836}
18837
18838/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18839
18840static CORE_ADDR
d521ce57 18841read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18842 unsigned int *bytes_read)
18843{
518817b3 18844 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18845 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18846
18847 return read_addr_index (cu, addr_index);
18848}
18849
450a1bfc 18850/* See read.h. */
3019eac3
DE
18851
18852CORE_ADDR
450a1bfc 18853dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18854{
ed2dc618 18855 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18856 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18857 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18858 int addr_size;
18859
3019eac3
DE
18860 /* We need addr_base and addr_size.
18861 If we don't have PER_CU->cu, we have to get it.
18862 Nasty, but the alternative is storing the needed info in PER_CU,
18863 which at this point doesn't seem justified: it's not clear how frequently
18864 it would get used and it would increase the size of every PER_CU.
18865 Entry points like dwarf2_per_cu_addr_size do a similar thing
18866 so we're not in uncharted territory here.
18867 Alas we need to be a bit more complicated as addr_base is contained
18868 in the DIE.
18869
18870 We don't need to read the entire CU(/TU).
18871 We just need the header and top level die.
a1b64ce1 18872
3019eac3 18873 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18874 For now we skip this optimization. */
3019eac3
DE
18875
18876 if (cu != NULL)
18877 {
18878 addr_base = cu->addr_base;
18879 addr_size = cu->header.addr_size;
18880 }
18881 else
18882 {
6751ebae 18883 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18884 addr_base = reader.cu->addr_base;
18885 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18886 }
18887
ed2dc618
SM
18888 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18889 addr_size);
3019eac3
DE
18890}
18891
18a8505e
AT
18892/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18893 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18894 DWO file. */
3019eac3 18895
d521ce57 18896static const char *
18a8505e
AT
18897read_str_index (struct dwarf2_cu *cu,
18898 struct dwarf2_section_info *str_section,
18899 struct dwarf2_section_info *str_offsets_section,
18900 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18901{
518817b3
SM
18902 struct dwarf2_per_objfile *dwarf2_per_objfile
18903 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18904 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18905 const char *objf_name = objfile_name (objfile);
3019eac3 18906 bfd *abfd = objfile->obfd;
d521ce57 18907 const gdb_byte *info_ptr;
3019eac3 18908 ULONGEST str_offset;
cf532bd1 18909 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18910
96b79293
TT
18911 str_section->read (objfile);
18912 str_offsets_section->read (objfile);
73869dc2 18913 if (str_section->buffer == NULL)
18a8505e 18914 error (_("%s used without %s section"
9d8780f0 18915 " in CU at offset %s [in module %s]"),
96b79293 18916 form_name, str_section->get_name (),
18a8505e 18917 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18918 if (str_offsets_section->buffer == NULL)
18a8505e 18919 error (_("%s used without %s section"
9d8780f0 18920 " in CU at offset %s [in module %s]"),
96b79293 18921 form_name, str_section->get_name (),
18a8505e 18922 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18923 info_ptr = (str_offsets_section->buffer
18a8505e 18924 + str_offsets_base
3019eac3
DE
18925 + str_index * cu->header.offset_size);
18926 if (cu->header.offset_size == 4)
18927 str_offset = bfd_get_32 (abfd, info_ptr);
18928 else
18929 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18930 if (str_offset >= str_section->size)
57d63ce2 18931 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18932 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18933 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18934 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18935}
18936
18a8505e
AT
18937/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18938
18939static const char *
18940read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18941{
18942 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18943 ? reader->cu->header.addr_size : 0;
18944 return read_str_index (reader->cu,
18945 &reader->dwo_file->sections.str,
18946 &reader->dwo_file->sections.str_offsets,
18947 str_offsets_base, str_index);
18948}
18949
18950/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18951
18952static const char *
18953read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18954{
18955 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18956 const char *objf_name = objfile_name (objfile);
18957 static const char form_name[] = "DW_FORM_GNU_str_index";
18958 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18959
18960 if (!cu->str_offsets_base.has_value ())
18961 error (_("%s used in Fission stub without %s"
18962 " in CU at offset 0x%lx [in module %s]"),
18963 form_name, str_offsets_attr_name,
18964 (long) cu->header.offset_size, objf_name);
18965
18966 return read_str_index (cu,
18967 &cu->per_cu->dwarf2_per_objfile->str,
18968 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18969 *cu->str_offsets_base, str_index);
18970}
18971
3019eac3
DE
18972/* Return the length of an LEB128 number in BUF. */
18973
18974static int
18975leb128_size (const gdb_byte *buf)
18976{
18977 const gdb_byte *begin = buf;
18978 gdb_byte byte;
18979
18980 while (1)
18981 {
18982 byte = *buf++;
18983 if ((byte & 128) == 0)
18984 return buf - begin;
18985 }
18986}
18987
c906108c 18988static void
e142c38c 18989set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
18990{
18991 switch (lang)
18992 {
18993 case DW_LANG_C89:
76bee0cc 18994 case DW_LANG_C99:
0cfd832f 18995 case DW_LANG_C11:
c906108c 18996 case DW_LANG_C:
d1be3247 18997 case DW_LANG_UPC:
e142c38c 18998 cu->language = language_c;
c906108c 18999 break;
9c37b5ae 19000 case DW_LANG_Java:
c906108c 19001 case DW_LANG_C_plus_plus:
0cfd832f
MW
19002 case DW_LANG_C_plus_plus_11:
19003 case DW_LANG_C_plus_plus_14:
e142c38c 19004 cu->language = language_cplus;
c906108c 19005 break;
6aecb9c2
JB
19006 case DW_LANG_D:
19007 cu->language = language_d;
19008 break;
c906108c
SS
19009 case DW_LANG_Fortran77:
19010 case DW_LANG_Fortran90:
b21b22e0 19011 case DW_LANG_Fortran95:
f7de9aab
MW
19012 case DW_LANG_Fortran03:
19013 case DW_LANG_Fortran08:
e142c38c 19014 cu->language = language_fortran;
c906108c 19015 break;
a766d390
DE
19016 case DW_LANG_Go:
19017 cu->language = language_go;
19018 break;
c906108c 19019 case DW_LANG_Mips_Assembler:
e142c38c 19020 cu->language = language_asm;
c906108c
SS
19021 break;
19022 case DW_LANG_Ada83:
8aaf0b47 19023 case DW_LANG_Ada95:
bc5f45f8
JB
19024 cu->language = language_ada;
19025 break;
72019c9c
GM
19026 case DW_LANG_Modula2:
19027 cu->language = language_m2;
19028 break;
fe8e67fd
PM
19029 case DW_LANG_Pascal83:
19030 cu->language = language_pascal;
19031 break;
22566fbd
DJ
19032 case DW_LANG_ObjC:
19033 cu->language = language_objc;
19034 break;
c44af4eb
TT
19035 case DW_LANG_Rust:
19036 case DW_LANG_Rust_old:
19037 cu->language = language_rust;
19038 break;
c906108c
SS
19039 case DW_LANG_Cobol74:
19040 case DW_LANG_Cobol85:
c906108c 19041 default:
e142c38c 19042 cu->language = language_minimal;
c906108c
SS
19043 break;
19044 }
e142c38c 19045 cu->language_defn = language_def (cu->language);
c906108c
SS
19046}
19047
19048/* Return the named attribute or NULL if not there. */
19049
19050static struct attribute *
e142c38c 19051dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19052{
a48e046c 19053 for (;;)
c906108c 19054 {
a48e046c
TT
19055 unsigned int i;
19056 struct attribute *spec = NULL;
19057
19058 for (i = 0; i < die->num_attrs; ++i)
19059 {
19060 if (die->attrs[i].name == name)
19061 return &die->attrs[i];
19062 if (die->attrs[i].name == DW_AT_specification
19063 || die->attrs[i].name == DW_AT_abstract_origin)
19064 spec = &die->attrs[i];
19065 }
19066
19067 if (!spec)
19068 break;
c906108c 19069
f2f0e013 19070 die = follow_die_ref (die, spec, &cu);
f2f0e013 19071 }
c5aa993b 19072
c906108c
SS
19073 return NULL;
19074}
19075
348e048f
DE
19076/* Return the named attribute or NULL if not there,
19077 but do not follow DW_AT_specification, etc.
19078 This is for use in contexts where we're reading .debug_types dies.
19079 Following DW_AT_specification, DW_AT_abstract_origin will take us
19080 back up the chain, and we want to go down. */
19081
19082static struct attribute *
45e58e77 19083dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19084{
19085 unsigned int i;
19086
19087 for (i = 0; i < die->num_attrs; ++i)
19088 if (die->attrs[i].name == name)
19089 return &die->attrs[i];
19090
19091 return NULL;
19092}
19093
7d45c7c3
KB
19094/* Return the string associated with a string-typed attribute, or NULL if it
19095 is either not found or is of an incorrect type. */
19096
19097static const char *
19098dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19099{
19100 struct attribute *attr;
19101 const char *str = NULL;
19102
19103 attr = dwarf2_attr (die, name, cu);
19104
19105 if (attr != NULL)
19106 {
43988095 19107 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19108 || attr->form == DW_FORM_string
cf532bd1 19109 || attr->form == DW_FORM_strx
8fe0f950
AT
19110 || attr->form == DW_FORM_strx1
19111 || attr->form == DW_FORM_strx2
19112 || attr->form == DW_FORM_strx3
19113 || attr->form == DW_FORM_strx4
b3340438 19114 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19115 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19116 str = DW_STRING (attr);
19117 else
b98664d3 19118 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19119 "DIE at %s in module %s"),
19120 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19121 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19122 }
19123
19124 return str;
19125}
19126
a084a2a6 19127/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19128 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19129static const char *
19130dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19131{
19132 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19133 if (dwo_name == nullptr)
19134 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19135 return dwo_name;
19136}
19137
05cf31d1
JB
19138/* Return non-zero iff the attribute NAME is defined for the given DIE,
19139 and holds a non-zero value. This function should only be used for
2dc7f7b3 19140 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19141
19142static int
19143dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19144{
19145 struct attribute *attr = dwarf2_attr (die, name, cu);
19146
19147 return (attr && DW_UNSND (attr));
19148}
19149
3ca72b44 19150static int
e142c38c 19151die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19152{
05cf31d1
JB
19153 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19154 which value is non-zero. However, we have to be careful with
19155 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19156 (via dwarf2_flag_true_p) follows this attribute. So we may
19157 end up accidently finding a declaration attribute that belongs
19158 to a different DIE referenced by the specification attribute,
19159 even though the given DIE does not have a declaration attribute. */
19160 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19161 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19162}
19163
63d06c5c 19164/* Return the die giving the specification for DIE, if there is
f2f0e013 19165 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19166 containing the return value on output. If there is no
19167 specification, but there is an abstract origin, that is
19168 returned. */
63d06c5c
DC
19169
19170static struct die_info *
f2f0e013 19171die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19172{
f2f0e013
DJ
19173 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19174 *spec_cu);
63d06c5c 19175
edb3359d
DJ
19176 if (spec_attr == NULL)
19177 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19178
63d06c5c
DC
19179 if (spec_attr == NULL)
19180 return NULL;
19181 else
f2f0e013 19182 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19183}
c906108c 19184
527f3840
JK
19185/* Stub for free_line_header to match void * callback types. */
19186
19187static void
19188free_line_header_voidp (void *arg)
19189{
9a3c8263 19190 struct line_header *lh = (struct line_header *) arg;
527f3840 19191
fff8551c 19192 delete lh;
527f3840
JK
19193}
19194
83769d0b 19195/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19196
19197static struct dwarf2_section_info *
19198get_debug_line_section (struct dwarf2_cu *cu)
19199{
19200 struct dwarf2_section_info *section;
518817b3
SM
19201 struct dwarf2_per_objfile *dwarf2_per_objfile
19202 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19203
19204 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19205 DWO file. */
19206 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19207 section = &cu->dwo_unit->dwo_file->sections.line;
19208 else if (cu->per_cu->is_dwz)
19209 {
ed2dc618 19210 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19211
19212 section = &dwz->line;
19213 }
19214 else
19215 section = &dwarf2_per_objfile->line;
19216
19217 return section;
19218}
19219
43988095
JK
19220/* Read directory or file name entry format, starting with byte of
19221 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19222 entries count and the entries themselves in the described entry
19223 format. */
19224
19225static void
ed2dc618
SM
19226read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19227 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19228 struct line_header *lh,
19229 const struct comp_unit_head *cu_header,
19230 void (*callback) (struct line_header *lh,
19231 const char *name,
ecfb656c 19232 dir_index d_index,
43988095
JK
19233 unsigned int mod_time,
19234 unsigned int length))
19235{
19236 gdb_byte format_count, formati;
19237 ULONGEST data_count, datai;
19238 const gdb_byte *buf = *bufp;
19239 const gdb_byte *format_header_data;
43988095
JK
19240 unsigned int bytes_read;
19241
19242 format_count = read_1_byte (abfd, buf);
19243 buf += 1;
19244 format_header_data = buf;
19245 for (formati = 0; formati < format_count; formati++)
19246 {
19247 read_unsigned_leb128 (abfd, buf, &bytes_read);
19248 buf += bytes_read;
19249 read_unsigned_leb128 (abfd, buf, &bytes_read);
19250 buf += bytes_read;
19251 }
19252
19253 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19254 buf += bytes_read;
19255 for (datai = 0; datai < data_count; datai++)
19256 {
19257 const gdb_byte *format = format_header_data;
19258 struct file_entry fe;
19259
43988095
JK
19260 for (formati = 0; formati < format_count; formati++)
19261 {
ecfb656c 19262 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19263 format += bytes_read;
43988095 19264
ecfb656c 19265 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19266 format += bytes_read;
ecfb656c
PA
19267
19268 gdb::optional<const char *> string;
19269 gdb::optional<unsigned int> uint;
19270
43988095
JK
19271 switch (form)
19272 {
19273 case DW_FORM_string:
ecfb656c 19274 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19275 buf += bytes_read;
19276 break;
19277
19278 case DW_FORM_line_strp:
86c0bb4c
TT
19279 string.emplace
19280 (dwarf2_per_objfile->read_line_string (buf,
19281 cu_header,
19282 &bytes_read));
43988095
JK
19283 buf += bytes_read;
19284 break;
19285
19286 case DW_FORM_data1:
ecfb656c 19287 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19288 buf += 1;
19289 break;
19290
19291 case DW_FORM_data2:
ecfb656c 19292 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19293 buf += 2;
19294 break;
19295
19296 case DW_FORM_data4:
ecfb656c 19297 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19298 buf += 4;
19299 break;
19300
19301 case DW_FORM_data8:
ecfb656c 19302 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19303 buf += 8;
19304 break;
19305
7ba99d21
AT
19306 case DW_FORM_data16:
19307 /* This is used for MD5, but file_entry does not record MD5s. */
19308 buf += 16;
19309 break;
19310
43988095 19311 case DW_FORM_udata:
ecfb656c 19312 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19313 buf += bytes_read;
19314 break;
19315
19316 case DW_FORM_block:
19317 /* It is valid only for DW_LNCT_timestamp which is ignored by
19318 current GDB. */
19319 break;
19320 }
ecfb656c
PA
19321
19322 switch (content_type)
19323 {
19324 case DW_LNCT_path:
19325 if (string.has_value ())
19326 fe.name = *string;
19327 break;
19328 case DW_LNCT_directory_index:
19329 if (uint.has_value ())
19330 fe.d_index = (dir_index) *uint;
19331 break;
19332 case DW_LNCT_timestamp:
19333 if (uint.has_value ())
19334 fe.mod_time = *uint;
19335 break;
19336 case DW_LNCT_size:
19337 if (uint.has_value ())
19338 fe.length = *uint;
19339 break;
19340 case DW_LNCT_MD5:
19341 break;
19342 default:
b98664d3 19343 complaint (_("Unknown format content type %s"),
ecfb656c
PA
19344 pulongest (content_type));
19345 }
43988095
JK
19346 }
19347
ecfb656c 19348 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
19349 }
19350
19351 *bufp = buf;
19352}
19353
debd256d 19354/* Read the statement program header starting at OFFSET in
3019eac3 19355 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19356 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19357 Returns NULL if there is a problem reading the header, e.g., if it
19358 has a version we don't understand.
debd256d
JB
19359
19360 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19361 the returned object point into the dwarf line section buffer,
19362 and must not be freed. */
ae2de4f8 19363
fff8551c 19364static line_header_up
9c541725 19365dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19366{
d521ce57 19367 const gdb_byte *line_ptr;
c764a876 19368 unsigned int bytes_read, offset_size;
debd256d 19369 int i;
d521ce57 19370 const char *cur_dir, *cur_file;
3019eac3
DE
19371 struct dwarf2_section_info *section;
19372 bfd *abfd;
518817b3
SM
19373 struct dwarf2_per_objfile *dwarf2_per_objfile
19374 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19375
36586728 19376 section = get_debug_line_section (cu);
96b79293 19377 section->read (dwarf2_per_objfile->objfile);
3019eac3 19378 if (section->buffer == NULL)
debd256d 19379 {
3019eac3 19380 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19381 complaint (_("missing .debug_line.dwo section"));
3019eac3 19382 else
b98664d3 19383 complaint (_("missing .debug_line section"));
debd256d
JB
19384 return 0;
19385 }
19386
fceca515
DE
19387 /* We can't do this until we know the section is non-empty.
19388 Only then do we know we have such a section. */
96b79293 19389 abfd = section->get_bfd_owner ();
fceca515 19390
a738430d
MK
19391 /* Make sure that at least there's room for the total_length field.
19392 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 19393 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 19394 {
4d3c2250 19395 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19396 return 0;
19397 }
19398
fff8551c 19399 line_header_up lh (new line_header ());
debd256d 19400
9c541725 19401 lh->sect_off = sect_off;
527f3840
JK
19402 lh->offset_in_dwz = cu->per_cu->is_dwz;
19403
9c541725 19404 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 19405
a738430d 19406 /* Read in the header. */
6e70227d 19407 lh->total_length =
c764a876
DE
19408 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19409 &bytes_read, &offset_size);
debd256d 19410 line_ptr += bytes_read;
7ba99d21
AT
19411
19412 const gdb_byte *start_here = line_ptr;
19413
3019eac3 19414 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 19415 {
4d3c2250 19416 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19417 return 0;
19418 }
7ba99d21 19419 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
19420 lh->version = read_2_bytes (abfd, line_ptr);
19421 line_ptr += 2;
43988095 19422 if (lh->version > 5)
cd366ee8
DE
19423 {
19424 /* This is a version we don't understand. The format could have
19425 changed in ways we don't handle properly so just punt. */
b98664d3 19426 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
19427 return NULL;
19428 }
43988095
JK
19429 if (lh->version >= 5)
19430 {
19431 gdb_byte segment_selector_size;
19432
19433 /* Skip address size. */
19434 read_1_byte (abfd, line_ptr);
19435 line_ptr += 1;
19436
19437 segment_selector_size = read_1_byte (abfd, line_ptr);
19438 line_ptr += 1;
19439 if (segment_selector_size != 0)
19440 {
b98664d3 19441 complaint (_("unsupported segment selector size %u "
43988095
JK
19442 "in .debug_line section"),
19443 segment_selector_size);
19444 return NULL;
19445 }
19446 }
24aa364d 19447 lh->header_length = read_offset (abfd, line_ptr, offset_size);
c764a876 19448 line_ptr += offset_size;
7ba99d21 19449 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
19450 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19451 line_ptr += 1;
2dc7f7b3
TT
19452 if (lh->version >= 4)
19453 {
19454 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19455 line_ptr += 1;
19456 }
19457 else
19458 lh->maximum_ops_per_instruction = 1;
19459
19460 if (lh->maximum_ops_per_instruction == 0)
19461 {
19462 lh->maximum_ops_per_instruction = 1;
b98664d3 19463 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 19464 "in `.debug_line' section"));
2dc7f7b3
TT
19465 }
19466
debd256d
JB
19467 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19468 line_ptr += 1;
19469 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19470 line_ptr += 1;
19471 lh->line_range = read_1_byte (abfd, line_ptr);
19472 line_ptr += 1;
19473 lh->opcode_base = read_1_byte (abfd, line_ptr);
19474 line_ptr += 1;
fff8551c 19475 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
19476
19477 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19478 for (i = 1; i < lh->opcode_base; ++i)
19479 {
19480 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19481 line_ptr += 1;
19482 }
19483
43988095 19484 if (lh->version >= 5)
debd256d 19485 {
43988095 19486 /* Read directory table. */
ed2dc618
SM
19487 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19488 &cu->header,
b926417a 19489 [] (struct line_header *header, const char *name,
ecfb656c 19490 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19491 unsigned int length)
19492 {
b926417a 19493 header->add_include_dir (name);
fff8551c 19494 });
debd256d 19495
43988095 19496 /* Read file name table. */
ed2dc618
SM
19497 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19498 &cu->header,
b926417a 19499 [] (struct line_header *header, const char *name,
ecfb656c 19500 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19501 unsigned int length)
19502 {
b926417a 19503 header->add_file_name (name, d_index, mod_time, length);
fff8551c 19504 });
43988095
JK
19505 }
19506 else
debd256d 19507 {
43988095
JK
19508 /* Read directory table. */
19509 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19510 {
19511 line_ptr += bytes_read;
fff8551c 19512 lh->add_include_dir (cur_dir);
43988095 19513 }
debd256d
JB
19514 line_ptr += bytes_read;
19515
43988095
JK
19516 /* Read file name table. */
19517 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19518 {
ecfb656c
PA
19519 unsigned int mod_time, length;
19520 dir_index d_index;
43988095
JK
19521
19522 line_ptr += bytes_read;
ecfb656c 19523 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
19524 line_ptr += bytes_read;
19525 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19526 line_ptr += bytes_read;
19527 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19528 line_ptr += bytes_read;
19529
ecfb656c 19530 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
19531 }
19532 line_ptr += bytes_read;
debd256d 19533 }
debd256d 19534
3019eac3 19535 if (line_ptr > (section->buffer + section->size))
b98664d3 19536 complaint (_("line number info header doesn't "
3e43a32a 19537 "fit in `.debug_line' section"));
debd256d 19538
debd256d
JB
19539 return lh;
19540}
c906108c 19541
c6da4cef 19542/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19543 Return the file name of the psymtab for the given file_entry.
c6da4cef 19544 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19545 If space for the result is malloc'd, *NAME_HOLDER will be set.
19546 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19547
d521ce57 19548static const char *
7ba99d21 19549psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19550 const dwarf2_psymtab *pst,
c89b44cd
TT
19551 const char *comp_dir,
19552 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19553{
d521ce57
TT
19554 const char *include_name = fe.name;
19555 const char *include_name_to_compare = include_name;
72b9f47f 19556 const char *pst_filename;
c6da4cef
DE
19557 int file_is_pst;
19558
8c43009f 19559 const char *dir_name = fe.include_dir (lh);
c6da4cef 19560
c89b44cd 19561 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19562 if (!IS_ABSOLUTE_PATH (include_name)
19563 && (dir_name != NULL || comp_dir != NULL))
19564 {
19565 /* Avoid creating a duplicate psymtab for PST.
19566 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19567 Before we do the comparison, however, we need to account
19568 for DIR_NAME and COMP_DIR.
19569 First prepend dir_name (if non-NULL). If we still don't
19570 have an absolute path prepend comp_dir (if non-NULL).
19571 However, the directory we record in the include-file's
19572 psymtab does not contain COMP_DIR (to match the
19573 corresponding symtab(s)).
19574
19575 Example:
19576
19577 bash$ cd /tmp
19578 bash$ gcc -g ./hello.c
19579 include_name = "hello.c"
19580 dir_name = "."
19581 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19582 DW_AT_name = "./hello.c"
19583
19584 */
c6da4cef
DE
19585
19586 if (dir_name != NULL)
19587 {
c89b44cd
TT
19588 name_holder->reset (concat (dir_name, SLASH_STRING,
19589 include_name, (char *) NULL));
19590 include_name = name_holder->get ();
c6da4cef 19591 include_name_to_compare = include_name;
c6da4cef
DE
19592 }
19593 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19594 {
c89b44cd
TT
19595 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19596 include_name, (char *) NULL));
19597 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19598 }
19599 }
19600
19601 pst_filename = pst->filename;
c89b44cd 19602 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19603 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19604 {
c89b44cd
TT
19605 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19606 pst_filename, (char *) NULL));
19607 pst_filename = copied_name.get ();
c6da4cef
DE
19608 }
19609
1e3fad37 19610 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19611
c6da4cef
DE
19612 if (file_is_pst)
19613 return NULL;
19614 return include_name;
19615}
19616
d9b3de22
DE
19617/* State machine to track the state of the line number program. */
19618
6f77053d 19619class lnp_state_machine
d9b3de22 19620{
6f77053d
PA
19621public:
19622 /* Initialize a machine state for the start of a line number
19623 program. */
804d2729
TT
19624 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19625 bool record_lines_p);
6f77053d 19626
8c43009f
PA
19627 file_entry *current_file ()
19628 {
19629 /* lh->file_names is 0-based, but the file name numbers in the
19630 statement program are 1-based. */
6f77053d
PA
19631 return m_line_header->file_name_at (m_file);
19632 }
19633
19634 /* Record the line in the state machine. END_SEQUENCE is true if
19635 we're processing the end of a sequence. */
19636 void record_line (bool end_sequence);
19637
7ab6656f
OJ
19638 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19639 nop-out rest of the lines in this sequence. */
6f77053d
PA
19640 void check_line_address (struct dwarf2_cu *cu,
19641 const gdb_byte *line_ptr,
7ab6656f 19642 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19643
19644 void handle_set_discriminator (unsigned int discriminator)
19645 {
19646 m_discriminator = discriminator;
19647 m_line_has_non_zero_discriminator |= discriminator != 0;
19648 }
19649
19650 /* Handle DW_LNE_set_address. */
19651 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19652 {
19653 m_op_index = 0;
19654 address += baseaddr;
19655 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19656 }
19657
19658 /* Handle DW_LNS_advance_pc. */
19659 void handle_advance_pc (CORE_ADDR adjust);
19660
19661 /* Handle a special opcode. */
19662 void handle_special_opcode (unsigned char op_code);
19663
19664 /* Handle DW_LNS_advance_line. */
19665 void handle_advance_line (int line_delta)
19666 {
19667 advance_line (line_delta);
19668 }
19669
19670 /* Handle DW_LNS_set_file. */
19671 void handle_set_file (file_name_index file);
19672
19673 /* Handle DW_LNS_negate_stmt. */
19674 void handle_negate_stmt ()
19675 {
19676 m_is_stmt = !m_is_stmt;
19677 }
19678
19679 /* Handle DW_LNS_const_add_pc. */
19680 void handle_const_add_pc ();
19681
19682 /* Handle DW_LNS_fixed_advance_pc. */
19683 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19684 {
19685 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19686 m_op_index = 0;
19687 }
19688
19689 /* Handle DW_LNS_copy. */
19690 void handle_copy ()
19691 {
19692 record_line (false);
19693 m_discriminator = 0;
19694 }
19695
19696 /* Handle DW_LNE_end_sequence. */
19697 void handle_end_sequence ()
19698 {
804d2729 19699 m_currently_recording_lines = true;
6f77053d
PA
19700 }
19701
19702private:
19703 /* Advance the line by LINE_DELTA. */
19704 void advance_line (int line_delta)
19705 {
19706 m_line += line_delta;
19707
19708 if (line_delta != 0)
19709 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19710 }
19711
804d2729
TT
19712 struct dwarf2_cu *m_cu;
19713
6f77053d
PA
19714 gdbarch *m_gdbarch;
19715
19716 /* True if we're recording lines.
19717 Otherwise we're building partial symtabs and are just interested in
19718 finding include files mentioned by the line number program. */
19719 bool m_record_lines_p;
19720
8c43009f 19721 /* The line number header. */
6f77053d 19722 line_header *m_line_header;
8c43009f 19723
6f77053d
PA
19724 /* These are part of the standard DWARF line number state machine,
19725 and initialized according to the DWARF spec. */
d9b3de22 19726
6f77053d 19727 unsigned char m_op_index = 0;
7ba99d21
AT
19728 /* The line table index of the current file. */
19729 file_name_index m_file = 1;
6f77053d
PA
19730 unsigned int m_line = 1;
19731
19732 /* These are initialized in the constructor. */
19733
19734 CORE_ADDR m_address;
19735 bool m_is_stmt;
19736 unsigned int m_discriminator;
d9b3de22
DE
19737
19738 /* Additional bits of state we need to track. */
19739
19740 /* The last file that we called dwarf2_start_subfile for.
19741 This is only used for TLLs. */
6f77053d 19742 unsigned int m_last_file = 0;
d9b3de22 19743 /* The last file a line number was recorded for. */
6f77053d 19744 struct subfile *m_last_subfile = NULL;
d9b3de22 19745
804d2729
TT
19746 /* When true, record the lines we decode. */
19747 bool m_currently_recording_lines = false;
d9b3de22
DE
19748
19749 /* The last line number that was recorded, used to coalesce
19750 consecutive entries for the same line. This can happen, for
19751 example, when discriminators are present. PR 17276. */
6f77053d
PA
19752 unsigned int m_last_line = 0;
19753 bool m_line_has_non_zero_discriminator = false;
8c43009f 19754};
d9b3de22 19755
6f77053d
PA
19756void
19757lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19758{
19759 CORE_ADDR addr_adj = (((m_op_index + adjust)
19760 / m_line_header->maximum_ops_per_instruction)
19761 * m_line_header->minimum_instruction_length);
19762 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19763 m_op_index = ((m_op_index + adjust)
19764 % m_line_header->maximum_ops_per_instruction);
19765}
d9b3de22 19766
6f77053d
PA
19767void
19768lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19769{
6f77053d 19770 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19771 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19772 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19773 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19774 / m_line_header->maximum_ops_per_instruction)
19775 * m_line_header->minimum_instruction_length);
19776 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19777 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19778 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19779
258bf0ee 19780 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19781 advance_line (line_delta);
19782 record_line (false);
19783 m_discriminator = 0;
19784}
d9b3de22 19785
6f77053d
PA
19786void
19787lnp_state_machine::handle_set_file (file_name_index file)
19788{
19789 m_file = file;
19790
19791 const file_entry *fe = current_file ();
19792 if (fe == NULL)
19793 dwarf2_debug_line_missing_file_complaint ();
19794 else if (m_record_lines_p)
19795 {
19796 const char *dir = fe->include_dir (m_line_header);
19797
c24bdb02 19798 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19799 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19800 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19801 }
19802}
19803
19804void
19805lnp_state_machine::handle_const_add_pc ()
19806{
19807 CORE_ADDR adjust
19808 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19809
19810 CORE_ADDR addr_adj
19811 = (((m_op_index + adjust)
19812 / m_line_header->maximum_ops_per_instruction)
19813 * m_line_header->minimum_instruction_length);
19814
19815 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19816 m_op_index = ((m_op_index + adjust)
19817 % m_line_header->maximum_ops_per_instruction);
19818}
d9b3de22 19819
a05a36a5
DE
19820/* Return non-zero if we should add LINE to the line number table.
19821 LINE is the line to add, LAST_LINE is the last line that was added,
19822 LAST_SUBFILE is the subfile for LAST_LINE.
19823 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19824 had a non-zero discriminator.
19825
19826 We have to be careful in the presence of discriminators.
19827 E.g., for this line:
19828
19829 for (i = 0; i < 100000; i++);
19830
19831 clang can emit four line number entries for that one line,
19832 each with a different discriminator.
19833 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19834
19835 However, we want gdb to coalesce all four entries into one.
19836 Otherwise the user could stepi into the middle of the line and
19837 gdb would get confused about whether the pc really was in the
19838 middle of the line.
19839
19840 Things are further complicated by the fact that two consecutive
19841 line number entries for the same line is a heuristic used by gcc
19842 to denote the end of the prologue. So we can't just discard duplicate
19843 entries, we have to be selective about it. The heuristic we use is
19844 that we only collapse consecutive entries for the same line if at least
19845 one of those entries has a non-zero discriminator. PR 17276.
19846
19847 Note: Addresses in the line number state machine can never go backwards
19848 within one sequence, thus this coalescing is ok. */
19849
19850static int
804d2729
TT
19851dwarf_record_line_p (struct dwarf2_cu *cu,
19852 unsigned int line, unsigned int last_line,
a05a36a5
DE
19853 int line_has_non_zero_discriminator,
19854 struct subfile *last_subfile)
19855{
c24bdb02 19856 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19857 return 1;
19858 if (line != last_line)
19859 return 1;
19860 /* Same line for the same file that we've seen already.
19861 As a last check, for pr 17276, only record the line if the line
19862 has never had a non-zero discriminator. */
19863 if (!line_has_non_zero_discriminator)
19864 return 1;
19865 return 0;
19866}
19867
804d2729
TT
19868/* Use the CU's builder to record line number LINE beginning at
19869 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19870
19871static void
d9b3de22 19872dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 19873 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 19874 struct dwarf2_cu *cu)
252a6764
DE
19875{
19876 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19877
27e0867f
DE
19878 if (dwarf_line_debug)
19879 {
19880 fprintf_unfiltered (gdb_stdlog,
19881 "Recording line %u, file %s, address %s\n",
19882 line, lbasename (subfile->name),
19883 paddress (gdbarch, address));
19884 }
19885
804d2729 19886 if (cu != nullptr)
8c95582d 19887 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
19888}
19889
19890/* Subroutine of dwarf_decode_lines_1 to simplify it.
19891 Mark the end of a set of line number records.
d9b3de22 19892 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19893 If SUBFILE is NULL the request is ignored. */
19894
19895static void
19896dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19897 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19898{
27e0867f
DE
19899 if (subfile == NULL)
19900 return;
19901
19902 if (dwarf_line_debug)
19903 {
19904 fprintf_unfiltered (gdb_stdlog,
19905 "Finishing current line, file %s, address %s\n",
19906 lbasename (subfile->name),
19907 paddress (gdbarch, address));
19908 }
19909
8c95582d 19910 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
19911}
19912
6f77053d
PA
19913void
19914lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19915{
d9b3de22
DE
19916 if (dwarf_line_debug)
19917 {
19918 fprintf_unfiltered (gdb_stdlog,
19919 "Processing actual line %u: file %u,"
94a72be7 19920 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19921 m_line, m_file,
6f77053d 19922 paddress (m_gdbarch, m_address),
94a72be7
AB
19923 m_is_stmt, m_discriminator,
19924 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19925 }
19926
6f77053d 19927 file_entry *fe = current_file ();
8c43009f
PA
19928
19929 if (fe == NULL)
d9b3de22
DE
19930 dwarf2_debug_line_missing_file_complaint ();
19931 /* For now we ignore lines not starting on an instruction boundary.
19932 But not when processing end_sequence for compatibility with the
19933 previous version of the code. */
6f77053d 19934 else if (m_op_index == 0 || end_sequence)
d9b3de22 19935 {
8c43009f 19936 fe->included_p = 1;
8c95582d 19937 if (m_record_lines_p)
d9b3de22 19938 {
c24bdb02 19939 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 19940 || end_sequence)
d9b3de22 19941 {
804d2729
TT
19942 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19943 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
19944 }
19945
19946 if (!end_sequence)
19947 {
8c95582d
AB
19948 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19949
804d2729 19950 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
19951 m_line_has_non_zero_discriminator,
19952 m_last_subfile))
d9b3de22 19953 {
c24bdb02 19954 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 19955 dwarf_record_line_1 (m_gdbarch,
c24bdb02 19956 builder->get_current_subfile (),
8c95582d 19957 m_line, m_address, is_stmt,
804d2729 19958 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 19959 }
c24bdb02 19960 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19961 m_last_line = m_line;
d9b3de22
DE
19962 }
19963 }
19964 }
19965}
19966
804d2729
TT
19967lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19968 line_header *lh, bool record_lines_p)
d9b3de22 19969{
804d2729 19970 m_cu = cu;
6f77053d
PA
19971 m_gdbarch = arch;
19972 m_record_lines_p = record_lines_p;
19973 m_line_header = lh;
d9b3de22 19974
804d2729 19975 m_currently_recording_lines = true;
d9b3de22 19976
d9b3de22
DE
19977 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19978 was a line entry for it so that the backend has a chance to adjust it
19979 and also record it in case it needs it. This is currently used by MIPS
19980 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19981 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19982 m_is_stmt = lh->default_is_stmt;
19983 m_discriminator = 0;
252a6764
DE
19984}
19985
6f77053d
PA
19986void
19987lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19988 const gdb_byte *line_ptr,
7ab6656f 19989 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 19990{
7ab6656f
OJ
19991 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19992 the pc range of the CU. However, we restrict the test to only ADDRESS
19993 values of zero to preserve GDB's previous behaviour which is to handle
19994 the specific case of a function being GC'd by the linker. */
924c2928 19995
7ab6656f 19996 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
19997 {
19998 /* This line table is for a function which has been
19999 GCd by the linker. Ignore it. PR gdb/12528 */
20000
518817b3 20001 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20002 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20003
b98664d3 20004 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20005 line_offset, objfile_name (objfile));
804d2729
TT
20006 m_currently_recording_lines = false;
20007 /* Note: m_currently_recording_lines is left as false until we see
20008 DW_LNE_end_sequence. */
924c2928
DE
20009 }
20010}
20011
f3f5162e 20012/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20013 Process the line number information in LH.
20014 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20015 program in order to set included_p for every referenced header. */
debd256d 20016
c906108c 20017static void
43f3e411
DE
20018dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20019 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20020{
d521ce57
TT
20021 const gdb_byte *line_ptr, *extended_end;
20022 const gdb_byte *line_end;
a8c50c1f 20023 unsigned int bytes_read, extended_len;
699ca60a 20024 unsigned char op_code, extended_op;
e142c38c 20025 CORE_ADDR baseaddr;
518817b3 20026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20027 bfd *abfd = objfile->obfd;
fbf65064 20028 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20029 /* True if we're recording line info (as opposed to building partial
20030 symtabs and just interested in finding include files mentioned by
20031 the line number program). */
20032 bool record_lines_p = !decode_for_pst_p;
e142c38c 20033
b3b3bada 20034 baseaddr = objfile->text_section_offset ();
c906108c 20035
debd256d
JB
20036 line_ptr = lh->statement_program_start;
20037 line_end = lh->statement_program_end;
c906108c
SS
20038
20039 /* Read the statement sequences until there's nothing left. */
20040 while (line_ptr < line_end)
20041 {
6f77053d
PA
20042 /* The DWARF line number program state machine. Reset the state
20043 machine at the start of each sequence. */
804d2729 20044 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20045 bool end_sequence = false;
d9b3de22 20046
8c43009f 20047 if (record_lines_p)
c906108c 20048 {
8c43009f
PA
20049 /* Start a subfile for the current file of the state
20050 machine. */
20051 const file_entry *fe = state_machine.current_file ();
20052
20053 if (fe != NULL)
804d2729 20054 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20055 }
20056
a738430d 20057 /* Decode the table. */
d9b3de22 20058 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20059 {
20060 op_code = read_1_byte (abfd, line_ptr);
20061 line_ptr += 1;
9aa1fe7e 20062
debd256d 20063 if (op_code >= lh->opcode_base)
6e70227d 20064 {
8e07a239 20065 /* Special opcode. */
6f77053d 20066 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20067 }
20068 else switch (op_code)
c906108c
SS
20069 {
20070 case DW_LNS_extended_op:
3e43a32a
MS
20071 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20072 &bytes_read);
473b7be6 20073 line_ptr += bytes_read;
a8c50c1f 20074 extended_end = line_ptr + extended_len;
c906108c
SS
20075 extended_op = read_1_byte (abfd, line_ptr);
20076 line_ptr += 1;
20077 switch (extended_op)
20078 {
20079 case DW_LNE_end_sequence:
6f77053d
PA
20080 state_machine.handle_end_sequence ();
20081 end_sequence = true;
c906108c
SS
20082 break;
20083 case DW_LNE_set_address:
d9b3de22
DE
20084 {
20085 CORE_ADDR address
c8a7a66f 20086 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20087 line_ptr += bytes_read;
6f77053d
PA
20088
20089 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20090 lowpc - baseaddr, address);
6f77053d 20091 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20092 }
c906108c
SS
20093 break;
20094 case DW_LNE_define_file:
debd256d 20095 {
d521ce57 20096 const char *cur_file;
ecfb656c
PA
20097 unsigned int mod_time, length;
20098 dir_index dindex;
6e70227d 20099
3e43a32a
MS
20100 cur_file = read_direct_string (abfd, line_ptr,
20101 &bytes_read);
debd256d 20102 line_ptr += bytes_read;
ecfb656c 20103 dindex = (dir_index)
debd256d
JB
20104 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20105 line_ptr += bytes_read;
20106 mod_time =
20107 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20108 line_ptr += bytes_read;
20109 length =
20110 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20111 line_ptr += bytes_read;
ecfb656c 20112 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20113 }
c906108c 20114 break;
d0c6ba3d 20115 case DW_LNE_set_discriminator:
6f77053d
PA
20116 {
20117 /* The discriminator is not interesting to the
20118 debugger; just ignore it. We still need to
20119 check its value though:
20120 if there are consecutive entries for the same
20121 (non-prologue) line we want to coalesce them.
20122 PR 17276. */
20123 unsigned int discr
20124 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20125 line_ptr += bytes_read;
20126
20127 state_machine.handle_set_discriminator (discr);
20128 }
d0c6ba3d 20129 break;
c906108c 20130 default:
b98664d3 20131 complaint (_("mangled .debug_line section"));
debd256d 20132 return;
c906108c 20133 }
a8c50c1f
DJ
20134 /* Make sure that we parsed the extended op correctly. If e.g.
20135 we expected a different address size than the producer used,
20136 we may have read the wrong number of bytes. */
20137 if (line_ptr != extended_end)
20138 {
b98664d3 20139 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20140 return;
20141 }
c906108c
SS
20142 break;
20143 case DW_LNS_copy:
6f77053d 20144 state_machine.handle_copy ();
c906108c
SS
20145 break;
20146 case DW_LNS_advance_pc:
2dc7f7b3
TT
20147 {
20148 CORE_ADDR adjust
20149 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20150 line_ptr += bytes_read;
6f77053d
PA
20151
20152 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20153 }
c906108c
SS
20154 break;
20155 case DW_LNS_advance_line:
a05a36a5
DE
20156 {
20157 int line_delta
20158 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20159 line_ptr += bytes_read;
6f77053d
PA
20160
20161 state_machine.handle_advance_line (line_delta);
a05a36a5 20162 }
c906108c
SS
20163 break;
20164 case DW_LNS_set_file:
d9b3de22 20165 {
6f77053d 20166 file_name_index file
ecfb656c
PA
20167 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20168 &bytes_read);
d9b3de22 20169 line_ptr += bytes_read;
8c43009f 20170
6f77053d 20171 state_machine.handle_set_file (file);
d9b3de22 20172 }
c906108c
SS
20173 break;
20174 case DW_LNS_set_column:
0ad93d4f 20175 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20176 line_ptr += bytes_read;
20177 break;
20178 case DW_LNS_negate_stmt:
6f77053d 20179 state_machine.handle_negate_stmt ();
c906108c
SS
20180 break;
20181 case DW_LNS_set_basic_block:
c906108c 20182 break;
c2c6d25f
JM
20183 /* Add to the address register of the state machine the
20184 address increment value corresponding to special opcode
a738430d
MK
20185 255. I.e., this value is scaled by the minimum
20186 instruction length since special opcode 255 would have
b021a221 20187 scaled the increment. */
c906108c 20188 case DW_LNS_const_add_pc:
6f77053d 20189 state_machine.handle_const_add_pc ();
c906108c
SS
20190 break;
20191 case DW_LNS_fixed_advance_pc:
3e29f34a 20192 {
6f77053d 20193 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20194 line_ptr += 2;
6f77053d
PA
20195
20196 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20197 }
c906108c 20198 break;
9aa1fe7e 20199 default:
a738430d
MK
20200 {
20201 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20202 int i;
a738430d 20203
debd256d 20204 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20205 {
20206 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20207 line_ptr += bytes_read;
20208 }
20209 }
c906108c
SS
20210 }
20211 }
d9b3de22
DE
20212
20213 if (!end_sequence)
20214 dwarf2_debug_line_missing_end_sequence_complaint ();
20215
20216 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20217 in which case we still finish recording the last line). */
6f77053d 20218 state_machine.record_line (true);
c906108c 20219 }
f3f5162e
DE
20220}
20221
20222/* Decode the Line Number Program (LNP) for the given line_header
20223 structure and CU. The actual information extracted and the type
20224 of structures created from the LNP depends on the value of PST.
20225
20226 1. If PST is NULL, then this procedure uses the data from the program
20227 to create all necessary symbol tables, and their linetables.
20228
20229 2. If PST is not NULL, this procedure reads the program to determine
20230 the list of files included by the unit represented by PST, and
20231 builds all the associated partial symbol tables.
20232
20233 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20234 It is used for relative paths in the line table.
20235 NOTE: When processing partial symtabs (pst != NULL),
20236 comp_dir == pst->dirname.
20237
20238 NOTE: It is important that psymtabs have the same file name (via strcmp)
20239 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20240 symtab we don't use it in the name of the psymtabs we create.
20241 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20242 A good testcase for this is mb-inline.exp.
20243
527f3840
JK
20244 LOWPC is the lowest address in CU (or 0 if not known).
20245
20246 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20247 for its PC<->lines mapping information. Otherwise only the filename
20248 table is read in. */
f3f5162e
DE
20249
20250static void
20251dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20252 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20253 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20254{
518817b3 20255 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20256 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20257
527f3840
JK
20258 if (decode_mapping)
20259 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20260
20261 if (decode_for_pst_p)
20262 {
aaa75496
JB
20263 /* Now that we're done scanning the Line Header Program, we can
20264 create the psymtab of each included file. */
7ba99d21
AT
20265 for (auto &file_entry : lh->file_names ())
20266 if (file_entry.included_p == 1)
aaa75496 20267 {
c89b44cd 20268 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20269 const char *include_name =
7ba99d21
AT
20270 psymtab_include_file_name (lh, file_entry, pst,
20271 comp_dir, &name_holder);
c6da4cef 20272 if (include_name != NULL)
aaa75496
JB
20273 dwarf2_create_include_psymtab (include_name, pst, objfile);
20274 }
20275 }
cb1df416
DJ
20276 else
20277 {
20278 /* Make sure a symtab is created for every file, even files
20279 which contain only variables (i.e. no code with associated
20280 line numbers). */
c24bdb02
KS
20281 buildsym_compunit *builder = cu->get_builder ();
20282 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20283
7ba99d21 20284 for (auto &fe : lh->file_names ())
cb1df416 20285 {
804d2729 20286 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20287 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20288 {
c24bdb02 20289 builder->get_current_subfile ()->symtab
804d2729 20290 = allocate_symtab (cust,
c24bdb02 20291 builder->get_current_subfile ()->name);
43f3e411 20292 }
c24bdb02 20293 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20294 }
20295 }
c906108c
SS
20296}
20297
20298/* Start a subfile for DWARF. FILENAME is the name of the file and
20299 DIRNAME the name of the source directory which contains FILENAME
4d663531 20300 or NULL if not known.
c906108c
SS
20301 This routine tries to keep line numbers from identical absolute and
20302 relative file names in a common subfile.
20303
20304 Using the `list' example from the GDB testsuite, which resides in
20305 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20306 of /srcdir/list0.c yields the following debugging information for list0.c:
20307
c5aa993b 20308 DW_AT_name: /srcdir/list0.c
4d663531 20309 DW_AT_comp_dir: /compdir
357e46e7 20310 files.files[0].name: list0.h
c5aa993b 20311 files.files[0].dir: /srcdir
357e46e7 20312 files.files[1].name: list0.c
c5aa993b 20313 files.files[1].dir: /srcdir
c906108c
SS
20314
20315 The line number information for list0.c has to end up in a single
4f1520fb
FR
20316 subfile, so that `break /srcdir/list0.c:1' works as expected.
20317 start_subfile will ensure that this happens provided that we pass the
20318 concatenation of files.files[1].dir and files.files[1].name as the
20319 subfile's name. */
c906108c
SS
20320
20321static void
804d2729
TT
20322dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20323 const char *dirname)
c906108c 20324{
43816ebc 20325 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20326
4d663531 20327 /* In order not to lose the line information directory,
4f1520fb
FR
20328 we concatenate it to the filename when it makes sense.
20329 Note that the Dwarf3 standard says (speaking of filenames in line
20330 information): ``The directory index is ignored for file names
20331 that represent full path names''. Thus ignoring dirname in the
20332 `else' branch below isn't an issue. */
c906108c 20333
d5166ae1 20334 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20335 {
43816ebc
TT
20336 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20337 filename = copy.get ();
d521ce57 20338 }
c906108c 20339
c24bdb02 20340 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20341}
20342
804d2729
TT
20343/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20344 buildsym_compunit constructor. */
f4dc4d17 20345
c24bdb02
KS
20346struct compunit_symtab *
20347dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20348 CORE_ADDR low_pc)
f4dc4d17 20349{
c24bdb02 20350 gdb_assert (m_builder == nullptr);
43f3e411 20351
c24bdb02
KS
20352 m_builder.reset (new struct buildsym_compunit
20353 (per_cu->dwarf2_per_objfile->objfile,
20354 name, comp_dir, language, low_pc));
93b8bea4 20355
c24bdb02 20356 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20357
c24bdb02
KS
20358 get_builder ()->record_debugformat ("DWARF 2");
20359 get_builder ()->record_producer (producer);
f4dc4d17 20360
c24bdb02 20361 processing_has_namespace_info = false;
43f3e411 20362
c24bdb02 20363 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20364}
20365
4c2df51b
DJ
20366static void
20367var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20368 struct dwarf2_cu *cu)
4c2df51b 20369{
518817b3 20370 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20371 struct comp_unit_head *cu_header = &cu->header;
20372
4c2df51b
DJ
20373 /* NOTE drow/2003-01-30: There used to be a comment and some special
20374 code here to turn a symbol with DW_AT_external and a
20375 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20376 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20377 with some versions of binutils) where shared libraries could have
20378 relocations against symbols in their debug information - the
20379 minimal symbol would have the right address, but the debug info
20380 would not. It's no longer necessary, because we will explicitly
20381 apply relocations when we read in the debug information now. */
20382
20383 /* A DW_AT_location attribute with no contents indicates that a
20384 variable has been optimized away. */
4fc6c0d5 20385 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20386 {
f1e6e072 20387 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20388 return;
20389 }
20390
20391 /* Handle one degenerate form of location expression specially, to
20392 preserve GDB's previous behavior when section offsets are
336d760d
AT
20393 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20394 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20395
4fc6c0d5 20396 if (attr->form_is_block ()
3019eac3
DE
20397 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20398 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20399 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20400 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20401 && (DW_BLOCK (attr)->size
20402 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20403 {
891d2f0b 20404 unsigned int dummy;
4c2df51b 20405
3019eac3 20406 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20407 SET_SYMBOL_VALUE_ADDRESS
20408 (sym, cu->header.read_address (objfile->obfd,
20409 DW_BLOCK (attr)->data + 1,
20410 &dummy));
3019eac3 20411 else
38583298
TT
20412 SET_SYMBOL_VALUE_ADDRESS
20413 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20414 &dummy));
f1e6e072 20415 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20416 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20417 SET_SYMBOL_VALUE_ADDRESS
20418 (sym,
20419 SYMBOL_VALUE_ADDRESS (sym)
20420 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20421 return;
20422 }
20423
20424 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20425 expression evaluator, and use LOC_COMPUTED only when necessary
20426 (i.e. when the value of a register or memory location is
20427 referenced, or a thread-local block, etc.). Then again, it might
20428 not be worthwhile. I'm assuming that it isn't unless performance
20429 or memory numbers show me otherwise. */
20430
f1e6e072 20431 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20432
f1e6e072 20433 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20434 cu->has_loclist = true;
4c2df51b
DJ
20435}
20436
c906108c
SS
20437/* Given a pointer to a DWARF information entry, figure out if we need
20438 to make a symbol table entry for it, and if so, create a new entry
20439 and return a pointer to it.
20440 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20441 used the passed type.
20442 If SPACE is not NULL, use it to hold the new symbol. If it is
20443 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20444
20445static struct symbol *
5e2db402
TT
20446new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20447 struct symbol *space)
c906108c 20448{
518817b3
SM
20449 struct dwarf2_per_objfile *dwarf2_per_objfile
20450 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20451 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20452 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20453 struct symbol *sym = NULL;
15d034d0 20454 const char *name;
c906108c
SS
20455 struct attribute *attr = NULL;
20456 struct attribute *attr2 = NULL;
e142c38c 20457 CORE_ADDR baseaddr;
e37fd15a
SW
20458 struct pending **list_to_add = NULL;
20459
edb3359d 20460 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20461
b3b3bada 20462 baseaddr = objfile->text_section_offset ();
c906108c 20463
94af9270 20464 name = dwarf2_name (die, cu);
c906108c
SS
20465 if (name)
20466 {
94af9270 20467 const char *linkagename;
34eaf542 20468 int suppress_add = 0;
94af9270 20469
34eaf542
TT
20470 if (space)
20471 sym = space;
20472 else
e623cf5d 20473 sym = allocate_symbol (objfile);
c906108c 20474 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20475
20476 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20477 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20478 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20479 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20480
f55ee35c
JK
20481 /* Fortran does not have mangling standard and the mangling does differ
20482 between gfortran, iFort etc. */
20483 if (cu->language == language_fortran
468c0cbb
CB
20484 && symbol_get_demangled_name (sym) == NULL)
20485 symbol_set_demangled_name (sym,
cfc594ee 20486 dwarf2_full_name (name, die, cu),
29df156d 20487 NULL);
f55ee35c 20488
c906108c 20489 /* Default assumptions.
c5aa993b 20490 Use the passed type or decode it from the die. */
176620f1 20491 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20492 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20493 if (type != NULL)
20494 SYMBOL_TYPE (sym) = type;
20495 else
e7c27a73 20496 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20497 attr = dwarf2_attr (die,
20498 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20499 cu);
435d3d88 20500 if (attr != nullptr)
c906108c
SS
20501 {
20502 SYMBOL_LINE (sym) = DW_UNSND (attr);
20503 }
cb1df416 20504
edb3359d
DJ
20505 attr = dwarf2_attr (die,
20506 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20507 cu);
435d3d88 20508 if (attr != nullptr)
cb1df416 20509 {
ecfb656c 20510 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20511 struct file_entry *fe;
9a619af0 20512
ecfb656c
PA
20513 if (cu->line_header != NULL)
20514 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20515 else
20516 fe = NULL;
20517
20518 if (fe == NULL)
b98664d3 20519 complaint (_("file index out of range"));
8c43009f
PA
20520 else
20521 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20522 }
20523
c906108c
SS
20524 switch (die->tag)
20525 {
20526 case DW_TAG_label:
e142c38c 20527 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20528 if (attr != nullptr)
3e29f34a
MR
20529 {
20530 CORE_ADDR addr;
20531
cd6c91b4 20532 addr = attr->value_as_address ();
3e29f34a 20533 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20534 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20535 }
0f5238ed
TT
20536 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20537 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20538 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20539 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20540 break;
20541 case DW_TAG_subprogram:
20542 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20543 finish_block. */
f1e6e072 20544 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20545 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20546 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20547 || cu->language == language_ada
20548 || cu->language == language_fortran)
c906108c 20549 {
2cfa0c8d 20550 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20551 Ada and Fortran subprograms, whether marked external or
20552 not, are always stored as a global symbol, because we want
20553 to be able to access them globally. For instance, we want
20554 to be able to break on a nested subprogram without having
20555 to specify the context. */
c24bdb02 20556 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20557 }
20558 else
20559 {
e37fd15a 20560 list_to_add = cu->list_in_scope;
c906108c
SS
20561 }
20562 break;
edb3359d
DJ
20563 case DW_TAG_inlined_subroutine:
20564 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20565 finish_block. */
f1e6e072 20566 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20567 SYMBOL_INLINED (sym) = 1;
481860b3 20568 list_to_add = cu->list_in_scope;
edb3359d 20569 break;
34eaf542
TT
20570 case DW_TAG_template_value_param:
20571 suppress_add = 1;
20572 /* Fall through. */
72929c62 20573 case DW_TAG_constant:
c906108c 20574 case DW_TAG_variable:
254e6b9e 20575 case DW_TAG_member:
0963b4bd
MS
20576 /* Compilation with minimal debug info may result in
20577 variables with missing type entries. Change the
20578 misleading `void' type to something sensible. */
c906108c 20579 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20580 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20581
e142c38c 20582 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20583 /* In the case of DW_TAG_member, we should only be called for
20584 static const members. */
20585 if (die->tag == DW_TAG_member)
20586 {
3863f96c
DE
20587 /* dwarf2_add_field uses die_is_declaration,
20588 so we do the same. */
254e6b9e
DE
20589 gdb_assert (die_is_declaration (die, cu));
20590 gdb_assert (attr);
20591 }
435d3d88 20592 if (attr != nullptr)
c906108c 20593 {
e7c27a73 20594 dwarf2_const_value (attr, sym, cu);
e142c38c 20595 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20596 if (!suppress_add)
34eaf542
TT
20597 {
20598 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20599 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20600 else
e37fd15a 20601 list_to_add = cu->list_in_scope;
34eaf542 20602 }
c906108c
SS
20603 break;
20604 }
e142c38c 20605 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20606 if (attr != nullptr)
c906108c 20607 {
e7c27a73 20608 var_decode_location (attr, sym, cu);
e142c38c 20609 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20610
20611 /* Fortran explicitly imports any global symbols to the local
20612 scope by DW_TAG_common_block. */
20613 if (cu->language == language_fortran && die->parent
20614 && die->parent->tag == DW_TAG_common_block)
20615 attr2 = NULL;
20616
caac4577
JG
20617 if (SYMBOL_CLASS (sym) == LOC_STATIC
20618 && SYMBOL_VALUE_ADDRESS (sym) == 0
20619 && !dwarf2_per_objfile->has_section_at_zero)
20620 {
20621 /* When a static variable is eliminated by the linker,
20622 the corresponding debug information is not stripped
20623 out, but the variable address is set to null;
20624 do not add such variables into symbol table. */
20625 }
20626 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20627 {
4b610737
TT
20628 if (SYMBOL_CLASS (sym) == LOC_STATIC
20629 && (objfile->flags & OBJF_MAINLINE) == 0
20630 && dwarf2_per_objfile->can_copy)
20631 {
20632 /* A global static variable might be subject to
20633 copy relocation. We first check for a local
20634 minsym, though, because maybe the symbol was
20635 marked hidden, in which case this would not
20636 apply. */
20637 bound_minimal_symbol found
20638 = (lookup_minimal_symbol_linkage
987012b8 20639 (sym->linkage_name (), objfile));
4b610737
TT
20640 if (found.minsym != nullptr)
20641 sym->maybe_copied = 1;
20642 }
f55ee35c 20643
1c809c68
TT
20644 /* A variable with DW_AT_external is never static,
20645 but it may be block-scoped. */
804d2729 20646 list_to_add
c24bdb02
KS
20647 = ((cu->list_in_scope
20648 == cu->get_builder ()->get_file_symbols ())
20649 ? cu->get_builder ()->get_global_symbols ()
804d2729 20650 : cu->list_in_scope);
1c809c68 20651 }
c906108c 20652 else
e37fd15a 20653 list_to_add = cu->list_in_scope;
c906108c
SS
20654 }
20655 else
20656 {
20657 /* We do not know the address of this symbol.
c5aa993b
JM
20658 If it is an external symbol and we have type information
20659 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20660 The address of the variable will then be determined from
20661 the minimal symbol table whenever the variable is
20662 referenced. */
e142c38c 20663 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20664
20665 /* Fortran explicitly imports any global symbols to the local
20666 scope by DW_TAG_common_block. */
20667 if (cu->language == language_fortran && die->parent
20668 && die->parent->tag == DW_TAG_common_block)
20669 {
20670 /* SYMBOL_CLASS doesn't matter here because
20671 read_common_block is going to reset it. */
20672 if (!suppress_add)
20673 list_to_add = cu->list_in_scope;
20674 }
20675 else if (attr2 && (DW_UNSND (attr2) != 0)
20676 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20677 {
0fe7935b
DJ
20678 /* A variable with DW_AT_external is never static, but it
20679 may be block-scoped. */
804d2729 20680 list_to_add
c24bdb02
KS
20681 = ((cu->list_in_scope
20682 == cu->get_builder ()->get_file_symbols ())
20683 ? cu->get_builder ()->get_global_symbols ()
804d2729 20684 : cu->list_in_scope);
0fe7935b 20685
f1e6e072 20686 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20687 }
442ddf59
JK
20688 else if (!die_is_declaration (die, cu))
20689 {
20690 /* Use the default LOC_OPTIMIZED_OUT class. */
20691 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20692 if (!suppress_add)
20693 list_to_add = cu->list_in_scope;
442ddf59 20694 }
c906108c
SS
20695 }
20696 break;
20697 case DW_TAG_formal_parameter:
a60f3166
TT
20698 {
20699 /* If we are inside a function, mark this as an argument. If
20700 not, we might be looking at an argument to an inlined function
20701 when we do not have enough information to show inlined frames;
20702 pretend it's a local variable in that case so that the user can
20703 still see it. */
804d2729 20704 struct context_stack *curr
c24bdb02 20705 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20706 if (curr != nullptr && curr->name != nullptr)
20707 SYMBOL_IS_ARGUMENT (sym) = 1;
20708 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20709 if (attr != nullptr)
a60f3166
TT
20710 {
20711 var_decode_location (attr, sym, cu);
20712 }
20713 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20714 if (attr != nullptr)
a60f3166
TT
20715 {
20716 dwarf2_const_value (attr, sym, cu);
20717 }
f346a30d 20718
a60f3166
TT
20719 list_to_add = cu->list_in_scope;
20720 }
c906108c
SS
20721 break;
20722 case DW_TAG_unspecified_parameters:
20723 /* From varargs functions; gdb doesn't seem to have any
20724 interest in this information, so just ignore it for now.
20725 (FIXME?) */
20726 break;
34eaf542
TT
20727 case DW_TAG_template_type_param:
20728 suppress_add = 1;
20729 /* Fall through. */
c906108c 20730 case DW_TAG_class_type:
680b30c7 20731 case DW_TAG_interface_type:
c906108c
SS
20732 case DW_TAG_structure_type:
20733 case DW_TAG_union_type:
72019c9c 20734 case DW_TAG_set_type:
c906108c 20735 case DW_TAG_enumeration_type:
f1e6e072 20736 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20737 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20738
63d06c5c 20739 {
9c37b5ae 20740 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20741 really ever be static objects: otherwise, if you try
20742 to, say, break of a class's method and you're in a file
20743 which doesn't mention that class, it won't work unless
20744 the check for all static symbols in lookup_symbol_aux
20745 saves you. See the OtherFileClass tests in
20746 gdb.c++/namespace.exp. */
20747
e37fd15a 20748 if (!suppress_add)
34eaf542 20749 {
c24bdb02 20750 buildsym_compunit *builder = cu->get_builder ();
804d2729 20751 list_to_add
c24bdb02 20752 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20753 && cu->language == language_cplus
c24bdb02 20754 ? builder->get_global_symbols ()
804d2729 20755 : cu->list_in_scope);
63d06c5c 20756
64382290 20757 /* The semantics of C++ state that "struct foo {
9c37b5ae 20758 ... }" also defines a typedef for "foo". */
64382290 20759 if (cu->language == language_cplus
45280282 20760 || cu->language == language_ada
c44af4eb
TT
20761 || cu->language == language_d
20762 || cu->language == language_rust)
64382290
TT
20763 {
20764 /* The symbol's name is already allocated along
20765 with this objfile, so we don't need to
20766 duplicate it for the type. */
20767 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20768 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20769 }
63d06c5c
DC
20770 }
20771 }
c906108c
SS
20772 break;
20773 case DW_TAG_typedef:
f1e6e072 20774 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20775 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20776 list_to_add = cu->list_in_scope;
63d06c5c 20777 break;
c906108c 20778 case DW_TAG_base_type:
a02abb62 20779 case DW_TAG_subrange_type:
f1e6e072 20780 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20781 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20782 list_to_add = cu->list_in_scope;
c906108c
SS
20783 break;
20784 case DW_TAG_enumerator:
e142c38c 20785 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20786 if (attr != nullptr)
c906108c 20787 {
e7c27a73 20788 dwarf2_const_value (attr, sym, cu);
c906108c 20789 }
63d06c5c
DC
20790 {
20791 /* NOTE: carlton/2003-11-10: See comment above in the
20792 DW_TAG_class_type, etc. block. */
20793
804d2729 20794 list_to_add
c24bdb02 20795 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20796 && cu->language == language_cplus
c24bdb02 20797 ? cu->get_builder ()->get_global_symbols ()
804d2729 20798 : cu->list_in_scope);
63d06c5c 20799 }
c906108c 20800 break;
74921315 20801 case DW_TAG_imported_declaration:
5c4e30ca 20802 case DW_TAG_namespace:
f1e6e072 20803 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20804 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20805 break;
530e8392
KB
20806 case DW_TAG_module:
20807 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20808 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20809 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20810 break;
4357ac6c 20811 case DW_TAG_common_block:
f1e6e072 20812 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20813 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20814 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20815 break;
c906108c
SS
20816 default:
20817 /* Not a tag we recognize. Hopefully we aren't processing
20818 trash data, but since we must specifically ignore things
20819 we don't recognize, there is nothing else we should do at
0963b4bd 20820 this point. */
b98664d3 20821 complaint (_("unsupported tag: '%s'"),
4d3c2250 20822 dwarf_tag_name (die->tag));
c906108c
SS
20823 break;
20824 }
df8a16a1 20825
e37fd15a
SW
20826 if (suppress_add)
20827 {
20828 sym->hash_next = objfile->template_symbols;
20829 objfile->template_symbols = sym;
20830 list_to_add = NULL;
20831 }
20832
20833 if (list_to_add != NULL)
d3cb6808 20834 add_symbol_to_list (sym, list_to_add);
e37fd15a 20835
df8a16a1
DJ
20836 /* For the benefit of old versions of GCC, check for anonymous
20837 namespaces based on the demangled name. */
4d4ec4e5 20838 if (!cu->processing_has_namespace_info
94af9270 20839 && cu->language == language_cplus)
c24bdb02 20840 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20841 }
20842 return (sym);
20843}
20844
98bfdba5
PA
20845/* Given an attr with a DW_FORM_dataN value in host byte order,
20846 zero-extend it as appropriate for the symbol's type. The DWARF
20847 standard (v4) is not entirely clear about the meaning of using
20848 DW_FORM_dataN for a constant with a signed type, where the type is
20849 wider than the data. The conclusion of a discussion on the DWARF
20850 list was that this is unspecified. We choose to always zero-extend
20851 because that is the interpretation long in use by GCC. */
c906108c 20852
98bfdba5 20853static gdb_byte *
ff39bb5e 20854dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20855 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20856{
518817b3 20857 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20858 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20859 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20860 LONGEST l = DW_UNSND (attr);
20861
20862 if (bits < sizeof (*value) * 8)
20863 {
20864 l &= ((LONGEST) 1 << bits) - 1;
20865 *value = l;
20866 }
20867 else if (bits == sizeof (*value) * 8)
20868 *value = l;
20869 else
20870 {
224c3ddb 20871 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20872 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20873 return bytes;
20874 }
20875
20876 return NULL;
20877}
20878
20879/* Read a constant value from an attribute. Either set *VALUE, or if
20880 the value does not fit in *VALUE, set *BYTES - either already
20881 allocated on the objfile obstack, or newly allocated on OBSTACK,
20882 or, set *BATON, if we translated the constant to a location
20883 expression. */
20884
20885static void
ff39bb5e 20886dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20887 const char *name, struct obstack *obstack,
20888 struct dwarf2_cu *cu,
d521ce57 20889 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20890 struct dwarf2_locexpr_baton **baton)
20891{
518817b3 20892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20893 struct comp_unit_head *cu_header = &cu->header;
c906108c 20894 struct dwarf_block *blk;
98bfdba5
PA
20895 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20896 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20897
20898 *value = 0;
20899 *bytes = NULL;
20900 *baton = NULL;
c906108c
SS
20901
20902 switch (attr->form)
20903 {
20904 case DW_FORM_addr:
336d760d 20905 case DW_FORM_addrx:
3019eac3 20906 case DW_FORM_GNU_addr_index:
ac56253d 20907 {
ac56253d
TT
20908 gdb_byte *data;
20909
98bfdba5
PA
20910 if (TYPE_LENGTH (type) != cu_header->addr_size)
20911 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20912 cu_header->addr_size,
98bfdba5 20913 TYPE_LENGTH (type));
ac56253d
TT
20914 /* Symbols of this form are reasonably rare, so we just
20915 piggyback on the existing location code rather than writing
20916 a new implementation of symbol_computed_ops. */
8d749320 20917 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20918 (*baton)->per_cu = cu->per_cu;
20919 gdb_assert ((*baton)->per_cu);
ac56253d 20920
98bfdba5 20921 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20922 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20923 (*baton)->data = data;
ac56253d
TT
20924
20925 data[0] = DW_OP_addr;
20926 store_unsigned_integer (&data[1], cu_header->addr_size,
20927 byte_order, DW_ADDR (attr));
20928 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20929 }
c906108c 20930 break;
4ac36638 20931 case DW_FORM_string:
93b5768b 20932 case DW_FORM_strp:
cf532bd1 20933 case DW_FORM_strx:
3019eac3 20934 case DW_FORM_GNU_str_index:
36586728 20935 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20936 /* DW_STRING is already allocated on the objfile obstack, point
20937 directly to it. */
d521ce57 20938 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20939 break;
c906108c
SS
20940 case DW_FORM_block1:
20941 case DW_FORM_block2:
20942 case DW_FORM_block4:
20943 case DW_FORM_block:
2dc7f7b3 20944 case DW_FORM_exprloc:
0224619f 20945 case DW_FORM_data16:
c906108c 20946 blk = DW_BLOCK (attr);
98bfdba5
PA
20947 if (TYPE_LENGTH (type) != blk->size)
20948 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20949 TYPE_LENGTH (type));
20950 *bytes = blk->data;
c906108c 20951 break;
2df3850c
JM
20952
20953 /* The DW_AT_const_value attributes are supposed to carry the
20954 symbol's value "represented as it would be on the target
20955 architecture." By the time we get here, it's already been
20956 converted to host endianness, so we just need to sign- or
20957 zero-extend it as appropriate. */
20958 case DW_FORM_data1:
3aef2284 20959 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20960 break;
c906108c 20961 case DW_FORM_data2:
3aef2284 20962 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20963 break;
c906108c 20964 case DW_FORM_data4:
3aef2284 20965 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20966 break;
c906108c 20967 case DW_FORM_data8:
3aef2284 20968 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20969 break;
20970
c906108c 20971 case DW_FORM_sdata:
663c44ac 20972 case DW_FORM_implicit_const:
98bfdba5 20973 *value = DW_SND (attr);
2df3850c
JM
20974 break;
20975
c906108c 20976 case DW_FORM_udata:
98bfdba5 20977 *value = DW_UNSND (attr);
c906108c 20978 break;
2df3850c 20979
c906108c 20980 default:
b98664d3 20981 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 20982 dwarf_form_name (attr->form));
98bfdba5 20983 *value = 0;
c906108c
SS
20984 break;
20985 }
20986}
20987
2df3850c 20988
98bfdba5
PA
20989/* Copy constant value from an attribute to a symbol. */
20990
2df3850c 20991static void
ff39bb5e 20992dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20993 struct dwarf2_cu *cu)
2df3850c 20994{
518817b3 20995 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 20996 LONGEST value;
d521ce57 20997 const gdb_byte *bytes;
98bfdba5 20998 struct dwarf2_locexpr_baton *baton;
2df3850c 20999
98bfdba5 21000 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21001 sym->print_name (),
98bfdba5
PA
21002 &objfile->objfile_obstack, cu,
21003 &value, &bytes, &baton);
2df3850c 21004
98bfdba5
PA
21005 if (baton != NULL)
21006 {
98bfdba5 21007 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21008 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21009 }
21010 else if (bytes != NULL)
21011 {
21012 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21013 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21014 }
21015 else
21016 {
21017 SYMBOL_VALUE (sym) = value;
f1e6e072 21018 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21019 }
2df3850c
JM
21020}
21021
c906108c
SS
21022/* Return the type of the die in question using its DW_AT_type attribute. */
21023
21024static struct type *
e7c27a73 21025die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21026{
c906108c 21027 struct attribute *type_attr;
c906108c 21028
e142c38c 21029 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21030 if (!type_attr)
21031 {
518817b3 21032 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21033 /* A missing DW_AT_type represents a void type. */
518817b3 21034 return objfile_type (objfile)->builtin_void;
c906108c 21035 }
348e048f 21036
673bfd45 21037 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21038}
21039
b4ba55a1
JB
21040/* True iff CU's producer generates GNAT Ada auxiliary information
21041 that allows to find parallel types through that information instead
21042 of having to do expensive parallel lookups by type name. */
21043
21044static int
21045need_gnat_info (struct dwarf2_cu *cu)
21046{
de4cb04a
JB
21047 /* Assume that the Ada compiler was GNAT, which always produces
21048 the auxiliary information. */
21049 return (cu->language == language_ada);
b4ba55a1
JB
21050}
21051
b4ba55a1
JB
21052/* Return the auxiliary type of the die in question using its
21053 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21054 attribute is not present. */
21055
21056static struct type *
21057die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21058{
b4ba55a1 21059 struct attribute *type_attr;
b4ba55a1
JB
21060
21061 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21062 if (!type_attr)
21063 return NULL;
21064
673bfd45 21065 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21066}
21067
21068/* If DIE has a descriptive_type attribute, then set the TYPE's
21069 descriptive type accordingly. */
21070
21071static void
21072set_descriptive_type (struct type *type, struct die_info *die,
21073 struct dwarf2_cu *cu)
21074{
21075 struct type *descriptive_type = die_descriptive_type (die, cu);
21076
21077 if (descriptive_type)
21078 {
21079 ALLOCATE_GNAT_AUX_TYPE (type);
21080 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21081 }
21082}
21083
c906108c
SS
21084/* Return the containing type of the die in question using its
21085 DW_AT_containing_type attribute. */
21086
21087static struct type *
e7c27a73 21088die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21089{
c906108c 21090 struct attribute *type_attr;
518817b3 21091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21092
e142c38c 21093 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21094 if (!type_attr)
21095 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21096 "[in module %s]"), objfile_name (objfile));
33ac96f0 21097
673bfd45 21098 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21099}
21100
ac9ec31b
DE
21101/* Return an error marker type to use for the ill formed type in DIE/CU. */
21102
21103static struct type *
21104build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21105{
518817b3
SM
21106 struct dwarf2_per_objfile *dwarf2_per_objfile
21107 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21108 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21109 char *saved;
ac9ec31b 21110
528e1572
SM
21111 std::string message
21112 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21113 objfile_name (objfile),
21114 sect_offset_str (cu->header.sect_off),
21115 sect_offset_str (die->sect_off));
efba19b0 21116 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21117
19f392bc 21118 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21119}
21120
673bfd45 21121/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21122 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21123 DW_AT_containing_type.
673bfd45
DE
21124 If there is no type substitute an error marker. */
21125
c906108c 21126static struct type *
ff39bb5e 21127lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21128 struct dwarf2_cu *cu)
c906108c 21129{
518817b3
SM
21130 struct dwarf2_per_objfile *dwarf2_per_objfile
21131 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21132 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21133 struct type *this_type;
21134
ac9ec31b
DE
21135 gdb_assert (attr->name == DW_AT_type
21136 || attr->name == DW_AT_GNAT_descriptive_type
21137 || attr->name == DW_AT_containing_type);
21138
673bfd45
DE
21139 /* First see if we have it cached. */
21140
36586728
TT
21141 if (attr->form == DW_FORM_GNU_ref_alt)
21142 {
21143 struct dwarf2_per_cu_data *per_cu;
9c541725 21144 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21145
ed2dc618
SM
21146 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21147 dwarf2_per_objfile);
9c541725 21148 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21149 }
cd6c91b4 21150 else if (attr->form_is_ref ())
673bfd45 21151 {
9c541725 21152 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21153
9c541725 21154 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21155 }
55f1336d 21156 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21157 {
ac9ec31b 21158 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21159
ac9ec31b 21160 return get_signatured_type (die, signature, cu);
673bfd45
DE
21161 }
21162 else
21163 {
b98664d3 21164 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21165 " at %s [in module %s]"),
21166 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21167 objfile_name (objfile));
ac9ec31b 21168 return build_error_marker_type (cu, die);
673bfd45
DE
21169 }
21170
21171 /* If not cached we need to read it in. */
21172
21173 if (this_type == NULL)
21174 {
ac9ec31b 21175 struct die_info *type_die = NULL;
673bfd45
DE
21176 struct dwarf2_cu *type_cu = cu;
21177
cd6c91b4 21178 if (attr->form_is_ref ())
ac9ec31b
DE
21179 type_die = follow_die_ref (die, attr, &type_cu);
21180 if (type_die == NULL)
21181 return build_error_marker_type (cu, die);
21182 /* If we find the type now, it's probably because the type came
3019eac3
DE
21183 from an inter-CU reference and the type's CU got expanded before
21184 ours. */
ac9ec31b 21185 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21186 }
21187
21188 /* If we still don't have a type use an error marker. */
21189
21190 if (this_type == NULL)
ac9ec31b 21191 return build_error_marker_type (cu, die);
673bfd45 21192
f792889a 21193 return this_type;
c906108c
SS
21194}
21195
673bfd45
DE
21196/* Return the type in DIE, CU.
21197 Returns NULL for invalid types.
21198
02142a6c 21199 This first does a lookup in die_type_hash,
673bfd45
DE
21200 and only reads the die in if necessary.
21201
21202 NOTE: This can be called when reading in partial or full symbols. */
21203
f792889a 21204static struct type *
e7c27a73 21205read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21206{
f792889a
DJ
21207 struct type *this_type;
21208
21209 this_type = get_die_type (die, cu);
21210 if (this_type)
21211 return this_type;
21212
673bfd45
DE
21213 return read_type_die_1 (die, cu);
21214}
21215
21216/* Read the type in DIE, CU.
21217 Returns NULL for invalid types. */
21218
21219static struct type *
21220read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21221{
21222 struct type *this_type = NULL;
21223
c906108c
SS
21224 switch (die->tag)
21225 {
21226 case DW_TAG_class_type:
680b30c7 21227 case DW_TAG_interface_type:
c906108c
SS
21228 case DW_TAG_structure_type:
21229 case DW_TAG_union_type:
f792889a 21230 this_type = read_structure_type (die, cu);
c906108c
SS
21231 break;
21232 case DW_TAG_enumeration_type:
f792889a 21233 this_type = read_enumeration_type (die, cu);
c906108c
SS
21234 break;
21235 case DW_TAG_subprogram:
21236 case DW_TAG_subroutine_type:
edb3359d 21237 case DW_TAG_inlined_subroutine:
f792889a 21238 this_type = read_subroutine_type (die, cu);
c906108c
SS
21239 break;
21240 case DW_TAG_array_type:
f792889a 21241 this_type = read_array_type (die, cu);
c906108c 21242 break;
72019c9c 21243 case DW_TAG_set_type:
f792889a 21244 this_type = read_set_type (die, cu);
72019c9c 21245 break;
c906108c 21246 case DW_TAG_pointer_type:
f792889a 21247 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21248 break;
21249 case DW_TAG_ptr_to_member_type:
f792889a 21250 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21251 break;
21252 case DW_TAG_reference_type:
4297a3f0
AV
21253 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21254 break;
21255 case DW_TAG_rvalue_reference_type:
21256 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21257 break;
21258 case DW_TAG_const_type:
f792889a 21259 this_type = read_tag_const_type (die, cu);
c906108c
SS
21260 break;
21261 case DW_TAG_volatile_type:
f792889a 21262 this_type = read_tag_volatile_type (die, cu);
c906108c 21263 break;
06d66ee9
TT
21264 case DW_TAG_restrict_type:
21265 this_type = read_tag_restrict_type (die, cu);
21266 break;
c906108c 21267 case DW_TAG_string_type:
f792889a 21268 this_type = read_tag_string_type (die, cu);
c906108c
SS
21269 break;
21270 case DW_TAG_typedef:
f792889a 21271 this_type = read_typedef (die, cu);
c906108c 21272 break;
a02abb62 21273 case DW_TAG_subrange_type:
f792889a 21274 this_type = read_subrange_type (die, cu);
a02abb62 21275 break;
c906108c 21276 case DW_TAG_base_type:
f792889a 21277 this_type = read_base_type (die, cu);
c906108c 21278 break;
81a17f79 21279 case DW_TAG_unspecified_type:
f792889a 21280 this_type = read_unspecified_type (die, cu);
81a17f79 21281 break;
0114d602
DJ
21282 case DW_TAG_namespace:
21283 this_type = read_namespace_type (die, cu);
21284 break;
f55ee35c
JK
21285 case DW_TAG_module:
21286 this_type = read_module_type (die, cu);
21287 break;
a2c2acaf
MW
21288 case DW_TAG_atomic_type:
21289 this_type = read_tag_atomic_type (die, cu);
21290 break;
c906108c 21291 default:
b98664d3 21292 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21293 dwarf_tag_name (die->tag));
c906108c
SS
21294 break;
21295 }
63d06c5c 21296
f792889a 21297 return this_type;
63d06c5c
DC
21298}
21299
abc72ce4
DE
21300/* See if we can figure out if the class lives in a namespace. We do
21301 this by looking for a member function; its demangled name will
21302 contain namespace info, if there is any.
21303 Return the computed name or NULL.
21304 Space for the result is allocated on the objfile's obstack.
21305 This is the full-die version of guess_partial_die_structure_name.
21306 In this case we know DIE has no useful parent. */
21307
43816ebc 21308static const char *
abc72ce4
DE
21309guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21310{
21311 struct die_info *spec_die;
21312 struct dwarf2_cu *spec_cu;
21313 struct die_info *child;
518817b3 21314 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21315
21316 spec_cu = cu;
21317 spec_die = die_specification (die, &spec_cu);
21318 if (spec_die != NULL)
21319 {
21320 die = spec_die;
21321 cu = spec_cu;
21322 }
21323
21324 for (child = die->child;
21325 child != NULL;
21326 child = child->sibling)
21327 {
21328 if (child->tag == DW_TAG_subprogram)
21329 {
73b9be8b 21330 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21331
7d45c7c3 21332 if (linkage_name != NULL)
abc72ce4 21333 {
43816ebc
TT
21334 gdb::unique_xmalloc_ptr<char> actual_name
21335 (language_class_name_from_physname (cu->language_defn,
21336 linkage_name));
21337 const char *name = NULL;
abc72ce4
DE
21338
21339 if (actual_name != NULL)
21340 {
15d034d0 21341 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21342
21343 if (die_name != NULL
43816ebc 21344 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21345 {
21346 /* Strip off the class name from the full name.
21347 We want the prefix. */
21348 int die_name_len = strlen (die_name);
43816ebc
TT
21349 int actual_name_len = strlen (actual_name.get ());
21350 const char *ptr = actual_name.get ();
abc72ce4
DE
21351
21352 /* Test for '::' as a sanity check. */
21353 if (actual_name_len > die_name_len + 2
43816ebc 21354 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21355 name = obstack_strndup (
e3b94546 21356 &objfile->per_bfd->storage_obstack,
43816ebc 21357 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21358 }
21359 }
abc72ce4
DE
21360 return name;
21361 }
21362 }
21363 }
21364
21365 return NULL;
21366}
21367
96408a79
SA
21368/* GCC might emit a nameless typedef that has a linkage name. Determine the
21369 prefix part in such case. See
21370 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21371
a121b7c1 21372static const char *
96408a79
SA
21373anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21374{
21375 struct attribute *attr;
e6a959d6 21376 const char *base;
96408a79
SA
21377
21378 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21379 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21380 return NULL;
21381
7d45c7c3 21382 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21383 return NULL;
21384
73b9be8b 21385 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21386 if (attr == NULL || DW_STRING (attr) == NULL)
21387 return NULL;
21388
21389 /* dwarf2_name had to be already called. */
21390 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21391
21392 /* Strip the base name, keep any leading namespaces/classes. */
21393 base = strrchr (DW_STRING (attr), ':');
21394 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21395 return "";
21396
518817b3 21397 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21398 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21399 DW_STRING (attr),
21400 &base[-1] - DW_STRING (attr));
96408a79
SA
21401}
21402
fdde2d81 21403/* Return the name of the namespace/class that DIE is defined within,
0114d602 21404 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21405
0114d602
DJ
21406 For example, if we're within the method foo() in the following
21407 code:
21408
21409 namespace N {
21410 class C {
21411 void foo () {
21412 }
21413 };
21414 }
21415
21416 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21417
0d5cff50 21418static const char *
e142c38c 21419determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21420{
518817b3
SM
21421 struct dwarf2_per_objfile *dwarf2_per_objfile
21422 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21423 struct die_info *parent, *spec_die;
21424 struct dwarf2_cu *spec_cu;
21425 struct type *parent_type;
a121b7c1 21426 const char *retval;
63d06c5c 21427
9c37b5ae 21428 if (cu->language != language_cplus
c44af4eb
TT
21429 && cu->language != language_fortran && cu->language != language_d
21430 && cu->language != language_rust)
0114d602
DJ
21431 return "";
21432
96408a79
SA
21433 retval = anonymous_struct_prefix (die, cu);
21434 if (retval)
21435 return retval;
21436
0114d602
DJ
21437 /* We have to be careful in the presence of DW_AT_specification.
21438 For example, with GCC 3.4, given the code
21439
21440 namespace N {
21441 void foo() {
21442 // Definition of N::foo.
21443 }
21444 }
21445
21446 then we'll have a tree of DIEs like this:
21447
21448 1: DW_TAG_compile_unit
21449 2: DW_TAG_namespace // N
21450 3: DW_TAG_subprogram // declaration of N::foo
21451 4: DW_TAG_subprogram // definition of N::foo
21452 DW_AT_specification // refers to die #3
21453
21454 Thus, when processing die #4, we have to pretend that we're in
21455 the context of its DW_AT_specification, namely the contex of die
21456 #3. */
21457 spec_cu = cu;
21458 spec_die = die_specification (die, &spec_cu);
21459 if (spec_die == NULL)
21460 parent = die->parent;
21461 else
63d06c5c 21462 {
0114d602
DJ
21463 parent = spec_die->parent;
21464 cu = spec_cu;
63d06c5c 21465 }
0114d602
DJ
21466
21467 if (parent == NULL)
21468 return "";
98bfdba5
PA
21469 else if (parent->building_fullname)
21470 {
21471 const char *name;
21472 const char *parent_name;
21473
21474 /* It has been seen on RealView 2.2 built binaries,
21475 DW_TAG_template_type_param types actually _defined_ as
21476 children of the parent class:
21477
21478 enum E {};
21479 template class <class Enum> Class{};
21480 Class<enum E> class_e;
21481
21482 1: DW_TAG_class_type (Class)
21483 2: DW_TAG_enumeration_type (E)
21484 3: DW_TAG_enumerator (enum1:0)
21485 3: DW_TAG_enumerator (enum2:1)
21486 ...
21487 2: DW_TAG_template_type_param
21488 DW_AT_type DW_FORM_ref_udata (E)
21489
21490 Besides being broken debug info, it can put GDB into an
21491 infinite loop. Consider:
21492
21493 When we're building the full name for Class<E>, we'll start
21494 at Class, and go look over its template type parameters,
21495 finding E. We'll then try to build the full name of E, and
21496 reach here. We're now trying to build the full name of E,
21497 and look over the parent DIE for containing scope. In the
21498 broken case, if we followed the parent DIE of E, we'd again
21499 find Class, and once again go look at its template type
21500 arguments, etc., etc. Simply don't consider such parent die
21501 as source-level parent of this die (it can't be, the language
21502 doesn't allow it), and break the loop here. */
21503 name = dwarf2_name (die, cu);
21504 parent_name = dwarf2_name (parent, cu);
b98664d3 21505 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21506 name ? name : "<unknown>",
21507 parent_name ? parent_name : "<unknown>");
21508 return "";
21509 }
63d06c5c 21510 else
0114d602
DJ
21511 switch (parent->tag)
21512 {
63d06c5c 21513 case DW_TAG_namespace:
0114d602 21514 parent_type = read_type_die (parent, cu);
acebe513
UW
21515 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21516 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21517 Work around this problem here. */
21518 if (cu->language == language_cplus
e86ca25f 21519 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21520 return "";
0114d602 21521 /* We give a name to even anonymous namespaces. */
e86ca25f 21522 return TYPE_NAME (parent_type);
63d06c5c 21523 case DW_TAG_class_type:
680b30c7 21524 case DW_TAG_interface_type:
63d06c5c 21525 case DW_TAG_structure_type:
0114d602 21526 case DW_TAG_union_type:
f55ee35c 21527 case DW_TAG_module:
0114d602 21528 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21529 if (TYPE_NAME (parent_type) != NULL)
21530 return TYPE_NAME (parent_type);
0114d602
DJ
21531 else
21532 /* An anonymous structure is only allowed non-static data
21533 members; no typedefs, no member functions, et cetera.
21534 So it does not need a prefix. */
21535 return "";
abc72ce4 21536 case DW_TAG_compile_unit:
95554aad 21537 case DW_TAG_partial_unit:
abc72ce4
DE
21538 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21539 if (cu->language == language_cplus
fd5866f6 21540 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21541 && die->child != NULL
21542 && (die->tag == DW_TAG_class_type
21543 || die->tag == DW_TAG_structure_type
21544 || die->tag == DW_TAG_union_type))
21545 {
43816ebc 21546 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21547 if (name != NULL)
21548 return name;
21549 }
21550 return "";
0a4b0913
AB
21551 case DW_TAG_subprogram:
21552 /* Nested subroutines in Fortran get a prefix with the name
21553 of the parent's subroutine. */
21554 if (cu->language == language_fortran)
21555 {
21556 if ((die->tag == DW_TAG_subprogram)
21557 && (dwarf2_name (parent, cu) != NULL))
21558 return dwarf2_name (parent, cu);
21559 }
21560 return determine_prefix (parent, cu);
3d567982
TT
21561 case DW_TAG_enumeration_type:
21562 parent_type = read_type_die (parent, cu);
21563 if (TYPE_DECLARED_CLASS (parent_type))
21564 {
e86ca25f
TT
21565 if (TYPE_NAME (parent_type) != NULL)
21566 return TYPE_NAME (parent_type);
3d567982
TT
21567 return "";
21568 }
21569 /* Fall through. */
63d06c5c 21570 default:
8176b9b8 21571 return determine_prefix (parent, cu);
63d06c5c 21572 }
63d06c5c
DC
21573}
21574
3e43a32a
MS
21575/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21576 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21577 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21578 an obconcat, otherwise allocate storage for the result. The CU argument is
21579 used to determine the language and hence, the appropriate separator. */
987504bb 21580
f55ee35c 21581#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21582
21583static char *
f55ee35c
JK
21584typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21585 int physname, struct dwarf2_cu *cu)
63d06c5c 21586{
f55ee35c 21587 const char *lead = "";
5c315b68 21588 const char *sep;
63d06c5c 21589
3e43a32a
MS
21590 if (suffix == NULL || suffix[0] == '\0'
21591 || prefix == NULL || prefix[0] == '\0')
987504bb 21592 sep = "";
45280282
IB
21593 else if (cu->language == language_d)
21594 {
21595 /* For D, the 'main' function could be defined in any module, but it
21596 should never be prefixed. */
21597 if (strcmp (suffix, "D main") == 0)
21598 {
21599 prefix = "";
21600 sep = "";
21601 }
21602 else
21603 sep = ".";
21604 }
f55ee35c
JK
21605 else if (cu->language == language_fortran && physname)
21606 {
21607 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21608 DW_AT_MIPS_linkage_name is preferred and used instead. */
21609
21610 lead = "__";
21611 sep = "_MOD_";
21612 }
987504bb
JJ
21613 else
21614 sep = "::";
63d06c5c 21615
6dd47d34
DE
21616 if (prefix == NULL)
21617 prefix = "";
21618 if (suffix == NULL)
21619 suffix = "";
21620
987504bb
JJ
21621 if (obs == NULL)
21622 {
3e43a32a 21623 char *retval
224c3ddb
SM
21624 = ((char *)
21625 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21626
f55ee35c
JK
21627 strcpy (retval, lead);
21628 strcat (retval, prefix);
6dd47d34
DE
21629 strcat (retval, sep);
21630 strcat (retval, suffix);
63d06c5c
DC
21631 return retval;
21632 }
987504bb
JJ
21633 else
21634 {
21635 /* We have an obstack. */
f55ee35c 21636 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21637 }
63d06c5c
DC
21638}
21639
c906108c
SS
21640/* Return sibling of die, NULL if no sibling. */
21641
f9aca02d 21642static struct die_info *
fba45db2 21643sibling_die (struct die_info *die)
c906108c 21644{
639d11d3 21645 return die->sibling;
c906108c
SS
21646}
21647
71c25dea
TT
21648/* Get name of a die, return NULL if not found. */
21649
15d034d0
TT
21650static const char *
21651dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21652 struct objfile *objfile)
71c25dea
TT
21653{
21654 if (name && cu->language == language_cplus)
21655 {
2f408ecb 21656 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21657
2f408ecb 21658 if (!canon_name.empty ())
71c25dea 21659 {
2f408ecb 21660 if (canon_name != name)
be1e3d3e 21661 name = objfile->intern (canon_name);
71c25dea
TT
21662 }
21663 }
21664
21665 return name;
c906108c
SS
21666}
21667
96553a0c
DE
21668/* Get name of a die, return NULL if not found.
21669 Anonymous namespaces are converted to their magic string. */
9219021c 21670
15d034d0 21671static const char *
e142c38c 21672dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21673{
21674 struct attribute *attr;
518817b3 21675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21676
e142c38c 21677 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21678 if ((!attr || !DW_STRING (attr))
96553a0c 21679 && die->tag != DW_TAG_namespace
53832f31
TT
21680 && die->tag != DW_TAG_class_type
21681 && die->tag != DW_TAG_interface_type
21682 && die->tag != DW_TAG_structure_type
21683 && die->tag != DW_TAG_union_type)
71c25dea
TT
21684 return NULL;
21685
21686 switch (die->tag)
21687 {
21688 case DW_TAG_compile_unit:
95554aad 21689 case DW_TAG_partial_unit:
71c25dea
TT
21690 /* Compilation units have a DW_AT_name that is a filename, not
21691 a source language identifier. */
21692 case DW_TAG_enumeration_type:
21693 case DW_TAG_enumerator:
21694 /* These tags always have simple identifiers already; no need
21695 to canonicalize them. */
21696 return DW_STRING (attr);
907af001 21697
96553a0c
DE
21698 case DW_TAG_namespace:
21699 if (attr != NULL && DW_STRING (attr) != NULL)
21700 return DW_STRING (attr);
21701 return CP_ANONYMOUS_NAMESPACE_STR;
21702
907af001
UW
21703 case DW_TAG_class_type:
21704 case DW_TAG_interface_type:
21705 case DW_TAG_structure_type:
21706 case DW_TAG_union_type:
21707 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21708 structures or unions. These were of the form "._%d" in GCC 4.1,
21709 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21710 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21711 if (attr && DW_STRING (attr)
61012eef
GB
21712 && (startswith (DW_STRING (attr), "._")
21713 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21714 return NULL;
53832f31
TT
21715
21716 /* GCC might emit a nameless typedef that has a linkage name. See
21717 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21718 if (!attr || DW_STRING (attr) == NULL)
21719 {
73b9be8b 21720 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21721 if (attr == NULL || DW_STRING (attr) == NULL)
21722 return NULL;
21723
df5c6c50
JK
21724 /* Avoid demangling DW_STRING (attr) the second time on a second
21725 call for the same DIE. */
21726 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21727 {
43816ebc
TT
21728 gdb::unique_xmalloc_ptr<char> demangled
21729 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21730 if (demangled == nullptr)
21731 return nullptr;
43816ebc 21732
be1e3d3e 21733 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21734 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21735 }
67430cd0
TT
21736
21737 /* Strip any leading namespaces/classes, keep only the base name.
21738 DW_AT_name for named DIEs does not contain the prefixes. */
21739 const char *base = strrchr (DW_STRING (attr), ':');
21740 if (base && base > DW_STRING (attr) && base[-1] == ':')
21741 return &base[1];
21742 else
21743 return DW_STRING (attr);
53832f31 21744 }
907af001
UW
21745 break;
21746
71c25dea 21747 default:
907af001
UW
21748 break;
21749 }
21750
21751 if (!DW_STRING_IS_CANONICAL (attr))
21752 {
be1e3d3e
TT
21753 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21754 objfile);
907af001 21755 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21756 }
907af001 21757 return DW_STRING (attr);
9219021c
DC
21758}
21759
21760/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21761 is none. *EXT_CU is the CU containing DIE on input, and the CU
21762 containing the return value on output. */
9219021c
DC
21763
21764static struct die_info *
f2f0e013 21765dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21766{
21767 struct attribute *attr;
9219021c 21768
f2f0e013 21769 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21770 if (attr == NULL)
21771 return NULL;
21772
f2f0e013 21773 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21774}
21775
fa9c3fa0
TT
21776/* A convenience function that returns an "unknown" DWARF name,
21777 including the value of V. STR is the name of the entity being
21778 printed, e.g., "TAG". */
21779
21780static const char *
21781dwarf_unknown (const char *str, unsigned v)
21782{
21783 char *cell = get_print_cell ();
21784 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21785 return cell;
21786}
21787
c906108c
SS
21788/* Convert a DIE tag into its string name. */
21789
f39c6ffd 21790static const char *
aa1ee363 21791dwarf_tag_name (unsigned tag)
c906108c 21792{
f39c6ffd
TT
21793 const char *name = get_DW_TAG_name (tag);
21794
21795 if (name == NULL)
fa9c3fa0 21796 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
21797
21798 return name;
c906108c
SS
21799}
21800
21801/* Convert a DWARF attribute code into its string name. */
21802
f39c6ffd 21803static const char *
aa1ee363 21804dwarf_attr_name (unsigned attr)
c906108c 21805{
f39c6ffd
TT
21806 const char *name;
21807
c764a876 21808#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21809 if (attr == DW_AT_MIPS_fde)
21810 return "DW_AT_MIPS_fde";
21811#else
21812 if (attr == DW_AT_HP_block_index)
21813 return "DW_AT_HP_block_index";
c764a876 21814#endif
f39c6ffd
TT
21815
21816 name = get_DW_AT_name (attr);
21817
21818 if (name == NULL)
fa9c3fa0 21819 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
21820
21821 return name;
c906108c
SS
21822}
21823
21824/* Convert a DWARF value form code into its string name. */
21825
f39c6ffd 21826static const char *
aa1ee363 21827dwarf_form_name (unsigned form)
c906108c 21828{
f39c6ffd
TT
21829 const char *name = get_DW_FORM_name (form);
21830
21831 if (name == NULL)
fa9c3fa0 21832 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
21833
21834 return name;
c906108c
SS
21835}
21836
a121b7c1 21837static const char *
fba45db2 21838dwarf_bool_name (unsigned mybool)
c906108c
SS
21839{
21840 if (mybool)
21841 return "TRUE";
21842 else
21843 return "FALSE";
21844}
21845
21846/* Convert a DWARF type code into its string name. */
21847
f39c6ffd 21848static const char *
aa1ee363 21849dwarf_type_encoding_name (unsigned enc)
c906108c 21850{
f39c6ffd 21851 const char *name = get_DW_ATE_name (enc);
c906108c 21852
f39c6ffd 21853 if (name == NULL)
fa9c3fa0 21854 return dwarf_unknown ("ATE", enc);
c906108c 21855
f39c6ffd 21856 return name;
c906108c 21857}
c906108c 21858
f9aca02d 21859static void
d97bc12b 21860dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21861{
21862 unsigned int i;
21863
d97bc12b 21864 print_spaces (indent, f);
9d8780f0 21865 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21866 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21867 sect_offset_str (die->sect_off));
d97bc12b
DE
21868
21869 if (die->parent != NULL)
21870 {
21871 print_spaces (indent, f);
9d8780f0
SM
21872 fprintf_unfiltered (f, " parent at offset: %s\n",
21873 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21874 }
21875
21876 print_spaces (indent, f);
21877 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21878 dwarf_bool_name (die->child != NULL));
c906108c 21879
d97bc12b
DE
21880 print_spaces (indent, f);
21881 fprintf_unfiltered (f, " attributes:\n");
21882
c906108c
SS
21883 for (i = 0; i < die->num_attrs; ++i)
21884 {
d97bc12b
DE
21885 print_spaces (indent, f);
21886 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21887 dwarf_attr_name (die->attrs[i].name),
21888 dwarf_form_name (die->attrs[i].form));
d97bc12b 21889
c906108c
SS
21890 switch (die->attrs[i].form)
21891 {
c906108c 21892 case DW_FORM_addr:
336d760d 21893 case DW_FORM_addrx:
3019eac3 21894 case DW_FORM_GNU_addr_index:
d97bc12b 21895 fprintf_unfiltered (f, "address: ");
5af949e3 21896 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21897 break;
21898 case DW_FORM_block2:
21899 case DW_FORM_block4:
21900 case DW_FORM_block:
21901 case DW_FORM_block1:
56eb65bd
SP
21902 fprintf_unfiltered (f, "block: size %s",
21903 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21904 break;
2dc7f7b3 21905 case DW_FORM_exprloc:
56eb65bd
SP
21906 fprintf_unfiltered (f, "expression: size %s",
21907 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21908 break;
0224619f
JK
21909 case DW_FORM_data16:
21910 fprintf_unfiltered (f, "constant of 16 bytes");
21911 break;
4568ecf9
DE
21912 case DW_FORM_ref_addr:
21913 fprintf_unfiltered (f, "ref address: ");
21914 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21915 break;
36586728
TT
21916 case DW_FORM_GNU_ref_alt:
21917 fprintf_unfiltered (f, "alt ref address: ");
21918 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21919 break;
10b3939b
DJ
21920 case DW_FORM_ref1:
21921 case DW_FORM_ref2:
21922 case DW_FORM_ref4:
4568ecf9
DE
21923 case DW_FORM_ref8:
21924 case DW_FORM_ref_udata:
d97bc12b 21925 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21926 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21927 break;
c906108c
SS
21928 case DW_FORM_data1:
21929 case DW_FORM_data2:
21930 case DW_FORM_data4:
ce5d95e1 21931 case DW_FORM_data8:
c906108c
SS
21932 case DW_FORM_udata:
21933 case DW_FORM_sdata:
43bbcdc2
PH
21934 fprintf_unfiltered (f, "constant: %s",
21935 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21936 break;
2dc7f7b3
TT
21937 case DW_FORM_sec_offset:
21938 fprintf_unfiltered (f, "section offset: %s",
21939 pulongest (DW_UNSND (&die->attrs[i])));
21940 break;
55f1336d 21941 case DW_FORM_ref_sig8:
ac9ec31b
DE
21942 fprintf_unfiltered (f, "signature: %s",
21943 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21944 break;
c906108c 21945 case DW_FORM_string:
4bdf3d34 21946 case DW_FORM_strp:
43988095 21947 case DW_FORM_line_strp:
cf532bd1 21948 case DW_FORM_strx:
3019eac3 21949 case DW_FORM_GNU_str_index:
36586728 21950 case DW_FORM_GNU_strp_alt:
8285870a 21951 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21952 DW_STRING (&die->attrs[i])
8285870a
JK
21953 ? DW_STRING (&die->attrs[i]) : "",
21954 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21955 break;
21956 case DW_FORM_flag:
21957 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21958 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21959 else
d97bc12b 21960 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21961 break;
2dc7f7b3
TT
21962 case DW_FORM_flag_present:
21963 fprintf_unfiltered (f, "flag: TRUE");
21964 break;
a8329558 21965 case DW_FORM_indirect:
0963b4bd
MS
21966 /* The reader will have reduced the indirect form to
21967 the "base form" so this form should not occur. */
5f48f8f3 21968 fprintf_unfiltered (f,
3e43a32a 21969 "unexpected attribute form: DW_FORM_indirect");
a8329558 21970 break;
663c44ac
JK
21971 case DW_FORM_implicit_const:
21972 fprintf_unfiltered (f, "constant: %s",
21973 plongest (DW_SND (&die->attrs[i])));
21974 break;
c906108c 21975 default:
d97bc12b 21976 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21977 die->attrs[i].form);
d97bc12b 21978 break;
c906108c 21979 }
d97bc12b 21980 fprintf_unfiltered (f, "\n");
c906108c
SS
21981 }
21982}
21983
f9aca02d 21984static void
d97bc12b 21985dump_die_for_error (struct die_info *die)
c906108c 21986{
d97bc12b
DE
21987 dump_die_shallow (gdb_stderr, 0, die);
21988}
21989
21990static void
21991dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21992{
21993 int indent = level * 4;
21994
21995 gdb_assert (die != NULL);
21996
21997 if (level >= max_level)
21998 return;
21999
22000 dump_die_shallow (f, indent, die);
22001
22002 if (die->child != NULL)
c906108c 22003 {
d97bc12b
DE
22004 print_spaces (indent, f);
22005 fprintf_unfiltered (f, " Children:");
22006 if (level + 1 < max_level)
22007 {
22008 fprintf_unfiltered (f, "\n");
22009 dump_die_1 (f, level + 1, max_level, die->child);
22010 }
22011 else
22012 {
3e43a32a
MS
22013 fprintf_unfiltered (f,
22014 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22015 }
22016 }
22017
22018 if (die->sibling != NULL && level > 0)
22019 {
22020 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22021 }
22022}
22023
d97bc12b
DE
22024/* This is called from the pdie macro in gdbinit.in.
22025 It's not static so gcc will keep a copy callable from gdb. */
22026
22027void
22028dump_die (struct die_info *die, int max_level)
22029{
22030 dump_die_1 (gdb_stdlog, 0, max_level, die);
22031}
22032
f9aca02d 22033static void
51545339 22034store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22035{
51545339 22036 void **slot;
c906108c 22037
9c541725
PA
22038 slot = htab_find_slot_with_hash (cu->die_hash, die,
22039 to_underlying (die->sect_off),
b64f50a1 22040 INSERT);
51545339
DJ
22041
22042 *slot = die;
c906108c
SS
22043}
22044
b64f50a1
JK
22045/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22046 required kind. */
22047
22048static sect_offset
ff39bb5e 22049dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22050{
cd6c91b4 22051 if (attr->form_is_ref ())
9c541725 22052 return (sect_offset) DW_UNSND (attr);
93311388 22053
b98664d3 22054 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22055 dwarf_form_name (attr->form));
9c541725 22056 return {};
c906108c
SS
22057}
22058
43bbcdc2
PH
22059/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22060 * the value held by the attribute is not constant. */
a02abb62 22061
43bbcdc2 22062static LONGEST
ff39bb5e 22063dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22064{
663c44ac 22065 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22066 return DW_SND (attr);
22067 else if (attr->form == DW_FORM_udata
22068 || attr->form == DW_FORM_data1
22069 || attr->form == DW_FORM_data2
22070 || attr->form == DW_FORM_data4
22071 || attr->form == DW_FORM_data8)
22072 return DW_UNSND (attr);
22073 else
22074 {
cd6c91b4 22075 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22076 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22077 dwarf_form_name (attr->form));
22078 return default_value;
22079 }
22080}
22081
348e048f
DE
22082/* Follow reference or signature attribute ATTR of SRC_DIE.
22083 On entry *REF_CU is the CU of SRC_DIE.
22084 On exit *REF_CU is the CU of the result. */
22085
22086static struct die_info *
ff39bb5e 22087follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22088 struct dwarf2_cu **ref_cu)
22089{
22090 struct die_info *die;
22091
cd6c91b4 22092 if (attr->form_is_ref ())
348e048f 22093 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22094 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22095 die = follow_die_sig (src_die, attr, ref_cu);
22096 else
22097 {
22098 dump_die_for_error (src_die);
22099 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22100 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22101 }
22102
22103 return die;
03dd20cc
DJ
22104}
22105
5c631832 22106/* Follow reference OFFSET.
673bfd45
DE
22107 On entry *REF_CU is the CU of the source die referencing OFFSET.
22108 On exit *REF_CU is the CU of the result.
22109 Returns NULL if OFFSET is invalid. */
f504f079 22110
f9aca02d 22111static struct die_info *
9c541725 22112follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22113 struct dwarf2_cu **ref_cu)
c906108c 22114{
10b3939b 22115 struct die_info temp_die;
f2f0e013 22116 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22117 struct dwarf2_per_objfile *dwarf2_per_objfile
22118 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22119
348e048f
DE
22120 gdb_assert (cu->per_cu != NULL);
22121
98bfdba5
PA
22122 target_cu = cu;
22123
3019eac3 22124 if (cu->per_cu->is_debug_types)
348e048f
DE
22125 {
22126 /* .debug_types CUs cannot reference anything outside their CU.
22127 If they need to, they have to reference a signatured type via
55f1336d 22128 DW_FORM_ref_sig8. */
4057dfde 22129 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22130 return NULL;
348e048f 22131 }
36586728 22132 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22133 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22134 {
22135 struct dwarf2_per_cu_data *per_cu;
9a619af0 22136
9c541725 22137 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22138 dwarf2_per_objfile);
03dd20cc
DJ
22139
22140 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22141 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22142 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22143
10b3939b
DJ
22144 target_cu = per_cu->cu;
22145 }
98bfdba5
PA
22146 else if (cu->dies == NULL)
22147 {
22148 /* We're loading full DIEs during partial symbol reading. */
22149 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22150 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22151 }
c906108c 22152
f2f0e013 22153 *ref_cu = target_cu;
9c541725 22154 temp_die.sect_off = sect_off;
c24bdb02
KS
22155
22156 if (target_cu != cu)
22157 target_cu->ancestor = cu;
22158
9a3c8263 22159 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22160 &temp_die,
22161 to_underlying (sect_off));
5c631832 22162}
10b3939b 22163
5c631832
JK
22164/* Follow reference attribute ATTR of SRC_DIE.
22165 On entry *REF_CU is the CU of SRC_DIE.
22166 On exit *REF_CU is the CU of the result. */
22167
22168static struct die_info *
ff39bb5e 22169follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22170 struct dwarf2_cu **ref_cu)
22171{
9c541725 22172 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22173 struct dwarf2_cu *cu = *ref_cu;
22174 struct die_info *die;
22175
9c541725 22176 die = follow_die_offset (sect_off,
36586728
TT
22177 (attr->form == DW_FORM_GNU_ref_alt
22178 || cu->per_cu->is_dwz),
22179 ref_cu);
5c631832 22180 if (!die)
9d8780f0
SM
22181 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22182 "at %s [in module %s]"),
22183 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22184 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22185
5c631832
JK
22186 return die;
22187}
22188
d4c9a4f8 22189/* See read.h. */
5c631832
JK
22190
22191struct dwarf2_locexpr_baton
9c541725 22192dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22193 dwarf2_per_cu_data *per_cu,
8b9737bf 22194 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22195 void *baton, bool resolve_abstract_p)
5c631832 22196{
918dd910 22197 struct dwarf2_cu *cu;
5c631832
JK
22198 struct die_info *die;
22199 struct attribute *attr;
22200 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22201 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22202 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22203
918dd910 22204 if (per_cu->cu == NULL)
58f0c718 22205 load_cu (per_cu, false);
918dd910 22206 cu = per_cu->cu;
cc12ce38
DE
22207 if (cu == NULL)
22208 {
22209 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22210 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22211 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22212 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22213 }
918dd910 22214
9c541725 22215 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22216 if (!die)
9d8780f0
SM
22217 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22218 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22219
22220 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22221 if (!attr && resolve_abstract_p
3360b6e7 22222 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22223 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22224 {
22225 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22226 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 22227 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 22228
3360b6e7
TV
22229 for (const auto &cand_off
22230 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22231 {
3360b6e7
TV
22232 struct dwarf2_cu *cand_cu = cu;
22233 struct die_info *cand
22234 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22235 if (!cand
22236 || !cand->parent
e4a62c65
TV
22237 || cand->parent->tag != DW_TAG_subprogram)
22238 continue;
22239
22240 CORE_ADDR pc_low, pc_high;
22241 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22242 if (pc_low == ((CORE_ADDR) -1))
22243 continue;
22244 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22245 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22246 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22247 continue;
22248
22249 die = cand;
22250 attr = dwarf2_attr (die, DW_AT_location, cu);
22251 break;
22252 }
22253 }
22254
5c631832
JK
22255 if (!attr)
22256 {
e103e986
JK
22257 /* DWARF: "If there is no such attribute, then there is no effect.".
22258 DATA is ignored if SIZE is 0. */
5c631832 22259
e103e986 22260 retval.data = NULL;
5c631832
JK
22261 retval.size = 0;
22262 }
cd6c91b4 22263 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22264 {
22265 struct dwarf2_loclist_baton loclist_baton;
22266 CORE_ADDR pc = (*get_frame_pc) (baton);
22267 size_t size;
22268
22269 fill_in_loclist_baton (cu, &loclist_baton, attr);
22270
22271 retval.data = dwarf2_find_location_expression (&loclist_baton,
22272 &size, pc);
22273 retval.size = size;
22274 }
5c631832
JK
22275 else
22276 {
4fc6c0d5 22277 if (!attr->form_is_block ())
9d8780f0 22278 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22279 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22280 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22281
22282 retval.data = DW_BLOCK (attr)->data;
22283 retval.size = DW_BLOCK (attr)->size;
22284 }
22285 retval.per_cu = cu->per_cu;
918dd910 22286
ed2dc618 22287 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22288
5c631832 22289 return retval;
348e048f
DE
22290}
22291
d4c9a4f8 22292/* See read.h. */
8b9737bf
TT
22293
22294struct dwarf2_locexpr_baton
22295dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22296 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22297 CORE_ADDR (*get_frame_pc) (void *baton),
22298 void *baton)
22299{
9c541725 22300 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22301
9c541725 22302 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22303}
22304
b6807d98
TT
22305/* Write a constant of a given type as target-ordered bytes into
22306 OBSTACK. */
22307
22308static const gdb_byte *
22309write_constant_as_bytes (struct obstack *obstack,
22310 enum bfd_endian byte_order,
22311 struct type *type,
22312 ULONGEST value,
22313 LONGEST *len)
22314{
22315 gdb_byte *result;
22316
22317 *len = TYPE_LENGTH (type);
224c3ddb 22318 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22319 store_unsigned_integer (result, *len, byte_order, value);
22320
22321 return result;
22322}
22323
d4c9a4f8 22324/* See read.h. */
b6807d98
TT
22325
22326const gdb_byte *
9c541725 22327dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22328 dwarf2_per_cu_data *per_cu,
22329 obstack *obstack,
b6807d98
TT
22330 LONGEST *len)
22331{
22332 struct dwarf2_cu *cu;
22333 struct die_info *die;
22334 struct attribute *attr;
22335 const gdb_byte *result = NULL;
22336 struct type *type;
22337 LONGEST value;
22338 enum bfd_endian byte_order;
e3b94546 22339 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22340
b6807d98 22341 if (per_cu->cu == NULL)
58f0c718 22342 load_cu (per_cu, false);
b6807d98 22343 cu = per_cu->cu;
cc12ce38
DE
22344 if (cu == NULL)
22345 {
22346 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22347 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22348 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22349 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22350 }
b6807d98 22351
9c541725 22352 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22353 if (!die)
9d8780f0
SM
22354 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22355 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22356
22357 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22358 if (attr == NULL)
22359 return NULL;
22360
e3b94546 22361 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22362 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22363
22364 switch (attr->form)
22365 {
22366 case DW_FORM_addr:
336d760d 22367 case DW_FORM_addrx:
b6807d98
TT
22368 case DW_FORM_GNU_addr_index:
22369 {
22370 gdb_byte *tem;
22371
22372 *len = cu->header.addr_size;
224c3ddb 22373 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22374 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22375 result = tem;
22376 }
22377 break;
22378 case DW_FORM_string:
22379 case DW_FORM_strp:
cf532bd1 22380 case DW_FORM_strx:
b6807d98
TT
22381 case DW_FORM_GNU_str_index:
22382 case DW_FORM_GNU_strp_alt:
22383 /* DW_STRING is already allocated on the objfile obstack, point
22384 directly to it. */
22385 result = (const gdb_byte *) DW_STRING (attr);
22386 *len = strlen (DW_STRING (attr));
22387 break;
22388 case DW_FORM_block1:
22389 case DW_FORM_block2:
22390 case DW_FORM_block4:
22391 case DW_FORM_block:
22392 case DW_FORM_exprloc:
0224619f 22393 case DW_FORM_data16:
b6807d98
TT
22394 result = DW_BLOCK (attr)->data;
22395 *len = DW_BLOCK (attr)->size;
22396 break;
22397
22398 /* The DW_AT_const_value attributes are supposed to carry the
22399 symbol's value "represented as it would be on the target
22400 architecture." By the time we get here, it's already been
22401 converted to host endianness, so we just need to sign- or
22402 zero-extend it as appropriate. */
22403 case DW_FORM_data1:
22404 type = die_type (die, cu);
22405 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22406 if (result == NULL)
22407 result = write_constant_as_bytes (obstack, byte_order,
22408 type, value, len);
22409 break;
22410 case DW_FORM_data2:
22411 type = die_type (die, cu);
22412 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22413 if (result == NULL)
22414 result = write_constant_as_bytes (obstack, byte_order,
22415 type, value, len);
22416 break;
22417 case DW_FORM_data4:
22418 type = die_type (die, cu);
22419 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22420 if (result == NULL)
22421 result = write_constant_as_bytes (obstack, byte_order,
22422 type, value, len);
22423 break;
22424 case DW_FORM_data8:
22425 type = die_type (die, cu);
22426 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22427 if (result == NULL)
22428 result = write_constant_as_bytes (obstack, byte_order,
22429 type, value, len);
22430 break;
22431
22432 case DW_FORM_sdata:
663c44ac 22433 case DW_FORM_implicit_const:
b6807d98
TT
22434 type = die_type (die, cu);
22435 result = write_constant_as_bytes (obstack, byte_order,
22436 type, DW_SND (attr), len);
22437 break;
22438
22439 case DW_FORM_udata:
22440 type = die_type (die, cu);
22441 result = write_constant_as_bytes (obstack, byte_order,
22442 type, DW_UNSND (attr), len);
22443 break;
22444
22445 default:
b98664d3 22446 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22447 dwarf_form_name (attr->form));
22448 break;
22449 }
22450
22451 return result;
22452}
22453
d4c9a4f8 22454/* See read.h. */
7942e96e
AA
22455
22456struct type *
9c541725 22457dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22458 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22459{
22460 struct dwarf2_cu *cu;
22461 struct die_info *die;
22462
7942e96e 22463 if (per_cu->cu == NULL)
58f0c718 22464 load_cu (per_cu, false);
7942e96e
AA
22465 cu = per_cu->cu;
22466 if (!cu)
22467 return NULL;
22468
9c541725 22469 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22470 if (!die)
22471 return NULL;
22472
22473 return die_type (die, cu);
22474}
22475
8cb5117c 22476/* See read.h. */
8a9b8146
TT
22477
22478struct type *
b64f50a1 22479dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22480 struct dwarf2_per_cu_data *per_cu)
22481{
9c541725 22482 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22483 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22484}
22485
ac9ec31b 22486/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22487 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22488 On exit *REF_CU is the CU of the result.
22489 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22490
22491static struct die_info *
ac9ec31b
DE
22492follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22493 struct dwarf2_cu **ref_cu)
348e048f 22494{
348e048f 22495 struct die_info temp_die;
c24bdb02 22496 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22497 struct die_info *die;
22498
ac9ec31b
DE
22499 /* While it might be nice to assert sig_type->type == NULL here,
22500 we can get here for DW_AT_imported_declaration where we need
22501 the DIE not the type. */
348e048f
DE
22502
22503 /* If necessary, add it to the queue and load its DIEs. */
22504
95554aad 22505 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22506 read_signatured_type (sig_type);
348e048f 22507
348e048f 22508 sig_cu = sig_type->per_cu.cu;
69d751e3 22509 gdb_assert (sig_cu != NULL);
9c541725
PA
22510 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22511 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22512 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22513 to_underlying (temp_die.sect_off));
348e048f
DE
22514 if (die)
22515 {
ed2dc618 22516 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22517 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22518
796a7ff8
DE
22519 /* For .gdb_index version 7 keep track of included TUs.
22520 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22521 if (dwarf2_per_objfile->index_table != NULL
22522 && dwarf2_per_objfile->index_table->version <= 7)
22523 {
ae640021 22524 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22525 }
22526
348e048f 22527 *ref_cu = sig_cu;
c24bdb02
KS
22528 if (sig_cu != cu)
22529 sig_cu->ancestor = cu;
22530
348e048f
DE
22531 return die;
22532 }
22533
ac9ec31b
DE
22534 return NULL;
22535}
22536
22537/* Follow signatured type referenced by ATTR in SRC_DIE.
22538 On entry *REF_CU is the CU of SRC_DIE.
22539 On exit *REF_CU is the CU of the result.
22540 The result is the DIE of the type.
22541 If the referenced type cannot be found an error is thrown. */
22542
22543static struct die_info *
ff39bb5e 22544follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22545 struct dwarf2_cu **ref_cu)
22546{
22547 ULONGEST signature = DW_SIGNATURE (attr);
22548 struct signatured_type *sig_type;
22549 struct die_info *die;
22550
22551 gdb_assert (attr->form == DW_FORM_ref_sig8);
22552
a2ce51a0 22553 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22554 /* sig_type will be NULL if the signatured type is missing from
22555 the debug info. */
22556 if (sig_type == NULL)
22557 {
22558 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22559 " from DIE at %s [in module %s]"),
22560 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22561 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22562 }
22563
22564 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22565 if (die == NULL)
22566 {
22567 dump_die_for_error (src_die);
22568 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22569 " from DIE at %s [in module %s]"),
22570 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22571 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22572 }
22573
22574 return die;
22575}
22576
22577/* Get the type specified by SIGNATURE referenced in DIE/CU,
22578 reading in and processing the type unit if necessary. */
22579
22580static struct type *
22581get_signatured_type (struct die_info *die, ULONGEST signature,
22582 struct dwarf2_cu *cu)
22583{
518817b3
SM
22584 struct dwarf2_per_objfile *dwarf2_per_objfile
22585 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22586 struct signatured_type *sig_type;
22587 struct dwarf2_cu *type_cu;
22588 struct die_info *type_die;
22589 struct type *type;
22590
a2ce51a0 22591 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22592 /* sig_type will be NULL if the signatured type is missing from
22593 the debug info. */
22594 if (sig_type == NULL)
22595 {
b98664d3 22596 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22597 " from DIE at %s [in module %s]"),
22598 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22599 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22600 return build_error_marker_type (cu, die);
22601 }
22602
22603 /* If we already know the type we're done. */
22604 if (sig_type->type != NULL)
22605 return sig_type->type;
22606
22607 type_cu = cu;
22608 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22609 if (type_die != NULL)
22610 {
22611 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22612 is created. This is important, for example, because for c++ classes
22613 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22614 type = read_type_die (type_die, type_cu);
22615 if (type == NULL)
22616 {
b98664d3 22617 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22618 " referenced from DIE at %s [in module %s]"),
22619 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22620 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22621 type = build_error_marker_type (cu, die);
22622 }
22623 }
22624 else
22625 {
b98664d3 22626 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22627 " from DIE at %s [in module %s]"),
22628 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22629 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22630 type = build_error_marker_type (cu, die);
22631 }
22632 sig_type->type = type;
22633
22634 return type;
22635}
22636
22637/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22638 reading in and processing the type unit if necessary. */
22639
22640static struct type *
ff39bb5e 22641get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22642 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22643{
22644 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22645 if (attr->form_is_ref ())
ac9ec31b
DE
22646 {
22647 struct dwarf2_cu *type_cu = cu;
22648 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22649
22650 return read_type_die (type_die, type_cu);
22651 }
22652 else if (attr->form == DW_FORM_ref_sig8)
22653 {
22654 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22655 }
22656 else
22657 {
518817b3
SM
22658 struct dwarf2_per_objfile *dwarf2_per_objfile
22659 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22660
b98664d3 22661 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22662 " at %s [in module %s]"),
22663 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22664 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22665 return build_error_marker_type (cu, die);
22666 }
348e048f
DE
22667}
22668
e5fe5e75 22669/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22670
22671static void
e5fe5e75 22672load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22673{
52dc124a 22674 struct signatured_type *sig_type;
348e048f 22675
f4dc4d17 22676 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22677 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22678
6721b2ec
DE
22679 /* We have the per_cu, but we need the signatured_type.
22680 Fortunately this is an easy translation. */
22681 gdb_assert (per_cu->is_debug_types);
22682 sig_type = (struct signatured_type *) per_cu;
348e048f 22683
6721b2ec 22684 gdb_assert (per_cu->cu == NULL);
348e048f 22685
52dc124a 22686 read_signatured_type (sig_type);
348e048f 22687
6721b2ec 22688 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22689}
22690
3019eac3
DE
22691/* Read in a signatured type and build its CU and DIEs.
22692 If the type is a stub for the real type in a DWO file,
22693 read in the real type from the DWO file as well. */
dee91e82
DE
22694
22695static void
22696read_signatured_type (struct signatured_type *sig_type)
22697{
22698 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22699
3019eac3 22700 gdb_assert (per_cu->is_debug_types);
dee91e82 22701 gdb_assert (per_cu->cu == NULL);
348e048f 22702
6751ebae 22703 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22704
22705 if (!reader.dummy_p)
22706 {
22707 struct dwarf2_cu *cu = reader.cu;
22708 const gdb_byte *info_ptr = reader.info_ptr;
22709
22710 gdb_assert (cu->die_hash == NULL);
22711 cu->die_hash =
22712 htab_create_alloc_ex (cu->header.length / 12,
22713 die_hash,
22714 die_eq,
22715 NULL,
22716 &cu->comp_unit_obstack,
22717 hashtab_obstack_allocate,
22718 dummy_obstack_deallocate);
22719
3e225074 22720 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22721 reader.comp_unit_die->child
22722 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22723 reader.comp_unit_die);
22724 cu->dies = reader.comp_unit_die;
22725 /* comp_unit_die is not stored in die_hash, no need. */
22726
22727 /* We try not to read any attributes in this function, because
22728 not all CUs needed for references have been loaded yet, and
22729 symbol table processing isn't initialized. But we have to
22730 set the CU language, or we won't be able to build types
22731 correctly. Similarly, if we do not read the producer, we can
22732 not apply producer-specific interpretation. */
22733 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22734
22735 reader.keep ();
c0ab21c2
TT
22736 }
22737
7ee85ab1 22738 sig_type->per_cu.tu_read = 1;
c906108c
SS
22739}
22740
c906108c
SS
22741/* Decode simple location descriptions.
22742 Given a pointer to a dwarf block that defines a location, compute
22743 the location and return the value.
22744
4cecd739
DJ
22745 NOTE drow/2003-11-18: This function is called in two situations
22746 now: for the address of static or global variables (partial symbols
22747 only) and for offsets into structures which are expected to be
22748 (more or less) constant. The partial symbol case should go away,
22749 and only the constant case should remain. That will let this
22750 function complain more accurately. A few special modes are allowed
22751 without complaint for global variables (for instance, global
22752 register values and thread-local values).
c906108c
SS
22753
22754 A location description containing no operations indicates that the
4cecd739 22755 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22756 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22757 callers will only want a very basic result and this can become a
21ae7a4d
JK
22758 complaint.
22759
22760 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22761
22762static CORE_ADDR
e7c27a73 22763decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22764{
518817b3 22765 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22766 size_t i;
22767 size_t size = blk->size;
d521ce57 22768 const gdb_byte *data = blk->data;
21ae7a4d
JK
22769 CORE_ADDR stack[64];
22770 int stacki;
22771 unsigned int bytes_read, unsnd;
22772 gdb_byte op;
c906108c 22773
21ae7a4d
JK
22774 i = 0;
22775 stacki = 0;
22776 stack[stacki] = 0;
22777 stack[++stacki] = 0;
22778
22779 while (i < size)
22780 {
22781 op = data[i++];
22782 switch (op)
22783 {
22784 case DW_OP_lit0:
22785 case DW_OP_lit1:
22786 case DW_OP_lit2:
22787 case DW_OP_lit3:
22788 case DW_OP_lit4:
22789 case DW_OP_lit5:
22790 case DW_OP_lit6:
22791 case DW_OP_lit7:
22792 case DW_OP_lit8:
22793 case DW_OP_lit9:
22794 case DW_OP_lit10:
22795 case DW_OP_lit11:
22796 case DW_OP_lit12:
22797 case DW_OP_lit13:
22798 case DW_OP_lit14:
22799 case DW_OP_lit15:
22800 case DW_OP_lit16:
22801 case DW_OP_lit17:
22802 case DW_OP_lit18:
22803 case DW_OP_lit19:
22804 case DW_OP_lit20:
22805 case DW_OP_lit21:
22806 case DW_OP_lit22:
22807 case DW_OP_lit23:
22808 case DW_OP_lit24:
22809 case DW_OP_lit25:
22810 case DW_OP_lit26:
22811 case DW_OP_lit27:
22812 case DW_OP_lit28:
22813 case DW_OP_lit29:
22814 case DW_OP_lit30:
22815 case DW_OP_lit31:
22816 stack[++stacki] = op - DW_OP_lit0;
22817 break;
f1bea926 22818
21ae7a4d
JK
22819 case DW_OP_reg0:
22820 case DW_OP_reg1:
22821 case DW_OP_reg2:
22822 case DW_OP_reg3:
22823 case DW_OP_reg4:
22824 case DW_OP_reg5:
22825 case DW_OP_reg6:
22826 case DW_OP_reg7:
22827 case DW_OP_reg8:
22828 case DW_OP_reg9:
22829 case DW_OP_reg10:
22830 case DW_OP_reg11:
22831 case DW_OP_reg12:
22832 case DW_OP_reg13:
22833 case DW_OP_reg14:
22834 case DW_OP_reg15:
22835 case DW_OP_reg16:
22836 case DW_OP_reg17:
22837 case DW_OP_reg18:
22838 case DW_OP_reg19:
22839 case DW_OP_reg20:
22840 case DW_OP_reg21:
22841 case DW_OP_reg22:
22842 case DW_OP_reg23:
22843 case DW_OP_reg24:
22844 case DW_OP_reg25:
22845 case DW_OP_reg26:
22846 case DW_OP_reg27:
22847 case DW_OP_reg28:
22848 case DW_OP_reg29:
22849 case DW_OP_reg30:
22850 case DW_OP_reg31:
22851 stack[++stacki] = op - DW_OP_reg0;
22852 if (i < size)
22853 dwarf2_complex_location_expr_complaint ();
22854 break;
c906108c 22855
21ae7a4d
JK
22856 case DW_OP_regx:
22857 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22858 i += bytes_read;
22859 stack[++stacki] = unsnd;
22860 if (i < size)
22861 dwarf2_complex_location_expr_complaint ();
22862 break;
c906108c 22863
21ae7a4d 22864 case DW_OP_addr:
c8a7a66f
TT
22865 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22866 &bytes_read);
21ae7a4d
JK
22867 i += bytes_read;
22868 break;
d53d4ac5 22869
21ae7a4d
JK
22870 case DW_OP_const1u:
22871 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22872 i += 1;
22873 break;
22874
22875 case DW_OP_const1s:
22876 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22877 i += 1;
22878 break;
22879
22880 case DW_OP_const2u:
22881 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22882 i += 2;
22883 break;
22884
22885 case DW_OP_const2s:
22886 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22887 i += 2;
22888 break;
d53d4ac5 22889
21ae7a4d
JK
22890 case DW_OP_const4u:
22891 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22892 i += 4;
22893 break;
22894
22895 case DW_OP_const4s:
22896 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22897 i += 4;
22898 break;
22899
585861ea
JK
22900 case DW_OP_const8u:
22901 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22902 i += 8;
22903 break;
22904
21ae7a4d
JK
22905 case DW_OP_constu:
22906 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22907 &bytes_read);
22908 i += bytes_read;
22909 break;
22910
22911 case DW_OP_consts:
22912 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22913 i += bytes_read;
22914 break;
22915
22916 case DW_OP_dup:
22917 stack[stacki + 1] = stack[stacki];
22918 stacki++;
22919 break;
22920
22921 case DW_OP_plus:
22922 stack[stacki - 1] += stack[stacki];
22923 stacki--;
22924 break;
22925
22926 case DW_OP_plus_uconst:
22927 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22928 &bytes_read);
22929 i += bytes_read;
22930 break;
22931
22932 case DW_OP_minus:
22933 stack[stacki - 1] -= stack[stacki];
22934 stacki--;
22935 break;
22936
22937 case DW_OP_deref:
22938 /* If we're not the last op, then we definitely can't encode
22939 this using GDB's address_class enum. This is valid for partial
22940 global symbols, although the variable's address will be bogus
22941 in the psymtab. */
22942 if (i < size)
22943 dwarf2_complex_location_expr_complaint ();
22944 break;
22945
22946 case DW_OP_GNU_push_tls_address:
4aa4e28b 22947 case DW_OP_form_tls_address:
21ae7a4d
JK
22948 /* The top of the stack has the offset from the beginning
22949 of the thread control block at which the variable is located. */
22950 /* Nothing should follow this operator, so the top of stack would
22951 be returned. */
22952 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22953 address will be bogus in the psymtab. Make it always at least
22954 non-zero to not look as a variable garbage collected by linker
22955 which have DW_OP_addr 0. */
21ae7a4d
JK
22956 if (i < size)
22957 dwarf2_complex_location_expr_complaint ();
585861ea 22958 stack[stacki]++;
21ae7a4d
JK
22959 break;
22960
22961 case DW_OP_GNU_uninit:
22962 break;
22963
336d760d 22964 case DW_OP_addrx:
3019eac3 22965 case DW_OP_GNU_addr_index:
49f6c839 22966 case DW_OP_GNU_const_index:
3019eac3
DE
22967 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22968 &bytes_read);
22969 i += bytes_read;
22970 break;
22971
21ae7a4d
JK
22972 default:
22973 {
f39c6ffd 22974 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22975
22976 if (name)
b98664d3 22977 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
22978 name);
22979 else
b98664d3 22980 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
22981 op);
22982 }
22983
22984 return (stack[stacki]);
d53d4ac5 22985 }
3c6e0cb3 22986
21ae7a4d
JK
22987 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22988 outside of the allocated space. Also enforce minimum>0. */
22989 if (stacki >= ARRAY_SIZE (stack) - 1)
22990 {
b98664d3 22991 complaint (_("location description stack overflow"));
21ae7a4d
JK
22992 return 0;
22993 }
22994
22995 if (stacki <= 0)
22996 {
b98664d3 22997 complaint (_("location description stack underflow"));
21ae7a4d
JK
22998 return 0;
22999 }
23000 }
23001 return (stack[stacki]);
c906108c
SS
23002}
23003
23004/* memory allocation interface */
23005
c906108c 23006static struct dwarf_block *
7b5a2f43 23007dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23008{
8d749320 23009 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23010}
23011
c906108c 23012static struct die_info *
b60c80d6 23013dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23014{
23015 struct die_info *die;
b60c80d6
DJ
23016 size_t size = sizeof (struct die_info);
23017
23018 if (num_attrs > 1)
23019 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23020
b60c80d6 23021 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23022 memset (die, 0, sizeof (struct die_info));
23023 return (die);
23024}
2e276125
JB
23025
23026\f
a036ba48 23027
c90ec28a 23028/* Macro support. */
cf2c3c16 23029
9eac9650
TT
23030/* An overload of dwarf_decode_macros that finds the correct section
23031 and ensures it is read in before calling the other overload. */
23032
23033static void
23034dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23035 int section_is_gnu)
23036{
23037 struct dwarf2_per_objfile *dwarf2_per_objfile
23038 = cu->per_cu->dwarf2_per_objfile;
23039 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23040 const struct line_header *lh = cu->line_header;
9eac9650
TT
23041 unsigned int offset_size = cu->header.offset_size;
23042 struct dwarf2_section_info *section;
23043 const char *section_name;
23044
23045 if (cu->dwo_unit != nullptr)
23046 {
23047 if (section_is_gnu)
23048 {
23049 section = &cu->dwo_unit->dwo_file->sections.macro;
23050 section_name = ".debug_macro.dwo";
23051 }
23052 else
23053 {
23054 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23055 section_name = ".debug_macinfo.dwo";
23056 }
23057 }
23058 else
23059 {
23060 if (section_is_gnu)
23061 {
23062 section = &dwarf2_per_objfile->macro;
23063 section_name = ".debug_macro";
23064 }
23065 else
23066 {
23067 section = &dwarf2_per_objfile->macinfo;
23068 section_name = ".debug_macinfo";
23069 }
23070 }
23071
23072 section->read (objfile);
23073 if (section->buffer == nullptr)
23074 {
23075 complaint (_("missing %s section"), section_name);
23076 return;
23077 }
23078
23079 buildsym_compunit *builder = cu->get_builder ();
23080
23081 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23082 offset_size, offset, section_is_gnu);
23083}
23084
3019eac3
DE
23085/* Return the .debug_loc section to use for CU.
23086 For DWO files use .debug_loc.dwo. */
23087
23088static struct dwarf2_section_info *
23089cu_debug_loc_section (struct dwarf2_cu *cu)
23090{
518817b3
SM
23091 struct dwarf2_per_objfile *dwarf2_per_objfile
23092 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23093
3019eac3 23094 if (cu->dwo_unit)
43988095
JK
23095 {
23096 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23097
43988095
JK
23098 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23099 }
23100 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23101 : &dwarf2_per_objfile->loc);
3019eac3
DE
23102}
23103
8cf6f0b1
TT
23104/* A helper function that fills in a dwarf2_loclist_baton. */
23105
23106static void
23107fill_in_loclist_baton (struct dwarf2_cu *cu,
23108 struct dwarf2_loclist_baton *baton,
ff39bb5e 23109 const struct attribute *attr)
8cf6f0b1 23110{
518817b3
SM
23111 struct dwarf2_per_objfile *dwarf2_per_objfile
23112 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23113 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23114
96b79293 23115 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23116
23117 baton->per_cu = cu->per_cu;
23118 gdb_assert (baton->per_cu);
23119 /* We don't know how long the location list is, but make sure we
23120 don't run off the edge of the section. */
3019eac3
DE
23121 baton->size = section->size - DW_UNSND (attr);
23122 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23123 baton->base_address = cu->base_address;
f664829e 23124 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23125}
23126
4c2df51b 23127static void
ff39bb5e 23128dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23129 struct dwarf2_cu *cu, int is_block)
4c2df51b 23130{
518817b3
SM
23131 struct dwarf2_per_objfile *dwarf2_per_objfile
23132 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23133 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23134 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23135
cd6c91b4 23136 if (attr->form_is_section_offset ()
3019eac3 23137 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23138 the section. If so, fall through to the complaint in the
23139 other branch. */
2c7d5afc 23140 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23141 {
0d53c4c4 23142 struct dwarf2_loclist_baton *baton;
4c2df51b 23143
8d749320 23144 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23145
8cf6f0b1 23146 fill_in_loclist_baton (cu, baton, attr);
be391dca 23147
d00adf39 23148 if (cu->base_known == 0)
b98664d3 23149 complaint (_("Location list used without "
3e43a32a 23150 "specifying the CU base address."));
4c2df51b 23151
f1e6e072
TT
23152 SYMBOL_ACLASS_INDEX (sym) = (is_block
23153 ? dwarf2_loclist_block_index
23154 : dwarf2_loclist_index);
0d53c4c4
DJ
23155 SYMBOL_LOCATION_BATON (sym) = baton;
23156 }
23157 else
23158 {
23159 struct dwarf2_locexpr_baton *baton;
23160
8d749320 23161 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23162 baton->per_cu = cu->per_cu;
23163 gdb_assert (baton->per_cu);
0d53c4c4 23164
4fc6c0d5 23165 if (attr->form_is_block ())
0d53c4c4
DJ
23166 {
23167 /* Note that we're just copying the block's data pointer
23168 here, not the actual data. We're still pointing into the
6502dd73
DJ
23169 info_buffer for SYM's objfile; right now we never release
23170 that buffer, but when we do clean up properly this may
23171 need to change. */
0d53c4c4
DJ
23172 baton->size = DW_BLOCK (attr)->size;
23173 baton->data = DW_BLOCK (attr)->data;
23174 }
23175 else
23176 {
23177 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23178 sym->natural_name ());
0d53c4c4 23179 baton->size = 0;
0d53c4c4 23180 }
6e70227d 23181
f1e6e072
TT
23182 SYMBOL_ACLASS_INDEX (sym) = (is_block
23183 ? dwarf2_locexpr_block_index
23184 : dwarf2_locexpr_index);
0d53c4c4
DJ
23185 SYMBOL_LOCATION_BATON (sym) = baton;
23186 }
4c2df51b 23187}
6502dd73 23188
09ba997f 23189/* See read.h. */
ae0d2f24
UW
23190
23191struct objfile *
09ba997f 23192dwarf2_per_cu_data::objfile () const
ae0d2f24 23193{
09ba997f 23194 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23195
23196 /* Return the master objfile, so that we can report and look up the
23197 correct file containing this variable. */
23198 if (objfile->separate_debug_objfile_backlink)
23199 objfile = objfile->separate_debug_objfile_backlink;
23200
23201 return objfile;
23202}
23203
96408a79
SA
23204/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23205 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23206 CU_HEADERP first. */
23207
23208static const struct comp_unit_head *
23209per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23210 const struct dwarf2_per_cu_data *per_cu)
96408a79 23211{
d521ce57 23212 const gdb_byte *info_ptr;
96408a79
SA
23213
23214 if (per_cu->cu)
23215 return &per_cu->cu->header;
23216
9c541725 23217 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23218
23219 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23220 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23221 rcuh_kind::COMPILE);
96408a79
SA
23222
23223 return cu_headerp;
23224}
23225
09ba997f 23226/* See read.h. */
ae0d2f24 23227
98714339 23228int
09ba997f 23229dwarf2_per_cu_data::addr_size () const
ae0d2f24 23230{
96408a79
SA
23231 struct comp_unit_head cu_header_local;
23232 const struct comp_unit_head *cu_headerp;
c471e790 23233
09ba997f 23234 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23235
23236 return cu_headerp->addr_size;
ae0d2f24
UW
23237}
23238
09ba997f 23239/* See read.h. */
9eae7c52
TT
23240
23241int
09ba997f 23242dwarf2_per_cu_data::offset_size () const
9eae7c52 23243{
96408a79
SA
23244 struct comp_unit_head cu_header_local;
23245 const struct comp_unit_head *cu_headerp;
9c6c53f7 23246
09ba997f 23247 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23248
23249 return cu_headerp->offset_size;
23250}
23251
09ba997f 23252/* See read.h. */
96408a79
SA
23253
23254int
09ba997f 23255dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23256{
23257 struct comp_unit_head cu_header_local;
23258 const struct comp_unit_head *cu_headerp;
23259
09ba997f 23260 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23261
23262 if (cu_headerp->version == 2)
23263 return cu_headerp->addr_size;
23264 else
23265 return cu_headerp->offset_size;
181cebd4
JK
23266}
23267
09ba997f 23268/* See read.h. */
9aa1f1e3
TT
23269
23270CORE_ADDR
09ba997f 23271dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23272{
09ba997f
TT
23273 struct objfile *objfile = dwarf2_per_objfile->objfile;
23274
23275 return objfile->text_section_offset ();
9aa1f1e3
TT
23276}
23277
09ba997f
TT
23278/* See read.h. */
23279
23280struct type *
23281dwarf2_per_cu_data::addr_type () const
9a49df9d 23282{
09ba997f 23283 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
23284 struct type *void_type = objfile_type (objfile)->builtin_void;
23285 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 23286 int addr_size = this->addr_size ();
9a49df9d
AB
23287
23288 if (TYPE_LENGTH (addr_type) == addr_size)
23289 return addr_type;
23290
09ba997f 23291 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23292 return addr_type;
23293}
23294
22b6cd70
TT
23295/* A helper function for dwarf2_find_containing_comp_unit that returns
23296 the index of the result, and that searches a vector. It will
23297 return a result even if the offset in question does not actually
23298 occur in any CU. This is separate so that it can be unit
23299 tested. */
ae038cb0 23300
22b6cd70
TT
23301static int
23302dwarf2_find_containing_comp_unit
23303 (sect_offset sect_off,
23304 unsigned int offset_in_dwz,
23305 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23306{
ae038cb0
DJ
23307 int low, high;
23308
ae038cb0 23309 low = 0;
22b6cd70 23310 high = all_comp_units.size () - 1;
ae038cb0
DJ
23311 while (high > low)
23312 {
36586728 23313 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23314 int mid = low + (high - low) / 2;
9a619af0 23315
22b6cd70 23316 mid_cu = all_comp_units[mid];
36586728 23317 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23318 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23319 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23320 high = mid;
23321 else
23322 low = mid + 1;
23323 }
23324 gdb_assert (low == high);
22b6cd70
TT
23325 return low;
23326}
23327
23328/* Locate the .debug_info compilation unit from CU's objfile which contains
23329 the DIE at OFFSET. Raises an error on failure. */
23330
23331static struct dwarf2_per_cu_data *
23332dwarf2_find_containing_comp_unit (sect_offset sect_off,
23333 unsigned int offset_in_dwz,
23334 struct dwarf2_per_objfile *dwarf2_per_objfile)
23335{
23336 int low
23337 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23338 dwarf2_per_objfile->all_comp_units);
23339 struct dwarf2_per_cu_data *this_cu
23340 = dwarf2_per_objfile->all_comp_units[low];
23341
45b8ae0c 23342 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23343 {
36586728 23344 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23345 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23346 "offset %s [in module %s]"),
23347 sect_offset_str (sect_off),
ed2dc618 23348 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23349
9c541725
PA
23350 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23351 <= sect_off);
ae038cb0
DJ
23352 return dwarf2_per_objfile->all_comp_units[low-1];
23353 }
23354 else
23355 {
b76e467d 23356 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 23357 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23358 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23359 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23360 return this_cu;
23361 }
23362}
23363
22b6cd70
TT
23364#if GDB_SELF_TEST
23365
23366namespace selftests {
23367namespace find_containing_comp_unit {
23368
23369static void
23370run_test ()
23371{
23372 struct dwarf2_per_cu_data one {};
23373 struct dwarf2_per_cu_data two {};
23374 struct dwarf2_per_cu_data three {};
23375 struct dwarf2_per_cu_data four {};
23376
23377 one.length = 5;
23378 two.sect_off = sect_offset (one.length);
23379 two.length = 7;
23380
23381 three.length = 5;
23382 three.is_dwz = 1;
23383 four.sect_off = sect_offset (three.length);
23384 four.length = 7;
23385 four.is_dwz = 1;
23386
23387 std::vector<dwarf2_per_cu_data *> units;
23388 units.push_back (&one);
23389 units.push_back (&two);
23390 units.push_back (&three);
23391 units.push_back (&four);
23392
23393 int result;
23394
23395 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23396 SELF_CHECK (units[result] == &one);
23397 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23398 SELF_CHECK (units[result] == &one);
23399 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23400 SELF_CHECK (units[result] == &two);
23401
23402 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23403 SELF_CHECK (units[result] == &three);
23404 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23405 SELF_CHECK (units[result] == &three);
23406 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23407 SELF_CHECK (units[result] == &four);
23408}
23409
23410}
23411}
23412
23413#endif /* GDB_SELF_TEST */
23414
23745b47 23415/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23416
fcd3b13d
SM
23417dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23418 : per_cu (per_cu_),
9068261f
AB
23419 mark (false),
23420 has_loclist (false),
23421 checked_producer (false),
23422 producer_is_gxx_lt_4_6 (false),
23423 producer_is_gcc_lt_4_3 (false),
eb77c9df 23424 producer_is_icc (false),
9068261f 23425 producer_is_icc_lt_14 (false),
c258c396 23426 producer_is_codewarrior (false),
9068261f 23427 processing_has_namespace_info (false)
93311388 23428{
fcd3b13d
SM
23429 per_cu->cu = this;
23430}
23431
23432/* Destroy a dwarf2_cu. */
23433
23434dwarf2_cu::~dwarf2_cu ()
23435{
23436 per_cu->cu = NULL;
9816fde3
JK
23437}
23438
23439/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23440
23441static void
95554aad
TT
23442prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23443 enum language pretend_language)
9816fde3
JK
23444{
23445 struct attribute *attr;
23446
23447 /* Set the language we're debugging. */
23448 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23449 if (attr != nullptr)
9816fde3
JK
23450 set_cu_language (DW_UNSND (attr), cu);
23451 else
9cded63f 23452 {
95554aad 23453 cu->language = pretend_language;
9cded63f
TT
23454 cu->language_defn = language_def (cu->language);
23455 }
dee91e82 23456
7d45c7c3 23457 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23458}
23459
ae038cb0
DJ
23460/* Increase the age counter on each cached compilation unit, and free
23461 any that are too old. */
23462
23463static void
ed2dc618 23464age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23465{
23466 struct dwarf2_per_cu_data *per_cu, **last_chain;
23467
23468 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23469 per_cu = dwarf2_per_objfile->read_in_chain;
23470 while (per_cu != NULL)
23471 {
23472 per_cu->cu->last_used ++;
b4f54984 23473 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23474 dwarf2_mark (per_cu->cu);
23475 per_cu = per_cu->cu->read_in_chain;
23476 }
23477
23478 per_cu = dwarf2_per_objfile->read_in_chain;
23479 last_chain = &dwarf2_per_objfile->read_in_chain;
23480 while (per_cu != NULL)
23481 {
23482 struct dwarf2_per_cu_data *next_cu;
23483
23484 next_cu = per_cu->cu->read_in_chain;
23485
23486 if (!per_cu->cu->mark)
23487 {
fcd3b13d 23488 delete per_cu->cu;
ae038cb0
DJ
23489 *last_chain = next_cu;
23490 }
23491 else
23492 last_chain = &per_cu->cu->read_in_chain;
23493
23494 per_cu = next_cu;
23495 }
23496}
23497
23498/* Remove a single compilation unit from the cache. */
23499
23500static void
dee91e82 23501free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23502{
23503 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23504 struct dwarf2_per_objfile *dwarf2_per_objfile
23505 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
23506
23507 per_cu = dwarf2_per_objfile->read_in_chain;
23508 last_chain = &dwarf2_per_objfile->read_in_chain;
23509 while (per_cu != NULL)
23510 {
23511 struct dwarf2_per_cu_data *next_cu;
23512
23513 next_cu = per_cu->cu->read_in_chain;
23514
dee91e82 23515 if (per_cu == target_per_cu)
ae038cb0 23516 {
fcd3b13d 23517 delete per_cu->cu;
dee91e82 23518 per_cu->cu = NULL;
ae038cb0
DJ
23519 *last_chain = next_cu;
23520 break;
23521 }
23522 else
23523 last_chain = &per_cu->cu->read_in_chain;
23524
23525 per_cu = next_cu;
23526 }
23527}
23528
dee91e82
DE
23529/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23530 We store these in a hash table separate from the DIEs, and preserve them
23531 when the DIEs are flushed out of cache.
23532
23533 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23534 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23535 or the type may come from a DWO file. Furthermore, while it's more logical
23536 to use per_cu->section+offset, with Fission the section with the data is in
23537 the DWO file but we don't know that section at the point we need it.
23538 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23539 because we can enter the lookup routine, get_die_type_at_offset, from
23540 outside this file, and thus won't necessarily have PER_CU->cu.
23541 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23542
dee91e82 23543struct dwarf2_per_cu_offset_and_type
1c379e20 23544{
dee91e82 23545 const struct dwarf2_per_cu_data *per_cu;
9c541725 23546 sect_offset sect_off;
1c379e20
DJ
23547 struct type *type;
23548};
23549
dee91e82 23550/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23551
23552static hashval_t
dee91e82 23553per_cu_offset_and_type_hash (const void *item)
1c379e20 23554{
9a3c8263
SM
23555 const struct dwarf2_per_cu_offset_and_type *ofs
23556 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23557
9c541725 23558 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23559}
23560
dee91e82 23561/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23562
23563static int
dee91e82 23564per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23565{
9a3c8263
SM
23566 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23567 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23568 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23569 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23570
dee91e82 23571 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23572 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23573}
23574
23575/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23576 table if necessary. For convenience, return TYPE.
23577
23578 The DIEs reading must have careful ordering to:
85102364 23579 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23580 reading current DIE.
23581 * Not trying to dereference contents of still incompletely read in types
23582 while reading in other DIEs.
23583 * Enable referencing still incompletely read in types just by a pointer to
23584 the type without accessing its fields.
23585
23586 Therefore caller should follow these rules:
23587 * Try to fetch any prerequisite types we may need to build this DIE type
23588 before building the type and calling set_die_type.
e71ec853 23589 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23590 possible before fetching more types to complete the current type.
23591 * Make the type as complete as possible before fetching more types. */
1c379e20 23592
f792889a 23593static struct type *
1c379e20
DJ
23594set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23595{
518817b3
SM
23596 struct dwarf2_per_objfile *dwarf2_per_objfile
23597 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23598 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23599 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23600 struct attribute *attr;
23601 struct dynamic_prop prop;
1c379e20 23602
b4ba55a1
JB
23603 /* For Ada types, make sure that the gnat-specific data is always
23604 initialized (if not already set). There are a few types where
23605 we should not be doing so, because the type-specific area is
23606 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23607 where the type-specific area is used to store the floatformat).
23608 But this is not a problem, because the gnat-specific information
23609 is actually not needed for these types. */
23610 if (need_gnat_info (cu)
23611 && TYPE_CODE (type) != TYPE_CODE_FUNC
23612 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23613 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23614 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23615 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23616 && !HAVE_GNAT_AUX_INFO (type))
23617 INIT_GNAT_SPECIFIC (type);
23618
3f2f83dd
KB
23619 /* Read DW_AT_allocated and set in type. */
23620 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23621 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23622 {
09ba997f 23623 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23624 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23625 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
23626 }
23627 else if (attr != NULL)
23628 {
b98664d3 23629 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23630 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23631 sect_offset_str (die->sect_off));
3f2f83dd
KB
23632 }
23633
23634 /* Read DW_AT_associated and set in type. */
23635 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23636 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23637 {
09ba997f 23638 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23639 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23640 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
23641 }
23642 else if (attr != NULL)
23643 {
b98664d3 23644 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23645 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23646 sect_offset_str (die->sect_off));
3f2f83dd
KB
23647 }
23648
3cdcd0ce
JB
23649 /* Read DW_AT_data_location and set in type. */
23650 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23651 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23652 cu->per_cu->addr_type ()))
50a82047 23653 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 23654
dee91e82 23655 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
23656 dwarf2_per_objfile->die_type_hash
23657 = htab_up (htab_create_alloc (127,
23658 per_cu_offset_and_type_hash,
23659 per_cu_offset_and_type_eq,
23660 NULL, xcalloc, xfree));
1c379e20 23661
dee91e82 23662 ofs.per_cu = cu->per_cu;
9c541725 23663 ofs.sect_off = die->sect_off;
1c379e20 23664 ofs.type = type;
dee91e82 23665 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 23666 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23667 if (*slot)
b98664d3 23668 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23669 sect_offset_str (die->sect_off));
8d749320
SM
23670 *slot = XOBNEW (&objfile->objfile_obstack,
23671 struct dwarf2_per_cu_offset_and_type);
1c379e20 23672 **slot = ofs;
f792889a 23673 return type;
1c379e20
DJ
23674}
23675
9c541725 23676/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23677 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23678
23679static struct type *
9c541725 23680get_die_type_at_offset (sect_offset sect_off,
673bfd45 23681 struct dwarf2_per_cu_data *per_cu)
1c379e20 23682{
dee91e82 23683 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23684 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23685
dee91e82 23686 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23687 return NULL;
1c379e20 23688
dee91e82 23689 ofs.per_cu = per_cu;
9c541725 23690 ofs.sect_off = sect_off;
9a3c8263 23691 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 23692 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23693 if (slot)
23694 return slot->type;
23695 else
23696 return NULL;
23697}
23698
02142a6c 23699/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23700 or return NULL if DIE does not have a saved type. */
23701
23702static struct type *
23703get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23704{
9c541725 23705 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23706}
23707
10b3939b
DJ
23708/* Add a dependence relationship from CU to REF_PER_CU. */
23709
23710static void
23711dwarf2_add_dependence (struct dwarf2_cu *cu,
23712 struct dwarf2_per_cu_data *ref_per_cu)
23713{
23714 void **slot;
23715
23716 if (cu->dependencies == NULL)
23717 cu->dependencies
23718 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23719 NULL, &cu->comp_unit_obstack,
23720 hashtab_obstack_allocate,
23721 dummy_obstack_deallocate);
23722
23723 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23724 if (*slot == NULL)
23725 *slot = ref_per_cu;
23726}
1c379e20 23727
f504f079
DE
23728/* Subroutine of dwarf2_mark to pass to htab_traverse.
23729 Set the mark field in every compilation unit in the
ae038cb0
DJ
23730 cache that we must keep because we are keeping CU. */
23731
10b3939b
DJ
23732static int
23733dwarf2_mark_helper (void **slot, void *data)
23734{
23735 struct dwarf2_per_cu_data *per_cu;
23736
23737 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23738
23739 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23740 reading of the chain. As such dependencies remain valid it is not much
23741 useful to track and undo them during QUIT cleanups. */
23742 if (per_cu->cu == NULL)
23743 return 1;
23744
10b3939b
DJ
23745 if (per_cu->cu->mark)
23746 return 1;
9068261f 23747 per_cu->cu->mark = true;
10b3939b
DJ
23748
23749 if (per_cu->cu->dependencies != NULL)
23750 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23751
23752 return 1;
23753}
23754
f504f079
DE
23755/* Set the mark field in CU and in every other compilation unit in the
23756 cache that we must keep because we are keeping CU. */
23757
ae038cb0
DJ
23758static void
23759dwarf2_mark (struct dwarf2_cu *cu)
23760{
23761 if (cu->mark)
23762 return;
9068261f 23763 cu->mark = true;
10b3939b
DJ
23764 if (cu->dependencies != NULL)
23765 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23766}
23767
23768static void
23769dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23770{
23771 while (per_cu)
23772 {
9068261f 23773 per_cu->cu->mark = false;
ae038cb0
DJ
23774 per_cu = per_cu->cu->read_in_chain;
23775 }
72bf9492
DJ
23776}
23777
72bf9492
DJ
23778/* Trivial hash function for partial_die_info: the hash value of a DIE
23779 is its offset in .debug_info for this objfile. */
23780
23781static hashval_t
23782partial_die_hash (const void *item)
23783{
9a3c8263
SM
23784 const struct partial_die_info *part_die
23785 = (const struct partial_die_info *) item;
9a619af0 23786
9c541725 23787 return to_underlying (part_die->sect_off);
72bf9492
DJ
23788}
23789
23790/* Trivial comparison function for partial_die_info structures: two DIEs
23791 are equal if they have the same offset. */
23792
23793static int
23794partial_die_eq (const void *item_lhs, const void *item_rhs)
23795{
9a3c8263
SM
23796 const struct partial_die_info *part_die_lhs
23797 = (const struct partial_die_info *) item_lhs;
23798 const struct partial_die_info *part_die_rhs
23799 = (const struct partial_die_info *) item_rhs;
9a619af0 23800
9c541725 23801 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23802}
23803
3c3bb058
AB
23804struct cmd_list_element *set_dwarf_cmdlist;
23805struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23806
23807static void
981a3fb3 23808set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 23809{
b4f54984 23810 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23811 gdb_stdout);
ae038cb0
DJ
23812}
23813
23814static void
981a3fb3 23815show_dwarf_cmd (const char *args, int from_tty)
6e70227d 23816{
b4f54984 23817 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23818}
23819
9291a0cd 23820static void
cd4fb1b2
SM
23821show_check_physname (struct ui_file *file, int from_tty,
23822 struct cmd_list_element *c, const char *value)
9291a0cd 23823{
cd4fb1b2
SM
23824 fprintf_filtered (file,
23825 _("Whether to check \"physname\" is %s.\n"),
23826 value);
9291a0cd
TT
23827}
23828
6c265988 23829void _initialize_dwarf2_read ();
cd4fb1b2 23830void
6c265988 23831_initialize_dwarf2_read ()
9291a0cd 23832{
cd4fb1b2
SM
23833 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23834Set DWARF specific variables.\n\
590042fc 23835Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
23836 &set_dwarf_cmdlist, "maintenance set dwarf ",
23837 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23838
cd4fb1b2 23839 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
23840Show DWARF specific variables.\n\
23841Show DWARF variables such as the cache size."),
cd4fb1b2
SM
23842 &show_dwarf_cmdlist, "maintenance show dwarf ",
23843 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23844
cd4fb1b2
SM
23845 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23846 &dwarf_max_cache_age, _("\
23847Set the upper bound on the age of cached DWARF compilation units."), _("\
23848Show the upper bound on the age of cached DWARF compilation units."), _("\
23849A higher limit means that cached compilation units will be stored\n\
23850in memory longer, and more total memory will be used. Zero disables\n\
23851caching, which can slow down startup."),
23852 NULL,
23853 show_dwarf_max_cache_age,
23854 &set_dwarf_cmdlist,
23855 &show_dwarf_cmdlist);
156942c7 23856
cd4fb1b2
SM
23857 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23858Set debugging of the DWARF reader."), _("\
23859Show debugging of the DWARF reader."), _("\
23860When enabled (non-zero), debugging messages are printed during DWARF\n\
23861reading and symtab expansion. A value of 1 (one) provides basic\n\
23862information. A value greater than 1 provides more verbose information."),
23863 NULL,
23864 NULL,
23865 &setdebuglist, &showdebuglist);
9291a0cd 23866
cd4fb1b2
SM
23867 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23868Set debugging of the DWARF DIE reader."), _("\
23869Show debugging of the DWARF DIE reader."), _("\
23870When enabled (non-zero), DIEs are dumped after they are read in.\n\
23871The value is the maximum depth to print."),
23872 NULL,
23873 NULL,
23874 &setdebuglist, &showdebuglist);
9291a0cd 23875
cd4fb1b2
SM
23876 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23877Set debugging of the dwarf line reader."), _("\
23878Show debugging of the dwarf line reader."), _("\
23879When enabled (non-zero), line number entries are dumped as they are read in.\n\
23880A value of 1 (one) provides basic information.\n\
23881A value greater than 1 provides more verbose information."),
23882 NULL,
23883 NULL,
23884 &setdebuglist, &showdebuglist);
437afbb8 23885
cd4fb1b2
SM
23886 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23887Set cross-checking of \"physname\" code against demangler."), _("\
23888Show cross-checking of \"physname\" code against demangler."), _("\
23889When enabled, GDB's internal \"physname\" code is checked against\n\
23890the demangler."),
23891 NULL, show_check_physname,
23892 &setdebuglist, &showdebuglist);
900e11f9 23893
e615022a
DE
23894 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23895 no_class, &use_deprecated_index_sections, _("\
23896Set whether to use deprecated gdb_index sections."), _("\
23897Show whether to use deprecated gdb_index sections."), _("\
23898When enabled, deprecated .gdb_index sections are used anyway.\n\
23899Normally they are ignored either because of a missing feature or\n\
23900performance issue.\n\
23901Warning: This option must be enabled before gdb reads the file."),
23902 NULL,
23903 NULL,
23904 &setlist, &showlist);
23905
f1e6e072
TT
23906 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23907 &dwarf2_locexpr_funcs);
23908 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23909 &dwarf2_loclist_funcs);
23910
23911 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23912 &dwarf2_block_frame_base_locexpr_funcs);
23913 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23914 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23915
23916#if GDB_SELF_TEST
23917 selftests::register_test ("dw2_expand_symtabs_matching",
23918 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23919 selftests::register_test ("dwarf2_find_containing_comp_unit",
23920 selftests::find_containing_comp_unit::run_test);
c62446b1 23921#endif
6502dd73 23922}
This page took 5.184161 seconds and 4 git commands to generate.