Split dwarf_decode_macros into two overloads
[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"
4de283e4
TT
41#include "bfd.h"
42#include "elf-bfd.h"
43#include "symtab.h"
44#include "gdbtypes.h"
45#include "objfiles.h"
d55e5aa6 46#include "dwarf2.h"
4de283e4
TT
47#include "buildsym.h"
48#include "demangle.h"
49#include "gdb-demangle.h"
4de283e4
TT
50#include "filenames.h" /* for DOSish file names */
51#include "macrotab.h"
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
SM
1254static const char *read_indirect_line_string
1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1256 const struct comp_unit_head *, unsigned int *);
36586728 1257
ed2dc618
SM
1258static const char *read_indirect_string_at_offset
1259 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1260 LONGEST str_offset);
927aa2e7 1261
d521ce57
TT
1262static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1263 const gdb_byte *,
3019eac3
DE
1264 unsigned int *);
1265
18a8505e
AT
1266static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1267 ULONGEST str_index);
1268
1269static const char *read_stub_str_index (struct dwarf2_cu *cu,
1270 ULONGEST str_index);
3019eac3 1271
e142c38c 1272static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1273
e142c38c
DJ
1274static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1275 struct dwarf2_cu *);
c906108c 1276
348e048f 1277static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1278 unsigned int);
348e048f 1279
7d45c7c3
KB
1280static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1281 struct dwarf2_cu *cu);
1282
a084a2a6
AT
1283static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1284
05cf31d1
JB
1285static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1286 struct dwarf2_cu *cu);
1287
e142c38c 1288static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1289
e142c38c 1290static struct die_info *die_specification (struct die_info *die,
f2f0e013 1291 struct dwarf2_cu **);
63d06c5c 1292
9c541725 1293static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1294 struct dwarf2_cu *cu);
debd256d 1295
f3f5162e 1296static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1297 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1298 CORE_ADDR, int decode_mapping);
c906108c 1299
804d2729
TT
1300static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1301 const char *);
c906108c 1302
a14ed312 1303static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1304 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1305
ff39bb5e 1306static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1307 struct dwarf2_cu *);
c906108c 1308
ff39bb5e 1309static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1310 struct type *type,
1311 const char *name,
1312 struct obstack *obstack,
12df843f 1313 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1314 const gdb_byte **bytes,
98bfdba5 1315 struct dwarf2_locexpr_baton **baton);
2df3850c 1316
e7c27a73 1317static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1318
b4ba55a1
JB
1319static int need_gnat_info (struct dwarf2_cu *);
1320
3e43a32a
MS
1321static struct type *die_descriptive_type (struct die_info *,
1322 struct dwarf2_cu *);
b4ba55a1
JB
1323
1324static void set_descriptive_type (struct type *, struct die_info *,
1325 struct dwarf2_cu *);
1326
e7c27a73
DJ
1327static struct type *die_containing_type (struct die_info *,
1328 struct dwarf2_cu *);
c906108c 1329
ff39bb5e 1330static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1331 struct dwarf2_cu *);
c906108c 1332
f792889a 1333static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1334
673bfd45
DE
1335static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1336
0d5cff50 1337static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1338
6e70227d 1339static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1340 const char *suffix, int physname,
1341 struct dwarf2_cu *cu);
63d06c5c 1342
e7c27a73 1343static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1344
348e048f
DE
1345static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1346
e7c27a73 1347static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1348
e7c27a73 1349static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1350
96408a79
SA
1351static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1352
71a3c369
TT
1353static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1354
ff013f42 1355static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1356 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1357
3a2b436a 1358/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1359 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1360enum pc_bounds_kind
1361{
e385593e 1362 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1363 PC_BOUNDS_NOT_PRESENT,
1364
e385593e
JK
1365 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1366 were present but they do not form a valid range of PC addresses. */
1367 PC_BOUNDS_INVALID,
1368
3a2b436a
JK
1369 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1370 PC_BOUNDS_RANGES,
1371
1372 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1373 PC_BOUNDS_HIGH_LOW,
1374};
1375
1376static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1377 CORE_ADDR *, CORE_ADDR *,
1378 struct dwarf2_cu *,
891813be 1379 dwarf2_psymtab *);
c906108c 1380
fae299cd
DC
1381static void get_scope_pc_bounds (struct die_info *,
1382 CORE_ADDR *, CORE_ADDR *,
1383 struct dwarf2_cu *);
1384
801e3a5b
JB
1385static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1386 CORE_ADDR, struct dwarf2_cu *);
1387
a14ed312 1388static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1389 struct dwarf2_cu *);
c906108c 1390
a14ed312 1391static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1392 struct type *, struct dwarf2_cu *);
c906108c 1393
a14ed312 1394static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1395 struct die_info *, struct type *,
e7c27a73 1396 struct dwarf2_cu *);
c906108c 1397
a14ed312 1398static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1399 struct type *,
1400 struct dwarf2_cu *);
c906108c 1401
134d01f1 1402static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1403
e7c27a73 1404static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1405
e7c27a73 1406static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1407
5d7cb8df
JK
1408static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1409
804d2729 1410static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1411
27aa8d6a
SW
1412static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1413
74921315
KS
1414static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1415
f55ee35c
JK
1416static struct type *read_module_type (struct die_info *die,
1417 struct dwarf2_cu *cu);
1418
38d518c9 1419static const char *namespace_name (struct die_info *die,
e142c38c 1420 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1421
134d01f1 1422static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1423
e7c27a73 1424static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1425
6e70227d 1426static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1427 struct dwarf2_cu *);
1428
bf6af496 1429static struct die_info *read_die_and_siblings_1
d521ce57 1430 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1431 struct die_info *);
639d11d3 1432
dee91e82 1433static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1434 const gdb_byte *info_ptr,
1435 const gdb_byte **new_info_ptr,
639d11d3
DC
1436 struct die_info *parent);
1437
d521ce57
TT
1438static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1439 struct die_info **, const gdb_byte *,
3e225074 1440 int);
3019eac3 1441
d521ce57 1442static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1443 struct die_info **, const gdb_byte *);
93311388 1444
e7c27a73 1445static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1446
15d034d0 1447static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1448 struct objfile *);
71c25dea 1449
15d034d0 1450static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1451
15d034d0 1452static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1453 struct die_info *die,
1454 struct dwarf2_cu *cu);
1455
ca69b9e6
DE
1456static const char *dwarf2_physname (const char *name, struct die_info *die,
1457 struct dwarf2_cu *cu);
1458
e142c38c 1459static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1460 struct dwarf2_cu **);
9219021c 1461
f39c6ffd 1462static const char *dwarf_tag_name (unsigned int);
c906108c 1463
f39c6ffd 1464static const char *dwarf_attr_name (unsigned int);
c906108c 1465
f39c6ffd 1466static const char *dwarf_form_name (unsigned int);
c906108c 1467
a121b7c1 1468static const char *dwarf_bool_name (unsigned int);
c906108c 1469
f39c6ffd 1470static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1471
f9aca02d 1472static struct die_info *sibling_die (struct die_info *);
c906108c 1473
d97bc12b
DE
1474static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1475
1476static void dump_die_for_error (struct die_info *);
1477
1478static void dump_die_1 (struct ui_file *, int level, int max_level,
1479 struct die_info *);
c906108c 1480
d97bc12b 1481/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1482
51545339 1483static void store_in_ref_table (struct die_info *,
10b3939b 1484 struct dwarf2_cu *);
c906108c 1485
ff39bb5e 1486static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1487
ff39bb5e 1488static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1489
348e048f 1490static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1491 const struct attribute *,
348e048f
DE
1492 struct dwarf2_cu **);
1493
10b3939b 1494static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1495 const struct attribute *,
f2f0e013 1496 struct dwarf2_cu **);
c906108c 1497
348e048f 1498static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1499 const struct attribute *,
348e048f
DE
1500 struct dwarf2_cu **);
1501
ac9ec31b
DE
1502static struct type *get_signatured_type (struct die_info *, ULONGEST,
1503 struct dwarf2_cu *);
1504
1505static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1506 const struct attribute *,
ac9ec31b
DE
1507 struct dwarf2_cu *);
1508
e5fe5e75 1509static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1510
52dc124a 1511static void read_signatured_type (struct signatured_type *);
348e048f 1512
63e43d3a
PMR
1513static int attr_to_dynamic_prop (const struct attribute *attr,
1514 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1515 struct dynamic_prop *prop, struct type *type);
63e43d3a 1516
c906108c
SS
1517/* memory allocation interface */
1518
7b5a2f43 1519static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1520
b60c80d6 1521static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1522
43f3e411 1523static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1524
8cf6f0b1
TT
1525static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1526 struct dwarf2_loclist_baton *baton,
ff39bb5e 1527 const struct attribute *attr);
8cf6f0b1 1528
ff39bb5e 1529static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1530 struct symbol *sym,
f1e6e072
TT
1531 struct dwarf2_cu *cu,
1532 int is_block);
4c2df51b 1533
d521ce57
TT
1534static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1535 const gdb_byte *info_ptr,
1536 struct abbrev_info *abbrev);
4bb7a0a7 1537
72bf9492
DJ
1538static hashval_t partial_die_hash (const void *item);
1539
1540static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1541
ae038cb0 1542static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1543 (sect_offset sect_off, unsigned int offset_in_dwz,
1544 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1545
9816fde3 1546static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1547 struct die_info *comp_unit_die,
1548 enum language pretend_language);
93311388 1549
ed2dc618 1550static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1551
dee91e82 1552static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1553
f792889a
DJ
1554static struct type *set_die_type (struct die_info *, struct type *,
1555 struct dwarf2_cu *);
1c379e20 1556
ed2dc618 1557static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1558
ed2dc618 1559static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1560
58f0c718 1561static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1562 enum language);
10b3939b 1563
95554aad
TT
1564static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1565 enum language);
10b3939b 1566
f4dc4d17
DE
1567static void process_full_type_unit (struct dwarf2_per_cu_data *,
1568 enum language);
1569
10b3939b
DJ
1570static void dwarf2_add_dependence (struct dwarf2_cu *,
1571 struct dwarf2_per_cu_data *);
1572
ae038cb0
DJ
1573static void dwarf2_mark (struct dwarf2_cu *);
1574
1575static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1576
b64f50a1 1577static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1578 struct dwarf2_per_cu_data *);
673bfd45 1579
f792889a 1580static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1581
95554aad
TT
1582static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1583 enum language pretend_language);
1584
ed2dc618 1585static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1586
b303c6f6
AB
1587/* Class, the destructor of which frees all allocated queue entries. This
1588 will only have work to do if an error was thrown while processing the
1589 dwarf. If no error was thrown then the queue entries should have all
1590 been processed, and freed, as we went along. */
1591
1592class dwarf2_queue_guard
1593{
1594public:
39856def
TT
1595 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1596 : m_per_objfile (per_objfile)
1597 {
1598 }
b303c6f6
AB
1599
1600 /* Free any entries remaining on the queue. There should only be
1601 entries left if we hit an error while processing the dwarf. */
1602 ~dwarf2_queue_guard ()
1603 {
39856def
TT
1604 /* Ensure that no memory is allocated by the queue. */
1605 std::queue<dwarf2_queue_item> empty;
1606 std::swap (m_per_objfile->queue, empty);
1607 }
b303c6f6 1608
39856def 1609 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1610
39856def
TT
1611private:
1612 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1613};
1614
39856def
TT
1615dwarf2_queue_item::~dwarf2_queue_item ()
1616{
1617 /* Anything still marked queued is likely to be in an
1618 inconsistent state, so discard it. */
1619 if (per_cu->queued)
1620 {
1621 if (per_cu->cu != NULL)
1622 free_one_cached_comp_unit (per_cu);
1623 per_cu->queued = 0;
1624 }
1625}
1626
d721ba37
PA
1627/* The return type of find_file_and_directory. Note, the enclosed
1628 string pointers are only valid while this object is valid. */
1629
1630struct file_and_directory
1631{
1632 /* The filename. This is never NULL. */
1633 const char *name;
1634
1635 /* The compilation directory. NULL if not known. If we needed to
1636 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1637 points directly to the DW_AT_comp_dir string attribute owned by
1638 the obstack that owns the DIE. */
1639 const char *comp_dir;
1640
1641 /* If we needed to build a new string for comp_dir, this is what
1642 owns the storage. */
1643 std::string comp_dir_storage;
1644};
1645
1646static file_and_directory find_file_and_directory (struct die_info *die,
1647 struct dwarf2_cu *cu);
9291a0cd 1648
298e9637 1649static htab_up allocate_signatured_type_table ();
1fd400ff 1650
298e9637 1651static htab_up allocate_dwo_unit_table ();
3019eac3 1652
57d63ce2 1653static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1654 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1655 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1656 ULONGEST signature, int is_debug_types);
a2ce51a0 1657
ed2dc618
SM
1658static struct dwp_file *get_dwp_file
1659 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1660
3019eac3 1661static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1662 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1663
1664static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1665 (struct signatured_type *, const char *, const char *);
3019eac3 1666
89e63ee4
DE
1667static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1668
263db9a1
TT
1669/* A unique pointer to a dwo_file. */
1670
51ac9db5 1671typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1672
ed2dc618 1673static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1674
1b80a9fa 1675static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1676
1677static void free_line_header_voidp (void *arg);
4390d890
DE
1678\f
1679/* Various complaints about symbol reading that don't abort the process. */
1680
1681static void
1682dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1683{
b98664d3 1684 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1685}
1686
1687static void
1688dwarf2_debug_line_missing_file_complaint (void)
1689{
b98664d3 1690 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1691}
1692
1693static void
1694dwarf2_debug_line_missing_end_sequence_complaint (void)
1695{
b98664d3 1696 complaint (_(".debug_line section has line "
4390d890
DE
1697 "program sequence without an end"));
1698}
1699
1700static void
1701dwarf2_complex_location_expr_complaint (void)
1702{
b98664d3 1703 complaint (_("location expression too complex"));
4390d890
DE
1704}
1705
1706static void
1707dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1708 int arg3)
1709{
b98664d3 1710 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1711 arg1, arg2, arg3);
1712}
1713
1714static void
1715dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1716{
b98664d3 1717 complaint (_("debug info runs off end of %s section"
4390d890 1718 " [in module %s]"),
96b79293
TT
1719 section->get_name (),
1720 section->get_file_name ());
4390d890 1721}
1b80a9fa 1722
4390d890
DE
1723static void
1724dwarf2_macro_malformed_definition_complaint (const char *arg1)
1725{
b98664d3 1726 complaint (_("macro debug info contains a "
4390d890
DE
1727 "malformed macro definition:\n`%s'"),
1728 arg1);
1729}
1730
1731static void
1732dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1733{
b98664d3 1734 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1735 arg1, arg2);
1736}
527f3840
JK
1737
1738/* Hash function for line_header_hash. */
1739
1740static hashval_t
1741line_header_hash (const struct line_header *ofs)
1742{
9c541725 1743 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1744}
1745
1746/* Hash function for htab_create_alloc_ex for line_header_hash. */
1747
1748static hashval_t
1749line_header_hash_voidp (const void *item)
1750{
9a3c8263 1751 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1752
1753 return line_header_hash (ofs);
1754}
1755
1756/* Equality function for line_header_hash. */
1757
1758static int
1759line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1760{
9a3c8263
SM
1761 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1762 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1763
9c541725 1764 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1765 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1766}
1767
4390d890 1768\f
9291a0cd 1769
330cdd98
PA
1770/* See declaration. */
1771
1772dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1773 const dwarf2_debug_sections *names,
1774 bool can_copy_)
1775 : objfile (objfile_),
1776 can_copy (can_copy_)
330cdd98
PA
1777{
1778 if (names == NULL)
1779 names = &dwarf2_elf_names;
1780
1781 bfd *obfd = objfile->obfd;
1782
1783 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1784 locate_sections (obfd, sec, *names);
1785}
1786
1787dwarf2_per_objfile::~dwarf2_per_objfile ()
1788{
1789 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1790 free_cached_comp_units ();
1791
b76e467d 1792 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1793 per_cu->imported_symtabs_free ();
fc8e7e75 1794
b2bdb8cf 1795 for (signatured_type *sig_type : all_type_units)
ae640021 1796 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1797
330cdd98
PA
1798 /* Everything else should be on the objfile obstack. */
1799}
1800
1801/* See declaration. */
1802
1803void
1804dwarf2_per_objfile::free_cached_comp_units ()
1805{
1806 dwarf2_per_cu_data *per_cu = read_in_chain;
1807 dwarf2_per_cu_data **last_chain = &read_in_chain;
1808 while (per_cu != NULL)
1809 {
1810 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1811
fcd3b13d 1812 delete per_cu->cu;
330cdd98
PA
1813 *last_chain = next_cu;
1814 per_cu = next_cu;
1815 }
1816}
1817
11ed8cad
TT
1818/* A helper class that calls free_cached_comp_units on
1819 destruction. */
1820
1821class free_cached_comp_units
1822{
1823public:
1824
1825 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1826 : m_per_objfile (per_objfile)
1827 {
1828 }
1829
1830 ~free_cached_comp_units ()
1831 {
1832 m_per_objfile->free_cached_comp_units ();
1833 }
1834
1835 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1836
1837private:
1838
1839 dwarf2_per_objfile *m_per_objfile;
1840};
1841
c906108c 1842/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1843 information and return true if we have enough to do something.
1844 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1845 ELF names are used. CAN_COPY is true for formats where symbol
1846 interposition is possible and so symbol values must follow copy
1847 relocation rules. */
c906108c
SS
1848
1849int
251d32d9 1850dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1851 const struct dwarf2_debug_sections *names,
1852 bool can_copy)
c906108c 1853{
97cbe998
SDJ
1854 if (objfile->flags & OBJF_READNEVER)
1855 return 0;
1856
ed2dc618
SM
1857 struct dwarf2_per_objfile *dwarf2_per_objfile
1858 = get_dwarf2_per_objfile (objfile);
1859
1860 if (dwarf2_per_objfile == NULL)
5bfd760d 1861 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1862 names,
1863 can_copy);
5bfd760d 1864
73869dc2 1865 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1866 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1867 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1868 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1869}
1870
251d32d9
TG
1871/* When loading sections, we look either for uncompressed section or for
1872 compressed section names. */
233a11ab
CS
1873
1874static int
251d32d9
TG
1875section_is_p (const char *section_name,
1876 const struct dwarf2_section_names *names)
233a11ab 1877{
251d32d9
TG
1878 if (names->normal != NULL
1879 && strcmp (section_name, names->normal) == 0)
1880 return 1;
1881 if (names->compressed != NULL
1882 && strcmp (section_name, names->compressed) == 0)
1883 return 1;
1884 return 0;
233a11ab
CS
1885}
1886
330cdd98 1887/* See declaration. */
c906108c 1888
330cdd98
PA
1889void
1890dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1891 const dwarf2_debug_sections &names)
c906108c 1892{
fd361982 1893 flagword aflag = bfd_section_flags (sectp);
251d32d9 1894
dc7650b8
JK
1895 if ((aflag & SEC_HAS_CONTENTS) == 0)
1896 {
1897 }
950b7495
KS
1898 else if (elf_section_data (sectp)->this_hdr.sh_size
1899 > bfd_get_file_size (abfd))
1900 {
1901 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1902 warning (_("Discarding section %s which has a section size (%s"
1903 ") larger than the file size [in module %s]"),
1904 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1905 bfd_get_filename (abfd));
1906 }
330cdd98 1907 else if (section_is_p (sectp->name, &names.info))
c906108c 1908 {
330cdd98 1909 this->info.s.section = sectp;
fd361982 1910 this->info.size = bfd_section_size (sectp);
c906108c 1911 }
330cdd98 1912 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1913 {
330cdd98 1914 this->abbrev.s.section = sectp;
fd361982 1915 this->abbrev.size = bfd_section_size (sectp);
c906108c 1916 }
330cdd98 1917 else if (section_is_p (sectp->name, &names.line))
c906108c 1918 {
330cdd98 1919 this->line.s.section = sectp;
fd361982 1920 this->line.size = bfd_section_size (sectp);
c906108c 1921 }
330cdd98 1922 else if (section_is_p (sectp->name, &names.loc))
c906108c 1923 {
330cdd98 1924 this->loc.s.section = sectp;
fd361982 1925 this->loc.size = bfd_section_size (sectp);
c906108c 1926 }
330cdd98 1927 else if (section_is_p (sectp->name, &names.loclists))
43988095 1928 {
330cdd98 1929 this->loclists.s.section = sectp;
fd361982 1930 this->loclists.size = bfd_section_size (sectp);
43988095 1931 }
330cdd98 1932 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1933 {
330cdd98 1934 this->macinfo.s.section = sectp;
fd361982 1935 this->macinfo.size = bfd_section_size (sectp);
c906108c 1936 }
330cdd98 1937 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1938 {
330cdd98 1939 this->macro.s.section = sectp;
fd361982 1940 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1941 }
330cdd98 1942 else if (section_is_p (sectp->name, &names.str))
c906108c 1943 {
330cdd98 1944 this->str.s.section = sectp;
fd361982 1945 this->str.size = bfd_section_size (sectp);
c906108c 1946 }
18a8505e
AT
1947 else if (section_is_p (sectp->name, &names.str_offsets))
1948 {
1949 this->str_offsets.s.section = sectp;
1950 this->str_offsets.size = bfd_section_size (sectp);
1951 }
330cdd98 1952 else if (section_is_p (sectp->name, &names.line_str))
43988095 1953 {
330cdd98 1954 this->line_str.s.section = sectp;
fd361982 1955 this->line_str.size = bfd_section_size (sectp);
43988095 1956 }
330cdd98 1957 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1958 {
330cdd98 1959 this->addr.s.section = sectp;
fd361982 1960 this->addr.size = bfd_section_size (sectp);
3019eac3 1961 }
330cdd98 1962 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1963 {
330cdd98 1964 this->frame.s.section = sectp;
fd361982 1965 this->frame.size = bfd_section_size (sectp);
b6af0555 1966 }
330cdd98 1967 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1968 {
330cdd98 1969 this->eh_frame.s.section = sectp;
fd361982 1970 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1971 }
330cdd98 1972 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1973 {
330cdd98 1974 this->ranges.s.section = sectp;
fd361982 1975 this->ranges.size = bfd_section_size (sectp);
af34e669 1976 }
330cdd98 1977 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1978 {
330cdd98 1979 this->rnglists.s.section = sectp;
fd361982 1980 this->rnglists.size = bfd_section_size (sectp);
43988095 1981 }
330cdd98 1982 else if (section_is_p (sectp->name, &names.types))
348e048f 1983 {
8b70b953
TT
1984 struct dwarf2_section_info type_section;
1985
1986 memset (&type_section, 0, sizeof (type_section));
049412e3 1987 type_section.s.section = sectp;
fd361982 1988 type_section.size = bfd_section_size (sectp);
8b70b953 1989
fd5866f6 1990 this->types.push_back (type_section);
348e048f 1991 }
330cdd98 1992 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1993 {
330cdd98 1994 this->gdb_index.s.section = sectp;
fd361982 1995 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1996 }
927aa2e7
JK
1997 else if (section_is_p (sectp->name, &names.debug_names))
1998 {
1999 this->debug_names.s.section = sectp;
fd361982 2000 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2001 }
2002 else if (section_is_p (sectp->name, &names.debug_aranges))
2003 {
2004 this->debug_aranges.s.section = sectp;
fd361982 2005 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2006 }
dce234bc 2007
fd361982
AM
2008 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2009 && bfd_section_vma (sectp) == 0)
330cdd98 2010 this->has_section_at_zero = true;
c906108c
SS
2011}
2012
dce234bc 2013/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2014 SECTION_NAME. */
af34e669 2015
dce234bc 2016void
3017a003
TG
2017dwarf2_get_section_info (struct objfile *objfile,
2018 enum dwarf2_section_enum sect,
d521ce57 2019 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2020 bfd_size_type *sizep)
2021{
5bfd760d 2022 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2023 struct dwarf2_section_info *info;
a3b2a86b
TT
2024
2025 /* We may see an objfile without any DWARF, in which case we just
2026 return nothing. */
2027 if (data == NULL)
2028 {
2029 *sectp = NULL;
2030 *bufp = NULL;
2031 *sizep = 0;
2032 return;
2033 }
3017a003
TG
2034 switch (sect)
2035 {
2036 case DWARF2_DEBUG_FRAME:
2037 info = &data->frame;
2038 break;
2039 case DWARF2_EH_FRAME:
2040 info = &data->eh_frame;
2041 break;
2042 default:
2043 gdb_assert_not_reached ("unexpected section");
2044 }
dce234bc 2045
96b79293 2046 info->read (objfile);
dce234bc 2047
96b79293 2048 *sectp = info->get_bfd_section ();
dce234bc
PP
2049 *bufp = info->buffer;
2050 *sizep = info->size;
2051}
2052
36586728
TT
2053/* A helper function to find the sections for a .dwz file. */
2054
2055static void
2056locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2057{
9a3c8263 2058 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2059
2060 /* Note that we only support the standard ELF names, because .dwz
2061 is ELF-only (at the time of writing). */
2062 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2063 {
049412e3 2064 dwz_file->abbrev.s.section = sectp;
fd361982 2065 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2066 }
2067 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2068 {
049412e3 2069 dwz_file->info.s.section = sectp;
fd361982 2070 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2071 }
2072 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2073 {
049412e3 2074 dwz_file->str.s.section = sectp;
fd361982 2075 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2076 }
2077 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2078 {
049412e3 2079 dwz_file->line.s.section = sectp;
fd361982 2080 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2081 }
2082 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2083 {
049412e3 2084 dwz_file->macro.s.section = sectp;
fd361982 2085 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2086 }
2ec9a5e0
TT
2087 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2088 {
049412e3 2089 dwz_file->gdb_index.s.section = sectp;
fd361982 2090 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2091 }
927aa2e7
JK
2092 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2093 {
2094 dwz_file->debug_names.s.section = sectp;
fd361982 2095 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2096 }
36586728
TT
2097}
2098
c4973306 2099/* See dwarf2read.h. */
36586728 2100
c4973306 2101struct dwz_file *
ed2dc618 2102dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2103{
36586728 2104 const char *filename;
acd13123 2105 bfd_size_type buildid_len_arg;
dc294be5
TT
2106 size_t buildid_len;
2107 bfd_byte *buildid;
36586728
TT
2108
2109 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2110 return dwarf2_per_objfile->dwz_file.get ();
36586728 2111
4db1a1dc 2112 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2113 gdb::unique_xmalloc_ptr<char> data
2114 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2115 &buildid_len_arg, &buildid));
4db1a1dc
TT
2116 if (data == NULL)
2117 {
2118 if (bfd_get_error () == bfd_error_no_error)
2119 return NULL;
2120 error (_("could not read '.gnu_debugaltlink' section: %s"),
2121 bfd_errmsg (bfd_get_error ()));
2122 }
791afaa2
TT
2123
2124 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2125
acd13123
TT
2126 buildid_len = (size_t) buildid_len_arg;
2127
791afaa2 2128 filename = data.get ();
d721ba37
PA
2129
2130 std::string abs_storage;
36586728
TT
2131 if (!IS_ABSOLUTE_PATH (filename))
2132 {
14278e1f
TT
2133 gdb::unique_xmalloc_ptr<char> abs
2134 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2135
14278e1f 2136 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2137 filename = abs_storage.c_str ();
36586728
TT
2138 }
2139
dc294be5
TT
2140 /* First try the file name given in the section. If that doesn't
2141 work, try to use the build-id instead. */
192b62ce 2142 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2143 if (dwz_bfd != NULL)
36586728 2144 {
192b62ce 2145 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2146 dwz_bfd.reset (nullptr);
36586728
TT
2147 }
2148
dc294be5
TT
2149 if (dwz_bfd == NULL)
2150 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2151
0d79cdc4
AM
2152 if (dwz_bfd == nullptr)
2153 {
2154 gdb::unique_xmalloc_ptr<char> alt_filename;
2155 const char *origname = dwarf2_per_objfile->objfile->original_name;
2156
2157 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2158 buildid_len,
2159 origname,
2160 &alt_filename));
2161
2162 if (fd.get () >= 0)
2163 {
2164 /* File successfully retrieved from server. */
2165 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2166
2167 if (dwz_bfd == nullptr)
2168 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2169 alt_filename.get ());
2170 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2171 dwz_bfd.reset (nullptr);
2172 }
2173 }
2174
dc294be5
TT
2175 if (dwz_bfd == NULL)
2176 error (_("could not find '.gnu_debugaltlink' file for %s"),
2177 objfile_name (dwarf2_per_objfile->objfile));
2178
7ff8cb8c
TT
2179 std::unique_ptr<struct dwz_file> result
2180 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2181
7ff8cb8c
TT
2182 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2183 result.get ());
36586728 2184
7ff8cb8c
TT
2185 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2186 result->dwz_bfd.get ());
2187 dwarf2_per_objfile->dwz_file = std::move (result);
2188 return dwarf2_per_objfile->dwz_file.get ();
36586728 2189}
9291a0cd 2190\f
7b9f3c50
DE
2191/* DWARF quick_symbols_functions support. */
2192
2193/* TUs can share .debug_line entries, and there can be a lot more TUs than
2194 unique line tables, so we maintain a separate table of all .debug_line
2195 derived entries to support the sharing.
2196 All the quick functions need is the list of file names. We discard the
2197 line_header when we're done and don't need to record it here. */
2198struct quick_file_names
2199{
094b34ac
DE
2200 /* The data used to construct the hash key. */
2201 struct stmt_list_hash hash;
7b9f3c50
DE
2202
2203 /* The number of entries in file_names, real_names. */
2204 unsigned int num_file_names;
2205
2206 /* The file names from the line table, after being run through
2207 file_full_name. */
2208 const char **file_names;
2209
2210 /* The file names from the line table after being run through
2211 gdb_realpath. These are computed lazily. */
2212 const char **real_names;
2213};
2214
2215/* When using the index (and thus not using psymtabs), each CU has an
2216 object of this type. This is used to hold information needed by
2217 the various "quick" methods. */
2218struct dwarf2_per_cu_quick_data
2219{
2220 /* The file table. This can be NULL if there was no file table
2221 or it's currently not read in.
2222 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2223 struct quick_file_names *file_names;
2224
2225 /* The corresponding symbol table. This is NULL if symbols for this
2226 CU have not yet been read. */
43f3e411 2227 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2228
2229 /* A temporary mark bit used when iterating over all CUs in
2230 expand_symtabs_matching. */
2231 unsigned int mark : 1;
2232
2233 /* True if we've tried to read the file table and found there isn't one.
2234 There will be no point in trying to read it again next time. */
2235 unsigned int no_file_data : 1;
2236};
2237
094b34ac
DE
2238/* Utility hash function for a stmt_list_hash. */
2239
2240static hashval_t
2241hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2242{
2243 hashval_t v = 0;
2244
2245 if (stmt_list_hash->dwo_unit != NULL)
2246 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2247 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2248 return v;
2249}
2250
2251/* Utility equality function for a stmt_list_hash. */
2252
2253static int
2254eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2255 const struct stmt_list_hash *rhs)
2256{
2257 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2258 return 0;
2259 if (lhs->dwo_unit != NULL
2260 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2261 return 0;
2262
9c541725 2263 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2264}
2265
7b9f3c50
DE
2266/* Hash function for a quick_file_names. */
2267
2268static hashval_t
2269hash_file_name_entry (const void *e)
2270{
9a3c8263
SM
2271 const struct quick_file_names *file_data
2272 = (const struct quick_file_names *) e;
7b9f3c50 2273
094b34ac 2274 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2275}
2276
2277/* Equality function for a quick_file_names. */
2278
2279static int
2280eq_file_name_entry (const void *a, const void *b)
2281{
9a3c8263
SM
2282 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2283 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2284
094b34ac 2285 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2286}
2287
2288/* Delete function for a quick_file_names. */
2289
2290static void
2291delete_file_name_entry (void *e)
2292{
9a3c8263 2293 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2294 int i;
2295
2296 for (i = 0; i < file_data->num_file_names; ++i)
2297 {
2298 xfree ((void*) file_data->file_names[i]);
2299 if (file_data->real_names)
2300 xfree ((void*) file_data->real_names[i]);
2301 }
2302
2303 /* The space for the struct itself lives on objfile_obstack,
2304 so we don't free it here. */
2305}
2306
2307/* Create a quick_file_names hash table. */
2308
5895093f 2309static htab_up
7b9f3c50
DE
2310create_quick_file_names_table (unsigned int nr_initial_entries)
2311{
5895093f
TT
2312 return htab_up (htab_create_alloc (nr_initial_entries,
2313 hash_file_name_entry, eq_file_name_entry,
2314 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2315}
9291a0cd 2316
918dd910
JK
2317/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2318 have to be created afterwards. You should call age_cached_comp_units after
2319 processing PER_CU->CU. dw2_setup must have been already called. */
2320
2321static void
58f0c718 2322load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2323{
3019eac3 2324 if (per_cu->is_debug_types)
e5fe5e75 2325 load_full_type_unit (per_cu);
918dd910 2326 else
58f0c718 2327 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2328
cc12ce38
DE
2329 if (per_cu->cu == NULL)
2330 return; /* Dummy CU. */
2dc860c0
DE
2331
2332 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2333}
2334
a0f42c21 2335/* Read in the symbols for PER_CU. */
2fdf6df6 2336
9291a0cd 2337static void
58f0c718 2338dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2339{
ed2dc618 2340 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2341
f4dc4d17
DE
2342 /* Skip type_unit_groups, reading the type units they contain
2343 is handled elsewhere. */
197400e8 2344 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2345 return;
2346
b303c6f6
AB
2347 /* The destructor of dwarf2_queue_guard frees any entries left on
2348 the queue. After this point we're guaranteed to leave this function
2349 with the dwarf queue empty. */
39856def 2350 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2351
95554aad 2352 if (dwarf2_per_objfile->using_index
43f3e411 2353 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2354 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2355 {
2356 queue_comp_unit (per_cu, language_minimal);
58f0c718 2357 load_cu (per_cu, skip_partial);
89e63ee4
DE
2358
2359 /* If we just loaded a CU from a DWO, and we're working with an index
2360 that may badly handle TUs, load all the TUs in that DWO as well.
2361 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2362 if (!per_cu->is_debug_types
cc12ce38 2363 && per_cu->cu != NULL
89e63ee4
DE
2364 && per_cu->cu->dwo_unit != NULL
2365 && dwarf2_per_objfile->index_table != NULL
2366 && dwarf2_per_objfile->index_table->version <= 7
2367 /* DWP files aren't supported yet. */
ed2dc618 2368 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2369 queue_and_load_all_dwo_tus (per_cu);
95554aad 2370 }
9291a0cd 2371
ed2dc618 2372 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2373
2374 /* Age the cache, releasing compilation units that have not
2375 been used recently. */
ed2dc618 2376 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2377}
2378
2379/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2380 the objfile from which this CU came. Returns the resulting symbol
2381 table. */
2fdf6df6 2382
43f3e411 2383static struct compunit_symtab *
58f0c718 2384dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2385{
ed2dc618
SM
2386 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2387
95554aad 2388 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2389 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2390 {
11ed8cad 2391 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2392 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2393 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2394 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2395 }
f194fefb 2396
43f3e411 2397 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2398}
2399
ff4c9fec 2400/* See declaration. */
f4dc4d17 2401
ff4c9fec
SM
2402dwarf2_per_cu_data *
2403dwarf2_per_objfile::get_cutu (int index)
2404{
b76e467d 2405 if (index >= this->all_comp_units.size ())
ff4c9fec 2406 {
b76e467d 2407 index -= this->all_comp_units.size ();
b2bdb8cf 2408 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2409 return &this->all_type_units[index]->per_cu;
2410 }
f4dc4d17 2411
ff4c9fec
SM
2412 return this->all_comp_units[index];
2413}
f4dc4d17 2414
ff4c9fec 2415/* See declaration. */
2fdf6df6 2416
ff4c9fec
SM
2417dwarf2_per_cu_data *
2418dwarf2_per_objfile::get_cu (int index)
1fd400ff 2419{
b76e467d 2420 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2421
ff4c9fec 2422 return this->all_comp_units[index];
f4dc4d17
DE
2423}
2424
ff4c9fec 2425/* See declaration. */
f4dc4d17 2426
ff4c9fec
SM
2427signatured_type *
2428dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2429{
b2bdb8cf 2430 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2431
ff4c9fec 2432 return this->all_type_units[index];
1fd400ff
TT
2433}
2434
4b514bc8
JK
2435/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2436 objfile_obstack, and constructed with the specified field
2437 values. */
2438
2439static dwarf2_per_cu_data *
ed2dc618 2440create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2441 struct dwarf2_section_info *section,
2442 int is_dwz,
2443 sect_offset sect_off, ULONGEST length)
2444{
ed2dc618 2445 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2446 dwarf2_per_cu_data *the_cu
2447 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2448 struct dwarf2_per_cu_data);
2449 the_cu->sect_off = sect_off;
2450 the_cu->length = length;
e3b94546 2451 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2452 the_cu->section = section;
2453 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2454 struct dwarf2_per_cu_quick_data);
2455 the_cu->is_dwz = is_dwz;
2456 return the_cu;
2457}
2458
2ec9a5e0
TT
2459/* A helper for create_cus_from_index that handles a given list of
2460 CUs. */
2fdf6df6 2461
74a0d9f6 2462static void
12359b5e 2463create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2464 const gdb_byte *cu_list, offset_type n_elements,
2465 struct dwarf2_section_info *section,
b76e467d 2466 int is_dwz)
9291a0cd 2467{
12359b5e 2468 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2469 {
74a0d9f6 2470 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2471
2472 sect_offset sect_off
2473 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2474 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2475 cu_list += 2 * 8;
2476
b76e467d 2477 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2478 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2479 sect_off, length);
b76e467d 2480 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2481 }
9291a0cd
TT
2482}
2483
2ec9a5e0 2484/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2485 the CU objects for this objfile. */
2ec9a5e0 2486
74a0d9f6 2487static void
12359b5e 2488create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2489 const gdb_byte *cu_list, offset_type cu_list_elements,
2490 const gdb_byte *dwz_list, offset_type dwz_elements)
2491{
b76e467d
SM
2492 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2493 dwarf2_per_objfile->all_comp_units.reserve
2494 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2495
12359b5e 2496 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2497 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2498
2499 if (dwz_elements == 0)
74a0d9f6 2500 return;
2ec9a5e0 2501
12359b5e
SM
2502 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2503 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2504 &dwz->info, 1);
2ec9a5e0
TT
2505}
2506
1fd400ff 2507/* Create the signatured type hash table from the index. */
673bfd45 2508
74a0d9f6 2509static void
12359b5e
SM
2510create_signatured_type_table_from_index
2511 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2512 struct dwarf2_section_info *section,
2513 const gdb_byte *bytes,
2514 offset_type elements)
1fd400ff 2515{
12359b5e 2516 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2517
b2bdb8cf
SM
2518 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2519 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2520
298e9637 2521 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2522
12359b5e 2523 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2524 {
52dc124a 2525 struct signatured_type *sig_type;
9c541725 2526 ULONGEST signature;
1fd400ff 2527 void **slot;
9c541725 2528 cu_offset type_offset_in_tu;
1fd400ff 2529
74a0d9f6 2530 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2531 sect_offset sect_off
2532 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2533 type_offset_in_tu
2534 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2535 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2536 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2537 bytes += 3 * 8;
2538
52dc124a 2539 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2540 struct signatured_type);
52dc124a 2541 sig_type->signature = signature;
9c541725 2542 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2543 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2544 sig_type->per_cu.section = section;
9c541725 2545 sig_type->per_cu.sect_off = sect_off;
e3b94546 2546 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2547 sig_type->per_cu.v.quick
1fd400ff
TT
2548 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2549 struct dwarf2_per_cu_quick_data);
2550
b0b6a987 2551 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2552 *slot = sig_type;
1fd400ff 2553
b2bdb8cf 2554 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2555 }
2556
b0b6a987 2557 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2558}
2559
927aa2e7
JK
2560/* Create the signatured type hash table from .debug_names. */
2561
2562static void
2563create_signatured_type_table_from_debug_names
ed2dc618 2564 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2565 const mapped_debug_names &map,
2566 struct dwarf2_section_info *section,
2567 struct dwarf2_section_info *abbrev_section)
2568{
ed2dc618
SM
2569 struct objfile *objfile = dwarf2_per_objfile->objfile;
2570
96b79293
TT
2571 section->read (objfile);
2572 abbrev_section->read (objfile);
927aa2e7 2573
b2bdb8cf
SM
2574 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2575 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2576
298e9637 2577 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2578
2579 for (uint32_t i = 0; i < map.tu_count; ++i)
2580 {
2581 struct signatured_type *sig_type;
927aa2e7 2582 void **slot;
927aa2e7
JK
2583
2584 sect_offset sect_off
2585 = (sect_offset) (extract_unsigned_integer
2586 (map.tu_table_reordered + i * map.offset_size,
2587 map.offset_size,
2588 map.dwarf5_byte_order));
2589
2590 comp_unit_head cu_header;
ed2dc618
SM
2591 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2592 abbrev_section,
927aa2e7
JK
2593 section->buffer + to_underlying (sect_off),
2594 rcuh_kind::TYPE);
2595
2596 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2597 struct signatured_type);
2598 sig_type->signature = cu_header.signature;
2599 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2600 sig_type->per_cu.is_debug_types = 1;
2601 sig_type->per_cu.section = section;
2602 sig_type->per_cu.sect_off = sect_off;
e3b94546 2603 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2604 sig_type->per_cu.v.quick
2605 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2606 struct dwarf2_per_cu_quick_data);
2607
b0b6a987 2608 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2609 *slot = sig_type;
2610
b2bdb8cf 2611 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2612 }
2613
b0b6a987 2614 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2615}
2616
9291a0cd
TT
2617/* Read the address map data from the mapped index, and use it to
2618 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2619
9291a0cd 2620static void
ed2dc618
SM
2621create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2622 struct mapped_index *index)
9291a0cd 2623{
ed2dc618 2624 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2625 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2626 const gdb_byte *iter, *end;
9291a0cd 2627 struct addrmap *mutable_map;
9291a0cd
TT
2628 CORE_ADDR baseaddr;
2629
8268c778
PA
2630 auto_obstack temp_obstack;
2631
9291a0cd
TT
2632 mutable_map = addrmap_create_mutable (&temp_obstack);
2633
f00a2de2
PA
2634 iter = index->address_table.data ();
2635 end = iter + index->address_table.size ();
9291a0cd 2636
b3b3bada 2637 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2638
2639 while (iter < end)
2640 {
2641 ULONGEST hi, lo, cu_index;
2642 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2643 iter += 8;
2644 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2645 iter += 8;
2646 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2647 iter += 4;
f652bce2 2648
24a55014 2649 if (lo > hi)
f652bce2 2650 {
b98664d3 2651 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2652 hex_string (lo), hex_string (hi));
24a55014 2653 continue;
f652bce2 2654 }
24a55014 2655
b76e467d 2656 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2657 {
b98664d3 2658 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2659 (unsigned) cu_index);
24a55014 2660 continue;
f652bce2 2661 }
24a55014 2662
79748972
TT
2663 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2664 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2665 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2666 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2667 }
2668
d320c2b5 2669 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2670 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2671}
2672
927aa2e7
JK
2673/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2674 populate the objfile's psymtabs_addrmap. */
2675
2676static void
ed2dc618 2677create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2678 struct dwarf2_section_info *section)
2679{
ed2dc618 2680 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2681 bfd *abfd = objfile->obfd;
2682 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2683 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2684
2685 auto_obstack temp_obstack;
2686 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2687
2688 std::unordered_map<sect_offset,
2689 dwarf2_per_cu_data *,
2690 gdb::hash_enum<sect_offset>>
2691 debug_info_offset_to_per_cu;
b76e467d 2692 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2693 {
927aa2e7
JK
2694 const auto insertpair
2695 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2696 if (!insertpair.second)
2697 {
2698 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2699 "debug_info_offset %s, ignoring .debug_aranges."),
2700 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2701 return;
2702 }
2703 }
2704
96b79293 2705 section->read (objfile);
927aa2e7
JK
2706
2707 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2708
2709 const gdb_byte *addr = section->buffer;
2710
2711 while (addr < section->buffer + section->size)
2712 {
2713 const gdb_byte *const entry_addr = addr;
2714 unsigned int bytes_read;
2715
2716 const LONGEST entry_length = read_initial_length (abfd, addr,
2717 &bytes_read);
2718 addr += bytes_read;
2719
2720 const gdb_byte *const entry_end = addr + entry_length;
2721 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2722 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2723 if (addr + entry_length > section->buffer + section->size)
2724 {
47e3f474 2725 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2726 "length %s exceeds section length %s, "
2727 "ignoring .debug_aranges."),
47e3f474
TV
2728 objfile_name (objfile),
2729 plongest (entry_addr - section->buffer),
927aa2e7
JK
2730 plongest (bytes_read + entry_length),
2731 pulongest (section->size));
2732 return;
2733 }
2734
2735 /* The version number. */
2736 const uint16_t version = read_2_bytes (abfd, addr);
2737 addr += 2;
2738 if (version != 2)
2739 {
47e3f474 2740 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2741 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2742 objfile_name (objfile),
2743 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2744 return;
2745 }
2746
2747 const uint64_t debug_info_offset
2748 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2749 addr += offset_size;
2750 const auto per_cu_it
2751 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2752 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2753 {
47e3f474 2754 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2755 "debug_info_offset %s does not exists, "
2756 "ignoring .debug_aranges."),
47e3f474
TV
2757 objfile_name (objfile),
2758 plongest (entry_addr - section->buffer),
927aa2e7
JK
2759 pulongest (debug_info_offset));
2760 return;
2761 }
2762 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2763
2764 const uint8_t address_size = *addr++;
2765 if (address_size < 1 || address_size > 8)
2766 {
47e3f474 2767 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2768 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2769 objfile_name (objfile),
2770 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2771 return;
2772 }
2773
2774 const uint8_t segment_selector_size = *addr++;
2775 if (segment_selector_size != 0)
2776 {
47e3f474 2777 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2778 "segment_selector_size %u is not supported, "
2779 "ignoring .debug_aranges."),
47e3f474
TV
2780 objfile_name (objfile),
2781 plongest (entry_addr - section->buffer),
927aa2e7
JK
2782 segment_selector_size);
2783 return;
2784 }
2785
2786 /* Must pad to an alignment boundary that is twice the address
2787 size. It is undocumented by the DWARF standard but GCC does
2788 use it. */
2789 for (size_t padding = ((-(addr - section->buffer))
2790 & (2 * address_size - 1));
2791 padding > 0; padding--)
2792 if (*addr++ != 0)
2793 {
47e3f474 2794 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2795 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2796 objfile_name (objfile),
2797 plongest (entry_addr - section->buffer));
927aa2e7
JK
2798 return;
2799 }
2800
2801 for (;;)
2802 {
2803 if (addr + 2 * address_size > entry_end)
2804 {
47e3f474 2805 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2806 "address list is not properly terminated, "
2807 "ignoring .debug_aranges."),
47e3f474
TV
2808 objfile_name (objfile),
2809 plongest (entry_addr - section->buffer));
927aa2e7
JK
2810 return;
2811 }
2812 ULONGEST start = extract_unsigned_integer (addr, address_size,
2813 dwarf5_byte_order);
2814 addr += address_size;
2815 ULONGEST length = extract_unsigned_integer (addr, address_size,
2816 dwarf5_byte_order);
2817 addr += address_size;
2818 if (start == 0 && length == 0)
2819 break;
2820 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2821 {
2822 /* Symbol was eliminated due to a COMDAT group. */
2823 continue;
2824 }
2825 ULONGEST end = start + length;
79748972
TT
2826 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2827 - baseaddr);
2828 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2829 - baseaddr);
927aa2e7
JK
2830 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2831 }
2832 }
2833
d320c2b5 2834 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2835 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2836}
2837
9291a0cd
TT
2838/* Find a slot in the mapped index INDEX for the object named NAME.
2839 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2840 constant pool and return true. If NAME cannot be found, return
2841 false. */
2fdf6df6 2842
109483d9 2843static bool
9291a0cd
TT
2844find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2845 offset_type **vec_out)
2846{
0cf03b49 2847 offset_type hash;
9291a0cd 2848 offset_type slot, step;
559a7a62 2849 int (*cmp) (const char *, const char *);
9291a0cd 2850
791afaa2 2851 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2852 if (current_language->la_language == language_cplus
45280282
IB
2853 || current_language->la_language == language_fortran
2854 || current_language->la_language == language_d)
0cf03b49
JK
2855 {
2856 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2857 not contain any. */
a8719064 2858
72998fb3 2859 if (strchr (name, '(') != NULL)
0cf03b49 2860 {
109483d9 2861 without_params = cp_remove_params (name);
0cf03b49 2862
72998fb3 2863 if (without_params != NULL)
791afaa2 2864 name = without_params.get ();
0cf03b49
JK
2865 }
2866 }
2867
559a7a62 2868 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2869 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2870 simulate our NAME being searched is also lowercased. */
2871 hash = mapped_index_string_hash ((index->version == 4
2872 && case_sensitivity == case_sensitive_off
2873 ? 5 : index->version),
2874 name);
2875
f00a2de2
PA
2876 slot = hash & (index->symbol_table.size () - 1);
2877 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2878 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2879
2880 for (;;)
2881 {
9291a0cd 2882 const char *str;
f00a2de2
PA
2883
2884 const auto &bucket = index->symbol_table[slot];
2885 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2886 return false;
9291a0cd 2887
f00a2de2 2888 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2889 if (!cmp (name, str))
9291a0cd
TT
2890 {
2891 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2892 + MAYBE_SWAP (bucket.vec));
109483d9 2893 return true;
9291a0cd
TT
2894 }
2895
f00a2de2 2896 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2897 }
2898}
2899
4485a1c1
SM
2900/* A helper function that reads the .gdb_index from BUFFER and fills
2901 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2902 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2903 ok to use deprecated sections.
2904
2905 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2906 out parameters that are filled in with information about the CU and
2907 TU lists in the section.
2908
4485a1c1 2909 Returns true if all went well, false otherwise. */
2fdf6df6 2910
d33bc52e 2911static bool
4485a1c1
SM
2912read_gdb_index_from_buffer (struct objfile *objfile,
2913 const char *filename,
2914 bool deprecated_ok,
2915 gdb::array_view<const gdb_byte> buffer,
2916 struct mapped_index *map,
2917 const gdb_byte **cu_list,
2918 offset_type *cu_list_elements,
2919 const gdb_byte **types_list,
2920 offset_type *types_list_elements)
2921{
2922 const gdb_byte *addr = &buffer[0];
82430852 2923
9291a0cd 2924 /* Version check. */
4485a1c1 2925 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2926 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2927 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2928 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2929 indices. */
831adc1f 2930 if (version < 4)
481860b3
GB
2931 {
2932 static int warning_printed = 0;
2933 if (!warning_printed)
2934 {
2935 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2936 filename);
481860b3
GB
2937 warning_printed = 1;
2938 }
2939 return 0;
2940 }
2941 /* Index version 4 uses a different hash function than index version
2942 5 and later.
2943
2944 Versions earlier than 6 did not emit psymbols for inlined
2945 functions. Using these files will cause GDB not to be able to
2946 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2947 indices unless the user has done
2948 "set use-deprecated-index-sections on". */
2ec9a5e0 2949 if (version < 6 && !deprecated_ok)
481860b3
GB
2950 {
2951 static int warning_printed = 0;
2952 if (!warning_printed)
2953 {
e615022a
DE
2954 warning (_("\
2955Skipping deprecated .gdb_index section in %s.\n\
2956Do \"set use-deprecated-index-sections on\" before the file is read\n\
2957to use the section anyway."),
2ec9a5e0 2958 filename);
481860b3
GB
2959 warning_printed = 1;
2960 }
2961 return 0;
2962 }
796a7ff8 2963 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2964 of the TU (for symbols coming from TUs),
2965 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2966 Plus gold-generated indices can have duplicate entries for global symbols,
2967 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2968 These are just performance bugs, and we can't distinguish gdb-generated
2969 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2970
481860b3 2971 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2972 longer backward compatible. */
796a7ff8 2973 if (version > 8)
594e8718 2974 return 0;
9291a0cd 2975
559a7a62 2976 map->version = version;
9291a0cd 2977
4485a1c1 2978 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2979
4485a1c1 2980 int i = 0;
2ec9a5e0
TT
2981 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2982 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2983 / 8);
1fd400ff
TT
2984 ++i;
2985
2ec9a5e0
TT
2986 *types_list = addr + MAYBE_SWAP (metadata[i]);
2987 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2988 - MAYBE_SWAP (metadata[i]))
2989 / 8);
987d643c 2990 ++i;
1fd400ff 2991
f00a2de2
PA
2992 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2993 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2994 map->address_table
2995 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2996 ++i;
2997
f00a2de2
PA
2998 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2999 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3000 map->symbol_table
3001 = gdb::array_view<mapped_index::symbol_table_slot>
3002 ((mapped_index::symbol_table_slot *) symbol_table,
3003 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3004
f00a2de2 3005 ++i;
f9d83a0b 3006 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3007
2ec9a5e0
TT
3008 return 1;
3009}
3010
4485a1c1
SM
3011/* Callback types for dwarf2_read_gdb_index. */
3012
3013typedef gdb::function_view
3014 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3015 get_gdb_index_contents_ftype;
3016typedef gdb::function_view
3017 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3018 get_gdb_index_contents_dwz_ftype;
3019
927aa2e7 3020/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3021 elements of all the CUs and return 1. Otherwise, return 0. */
3022
3023static int
4485a1c1
SM
3024dwarf2_read_gdb_index
3025 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3026 get_gdb_index_contents_ftype get_gdb_index_contents,
3027 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3028{
2ec9a5e0
TT
3029 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3030 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3031 struct dwz_file *dwz;
12359b5e 3032 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3033
4485a1c1
SM
3034 gdb::array_view<const gdb_byte> main_index_contents
3035 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3036
3037 if (main_index_contents.empty ())
3038 return 0;
3039
3063847f 3040 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3041 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3042 use_deprecated_index_sections,
3043 main_index_contents, map.get (), &cu_list,
3044 &cu_list_elements, &types_list,
3045 &types_list_elements))
2ec9a5e0
TT
3046 return 0;
3047
0fefef59 3048 /* Don't use the index if it's empty. */
3063847f 3049 if (map->symbol_table.empty ())
0fefef59
DE
3050 return 0;
3051
2ec9a5e0
TT
3052 /* If there is a .dwz file, read it so we can get its CU list as
3053 well. */
ed2dc618 3054 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3055 if (dwz != NULL)
2ec9a5e0 3056 {
2ec9a5e0
TT
3057 struct mapped_index dwz_map;
3058 const gdb_byte *dwz_types_ignore;
3059 offset_type dwz_types_elements_ignore;
3060
4485a1c1
SM
3061 gdb::array_view<const gdb_byte> dwz_index_content
3062 = get_gdb_index_contents_dwz (objfile, dwz);
3063
3064 if (dwz_index_content.empty ())
3065 return 0;
3066
3067 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3068 bfd_get_filename (dwz->dwz_bfd.get ()),
3069 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3070 &dwz_list, &dwz_list_elements,
3071 &dwz_types_ignore,
3072 &dwz_types_elements_ignore))
2ec9a5e0
TT
3073 {
3074 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3075 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3076 return 0;
3077 }
3078 }
3079
12359b5e
SM
3080 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3081 dwz_list, dwz_list_elements);
1fd400ff 3082
8b70b953
TT
3083 if (types_list_elements)
3084 {
8b70b953
TT
3085 /* We can only handle a single .debug_types when we have an
3086 index. */
fd5866f6 3087 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3088 return 0;
3089
fd5866f6 3090 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3091
12359b5e
SM
3092 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3093 types_list, types_list_elements);
8b70b953 3094 }
9291a0cd 3095
3063847f 3096 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3097
3063847f 3098 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3099 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3100 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3101 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3102
3103 return 1;
3104}
3105
dee91e82 3106/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3107
dee91e82
DE
3108static void
3109dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3110 const gdb_byte *info_ptr,
3e225074 3111 struct die_info *comp_unit_die)
9291a0cd 3112{
dee91e82 3113 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3114 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3115 struct dwarf2_per_objfile *dwarf2_per_objfile
3116 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3117 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3118 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3119 struct attribute *attr;
7b9f3c50
DE
3120 void **slot;
3121 struct quick_file_names *qfn;
9291a0cd 3122
0186c6a7
DE
3123 gdb_assert (! this_cu->is_debug_types);
3124
07261596
TT
3125 /* Our callers never want to match partial units -- instead they
3126 will match the enclosing full CU. */
3127 if (comp_unit_die->tag == DW_TAG_partial_unit)
3128 {
3129 this_cu->v.quick->no_file_data = 1;
3130 return;
3131 }
3132
0186c6a7 3133 lh_cu = this_cu;
7b9f3c50 3134 slot = NULL;
dee91e82 3135
fff8551c 3136 line_header_up lh;
9c541725 3137 sect_offset line_offset {};
fff8551c 3138
dee91e82 3139 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3140 if (attr != nullptr)
9291a0cd 3141 {
7b9f3c50
DE
3142 struct quick_file_names find_entry;
3143
9c541725 3144 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3145
3146 /* We may have already read in this line header (TU line header sharing).
3147 If we have we're done. */
094b34ac 3148 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3149 find_entry.hash.line_sect_off = line_offset;
5895093f 3150 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3151 &find_entry, INSERT);
3152 if (*slot != NULL)
3153 {
9a3c8263 3154 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3155 return;
7b9f3c50
DE
3156 }
3157
3019eac3 3158 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3159 }
3160 if (lh == NULL)
3161 {
094b34ac 3162 lh_cu->v.quick->no_file_data = 1;
dee91e82 3163 return;
9291a0cd
TT
3164 }
3165
8d749320 3166 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3167 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3168 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3169 gdb_assert (slot != NULL);
3170 *slot = qfn;
9291a0cd 3171
d721ba37 3172 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3173
aa391654
TT
3174 int offset = 0;
3175 if (strcmp (fnd.name, "<unknown>") != 0)
3176 ++offset;
3177
7ba99d21 3178 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3179 qfn->file_names =
aa391654
TT
3180 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3181 if (offset != 0)
3182 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3183 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3184 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3185 fnd.comp_dir).release ();
7b9f3c50 3186 qfn->real_names = NULL;
9291a0cd 3187
094b34ac 3188 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3189}
3190
3191/* A helper for the "quick" functions which attempts to read the line
3192 table for THIS_CU. */
3193
3194static struct quick_file_names *
e4a48d9d 3195dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3196{
0186c6a7
DE
3197 /* This should never be called for TUs. */
3198 gdb_assert (! this_cu->is_debug_types);
3199 /* Nor type unit groups. */
197400e8 3200 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3201
dee91e82
DE
3202 if (this_cu->v.quick->file_names != NULL)
3203 return this_cu->v.quick->file_names;
3204 /* If we know there is no line data, no point in looking again. */
3205 if (this_cu->v.quick->no_file_data)
3206 return NULL;
3207
c0ab21c2
TT
3208 cutu_reader reader (this_cu);
3209 if (!reader.dummy_p)
3e225074 3210 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3211
3212 if (this_cu->v.quick->no_file_data)
3213 return NULL;
3214 return this_cu->v.quick->file_names;
9291a0cd
TT
3215}
3216
3217/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3218 real path for a given file name from the line table. */
2fdf6df6 3219
9291a0cd 3220static const char *
7b9f3c50
DE
3221dw2_get_real_path (struct objfile *objfile,
3222 struct quick_file_names *qfn, int index)
9291a0cd 3223{
7b9f3c50
DE
3224 if (qfn->real_names == NULL)
3225 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3226 qfn->num_file_names, const char *);
9291a0cd 3227
7b9f3c50 3228 if (qfn->real_names[index] == NULL)
14278e1f 3229 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3230
7b9f3c50 3231 return qfn->real_names[index];
9291a0cd
TT
3232}
3233
3234static struct symtab *
3235dw2_find_last_source_symtab (struct objfile *objfile)
3236{
ed2dc618
SM
3237 struct dwarf2_per_objfile *dwarf2_per_objfile
3238 = get_dwarf2_per_objfile (objfile);
b76e467d 3239 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3240 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3241
43f3e411
DE
3242 if (cust == NULL)
3243 return NULL;
ed2dc618 3244
43f3e411 3245 return compunit_primary_filetab (cust);
9291a0cd
TT
3246}
3247
7b9f3c50
DE
3248/* Traversal function for dw2_forget_cached_source_info. */
3249
3250static int
3251dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3252{
7b9f3c50 3253 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3254
7b9f3c50 3255 if (file_data->real_names)
9291a0cd 3256 {
7b9f3c50 3257 int i;
9291a0cd 3258
7b9f3c50 3259 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3260 {
7b9f3c50
DE
3261 xfree ((void*) file_data->real_names[i]);
3262 file_data->real_names[i] = NULL;
9291a0cd
TT
3263 }
3264 }
7b9f3c50
DE
3265
3266 return 1;
3267}
3268
3269static void
3270dw2_forget_cached_source_info (struct objfile *objfile)
3271{
ed2dc618
SM
3272 struct dwarf2_per_objfile *dwarf2_per_objfile
3273 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3274
5895093f 3275 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3276 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3277}
3278
f8eba3c6
TT
3279/* Helper function for dw2_map_symtabs_matching_filename that expands
3280 the symtabs and calls the iterator. */
3281
3282static int
3283dw2_map_expand_apply (struct objfile *objfile,
3284 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3285 const char *name, const char *real_path,
14bc53a8 3286 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3287{
43f3e411 3288 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3289
3290 /* Don't visit already-expanded CUs. */
43f3e411 3291 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3292 return 0;
3293
3294 /* This may expand more than one symtab, and we want to iterate over
3295 all of them. */
58f0c718 3296 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3297
14bc53a8
PA
3298 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3299 last_made, callback);
f8eba3c6
TT
3300}
3301
3302/* Implementation of the map_symtabs_matching_filename method. */
3303
14bc53a8
PA
3304static bool
3305dw2_map_symtabs_matching_filename
3306 (struct objfile *objfile, const char *name, const char *real_path,
3307 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3308{
c011a4f4 3309 const char *name_basename = lbasename (name);
ed2dc618
SM
3310 struct dwarf2_per_objfile *dwarf2_per_objfile
3311 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3312
848e3e78
DE
3313 /* The rule is CUs specify all the files, including those used by
3314 any TU, so there's no need to scan TUs here. */
f4dc4d17 3315
b76e467d 3316 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3317 {
3d7bb9d9 3318 /* We only need to look at symtabs not already expanded. */
43f3e411 3319 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3320 continue;
3321
b76e467d 3322 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3323 if (file_data == NULL)
9291a0cd
TT
3324 continue;
3325
b76e467d 3326 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3327 {
7b9f3c50 3328 const char *this_name = file_data->file_names[j];
da235a7c 3329 const char *this_real_name;
9291a0cd 3330
af529f8f 3331 if (compare_filenames_for_search (this_name, name))
9291a0cd 3332 {
f5b95b50 3333 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3334 callback))
3335 return true;
288e77a7 3336 continue;
4aac40c8 3337 }
9291a0cd 3338
c011a4f4
DE
3339 /* Before we invoke realpath, which can get expensive when many
3340 files are involved, do a quick comparison of the basenames. */
3341 if (! basenames_may_differ
3342 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3343 continue;
3344
da235a7c
JK
3345 this_real_name = dw2_get_real_path (objfile, file_data, j);
3346 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3347 {
da235a7c 3348 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3349 callback))
3350 return true;
288e77a7 3351 continue;
da235a7c 3352 }
9291a0cd 3353
da235a7c
JK
3354 if (real_path != NULL)
3355 {
af529f8f
JK
3356 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3357 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3358 if (this_real_name != NULL
af529f8f 3359 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3360 {
f5b95b50 3361 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3362 callback))
3363 return true;
288e77a7 3364 continue;
9291a0cd
TT
3365 }
3366 }
3367 }
3368 }
3369
14bc53a8 3370 return false;
9291a0cd
TT
3371}
3372
da51c347
DE
3373/* Struct used to manage iterating over all CUs looking for a symbol. */
3374
3375struct dw2_symtab_iterator
9291a0cd 3376{
ed2dc618
SM
3377 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3378 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3379 /* If set, only look for symbols that match that block. Valid values are
3380 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3381 gdb::optional<block_enum> block_index;
da51c347
DE
3382 /* The kind of symbol we're looking for. */
3383 domain_enum domain;
3384 /* The list of CUs from the index entry of the symbol,
3385 or NULL if not found. */
3386 offset_type *vec;
3387 /* The next element in VEC to look at. */
3388 int next;
3389 /* The number of elements in VEC, or zero if there is no match. */
3390 int length;
8943b874
DE
3391 /* Have we seen a global version of the symbol?
3392 If so we can ignore all further global instances.
3393 This is to work around gold/15646, inefficient gold-generated
3394 indices. */
3395 int global_seen;
da51c347 3396};
9291a0cd 3397
2b79f376 3398/* Initialize the index symtab iterator ITER. */
2fdf6df6 3399
9291a0cd 3400static void
da51c347 3401dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3402 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3403 gdb::optional<block_enum> block_index,
da51c347
DE
3404 domain_enum domain,
3405 const char *name)
3406{
ed2dc618 3407 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3408 iter->block_index = block_index;
3409 iter->domain = domain;
3410 iter->next = 0;
8943b874 3411 iter->global_seen = 0;
da51c347 3412
3063847f 3413 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3414
3415 /* index is NULL if OBJF_READNOW. */
3416 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3417 iter->length = MAYBE_SWAP (*iter->vec);
3418 else
3419 {
3420 iter->vec = NULL;
3421 iter->length = 0;
3422 }
3423}
3424
3425/* Return the next matching CU or NULL if there are no more. */
3426
3427static struct dwarf2_per_cu_data *
3428dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3429{
ed2dc618
SM
3430 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3431
da51c347
DE
3432 for ( ; iter->next < iter->length; ++iter->next)
3433 {
3434 offset_type cu_index_and_attrs =
3435 MAYBE_SWAP (iter->vec[iter->next + 1]);
3436 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3437 gdb_index_symbol_kind symbol_kind =
3438 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3439 /* Only check the symbol attributes if they're present.
3440 Indices prior to version 7 don't record them,
3441 and indices >= 7 may elide them for certain symbols
3442 (gold does this). */
3443 int attrs_valid =
ed2dc618 3444 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3445 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3446
3190f0c6 3447 /* Don't crash on bad data. */
b76e467d 3448 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3449 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3450 {
b98664d3 3451 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3452 " [in module %s]"),
3453 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3454 continue;
3455 }
3456
ff4c9fec 3457 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3458
da51c347 3459 /* Skip if already read in. */
43f3e411 3460 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3461 continue;
3462
8943b874
DE
3463 /* Check static vs global. */
3464 if (attrs_valid)
3465 {
2b79f376
SM
3466 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3467
3468 if (iter->block_index.has_value ())
3469 {
3470 bool want_static = *iter->block_index == STATIC_BLOCK;
3471
3472 if (is_static != want_static)
3473 continue;
3474 }
3475
8943b874
DE
3476 /* Work around gold/15646. */
3477 if (!is_static && iter->global_seen)
3478 continue;
3479 if (!is_static)
3480 iter->global_seen = 1;
3481 }
da51c347
DE
3482
3483 /* Only check the symbol's kind if it has one. */
3484 if (attrs_valid)
3485 {
3486 switch (iter->domain)
3487 {
3488 case VAR_DOMAIN:
3489 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3490 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3491 /* Some types are also in VAR_DOMAIN. */
3492 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3493 continue;
3494 break;
3495 case STRUCT_DOMAIN:
3496 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3497 continue;
3498 break;
3499 case LABEL_DOMAIN:
3500 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3501 continue;
3502 break;
59c35742
AB
3503 case MODULE_DOMAIN:
3504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3505 continue;
3506 break;
da51c347
DE
3507 default:
3508 break;
3509 }
3510 }
3511
3512 ++iter->next;
3513 return per_cu;
3514 }
3515
3516 return NULL;
3517}
3518
43f3e411 3519static struct compunit_symtab *
c7f839cb 3520dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3521 const char *name, domain_enum domain)
9291a0cd 3522{
43f3e411 3523 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3524 struct dwarf2_per_objfile *dwarf2_per_objfile
3525 = get_dwarf2_per_objfile (objfile);
9291a0cd 3526
b5ec771e
PA
3527 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3528
ed2dc618
SM
3529 struct dw2_symtab_iterator iter;
3530 struct dwarf2_per_cu_data *per_cu;
da51c347 3531
2b79f376 3532 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3533
ed2dc618
SM
3534 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3535 {
3536 struct symbol *sym, *with_opaque = NULL;
58f0c718 3537 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3538 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3539 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3540
ed2dc618
SM
3541 sym = block_find_symbol (block, name, domain,
3542 block_find_non_opaque_type_preferred,
3543 &with_opaque);
b2e2f908 3544
ed2dc618
SM
3545 /* Some caution must be observed with overloaded functions
3546 and methods, since the index will not contain any overload
3547 information (but NAME might contain it). */
da51c347 3548
ed2dc618
SM
3549 if (sym != NULL
3550 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3551 return stab;
3552 if (with_opaque != NULL
3553 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3554 stab_best = stab;
da51c347 3555
ed2dc618 3556 /* Keep looking through other CUs. */
9291a0cd 3557 }
9291a0cd 3558
da51c347 3559 return stab_best;
9291a0cd
TT
3560}
3561
3562static void
3563dw2_print_stats (struct objfile *objfile)
3564{
ed2dc618
SM
3565 struct dwarf2_per_objfile *dwarf2_per_objfile
3566 = get_dwarf2_per_objfile (objfile);
b76e467d 3567 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3568 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3569 int count = 0;
9291a0cd 3570
ed2dc618 3571 for (int i = 0; i < total; ++i)
9291a0cd 3572 {
ff4c9fec 3573 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3574
43f3e411 3575 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3576 ++count;
3577 }
e4a48d9d 3578 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3579 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3580}
3581
779bd270
DE
3582/* This dumps minimal information about the index.
3583 It is called via "mt print objfiles".
3584 One use is to verify .gdb_index has been loaded by the
3585 gdb.dwarf2/gdb-index.exp testcase. */
3586
9291a0cd
TT
3587static void
3588dw2_dump (struct objfile *objfile)
3589{
ed2dc618
SM
3590 struct dwarf2_per_objfile *dwarf2_per_objfile
3591 = get_dwarf2_per_objfile (objfile);
3592
779bd270
DE
3593 gdb_assert (dwarf2_per_objfile->using_index);
3594 printf_filtered (".gdb_index:");
3595 if (dwarf2_per_objfile->index_table != NULL)
3596 {
3597 printf_filtered (" version %d\n",
3598 dwarf2_per_objfile->index_table->version);
3599 }
3600 else
3601 printf_filtered (" faked for \"readnow\"\n");
3602 printf_filtered ("\n");
9291a0cd
TT
3603}
3604
9291a0cd
TT
3605static void
3606dw2_expand_symtabs_for_function (struct objfile *objfile,
3607 const char *func_name)
3608{
ed2dc618
SM
3609 struct dwarf2_per_objfile *dwarf2_per_objfile
3610 = get_dwarf2_per_objfile (objfile);
da51c347 3611
ed2dc618
SM
3612 struct dw2_symtab_iterator iter;
3613 struct dwarf2_per_cu_data *per_cu;
da51c347 3614
2b79f376 3615 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3616
ed2dc618 3617 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3618 dw2_instantiate_symtab (per_cu, false);
da51c347 3619
9291a0cd
TT
3620}
3621
3622static void
3623dw2_expand_all_symtabs (struct objfile *objfile)
3624{
ed2dc618
SM
3625 struct dwarf2_per_objfile *dwarf2_per_objfile
3626 = get_dwarf2_per_objfile (objfile);
b76e467d 3627 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3628 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3629
ed2dc618 3630 for (int i = 0; i < total_units; ++i)
9291a0cd 3631 {
ff4c9fec 3632 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3633
58f0c718
TT
3634 /* We don't want to directly expand a partial CU, because if we
3635 read it with the wrong language, then assertion failures can
3636 be triggered later on. See PR symtab/23010. So, tell
3637 dw2_instantiate_symtab to skip partial CUs -- any important
3638 partial CU will be read via DW_TAG_imported_unit anyway. */
3639 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3640 }
3641}
3642
3643static void
652a8996
JK
3644dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3645 const char *fullname)
9291a0cd 3646{
ed2dc618
SM
3647 struct dwarf2_per_objfile *dwarf2_per_objfile
3648 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3649
3650 /* We don't need to consider type units here.
3651 This is only called for examining code, e.g. expand_line_sal.
3652 There can be an order of magnitude (or more) more type units
3653 than comp units, and we avoid them if we can. */
3654
b76e467d 3655 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3656 {
3d7bb9d9 3657 /* We only need to look at symtabs not already expanded. */
43f3e411 3658 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3659 continue;
3660
b76e467d 3661 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3662 if (file_data == NULL)
9291a0cd
TT
3663 continue;
3664
b76e467d 3665 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3666 {
652a8996
JK
3667 const char *this_fullname = file_data->file_names[j];
3668
3669 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3670 {
58f0c718 3671 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3672 break;
3673 }
3674 }
3675 }
3676}
3677
9291a0cd 3678static void
199b4314
TT
3679dw2_map_matching_symbols
3680 (struct objfile *objfile,
b054970d 3681 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3682 int global,
3683 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3684 symbol_compare_ftype *ordered_compare)
9291a0cd 3685{
40658b94 3686 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3687 current language is Ada for a non-Ada objfile using GNU index. As Ada
3688 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3689}
3690
e1ef7d7a
PA
3691/* Starting from a search name, return the string that finds the upper
3692 bound of all strings that start with SEARCH_NAME in a sorted name
3693 list. Returns the empty string to indicate that the upper bound is
3694 the end of the list. */
3695
3696static std::string
3697make_sort_after_prefix_name (const char *search_name)
3698{
3699 /* When looking to complete "func", we find the upper bound of all
3700 symbols that start with "func" by looking for where we'd insert
3701 the closest string that would follow "func" in lexicographical
3702 order. Usually, that's "func"-with-last-character-incremented,
3703 i.e. "fund". Mind non-ASCII characters, though. Usually those
3704 will be UTF-8 multi-byte sequences, but we can't be certain.
3705 Especially mind the 0xff character, which is a valid character in
3706 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3707 rule out compilers allowing it in identifiers. Note that
3708 conveniently, strcmp/strcasecmp are specified to compare
3709 characters interpreted as unsigned char. So what we do is treat
3710 the whole string as a base 256 number composed of a sequence of
3711 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3712 to 0, and carries 1 to the following more-significant position.
3713 If the very first character in SEARCH_NAME ends up incremented
3714 and carries/overflows, then the upper bound is the end of the
3715 list. The string after the empty string is also the empty
3716 string.
3717
3718 Some examples of this operation:
3719
3720 SEARCH_NAME => "+1" RESULT
3721
3722 "abc" => "abd"
3723 "ab\xff" => "ac"
3724 "\xff" "a" "\xff" => "\xff" "b"
3725 "\xff" => ""
3726 "\xff\xff" => ""
3727 "" => ""
3728
3729 Then, with these symbols for example:
3730
3731 func
3732 func1
3733 fund
3734
3735 completing "func" looks for symbols between "func" and
3736 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3737 which finds "func" and "func1", but not "fund".
3738
3739 And with:
3740
3741 funcÿ (Latin1 'ÿ' [0xff])
3742 funcÿ1
3743 fund
3744
3745 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3746 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3747
3748 And with:
3749
3750 ÿÿ (Latin1 'ÿ' [0xff])
3751 ÿÿ1
3752
3753 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3754 the end of the list.
3755 */
3756 std::string after = search_name;
3757 while (!after.empty () && (unsigned char) after.back () == 0xff)
3758 after.pop_back ();
3759 if (!after.empty ())
3760 after.back () = (unsigned char) after.back () + 1;
3761 return after;
3762}
3763
5c58de74 3764/* See declaration. */
61d96d7e 3765
5c58de74
PA
3766std::pair<std::vector<name_component>::const_iterator,
3767 std::vector<name_component>::const_iterator>
44ed8f3e 3768mapped_index_base::find_name_components_bounds
3b00ef10 3769 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3770{
5c58de74
PA
3771 auto *name_cmp
3772 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3773
3b00ef10
TT
3774 const char *lang_name
3775 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3776
3f563c84
PA
3777 /* Comparison function object for lower_bound that matches against a
3778 given symbol name. */
3779 auto lookup_compare_lower = [&] (const name_component &elem,
3780 const char *name)
3781 {
5c58de74 3782 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3783 const char *elem_name = elem_qualified + elem.name_offset;
3784 return name_cmp (elem_name, name) < 0;
3785 };
3786
3787 /* Comparison function object for upper_bound that matches against a
3788 given symbol name. */
3789 auto lookup_compare_upper = [&] (const char *name,
3790 const name_component &elem)
3791 {
5c58de74 3792 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3793 const char *elem_name = elem_qualified + elem.name_offset;
3794 return name_cmp (name, elem_name) < 0;
3795 };
3796
5c58de74
PA
3797 auto begin = this->name_components.begin ();
3798 auto end = this->name_components.end ();
3f563c84
PA
3799
3800 /* Find the lower bound. */
3801 auto lower = [&] ()
3802 {
3b00ef10 3803 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3804 return begin;
3805 else
3b00ef10 3806 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3807 } ();
3808
3809 /* Find the upper bound. */
3810 auto upper = [&] ()
3811 {
5c58de74 3812 if (lookup_name_without_params.completion_mode ())
3f563c84 3813 {
e1ef7d7a
PA
3814 /* In completion mode, we want UPPER to point past all
3815 symbols names that have the same prefix. I.e., with
3816 these symbols, and completing "func":
3817
3818 function << lower bound
3819 function1
3820 other_function << upper bound
3821
3822 We find the upper bound by looking for the insertion
3823 point of "func"-with-last-character-incremented,
3824 i.e. "fund". */
3b00ef10 3825 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3826 if (after.empty ())
3f563c84 3827 return end;
e6b2f5ef
PA
3828 return std::lower_bound (lower, end, after.c_str (),
3829 lookup_compare_lower);
3f563c84
PA
3830 }
3831 else
3b00ef10 3832 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3833 } ();
3834
5c58de74
PA
3835 return {lower, upper};
3836}
3837
3838/* See declaration. */
3839
3840void
44ed8f3e 3841mapped_index_base::build_name_components ()
5c58de74
PA
3842{
3843 if (!this->name_components.empty ())
3844 return;
3845
3846 this->name_components_casing = case_sensitivity;
3847 auto *name_cmp
3848 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3849
3850 /* The code below only knows how to break apart components of C++
3851 symbol names (and other languages that use '::' as
3b00ef10 3852 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3853 auto count = this->symbol_name_count ();
3854 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3855 {
44ed8f3e 3856 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3857 continue;
3858
3859 const char *name = this->symbol_name_at (idx);
3860
3861 /* Add each name component to the name component table. */
3862 unsigned int previous_len = 0;
3b00ef10
TT
3863
3864 if (strstr (name, "::") != nullptr)
3865 {
3866 for (unsigned int current_len = cp_find_first_component (name);
3867 name[current_len] != '\0';
3868 current_len += cp_find_first_component (name + current_len))
3869 {
3870 gdb_assert (name[current_len] == ':');
3871 this->name_components.push_back ({previous_len, idx});
3872 /* Skip the '::'. */
3873 current_len += 2;
3874 previous_len = current_len;
3875 }
3876 }
3877 else
5c58de74 3878 {
3b00ef10
TT
3879 /* Handle the Ada encoded (aka mangled) form here. */
3880 for (const char *iter = strstr (name, "__");
3881 iter != nullptr;
3882 iter = strstr (iter, "__"))
3883 {
3884 this->name_components.push_back ({previous_len, idx});
3885 iter += 2;
3886 previous_len = iter - name;
3887 }
5c58de74 3888 }
3b00ef10 3889
5c58de74
PA
3890 this->name_components.push_back ({previous_len, idx});
3891 }
3892
3893 /* Sort name_components elements by name. */
3894 auto name_comp_compare = [&] (const name_component &left,
3895 const name_component &right)
3896 {
3897 const char *left_qualified = this->symbol_name_at (left.idx);
3898 const char *right_qualified = this->symbol_name_at (right.idx);
3899
3900 const char *left_name = left_qualified + left.name_offset;
3901 const char *right_name = right_qualified + right.name_offset;
3902
3903 return name_cmp (left_name, right_name) < 0;
3904 };
3905
3906 std::sort (this->name_components.begin (),
3907 this->name_components.end (),
3908 name_comp_compare);
3909}
3910
3911/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3912 mapped_index_base instead of the containing objfile. This is split
3913 to a separate function in order to be able to unit test the
3914 name_components matching using a mock mapped_index_base. For each
5c58de74 3915 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3916 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3917
3918static void
3919dw2_expand_symtabs_matching_symbol
44ed8f3e 3920 (mapped_index_base &index,
5c58de74
PA
3921 const lookup_name_info &lookup_name_in,
3922 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3923 enum search_domain kind,
3b00ef10 3924 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3925{
3926 lookup_name_info lookup_name_without_params
3927 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3928
3929 /* Build the symbol name component sorted vector, if we haven't
3930 yet. */
3931 index.build_name_components ();
3932
3f563c84
PA
3933 /* The same symbol may appear more than once in the range though.
3934 E.g., if we're looking for symbols that complete "w", and we have
3935 a symbol named "w1::w2", we'll find the two name components for
3936 that same symbol in the range. To be sure we only call the
3937 callback once per symbol, we first collect the symbol name
3938 indexes that matched in a temporary vector and ignore
3939 duplicates. */
3940 std::vector<offset_type> matches;
3f563c84 3941
3b00ef10
TT
3942 struct name_and_matcher
3943 {
3944 symbol_name_matcher_ftype *matcher;
3945 const std::string &name;
3946
3947 bool operator== (const name_and_matcher &other) const
3f563c84 3948 {
3b00ef10
TT
3949 return matcher == other.matcher && name == other.name;
3950 }
3951 };
3952
3953 /* A vector holding all the different symbol name matchers, for all
3954 languages. */
3955 std::vector<name_and_matcher> matchers;
3956
3957 for (int i = 0; i < nr_languages; i++)
3958 {
3959 enum language lang_e = (enum language) i;
3960
3961 const language_defn *lang = language_def (lang_e);
3962 symbol_name_matcher_ftype *name_matcher
3963 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3964
3b00ef10
TT
3965 name_and_matcher key {
3966 name_matcher,
3967 lookup_name_without_params.language_lookup_name (lang_e)
3968 };
3969
3970 /* Don't insert the same comparison routine more than once.
3971 Note that we do this linear walk. This is not a problem in
3972 practice because the number of supported languages is
3973 low. */
3974 if (std::find (matchers.begin (), matchers.end (), key)
3975 != matchers.end ())
9291a0cd 3976 continue;
3b00ef10
TT
3977 matchers.push_back (std::move (key));
3978
3979 auto bounds
3980 = index.find_name_components_bounds (lookup_name_without_params,
3981 lang_e);
3982
3983 /* Now for each symbol name in range, check to see if we have a name
3984 match, and if so, call the MATCH_CALLBACK callback. */
3985
3986 for (; bounds.first != bounds.second; ++bounds.first)
3987 {
3988 const char *qualified = index.symbol_name_at (bounds.first->idx);
3989
3990 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3991 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3992 continue;
9291a0cd 3993
3b00ef10
TT
3994 matches.push_back (bounds.first->idx);
3995 }
3f563c84
PA
3996 }
3997
3998 std::sort (matches.begin (), matches.end ());
3999
4000 /* Finally call the callback, once per match. */
4001 ULONGEST prev = -1;
4002 for (offset_type idx : matches)
4003 {
4004 if (prev != idx)
4005 {
3b00ef10
TT
4006 if (!match_callback (idx))
4007 break;
3f563c84
PA
4008 prev = idx;
4009 }
4010 }
4011
4012 /* Above we use a type wider than idx's for 'prev', since 0 and
4013 (offset_type)-1 are both possible values. */
4014 static_assert (sizeof (prev) > sizeof (offset_type), "");
4015}
4016
c62446b1
PA
4017#if GDB_SELF_TEST
4018
4019namespace selftests { namespace dw2_expand_symtabs_matching {
4020
a3c5fafd
PA
4021/* A mock .gdb_index/.debug_names-like name index table, enough to
4022 exercise dw2_expand_symtabs_matching_symbol, which works with the
4023 mapped_index_base interface. Builds an index from the symbol list
4024 passed as parameter to the constructor. */
4025class mock_mapped_index : public mapped_index_base
c62446b1
PA
4026{
4027public:
a3c5fafd
PA
4028 mock_mapped_index (gdb::array_view<const char *> symbols)
4029 : m_symbol_table (symbols)
c62446b1
PA
4030 {}
4031
a3c5fafd 4032 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4033
a3c5fafd 4034 /* Return the number of names in the symbol table. */
632e107b 4035 size_t symbol_name_count () const override
c62446b1 4036 {
a3c5fafd 4037 return m_symbol_table.size ();
c62446b1
PA
4038 }
4039
a3c5fafd 4040 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4041 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4042 {
4043 return m_symbol_table[idx];
4044 }
c62446b1 4045
a3c5fafd
PA
4046private:
4047 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4048};
4049
4050/* Convenience function that converts a NULL pointer to a "<null>"
4051 string, to pass to print routines. */
4052
4053static const char *
4054string_or_null (const char *str)
4055{
4056 return str != NULL ? str : "<null>";
4057}
4058
4059/* Check if a lookup_name_info built from
4060 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4061 index. EXPECTED_LIST is the list of expected matches, in expected
4062 matching order. If no match expected, then an empty list is
4063 specified. Returns true on success. On failure prints a warning
4064 indicating the file:line that failed, and returns false. */
4065
4066static bool
4067check_match (const char *file, int line,
4068 mock_mapped_index &mock_index,
4069 const char *name, symbol_name_match_type match_type,
4070 bool completion_mode,
4071 std::initializer_list<const char *> expected_list)
4072{
4073 lookup_name_info lookup_name (name, match_type, completion_mode);
4074
4075 bool matched = true;
4076
4077 auto mismatch = [&] (const char *expected_str,
4078 const char *got)
4079 {
4080 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4081 "expected=\"%s\", got=\"%s\"\n"),
4082 file, line,
4083 (match_type == symbol_name_match_type::FULL
4084 ? "FULL" : "WILD"),
4085 name, string_or_null (expected_str), string_or_null (got));
4086 matched = false;
4087 };
4088
4089 auto expected_it = expected_list.begin ();
4090 auto expected_end = expected_list.end ();
4091
a3c5fafd 4092 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4093 NULL, ALL_DOMAIN,
4094 [&] (offset_type idx)
4095 {
a3c5fafd 4096 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4097 const char *expected_str
4098 = expected_it == expected_end ? NULL : *expected_it++;
4099
4100 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4101 mismatch (expected_str, matched_name);
3b00ef10 4102 return true;
c62446b1
PA
4103 });
4104
4105 const char *expected_str
4106 = expected_it == expected_end ? NULL : *expected_it++;
4107 if (expected_str != NULL)
4108 mismatch (expected_str, NULL);
4109
4110 return matched;
4111}
4112
4113/* The symbols added to the mock mapped_index for testing (in
4114 canonical form). */
4115static const char *test_symbols[] = {
4116 "function",
4117 "std::bar",
4118 "std::zfunction",
4119 "std::zfunction2",
4120 "w1::w2",
4121 "ns::foo<char*>",
4122 "ns::foo<int>",
4123 "ns::foo<long>",
a20714ff
PA
4124 "ns2::tmpl<int>::foo2",
4125 "(anonymous namespace)::A::B::C",
c62446b1 4126
e1ef7d7a
PA
4127 /* These are used to check that the increment-last-char in the
4128 matching algorithm for completion doesn't match "t1_fund" when
4129 completing "t1_func". */
4130 "t1_func",
4131 "t1_func1",
4132 "t1_fund",
4133 "t1_fund1",
4134
4135 /* A UTF-8 name with multi-byte sequences to make sure that
4136 cp-name-parser understands this as a single identifier ("função"
4137 is "function" in PT). */
4138 u8"u8função",
4139
4140 /* \377 (0xff) is Latin1 'ÿ'. */
4141 "yfunc\377",
4142
4143 /* \377 (0xff) is Latin1 'ÿ'. */
4144 "\377",
4145 "\377\377123",
4146
c62446b1
PA
4147 /* A name with all sorts of complications. Starts with "z" to make
4148 it easier for the completion tests below. */
4149#define Z_SYM_NAME \
4150 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4151 "::tuple<(anonymous namespace)::ui*, " \
4152 "std::default_delete<(anonymous namespace)::ui>, void>"
4153
4154 Z_SYM_NAME
4155};
4156
a3c5fafd
PA
4157/* Returns true if the mapped_index_base::find_name_component_bounds
4158 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4159 in completion mode. */
5c58de74
PA
4160
4161static bool
a3c5fafd 4162check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4163 const char *search_name,
4164 gdb::array_view<const char *> expected_syms)
4165{
4166 lookup_name_info lookup_name (search_name,
4167 symbol_name_match_type::FULL, true);
4168
3b00ef10
TT
4169 auto bounds = index.find_name_components_bounds (lookup_name,
4170 language_cplus);
5c58de74
PA
4171
4172 size_t distance = std::distance (bounds.first, bounds.second);
4173 if (distance != expected_syms.size ())
4174 return false;
4175
4176 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4177 {
4178 auto nc_elem = bounds.first + exp_elem;
4179 const char *qualified = index.symbol_name_at (nc_elem->idx);
4180 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4181 return false;
4182 }
4183
4184 return true;
4185}
4186
4187/* Test the lower-level mapped_index::find_name_component_bounds
4188 method. */
4189
c62446b1 4190static void
5c58de74
PA
4191test_mapped_index_find_name_component_bounds ()
4192{
4193 mock_mapped_index mock_index (test_symbols);
4194
a3c5fafd 4195 mock_index.build_name_components ();
5c58de74
PA
4196
4197 /* Test the lower-level mapped_index::find_name_component_bounds
4198 method in completion mode. */
4199 {
4200 static const char *expected_syms[] = {
4201 "t1_func",
4202 "t1_func1",
5c58de74
PA
4203 };
4204
a3c5fafd 4205 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4206 "t1_func", expected_syms));
4207 }
4208
4209 /* Check that the increment-last-char in the name matching algorithm
4210 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4211 {
4212 static const char *expected_syms1[] = {
4213 "\377",
4214 "\377\377123",
4215 };
a3c5fafd 4216 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4217 "\377", expected_syms1));
4218
4219 static const char *expected_syms2[] = {
4220 "\377\377123",
4221 };
a3c5fafd 4222 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4223 "\377\377", expected_syms2));
4224 }
4225}
4226
4227/* Test dw2_expand_symtabs_matching_symbol. */
4228
4229static void
4230test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4231{
4232 mock_mapped_index mock_index (test_symbols);
4233
4234 /* We let all tests run until the end even if some fails, for debug
4235 convenience. */
4236 bool any_mismatch = false;
4237
4238 /* Create the expected symbols list (an initializer_list). Needed
4239 because lists have commas, and we need to pass them to CHECK,
4240 which is a macro. */
4241#define EXPECT(...) { __VA_ARGS__ }
4242
4243 /* Wrapper for check_match that passes down the current
4244 __FILE__/__LINE__. */
4245#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4246 any_mismatch |= !check_match (__FILE__, __LINE__, \
4247 mock_index, \
4248 NAME, MATCH_TYPE, COMPLETION_MODE, \
4249 EXPECTED_LIST)
4250
4251 /* Identity checks. */
4252 for (const char *sym : test_symbols)
4253 {
4254 /* Should be able to match all existing symbols. */
4255 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4256 EXPECT (sym));
4257
4258 /* Should be able to match all existing symbols with
4259 parameters. */
4260 std::string with_params = std::string (sym) + "(int)";
4261 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4262 EXPECT (sym));
4263
4264 /* Should be able to match all existing symbols with
4265 parameters and qualifiers. */
4266 with_params = std::string (sym) + " ( int ) const";
4267 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4268 EXPECT (sym));
4269
4270 /* This should really find sym, but cp-name-parser.y doesn't
4271 know about lvalue/rvalue qualifiers yet. */
4272 with_params = std::string (sym) + " ( int ) &&";
4273 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4274 {});
4275 }
4276
e1ef7d7a
PA
4277 /* Check that the name matching algorithm for completion doesn't get
4278 confused with Latin1 'ÿ' / 0xff. */
4279 {
4280 static const char str[] = "\377";
4281 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4282 EXPECT ("\377", "\377\377123"));
4283 }
4284
4285 /* Check that the increment-last-char in the matching algorithm for
4286 completion doesn't match "t1_fund" when completing "t1_func". */
4287 {
4288 static const char str[] = "t1_func";
4289 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4290 EXPECT ("t1_func", "t1_func1"));
4291 }
4292
c62446b1
PA
4293 /* Check that completion mode works at each prefix of the expected
4294 symbol name. */
4295 {
4296 static const char str[] = "function(int)";
4297 size_t len = strlen (str);
4298 std::string lookup;
4299
4300 for (size_t i = 1; i < len; i++)
4301 {
4302 lookup.assign (str, i);
4303 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4304 EXPECT ("function"));
4305 }
4306 }
4307
4308 /* While "w" is a prefix of both components, the match function
4309 should still only be called once. */
4310 {
4311 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4312 EXPECT ("w1::w2"));
a20714ff
PA
4313 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4314 EXPECT ("w1::w2"));
c62446b1
PA
4315 }
4316
4317 /* Same, with a "complicated" symbol. */
4318 {
4319 static const char str[] = Z_SYM_NAME;
4320 size_t len = strlen (str);
4321 std::string lookup;
4322
4323 for (size_t i = 1; i < len; i++)
4324 {
4325 lookup.assign (str, i);
4326 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4327 EXPECT (Z_SYM_NAME));
4328 }
4329 }
4330
4331 /* In FULL mode, an incomplete symbol doesn't match. */
4332 {
4333 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4334 {});
4335 }
4336
4337 /* A complete symbol with parameters matches any overload, since the
4338 index has no overload info. */
4339 {
4340 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4341 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4342 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4343 EXPECT ("std::zfunction", "std::zfunction2"));
4344 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4345 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4346 }
4347
4348 /* Check that whitespace is ignored appropriately. A symbol with a
4349 template argument list. */
4350 {
4351 static const char expected[] = "ns::foo<int>";
4352 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4353 EXPECT (expected));
a20714ff
PA
4354 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4355 EXPECT (expected));
c62446b1
PA
4356 }
4357
4358 /* Check that whitespace is ignored appropriately. A symbol with a
4359 template argument list that includes a pointer. */
4360 {
4361 static const char expected[] = "ns::foo<char*>";
4362 /* Try both completion and non-completion modes. */
4363 static const bool completion_mode[2] = {false, true};
4364 for (size_t i = 0; i < 2; i++)
4365 {
4366 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4367 completion_mode[i], EXPECT (expected));
a20714ff
PA
4368 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4369 completion_mode[i], EXPECT (expected));
c62446b1
PA
4370
4371 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4372 completion_mode[i], EXPECT (expected));
a20714ff
PA
4373 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4374 completion_mode[i], EXPECT (expected));
c62446b1
PA
4375 }
4376 }
4377
4378 {
4379 /* Check method qualifiers are ignored. */
4380 static const char expected[] = "ns::foo<char*>";
4381 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4382 symbol_name_match_type::FULL, true, EXPECT (expected));
4383 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4384 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4385 CHECK_MATCH ("foo < char * > ( int ) const",
4386 symbol_name_match_type::WILD, true, EXPECT (expected));
4387 CHECK_MATCH ("foo < char * > ( int ) &&",
4388 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4389 }
4390
4391 /* Test lookup names that don't match anything. */
4392 {
a20714ff
PA
4393 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4394 {});
4395
c62446b1
PA
4396 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4397 {});
4398 }
4399
a20714ff
PA
4400 /* Some wild matching tests, exercising "(anonymous namespace)",
4401 which should not be confused with a parameter list. */
4402 {
4403 static const char *syms[] = {
4404 "A::B::C",
4405 "B::C",
4406 "C",
4407 "A :: B :: C ( int )",
4408 "B :: C ( int )",
4409 "C ( int )",
4410 };
4411
4412 for (const char *s : syms)
4413 {
4414 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4415 EXPECT ("(anonymous namespace)::A::B::C"));
4416 }
4417 }
4418
4419 {
4420 static const char expected[] = "ns2::tmpl<int>::foo2";
4421 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4422 EXPECT (expected));
4423 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4424 EXPECT (expected));
4425 }
4426
c62446b1
PA
4427 SELF_CHECK (!any_mismatch);
4428
4429#undef EXPECT
4430#undef CHECK_MATCH
4431}
4432
5c58de74
PA
4433static void
4434run_test ()
4435{
4436 test_mapped_index_find_name_component_bounds ();
4437 test_dw2_expand_symtabs_matching_symbol ();
4438}
4439
c62446b1
PA
4440}} // namespace selftests::dw2_expand_symtabs_matching
4441
4442#endif /* GDB_SELF_TEST */
4443
4b514bc8
JK
4444/* If FILE_MATCHER is NULL or if PER_CU has
4445 dwarf2_per_cu_quick_data::MARK set (see
4446 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4447 EXPANSION_NOTIFY on it. */
4448
4449static void
4450dw2_expand_symtabs_matching_one
4451 (struct dwarf2_per_cu_data *per_cu,
4452 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4453 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4454{
4455 if (file_matcher == NULL || per_cu->v.quick->mark)
4456 {
4457 bool symtab_was_null
4458 = (per_cu->v.quick->compunit_symtab == NULL);
4459
58f0c718 4460 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4461
4462 if (expansion_notify != NULL
4463 && symtab_was_null
4464 && per_cu->v.quick->compunit_symtab != NULL)
4465 expansion_notify (per_cu->v.quick->compunit_symtab);
4466 }
4467}
4468
3f563c84
PA
4469/* Helper for dw2_expand_matching symtabs. Called on each symbol
4470 matched, to expand corresponding CUs that were marked. IDX is the
4471 index of the symbol name that matched. */
4472
4473static void
4474dw2_expand_marked_cus
ed2dc618 4475 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4476 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4477 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4478 search_domain kind)
4479{
3f563c84
PA
4480 offset_type *vec, vec_len, vec_idx;
4481 bool global_seen = false;
ed2dc618 4482 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4483
61920122 4484 vec = (offset_type *) (index.constant_pool
f00a2de2 4485 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4486 vec_len = MAYBE_SWAP (vec[0]);
4487 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4488 {
61920122
PA
4489 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4490 /* This value is only valid for index versions >= 7. */
4491 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4492 gdb_index_symbol_kind symbol_kind =
4493 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4494 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4495 /* Only check the symbol attributes if they're present.
4496 Indices prior to version 7 don't record them,
4497 and indices >= 7 may elide them for certain symbols
4498 (gold does this). */
4499 int attrs_valid =
4500 (index.version >= 7
4501 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4502
4503 /* Work around gold/15646. */
4504 if (attrs_valid)
9291a0cd 4505 {
61920122
PA
4506 if (!is_static && global_seen)
4507 continue;
4508 if (!is_static)
4509 global_seen = true;
4510 }
3190f0c6 4511
61920122
PA
4512 /* Only check the symbol's kind if it has one. */
4513 if (attrs_valid)
4514 {
4515 switch (kind)
8943b874 4516 {
61920122
PA
4517 case VARIABLES_DOMAIN:
4518 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4519 continue;
4520 break;
4521 case FUNCTIONS_DOMAIN:
4522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4523 continue;
61920122
PA
4524 break;
4525 case TYPES_DOMAIN:
4526 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4527 continue;
4528 break;
59c35742
AB
4529 case MODULES_DOMAIN:
4530 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4531 continue;
4532 break;
61920122
PA
4533 default:
4534 break;
8943b874 4535 }
61920122 4536 }
8943b874 4537
61920122 4538 /* Don't crash on bad data. */
b76e467d 4539 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4540 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4541 {
b98664d3 4542 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4543 " [in module %s]"),
4544 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4545 continue;
4546 }
4547
ff4c9fec 4548 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4549 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4550 expansion_notify);
61920122
PA
4551 }
4552}
4553
4b514bc8
JK
4554/* If FILE_MATCHER is non-NULL, set all the
4555 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4556 that match FILE_MATCHER. */
4557
61920122 4558static void
4b514bc8 4559dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4560 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4561 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4562{
4b514bc8 4563 if (file_matcher == NULL)
61920122
PA
4564 return;
4565
4b514bc8
JK
4566 objfile *const objfile = dwarf2_per_objfile->objfile;
4567
4568 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4569 htab_eq_pointer,
4570 NULL, xcalloc, xfree));
4571 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4572 htab_eq_pointer,
4573 NULL, xcalloc, xfree));
61920122 4574
4b514bc8
JK
4575 /* The rule is CUs specify all the files, including those used by
4576 any TU, so there's no need to scan TUs here. */
61920122 4577
b76e467d 4578 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4579 {
927aa2e7
JK
4580 QUIT;
4581
4582 per_cu->v.quick->mark = 0;
4583
4584 /* We only need to look at symtabs not already expanded. */
4585 if (per_cu->v.quick->compunit_symtab)
4586 continue;
4587
b76e467d 4588 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4589 if (file_data == NULL)
4590 continue;
4591
4592 if (htab_find (visited_not_found.get (), file_data) != NULL)
4593 continue;
4594 else if (htab_find (visited_found.get (), file_data) != NULL)
4595 {
4596 per_cu->v.quick->mark = 1;
4597 continue;
4598 }
4599
b76e467d 4600 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4601 {
4602 const char *this_real_name;
4603
4604 if (file_matcher (file_data->file_names[j], false))
4605 {
4606 per_cu->v.quick->mark = 1;
4607 break;
4608 }
4609
4610 /* Before we invoke realpath, which can get expensive when many
4611 files are involved, do a quick comparison of the basenames. */
4612 if (!basenames_may_differ
4613 && !file_matcher (lbasename (file_data->file_names[j]),
4614 true))
4615 continue;
4616
4617 this_real_name = dw2_get_real_path (objfile, file_data, j);
4618 if (file_matcher (this_real_name, false))
4619 {
4620 per_cu->v.quick->mark = 1;
4621 break;
4622 }
4623 }
4624
b76e467d
SM
4625 void **slot = htab_find_slot (per_cu->v.quick->mark
4626 ? visited_found.get ()
4627 : visited_not_found.get (),
4628 file_data, INSERT);
927aa2e7
JK
4629 *slot = file_data;
4630 }
4631}
4632
4633static void
4634dw2_expand_symtabs_matching
4635 (struct objfile *objfile,
4636 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4637 const lookup_name_info &lookup_name,
4638 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4639 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4640 enum search_domain kind)
4641{
ed2dc618
SM
4642 struct dwarf2_per_objfile *dwarf2_per_objfile
4643 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4644
4645 /* index_table is NULL if OBJF_READNOW. */
4646 if (!dwarf2_per_objfile->index_table)
4647 return;
4648
ed2dc618 4649 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4650
4651 mapped_index &index = *dwarf2_per_objfile->index_table;
4652
4653 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4654 symbol_matcher,
4655 kind, [&] (offset_type idx)
4656 {
ed2dc618 4657 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4658 expansion_notify, kind);
3b00ef10 4659 return true;
927aa2e7
JK
4660 });
4661}
4662
4663/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4664 symtab. */
4665
4666static struct compunit_symtab *
4667recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4668 CORE_ADDR pc)
4669{
4670 int i;
4671
4672 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4673 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4674 return cust;
4675
4676 if (cust->includes == NULL)
4677 return NULL;
4678
4679 for (i = 0; cust->includes[i]; ++i)
4680 {
4681 struct compunit_symtab *s = cust->includes[i];
4682
4683 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4684 if (s != NULL)
4685 return s;
4686 }
4687
4688 return NULL;
4689}
4690
4691static struct compunit_symtab *
4692dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4693 struct bound_minimal_symbol msymbol,
4694 CORE_ADDR pc,
4695 struct obj_section *section,
4696 int warn_if_readin)
4697{
4698 struct dwarf2_per_cu_data *data;
4699 struct compunit_symtab *result;
4700
d320c2b5 4701 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4702 return NULL;
4703
b3b3bada 4704 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4705 data = (struct dwarf2_per_cu_data *) addrmap_find
4706 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4707 if (!data)
4708 return NULL;
4709
4710 if (warn_if_readin && data->v.quick->compunit_symtab)
4711 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4712 paddress (get_objfile_arch (objfile), pc));
4713
4714 result
58f0c718
TT
4715 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4716 false),
927aa2e7
JK
4717 pc);
4718 gdb_assert (result != NULL);
4719 return result;
4720}
4721
4722static void
4723dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4724 void *data, int need_fullname)
4725{
ed2dc618
SM
4726 struct dwarf2_per_objfile *dwarf2_per_objfile
4727 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4728
4729 if (!dwarf2_per_objfile->filenames_cache)
4730 {
4731 dwarf2_per_objfile->filenames_cache.emplace ();
4732
4733 htab_up visited (htab_create_alloc (10,
4734 htab_hash_pointer, htab_eq_pointer,
4735 NULL, xcalloc, xfree));
4736
4737 /* The rule is CUs specify all the files, including those used
4738 by any TU, so there's no need to scan TUs here. We can
4739 ignore file names coming from already-expanded CUs. */
4740
b76e467d 4741 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4742 {
927aa2e7
JK
4743 if (per_cu->v.quick->compunit_symtab)
4744 {
4745 void **slot = htab_find_slot (visited.get (),
4746 per_cu->v.quick->file_names,
4747 INSERT);
4748
4749 *slot = per_cu->v.quick->file_names;
4750 }
4751 }
4752
b76e467d 4753 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4754 {
927aa2e7
JK
4755 /* We only need to look at symtabs not already expanded. */
4756 if (per_cu->v.quick->compunit_symtab)
4757 continue;
4758
b76e467d 4759 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4760 if (file_data == NULL)
4761 continue;
4762
b76e467d 4763 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4764 if (*slot)
4765 {
4766 /* Already visited. */
4767 continue;
4768 }
4769 *slot = file_data;
4770
4771 for (int j = 0; j < file_data->num_file_names; ++j)
4772 {
4773 const char *filename = file_data->file_names[j];
4774 dwarf2_per_objfile->filenames_cache->seen (filename);
4775 }
4776 }
4777 }
4778
4779 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4780 {
4781 gdb::unique_xmalloc_ptr<char> this_real_name;
4782
4783 if (need_fullname)
4784 this_real_name = gdb_realpath (filename);
4785 (*fun) (filename, this_real_name.get (), data);
4786 });
4787}
4788
4789static int
4790dw2_has_symbols (struct objfile *objfile)
4791{
4792 return 1;
4793}
4794
4795const struct quick_symbol_functions dwarf2_gdb_index_functions =
4796{
4797 dw2_has_symbols,
4798 dw2_find_last_source_symtab,
4799 dw2_forget_cached_source_info,
4800 dw2_map_symtabs_matching_filename,
4801 dw2_lookup_symbol,
4802 dw2_print_stats,
4803 dw2_dump,
927aa2e7
JK
4804 dw2_expand_symtabs_for_function,
4805 dw2_expand_all_symtabs,
4806 dw2_expand_symtabs_with_fullname,
4807 dw2_map_matching_symbols,
4808 dw2_expand_symtabs_matching,
4809 dw2_find_pc_sect_compunit_symtab,
4810 NULL,
4811 dw2_map_symbol_filenames
4812};
4813
4814/* DWARF-5 debug_names reader. */
4815
4816/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4817static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4818
4819/* A helper function that reads the .debug_names section in SECTION
4820 and fills in MAP. FILENAME is the name of the file containing the
4821 section; it is used for error reporting.
4822
4823 Returns true if all went well, false otherwise. */
4824
4825static bool
4826read_debug_names_from_section (struct objfile *objfile,
4827 const char *filename,
4828 struct dwarf2_section_info *section,
4829 mapped_debug_names &map)
4830{
96b79293 4831 if (section->empty ())
927aa2e7
JK
4832 return false;
4833
4834 /* Older elfutils strip versions could keep the section in the main
4835 executable while splitting it for the separate debug info file. */
96b79293 4836 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4837 return false;
4838
96b79293 4839 section->read (objfile);
927aa2e7
JK
4840
4841 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4842
4843 const gdb_byte *addr = section->buffer;
4844
96b79293 4845 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4846
4847 unsigned int bytes_read;
4848 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4849 addr += bytes_read;
4850
4851 map.dwarf5_is_dwarf64 = bytes_read != 4;
4852 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4853 if (bytes_read + length != section->size)
4854 {
4855 /* There may be multiple per-CU indices. */
4856 warning (_("Section .debug_names in %s length %s does not match "
4857 "section length %s, ignoring .debug_names."),
4858 filename, plongest (bytes_read + length),
4859 pulongest (section->size));
4860 return false;
4861 }
4862
4863 /* The version number. */
4864 uint16_t version = read_2_bytes (abfd, addr);
4865 addr += 2;
4866 if (version != 5)
4867 {
4868 warning (_("Section .debug_names in %s has unsupported version %d, "
4869 "ignoring .debug_names."),
4870 filename, version);
4871 return false;
4872 }
4873
4874 /* Padding. */
4875 uint16_t padding = read_2_bytes (abfd, addr);
4876 addr += 2;
4877 if (padding != 0)
4878 {
4879 warning (_("Section .debug_names in %s has unsupported padding %d, "
4880 "ignoring .debug_names."),
4881 filename, padding);
4882 return false;
4883 }
4884
4885 /* comp_unit_count - The number of CUs in the CU list. */
4886 map.cu_count = read_4_bytes (abfd, addr);
4887 addr += 4;
4888
4889 /* local_type_unit_count - The number of TUs in the local TU
4890 list. */
4891 map.tu_count = read_4_bytes (abfd, addr);
4892 addr += 4;
4893
4894 /* foreign_type_unit_count - The number of TUs in the foreign TU
4895 list. */
4896 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4897 addr += 4;
4898 if (foreign_tu_count != 0)
4899 {
4900 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4901 "ignoring .debug_names."),
4902 filename, static_cast<unsigned long> (foreign_tu_count));
4903 return false;
4904 }
4905
4906 /* bucket_count - The number of hash buckets in the hash lookup
4907 table. */
4908 map.bucket_count = read_4_bytes (abfd, addr);
4909 addr += 4;
4910
4911 /* name_count - The number of unique names in the index. */
4912 map.name_count = read_4_bytes (abfd, addr);
4913 addr += 4;
4914
4915 /* abbrev_table_size - The size in bytes of the abbreviations
4916 table. */
4917 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4918 addr += 4;
4919
4920 /* augmentation_string_size - The size in bytes of the augmentation
4921 string. This value is rounded up to a multiple of 4. */
4922 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4923 addr += 4;
4924 map.augmentation_is_gdb = ((augmentation_string_size
4925 == sizeof (dwarf5_augmentation))
4926 && memcmp (addr, dwarf5_augmentation,
4927 sizeof (dwarf5_augmentation)) == 0);
4928 augmentation_string_size += (-augmentation_string_size) & 3;
4929 addr += augmentation_string_size;
4930
4931 /* List of CUs */
4932 map.cu_table_reordered = addr;
4933 addr += map.cu_count * map.offset_size;
4934
4935 /* List of Local TUs */
4936 map.tu_table_reordered = addr;
4937 addr += map.tu_count * map.offset_size;
4938
4939 /* Hash Lookup Table */
4940 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4941 addr += map.bucket_count * 4;
4942 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4943 addr += map.name_count * 4;
4944
4945 /* Name Table */
4946 map.name_table_string_offs_reordered = addr;
4947 addr += map.name_count * map.offset_size;
4948 map.name_table_entry_offs_reordered = addr;
4949 addr += map.name_count * map.offset_size;
4950
4951 const gdb_byte *abbrev_table_start = addr;
4952 for (;;)
4953 {
927aa2e7
JK
4954 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4955 addr += bytes_read;
4956 if (index_num == 0)
4957 break;
4958
4959 const auto insertpair
4960 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4961 if (!insertpair.second)
4962 {
4963 warning (_("Section .debug_names in %s has duplicate index %s, "
4964 "ignoring .debug_names."),
4965 filename, pulongest (index_num));
4966 return false;
4967 }
4968 mapped_debug_names::index_val &indexval = insertpair.first->second;
4969 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4970 addr += bytes_read;
4971
4972 for (;;)
4973 {
4974 mapped_debug_names::index_val::attr attr;
4975 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4976 addr += bytes_read;
4977 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4978 addr += bytes_read;
4979 if (attr.form == DW_FORM_implicit_const)
4980 {
4981 attr.implicit_const = read_signed_leb128 (abfd, addr,
4982 &bytes_read);
4983 addr += bytes_read;
4984 }
4985 if (attr.dw_idx == 0 && attr.form == 0)
4986 break;
4987 indexval.attr_vec.push_back (std::move (attr));
4988 }
4989 }
4990 if (addr != abbrev_table_start + abbrev_table_size)
4991 {
4992 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4993 "of size %s vs. written as %u, ignoring .debug_names."),
4994 filename, plongest (addr - abbrev_table_start),
4995 abbrev_table_size);
927aa2e7
JK
4996 return false;
4997 }
4998 map.entry_pool = addr;
4999
5000 return true;
5001}
5002
5003/* A helper for create_cus_from_debug_names that handles the MAP's CU
5004 list. */
5005
5006static void
ed2dc618 5007create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5008 const mapped_debug_names &map,
5009 dwarf2_section_info &section,
b76e467d 5010 bool is_dwz)
927aa2e7
JK
5011{
5012 sect_offset sect_off_prev;
5013 for (uint32_t i = 0; i <= map.cu_count; ++i)
5014 {
5015 sect_offset sect_off_next;
5016 if (i < map.cu_count)
5017 {
5018 sect_off_next
5019 = (sect_offset) (extract_unsigned_integer
5020 (map.cu_table_reordered + i * map.offset_size,
5021 map.offset_size,
5022 map.dwarf5_byte_order));
5023 }
5024 else
5025 sect_off_next = (sect_offset) section.size;
5026 if (i >= 1)
5027 {
5028 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5029 dwarf2_per_cu_data *per_cu
ed2dc618 5030 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5031 sect_off_prev, length);
b76e467d 5032 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5033 }
5034 sect_off_prev = sect_off_next;
5035 }
5036}
5037
5038/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5039 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5040
5041static void
ed2dc618 5042create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5043 const mapped_debug_names &map,
5044 const mapped_debug_names &dwz_map)
5045{
b76e467d
SM
5046 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5047 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5048
ed2dc618
SM
5049 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5050 dwarf2_per_objfile->info,
b76e467d 5051 false /* is_dwz */);
927aa2e7
JK
5052
5053 if (dwz_map.cu_count == 0)
5054 return;
5055
ed2dc618
SM
5056 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5057 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5058 true /* is_dwz */);
927aa2e7
JK
5059}
5060
5061/* Read .debug_names. If everything went ok, initialize the "quick"
5062 elements of all the CUs and return true. Otherwise, return false. */
5063
5064static bool
ed2dc618 5065dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5066{
22ca247e
TT
5067 std::unique_ptr<mapped_debug_names> map
5068 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5069 mapped_debug_names dwz_map (dwarf2_per_objfile);
5070 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5071
5072 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5073 &dwarf2_per_objfile->debug_names,
22ca247e 5074 *map))
927aa2e7
JK
5075 return false;
5076
5077 /* Don't use the index if it's empty. */
22ca247e 5078 if (map->name_count == 0)
927aa2e7
JK
5079 return false;
5080
5081 /* If there is a .dwz file, read it so we can get its CU list as
5082 well. */
ed2dc618 5083 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5084 if (dwz != NULL)
5085 {
5086 if (!read_debug_names_from_section (objfile,
00f93c44 5087 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5088 &dwz->debug_names, dwz_map))
5089 {
5090 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5091 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5092 return false;
5093 }
5094 }
5095
22ca247e 5096 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5097
22ca247e 5098 if (map->tu_count != 0)
927aa2e7
JK
5099 {
5100 /* We can only handle a single .debug_types when we have an
5101 index. */
fd5866f6 5102 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5103 return false;
5104
fd5866f6 5105 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5106
5107 create_signatured_type_table_from_debug_names
22ca247e 5108 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5109 }
5110
ed2dc618
SM
5111 create_addrmap_from_aranges (dwarf2_per_objfile,
5112 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5113
22ca247e 5114 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5115 dwarf2_per_objfile->using_index = 1;
5116 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5117 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5118
5119 return true;
5120}
5121
927aa2e7
JK
5122/* Type used to manage iterating over all CUs looking for a symbol for
5123 .debug_names. */
5124
5125class dw2_debug_names_iterator
5126{
5127public:
927aa2e7 5128 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5129 gdb::optional<block_enum> block_index,
5130 domain_enum domain,
927aa2e7 5131 const char *name)
2b79f376 5132 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5133 m_addr (find_vec_in_debug_names (map, name))
5134 {}
5135
5136 dw2_debug_names_iterator (const mapped_debug_names &map,
5137 search_domain search, uint32_t namei)
5138 : m_map (map),
5139 m_search (search),
5140 m_addr (find_vec_in_debug_names (map, namei))
5141 {}
5142
3b00ef10
TT
5143 dw2_debug_names_iterator (const mapped_debug_names &map,
5144 block_enum block_index, domain_enum domain,
5145 uint32_t namei)
5146 : m_map (map), m_block_index (block_index), m_domain (domain),
5147 m_addr (find_vec_in_debug_names (map, namei))
5148 {}
5149
927aa2e7
JK
5150 /* Return the next matching CU or NULL if there are no more. */
5151 dwarf2_per_cu_data *next ();
5152
5153private:
5154 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5155 const char *name);
5156 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5157 uint32_t namei);
5158
5159 /* The internalized form of .debug_names. */
5160 const mapped_debug_names &m_map;
5161
2b79f376
SM
5162 /* If set, only look for symbols that match that block. Valid values are
5163 GLOBAL_BLOCK and STATIC_BLOCK. */
5164 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5165
5166 /* The kind of symbol we're looking for. */
5167 const domain_enum m_domain = UNDEF_DOMAIN;
5168 const search_domain m_search = ALL_DOMAIN;
5169
5170 /* The list of CUs from the index entry of the symbol, or NULL if
5171 not found. */
5172 const gdb_byte *m_addr;
5173};
5174
5175const char *
5176mapped_debug_names::namei_to_name (uint32_t namei) const
5177{
5178 const ULONGEST namei_string_offs
5179 = extract_unsigned_integer ((name_table_string_offs_reordered
5180 + namei * offset_size),
5181 offset_size,
5182 dwarf5_byte_order);
5183 return read_indirect_string_at_offset
ed2dc618 5184 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5185}
5186
5187/* Find a slot in .debug_names for the object named NAME. If NAME is
5188 found, return pointer to its pool data. If NAME cannot be found,
5189 return NULL. */
5190
5191const gdb_byte *
5192dw2_debug_names_iterator::find_vec_in_debug_names
5193 (const mapped_debug_names &map, const char *name)
5194{
5195 int (*cmp) (const char *, const char *);
5196
54ee4252 5197 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5198 if (current_language->la_language == language_cplus
5199 || current_language->la_language == language_fortran
5200 || current_language->la_language == language_d)
5201 {
5202 /* NAME is already canonical. Drop any qualifiers as
5203 .debug_names does not contain any. */
5204
5205 if (strchr (name, '(') != NULL)
5206 {
54ee4252 5207 without_params = cp_remove_params (name);
927aa2e7 5208 if (without_params != NULL)
54ee4252 5209 name = without_params.get ();
927aa2e7
JK
5210 }
5211 }
5212
5213 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5214
5215 const uint32_t full_hash = dwarf5_djb_hash (name);
5216 uint32_t namei
5217 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5218 (map.bucket_table_reordered
5219 + (full_hash % map.bucket_count)), 4,
5220 map.dwarf5_byte_order);
5221 if (namei == 0)
5222 return NULL;
5223 --namei;
5224 if (namei >= map.name_count)
5225 {
b98664d3 5226 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5227 "[in module %s]"),
5228 namei, map.name_count,
ed2dc618 5229 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5230 return NULL;
5231 }
5232
5233 for (;;)
5234 {
5235 const uint32_t namei_full_hash
5236 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5237 (map.hash_table_reordered + namei), 4,
5238 map.dwarf5_byte_order);
5239 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5240 return NULL;
5241
5242 if (full_hash == namei_full_hash)
5243 {
5244 const char *const namei_string = map.namei_to_name (namei);
5245
5246#if 0 /* An expensive sanity check. */
5247 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5248 {
b98664d3 5249 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5250 "[in module %s]"),
5251 namei, objfile_name (dwarf2_per_objfile->objfile));
5252 return NULL;
5253 }
5254#endif
5255
5256 if (cmp (namei_string, name) == 0)
5257 {
5258 const ULONGEST namei_entry_offs
5259 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5260 + namei * map.offset_size),
5261 map.offset_size, map.dwarf5_byte_order);
5262 return map.entry_pool + namei_entry_offs;
5263 }
5264 }
5265
5266 ++namei;
5267 if (namei >= map.name_count)
5268 return NULL;
5269 }
5270}
5271
5272const gdb_byte *
5273dw2_debug_names_iterator::find_vec_in_debug_names
5274 (const mapped_debug_names &map, uint32_t namei)
5275{
5276 if (namei >= map.name_count)
5277 {
b98664d3 5278 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5279 "[in module %s]"),
5280 namei, map.name_count,
ed2dc618 5281 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5282 return NULL;
5283 }
5284
5285 const ULONGEST namei_entry_offs
5286 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5287 + namei * map.offset_size),
5288 map.offset_size, map.dwarf5_byte_order);
5289 return map.entry_pool + namei_entry_offs;
5290}
5291
5292/* See dw2_debug_names_iterator. */
5293
5294dwarf2_per_cu_data *
5295dw2_debug_names_iterator::next ()
5296{
5297 if (m_addr == NULL)
5298 return NULL;
5299
ed2dc618
SM
5300 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5301 struct objfile *objfile = dwarf2_per_objfile->objfile;
5302 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5303
5304 again:
5305
5306 unsigned int bytes_read;
5307 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5308 m_addr += bytes_read;
5309 if (abbrev == 0)
5310 return NULL;
5311
5312 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5313 if (indexval_it == m_map.abbrev_map.cend ())
5314 {
b98664d3 5315 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5316 "[in module %s]"),
ed2dc618 5317 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5318 return NULL;
5319 }
5320 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5321 enum class symbol_linkage {
5322 unknown,
5323 static_,
5324 extern_,
23c13d42 5325 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5326 dwarf2_per_cu_data *per_cu = NULL;
5327 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5328 {
5329 ULONGEST ull;
5330 switch (attr.form)
5331 {
5332 case DW_FORM_implicit_const:
5333 ull = attr.implicit_const;
5334 break;
5335 case DW_FORM_flag_present:
5336 ull = 1;
5337 break;
5338 case DW_FORM_udata:
5339 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5340 m_addr += bytes_read;
5341 break;
5342 default:
b98664d3 5343 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5344 dwarf_form_name (attr.form),
ed2dc618 5345 objfile_name (objfile));
927aa2e7
JK
5346 return NULL;
5347 }
5348 switch (attr.dw_idx)
5349 {
5350 case DW_IDX_compile_unit:
5351 /* Don't crash on bad data. */
b76e467d 5352 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5353 {
b98664d3 5354 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5355 " [in module %s]"),
5356 pulongest (ull),
5357 objfile_name (dwarf2_per_objfile->objfile));
5358 continue;
5359 }
ff4c9fec 5360 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5361 break;
8af5c486
JK
5362 case DW_IDX_type_unit:
5363 /* Don't crash on bad data. */
b2bdb8cf 5364 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5365 {
b98664d3 5366 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5367 " [in module %s]"),
5368 pulongest (ull),
5369 objfile_name (dwarf2_per_objfile->objfile));
5370 continue;
5371 }
ff4c9fec 5372 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5373 break;
927aa2e7
JK
5374 case DW_IDX_GNU_internal:
5375 if (!m_map.augmentation_is_gdb)
5376 break;
23c13d42 5377 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5378 break;
5379 case DW_IDX_GNU_external:
5380 if (!m_map.augmentation_is_gdb)
5381 break;
23c13d42 5382 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5383 break;
5384 }
5385 }
5386
5387 /* Skip if already read in. */
5388 if (per_cu->v.quick->compunit_symtab)
5389 goto again;
5390
5391 /* Check static vs global. */
23c13d42 5392 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5393 {
2b79f376 5394 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5395 const bool symbol_is_static =
5396 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5397 if (want_static != symbol_is_static)
2b79f376 5398 goto again;
927aa2e7
JK
5399 }
5400
5401 /* Match dw2_symtab_iter_next, symbol_kind
5402 and debug_names::psymbol_tag. */
5403 switch (m_domain)
5404 {
5405 case VAR_DOMAIN:
5406 switch (indexval.dwarf_tag)
5407 {
5408 case DW_TAG_variable:
5409 case DW_TAG_subprogram:
5410 /* Some types are also in VAR_DOMAIN. */
5411 case DW_TAG_typedef:
5412 case DW_TAG_structure_type:
5413 break;
5414 default:
5415 goto again;
5416 }
5417 break;
5418 case STRUCT_DOMAIN:
5419 switch (indexval.dwarf_tag)
5420 {
5421 case DW_TAG_typedef:
5422 case DW_TAG_structure_type:
5423 break;
5424 default:
5425 goto again;
5426 }
5427 break;
5428 case LABEL_DOMAIN:
5429 switch (indexval.dwarf_tag)
5430 {
5431 case 0:
5432 case DW_TAG_variable:
5433 break;
5434 default:
5435 goto again;
5436 }
5437 break;
59c35742
AB
5438 case MODULE_DOMAIN:
5439 switch (indexval.dwarf_tag)
5440 {
5441 case DW_TAG_module:
5442 break;
5443 default:
5444 goto again;
5445 }
5446 break;
927aa2e7
JK
5447 default:
5448 break;
5449 }
5450
5451 /* Match dw2_expand_symtabs_matching, symbol_kind and
5452 debug_names::psymbol_tag. */
5453 switch (m_search)
4b514bc8 5454 {
927aa2e7
JK
5455 case VARIABLES_DOMAIN:
5456 switch (indexval.dwarf_tag)
4b514bc8 5457 {
927aa2e7
JK
5458 case DW_TAG_variable:
5459 break;
5460 default:
5461 goto again;
4b514bc8 5462 }
927aa2e7
JK
5463 break;
5464 case FUNCTIONS_DOMAIN:
5465 switch (indexval.dwarf_tag)
4b514bc8 5466 {
927aa2e7
JK
5467 case DW_TAG_subprogram:
5468 break;
5469 default:
5470 goto again;
4b514bc8 5471 }
927aa2e7
JK
5472 break;
5473 case TYPES_DOMAIN:
5474 switch (indexval.dwarf_tag)
5475 {
5476 case DW_TAG_typedef:
5477 case DW_TAG_structure_type:
5478 break;
5479 default:
5480 goto again;
5481 }
5482 break;
59c35742
AB
5483 case MODULES_DOMAIN:
5484 switch (indexval.dwarf_tag)
5485 {
5486 case DW_TAG_module:
5487 break;
5488 default:
5489 goto again;
5490 }
927aa2e7
JK
5491 default:
5492 break;
4b514bc8 5493 }
927aa2e7
JK
5494
5495 return per_cu;
4b514bc8 5496}
61920122 5497
927aa2e7 5498static struct compunit_symtab *
c7f839cb 5499dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5500 const char *name, domain_enum domain)
4b514bc8 5501{
ed2dc618
SM
5502 struct dwarf2_per_objfile *dwarf2_per_objfile
5503 = get_dwarf2_per_objfile (objfile);
61920122 5504
927aa2e7
JK
5505 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5506 if (!mapp)
61920122 5507 {
927aa2e7
JK
5508 /* index is NULL if OBJF_READNOW. */
5509 return NULL;
5510 }
5511 const auto &map = *mapp;
9291a0cd 5512
2b79f376 5513 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5514
927aa2e7
JK
5515 struct compunit_symtab *stab_best = NULL;
5516 struct dwarf2_per_cu_data *per_cu;
5517 while ((per_cu = iter.next ()) != NULL)
5518 {
5519 struct symbol *sym, *with_opaque = NULL;
58f0c718 5520 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5521 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5522 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5523
927aa2e7
JK
5524 sym = block_find_symbol (block, name, domain,
5525 block_find_non_opaque_type_preferred,
5526 &with_opaque);
9703b513 5527
927aa2e7
JK
5528 /* Some caution must be observed with overloaded functions and
5529 methods, since the index will not contain any overload
5530 information (but NAME might contain it). */
a3ec0bb1 5531
927aa2e7 5532 if (sym != NULL
987012b8 5533 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5534 return stab;
5535 if (with_opaque != NULL
987012b8 5536 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5537 stab_best = stab;
9703b513 5538
927aa2e7 5539 /* Keep looking through other CUs. */
9703b513
TT
5540 }
5541
927aa2e7 5542 return stab_best;
9703b513
TT
5543}
5544
927aa2e7
JK
5545/* This dumps minimal information about .debug_names. It is called
5546 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5547 uses this to verify that .debug_names has been loaded. */
9291a0cd 5548
927aa2e7
JK
5549static void
5550dw2_debug_names_dump (struct objfile *objfile)
5551{
ed2dc618
SM
5552 struct dwarf2_per_objfile *dwarf2_per_objfile
5553 = get_dwarf2_per_objfile (objfile);
5554
927aa2e7
JK
5555 gdb_assert (dwarf2_per_objfile->using_index);
5556 printf_filtered (".debug_names:");
5557 if (dwarf2_per_objfile->debug_names_table)
5558 printf_filtered (" exists\n");
5559 else
5560 printf_filtered (" faked for \"readnow\"\n");
5561 printf_filtered ("\n");
9291a0cd
TT
5562}
5563
9291a0cd 5564static void
927aa2e7
JK
5565dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5566 const char *func_name)
9291a0cd 5567{
ed2dc618
SM
5568 struct dwarf2_per_objfile *dwarf2_per_objfile
5569 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5570
927aa2e7
JK
5571 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5572 if (dwarf2_per_objfile->debug_names_table)
24c79950 5573 {
927aa2e7 5574 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5575
2b79f376 5576 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5577
927aa2e7
JK
5578 struct dwarf2_per_cu_data *per_cu;
5579 while ((per_cu = iter.next ()) != NULL)
58f0c718 5580 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5581 }
5582}
24c79950 5583
3b00ef10
TT
5584static void
5585dw2_debug_names_map_matching_symbols
5586 (struct objfile *objfile,
5587 const lookup_name_info &name, domain_enum domain,
5588 int global,
5589 gdb::function_view<symbol_found_callback_ftype> callback,
5590 symbol_compare_ftype *ordered_compare)
5591{
5592 struct dwarf2_per_objfile *dwarf2_per_objfile
5593 = get_dwarf2_per_objfile (objfile);
5594
5595 /* debug_names_table is NULL if OBJF_READNOW. */
5596 if (!dwarf2_per_objfile->debug_names_table)
5597 return;
5598
5599 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5600 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5601
5602 const char *match_name = name.ada ().lookup_name ().c_str ();
5603 auto matcher = [&] (const char *symname)
5604 {
5605 if (ordered_compare == nullptr)
5606 return true;
5607 return ordered_compare (symname, match_name) == 0;
5608 };
5609
5610 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5611 [&] (offset_type namei)
5612 {
5613 /* The name was matched, now expand corresponding CUs that were
5614 marked. */
5615 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5616
5617 struct dwarf2_per_cu_data *per_cu;
5618 while ((per_cu = iter.next ()) != NULL)
5619 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5620 return true;
5621 });
5622
5623 /* It's a shame we couldn't do this inside the
5624 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5625 that have already been expanded. Instead, this loop matches what
5626 the psymtab code does. */
5627 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5628 {
5629 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5630 if (cust != nullptr)
5631 {
5632 const struct block *block
5633 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5634 if (!iterate_over_symbols_terminated (block, name,
5635 domain, callback))
5636 break;
5637 }
5638 }
5639}
5640
927aa2e7
JK
5641static void
5642dw2_debug_names_expand_symtabs_matching
5643 (struct objfile *objfile,
5644 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5645 const lookup_name_info &lookup_name,
5646 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5647 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5648 enum search_domain kind)
5649{
ed2dc618
SM
5650 struct dwarf2_per_objfile *dwarf2_per_objfile
5651 = get_dwarf2_per_objfile (objfile);
9291a0cd 5652
927aa2e7
JK
5653 /* debug_names_table is NULL if OBJF_READNOW. */
5654 if (!dwarf2_per_objfile->debug_names_table)
5655 return;
9291a0cd 5656
ed2dc618 5657 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5658
44ed8f3e 5659 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5660
44ed8f3e
PA
5661 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5662 symbol_matcher,
5663 kind, [&] (offset_type namei)
927aa2e7 5664 {
927aa2e7
JK
5665 /* The name was matched, now expand corresponding CUs that were
5666 marked. */
5667 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5668
927aa2e7
JK
5669 struct dwarf2_per_cu_data *per_cu;
5670 while ((per_cu = iter.next ()) != NULL)
5671 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5672 expansion_notify);
3b00ef10 5673 return true;
44ed8f3e 5674 });
9291a0cd
TT
5675}
5676
927aa2e7 5677const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5678{
5679 dw2_has_symbols,
5680 dw2_find_last_source_symtab,
5681 dw2_forget_cached_source_info,
f8eba3c6 5682 dw2_map_symtabs_matching_filename,
927aa2e7 5683 dw2_debug_names_lookup_symbol,
9291a0cd 5684 dw2_print_stats,
927aa2e7 5685 dw2_debug_names_dump,
927aa2e7 5686 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5687 dw2_expand_all_symtabs,
652a8996 5688 dw2_expand_symtabs_with_fullname,
3b00ef10 5689 dw2_debug_names_map_matching_symbols,
927aa2e7 5690 dw2_debug_names_expand_symtabs_matching,
43f3e411 5691 dw2_find_pc_sect_compunit_symtab,
71a3c369 5692 NULL,
9291a0cd
TT
5693 dw2_map_symbol_filenames
5694};
5695
4485a1c1
SM
5696/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5697 to either a dwarf2_per_objfile or dwz_file object. */
5698
5699template <typename T>
5700static gdb::array_view<const gdb_byte>
5701get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5702{
5703 dwarf2_section_info *section = &section_owner->gdb_index;
5704
96b79293 5705 if (section->empty ())
4485a1c1
SM
5706 return {};
5707
5708 /* Older elfutils strip versions could keep the section in the main
5709 executable while splitting it for the separate debug info file. */
96b79293 5710 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5711 return {};
5712
96b79293 5713 section->read (obj);
4485a1c1 5714
8bebfcda
PA
5715 /* dwarf2_section_info::size is a bfd_size_type, while
5716 gdb::array_view works with size_t. On 32-bit hosts, with
5717 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5718 is 32-bit. So we need an explicit narrowing conversion here.
5719 This is fine, because it's impossible to allocate or mmap an
5720 array/buffer larger than what size_t can represent. */
5721 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5722}
5723
87d6a7aa
SM
5724/* Lookup the index cache for the contents of the index associated to
5725 DWARF2_OBJ. */
5726
5727static gdb::array_view<const gdb_byte>
5728get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5729{
5730 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5731 if (build_id == nullptr)
5732 return {};
5733
5734 return global_index_cache.lookup_gdb_index (build_id,
5735 &dwarf2_obj->index_cache_res);
5736}
5737
5738/* Same as the above, but for DWZ. */
5739
5740static gdb::array_view<const gdb_byte>
5741get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5742{
5743 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5744 if (build_id == nullptr)
5745 return {};
5746
5747 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5748}
5749
3c0aa29a 5750/* See symfile.h. */
9291a0cd 5751
3c0aa29a
PA
5752bool
5753dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5754{
ed2dc618
SM
5755 struct dwarf2_per_objfile *dwarf2_per_objfile
5756 = get_dwarf2_per_objfile (objfile);
5757
9291a0cd
TT
5758 /* If we're about to read full symbols, don't bother with the
5759 indices. In this case we also don't care if some other debug
5760 format is making psymtabs, because they are all about to be
5761 expanded anyway. */
5762 if ((objfile->flags & OBJF_READNOW))
5763 {
9291a0cd 5764 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5765 create_all_comp_units (dwarf2_per_objfile);
5766 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5767 dwarf2_per_objfile->quick_file_names_table
5768 = create_quick_file_names_table
5769 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5770
b76e467d 5771 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5772 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5773 {
ff4c9fec 5774 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5775
e254ef6a
DE
5776 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5777 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5778 }
5779
5780 /* Return 1 so that gdb sees the "quick" functions. However,
5781 these functions will be no-ops because we will have expanded
5782 all symtabs. */
3c0aa29a
PA
5783 *index_kind = dw_index_kind::GDB_INDEX;
5784 return true;
9291a0cd
TT
5785 }
5786
ed2dc618 5787 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5788 {
5789 *index_kind = dw_index_kind::DEBUG_NAMES;
5790 return true;
5791 }
927aa2e7 5792
4485a1c1
SM
5793 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5794 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5795 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5796 {
5797 *index_kind = dw_index_kind::GDB_INDEX;
5798 return true;
5799 }
9291a0cd 5800
87d6a7aa
SM
5801 /* ... otherwise, try to find the index in the index cache. */
5802 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5803 get_gdb_index_contents_from_cache,
5804 get_gdb_index_contents_from_cache_dwz))
5805 {
5806 global_index_cache.hit ();
5807 *index_kind = dw_index_kind::GDB_INDEX;
5808 return true;
5809 }
5810
5811 global_index_cache.miss ();
3c0aa29a 5812 return false;
9291a0cd
TT
5813}
5814
5815\f
5816
dce234bc
PP
5817/* Build a partial symbol table. */
5818
5819void
f29dff0a 5820dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5821{
ed2dc618
SM
5822 struct dwarf2_per_objfile *dwarf2_per_objfile
5823 = get_dwarf2_per_objfile (objfile);
c9bf0622 5824
6eee24ce 5825 init_psymbol_list (objfile, 1024);
c906108c 5826
a70b8144 5827 try
c9bf0622
TT
5828 {
5829 /* This isn't really ideal: all the data we allocate on the
5830 objfile's obstack is still uselessly kept around. However,
5831 freeing it seems unsafe. */
906768f9 5832 psymtab_discarder psymtabs (objfile);
ed2dc618 5833 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5834 psymtabs.keep ();
87d6a7aa
SM
5835
5836 /* (maybe) store an index in the cache. */
5837 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5838 }
230d2906 5839 catch (const gdb_exception_error &except)
492d29ea
PA
5840 {
5841 exception_print (gdb_stderr, except);
5842 }
c906108c 5843}
c906108c 5844
3b80fe9b
DE
5845/* Find the base address of the compilation unit for range lists and
5846 location lists. It will normally be specified by DW_AT_low_pc.
5847 In DWARF-3 draft 4, the base address could be overridden by
5848 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5849 compilation units with discontinuous ranges. */
5850
5851static void
5852dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5853{
5854 struct attribute *attr;
5855
5856 cu->base_known = 0;
5857 cu->base_address = 0;
5858
5859 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5860 if (attr != nullptr)
3b80fe9b 5861 {
cd6c91b4 5862 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5863 cu->base_known = 1;
5864 }
5865 else
5866 {
5867 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5868 if (attr != nullptr)
3b80fe9b 5869 {
cd6c91b4 5870 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5871 cu->base_known = 1;
5872 }
5873 }
5874}
5875
36586728
TT
5876/* Helper function that returns the proper abbrev section for
5877 THIS_CU. */
5878
5879static struct dwarf2_section_info *
5880get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5881{
5882 struct dwarf2_section_info *abbrev;
ed2dc618 5883 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5884
5885 if (this_cu->is_dwz)
ed2dc618 5886 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5887 else
5888 abbrev = &dwarf2_per_objfile->abbrev;
5889
5890 return abbrev;
5891}
5892
f4dc4d17
DE
5893/* Fetch the abbreviation table offset from a comp or type unit header. */
5894
5895static sect_offset
ed2dc618
SM
5896read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5897 struct dwarf2_section_info *section,
9c541725 5898 sect_offset sect_off)
f4dc4d17 5899{
96b79293 5900 bfd *abfd = section->get_bfd_owner ();
d521ce57 5901 const gdb_byte *info_ptr;
ac298888 5902 unsigned int initial_length_size, offset_size;
43988095 5903 uint16_t version;
f4dc4d17 5904
96b79293 5905 section->read (dwarf2_per_objfile->objfile);
9c541725 5906 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5907 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5908 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5909 info_ptr += initial_length_size;
5910
5911 version = read_2_bytes (abfd, info_ptr);
5912 info_ptr += 2;
5913 if (version >= 5)
5914 {
5915 /* Skip unit type and address size. */
5916 info_ptr += 2;
5917 }
5918
24aa364d 5919 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5920}
5921
b83470bf
TT
5922/* A partial symtab that is used only for include files. */
5923struct dwarf2_include_psymtab : public partial_symtab
5924{
5925 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5926 : partial_symtab (filename, objfile)
5927 {
5928 }
5929
5930 void read_symtab (struct objfile *objfile) override
5931 {
5932 expand_psymtab (objfile);
5933 }
5934
5935 void expand_psymtab (struct objfile *objfile) override
5936 {
5937 if (m_readin)
5938 return;
5939 /* It's an include file, no symbols to read for it.
5940 Everything is in the parent symtab. */
5941 read_dependencies (objfile);
5942 m_readin = true;
5943 }
5944
5945 bool readin_p () const override
5946 {
5947 return m_readin;
5948 }
5949
5950 struct compunit_symtab *get_compunit_symtab () const override
5951 {
5952 return nullptr;
5953 }
5954
5955private:
5956
5957 bool m_readin = false;
5958};
5959
aaa75496
JB
5960/* Allocate a new partial symtab for file named NAME and mark this new
5961 partial symtab as being an include of PST. */
5962
5963static void
891813be 5964dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5965 struct objfile *objfile)
5966{
b83470bf 5967 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 5968
fbd9ab74
JK
5969 if (!IS_ABSOLUTE_PATH (subpst->filename))
5970 {
5971 /* It shares objfile->objfile_obstack. */
5972 subpst->dirname = pst->dirname;
5973 }
5974
a9342b62 5975 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5976 subpst->dependencies[0] = pst;
5977 subpst->number_of_dependencies = 1;
aaa75496
JB
5978}
5979
5980/* Read the Line Number Program data and extract the list of files
5981 included by the source file represented by PST. Build an include
d85a05f0 5982 partial symtab for each of these included files. */
aaa75496
JB
5983
5984static void
5985dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5986 struct die_info *die,
891813be 5987 dwarf2_psymtab *pst)
aaa75496 5988{
fff8551c 5989 line_header_up lh;
d85a05f0 5990 struct attribute *attr;
aaa75496 5991
d85a05f0 5992 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5993 if (attr != nullptr)
9c541725 5994 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5995 if (lh == NULL)
5996 return; /* No linetable, so no includes. */
5997
79748972
TT
5998 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5999 that we pass in the raw text_low here; that is ok because we're
6000 only decoding the line table to make include partial symtabs, and
6001 so the addresses aren't really used. */
4ae976d1 6002 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6003 pst->raw_text_low (), 1);
aaa75496
JB
6004}
6005
348e048f 6006static hashval_t
52dc124a 6007hash_signatured_type (const void *item)
348e048f 6008{
9a3c8263
SM
6009 const struct signatured_type *sig_type
6010 = (const struct signatured_type *) item;
9a619af0 6011
348e048f 6012 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6013 return sig_type->signature;
348e048f
DE
6014}
6015
6016static int
52dc124a 6017eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6018{
9a3c8263
SM
6019 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6020 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6021
348e048f
DE
6022 return lhs->signature == rhs->signature;
6023}
6024
1fd400ff
TT
6025/* Allocate a hash table for signatured types. */
6026
b0b6a987 6027static htab_up
298e9637 6028allocate_signatured_type_table ()
1fd400ff 6029{
b0b6a987
TT
6030 return htab_up (htab_create_alloc (41,
6031 hash_signatured_type,
6032 eq_signatured_type,
6033 NULL, xcalloc, xfree));
1fd400ff
TT
6034}
6035
d467dd73 6036/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6037
6038static int
d467dd73 6039add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6040{
9a3c8263 6041 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6042 std::vector<signatured_type *> *all_type_units
6043 = (std::vector<signatured_type *> *) datum;
1fd400ff 6044
b2bdb8cf 6045 all_type_units->push_back (sigt);
1fd400ff
TT
6046
6047 return 1;
6048}
6049
78d4d2c5 6050/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6051 and fill them into TYPES_HTAB. It will process only type units,
6052 therefore DW_UT_type. */
c88ee1f0 6053
78d4d2c5 6054static void
ed2dc618
SM
6055create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6056 struct dwo_file *dwo_file,
b0b6a987 6057 dwarf2_section_info *section, htab_up &types_htab,
43988095 6058 rcuh_kind section_kind)
348e048f 6059{
3019eac3 6060 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6061 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6062 bfd *abfd;
6063 const gdb_byte *info_ptr, *end_ptr;
348e048f 6064
4bdcc0c1
DE
6065 abbrev_section = (dwo_file != NULL
6066 ? &dwo_file->sections.abbrev
6067 : &dwarf2_per_objfile->abbrev);
6068
b4f54984 6069 if (dwarf_read_debug)
43988095 6070 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6071 section->get_name (),
6072 abbrev_section->get_file_name ());
09406207 6073
96b79293 6074 section->read (objfile);
78d4d2c5 6075 info_ptr = section->buffer;
348e048f 6076
78d4d2c5
JK
6077 if (info_ptr == NULL)
6078 return;
348e048f 6079
78d4d2c5
JK
6080 /* We can't set abfd until now because the section may be empty or
6081 not present, in which case the bfd is unknown. */
96b79293 6082 abfd = section->get_bfd_owner ();
348e048f 6083
c0ab21c2
TT
6084 /* We don't use cutu_reader here because we don't need to read
6085 any dies: the signature is in the header. */
3019eac3 6086
78d4d2c5
JK
6087 end_ptr = info_ptr + section->size;
6088 while (info_ptr < end_ptr)
6089 {
78d4d2c5
JK
6090 struct signatured_type *sig_type;
6091 struct dwo_unit *dwo_tu;
6092 void **slot;
6093 const gdb_byte *ptr = info_ptr;
6094 struct comp_unit_head header;
6095 unsigned int length;
8b70b953 6096
9c541725 6097 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6098
a49dd8dd
JK
6099 /* Initialize it due to a false compiler warning. */
6100 header.signature = -1;
9c541725 6101 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6102
78d4d2c5
JK
6103 /* We need to read the type's signature in order to build the hash
6104 table, but we don't need anything else just yet. */
348e048f 6105
ed2dc618 6106 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6107 abbrev_section, ptr, section_kind);
348e048f 6108
4057dfde 6109 length = header.get_length ();
6caca83c 6110
78d4d2c5
JK
6111 /* Skip dummy type units. */
6112 if (ptr >= info_ptr + length
43988095
JK
6113 || peek_abbrev_code (abfd, ptr) == 0
6114 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6115 {
6116 info_ptr += length;
6117 continue;
6118 }
dee91e82 6119
78d4d2c5
JK
6120 if (types_htab == NULL)
6121 {
6122 if (dwo_file)
298e9637 6123 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6124 else
298e9637 6125 types_htab = allocate_signatured_type_table ();
78d4d2c5 6126 }
8b70b953 6127
78d4d2c5
JK
6128 if (dwo_file)
6129 {
6130 sig_type = NULL;
6131 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6132 struct dwo_unit);
6133 dwo_tu->dwo_file = dwo_file;
43988095 6134 dwo_tu->signature = header.signature;
9c541725 6135 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6136 dwo_tu->section = section;
9c541725 6137 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6138 dwo_tu->length = length;
6139 }
6140 else
6141 {
6142 /* N.B.: type_offset is not usable if this type uses a DWO file.
6143 The real type_offset is in the DWO file. */
6144 dwo_tu = NULL;
6145 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6146 struct signatured_type);
43988095 6147 sig_type->signature = header.signature;
9c541725 6148 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6149 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6150 sig_type->per_cu.is_debug_types = 1;
6151 sig_type->per_cu.section = section;
9c541725 6152 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6153 sig_type->per_cu.length = length;
6154 }
6155
b0b6a987 6156 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6157 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6158 INSERT);
6159 gdb_assert (slot != NULL);
6160 if (*slot != NULL)
6161 {
9c541725 6162 sect_offset dup_sect_off;
0349ea22 6163
3019eac3
DE
6164 if (dwo_file)
6165 {
78d4d2c5
JK
6166 const struct dwo_unit *dup_tu
6167 = (const struct dwo_unit *) *slot;
6168
9c541725 6169 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6170 }
6171 else
6172 {
78d4d2c5
JK
6173 const struct signatured_type *dup_tu
6174 = (const struct signatured_type *) *slot;
6175
9c541725 6176 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6177 }
8b70b953 6178
b98664d3 6179 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6180 " the entry at offset %s, signature %s"),
6181 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6182 hex_string (header.signature));
78d4d2c5
JK
6183 }
6184 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6185
78d4d2c5 6186 if (dwarf_read_debug > 1)
9d8780f0
SM
6187 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6188 sect_offset_str (sect_off),
43988095 6189 hex_string (header.signature));
3019eac3 6190
78d4d2c5
JK
6191 info_ptr += length;
6192 }
6193}
3019eac3 6194
78d4d2c5
JK
6195/* Create the hash table of all entries in the .debug_types
6196 (or .debug_types.dwo) section(s).
6197 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6198 otherwise it is NULL.
b3c8eb43 6199
78d4d2c5 6200 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6201
78d4d2c5 6202 Note: This function processes DWO files only, not DWP files. */
348e048f 6203
78d4d2c5 6204static void
ed2dc618
SM
6205create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6206 struct dwo_file *dwo_file,
fd5866f6 6207 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6208 htab_up &types_htab)
78d4d2c5 6209{
fd5866f6
SM
6210 for (dwarf2_section_info &section : type_sections)
6211 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6212 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6213}
6214
6215/* Create the hash table of all entries in the .debug_types section,
6216 and initialize all_type_units.
6217 The result is zero if there is an error (e.g. missing .debug_types section),
6218 otherwise non-zero. */
6219
6220static int
ed2dc618 6221create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6222{
b0b6a987 6223 htab_up types_htab;
3019eac3 6224
ed2dc618
SM
6225 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6226 &dwarf2_per_objfile->info, types_htab,
43988095 6227 rcuh_kind::COMPILE);
ed2dc618
SM
6228 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6229 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6230 if (types_htab == NULL)
6231 {
6232 dwarf2_per_objfile->signatured_types = NULL;
6233 return 0;
6234 }
6235
b0b6a987 6236 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6237
b2bdb8cf 6238 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6239 dwarf2_per_objfile->all_type_units.reserve
6240 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6241
b0b6a987
TT
6242 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6243 add_signatured_type_cu_to_table,
b2bdb8cf 6244 &dwarf2_per_objfile->all_type_units);
1fd400ff 6245
348e048f
DE
6246 return 1;
6247}
6248
6aa5f3a6
DE
6249/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6250 If SLOT is non-NULL, it is the entry to use in the hash table.
6251 Otherwise we find one. */
6252
6253static struct signatured_type *
ed2dc618
SM
6254add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6255 void **slot)
6aa5f3a6
DE
6256{
6257 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6258
b2bdb8cf
SM
6259 if (dwarf2_per_objfile->all_type_units.size ()
6260 == dwarf2_per_objfile->all_type_units.capacity ())
6261 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6262
b2bdb8cf
SM
6263 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6264 struct signatured_type);
6265
6266 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6267 sig_type->signature = sig;
6268 sig_type->per_cu.is_debug_types = 1;
6269 if (dwarf2_per_objfile->using_index)
6270 {
6271 sig_type->per_cu.v.quick =
6272 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6273 struct dwarf2_per_cu_quick_data);
6274 }
6275
6276 if (slot == NULL)
6277 {
b0b6a987 6278 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6279 sig_type, INSERT);
6280 }
6281 gdb_assert (*slot == NULL);
6282 *slot = sig_type;
6283 /* The rest of sig_type must be filled in by the caller. */
6284 return sig_type;
6285}
6286
a2ce51a0
DE
6287/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6288 Fill in SIG_ENTRY with DWO_ENTRY. */
6289
6290static void
ed2dc618 6291fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6292 struct signatured_type *sig_entry,
6293 struct dwo_unit *dwo_entry)
6294{
7ee85ab1 6295 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6296 gdb_assert (! sig_entry->per_cu.queued);
6297 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6298 if (dwarf2_per_objfile->using_index)
6299 {
6300 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6301 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6302 }
6303 else
6304 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6305 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6306 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6307 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6308 gdb_assert (sig_entry->dwo_unit == NULL);
6309
6310 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6311 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6312 sig_entry->per_cu.length = dwo_entry->length;
6313 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6314 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6315 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6316 sig_entry->dwo_unit = dwo_entry;
6317}
6318
6319/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6320 If we haven't read the TU yet, create the signatured_type data structure
6321 for a TU to be read in directly from a DWO file, bypassing the stub.
6322 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6323 using .gdb_index, then when reading a CU we want to stay in the DWO file
6324 containing that CU. Otherwise we could end up reading several other DWO
6325 files (due to comdat folding) to process the transitive closure of all the
6326 mentioned TUs, and that can be slow. The current DWO file will have every
6327 type signature that it needs.
a2ce51a0
DE
6328 We only do this for .gdb_index because in the psymtab case we already have
6329 to read all the DWOs to build the type unit groups. */
6330
6331static struct signatured_type *
6332lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6333{
518817b3
SM
6334 struct dwarf2_per_objfile *dwarf2_per_objfile
6335 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6336 struct dwo_file *dwo_file;
6337 struct dwo_unit find_dwo_entry, *dwo_entry;
6338 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6339 void **slot;
a2ce51a0
DE
6340
6341 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6342
6aa5f3a6
DE
6343 /* If TU skeletons have been removed then we may not have read in any
6344 TUs yet. */
6345 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6346 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6347
6348 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6349 Use the global signatured_types array to do our own comdat-folding
6350 of types. If this is the first time we're reading this TU, and
6351 the TU has an entry in .gdb_index, replace the recorded data from
6352 .gdb_index with this TU. */
a2ce51a0 6353
a2ce51a0 6354 find_sig_entry.signature = sig;
b0b6a987 6355 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6356 &find_sig_entry, INSERT);
9a3c8263 6357 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6358
6359 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6360 read. Don't reassign the global entry to point to this DWO if that's
6361 the case. Also note that if the TU is already being read, it may not
6362 have come from a DWO, the program may be a mix of Fission-compiled
6363 code and non-Fission-compiled code. */
6364
6365 /* Have we already tried to read this TU?
6366 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6367 needn't exist in the global table yet). */
6368 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6369 return sig_entry;
6370
6aa5f3a6
DE
6371 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6372 dwo_unit of the TU itself. */
6373 dwo_file = cu->dwo_unit->dwo_file;
6374
a2ce51a0
DE
6375 /* Ok, this is the first time we're reading this TU. */
6376 if (dwo_file->tus == NULL)
6377 return NULL;
6378 find_dwo_entry.signature = sig;
b0b6a987
TT
6379 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6380 &find_dwo_entry);
a2ce51a0
DE
6381 if (dwo_entry == NULL)
6382 return NULL;
6383
6aa5f3a6
DE
6384 /* If the global table doesn't have an entry for this TU, add one. */
6385 if (sig_entry == NULL)
ed2dc618 6386 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6387
ed2dc618 6388 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6389 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6390 return sig_entry;
6391}
6392
a2ce51a0
DE
6393/* Subroutine of lookup_signatured_type.
6394 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6395 then try the DWP file. If the TU stub (skeleton) has been removed then
6396 it won't be in .gdb_index. */
a2ce51a0
DE
6397
6398static struct signatured_type *
6399lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6400{
518817b3
SM
6401 struct dwarf2_per_objfile *dwarf2_per_objfile
6402 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6403 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6404 struct dwo_unit *dwo_entry;
6405 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6406 void **slot;
a2ce51a0
DE
6407
6408 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6409 gdb_assert (dwp_file != NULL);
6410
6aa5f3a6
DE
6411 /* If TU skeletons have been removed then we may not have read in any
6412 TUs yet. */
6413 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6414 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6415
6aa5f3a6 6416 find_sig_entry.signature = sig;
b0b6a987 6417 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6418 &find_sig_entry, INSERT);
9a3c8263 6419 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6420
6421 /* Have we already tried to read this TU?
6422 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6423 needn't exist in the global table yet). */
6424 if (sig_entry != NULL)
6425 return sig_entry;
6426
a2ce51a0
DE
6427 if (dwp_file->tus == NULL)
6428 return NULL;
ed2dc618 6429 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6430 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6431 if (dwo_entry == NULL)
6432 return NULL;
6433
ed2dc618
SM
6434 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6435 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6436
a2ce51a0
DE
6437 return sig_entry;
6438}
6439
380bca97 6440/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6441 Returns NULL if signature SIG is not present in the table.
6442 It is up to the caller to complain about this. */
348e048f
DE
6443
6444static struct signatured_type *
a2ce51a0 6445lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6446{
518817b3
SM
6447 struct dwarf2_per_objfile *dwarf2_per_objfile
6448 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6449
a2ce51a0
DE
6450 if (cu->dwo_unit
6451 && dwarf2_per_objfile->using_index)
6452 {
6453 /* We're in a DWO/DWP file, and we're using .gdb_index.
6454 These cases require special processing. */
ed2dc618 6455 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6456 return lookup_dwo_signatured_type (cu, sig);
6457 else
6458 return lookup_dwp_signatured_type (cu, sig);
6459 }
6460 else
6461 {
6462 struct signatured_type find_entry, *entry;
348e048f 6463
a2ce51a0
DE
6464 if (dwarf2_per_objfile->signatured_types == NULL)
6465 return NULL;
6466 find_entry.signature = sig;
9a3c8263 6467 entry = ((struct signatured_type *)
b0b6a987
TT
6468 htab_find (dwarf2_per_objfile->signatured_types.get (),
6469 &find_entry));
a2ce51a0
DE
6470 return entry;
6471 }
348e048f 6472}
18a8505e
AT
6473
6474/* Return the address base of the compile unit, which, if exists, is stored
6475 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6476static gdb::optional<ULONGEST>
6477lookup_addr_base (struct die_info *comp_unit_die)
6478{
6479 struct attribute *attr;
6480 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6481 if (attr == nullptr)
6482 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6483 if (attr == nullptr)
6484 return gdb::optional<ULONGEST> ();
6485 return DW_UNSND (attr);
6486}
6487
6488/* Return range lists base of the compile unit, which, if exists, is stored
6489 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6490static ULONGEST
6491lookup_ranges_base (struct die_info *comp_unit_die)
6492{
6493 struct attribute *attr;
6494 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6495 if (attr == nullptr)
6496 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6497 if (attr == nullptr)
6498 return 0;
6499 return DW_UNSND (attr);
6500}
6501
42e7ad6c 6502/* Low level DIE reading support. */
348e048f 6503
d85a05f0
DJ
6504/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6505
6506static void
6507init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6508 struct dwarf2_cu *cu,
3019eac3 6509 struct dwarf2_section_info *section,
685af9cd
TT
6510 struct dwo_file *dwo_file,
6511 struct abbrev_table *abbrev_table)
d85a05f0 6512{
fceca515 6513 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6514 reader->abfd = section->get_bfd_owner ();
d85a05f0 6515 reader->cu = cu;
3019eac3 6516 reader->dwo_file = dwo_file;
dee91e82
DE
6517 reader->die_section = section;
6518 reader->buffer = section->buffer;
f664829e 6519 reader->buffer_end = section->buffer + section->size;
685af9cd 6520 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6521}
6522
c0ab21c2 6523/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6524 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6525 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6526 already.
6527
6528 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6529 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6530 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6531 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6532 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6533 STUB_COMP_DIR may be non-NULL.
3e225074 6534 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6535 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6536 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6537 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6538 kept around for at least as long as *RESULT_READER.
6539
b0c7bfa9
DE
6540 The result is non-zero if a valid (non-dummy) DIE was found. */
6541
6542static int
6543read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6544 struct dwo_unit *dwo_unit,
b0c7bfa9 6545 struct die_info *stub_comp_unit_die,
a2ce51a0 6546 const char *stub_comp_dir,
b0c7bfa9 6547 struct die_reader_specs *result_reader,
d521ce57 6548 const gdb_byte **result_info_ptr,
b0c7bfa9 6549 struct die_info **result_comp_unit_die,
685af9cd 6550 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6551{
ed2dc618 6552 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6553 struct objfile *objfile = dwarf2_per_objfile->objfile;
6554 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6555 bfd *abfd;
d521ce57 6556 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6557 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6558 int i,num_extra_attrs;
6559 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6560 struct die_info *comp_unit_die;
6561
b0aeadb3
DE
6562 /* At most one of these may be provided. */
6563 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6564
b0c7bfa9
DE
6565 /* These attributes aren't processed until later:
6566 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6567 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6568 referenced later. However, these attributes are found in the stub
6569 which we won't have later. In order to not impose this complication
6570 on the rest of the code, we read them here and copy them to the
6571 DWO CU/TU die. */
b0c7bfa9
DE
6572
6573 stmt_list = NULL;
6574 low_pc = NULL;
6575 high_pc = NULL;
6576 ranges = NULL;
6577 comp_dir = NULL;
6578
6579 if (stub_comp_unit_die != NULL)
6580 {
6581 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6582 DWO file. */
6583 if (! this_cu->is_debug_types)
6584 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6585 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6586 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6587 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6588 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6589
18a8505e 6590 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6591
18a8505e
AT
6592 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6593 here (if needed). We need the value before we can process
6594 DW_AT_ranges. */
6595 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6596 }
a2ce51a0
DE
6597 else if (stub_comp_dir != NULL)
6598 {
6599 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6600 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6601 comp_dir->name = DW_AT_comp_dir;
6602 comp_dir->form = DW_FORM_string;
6603 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6604 DW_STRING (comp_dir) = stub_comp_dir;
6605 }
b0c7bfa9
DE
6606
6607 /* Set up for reading the DWO CU/TU. */
6608 cu->dwo_unit = dwo_unit;
685af9cd 6609 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6610 section->read (objfile);
6611 abfd = section->get_bfd_owner ();
9c541725
PA
6612 begin_info_ptr = info_ptr = (section->buffer
6613 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6614 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6615
6616 if (this_cu->is_debug_types)
6617 {
b0c7bfa9
DE
6618 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6619
ed2dc618
SM
6620 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6621 &cu->header, section,
b0c7bfa9 6622 dwo_abbrev_section,
43988095 6623 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6624 /* This is not an assert because it can be caused by bad debug info. */
43988095 6625 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6626 {
6627 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6628 " TU at offset %s [in module %s]"),
a2ce51a0 6629 hex_string (sig_type->signature),
43988095 6630 hex_string (cu->header.signature),
9d8780f0 6631 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6632 bfd_get_filename (abfd));
6633 }
9c541725 6634 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6635 /* For DWOs coming from DWP files, we don't know the CU length
6636 nor the type's offset in the TU until now. */
4057dfde 6637 dwo_unit->length = cu->header.get_length ();
9c541725 6638 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6639
6640 /* Establish the type offset that can be used to lookup the type.
6641 For DWO files, we don't know it until now. */
9c541725
PA
6642 sig_type->type_offset_in_section
6643 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6644 }
6645 else
6646 {
ed2dc618
SM
6647 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6648 &cu->header, section,
b0c7bfa9 6649 dwo_abbrev_section,
43988095 6650 info_ptr, rcuh_kind::COMPILE);
9c541725 6651 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6652 /* For DWOs coming from DWP files, we don't know the CU length
6653 until now. */
4057dfde 6654 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6655 }
6656
685af9cd 6657 *result_dwo_abbrev_table
86de1d91
TT
6658 = abbrev_table::read (objfile, dwo_abbrev_section,
6659 cu->header.abbrev_sect_off);
685af9cd
TT
6660 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6661 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6662
6663 /* Read in the die, but leave space to copy over the attributes
6664 from the stub. This has the benefit of simplifying the rest of
6665 the code - all the work to maintain the illusion of a single
6666 DW_TAG_{compile,type}_unit DIE is done here. */
6667 num_extra_attrs = ((stmt_list != NULL)
6668 + (low_pc != NULL)
6669 + (high_pc != NULL)
6670 + (ranges != NULL)
6671 + (comp_dir != NULL));
6672 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6673 num_extra_attrs);
b0c7bfa9
DE
6674
6675 /* Copy over the attributes from the stub to the DIE we just read in. */
6676 comp_unit_die = *result_comp_unit_die;
6677 i = comp_unit_die->num_attrs;
6678 if (stmt_list != NULL)
6679 comp_unit_die->attrs[i++] = *stmt_list;
6680 if (low_pc != NULL)
6681 comp_unit_die->attrs[i++] = *low_pc;
6682 if (high_pc != NULL)
6683 comp_unit_die->attrs[i++] = *high_pc;
6684 if (ranges != NULL)
6685 comp_unit_die->attrs[i++] = *ranges;
6686 if (comp_dir != NULL)
6687 comp_unit_die->attrs[i++] = *comp_dir;
6688 comp_unit_die->num_attrs += num_extra_attrs;
6689
b4f54984 6690 if (dwarf_die_debug)
bf6af496
DE
6691 {
6692 fprintf_unfiltered (gdb_stdlog,
6693 "Read die from %s@0x%x of %s:\n",
96b79293 6694 section->get_name (),
bf6af496
DE
6695 (unsigned) (begin_info_ptr - section->buffer),
6696 bfd_get_filename (abfd));
b4f54984 6697 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6698 }
6699
b0c7bfa9
DE
6700 /* Skip dummy compilation units. */
6701 if (info_ptr >= begin_info_ptr + dwo_unit->length
6702 || peek_abbrev_code (abfd, info_ptr) == 0)
6703 return 0;
6704
6705 *result_info_ptr = info_ptr;
6706 return 1;
6707}
6708
a084a2a6
AT
6709/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6710 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6711 signature is part of the header. */
6712static gdb::optional<ULONGEST>
6713lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6714{
6715 if (cu->header.version >= 5)
6716 return cu->header.signature;
6717 struct attribute *attr;
6718 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6719 if (attr == nullptr)
6720 return gdb::optional<ULONGEST> ();
6721 return DW_UNSND (attr);
6722}
6723
c0ab21c2 6724/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6725 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6726 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6727
6728static struct dwo_unit *
6729lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6730 struct die_info *comp_unit_die,
6731 const char *dwo_name)
b0c7bfa9
DE
6732{
6733 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6734 struct dwo_unit *dwo_unit;
c0ab21c2 6735 const char *comp_dir;
b0c7bfa9 6736
a2ce51a0
DE
6737 gdb_assert (cu != NULL);
6738
b0c7bfa9 6739 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6740 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6741 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6742
6743 if (this_cu->is_debug_types)
6744 {
6745 struct signatured_type *sig_type;
6746
6747 /* Since this_cu is the first member of struct signatured_type,
6748 we can go from a pointer to one to a pointer to the other. */
6749 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6750 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6751 }
6752 else
6753 {
a084a2a6
AT
6754 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6755 if (!signature.has_value ())
b0c7bfa9
DE
6756 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6757 " [in module %s]"),
e3b94546 6758 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6759 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6760 *signature);
b0c7bfa9
DE
6761 }
6762
b0c7bfa9
DE
6763 return dwo_unit;
6764}
6765
c0ab21c2 6766/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6767 See it for a description of the parameters.
fcd3b13d 6768 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6769
c0ab21c2
TT
6770void
6771cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6772 int use_existing_cu)
a2ce51a0 6773{
a2ce51a0 6774 struct signatured_type *sig_type;
a2ce51a0
DE
6775
6776 /* Verify we can do the following downcast, and that we have the
6777 data we need. */
6778 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6779 sig_type = (struct signatured_type *) this_cu;
6780 gdb_assert (sig_type->dwo_unit != NULL);
6781
6aa5f3a6
DE
6782 if (use_existing_cu && this_cu->cu != NULL)
6783 {
6784 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6785 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6786 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6787 }
6788 else
6789 {
6790 /* If !use_existing_cu, this_cu->cu must be NULL. */
6791 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6792 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6793 }
6794
6795 /* A future optimization, if needed, would be to use an existing
6796 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6797 could share abbrev tables. */
a2ce51a0
DE
6798
6799 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6800 NULL /* stub_comp_unit_die */,
6801 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6802 this, &info_ptr,
3e225074 6803 &comp_unit_die,
c0ab21c2 6804 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6805 {
6806 /* Dummy die. */
c0ab21c2 6807 dummy_p = true;
a2ce51a0 6808 }
a2ce51a0
DE
6809}
6810
fd820528 6811/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6812 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6813
f4dc4d17
DE
6814 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6815 Otherwise the table specified in the comp unit header is read in and used.
6816 This is an optimization for when we already have the abbrev table.
6817
dee91e82 6818 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6819 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6820
c0ab21c2
TT
6821cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6822 struct abbrev_table *abbrev_table,
6751ebae 6823 int use_existing_cu,
c0ab21c2
TT
6824 bool skip_partial)
6825 : die_reader_specs {},
6751ebae 6826 m_this_cu (this_cu)
c906108c 6827{
ed2dc618 6828 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6829 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6830 struct dwarf2_section_info *section = this_cu->section;
96b79293 6831 bfd *abfd = section->get_bfd_owner ();
dee91e82 6832 struct dwarf2_cu *cu;
c0ab21c2 6833 const gdb_byte *begin_info_ptr;
dee91e82 6834 struct signatured_type *sig_type = NULL;
4bdcc0c1 6835 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6836 /* Non-zero if CU currently points to a DWO file and we need to
6837 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6838 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6839 int rereading_dwo_cu = 0;
c906108c 6840
b4f54984 6841 if (dwarf_die_debug)
9d8780f0 6842 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6843 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6844 sect_offset_str (this_cu->sect_off));
09406207 6845
a2ce51a0
DE
6846 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6847 file (instead of going through the stub), short-circuit all of this. */
6848 if (this_cu->reading_dwo_directly)
6849 {
6850 /* Narrow down the scope of possibilities to have to understand. */
6851 gdb_assert (this_cu->is_debug_types);
6852 gdb_assert (abbrev_table == NULL);
6751ebae 6853 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6854 return;
6855 }
6856
dee91e82 6857 /* This is cheap if the section is already read in. */
96b79293 6858 section->read (objfile);
dee91e82 6859
9c541725 6860 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6861
6862 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6863
6864 if (use_existing_cu && this_cu->cu != NULL)
6865 {
6866 cu = this_cu->cu;
42e7ad6c
DE
6867 /* If this CU is from a DWO file we need to start over, we need to
6868 refetch the attributes from the skeleton CU.
6869 This could be optimized by retrieving those attributes from when we
6870 were here the first time: the previous comp_unit_die was stored in
6871 comp_unit_obstack. But there's no data yet that we need this
6872 optimization. */
6873 if (cu->dwo_unit != NULL)
6874 rereading_dwo_cu = 1;
dee91e82
DE
6875 }
6876 else
6877 {
6878 /* If !use_existing_cu, this_cu->cu must be NULL. */
6879 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6880 m_new_cu.reset (new dwarf2_cu (this_cu));
6881 cu = m_new_cu.get ();
42e7ad6c 6882 }
dee91e82 6883
b0c7bfa9 6884 /* Get the header. */
9c541725 6885 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6886 {
6887 /* We already have the header, there's no need to read it in again. */
9c541725 6888 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6889 }
6890 else
6891 {
3019eac3 6892 if (this_cu->is_debug_types)
dee91e82 6893 {
ed2dc618
SM
6894 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6895 &cu->header, section,
4bdcc0c1 6896 abbrev_section, info_ptr,
43988095 6897 rcuh_kind::TYPE);
dee91e82 6898
42e7ad6c
DE
6899 /* Since per_cu is the first member of struct signatured_type,
6900 we can go from a pointer to one to a pointer to the other. */
6901 sig_type = (struct signatured_type *) this_cu;
43988095 6902 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6903 gdb_assert (sig_type->type_offset_in_tu
6904 == cu->header.type_cu_offset_in_tu);
6905 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6906
42e7ad6c
DE
6907 /* LENGTH has not been set yet for type units if we're
6908 using .gdb_index. */
4057dfde 6909 this_cu->length = cu->header.get_length ();
3019eac3
DE
6910
6911 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6912 sig_type->type_offset_in_section =
6913 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6914
6915 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6916 }
6917 else
6918 {
ed2dc618
SM
6919 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6920 &cu->header, section,
4bdcc0c1 6921 abbrev_section,
43988095
JK
6922 info_ptr,
6923 rcuh_kind::COMPILE);
dee91e82 6924
9c541725 6925 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6926 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6927 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6928 }
6929 }
10b3939b 6930
6caca83c 6931 /* Skip dummy compilation units. */
dee91e82 6932 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6933 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6934 {
6935 dummy_p = true;
6936 return;
6937 }
6caca83c 6938
433df2d4
DE
6939 /* If we don't have them yet, read the abbrevs for this compilation unit.
6940 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6941 done. */
f4dc4d17 6942 if (abbrev_table != NULL)
685af9cd
TT
6943 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6944 else
f4dc4d17 6945 {
c0ab21c2 6946 m_abbrev_table_holder
86de1d91
TT
6947 = abbrev_table::read (objfile, abbrev_section,
6948 cu->header.abbrev_sect_off);
c0ab21c2 6949 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6950 }
af703f96 6951
dee91e82 6952 /* Read the top level CU/TU die. */
c0ab21c2 6953 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6954 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6955
58f0c718 6956 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6957 {
6958 dummy_p = true;
6959 return;
6960 }
58f0c718 6961
b0c7bfa9 6962 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6963 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6964 table from the DWO file and pass the ownership over to us. It will be
6965 referenced from READER, so we must make sure to free it after we're done
6966 with READER.
6967
b0c7bfa9
DE
6968 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6969 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6970 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6971 if (dwo_name != nullptr)
3019eac3 6972 {
3019eac3 6973 struct dwo_unit *dwo_unit;
b0c7bfa9 6974 struct die_info *dwo_comp_unit_die;
3019eac3 6975
3e225074 6976 if (comp_unit_die->has_children)
6a506a2d 6977 {
b98664d3 6978 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6979 " has children (offset %s) [in module %s]"),
6980 sect_offset_str (this_cu->sect_off),
6981 bfd_get_filename (abfd));
6a506a2d 6982 }
c0ab21c2 6983 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6984 if (dwo_unit != NULL)
3019eac3 6985 {
6a506a2d 6986 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6987 comp_unit_die, NULL,
c0ab21c2 6988 this, &info_ptr,
3e225074 6989 &dwo_comp_unit_die,
c0ab21c2 6990 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6991 {
6992 /* Dummy die. */
c0ab21c2 6993 dummy_p = true;
6a506a2d
DE
6994 return;
6995 }
6996 comp_unit_die = dwo_comp_unit_die;
6997 }
6998 else
6999 {
7000 /* Yikes, we couldn't find the rest of the DIE, we only have
7001 the stub. A complaint has already been logged. There's
7002 not much more we can do except pass on the stub DIE to
7003 die_reader_func. We don't want to throw an error on bad
7004 debug info. */
3019eac3
DE
7005 }
7006 }
c0ab21c2 7007}
3019eac3 7008
6751ebae
TT
7009void
7010cutu_reader::keep ()
c0ab21c2 7011{
b0c7bfa9 7012 /* Done, clean up. */
6751ebae
TT
7013 gdb_assert (!dummy_p);
7014 if (m_new_cu != NULL)
348e048f 7015 {
c0ab21c2
TT
7016 struct dwarf2_per_objfile *dwarf2_per_objfile
7017 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7018 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7019 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7020 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7021 /* The chain owns it now. */
c0ab21c2 7022 m_new_cu.release ();
348e048f 7023 }
dee91e82
DE
7024}
7025
18a8505e
AT
7026/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7027 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7028 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7029
7030 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7031 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7032
7033 We fill in THIS_CU->length.
7034
dee91e82 7035 THIS_CU->cu is always freed when done.
3019eac3 7036 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7037 to care whether it refers to the "main" CU or the DWO CU.
7038
7039 When parent_cu is passed, it is used to provide a default value for
7040 str_offsets_base and addr_base from the parent. */
dee91e82 7041
c0ab21c2
TT
7042cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7043 struct dwarf2_cu *parent_cu,
7044 struct dwo_file *dwo_file)
7045 : die_reader_specs {},
7046 m_this_cu (this_cu)
dee91e82 7047{
ed2dc618 7048 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7049 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7050 struct dwarf2_section_info *section = this_cu->section;
96b79293 7051 bfd *abfd = section->get_bfd_owner ();
33e80786 7052 struct dwarf2_section_info *abbrev_section;
d521ce57 7053 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7054
b4f54984 7055 if (dwarf_die_debug)
9d8780f0 7056 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7057 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7058 sect_offset_str (this_cu->sect_off));
09406207 7059
dee91e82
DE
7060 gdb_assert (this_cu->cu == NULL);
7061
33e80786
DE
7062 abbrev_section = (dwo_file != NULL
7063 ? &dwo_file->sections.abbrev
7064 : get_abbrev_section_for_cu (this_cu));
7065
dee91e82 7066 /* This is cheap if the section is already read in. */
96b79293 7067 section->read (objfile);
dee91e82 7068
c0ab21c2 7069 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7070
9c541725 7071 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7072 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7073 &m_new_cu->header, section,
4bdcc0c1 7074 abbrev_section, info_ptr,
43988095
JK
7075 (this_cu->is_debug_types
7076 ? rcuh_kind::TYPE
7077 : rcuh_kind::COMPILE));
dee91e82 7078
18a8505e
AT
7079 if (parent_cu != nullptr)
7080 {
c0ab21c2
TT
7081 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7082 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7083 }
4057dfde 7084 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7085
7086 /* Skip dummy compilation units. */
7087 if (info_ptr >= begin_info_ptr + this_cu->length
7088 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7089 {
7090 dummy_p = true;
7091 return;
7092 }
72bf9492 7093
c0ab21c2 7094 m_abbrev_table_holder
86de1d91
TT
7095 = abbrev_table::read (objfile, abbrev_section,
7096 m_new_cu->header.abbrev_sect_off);
dee91e82 7097
c0ab21c2
TT
7098 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7099 m_abbrev_table_holder.get ());
3e225074 7100 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7101}
7102
0018ea6f
DE
7103\f
7104/* Type Unit Groups.
dee91e82 7105
0018ea6f
DE
7106 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7107 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7108 so that all types coming from the same compilation (.o file) are grouped
7109 together. A future step could be to put the types in the same symtab as
7110 the CU the types ultimately came from. */
ff013f42 7111
f4dc4d17
DE
7112static hashval_t
7113hash_type_unit_group (const void *item)
7114{
9a3c8263
SM
7115 const struct type_unit_group *tu_group
7116 = (const struct type_unit_group *) item;
f4dc4d17 7117
094b34ac 7118 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7119}
348e048f
DE
7120
7121static int
f4dc4d17 7122eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7123{
9a3c8263
SM
7124 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7125 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7126
094b34ac 7127 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7128}
348e048f 7129
f4dc4d17
DE
7130/* Allocate a hash table for type unit groups. */
7131
eaa5fa8b 7132static htab_up
298e9637 7133allocate_type_unit_groups_table ()
f4dc4d17 7134{
eaa5fa8b
TT
7135 return htab_up (htab_create_alloc (3,
7136 hash_type_unit_group,
7137 eq_type_unit_group,
7138 NULL, xcalloc, xfree));
f4dc4d17 7139}
dee91e82 7140
f4dc4d17
DE
7141/* Type units that don't have DW_AT_stmt_list are grouped into their own
7142 partial symtabs. We combine several TUs per psymtab to not let the size
7143 of any one psymtab grow too big. */
7144#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7145#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7146
094b34ac 7147/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7148 Create the type_unit_group object used to hold one or more TUs. */
7149
7150static struct type_unit_group *
094b34ac 7151create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7152{
518817b3
SM
7153 struct dwarf2_per_objfile *dwarf2_per_objfile
7154 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7155 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7156 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7157 struct type_unit_group *tu_group;
f4dc4d17
DE
7158
7159 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7160 struct type_unit_group);
094b34ac 7161 per_cu = &tu_group->per_cu;
518817b3 7162 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7163
094b34ac
DE
7164 if (dwarf2_per_objfile->using_index)
7165 {
7166 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7167 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7168 }
7169 else
7170 {
9c541725 7171 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7172 dwarf2_psymtab *pst;
528e1572 7173 std::string name;
094b34ac
DE
7174
7175 /* Give the symtab a useful name for debug purposes. */
7176 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7177 name = string_printf ("<type_units_%d>",
7178 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7179 else
528e1572 7180 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7181
528e1572 7182 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7183 pst->anonymous = true;
094b34ac 7184 }
f4dc4d17 7185
094b34ac 7186 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7187 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7188
7189 return tu_group;
7190}
7191
094b34ac
DE
7192/* Look up the type_unit_group for type unit CU, and create it if necessary.
7193 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7194
7195static struct type_unit_group *
ff39bb5e 7196get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7197{
518817b3
SM
7198 struct dwarf2_per_objfile *dwarf2_per_objfile
7199 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7200 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7201 struct type_unit_group *tu_group;
7202 void **slot;
7203 unsigned int line_offset;
7204 struct type_unit_group type_unit_group_for_lookup;
7205
7206 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7207 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7208
7209 /* Do we need to create a new group, or can we use an existing one? */
7210
7211 if (stmt_list)
7212 {
7213 line_offset = DW_UNSND (stmt_list);
7214 ++tu_stats->nr_symtab_sharers;
7215 }
7216 else
7217 {
7218 /* Ugh, no stmt_list. Rare, but we have to handle it.
7219 We can do various things here like create one group per TU or
7220 spread them over multiple groups to split up the expansion work.
7221 To avoid worst case scenarios (too many groups or too large groups)
7222 we, umm, group them in bunches. */
7223 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7224 | (tu_stats->nr_stmt_less_type_units
7225 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7226 ++tu_stats->nr_stmt_less_type_units;
7227 }
7228
094b34ac 7229 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7230 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7231 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7232 &type_unit_group_for_lookup, INSERT);
7233 if (*slot != NULL)
7234 {
9a3c8263 7235 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7236 gdb_assert (tu_group != NULL);
7237 }
7238 else
7239 {
9c541725 7240 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7241 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7242 *slot = tu_group;
7243 ++tu_stats->nr_symtabs;
7244 }
7245
7246 return tu_group;
7247}
0018ea6f
DE
7248\f
7249/* Partial symbol tables. */
7250
7251/* Create a psymtab named NAME and assign it to PER_CU.
7252
7253 The caller must fill in the following details:
7254 dirname, textlow, texthigh. */
7255
891813be 7256static dwarf2_psymtab *
0018ea6f
DE
7257create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7258{
e3b94546 7259 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7260 dwarf2_psymtab *pst;
0018ea6f 7261
891813be 7262 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7263
6d94535f 7264 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7265
7266 /* This is the glue that links PST into GDB's symbol API. */
891813be 7267 pst->per_cu_data = per_cu;
0018ea6f
DE
7268 per_cu->v.psymtab = pst;
7269
7270 return pst;
7271}
7272
c0ab21c2 7273/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7274
7275static void
7276process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7277 const gdb_byte *info_ptr,
0018ea6f 7278 struct die_info *comp_unit_die,
c0ab21c2 7279 enum language pretend_language)
0018ea6f
DE
7280{
7281 struct dwarf2_cu *cu = reader->cu;
518817b3 7282 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7283 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7284 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7285 CORE_ADDR baseaddr;
7286 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7287 dwarf2_psymtab *pst;
3a2b436a 7288 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7289 const char *filename;
0018ea6f 7290
0018ea6f
DE
7291 gdb_assert (! per_cu->is_debug_types);
7292
c0ab21c2 7293 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7294
0018ea6f 7295 /* Allocate a new partial symbol table structure. */
2e927613
TV
7296 gdb::unique_xmalloc_ptr<char> debug_filename;
7297 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7298 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7299 if (filename == NULL)
0018ea6f 7300 filename = "";
2e927613
TV
7301 else if (strcmp (filename, artificial) == 0)
7302 {
7303 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7304 sect_offset_str (per_cu->sect_off),
7305 (char *) NULL));
2e927613
TV
7306 filename = debug_filename.get ();
7307 }
0018ea6f
DE
7308
7309 pst = create_partial_symtab (per_cu, filename);
7310
7311 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7312 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7313
b3b3bada 7314 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7315
7316 dwarf2_find_base_address (comp_unit_die, cu);
7317
7318 /* Possibly set the default values of LOWPC and HIGHPC from
7319 `DW_AT_ranges'. */
3a2b436a
JK
7320 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7321 &best_highpc, cu, pst);
7322 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7323 {
7324 CORE_ADDR low
7325 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7326 - baseaddr);
7327 CORE_ADDR high
7328 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7329 - baseaddr - 1);
7330 /* Store the contiguous range if it is not empty; it can be
7331 empty for CUs with no code. */
d320c2b5
TT
7332 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7333 low, high, pst);
79748972 7334 }
0018ea6f
DE
7335
7336 /* Check if comp unit has_children.
7337 If so, read the rest of the partial symbols from this comp unit.
7338 If not, there's no more debug_info for this comp unit. */
3e225074 7339 if (comp_unit_die->has_children)
0018ea6f
DE
7340 {
7341 struct partial_die_info *first_die;
7342 CORE_ADDR lowpc, highpc;
7343
7344 lowpc = ((CORE_ADDR) -1);
7345 highpc = ((CORE_ADDR) 0);
7346
7347 first_die = load_partial_dies (reader, info_ptr, 1);
7348
7349 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7350 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7351
7352 /* If we didn't find a lowpc, set it to highpc to avoid
7353 complaints from `maint check'. */
7354 if (lowpc == ((CORE_ADDR) -1))
7355 lowpc = highpc;
7356
7357 /* If the compilation unit didn't have an explicit address range,
7358 then use the information extracted from its child dies. */
e385593e 7359 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7360 {
7361 best_lowpc = lowpc;
7362 best_highpc = highpc;
7363 }
7364 }
4ae976d1 7365 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7366 best_lowpc + baseaddr)
7367 - baseaddr);
4ae976d1 7368 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7369 best_highpc + baseaddr)
7370 - baseaddr);
0018ea6f 7371
8763cede 7372 end_psymtab_common (objfile, pst);
0018ea6f 7373
ae640021 7374 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7375 {
7376 int i;
ae640021 7377 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7378
7379 /* Fill in 'dependencies' here; we fill in 'users' in a
7380 post-pass. */
7381 pst->number_of_dependencies = len;
a9342b62
TT
7382 pst->dependencies
7383 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7384 for (i = 0; i < len; ++i)
7385 {
7386 pst->dependencies[i]
7387 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7388 }
0018ea6f 7389
ae640021 7390 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7391 }
7392
7393 /* Get the list of files included in the current compilation unit,
7394 and build a psymtab for each of them. */
7395 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7396
b4f54984 7397 if (dwarf_read_debug)
b926417a
TT
7398 fprintf_unfiltered (gdb_stdlog,
7399 "Psymtab for %s unit @%s: %s - %s"
7400 ", %d global, %d static syms\n",
7401 per_cu->is_debug_types ? "type" : "comp",
7402 sect_offset_str (per_cu->sect_off),
7403 paddress (gdbarch, pst->text_low (objfile)),
7404 paddress (gdbarch, pst->text_high (objfile)),
7405 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7406}
7407
7408/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7409 Process compilation unit THIS_CU for a psymtab. */
7410
7411static void
7412process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7413 bool want_partial_unit,
b93601f3 7414 enum language pretend_language)
0018ea6f
DE
7415{
7416 /* If this compilation unit was already read in, free the
7417 cached copy in order to read it in again. This is
7418 necessary because we skipped some symbols when we first
7419 read in the compilation unit (see load_partial_dies).
7420 This problem could be avoided, but the benefit is unclear. */
7421 if (this_cu->cu != NULL)
7422 free_one_cached_comp_unit (this_cu);
7423
6751ebae 7424 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7425
58990295
TV
7426 switch (reader.comp_unit_die->tag)
7427 {
7428 case DW_TAG_compile_unit:
7429 this_cu->unit_type = DW_UT_compile;
7430 break;
7431 case DW_TAG_partial_unit:
7432 this_cu->unit_type = DW_UT_partial;
7433 break;
7434 default:
7435 abort ();
7436 }
7437
c0ab21c2 7438 if (reader.dummy_p)
f1902523 7439 {
c0ab21c2 7440 /* Nothing. */
f1902523 7441 }
c0ab21c2 7442 else if (this_cu->is_debug_types)
3e225074
TT
7443 build_type_psymtabs_reader (&reader, reader.info_ptr,
7444 reader.comp_unit_die);
135f5437
TT
7445 else if (want_partial_unit
7446 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7447 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7448 reader.comp_unit_die,
c0ab21c2 7449 pretend_language);
0018ea6f 7450
58990295
TV
7451 this_cu->lang = this_cu->cu->language;
7452
0018ea6f 7453 /* Age out any secondary CUs. */
ed2dc618 7454 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7455}
f4dc4d17
DE
7456
7457/* Reader function for build_type_psymtabs. */
7458
7459static void
7460build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7461 const gdb_byte *info_ptr,
3e225074 7462 struct die_info *type_unit_die)
f4dc4d17 7463{
ed2dc618 7464 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7465 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7466 struct objfile *objfile = dwarf2_per_objfile->objfile;
7467 struct dwarf2_cu *cu = reader->cu;
7468 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7469 struct signatured_type *sig_type;
f4dc4d17
DE
7470 struct type_unit_group *tu_group;
7471 struct attribute *attr;
7472 struct partial_die_info *first_die;
7473 CORE_ADDR lowpc, highpc;
891813be 7474 dwarf2_psymtab *pst;
f4dc4d17 7475
0186c6a7
DE
7476 gdb_assert (per_cu->is_debug_types);
7477 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7478
3e225074 7479 if (! type_unit_die->has_children)
f4dc4d17
DE
7480 return;
7481
7482 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7483 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7484
df07e2c7 7485 if (tu_group->tus == nullptr)
a8b3b8e9 7486 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7487 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7488
7489 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7490 pst = create_partial_symtab (per_cu, "");
6d94535f 7491 pst->anonymous = true;
f4dc4d17
DE
7492
7493 first_die = load_partial_dies (reader, info_ptr, 1);
7494
7495 lowpc = (CORE_ADDR) -1;
7496 highpc = (CORE_ADDR) 0;
7497 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7498
8763cede 7499 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7500}
7501
73051182
DE
7502/* Struct used to sort TUs by their abbreviation table offset. */
7503
7504struct tu_abbrev_offset
7505{
b2bdb8cf
SM
7506 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7507 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7508 {}
7509
7510 signatured_type *sig_type;
73051182
DE
7511 sect_offset abbrev_offset;
7512};
7513
484cf504 7514/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7515
484cf504
TT
7516static bool
7517sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7518 const struct tu_abbrev_offset &b)
73051182 7519{
484cf504 7520 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7521}
7522
7523/* Efficiently read all the type units.
7524 This does the bulk of the work for build_type_psymtabs.
7525
7526 The efficiency is because we sort TUs by the abbrev table they use and
7527 only read each abbrev table once. In one program there are 200K TUs
7528 sharing 8K abbrev tables.
7529
7530 The main purpose of this function is to support building the
7531 dwarf2_per_objfile->type_unit_groups table.
7532 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7533 can collapse the search space by grouping them by stmt_list.
7534 The savings can be significant, in the same program from above the 200K TUs
7535 share 8K stmt_list tables.
7536
7537 FUNC is expected to call get_type_unit_group, which will create the
7538 struct type_unit_group if necessary and add it to
7539 dwarf2_per_objfile->type_unit_groups. */
7540
7541static void
ed2dc618 7542build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7543{
73051182 7544 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7545 abbrev_table_up abbrev_table;
73051182 7546 sect_offset abbrev_offset;
73051182
DE
7547
7548 /* It's up to the caller to not call us multiple times. */
7549 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7550
b2bdb8cf 7551 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7552 return;
7553
7554 /* TUs typically share abbrev tables, and there can be way more TUs than
7555 abbrev tables. Sort by abbrev table to reduce the number of times we
7556 read each abbrev table in.
7557 Alternatives are to punt or to maintain a cache of abbrev tables.
7558 This is simpler and efficient enough for now.
7559
7560 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7561 symtab to use). Typically TUs with the same abbrev offset have the same
7562 stmt_list value too so in practice this should work well.
7563
7564 The basic algorithm here is:
7565
7566 sort TUs by abbrev table
7567 for each TU with same abbrev table:
7568 read abbrev table if first user
7569 read TU top level DIE
7570 [IWBN if DWO skeletons had DW_AT_stmt_list]
7571 call FUNC */
7572
b4f54984 7573 if (dwarf_read_debug)
73051182
DE
7574 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7575
7576 /* Sort in a separate table to maintain the order of all_type_units
7577 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7578 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7579 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7580
7581 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7582 sorted_by_abbrev.emplace_back
7583 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7584 sig_type->per_cu.section,
7585 sig_type->per_cu.sect_off));
73051182 7586
484cf504
TT
7587 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7588 sort_tu_by_abbrev_offset);
73051182 7589
9c541725 7590 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7591
b2bdb8cf 7592 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7593 {
73051182
DE
7594 /* Switch to the next abbrev table if necessary. */
7595 if (abbrev_table == NULL
b2bdb8cf 7596 || tu.abbrev_offset != abbrev_offset)
73051182 7597 {
b2bdb8cf 7598 abbrev_offset = tu.abbrev_offset;
73051182 7599 abbrev_table =
86de1d91
TT
7600 abbrev_table::read (dwarf2_per_objfile->objfile,
7601 &dwarf2_per_objfile->abbrev,
7602 abbrev_offset);
73051182
DE
7603 ++tu_stats->nr_uniq_abbrev_tables;
7604 }
7605
c0ab21c2 7606 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7607 0, false);
c0ab21c2
TT
7608 if (!reader.dummy_p)
7609 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7610 reader.comp_unit_die);
73051182 7611 }
6aa5f3a6 7612}
73051182 7613
6aa5f3a6
DE
7614/* Print collected type unit statistics. */
7615
7616static void
ed2dc618 7617print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7618{
7619 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7620
7621 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7622 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7623 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7624 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7625 tu_stats->nr_uniq_abbrev_tables);
7626 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7627 tu_stats->nr_symtabs);
7628 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7629 tu_stats->nr_symtab_sharers);
7630 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7631 tu_stats->nr_stmt_less_type_units);
7632 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7633 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7634}
7635
f4dc4d17
DE
7636/* Traversal function for build_type_psymtabs. */
7637
7638static int
7639build_type_psymtab_dependencies (void **slot, void *info)
7640{
ed2dc618
SM
7641 struct dwarf2_per_objfile *dwarf2_per_objfile
7642 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7643 struct objfile *objfile = dwarf2_per_objfile->objfile;
7644 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7645 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7646 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7647 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7648 int i;
7649
7650 gdb_assert (len > 0);
197400e8 7651 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7652
7653 pst->number_of_dependencies = len;
a9342b62 7654 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7655 for (i = 0; i < len; ++i)
f4dc4d17 7656 {
df07e2c7 7657 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7658 gdb_assert (iter->per_cu.is_debug_types);
7659 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7660 iter->type_unit_group = tu_group;
f4dc4d17
DE
7661 }
7662
df07e2c7
AB
7663 delete tu_group->tus;
7664 tu_group->tus = nullptr;
348e048f
DE
7665
7666 return 1;
7667}
7668
7669/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7670 Build partial symbol tables for the .debug_types comp-units. */
7671
7672static void
ed2dc618 7673build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7674{
ed2dc618 7675 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7676 return;
7677
ed2dc618 7678 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7679}
f4dc4d17 7680
6aa5f3a6
DE
7681/* Traversal function for process_skeletonless_type_unit.
7682 Read a TU in a DWO file and build partial symbols for it. */
7683
7684static int
7685process_skeletonless_type_unit (void **slot, void *info)
7686{
7687 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7688 struct dwarf2_per_objfile *dwarf2_per_objfile
7689 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7690 struct signatured_type find_entry, *entry;
7691
7692 /* If this TU doesn't exist in the global table, add it and read it in. */
7693
7694 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7695 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7696
7697 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7698 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7699 &find_entry, INSERT);
6aa5f3a6
DE
7700 /* If we've already seen this type there's nothing to do. What's happening
7701 is we're doing our own version of comdat-folding here. */
7702 if (*slot != NULL)
7703 return 1;
7704
7705 /* This does the job that create_all_type_units would have done for
7706 this TU. */
ed2dc618
SM
7707 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7708 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7709 *slot = entry;
7710
7711 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7712 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7713 if (!reader.dummy_p)
7714 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7715 reader.comp_unit_die);
6aa5f3a6
DE
7716
7717 return 1;
7718}
7719
7720/* Traversal function for process_skeletonless_type_units. */
7721
7722static int
7723process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7724{
7725 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7726
7727 if (dwo_file->tus != NULL)
b0b6a987
TT
7728 htab_traverse_noresize (dwo_file->tus.get (),
7729 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7730
7731 return 1;
7732}
7733
7734/* Scan all TUs of DWO files, verifying we've processed them.
7735 This is needed in case a TU was emitted without its skeleton.
7736 Note: This can't be done until we know what all the DWO files are. */
7737
7738static void
ed2dc618 7739process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7740{
7741 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7742 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7743 && dwarf2_per_objfile->dwo_files != NULL)
7744 {
51ac9db5 7745 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7746 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7747 dwarf2_per_objfile);
6aa5f3a6 7748 }
348e048f
DE
7749}
7750
ed2dc618 7751/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7752
7753static void
ed2dc618 7754set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7755{
b76e467d 7756 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7757 {
891813be 7758 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7759
36586728
TT
7760 if (pst == NULL)
7761 continue;
7762
b76e467d 7763 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7764 {
7765 /* Set the 'user' field only if it is not already set. */
7766 if (pst->dependencies[j]->user == NULL)
7767 pst->dependencies[j]->user = pst;
7768 }
7769 }
7770}
7771
93311388
DE
7772/* Build the partial symbol table by doing a quick pass through the
7773 .debug_info and .debug_abbrev sections. */
72bf9492 7774
93311388 7775static void
ed2dc618 7776dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7777{
ed2dc618 7778 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7779
b4f54984 7780 if (dwarf_read_debug)
45cfd468
DE
7781 {
7782 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7783 objfile_name (objfile));
45cfd468
DE
7784 }
7785
76935768
TT
7786 scoped_restore restore_reading_psyms
7787 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7788 true);
98bfdba5 7789
96b79293 7790 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7791
93311388
DE
7792 /* Any cached compilation units will be linked by the per-objfile
7793 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7794 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7795
ed2dc618 7796 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7797
ed2dc618 7798 create_all_comp_units (dwarf2_per_objfile);
c906108c 7799
60606b2c
TT
7800 /* Create a temporary address map on a temporary obstack. We later
7801 copy this to the final obstack. */
8268c778 7802 auto_obstack temp_obstack;
791afaa2
TT
7803
7804 scoped_restore save_psymtabs_addrmap
d320c2b5 7805 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7806 addrmap_create_mutable (&temp_obstack));
72bf9492 7807
b76e467d 7808 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7809 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7810
6aa5f3a6 7811 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7812 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7813
7814 /* Now that all TUs have been processed we can fill in the dependencies. */
7815 if (dwarf2_per_objfile->type_unit_groups != NULL)
7816 {
eaa5fa8b 7817 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7818 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7819 }
7820
b4f54984 7821 if (dwarf_read_debug)
ed2dc618 7822 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7823
ed2dc618 7824 set_partial_user (dwarf2_per_objfile);
95554aad 7825
d320c2b5
TT
7826 objfile->partial_symtabs->psymtabs_addrmap
7827 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7828 objfile->partial_symtabs->obstack ());
791afaa2
TT
7829 /* At this point we want to keep the address map. */
7830 save_psymtabs_addrmap.release ();
ff013f42 7831
b4f54984 7832 if (dwarf_read_debug)
45cfd468 7833 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7834 objfile_name (objfile));
ae038cb0
DJ
7835}
7836
dee91e82
DE
7837/* Load the partial DIEs for a secondary CU into memory.
7838 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7839
dee91e82
DE
7840static void
7841load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7842{
6751ebae 7843 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7844
7845 if (!reader.dummy_p)
7846 {
7847 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7848 language_minimal);
7849
7850 /* Check if comp unit has_children.
7851 If so, read the rest of the partial symbols from this comp unit.
7852 If not, there's no more debug_info for this comp unit. */
3e225074 7853 if (reader.comp_unit_die->has_children)
c0ab21c2 7854 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7855
7856 reader.keep ();
c0ab21c2 7857 }
ae038cb0
DJ
7858}
7859
ae038cb0 7860static void
ed2dc618 7861read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7862 struct dwarf2_section_info *section,
f1902523 7863 struct dwarf2_section_info *abbrev_section,
b76e467d 7864 unsigned int is_dwz)
ae038cb0 7865{
d521ce57 7866 const gdb_byte *info_ptr;
ed2dc618 7867 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7868
b4f54984 7869 if (dwarf_read_debug)
bf6af496 7870 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7871 section->get_name (),
7872 section->get_file_name ());
bf6af496 7873
96b79293 7874 section->read (objfile);
ae038cb0 7875
36586728 7876 info_ptr = section->buffer;
6e70227d 7877
36586728 7878 while (info_ptr < section->buffer + section->size)
ae038cb0 7879 {
ae038cb0 7880 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7881
9c541725 7882 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7883
f1902523 7884 comp_unit_head cu_header;
ed2dc618
SM
7885 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7886 abbrev_section, info_ptr,
7887 rcuh_kind::COMPILE);
ae038cb0
DJ
7888
7889 /* Save the compilation unit for later lookup. */
f1902523
JK
7890 if (cu_header.unit_type != DW_UT_type)
7891 {
7892 this_cu = XOBNEW (&objfile->objfile_obstack,
7893 struct dwarf2_per_cu_data);
7894 memset (this_cu, 0, sizeof (*this_cu));
7895 }
7896 else
7897 {
7898 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7899 struct signatured_type);
7900 memset (sig_type, 0, sizeof (*sig_type));
7901 sig_type->signature = cu_header.signature;
7902 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7903 this_cu = &sig_type->per_cu;
7904 }
7905 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7906 this_cu->sect_off = sect_off;
f1902523 7907 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7908 this_cu->is_dwz = is_dwz;
e3b94546 7909 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7910 this_cu->section = section;
ae038cb0 7911
b76e467d 7912 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7913
7914 info_ptr = info_ptr + this_cu->length;
7915 }
36586728
TT
7916}
7917
7918/* Create a list of all compilation units in OBJFILE.
7919 This is only done for -readnow and building partial symtabs. */
7920
7921static void
ed2dc618 7922create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7923{
b76e467d 7924 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7925 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7926 &dwarf2_per_objfile->abbrev, 0);
36586728 7927
b76e467d 7928 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7929 if (dwz != NULL)
ed2dc618 7930 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7931 1);
c906108c
SS
7932}
7933
5734ee8b 7934/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7935 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7936 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7937 DW_AT_ranges). See the comments of add_partial_subprogram on how
7938 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7939
72bf9492
DJ
7940static void
7941scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7942 CORE_ADDR *highpc, int set_addrmap,
7943 struct dwarf2_cu *cu)
c906108c 7944{
72bf9492 7945 struct partial_die_info *pdi;
c906108c 7946
91c24f0a
DC
7947 /* Now, march along the PDI's, descending into ones which have
7948 interesting children but skipping the children of the other ones,
7949 until we reach the end of the compilation unit. */
c906108c 7950
72bf9492 7951 pdi = first_die;
91c24f0a 7952
72bf9492
DJ
7953 while (pdi != NULL)
7954 {
52356b79 7955 pdi->fixup (cu);
c906108c 7956
f55ee35c 7957 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7958 children, so we need to look at them. Ditto for anonymous
7959 enums. */
933c6fe4 7960
72bf9492 7961 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7962 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7963 || pdi->tag == DW_TAG_imported_unit
7964 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7965 {
72bf9492 7966 switch (pdi->tag)
c906108c
SS
7967 {
7968 case DW_TAG_subprogram:
b1dc1806 7969 case DW_TAG_inlined_subroutine:
cdc07690 7970 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7971 break;
72929c62 7972 case DW_TAG_constant:
c906108c
SS
7973 case DW_TAG_variable:
7974 case DW_TAG_typedef:
91c24f0a 7975 case DW_TAG_union_type:
72bf9492 7976 if (!pdi->is_declaration)
63d06c5c 7977 {
72bf9492 7978 add_partial_symbol (pdi, cu);
63d06c5c
DC
7979 }
7980 break;
c906108c 7981 case DW_TAG_class_type:
680b30c7 7982 case DW_TAG_interface_type:
c906108c 7983 case DW_TAG_structure_type:
72bf9492 7984 if (!pdi->is_declaration)
c906108c 7985 {
72bf9492 7986 add_partial_symbol (pdi, cu);
c906108c 7987 }
b7fee5a3
KS
7988 if ((cu->language == language_rust
7989 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7990 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7991 set_addrmap, cu);
c906108c 7992 break;
91c24f0a 7993 case DW_TAG_enumeration_type:
72bf9492
DJ
7994 if (!pdi->is_declaration)
7995 add_partial_enumeration (pdi, cu);
c906108c
SS
7996 break;
7997 case DW_TAG_base_type:
a02abb62 7998 case DW_TAG_subrange_type:
c906108c 7999 /* File scope base type definitions are added to the partial
c5aa993b 8000 symbol table. */
72bf9492 8001 add_partial_symbol (pdi, cu);
c906108c 8002 break;
d9fa45fe 8003 case DW_TAG_namespace:
cdc07690 8004 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8005 break;
5d7cb8df 8006 case DW_TAG_module:
59c35742
AB
8007 if (!pdi->is_declaration)
8008 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8009 break;
95554aad
TT
8010 case DW_TAG_imported_unit:
8011 {
8012 struct dwarf2_per_cu_data *per_cu;
8013
f4dc4d17
DE
8014 /* For now we don't handle imported units in type units. */
8015 if (cu->per_cu->is_debug_types)
8016 {
8017 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8018 " supported in type units [in module %s]"),
518817b3 8019 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8020 }
8021
e3b94546
SM
8022 per_cu = dwarf2_find_containing_comp_unit
8023 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8024 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8025
8026 /* Go read the partial unit, if needed. */
8027 if (per_cu->v.psymtab == NULL)
135f5437 8028 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8029
ae640021 8030 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8031 }
8032 break;
74921315
KS
8033 case DW_TAG_imported_declaration:
8034 add_partial_symbol (pdi, cu);
8035 break;
c906108c
SS
8036 default:
8037 break;
8038 }
8039 }
8040
72bf9492
DJ
8041 /* If the die has a sibling, skip to the sibling. */
8042
8043 pdi = pdi->die_sibling;
8044 }
8045}
8046
8047/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8048
72bf9492 8049 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8050 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8051 Enumerators are an exception; they use the scope of their parent
8052 enumeration type, i.e. the name of the enumeration type is not
8053 prepended to the enumerator.
91c24f0a 8054
72bf9492
DJ
8055 There are two complexities. One is DW_AT_specification; in this
8056 case "parent" means the parent of the target of the specification,
8057 instead of the direct parent of the DIE. The other is compilers
8058 which do not emit DW_TAG_namespace; in this case we try to guess
8059 the fully qualified name of structure types from their members'
8060 linkage names. This must be done using the DIE's children rather
8061 than the children of any DW_AT_specification target. We only need
8062 to do this for structures at the top level, i.e. if the target of
8063 any DW_AT_specification (if any; otherwise the DIE itself) does not
8064 have a parent. */
8065
8066/* Compute the scope prefix associated with PDI's parent, in
8067 compilation unit CU. The result will be allocated on CU's
8068 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8069 field. NULL is returned if no prefix is necessary. */
15d034d0 8070static const char *
72bf9492
DJ
8071partial_die_parent_scope (struct partial_die_info *pdi,
8072 struct dwarf2_cu *cu)
8073{
15d034d0 8074 const char *grandparent_scope;
72bf9492 8075 struct partial_die_info *parent, *real_pdi;
91c24f0a 8076
72bf9492
DJ
8077 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8078 then this means the parent of the specification DIE. */
8079
8080 real_pdi = pdi;
72bf9492 8081 while (real_pdi->has_specification)
fb816e8b 8082 {
122cf0f2
AB
8083 auto res = find_partial_die (real_pdi->spec_offset,
8084 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8085 real_pdi = res.pdi;
8086 cu = res.cu;
8087 }
72bf9492
DJ
8088
8089 parent = real_pdi->die_parent;
8090 if (parent == NULL)
8091 return NULL;
8092
8093 if (parent->scope_set)
8094 return parent->scope;
8095
52356b79 8096 parent->fixup (cu);
72bf9492 8097
10b3939b 8098 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8099
acebe513
UW
8100 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8101 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8102 Work around this problem here. */
8103 if (cu->language == language_cplus
6e70227d 8104 && parent->tag == DW_TAG_namespace
acebe513
UW
8105 && strcmp (parent->name, "::") == 0
8106 && grandparent_scope == NULL)
8107 {
8108 parent->scope = NULL;
8109 parent->scope_set = 1;
8110 return NULL;
8111 }
8112
0a4b0913 8113 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8114 if (pdi->tag == DW_TAG_enumerator)
8115 /* Enumerators should not get the name of the enumeration as a prefix. */
8116 parent->scope = grandparent_scope;
8117 else if (parent->tag == DW_TAG_namespace
f55ee35c 8118 || parent->tag == DW_TAG_module
72bf9492
DJ
8119 || parent->tag == DW_TAG_structure_type
8120 || parent->tag == DW_TAG_class_type
680b30c7 8121 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8122 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8123 || parent->tag == DW_TAG_enumeration_type
8124 || (cu->language == language_fortran
8125 && parent->tag == DW_TAG_subprogram
8126 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8127 {
8128 if (grandparent_scope == NULL)
8129 parent->scope = parent->name;
8130 else
3e43a32a
MS
8131 parent->scope = typename_concat (&cu->comp_unit_obstack,
8132 grandparent_scope,
f55ee35c 8133 parent->name, 0, cu);
72bf9492 8134 }
72bf9492
DJ
8135 else
8136 {
8137 /* FIXME drow/2004-04-01: What should we be doing with
8138 function-local names? For partial symbols, we should probably be
8139 ignoring them. */
fa9c3fa0
TT
8140 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8141 dwarf_tag_name (parent->tag),
8142 sect_offset_str (pdi->sect_off));
72bf9492 8143 parent->scope = grandparent_scope;
c906108c
SS
8144 }
8145
72bf9492
DJ
8146 parent->scope_set = 1;
8147 return parent->scope;
8148}
8149
8150/* Return the fully scoped name associated with PDI, from compilation unit
8151 CU. The result will be allocated with malloc. */
4568ecf9 8152
43816ebc 8153static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8154partial_die_full_name (struct partial_die_info *pdi,
8155 struct dwarf2_cu *cu)
8156{
15d034d0 8157 const char *parent_scope;
72bf9492 8158
98bfdba5
PA
8159 /* If this is a template instantiation, we can not work out the
8160 template arguments from partial DIEs. So, unfortunately, we have
8161 to go through the full DIEs. At least any work we do building
8162 types here will be reused if full symbols are loaded later. */
8163 if (pdi->has_template_arguments)
8164 {
52356b79 8165 pdi->fixup (cu);
98bfdba5
PA
8166
8167 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8168 {
8169 struct die_info *die;
8170 struct attribute attr;
8171 struct dwarf2_cu *ref_cu = cu;
8172
b64f50a1 8173 /* DW_FORM_ref_addr is using section offset. */
b4069958 8174 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8175 attr.form = DW_FORM_ref_addr;
9c541725 8176 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8177 die = follow_die_ref (NULL, &attr, &ref_cu);
8178
43816ebc 8179 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8180 }
8181 }
8182
72bf9492
DJ
8183 parent_scope = partial_die_parent_scope (pdi, cu);
8184 if (parent_scope == NULL)
8185 return NULL;
8186 else
43816ebc
TT
8187 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8188 pdi->name, 0, cu));
c906108c
SS
8189}
8190
8191static void
72bf9492 8192add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8193{
518817b3
SM
8194 struct dwarf2_per_objfile *dwarf2_per_objfile
8195 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8196 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8197 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8198 CORE_ADDR addr = 0;
15d034d0 8199 const char *actual_name = NULL;
e142c38c
DJ
8200 CORE_ADDR baseaddr;
8201
b3b3bada 8202 baseaddr = objfile->text_section_offset ();
c906108c 8203
43816ebc
TT
8204 gdb::unique_xmalloc_ptr<char> built_actual_name
8205 = partial_die_full_name (pdi, cu);
15d034d0 8206 if (built_actual_name != NULL)
43816ebc 8207 actual_name = built_actual_name.get ();
63d06c5c 8208
72bf9492
DJ
8209 if (actual_name == NULL)
8210 actual_name = pdi->name;
8211
c906108c
SS
8212 switch (pdi->tag)
8213 {
b1dc1806 8214 case DW_TAG_inlined_subroutine:
c906108c 8215 case DW_TAG_subprogram:
79748972
TT
8216 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8217 - baseaddr);
0a4b0913
AB
8218 if (pdi->is_external
8219 || cu->language == language_ada
8220 || (cu->language == language_fortran
8221 && pdi->die_parent != NULL
8222 && pdi->die_parent->tag == DW_TAG_subprogram))
8223 {
8224 /* Normally, only "external" DIEs are part of the global scope.
8225 But in Ada and Fortran, we want to be able to access nested
8226 procedures globally. So all Ada and Fortran subprograms are
8227 stored in the global scope. */
31edb802 8228 add_psymbol_to_list (actual_name,
15d034d0 8229 built_actual_name != NULL,
f47fb265 8230 VAR_DOMAIN, LOC_BLOCK,
79748972 8231 SECT_OFF_TEXT (objfile),
75aedd27 8232 psymbol_placement::GLOBAL,
79748972
TT
8233 addr,
8234 cu->language, objfile);
c906108c
SS
8235 }
8236 else
8237 {
31edb802 8238 add_psymbol_to_list (actual_name,
15d034d0 8239 built_actual_name != NULL,
f47fb265 8240 VAR_DOMAIN, LOC_BLOCK,
79748972 8241 SECT_OFF_TEXT (objfile),
75aedd27 8242 psymbol_placement::STATIC,
1762568f 8243 addr, cu->language, objfile);
c906108c 8244 }
0c1b455e
TT
8245
8246 if (pdi->main_subprogram && actual_name != NULL)
8247 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8248 break;
72929c62 8249 case DW_TAG_constant:
31edb802 8250 add_psymbol_to_list (actual_name,
75aedd27
TT
8251 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8252 -1, (pdi->is_external
8253 ? psymbol_placement::GLOBAL
8254 : psymbol_placement::STATIC),
8255 0, cu->language, objfile);
72929c62 8256 break;
c906108c 8257 case DW_TAG_variable:
95554aad
TT
8258 if (pdi->d.locdesc)
8259 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8260
95554aad 8261 if (pdi->d.locdesc
caac4577
JG
8262 && addr == 0
8263 && !dwarf2_per_objfile->has_section_at_zero)
8264 {
8265 /* A global or static variable may also have been stripped
8266 out by the linker if unused, in which case its address
8267 will be nullified; do not add such variables into partial
8268 symbol table then. */
8269 }
8270 else if (pdi->is_external)
c906108c
SS
8271 {
8272 /* Global Variable.
8273 Don't enter into the minimal symbol tables as there is
8274 a minimal symbol table entry from the ELF symbols already.
8275 Enter into partial symbol table if it has a location
8276 descriptor or a type.
8277 If the location descriptor is missing, new_symbol will create
8278 a LOC_UNRESOLVED symbol, the address of the variable will then
8279 be determined from the minimal symbol table whenever the variable
8280 is referenced.
8281 The address for the partial symbol table entry is not
8282 used by GDB, but it comes in handy for debugging partial symbol
8283 table building. */
8284
95554aad 8285 if (pdi->d.locdesc || pdi->has_type)
31edb802 8286 add_psymbol_to_list (actual_name,
15d034d0 8287 built_actual_name != NULL,
f47fb265 8288 VAR_DOMAIN, LOC_STATIC,
79748972 8289 SECT_OFF_TEXT (objfile),
75aedd27 8290 psymbol_placement::GLOBAL,
79748972 8291 addr, cu->language, objfile);
c906108c
SS
8292 }
8293 else
8294 {
ff908ebf
AW
8295 int has_loc = pdi->d.locdesc != NULL;
8296
8297 /* Static Variable. Skip symbols whose value we cannot know (those
8298 without location descriptors or constant values). */
8299 if (!has_loc && !pdi->has_const_value)
43816ebc 8300 return;
ff908ebf 8301
31edb802 8302 add_psymbol_to_list (actual_name,
15d034d0 8303 built_actual_name != NULL,
f47fb265 8304 VAR_DOMAIN, LOC_STATIC,
79748972 8305 SECT_OFF_TEXT (objfile),
75aedd27 8306 psymbol_placement::STATIC,
79748972 8307 has_loc ? addr : 0,
f47fb265 8308 cu->language, objfile);
c906108c
SS
8309 }
8310 break;
8311 case DW_TAG_typedef:
8312 case DW_TAG_base_type:
a02abb62 8313 case DW_TAG_subrange_type:
31edb802 8314 add_psymbol_to_list (actual_name,
15d034d0 8315 built_actual_name != NULL,
79748972 8316 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8317 psymbol_placement::STATIC,
1762568f 8318 0, cu->language, objfile);
c906108c 8319 break;
74921315 8320 case DW_TAG_imported_declaration:
72bf9492 8321 case DW_TAG_namespace:
31edb802 8322 add_psymbol_to_list (actual_name,
15d034d0 8323 built_actual_name != NULL,
79748972 8324 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8325 psymbol_placement::GLOBAL,
1762568f 8326 0, cu->language, objfile);
72bf9492 8327 break;
530e8392 8328 case DW_TAG_module:
a5fd13a9
BH
8329 /* With Fortran 77 there might be a "BLOCK DATA" module
8330 available without any name. If so, we skip the module as it
8331 doesn't bring any value. */
8332 if (actual_name != nullptr)
31edb802 8333 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8334 built_actual_name != NULL,
8335 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8336 psymbol_placement::GLOBAL,
8337 0, cu->language, objfile);
530e8392 8338 break;
c906108c 8339 case DW_TAG_class_type:
680b30c7 8340 case DW_TAG_interface_type:
c906108c
SS
8341 case DW_TAG_structure_type:
8342 case DW_TAG_union_type:
8343 case DW_TAG_enumeration_type:
fa4028e9
JB
8344 /* Skip external references. The DWARF standard says in the section
8345 about "Structure, Union, and Class Type Entries": "An incomplete
8346 structure, union or class type is represented by a structure,
8347 union or class entry that does not have a byte size attribute
8348 and that has a DW_AT_declaration attribute." */
8349 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8350 return;
fa4028e9 8351
63d06c5c
DC
8352 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8353 static vs. global. */
31edb802 8354 add_psymbol_to_list (actual_name,
15d034d0 8355 built_actual_name != NULL,
79748972 8356 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8357 cu->language == language_cplus
75aedd27
TT
8358 ? psymbol_placement::GLOBAL
8359 : psymbol_placement::STATIC,
1762568f 8360 0, cu->language, objfile);
c906108c 8361
c906108c
SS
8362 break;
8363 case DW_TAG_enumerator:
31edb802 8364 add_psymbol_to_list (actual_name,
15d034d0 8365 built_actual_name != NULL,
79748972 8366 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8367 cu->language == language_cplus
75aedd27
TT
8368 ? psymbol_placement::GLOBAL
8369 : psymbol_placement::STATIC,
1762568f 8370 0, cu->language, objfile);
c906108c
SS
8371 break;
8372 default:
8373 break;
8374 }
8375}
8376
5c4e30ca
DC
8377/* Read a partial die corresponding to a namespace; also, add a symbol
8378 corresponding to that namespace to the symbol table. NAMESPACE is
8379 the name of the enclosing namespace. */
91c24f0a 8380
72bf9492
DJ
8381static void
8382add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8383 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8384 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8385{
72bf9492 8386 /* Add a symbol for the namespace. */
e7c27a73 8387
72bf9492 8388 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8389
8390 /* Now scan partial symbols in that namespace. */
8391
91c24f0a 8392 if (pdi->has_children)
cdc07690 8393 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8394}
8395
5d7cb8df
JK
8396/* Read a partial die corresponding to a Fortran module. */
8397
8398static void
8399add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8400 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8401{
530e8392
KB
8402 /* Add a symbol for the namespace. */
8403
8404 add_partial_symbol (pdi, cu);
8405
f55ee35c 8406 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8407
8408 if (pdi->has_children)
cdc07690 8409 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8410}
8411
b1dc1806
XR
8412/* Read a partial die corresponding to a subprogram or an inlined
8413 subprogram and create a partial symbol for that subprogram.
8414 When the CU language allows it, this routine also defines a partial
8415 symbol for each nested subprogram that this subprogram contains.
8416 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8417 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8418
cdc07690
YQ
8419 PDI may also be a lexical block, in which case we simply search
8420 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8421 Again, this is only performed when the CU language allows this
8422 type of definitions. */
8423
8424static void
8425add_partial_subprogram (struct partial_die_info *pdi,
8426 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8427 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8428{
b1dc1806 8429 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8430 {
8431 if (pdi->has_pc_info)
8432 {
8433 if (pdi->lowpc < *lowpc)
8434 *lowpc = pdi->lowpc;
8435 if (pdi->highpc > *highpc)
8436 *highpc = pdi->highpc;
cdc07690 8437 if (set_addrmap)
5734ee8b 8438 {
518817b3 8439 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8440 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8441 CORE_ADDR baseaddr;
b926417a
TT
8442 CORE_ADDR this_highpc;
8443 CORE_ADDR this_lowpc;
5734ee8b 8444
b3b3bada 8445 baseaddr = objfile->text_section_offset ();
b926417a
TT
8446 this_lowpc
8447 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8448 pdi->lowpc + baseaddr)
8449 - baseaddr);
8450 this_highpc
8451 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8452 pdi->highpc + baseaddr)
8453 - baseaddr);
d320c2b5 8454 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8455 this_lowpc, this_highpc - 1,
9291a0cd 8456 cu->per_cu->v.psymtab);
5734ee8b 8457 }
481860b3
GB
8458 }
8459
8460 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8461 {
bc30ff58 8462 if (!pdi->is_declaration)
e8d05480
JB
8463 /* Ignore subprogram DIEs that do not have a name, they are
8464 illegal. Do not emit a complaint at this point, we will
8465 do so when we convert this psymtab into a symtab. */
8466 if (pdi->name)
8467 add_partial_symbol (pdi, cu);
bc30ff58
JB
8468 }
8469 }
6e70227d 8470
bc30ff58
JB
8471 if (! pdi->has_children)
8472 return;
8473
0a4b0913 8474 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8475 {
8476 pdi = pdi->die_child;
8477 while (pdi != NULL)
8478 {
52356b79 8479 pdi->fixup (cu);
bc30ff58 8480 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8481 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8482 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8483 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8484 pdi = pdi->die_sibling;
8485 }
8486 }
8487}
8488
91c24f0a
DC
8489/* Read a partial die corresponding to an enumeration type. */
8490
72bf9492
DJ
8491static void
8492add_partial_enumeration (struct partial_die_info *enum_pdi,
8493 struct dwarf2_cu *cu)
91c24f0a 8494{
72bf9492 8495 struct partial_die_info *pdi;
91c24f0a
DC
8496
8497 if (enum_pdi->name != NULL)
72bf9492
DJ
8498 add_partial_symbol (enum_pdi, cu);
8499
8500 pdi = enum_pdi->die_child;
8501 while (pdi)
91c24f0a 8502 {
72bf9492 8503 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8504 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8505 else
72bf9492
DJ
8506 add_partial_symbol (pdi, cu);
8507 pdi = pdi->die_sibling;
91c24f0a 8508 }
91c24f0a
DC
8509}
8510
6caca83c
CC
8511/* Return the initial uleb128 in the die at INFO_PTR. */
8512
8513static unsigned int
d521ce57 8514peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8515{
8516 unsigned int bytes_read;
8517
8518 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8519}
8520
685af9cd
TT
8521/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8522 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8523
4bb7a0a7
DJ
8524 Return the corresponding abbrev, or NULL if the number is zero (indicating
8525 an empty DIE). In either case *BYTES_READ will be set to the length of
8526 the initial number. */
8527
8528static struct abbrev_info *
685af9cd
TT
8529peek_die_abbrev (const die_reader_specs &reader,
8530 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8531{
685af9cd 8532 dwarf2_cu *cu = reader.cu;
518817b3 8533 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8534 unsigned int abbrev_number
8535 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8536
8537 if (abbrev_number == 0)
8538 return NULL;
8539
685af9cd 8540 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8541 if (!abbrev)
8542 {
422b9917 8543 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8544 " at offset %s [in module %s]"),
422b9917 8545 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8546 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8547 }
8548
8549 return abbrev;
8550}
8551
93311388
DE
8552/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8553 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8554 DIE. Any children of the skipped DIEs will also be skipped. */
8555
d521ce57
TT
8556static const gdb_byte *
8557skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8558{
4bb7a0a7
DJ
8559 while (1)
8560 {
685af9cd
TT
8561 unsigned int bytes_read;
8562 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8563
4bb7a0a7
DJ
8564 if (abbrev == NULL)
8565 return info_ptr + bytes_read;
8566 else
dee91e82 8567 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8568 }
8569}
8570
93311388
DE
8571/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8572 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8573 abbrev corresponding to that skipped uleb128 should be passed in
8574 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8575 children. */
8576
d521ce57
TT
8577static const gdb_byte *
8578skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8579 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8580{
8581 unsigned int bytes_read;
8582 struct attribute attr;
dee91e82
DE
8583 bfd *abfd = reader->abfd;
8584 struct dwarf2_cu *cu = reader->cu;
d521ce57 8585 const gdb_byte *buffer = reader->buffer;
f664829e 8586 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8587 unsigned int form, i;
8588
8589 for (i = 0; i < abbrev->num_attrs; i++)
8590 {
8591 /* The only abbrev we care about is DW_AT_sibling. */
8592 if (abbrev->attrs[i].name == DW_AT_sibling)
8593 {
18a8505e
AT
8594 bool ignored;
8595 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8596 &ignored);
4bb7a0a7 8597 if (attr.form == DW_FORM_ref_addr)
b98664d3 8598 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8599 else
b9502d3f 8600 {
9c541725
PA
8601 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8602 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8603
8604 if (sibling_ptr < info_ptr)
b98664d3 8605 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
8606 else if (sibling_ptr > reader->buffer_end)
8607 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8608 else
8609 return sibling_ptr;
8610 }
4bb7a0a7
DJ
8611 }
8612
8613 /* If it isn't DW_AT_sibling, skip this attribute. */
8614 form = abbrev->attrs[i].form;
8615 skip_attribute:
8616 switch (form)
8617 {
4bb7a0a7 8618 case DW_FORM_ref_addr:
ae411497
TT
8619 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8620 and later it is offset sized. */
8621 if (cu->header.version == 2)
8622 info_ptr += cu->header.addr_size;
8623 else
8624 info_ptr += cu->header.offset_size;
8625 break;
36586728
TT
8626 case DW_FORM_GNU_ref_alt:
8627 info_ptr += cu->header.offset_size;
8628 break;
ae411497 8629 case DW_FORM_addr:
4bb7a0a7
DJ
8630 info_ptr += cu->header.addr_size;
8631 break;
8632 case DW_FORM_data1:
8633 case DW_FORM_ref1:
8634 case DW_FORM_flag:
8fe0f950 8635 case DW_FORM_strx1:
4bb7a0a7
DJ
8636 info_ptr += 1;
8637 break;
2dc7f7b3 8638 case DW_FORM_flag_present:
43988095 8639 case DW_FORM_implicit_const:
2dc7f7b3 8640 break;
4bb7a0a7
DJ
8641 case DW_FORM_data2:
8642 case DW_FORM_ref2:
8fe0f950 8643 case DW_FORM_strx2:
4bb7a0a7
DJ
8644 info_ptr += 2;
8645 break;
8fe0f950
AT
8646 case DW_FORM_strx3:
8647 info_ptr += 3;
8648 break;
4bb7a0a7
DJ
8649 case DW_FORM_data4:
8650 case DW_FORM_ref4:
8fe0f950 8651 case DW_FORM_strx4:
4bb7a0a7
DJ
8652 info_ptr += 4;
8653 break;
8654 case DW_FORM_data8:
8655 case DW_FORM_ref8:
55f1336d 8656 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8657 info_ptr += 8;
8658 break;
0224619f
JK
8659 case DW_FORM_data16:
8660 info_ptr += 16;
8661 break;
4bb7a0a7 8662 case DW_FORM_string:
9b1c24c8 8663 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8664 info_ptr += bytes_read;
8665 break;
2dc7f7b3 8666 case DW_FORM_sec_offset:
4bb7a0a7 8667 case DW_FORM_strp:
36586728 8668 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8669 info_ptr += cu->header.offset_size;
8670 break;
2dc7f7b3 8671 case DW_FORM_exprloc:
4bb7a0a7
DJ
8672 case DW_FORM_block:
8673 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8674 info_ptr += bytes_read;
8675 break;
8676 case DW_FORM_block1:
8677 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8678 break;
8679 case DW_FORM_block2:
8680 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8681 break;
8682 case DW_FORM_block4:
8683 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8684 break;
336d760d 8685 case DW_FORM_addrx:
cf532bd1 8686 case DW_FORM_strx:
4bb7a0a7
DJ
8687 case DW_FORM_sdata:
8688 case DW_FORM_udata:
8689 case DW_FORM_ref_udata:
3019eac3
DE
8690 case DW_FORM_GNU_addr_index:
8691 case DW_FORM_GNU_str_index:
18a8505e 8692 case DW_FORM_rnglistx:
d521ce57 8693 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8694 break;
8695 case DW_FORM_indirect:
8696 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8697 info_ptr += bytes_read;
8698 /* We need to continue parsing from here, so just go back to
8699 the top. */
8700 goto skip_attribute;
8701
8702 default:
3e43a32a
MS
8703 error (_("Dwarf Error: Cannot handle %s "
8704 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8705 dwarf_form_name (form),
8706 bfd_get_filename (abfd));
8707 }
8708 }
8709
8710 if (abbrev->has_children)
dee91e82 8711 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8712 else
8713 return info_ptr;
8714}
8715
93311388 8716/* Locate ORIG_PDI's sibling.
dee91e82 8717 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8718
d521ce57 8719static const gdb_byte *
dee91e82
DE
8720locate_pdi_sibling (const struct die_reader_specs *reader,
8721 struct partial_die_info *orig_pdi,
d521ce57 8722 const gdb_byte *info_ptr)
91c24f0a
DC
8723{
8724 /* Do we know the sibling already? */
72bf9492 8725
91c24f0a
DC
8726 if (orig_pdi->sibling)
8727 return orig_pdi->sibling;
8728
8729 /* Are there any children to deal with? */
8730
8731 if (!orig_pdi->has_children)
8732 return info_ptr;
8733
4bb7a0a7 8734 /* Skip the children the long way. */
91c24f0a 8735
dee91e82 8736 return skip_children (reader, info_ptr);
91c24f0a
DC
8737}
8738
257e7a09 8739/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8740 not NULL. */
c906108c 8741
891813be
TT
8742void
8743dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8744{
ed2dc618
SM
8745 struct dwarf2_per_objfile *dwarf2_per_objfile
8746 = get_dwarf2_per_objfile (objfile);
8747
077cbab2
TT
8748 gdb_assert (!readin);
8749 /* If this psymtab is constructed from a debug-only objfile, the
8750 has_section_at_zero flag will not necessarily be correct. We
8751 can get the correct value for this flag by looking at the data
8752 associated with the (presumably stripped) associated objfile. */
8753 if (objfile->separate_debug_objfile_backlink)
c906108c 8754 {
077cbab2
TT
8755 struct dwarf2_per_objfile *dpo_backlink
8756 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8757
077cbab2
TT
8758 dwarf2_per_objfile->has_section_at_zero
8759 = dpo_backlink->has_section_at_zero;
8760 }
98bfdba5 8761
8566b89b 8762 expand_psymtab (objfile);
95554aad 8763
ed2dc618 8764 process_cu_includes (dwarf2_per_objfile);
c906108c 8765}
9cdd5dbd
DE
8766\f
8767/* Reading in full CUs. */
c906108c 8768
10b3939b
DJ
8769/* Add PER_CU to the queue. */
8770
8771static void
95554aad
TT
8772queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8773 enum language pretend_language)
10b3939b 8774{
10b3939b 8775 per_cu->queued = 1;
39856def 8776 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8777}
8778
89e63ee4
DE
8779/* If PER_CU is not yet queued, add it to the queue.
8780 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8781 dependency.
0907af0c 8782 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8783 meaning either PER_CU is already queued or it is already loaded.
8784
8785 N.B. There is an invariant here that if a CU is queued then it is loaded.
8786 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8787
8788static int
89e63ee4 8789maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8790 struct dwarf2_per_cu_data *per_cu,
8791 enum language pretend_language)
8792{
8793 /* We may arrive here during partial symbol reading, if we need full
8794 DIEs to process an unusual case (e.g. template arguments). Do
8795 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8796 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8797 {
8798 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8799 return 1;
8800 return 0;
8801 }
8802
8803 /* Mark the dependence relation so that we don't flush PER_CU
8804 too early. */
89e63ee4
DE
8805 if (dependent_cu != NULL)
8806 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8807
8808 /* If it's already on the queue, we have nothing to do. */
8809 if (per_cu->queued)
8810 return 0;
8811
8812 /* If the compilation unit is already loaded, just mark it as
8813 used. */
8814 if (per_cu->cu != NULL)
8815 {
8816 per_cu->cu->last_used = 0;
8817 return 0;
8818 }
8819
8820 /* Add it to the queue. */
8821 queue_comp_unit (per_cu, pretend_language);
8822
8823 return 1;
8824}
8825
10b3939b
DJ
8826/* Process the queue. */
8827
8828static void
ed2dc618 8829process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8830{
b4f54984 8831 if (dwarf_read_debug)
45cfd468
DE
8832 {
8833 fprintf_unfiltered (gdb_stdlog,
8834 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8835 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8836 }
8837
03dd20cc
DJ
8838 /* The queue starts out with one item, but following a DIE reference
8839 may load a new CU, adding it to the end of the queue. */
39856def 8840 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8841 {
39856def
TT
8842 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8843
cc12ce38 8844 if ((dwarf2_per_objfile->using_index
39856def
TT
8845 ? !item.per_cu->v.quick->compunit_symtab
8846 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8847 /* Skip dummy CUs. */
39856def 8848 && item.per_cu->cu != NULL)
f4dc4d17 8849 {
39856def 8850 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8851 unsigned int debug_print_threshold;
247f5c4f 8852 char buf[100];
f4dc4d17 8853
247f5c4f 8854 if (per_cu->is_debug_types)
f4dc4d17 8855 {
247f5c4f
DE
8856 struct signatured_type *sig_type =
8857 (struct signatured_type *) per_cu;
8858
9d8780f0 8859 sprintf (buf, "TU %s at offset %s",
73be47f5 8860 hex_string (sig_type->signature),
9d8780f0 8861 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8862 /* There can be 100s of TUs.
8863 Only print them in verbose mode. */
8864 debug_print_threshold = 2;
f4dc4d17 8865 }
247f5c4f 8866 else
73be47f5 8867 {
9d8780f0
SM
8868 sprintf (buf, "CU at offset %s",
8869 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8870 debug_print_threshold = 1;
8871 }
247f5c4f 8872
b4f54984 8873 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8874 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8875
8876 if (per_cu->is_debug_types)
39856def 8877 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8878 else
39856def 8879 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8880
b4f54984 8881 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8882 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8883 }
10b3939b 8884
39856def
TT
8885 item.per_cu->queued = 0;
8886 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8887 }
8888
b4f54984 8889 if (dwarf_read_debug)
45cfd468
DE
8890 {
8891 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8892 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8893 }
10b3939b
DJ
8894}
8895
10b3939b
DJ
8896/* Read in full symbols for PST, and anything it depends on. */
8897
8566b89b
TT
8898void
8899dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8900{
8566b89b 8901 if (readin)
95554aad
TT
8902 return;
8903
0494dbec 8904 read_dependencies (objfile);
aaa75496 8905
b83470bf
TT
8906 dw2_do_instantiate_symtab (per_cu_data, false);
8907 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8908}
8909
dee91e82
DE
8910/* Trivial hash function for die_info: the hash value of a DIE
8911 is its offset in .debug_info for this objfile. */
10b3939b 8912
dee91e82
DE
8913static hashval_t
8914die_hash (const void *item)
10b3939b 8915{
9a3c8263 8916 const struct die_info *die = (const struct die_info *) item;
6502dd73 8917
9c541725 8918 return to_underlying (die->sect_off);
dee91e82 8919}
63d06c5c 8920
dee91e82
DE
8921/* Trivial comparison function for die_info structures: two DIEs
8922 are equal if they have the same offset. */
98bfdba5 8923
dee91e82
DE
8924static int
8925die_eq (const void *item_lhs, const void *item_rhs)
8926{
9a3c8263
SM
8927 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8928 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8929
9c541725 8930 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8931}
c906108c 8932
c0ab21c2 8933/* Load the DIEs associated with PER_CU into memory. */
c906108c 8934
dee91e82 8935static void
c0ab21c2
TT
8936load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8937 bool skip_partial,
8938 enum language pretend_language)
dee91e82 8939{
c0ab21c2
TT
8940 gdb_assert (! this_cu->is_debug_types);
8941
6751ebae 8942 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8943 if (reader.dummy_p)
8944 return;
8945
8946 struct dwarf2_cu *cu = reader.cu;
8947 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8948
dee91e82
DE
8949 gdb_assert (cu->die_hash == NULL);
8950 cu->die_hash =
8951 htab_create_alloc_ex (cu->header.length / 12,
8952 die_hash,
8953 die_eq,
8954 NULL,
8955 &cu->comp_unit_obstack,
8956 hashtab_obstack_allocate,
8957 dummy_obstack_deallocate);
e142c38c 8958
3e225074 8959 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8960 reader.comp_unit_die->child
8961 = read_die_and_siblings (&reader, reader.info_ptr,
8962 &info_ptr, reader.comp_unit_die);
8963 cu->dies = reader.comp_unit_die;
dee91e82 8964 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8965
8966 /* We try not to read any attributes in this function, because not
9cdd5dbd 8967 all CUs needed for references have been loaded yet, and symbol
10b3939b 8968 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8969 or we won't be able to build types correctly.
8970 Similarly, if we do not read the producer, we can not apply
8971 producer-specific interpretation. */
c0ab21c2 8972 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8973
8974 reader.keep ();
10b3939b
DJ
8975}
8976
3da10d80
KS
8977/* Add a DIE to the delayed physname list. */
8978
8979static void
8980add_to_method_list (struct type *type, int fnfield_index, int index,
8981 const char *name, struct die_info *die,
8982 struct dwarf2_cu *cu)
8983{
8984 struct delayed_method_info mi;
8985 mi.type = type;
8986 mi.fnfield_index = fnfield_index;
8987 mi.index = index;
8988 mi.name = name;
8989 mi.die = die;
c89b44cd 8990 cu->method_list.push_back (mi);
3da10d80
KS
8991}
8992
3693fdb3
PA
8993/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8994 "const" / "volatile". If so, decrements LEN by the length of the
8995 modifier and return true. Otherwise return false. */
8996
8997template<size_t N>
8998static bool
8999check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9000{
9001 size_t mod_len = sizeof (mod) - 1;
9002 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9003 {
9004 len -= mod_len;
9005 return true;
9006 }
9007 return false;
9008}
9009
3da10d80
KS
9010/* Compute the physnames of any methods on the CU's method list.
9011
9012 The computation of method physnames is delayed in order to avoid the
9013 (bad) condition that one of the method's formal parameters is of an as yet
9014 incomplete type. */
9015
9016static void
9017compute_delayed_physnames (struct dwarf2_cu *cu)
9018{
3693fdb3 9019 /* Only C++ delays computing physnames. */
c89b44cd 9020 if (cu->method_list.empty ())
3693fdb3
PA
9021 return;
9022 gdb_assert (cu->language == language_cplus);
9023
52941706 9024 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9025 {
1d06ead6 9026 const char *physname;
3da10d80 9027 struct fn_fieldlist *fn_flp
c89b44cd
TT
9028 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9029 physname = dwarf2_physname (mi.name, mi.die, cu);
9030 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9031 = physname ? physname : "";
3693fdb3
PA
9032
9033 /* Since there's no tag to indicate whether a method is a
9034 const/volatile overload, extract that information out of the
9035 demangled name. */
9036 if (physname != NULL)
9037 {
9038 size_t len = strlen (physname);
9039
9040 while (1)
9041 {
9042 if (physname[len] == ')') /* shortcut */
9043 break;
9044 else if (check_modifier (physname, len, " const"))
c89b44cd 9045 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9046 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9047 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9048 else
9049 break;
9050 }
9051 }
3da10d80 9052 }
c89b44cd
TT
9053
9054 /* The list is no longer needed. */
9055 cu->method_list.clear ();
3da10d80
KS
9056}
9057
a766d390
DE
9058/* Go objects should be embedded in a DW_TAG_module DIE,
9059 and it's not clear if/how imported objects will appear.
9060 To keep Go support simple until that's worked out,
9061 go back through what we've read and create something usable.
9062 We could do this while processing each DIE, and feels kinda cleaner,
9063 but that way is more invasive.
9064 This is to, for example, allow the user to type "p var" or "b main"
9065 without having to specify the package name, and allow lookups
9066 of module.object to work in contexts that use the expression
9067 parser. */
9068
9069static void
9070fixup_go_packaging (struct dwarf2_cu *cu)
9071{
421d1616 9072 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9073 struct pending *list;
9074 int i;
9075
c24bdb02 9076 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9077 list != NULL;
9078 list = list->next)
a766d390
DE
9079 {
9080 for (i = 0; i < list->nsyms; ++i)
9081 {
9082 struct symbol *sym = list->symbol[i];
9083
c1b5c1eb 9084 if (sym->language () == language_go
a766d390
DE
9085 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9086 {
421d1616
TT
9087 gdb::unique_xmalloc_ptr<char> this_package_name
9088 (go_symbol_package_name (sym));
a766d390
DE
9089
9090 if (this_package_name == NULL)
9091 continue;
9092 if (package_name == NULL)
421d1616 9093 package_name = std::move (this_package_name);
a766d390
DE
9094 else
9095 {
518817b3
SM
9096 struct objfile *objfile
9097 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9098 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9099 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9100 (symbol_symtab (sym) != NULL
9101 ? symtab_to_filename_for_display
9102 (symbol_symtab (sym))
e3b94546 9103 : objfile_name (objfile)),
421d1616 9104 this_package_name.get (), package_name.get ());
a766d390
DE
9105 }
9106 }
9107 }
9108 }
9109
9110 if (package_name != NULL)
9111 {
518817b3 9112 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9113 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9114 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9115 saved_package_name);
a766d390
DE
9116 struct symbol *sym;
9117
e623cf5d 9118 sym = allocate_symbol (objfile);
d3ecddab 9119 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9120 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9121 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9122 e.g., "main" finds the "main" module and not C's main(). */
9123 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9124 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9125 SYMBOL_TYPE (sym) = type;
9126
c24bdb02 9127 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9128 }
9129}
9130
c9317f21
TT
9131/* Allocate a fully-qualified name consisting of the two parts on the
9132 obstack. */
9133
9134static const char *
9135rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9136{
9137 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9138}
9139
9140/* A helper that allocates a struct discriminant_info to attach to a
9141 union type. */
9142
9143static struct discriminant_info *
9144alloc_discriminant_info (struct type *type, int discriminant_index,
9145 int default_index)
9146{
9147 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9148 gdb_assert (discriminant_index == -1
9149 || (discriminant_index >= 0
9150 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9151 gdb_assert (default_index == -1
c7b15a66 9152 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9153
9154 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9155
9156 struct discriminant_info *disc
9157 = ((struct discriminant_info *)
9158 TYPE_ZALLOC (type,
9159 offsetof (struct discriminant_info, discriminants)
9160 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9161 disc->default_index = default_index;
9162 disc->discriminant_index = discriminant_index;
9163
9164 struct dynamic_prop prop;
9165 prop.kind = PROP_UNDEFINED;
9166 prop.data.baton = disc;
9167
9168 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9169
9170 return disc;
9171}
9172
9173/* Some versions of rustc emitted enums in an unusual way.
9174
9175 Ordinary enums were emitted as unions. The first element of each
9176 structure in the union was named "RUST$ENUM$DISR". This element
9177 held the discriminant.
9178
9179 These versions of Rust also implemented the "non-zero"
9180 optimization. When the enum had two values, and one is empty and
9181 the other holds a pointer that cannot be zero, the pointer is used
9182 as the discriminant, with a zero value meaning the empty variant.
9183 Here, the union's first member is of the form
9184 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9185 where the fieldnos are the indices of the fields that should be
9186 traversed in order to find the field (which may be several fields deep)
9187 and the variantname is the name of the variant of the case when the
9188 field is zero.
9189
9190 This function recognizes whether TYPE is of one of these forms,
9191 and, if so, smashes it to be a variant type. */
9192
9193static void
9194quirk_rust_enum (struct type *type, struct objfile *objfile)
9195{
9196 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9197
9198 /* We don't need to deal with empty enums. */
9199 if (TYPE_NFIELDS (type) == 0)
9200 return;
9201
9202#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9203 if (TYPE_NFIELDS (type) == 1
9204 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9205 {
9206 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9207
9208 /* Decode the field name to find the offset of the
9209 discriminant. */
9210 ULONGEST bit_offset = 0;
9211 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9212 while (name[0] >= '0' && name[0] <= '9')
9213 {
9214 char *tail;
9215 unsigned long index = strtoul (name, &tail, 10);
9216 name = tail;
9217 if (*name != '$'
9218 || index >= TYPE_NFIELDS (field_type)
9219 || (TYPE_FIELD_LOC_KIND (field_type, index)
9220 != FIELD_LOC_KIND_BITPOS))
9221 {
b98664d3 9222 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9223 "[in module %s]"),
9224 TYPE_FIELD_NAME (type, 0),
9225 objfile_name (objfile));
9226 return;
9227 }
9228 ++name;
9229
9230 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9231 field_type = TYPE_FIELD_TYPE (field_type, index);
9232 }
9233
9234 /* Make a union to hold the variants. */
9235 struct type *union_type = alloc_type (objfile);
9236 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9237 TYPE_NFIELDS (union_type) = 3;
9238 TYPE_FIELDS (union_type)
9239 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9240 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9241 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9242
9243 /* Put the discriminant must at index 0. */
9244 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9245 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9246 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9247 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9248
9249 /* The order of fields doesn't really matter, so put the real
9250 field at index 1 and the data-less field at index 2. */
9251 struct discriminant_info *disc
9252 = alloc_discriminant_info (union_type, 0, 1);
9253 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9254 TYPE_FIELD_NAME (union_type, 1)
9255 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9256 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9257 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9258 TYPE_FIELD_NAME (union_type, 1));
9259
9260 const char *dataless_name
9261 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9262 name);
9263 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9264 dataless_name);
9265 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9266 /* NAME points into the original discriminant name, which
9267 already has the correct lifetime. */
9268 TYPE_FIELD_NAME (union_type, 2) = name;
9269 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9270 disc->discriminants[2] = 0;
9271
9272 /* Smash this type to be a structure type. We have to do this
9273 because the type has already been recorded. */
9274 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9275 TYPE_NFIELDS (type) = 1;
9276 TYPE_FIELDS (type)
9277 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9278
9279 /* Install the variant part. */
9280 TYPE_FIELD_TYPE (type, 0) = union_type;
9281 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9282 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9283 }
77c2dba3
TT
9284 /* A union with a single anonymous field is probably an old-style
9285 univariant enum. */
9286 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9287 {
c9317f21
TT
9288 /* Smash this type to be a structure type. We have to do this
9289 because the type has already been recorded. */
9290 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9291
9292 /* Make a union to hold the variants. */
9293 struct type *union_type = alloc_type (objfile);
9294 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9295 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9296 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9297 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9298 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9299
9300 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9301 const char *variant_name
9302 = rust_last_path_segment (TYPE_NAME (field_type));
9303 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9304 TYPE_NAME (field_type)
9305 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9306 TYPE_NAME (type), variant_name);
c9317f21
TT
9307
9308 /* Install the union in the outer struct type. */
9309 TYPE_NFIELDS (type) = 1;
9310 TYPE_FIELDS (type)
9311 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9312 TYPE_FIELD_TYPE (type, 0) = union_type;
9313 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9314 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9315
9316 alloc_discriminant_info (union_type, -1, 0);
9317 }
9318 else
9319 {
9320 struct type *disr_type = nullptr;
9321 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9322 {
9323 disr_type = TYPE_FIELD_TYPE (type, i);
9324
a037790e
TT
9325 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9326 {
9327 /* All fields of a true enum will be structs. */
9328 return;
9329 }
9330 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9331 {
9332 /* Could be data-less variant, so keep going. */
a037790e 9333 disr_type = nullptr;
c9317f21
TT
9334 }
9335 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9336 "RUST$ENUM$DISR") != 0)
9337 {
9338 /* Not a Rust enum. */
9339 return;
9340 }
9341 else
9342 {
9343 /* Found one. */
9344 break;
9345 }
9346 }
9347
9348 /* If we got here without a discriminant, then it's probably
9349 just a union. */
9350 if (disr_type == nullptr)
9351 return;
9352
9353 /* Smash this type to be a structure type. We have to do this
9354 because the type has already been recorded. */
9355 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9356
9357 /* Make a union to hold the variants. */
9358 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9359 struct type *union_type = alloc_type (objfile);
9360 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9361 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9362 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9363 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9364 TYPE_FIELDS (union_type)
9365 = (struct field *) TYPE_ZALLOC (union_type,
9366 (TYPE_NFIELDS (union_type)
9367 * sizeof (struct field)));
9368
9369 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9370 TYPE_NFIELDS (type) * sizeof (struct field));
9371
9372 /* Install the discriminant at index 0 in the union. */
9373 TYPE_FIELD (union_type, 0) = *disr_field;
9374 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9375 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9376
9377 /* Install the union in the outer struct type. */
9378 TYPE_FIELD_TYPE (type, 0) = union_type;
9379 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9380 TYPE_NFIELDS (type) = 1;
9381
9382 /* Set the size and offset of the union type. */
9383 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9384
9385 /* We need a way to find the correct discriminant given a
9386 variant name. For convenience we build a map here. */
9387 struct type *enum_type = FIELD_TYPE (*disr_field);
9388 std::unordered_map<std::string, ULONGEST> discriminant_map;
9389 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9390 {
9391 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9392 {
9393 const char *name
9394 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9395 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9396 }
9397 }
9398
9399 int n_fields = TYPE_NFIELDS (union_type);
9400 struct discriminant_info *disc
9401 = alloc_discriminant_info (union_type, 0, -1);
9402 /* Skip the discriminant here. */
9403 for (int i = 1; i < n_fields; ++i)
9404 {
9405 /* Find the final word in the name of this variant's type.
9406 That name can be used to look up the correct
9407 discriminant. */
9408 const char *variant_name
9409 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9410 i)));
9411
9412 auto iter = discriminant_map.find (variant_name);
9413 if (iter != discriminant_map.end ())
9414 disc->discriminants[i] = iter->second;
9415
bedda9ac 9416 /* Remove the discriminant field, if it exists. */
c9317f21 9417 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9418 if (TYPE_NFIELDS (sub_type) > 0)
9419 {
9420 --TYPE_NFIELDS (sub_type);
9421 ++TYPE_FIELDS (sub_type);
9422 }
c9317f21
TT
9423 TYPE_FIELD_NAME (union_type, i) = variant_name;
9424 TYPE_NAME (sub_type)
9425 = rust_fully_qualify (&objfile->objfile_obstack,
9426 TYPE_NAME (type), variant_name);
9427 }
9428 }
9429}
9430
9431/* Rewrite some Rust unions to be structures with variants parts. */
9432
9433static void
9434rust_union_quirks (struct dwarf2_cu *cu)
9435{
9436 gdb_assert (cu->language == language_rust);
52941706
SM
9437 for (type *type_ : cu->rust_unions)
9438 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9439 /* We don't need this any more. */
9440 cu->rust_unions.clear ();
c9317f21
TT
9441}
9442
95554aad
TT
9443/* Return the symtab for PER_CU. This works properly regardless of
9444 whether we're using the index or psymtabs. */
9445
43f3e411
DE
9446static struct compunit_symtab *
9447get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9448{
ed2dc618 9449 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9450 ? per_cu->v.quick->compunit_symtab
9451 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9452}
9453
9454/* A helper function for computing the list of all symbol tables
9455 included by PER_CU. */
9456
9457static void
4c39bc03 9458recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9459 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9460 struct dwarf2_per_cu_data *per_cu,
43f3e411 9461 struct compunit_symtab *immediate_parent)
95554aad
TT
9462{
9463 void **slot;
43f3e411 9464 struct compunit_symtab *cust;
95554aad
TT
9465
9466 slot = htab_find_slot (all_children, per_cu, INSERT);
9467 if (*slot != NULL)
9468 {
9469 /* This inclusion and its children have been processed. */
9470 return;
9471 }
9472
9473 *slot = per_cu;
9474 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9475 cust = get_compunit_symtab (per_cu);
9476 if (cust != NULL)
ec94af83
DE
9477 {
9478 /* If this is a type unit only add its symbol table if we haven't
9479 seen it yet (type unit per_cu's can share symtabs). */
9480 if (per_cu->is_debug_types)
9481 {
43f3e411 9482 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9483 if (*slot == NULL)
9484 {
43f3e411 9485 *slot = cust;
4c39bc03 9486 result->push_back (cust);
43f3e411
DE
9487 if (cust->user == NULL)
9488 cust->user = immediate_parent;
ec94af83
DE
9489 }
9490 }
9491 else
f9125b6c 9492 {
4c39bc03 9493 result->push_back (cust);
43f3e411
DE
9494 if (cust->user == NULL)
9495 cust->user = immediate_parent;
f9125b6c 9496 }
ec94af83 9497 }
95554aad 9498
ae640021
AB
9499 if (!per_cu->imported_symtabs_empty ())
9500 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9501 {
9502 recursively_compute_inclusions (result, all_children,
9503 all_type_symtabs, ptr, cust);
9504 }
95554aad
TT
9505}
9506
43f3e411 9507/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9508 PER_CU. */
9509
9510static void
43f3e411 9511compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9512{
f4dc4d17
DE
9513 gdb_assert (! per_cu->is_debug_types);
9514
ae640021 9515 if (!per_cu->imported_symtabs_empty ())
95554aad 9516 {
ae640021 9517 int len;
4c39bc03 9518 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9519 htab_t all_children, all_type_symtabs;
43f3e411 9520 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9521
9522 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9523 if (cust == NULL)
95554aad
TT
9524 return;
9525
9526 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9527 NULL, xcalloc, xfree);
ec94af83
DE
9528 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9529 NULL, xcalloc, xfree);
95554aad 9530
ae640021 9531 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9532 {
9533 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9534 all_type_symtabs, ptr, cust);
ec94af83 9535 }
95554aad 9536
ec94af83 9537 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9538 len = result_symtabs.size ();
43f3e411 9539 cust->includes
ed2dc618 9540 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9541 struct compunit_symtab *, len + 1);
4c39bc03
TT
9542 memcpy (cust->includes, result_symtabs.data (),
9543 len * sizeof (compunit_symtab *));
43f3e411 9544 cust->includes[len] = NULL;
95554aad 9545
95554aad 9546 htab_delete (all_children);
ec94af83 9547 htab_delete (all_type_symtabs);
95554aad
TT
9548 }
9549}
9550
9551/* Compute the 'includes' field for the symtabs of all the CUs we just
9552 read. */
9553
9554static void
ed2dc618 9555process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9556{
71b73764 9557 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9558 {
9559 if (! iter->is_debug_types)
43f3e411 9560 compute_compunit_symtab_includes (iter);
f4dc4d17 9561 }
95554aad 9562
c5d0225d 9563 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9564}
9565
9cdd5dbd 9566/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9567 already been loaded into memory. */
9568
9569static void
95554aad
TT
9570process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9571 enum language pretend_language)
10b3939b 9572{
10b3939b 9573 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9574 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9575 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9576 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9577 CORE_ADDR lowpc, highpc;
43f3e411 9578 struct compunit_symtab *cust;
10b3939b 9579 CORE_ADDR baseaddr;
4359dff1 9580 struct block *static_block;
3e29f34a 9581 CORE_ADDR addr;
10b3939b 9582
b3b3bada 9583 baseaddr = objfile->text_section_offset ();
10b3939b 9584
c89b44cd
TT
9585 /* Clear the list here in case something was left over. */
9586 cu->method_list.clear ();
10b3939b 9587
95554aad
TT
9588 cu->language = pretend_language;
9589 cu->language_defn = language_def (cu->language);
9590
c906108c 9591 /* Do line number decoding in read_file_scope () */
10b3939b 9592 process_die (cu->dies, cu);
c906108c 9593
a766d390
DE
9594 /* For now fudge the Go package. */
9595 if (cu->language == language_go)
9596 fixup_go_packaging (cu);
9597
5f48f8f3 9598 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9599 should be complete, and it should now be safe to compute all of the
9600 physnames. */
9601 compute_delayed_physnames (cu);
3da10d80 9602
c9317f21
TT
9603 if (cu->language == language_rust)
9604 rust_union_quirks (cu);
9605
fae299cd
DC
9606 /* Some compilers don't define a DW_AT_high_pc attribute for the
9607 compilation unit. If the DW_AT_high_pc is missing, synthesize
9608 it, by scanning the DIE's below the compilation unit. */
10b3939b 9609 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9610
3e29f34a 9611 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9612 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9613
9614 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9615 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9616 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9617 addrmap to help ensure it has an accurate map of pc values belonging to
9618 this comp unit. */
9619 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9620
c24bdb02 9621 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9622 SECT_OFF_TEXT (objfile),
9623 0);
c906108c 9624
43f3e411 9625 if (cust != NULL)
c906108c 9626 {
df15bd07 9627 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9628
8be455d7
JK
9629 /* Set symtab language to language from DW_AT_language. If the
9630 compilation is from a C file generated by language preprocessors, do
9631 not set the language if it was already deduced by start_subfile. */
43f3e411 9632 if (!(cu->language == language_c
40e3ad0e 9633 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9634 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9635
9636 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9637 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9638 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9639 there were bugs in prologue debug info, fixed later in GCC-4.5
9640 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9641
9642 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9643 needed, it would be wrong due to missing DW_AT_producer there.
9644
9645 Still one can confuse GDB by using non-standard GCC compilation
9646 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9647 */
ab260dad 9648 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9649 cust->locations_valid = 1;
e0d00bc7
JK
9650
9651 if (gcc_4_minor >= 5)
43f3e411 9652 cust->epilogue_unwind_valid = 1;
96408a79 9653
43f3e411 9654 cust->call_site_htab = cu->call_site_htab;
c906108c 9655 }
9291a0cd
TT
9656
9657 if (dwarf2_per_objfile->using_index)
43f3e411 9658 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9659 else
9660 {
891813be 9661 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9662 pst->compunit_symtab = cust;
6d94535f 9663 pst->readin = true;
9291a0cd 9664 }
c906108c 9665
95554aad 9666 /* Push it for inclusion processing later. */
c5d0225d 9667 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9668
9669 /* Not needed any more. */
c24bdb02 9670 cu->reset_builder ();
f4dc4d17 9671}
45cfd468 9672
f4dc4d17
DE
9673/* Generate full symbol information for type unit PER_CU, whose DIEs have
9674 already been loaded into memory. */
9675
9676static void
9677process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9678 enum language pretend_language)
9679{
9680 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9681 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9682 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9683 struct compunit_symtab *cust;
0186c6a7
DE
9684 struct signatured_type *sig_type;
9685
9686 gdb_assert (per_cu->is_debug_types);
9687 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9688
c89b44cd
TT
9689 /* Clear the list here in case something was left over. */
9690 cu->method_list.clear ();
f4dc4d17 9691
f4dc4d17
DE
9692 cu->language = pretend_language;
9693 cu->language_defn = language_def (cu->language);
9694
9695 /* The symbol tables are set up in read_type_unit_scope. */
9696 process_die (cu->dies, cu);
9697
9698 /* For now fudge the Go package. */
9699 if (cu->language == language_go)
9700 fixup_go_packaging (cu);
9701
5f48f8f3 9702 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9703 should be complete, and it should now be safe to compute all of the
9704 physnames. */
9705 compute_delayed_physnames (cu);
f4dc4d17 9706
c9317f21
TT
9707 if (cu->language == language_rust)
9708 rust_union_quirks (cu);
9709
f4dc4d17
DE
9710 /* TUs share symbol tables.
9711 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9712 of it with end_expandable_symtab. Otherwise, complete the addition of
9713 this TU's symbols to the existing symtab. */
43f3e411 9714 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9715 {
c24bdb02
KS
9716 buildsym_compunit *builder = cu->get_builder ();
9717 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9718 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9719
43f3e411 9720 if (cust != NULL)
f4dc4d17
DE
9721 {
9722 /* Set symtab language to language from DW_AT_language. If the
9723 compilation is from a C file generated by language preprocessors,
9724 do not set the language if it was already deduced by
9725 start_subfile. */
43f3e411
DE
9726 if (!(cu->language == language_c
9727 && COMPUNIT_FILETABS (cust)->language != language_c))
9728 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9729 }
9730 }
9731 else
9732 {
c24bdb02 9733 cu->get_builder ()->augment_type_symtab ();
43f3e411 9734 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9735 }
9736
9737 if (dwarf2_per_objfile->using_index)
43f3e411 9738 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9739 else
9740 {
891813be 9741 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9742 pst->compunit_symtab = cust;
6d94535f 9743 pst->readin = true;
45cfd468 9744 }
804d2729
TT
9745
9746 /* Not needed any more. */
c24bdb02 9747 cu->reset_builder ();
c906108c
SS
9748}
9749
95554aad
TT
9750/* Process an imported unit DIE. */
9751
9752static void
9753process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9754{
9755 struct attribute *attr;
9756
f4dc4d17
DE
9757 /* For now we don't handle imported units in type units. */
9758 if (cu->per_cu->is_debug_types)
9759 {
9760 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9761 " supported in type units [in module %s]"),
518817b3 9762 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9763 }
9764
95554aad
TT
9765 attr = dwarf2_attr (die, DW_AT_import, cu);
9766 if (attr != NULL)
9767 {
9c541725
PA
9768 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9769 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9770 dwarf2_per_cu_data *per_cu
e3b94546 9771 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9772 cu->per_cu->dwarf2_per_objfile);
95554aad 9773
58990295
TV
9774 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9775 into another compilation unit, at root level. Regard this as a hint,
9776 and ignore it. */
9777 if (die->parent && die->parent->parent == NULL
9778 && per_cu->unit_type == DW_UT_compile
9779 && per_cu->lang == language_cplus)
9780 return;
9781
69d751e3 9782 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9783 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9784 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9785
ae640021 9786 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9787 }
9788}
9789
4c8aa72d
PA
9790/* RAII object that represents a process_die scope: i.e.,
9791 starts/finishes processing a DIE. */
9792class process_die_scope
adde2bff 9793{
4c8aa72d
PA
9794public:
9795 process_die_scope (die_info *die, dwarf2_cu *cu)
9796 : m_die (die), m_cu (cu)
9797 {
9798 /* We should only be processing DIEs not already in process. */
9799 gdb_assert (!m_die->in_process);
9800 m_die->in_process = true;
9801 }
8c3cb9fa 9802
4c8aa72d
PA
9803 ~process_die_scope ()
9804 {
9805 m_die->in_process = false;
9806
9807 /* If we're done processing the DIE for the CU that owns the line
9808 header, we don't need the line header anymore. */
9809 if (m_cu->line_header_die_owner == m_die)
9810 {
9811 delete m_cu->line_header;
9812 m_cu->line_header = NULL;
9813 m_cu->line_header_die_owner = NULL;
9814 }
9815 }
9816
9817private:
9818 die_info *m_die;
9819 dwarf2_cu *m_cu;
9820};
adde2bff 9821
c906108c
SS
9822/* Process a die and its children. */
9823
9824static void
e7c27a73 9825process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9826{
4c8aa72d 9827 process_die_scope scope (die, cu);
adde2bff 9828
c906108c
SS
9829 switch (die->tag)
9830 {
9831 case DW_TAG_padding:
9832 break;
9833 case DW_TAG_compile_unit:
95554aad 9834 case DW_TAG_partial_unit:
e7c27a73 9835 read_file_scope (die, cu);
c906108c 9836 break;
348e048f
DE
9837 case DW_TAG_type_unit:
9838 read_type_unit_scope (die, cu);
9839 break;
c906108c 9840 case DW_TAG_subprogram:
0a4b0913
AB
9841 /* Nested subprograms in Fortran get a prefix. */
9842 if (cu->language == language_fortran
9843 && die->parent != NULL
9844 && die->parent->tag == DW_TAG_subprogram)
9845 cu->processing_has_namespace_info = true;
9846 /* Fall through. */
c906108c 9847 case DW_TAG_inlined_subroutine:
edb3359d 9848 read_func_scope (die, cu);
c906108c
SS
9849 break;
9850 case DW_TAG_lexical_block:
14898363
L
9851 case DW_TAG_try_block:
9852 case DW_TAG_catch_block:
e7c27a73 9853 read_lexical_block_scope (die, cu);
c906108c 9854 break;
216f72a1 9855 case DW_TAG_call_site:
96408a79
SA
9856 case DW_TAG_GNU_call_site:
9857 read_call_site_scope (die, cu);
9858 break;
c906108c 9859 case DW_TAG_class_type:
680b30c7 9860 case DW_TAG_interface_type:
c906108c
SS
9861 case DW_TAG_structure_type:
9862 case DW_TAG_union_type:
134d01f1 9863 process_structure_scope (die, cu);
c906108c
SS
9864 break;
9865 case DW_TAG_enumeration_type:
134d01f1 9866 process_enumeration_scope (die, cu);
c906108c 9867 break;
134d01f1 9868
f792889a
DJ
9869 /* These dies have a type, but processing them does not create
9870 a symbol or recurse to process the children. Therefore we can
9871 read them on-demand through read_type_die. */
c906108c 9872 case DW_TAG_subroutine_type:
72019c9c 9873 case DW_TAG_set_type:
c906108c 9874 case DW_TAG_array_type:
c906108c 9875 case DW_TAG_pointer_type:
c906108c 9876 case DW_TAG_ptr_to_member_type:
c906108c 9877 case DW_TAG_reference_type:
4297a3f0 9878 case DW_TAG_rvalue_reference_type:
c906108c 9879 case DW_TAG_string_type:
c906108c 9880 break;
134d01f1 9881
c906108c 9882 case DW_TAG_base_type:
a02abb62 9883 case DW_TAG_subrange_type:
cb249c71 9884 case DW_TAG_typedef:
134d01f1
DJ
9885 /* Add a typedef symbol for the type definition, if it has a
9886 DW_AT_name. */
f792889a 9887 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9888 break;
c906108c 9889 case DW_TAG_common_block:
e7c27a73 9890 read_common_block (die, cu);
c906108c
SS
9891 break;
9892 case DW_TAG_common_inclusion:
9893 break;
d9fa45fe 9894 case DW_TAG_namespace:
9068261f 9895 cu->processing_has_namespace_info = true;
e7c27a73 9896 read_namespace (die, cu);
d9fa45fe 9897 break;
5d7cb8df 9898 case DW_TAG_module:
9068261f 9899 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9900 read_module (die, cu);
9901 break;
d9fa45fe 9902 case DW_TAG_imported_declaration:
9068261f 9903 cu->processing_has_namespace_info = true;
74921315
KS
9904 if (read_namespace_alias (die, cu))
9905 break;
86a73007
TT
9906 /* The declaration is not a global namespace alias. */
9907 /* Fall through. */
d9fa45fe 9908 case DW_TAG_imported_module:
9068261f 9909 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9910 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9911 || cu->language != language_fortran))
b98664d3 9912 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9913 dwarf_tag_name (die->tag));
9914 read_import_statement (die, cu);
d9fa45fe 9915 break;
95554aad
TT
9916
9917 case DW_TAG_imported_unit:
9918 process_imported_unit_die (die, cu);
9919 break;
9920
71a3c369
TT
9921 case DW_TAG_variable:
9922 read_variable (die, cu);
9923 break;
9924
c906108c 9925 default:
e7c27a73 9926 new_symbol (die, NULL, cu);
c906108c
SS
9927 break;
9928 }
9929}
ca69b9e6
DE
9930\f
9931/* DWARF name computation. */
c906108c 9932
94af9270
KS
9933/* A helper function for dwarf2_compute_name which determines whether DIE
9934 needs to have the name of the scope prepended to the name listed in the
9935 die. */
9936
9937static int
9938die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9939{
1c809c68
TT
9940 struct attribute *attr;
9941
94af9270
KS
9942 switch (die->tag)
9943 {
9944 case DW_TAG_namespace:
9945 case DW_TAG_typedef:
9946 case DW_TAG_class_type:
9947 case DW_TAG_interface_type:
9948 case DW_TAG_structure_type:
9949 case DW_TAG_union_type:
9950 case DW_TAG_enumeration_type:
9951 case DW_TAG_enumerator:
9952 case DW_TAG_subprogram:
08a76f8a 9953 case DW_TAG_inlined_subroutine:
94af9270 9954 case DW_TAG_member:
74921315 9955 case DW_TAG_imported_declaration:
94af9270
KS
9956 return 1;
9957
9958 case DW_TAG_variable:
c2b0a229 9959 case DW_TAG_constant:
94af9270
KS
9960 /* We only need to prefix "globally" visible variables. These include
9961 any variable marked with DW_AT_external or any variable that
9962 lives in a namespace. [Variables in anonymous namespaces
9963 require prefixing, but they are not DW_AT_external.] */
9964
9965 if (dwarf2_attr (die, DW_AT_specification, cu))
9966 {
9967 struct dwarf2_cu *spec_cu = cu;
9a619af0 9968
94af9270
KS
9969 return die_needs_namespace (die_specification (die, &spec_cu),
9970 spec_cu);
9971 }
9972
1c809c68 9973 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9974 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9975 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9976 return 0;
9977 /* A variable in a lexical block of some kind does not need a
9978 namespace, even though in C++ such variables may be external
9979 and have a mangled name. */
9980 if (die->parent->tag == DW_TAG_lexical_block
9981 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9982 || die->parent->tag == DW_TAG_catch_block
9983 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9984 return 0;
9985 return 1;
94af9270
KS
9986
9987 default:
9988 return 0;
9989 }
9990}
9991
73b9be8b
KS
9992/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9993 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9994 defined for the given DIE. */
9995
9996static struct attribute *
9997dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9998{
9999 struct attribute *attr;
10000
10001 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10002 if (attr == NULL)
10003 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10004
10005 return attr;
10006}
10007
10008/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10009 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10010 defined for the given DIE. */
10011
10012static const char *
10013dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10014{
10015 const char *linkage_name;
10016
10017 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10018 if (linkage_name == NULL)
10019 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10020
10021 return linkage_name;
10022}
10023
94af9270 10024/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10025 compute the physname for the object, which include a method's:
9c37b5ae 10026 - formal parameters (C++),
a766d390 10027 - receiver type (Go),
a766d390
DE
10028
10029 The term "physname" is a bit confusing.
10030 For C++, for example, it is the demangled name.
10031 For Go, for example, it's the mangled name.
94af9270 10032
af6b7be1
JB
10033 For Ada, return the DIE's linkage name rather than the fully qualified
10034 name. PHYSNAME is ignored..
10035
94af9270
KS
10036 The result is allocated on the objfile_obstack and canonicalized. */
10037
10038static const char *
15d034d0
TT
10039dwarf2_compute_name (const char *name,
10040 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10041 int physname)
10042{
518817b3 10043 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10044
94af9270
KS
10045 if (name == NULL)
10046 name = dwarf2_name (die, cu);
10047
2ee7123e
DE
10048 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10049 but otherwise compute it by typename_concat inside GDB.
10050 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10051 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10052 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10053 will set the demangled name to the result of dwarf2_full_name, and it is
10054 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10055 if (cu->language == language_ada
10056 || (cu->language == language_fortran && physname))
10057 {
10058 /* For Ada unit, we prefer the linkage name over the name, as
10059 the former contains the exported name, which the user expects
10060 to be able to reference. Ideally, we want the user to be able
10061 to reference this entity using either natural or linkage name,
10062 but we haven't started looking at this enhancement yet. */
73b9be8b 10063 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10064
2ee7123e
DE
10065 if (linkage_name != NULL)
10066 return linkage_name;
f55ee35c
JK
10067 }
10068
94af9270
KS
10069 /* These are the only languages we know how to qualify names in. */
10070 if (name != NULL
9c37b5ae 10071 && (cu->language == language_cplus
c44af4eb
TT
10072 || cu->language == language_fortran || cu->language == language_d
10073 || cu->language == language_rust))
94af9270
KS
10074 {
10075 if (die_needs_namespace (die, cu))
10076 {
0d5cff50 10077 const char *prefix;
34a68019 10078 const char *canonical_name = NULL;
94af9270 10079
d7e74731
PA
10080 string_file buf;
10081
94af9270 10082 prefix = determine_prefix (die, cu);
94af9270
KS
10083 if (*prefix != '\0')
10084 {
43816ebc
TT
10085 gdb::unique_xmalloc_ptr<char> prefixed_name
10086 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10087
43816ebc 10088 buf.puts (prefixed_name.get ());
94af9270
KS
10089 }
10090 else
d7e74731 10091 buf.puts (name);
94af9270 10092
98bfdba5
PA
10093 /* Template parameters may be specified in the DIE's DW_AT_name, or
10094 as children with DW_TAG_template_type_param or
10095 DW_TAG_value_type_param. If the latter, add them to the name
10096 here. If the name already has template parameters, then
10097 skip this step; some versions of GCC emit both, and
10098 it is more efficient to use the pre-computed name.
10099
10100 Something to keep in mind about this process: it is very
10101 unlikely, or in some cases downright impossible, to produce
10102 something that will match the mangled name of a function.
10103 If the definition of the function has the same debug info,
10104 we should be able to match up with it anyway. But fallbacks
10105 using the minimal symbol, for instance to find a method
10106 implemented in a stripped copy of libstdc++, will not work.
10107 If we do not have debug info for the definition, we will have to
10108 match them up some other way.
10109
10110 When we do name matching there is a related problem with function
10111 templates; two instantiated function templates are allowed to
10112 differ only by their return types, which we do not add here. */
10113
10114 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10115 {
10116 struct attribute *attr;
10117 struct die_info *child;
10118 int first = 1;
10119
10120 die->building_fullname = 1;
10121
10122 for (child = die->child; child != NULL; child = child->sibling)
10123 {
10124 struct type *type;
12df843f 10125 LONGEST value;
d521ce57 10126 const gdb_byte *bytes;
98bfdba5
PA
10127 struct dwarf2_locexpr_baton *baton;
10128 struct value *v;
10129
10130 if (child->tag != DW_TAG_template_type_param
10131 && child->tag != DW_TAG_template_value_param)
10132 continue;
10133
10134 if (first)
10135 {
d7e74731 10136 buf.puts ("<");
98bfdba5
PA
10137 first = 0;
10138 }
10139 else
d7e74731 10140 buf.puts (", ");
98bfdba5
PA
10141
10142 attr = dwarf2_attr (child, DW_AT_type, cu);
10143 if (attr == NULL)
10144 {
b98664d3 10145 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10146 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10147 continue;
10148 }
10149 type = die_type (child, cu);
10150
10151 if (child->tag == DW_TAG_template_type_param)
10152 {
c1ec8cea
TT
10153 c_print_type (type, "", &buf, -1, 0, cu->language,
10154 &type_print_raw_options);
98bfdba5
PA
10155 continue;
10156 }
10157
10158 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10159 if (attr == NULL)
10160 {
b98664d3 10161 complaint (_("template parameter missing "
3e43a32a 10162 "DW_AT_const_value"));
d7e74731 10163 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10164 continue;
10165 }
10166
10167 dwarf2_const_value_attr (attr, type, name,
10168 &cu->comp_unit_obstack, cu,
10169 &value, &bytes, &baton);
10170
10171 if (TYPE_NOSIGN (type))
10172 /* GDB prints characters as NUMBER 'CHAR'. If that's
10173 changed, this can use value_print instead. */
d7e74731 10174 c_printchar (value, type, &buf);
98bfdba5
PA
10175 else
10176 {
10177 struct value_print_options opts;
10178
10179 if (baton != NULL)
10180 v = dwarf2_evaluate_loc_desc (type, NULL,
10181 baton->data,
10182 baton->size,
10183 baton->per_cu);
10184 else if (bytes != NULL)
10185 {
10186 v = allocate_value (type);
10187 memcpy (value_contents_writeable (v), bytes,
10188 TYPE_LENGTH (type));
10189 }
10190 else
10191 v = value_from_longest (type, value);
10192
3e43a32a
MS
10193 /* Specify decimal so that we do not depend on
10194 the radix. */
98bfdba5
PA
10195 get_formatted_print_options (&opts, 'd');
10196 opts.raw = 1;
d7e74731 10197 value_print (v, &buf, &opts);
98bfdba5 10198 release_value (v);
98bfdba5
PA
10199 }
10200 }
10201
10202 die->building_fullname = 0;
10203
10204 if (!first)
10205 {
10206 /* Close the argument list, with a space if necessary
10207 (nested templates). */
d7e74731
PA
10208 if (!buf.empty () && buf.string ().back () == '>')
10209 buf.puts (" >");
98bfdba5 10210 else
d7e74731 10211 buf.puts (">");
98bfdba5
PA
10212 }
10213 }
10214
9c37b5ae 10215 /* For C++ methods, append formal parameter type
94af9270 10216 information, if PHYSNAME. */
6e70227d 10217
94af9270 10218 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10219 && cu->language == language_cplus)
94af9270
KS
10220 {
10221 struct type *type = read_type_die (die, cu);
10222
d7e74731 10223 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10224 &type_print_raw_options);
94af9270 10225
9c37b5ae 10226 if (cu->language == language_cplus)
94af9270 10227 {
60430eff
DJ
10228 /* Assume that an artificial first parameter is
10229 "this", but do not crash if it is not. RealView
10230 marks unnamed (and thus unused) parameters as
10231 artificial; there is no way to differentiate
10232 the two cases. */
94af9270
KS
10233 if (TYPE_NFIELDS (type) > 0
10234 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10235 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10236 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10237 0))))
d7e74731 10238 buf.puts (" const");
94af9270
KS
10239 }
10240 }
10241
d7e74731 10242 const std::string &intermediate_name = buf.string ();
94af9270
KS
10243
10244 if (cu->language == language_cplus)
34a68019 10245 canonical_name
322a8516 10246 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10247 objfile);
34a68019
TT
10248
10249 /* If we only computed INTERMEDIATE_NAME, or if
10250 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10251 intern it. */
322a8516 10252 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10253 name = objfile->intern (intermediate_name);
34a68019
TT
10254 else
10255 name = canonical_name;
94af9270
KS
10256 }
10257 }
10258
10259 return name;
10260}
10261
0114d602
DJ
10262/* Return the fully qualified name of DIE, based on its DW_AT_name.
10263 If scope qualifiers are appropriate they will be added. The result
34a68019 10264 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10265 not have a name. NAME may either be from a previous call to
10266 dwarf2_name or NULL.
10267
9c37b5ae 10268 The output string will be canonicalized (if C++). */
0114d602
DJ
10269
10270static const char *
15d034d0 10271dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10272{
94af9270
KS
10273 return dwarf2_compute_name (name, die, cu, 0);
10274}
0114d602 10275
94af9270
KS
10276/* Construct a physname for the given DIE in CU. NAME may either be
10277 from a previous call to dwarf2_name or NULL. The result will be
10278 allocated on the objfile_objstack or NULL if the DIE does not have a
10279 name.
0114d602 10280
9c37b5ae 10281 The output string will be canonicalized (if C++). */
0114d602 10282
94af9270 10283static const char *
15d034d0 10284dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10285{
518817b3 10286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10287 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10288 int need_copy = 1;
10289
10290 /* In this case dwarf2_compute_name is just a shortcut not building anything
10291 on its own. */
10292 if (!die_needs_namespace (die, cu))
10293 return dwarf2_compute_name (name, die, cu, 1);
10294
73b9be8b 10295 mangled = dw2_linkage_name (die, cu);
900e11f9 10296
e98c9e7c
TT
10297 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10298 See https://github.com/rust-lang/rust/issues/32925. */
10299 if (cu->language == language_rust && mangled != NULL
10300 && strchr (mangled, '{') != NULL)
10301 mangled = NULL;
10302
900e11f9
JK
10303 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10304 has computed. */
791afaa2 10305 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10306 if (mangled != NULL)
900e11f9 10307 {
900e11f9 10308
59cc4834
JB
10309 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10310 {
10311 /* Do nothing (do not demangle the symbol name). */
10312 }
10313 else if (cu->language == language_go)
a766d390 10314 {
5e2db402
TT
10315 /* This is a lie, but we already lie to the caller new_symbol.
10316 new_symbol assumes we return the mangled name.
a766d390 10317 This just undoes that lie until things are cleaned up. */
a766d390
DE
10318 }
10319 else
10320 {
0eb876f5
JB
10321 /* Use DMGL_RET_DROP for C++ template functions to suppress
10322 their return type. It is easier for GDB users to search
10323 for such functions as `name(params)' than `long name(params)'.
10324 In such case the minimal symbol names do not match the full
10325 symbol names but for template functions there is never a need
10326 to look up their definition from their declaration so
10327 the only disadvantage remains the minimal symbol variant
10328 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10329 demangled.reset (gdb_demangle (mangled,
10330 (DMGL_PARAMS | DMGL_ANSI
10331 | DMGL_RET_DROP)));
a766d390 10332 }
900e11f9 10333 if (demangled)
791afaa2 10334 canon = demangled.get ();
900e11f9
JK
10335 else
10336 {
10337 canon = mangled;
10338 need_copy = 0;
10339 }
10340 }
10341
10342 if (canon == NULL || check_physname)
10343 {
10344 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10345
10346 if (canon != NULL && strcmp (physname, canon) != 0)
10347 {
10348 /* It may not mean a bug in GDB. The compiler could also
10349 compute DW_AT_linkage_name incorrectly. But in such case
10350 GDB would need to be bug-to-bug compatible. */
10351
b98664d3 10352 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10353 "(from linkage <%s>) - DIE at %s [in module %s]"),
10354 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10355 objfile_name (objfile));
900e11f9
JK
10356
10357 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10358 is available here - over computed PHYSNAME. It is safer
10359 against both buggy GDB and buggy compilers. */
10360
10361 retval = canon;
10362 }
10363 else
10364 {
10365 retval = physname;
10366 need_copy = 0;
10367 }
10368 }
10369 else
10370 retval = canon;
10371
10372 if (need_copy)
be1e3d3e 10373 retval = objfile->intern (retval);
900e11f9 10374
900e11f9 10375 return retval;
0114d602
DJ
10376}
10377
74921315
KS
10378/* Inspect DIE in CU for a namespace alias. If one exists, record
10379 a new symbol for it.
10380
10381 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10382
10383static int
10384read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10385{
10386 struct attribute *attr;
10387
10388 /* If the die does not have a name, this is not a namespace
10389 alias. */
10390 attr = dwarf2_attr (die, DW_AT_name, cu);
10391 if (attr != NULL)
10392 {
10393 int num;
10394 struct die_info *d = die;
10395 struct dwarf2_cu *imported_cu = cu;
10396
10397 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10398 keep inspecting DIEs until we hit the underlying import. */
10399#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10400 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10401 {
10402 attr = dwarf2_attr (d, DW_AT_import, cu);
10403 if (attr == NULL)
10404 break;
10405
10406 d = follow_die_ref (d, attr, &imported_cu);
10407 if (d->tag != DW_TAG_imported_declaration)
10408 break;
10409 }
10410
10411 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10412 {
b98664d3 10413 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10414 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10415 return 0;
10416 }
10417
10418 if (attr != NULL)
10419 {
10420 struct type *type;
9c541725 10421 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10422
9c541725 10423 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10424 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10425 {
10426 /* This declaration is a global namespace alias. Add
10427 a symbol for it whose type is the aliased namespace. */
10428 new_symbol (die, type, cu);
10429 return 1;
10430 }
10431 }
10432 }
10433
10434 return 0;
10435}
10436
22cee43f 10437/* Return the using directives repository (global or local?) to use in the
804d2729 10438 current context for CU.
22cee43f
PMR
10439
10440 For Ada, imported declarations can materialize renamings, which *may* be
10441 global. However it is impossible (for now?) in DWARF to distinguish
10442 "external" imported declarations and "static" ones. As all imported
10443 declarations seem to be static in all other languages, make them all CU-wide
10444 global only in Ada. */
10445
10446static struct using_direct **
804d2729 10447using_directives (struct dwarf2_cu *cu)
22cee43f 10448{
c24bdb02
KS
10449 if (cu->language == language_ada
10450 && cu->get_builder ()->outermost_context_p ())
10451 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10452 else
c24bdb02 10453 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10454}
10455
27aa8d6a
SW
10456/* Read the import statement specified by the given die and record it. */
10457
10458static void
10459read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10460{
518817b3 10461 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10462 struct attribute *import_attr;
32019081 10463 struct die_info *imported_die, *child_die;
de4affc9 10464 struct dwarf2_cu *imported_cu;
27aa8d6a 10465 const char *imported_name;
794684b6 10466 const char *imported_name_prefix;
13387711
SW
10467 const char *canonical_name;
10468 const char *import_alias;
10469 const char *imported_declaration = NULL;
794684b6 10470 const char *import_prefix;
eb1e02fd 10471 std::vector<const char *> excludes;
13387711 10472
27aa8d6a
SW
10473 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10474 if (import_attr == NULL)
10475 {
b98664d3 10476 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10477 dwarf_tag_name (die->tag));
10478 return;
10479 }
10480
de4affc9
CC
10481 imported_cu = cu;
10482 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10483 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10484 if (imported_name == NULL)
10485 {
10486 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10487
10488 The import in the following code:
10489 namespace A
10490 {
10491 typedef int B;
10492 }
10493
10494 int main ()
10495 {
10496 using A::B;
10497 B b;
10498 return b;
10499 }
10500
10501 ...
10502 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10503 <52> DW_AT_decl_file : 1
10504 <53> DW_AT_decl_line : 6
10505 <54> DW_AT_import : <0x75>
10506 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10507 <59> DW_AT_name : B
10508 <5b> DW_AT_decl_file : 1
10509 <5c> DW_AT_decl_line : 2
10510 <5d> DW_AT_type : <0x6e>
10511 ...
10512 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10513 <76> DW_AT_byte_size : 4
10514 <77> DW_AT_encoding : 5 (signed)
10515
10516 imports the wrong die ( 0x75 instead of 0x58 ).
10517 This case will be ignored until the gcc bug is fixed. */
10518 return;
10519 }
10520
82856980
SW
10521 /* Figure out the local name after import. */
10522 import_alias = dwarf2_name (die, cu);
27aa8d6a 10523
794684b6
SW
10524 /* Figure out where the statement is being imported to. */
10525 import_prefix = determine_prefix (die, cu);
10526
10527 /* Figure out what the scope of the imported die is and prepend it
10528 to the name of the imported die. */
de4affc9 10529 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10530
f55ee35c
JK
10531 if (imported_die->tag != DW_TAG_namespace
10532 && imported_die->tag != DW_TAG_module)
794684b6 10533 {
13387711
SW
10534 imported_declaration = imported_name;
10535 canonical_name = imported_name_prefix;
794684b6 10536 }
13387711 10537 else if (strlen (imported_name_prefix) > 0)
12aaed36 10538 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10539 imported_name_prefix,
10540 (cu->language == language_d ? "." : "::"),
10541 imported_name, (char *) NULL);
13387711
SW
10542 else
10543 canonical_name = imported_name;
794684b6 10544
32019081
JK
10545 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10546 for (child_die = die->child; child_die && child_die->tag;
10547 child_die = sibling_die (child_die))
10548 {
10549 /* DWARF-4: A Fortran use statement with a “rename list” may be
10550 represented by an imported module entry with an import attribute
10551 referring to the module and owned entries corresponding to those
10552 entities that are renamed as part of being imported. */
10553
10554 if (child_die->tag != DW_TAG_imported_declaration)
10555 {
b98664d3 10556 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10557 "- DIE at %s [in module %s]"),
10558 sect_offset_str (child_die->sect_off),
10559 objfile_name (objfile));
32019081
JK
10560 continue;
10561 }
10562
10563 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10564 if (import_attr == NULL)
10565 {
b98664d3 10566 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10567 dwarf_tag_name (child_die->tag));
10568 continue;
10569 }
10570
10571 imported_cu = cu;
10572 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10573 &imported_cu);
10574 imported_name = dwarf2_name (imported_die, imported_cu);
10575 if (imported_name == NULL)
10576 {
b98664d3 10577 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10578 "imported name - DIE at %s [in module %s]"),
10579 sect_offset_str (child_die->sect_off),
10580 objfile_name (objfile));
32019081
JK
10581 continue;
10582 }
10583
eb1e02fd 10584 excludes.push_back (imported_name);
32019081
JK
10585
10586 process_die (child_die, cu);
10587 }
10588
804d2729 10589 add_using_directive (using_directives (cu),
22cee43f
PMR
10590 import_prefix,
10591 canonical_name,
10592 import_alias,
10593 imported_declaration,
10594 excludes,
10595 0,
10596 &objfile->objfile_obstack);
27aa8d6a
SW
10597}
10598
5230b05a
WT
10599/* ICC<14 does not output the required DW_AT_declaration on incomplete
10600 types, but gives them a size of zero. Starting with version 14,
10601 ICC is compatible with GCC. */
10602
9068261f 10603static bool
5230b05a
WT
10604producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10605{
10606 if (!cu->checked_producer)
10607 check_producer (cu);
10608
10609 return cu->producer_is_icc_lt_14;
10610}
10611
eb77c9df
AB
10612/* ICC generates a DW_AT_type for C void functions. This was observed on
10613 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10614 which says that void functions should not have a DW_AT_type. */
10615
10616static bool
10617producer_is_icc (struct dwarf2_cu *cu)
10618{
10619 if (!cu->checked_producer)
10620 check_producer (cu);
10621
10622 return cu->producer_is_icc;
10623}
10624
1b80a9fa
JK
10625/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10626 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10627 this, it was first present in GCC release 4.3.0. */
10628
9068261f 10629static bool
1b80a9fa
JK
10630producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10631{
10632 if (!cu->checked_producer)
10633 check_producer (cu);
10634
10635 return cu->producer_is_gcc_lt_4_3;
10636}
10637
d721ba37
PA
10638static file_and_directory
10639find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10640{
d721ba37
PA
10641 file_and_directory res;
10642
9291a0cd
TT
10643 /* Find the filename. Do not use dwarf2_name here, since the filename
10644 is not a source language identifier. */
d721ba37
PA
10645 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10646 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10647
d721ba37
PA
10648 if (res.comp_dir == NULL
10649 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10650 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10651 {
d721ba37
PA
10652 res.comp_dir_storage = ldirname (res.name);
10653 if (!res.comp_dir_storage.empty ())
10654 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10655 }
d721ba37 10656 if (res.comp_dir != NULL)
9291a0cd
TT
10657 {
10658 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10659 directory, get rid of it. */
d721ba37 10660 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10661
d721ba37
PA
10662 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10663 res.comp_dir = cp + 1;
9291a0cd
TT
10664 }
10665
d721ba37
PA
10666 if (res.name == NULL)
10667 res.name = "<unknown>";
10668
10669 return res;
9291a0cd
TT
10670}
10671
f4dc4d17
DE
10672/* Handle DW_AT_stmt_list for a compilation unit.
10673 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10674 COMP_DIR is the compilation directory. LOWPC is passed to
10675 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10676
10677static void
10678handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10679 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10680{
518817b3
SM
10681 struct dwarf2_per_objfile *dwarf2_per_objfile
10682 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10683 struct attribute *attr;
527f3840
JK
10684 struct line_header line_header_local;
10685 hashval_t line_header_local_hash;
527f3840
JK
10686 void **slot;
10687 int decode_mapping;
2ab95328 10688
f4dc4d17
DE
10689 gdb_assert (! cu->per_cu->is_debug_types);
10690
2ab95328 10691 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10692 if (attr == NULL)
10693 return;
10694
9c541725 10695 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10696
10697 /* The line header hash table is only created if needed (it exists to
10698 prevent redundant reading of the line table for partial_units).
10699 If we're given a partial_unit, we'll need it. If we're given a
10700 compile_unit, then use the line header hash table if it's already
10701 created, but don't create one just yet. */
10702
10703 if (dwarf2_per_objfile->line_header_hash == NULL
10704 && die->tag == DW_TAG_partial_unit)
2ab95328 10705 {
527f3840 10706 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10707 .reset (htab_create_alloc (127, line_header_hash_voidp,
10708 line_header_eq_voidp,
10709 free_line_header_voidp,
10710 xcalloc, xfree));
527f3840 10711 }
2ab95328 10712
9c541725 10713 line_header_local.sect_off = line_offset;
527f3840
JK
10714 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10715 line_header_local_hash = line_header_hash (&line_header_local);
10716 if (dwarf2_per_objfile->line_header_hash != NULL)
10717 {
d15acc42 10718 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10719 &line_header_local,
10720 line_header_local_hash, NO_INSERT);
10721
10722 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10723 is not present in *SLOT (since if there is something in *SLOT then
10724 it will be for a partial_unit). */
10725 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10726 {
527f3840 10727 gdb_assert (*slot != NULL);
9a3c8263 10728 cu->line_header = (struct line_header *) *slot;
527f3840 10729 return;
dee91e82 10730 }
2ab95328 10731 }
527f3840
JK
10732
10733 /* dwarf_decode_line_header does not yet provide sufficient information.
10734 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10735 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10736 if (lh == NULL)
527f3840 10737 return;
4c8aa72d
PA
10738
10739 cu->line_header = lh.release ();
10740 cu->line_header_die_owner = die;
527f3840
JK
10741
10742 if (dwarf2_per_objfile->line_header_hash == NULL)
10743 slot = NULL;
10744 else
10745 {
d15acc42 10746 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10747 &line_header_local,
10748 line_header_local_hash, INSERT);
10749 gdb_assert (slot != NULL);
10750 }
10751 if (slot != NULL && *slot == NULL)
10752 {
10753 /* This newly decoded line number information unit will be owned
10754 by line_header_hash hash table. */
10755 *slot = cu->line_header;
4c8aa72d 10756 cu->line_header_die_owner = NULL;
527f3840
JK
10757 }
10758 else
10759 {
10760 /* We cannot free any current entry in (*slot) as that struct line_header
10761 may be already used by multiple CUs. Create only temporary decoded
10762 line_header for this CU - it may happen at most once for each line
10763 number information unit. And if we're not using line_header_hash
10764 then this is what we want as well. */
10765 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10766 }
10767 decode_mapping = (die->tag != DW_TAG_partial_unit);
10768 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10769 decode_mapping);
fff8551c 10770
2ab95328
TT
10771}
10772
95554aad 10773/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10774
c906108c 10775static void
e7c27a73 10776read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10777{
518817b3
SM
10778 struct dwarf2_per_objfile *dwarf2_per_objfile
10779 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10780 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10781 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10782 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10783 CORE_ADDR highpc = ((CORE_ADDR) 0);
10784 struct attribute *attr;
c906108c 10785 struct die_info *child_die;
e142c38c 10786 CORE_ADDR baseaddr;
6e70227d 10787
380618d6 10788 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10789 baseaddr = objfile->text_section_offset ();
c906108c 10790
fae299cd 10791 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10792
10793 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10794 from finish_block. */
2acceee2 10795 if (lowpc == ((CORE_ADDR) -1))
c906108c 10796 lowpc = highpc;
3e29f34a 10797 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10798
d721ba37 10799 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10800
f4b8a18d
KW
10801 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10802 standardised yet. As a workaround for the language detection we fall
10803 back to the DW_AT_producer string. */
10804 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10805 cu->language = language_opencl;
10806
3019eac3
DE
10807 /* Similar hack for Go. */
10808 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10809 set_cu_language (DW_LANG_Go, cu);
10810
c24bdb02 10811 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10812
10813 /* Decode line number information if present. We do this before
10814 processing child DIEs, so that the line header table is available
10815 for DW_AT_decl_file. */
d721ba37 10816 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10817
10818 /* Process all dies in compilation unit. */
10819 if (die->child != NULL)
10820 {
10821 child_die = die->child;
10822 while (child_die && child_die->tag)
10823 {
10824 process_die (child_die, cu);
10825 child_die = sibling_die (child_die);
10826 }
10827 }
10828
10829 /* Decode macro information, if present. Dwarf 2 macro information
10830 refers to information in the line number info statement program
10831 header, so we can only read it if we've read the header
10832 successfully. */
0af92d60
JK
10833 attr = dwarf2_attr (die, DW_AT_macros, cu);
10834 if (attr == NULL)
10835 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10836 if (attr && cu->line_header)
10837 {
10838 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10839 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10840
43f3e411 10841 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10842 }
10843 else
10844 {
10845 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10846 if (attr && cu->line_header)
10847 {
10848 unsigned int macro_offset = DW_UNSND (attr);
10849
43f3e411 10850 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10851 }
10852 }
3019eac3
DE
10853}
10854
c24bdb02
KS
10855void
10856dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10857{
f4dc4d17
DE
10858 struct type_unit_group *tu_group;
10859 int first_time;
3019eac3 10860 struct attribute *attr;
9c541725 10861 unsigned int i;
0186c6a7 10862 struct signatured_type *sig_type;
3019eac3 10863
f4dc4d17 10864 gdb_assert (per_cu->is_debug_types);
0186c6a7 10865 sig_type = (struct signatured_type *) per_cu;
3019eac3 10866
c24bdb02 10867 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10868
f4dc4d17 10869 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10870 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10871 if (sig_type->type_unit_group == NULL)
c24bdb02 10872 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10873 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10874
10875 /* If we've already processed this stmt_list there's no real need to
10876 do it again, we could fake it and just recreate the part we need
10877 (file name,index -> symtab mapping). If data shows this optimization
10878 is useful we can do it then. */
43f3e411 10879 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10880
10881 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10882 debug info. */
fff8551c 10883 line_header_up lh;
f4dc4d17 10884 if (attr != NULL)
3019eac3 10885 {
9c541725 10886 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10887 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10888 }
10889 if (lh == NULL)
10890 {
10891 if (first_time)
c24bdb02 10892 start_symtab ("", NULL, 0);
f4dc4d17
DE
10893 else
10894 {
10895 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10896 gdb_assert (m_builder == nullptr);
804d2729 10897 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10898 m_builder.reset (new struct buildsym_compunit
10899 (COMPUNIT_OBJFILE (cust), "",
10900 COMPUNIT_DIRNAME (cust),
10901 compunit_language (cust),
10902 0, cust));
f4dc4d17 10903 }
f4dc4d17 10904 return;
3019eac3
DE
10905 }
10906
c24bdb02
KS
10907 line_header = lh.release ();
10908 line_header_die_owner = die;
3019eac3 10909
f4dc4d17
DE
10910 if (first_time)
10911 {
c24bdb02 10912 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10913
1fd60fc0
DE
10914 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10915 still initializing it, and our caller (a few levels up)
10916 process_full_type_unit still needs to know if this is the first
10917 time. */
10918
4ac93832
TT
10919 tu_group->symtabs
10920 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10921 struct symtab *, line_header->file_names_size ());
3019eac3 10922
7ba99d21
AT
10923 auto &file_names = line_header->file_names ();
10924 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10925 {
7ba99d21 10926 file_entry &fe = file_names[i];
c24bdb02
KS
10927 dwarf2_start_subfile (this, fe.name,
10928 fe.include_dir (line_header));
10929 buildsym_compunit *b = get_builder ();
10930 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10931 {
4c8aa72d
PA
10932 /* NOTE: start_subfile will recognize when it's been
10933 passed a file it has already seen. So we can't
10934 assume there's a simple mapping from
10935 cu->line_header->file_names to subfiles, plus
10936 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10937 b->get_current_subfile ()->symtab
10938 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10939 }
10940
c24bdb02 10941 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10942 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10943 }
10944 }
10945 else
3019eac3 10946 {
c24bdb02 10947 gdb_assert (m_builder == nullptr);
804d2729 10948 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10949 m_builder.reset (new struct buildsym_compunit
10950 (COMPUNIT_OBJFILE (cust), "",
10951 COMPUNIT_DIRNAME (cust),
10952 compunit_language (cust),
10953 0, cust));
f4dc4d17 10954
7ba99d21
AT
10955 auto &file_names = line_header->file_names ();
10956 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10957 {
7ba99d21 10958 file_entry &fe = file_names[i];
4c8aa72d 10959 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10960 }
3019eac3
DE
10961 }
10962
f4dc4d17
DE
10963 /* The main symtab is allocated last. Type units don't have DW_AT_name
10964 so they don't have a "real" (so to speak) symtab anyway.
10965 There is later code that will assign the main symtab to all symbols
10966 that don't have one. We need to handle the case of a symbol with a
10967 missing symtab (DW_AT_decl_file) anyway. */
10968}
3019eac3 10969
f4dc4d17
DE
10970/* Process DW_TAG_type_unit.
10971 For TUs we want to skip the first top level sibling if it's not the
10972 actual type being defined by this TU. In this case the first top
10973 level sibling is there to provide context only. */
3019eac3 10974
f4dc4d17
DE
10975static void
10976read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10977{
10978 struct die_info *child_die;
3019eac3 10979
f4dc4d17
DE
10980 prepare_one_comp_unit (cu, die, language_minimal);
10981
10982 /* Initialize (or reinitialize) the machinery for building symtabs.
10983 We do this before processing child DIEs, so that the line header table
10984 is available for DW_AT_decl_file. */
c24bdb02 10985 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10986
10987 if (die->child != NULL)
10988 {
10989 child_die = die->child;
10990 while (child_die && child_die->tag)
10991 {
10992 process_die (child_die, cu);
10993 child_die = sibling_die (child_die);
10994 }
10995 }
3019eac3
DE
10996}
10997\f
80626a55
DE
10998/* DWO/DWP files.
10999
11000 http://gcc.gnu.org/wiki/DebugFission
11001 http://gcc.gnu.org/wiki/DebugFissionDWP
11002
11003 To simplify handling of both DWO files ("object" files with the DWARF info)
11004 and DWP files (a file with the DWOs packaged up into one file), we treat
11005 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11006
11007static hashval_t
11008hash_dwo_file (const void *item)
11009{
9a3c8263 11010 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11011 hashval_t hash;
3019eac3 11012
a2ce51a0
DE
11013 hash = htab_hash_string (dwo_file->dwo_name);
11014 if (dwo_file->comp_dir != NULL)
11015 hash += htab_hash_string (dwo_file->comp_dir);
11016 return hash;
3019eac3
DE
11017}
11018
11019static int
11020eq_dwo_file (const void *item_lhs, const void *item_rhs)
11021{
9a3c8263
SM
11022 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11023 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11024
a2ce51a0
DE
11025 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11026 return 0;
11027 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11028 return lhs->comp_dir == rhs->comp_dir;
11029 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11030}
11031
11032/* Allocate a hash table for DWO files. */
11033
51ac9db5 11034static htab_up
298e9637 11035allocate_dwo_file_hash_table ()
3019eac3 11036{
51ac9db5
SM
11037 auto delete_dwo_file = [] (void *item)
11038 {
11039 struct dwo_file *dwo_file = (struct dwo_file *) item;
11040
11041 delete dwo_file;
11042 };
11043
bc68fb19
TT
11044 return htab_up (htab_create_alloc (41,
11045 hash_dwo_file,
11046 eq_dwo_file,
11047 delete_dwo_file,
11048 xcalloc, xfree));
3019eac3
DE
11049}
11050
80626a55
DE
11051/* Lookup DWO file DWO_NAME. */
11052
11053static void **
ed2dc618
SM
11054lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11055 const char *dwo_name,
11056 const char *comp_dir)
80626a55
DE
11057{
11058 struct dwo_file find_entry;
11059 void **slot;
11060
11061 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11062 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11063
0ac5b59e
DE
11064 find_entry.dwo_name = dwo_name;
11065 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11066 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11067 INSERT);
80626a55
DE
11068
11069 return slot;
11070}
11071
3019eac3
DE
11072static hashval_t
11073hash_dwo_unit (const void *item)
11074{
9a3c8263 11075 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11076
11077 /* This drops the top 32 bits of the id, but is ok for a hash. */
11078 return dwo_unit->signature;
11079}
11080
11081static int
11082eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11083{
9a3c8263
SM
11084 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11085 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11086
11087 /* The signature is assumed to be unique within the DWO file.
11088 So while object file CU dwo_id's always have the value zero,
11089 that's OK, assuming each object file DWO file has only one CU,
11090 and that's the rule for now. */
11091 return lhs->signature == rhs->signature;
11092}
11093
11094/* Allocate a hash table for DWO CUs,TUs.
11095 There is one of these tables for each of CUs,TUs for each DWO file. */
11096
b0b6a987 11097static htab_up
298e9637 11098allocate_dwo_unit_table ()
3019eac3
DE
11099{
11100 /* Start out with a pretty small number.
11101 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11102 return htab_up (htab_create_alloc (3,
11103 hash_dwo_unit,
11104 eq_dwo_unit,
11105 NULL, xcalloc, xfree));
3019eac3
DE
11106}
11107
19c3d4c9 11108/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11109
11110static void
19c3d4c9
DE
11111create_dwo_cu_reader (const struct die_reader_specs *reader,
11112 const gdb_byte *info_ptr,
11113 struct die_info *comp_unit_die,
c0ab21c2
TT
11114 struct dwo_file *dwo_file,
11115 struct dwo_unit *dwo_unit)
3019eac3
DE
11116{
11117 struct dwarf2_cu *cu = reader->cu;
9c541725 11118 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11119 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11120
a084a2a6
AT
11121 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11122 if (!signature.has_value ())
3019eac3 11123 {
b98664d3 11124 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11125 " its dwo_id [in module %s]"),
9d8780f0 11126 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11127 return;
11128 }
11129
3019eac3 11130 dwo_unit->dwo_file = dwo_file;
a084a2a6 11131 dwo_unit->signature = *signature;
8a0459fd 11132 dwo_unit->section = section;
9c541725 11133 dwo_unit->sect_off = sect_off;
3019eac3
DE
11134 dwo_unit->length = cu->per_cu->length;
11135
b4f54984 11136 if (dwarf_read_debug)
9d8780f0
SM
11137 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11138 sect_offset_str (sect_off),
9c541725 11139 hex_string (dwo_unit->signature));
3019eac3
DE
11140}
11141
33c5cd75 11142/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11143 Note: This function processes DWO files only, not DWP files. */
3019eac3 11144
33c5cd75 11145static void
ed2dc618 11146create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11147 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11148 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11149{
11150 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11151 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11152
96b79293 11153 section.read (objfile);
33c5cd75 11154 info_ptr = section.buffer;
3019eac3
DE
11155
11156 if (info_ptr == NULL)
33c5cd75 11157 return;
3019eac3 11158
b4f54984 11159 if (dwarf_read_debug)
19c3d4c9
DE
11160 {
11161 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11162 section.get_name (),
11163 section.get_file_name ());
19c3d4c9 11164 }
3019eac3 11165
33c5cd75 11166 end_ptr = info_ptr + section.size;
3019eac3
DE
11167 while (info_ptr < end_ptr)
11168 {
11169 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11170 struct dwo_unit read_unit {};
33c5cd75
DB
11171 struct dwo_unit *dwo_unit;
11172 void **slot;
11173 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11174
11175 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11176 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11177 per_cu.is_debug_types = 0;
33c5cd75
DB
11178 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11179 per_cu.section = &section;
11180
c0ab21c2
TT
11181 cutu_reader reader (&per_cu, cu, &dwo_file);
11182 if (!reader.dummy_p)
11183 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11184 &dwo_file, &read_unit);
33c5cd75
DB
11185 info_ptr += per_cu.length;
11186
11187 // If the unit could not be parsed, skip it.
c0ab21c2 11188 if (read_unit.dwo_file == NULL)
33c5cd75 11189 continue;
3019eac3 11190
33c5cd75 11191 if (cus_htab == NULL)
298e9637 11192 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11193
33c5cd75 11194 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11195 *dwo_unit = read_unit;
b0b6a987 11196 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11197 gdb_assert (slot != NULL);
11198 if (*slot != NULL)
19c3d4c9 11199 {
33c5cd75
DB
11200 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11201 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11202
b98664d3 11203 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11204 " the entry at offset %s, signature %s"),
11205 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11206 hex_string (dwo_unit->signature));
19c3d4c9 11207 }
33c5cd75 11208 *slot = (void *)dwo_unit;
3019eac3 11209 }
3019eac3
DE
11210}
11211
80626a55
DE
11212/* DWP file .debug_{cu,tu}_index section format:
11213 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11214
d2415c6c
DE
11215 DWP Version 1:
11216
80626a55
DE
11217 Both index sections have the same format, and serve to map a 64-bit
11218 signature to a set of section numbers. Each section begins with a header,
11219 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11220 indexes, and a pool of 32-bit section numbers. The index sections will be
11221 aligned at 8-byte boundaries in the file.
11222
d2415c6c
DE
11223 The index section header consists of:
11224
11225 V, 32 bit version number
11226 -, 32 bits unused
11227 N, 32 bit number of compilation units or type units in the index
11228 M, 32 bit number of slots in the hash table
80626a55 11229
d2415c6c 11230 Numbers are recorded using the byte order of the application binary.
80626a55 11231
d2415c6c
DE
11232 The hash table begins at offset 16 in the section, and consists of an array
11233 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11234 order of the application binary). Unused slots in the hash table are 0.
11235 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11236
d2415c6c
DE
11237 The parallel table begins immediately after the hash table
11238 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11239 array of 32-bit indexes (using the byte order of the application binary),
11240 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11241 table contains a 32-bit index into the pool of section numbers. For unused
11242 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11243
73869dc2
DE
11244 The pool of section numbers begins immediately following the hash table
11245 (at offset 16 + 12 * M from the beginning of the section). The pool of
11246 section numbers consists of an array of 32-bit words (using the byte order
11247 of the application binary). Each item in the array is indexed starting
11248 from 0. The hash table entry provides the index of the first section
11249 number in the set. Additional section numbers in the set follow, and the
11250 set is terminated by a 0 entry (section number 0 is not used in ELF).
11251
11252 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11253 section must be the first entry in the set, and the .debug_abbrev.dwo must
11254 be the second entry. Other members of the set may follow in any order.
11255
11256 ---
11257
11258 DWP Version 2:
11259
11260 DWP Version 2 combines all the .debug_info, etc. sections into one,
11261 and the entries in the index tables are now offsets into these sections.
11262 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11263 section.
11264
11265 Index Section Contents:
11266 Header
11267 Hash Table of Signatures dwp_hash_table.hash_table
11268 Parallel Table of Indices dwp_hash_table.unit_table
11269 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11270 Table of Section Sizes dwp_hash_table.v2.sizes
11271
11272 The index section header consists of:
11273
11274 V, 32 bit version number
11275 L, 32 bit number of columns in the table of section offsets
11276 N, 32 bit number of compilation units or type units in the index
11277 M, 32 bit number of slots in the hash table
11278
11279 Numbers are recorded using the byte order of the application binary.
11280
11281 The hash table has the same format as version 1.
11282 The parallel table of indices has the same format as version 1,
11283 except that the entries are origin-1 indices into the table of sections
11284 offsets and the table of section sizes.
11285
11286 The table of offsets begins immediately following the parallel table
11287 (at offset 16 + 12 * M from the beginning of the section). The table is
11288 a two-dimensional array of 32-bit words (using the byte order of the
11289 application binary), with L columns and N+1 rows, in row-major order.
11290 Each row in the array is indexed starting from 0. The first row provides
11291 a key to the remaining rows: each column in this row provides an identifier
11292 for a debug section, and the offsets in the same column of subsequent rows
11293 refer to that section. The section identifiers are:
11294
11295 DW_SECT_INFO 1 .debug_info.dwo
11296 DW_SECT_TYPES 2 .debug_types.dwo
11297 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11298 DW_SECT_LINE 4 .debug_line.dwo
11299 DW_SECT_LOC 5 .debug_loc.dwo
11300 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11301 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11302 DW_SECT_MACRO 8 .debug_macro.dwo
11303
11304 The offsets provided by the CU and TU index sections are the base offsets
11305 for the contributions made by each CU or TU to the corresponding section
11306 in the package file. Each CU and TU header contains an abbrev_offset
11307 field, used to find the abbreviations table for that CU or TU within the
11308 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11309 be interpreted as relative to the base offset given in the index section.
11310 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11311 should be interpreted as relative to the base offset for .debug_line.dwo,
11312 and offsets into other debug sections obtained from DWARF attributes should
11313 also be interpreted as relative to the corresponding base offset.
11314
11315 The table of sizes begins immediately following the table of offsets.
11316 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11317 with L columns and N rows, in row-major order. Each row in the array is
11318 indexed starting from 1 (row 0 is shared by the two tables).
11319
11320 ---
11321
11322 Hash table lookup is handled the same in version 1 and 2:
11323
11324 We assume that N and M will not exceed 2^32 - 1.
11325 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11326
d2415c6c
DE
11327 Given a 64-bit compilation unit signature or a type signature S, an entry
11328 in the hash table is located as follows:
80626a55 11329
d2415c6c
DE
11330 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11331 the low-order k bits all set to 1.
80626a55 11332
d2415c6c 11333 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11334
d2415c6c
DE
11335 3) If the hash table entry at index H matches the signature, use that
11336 entry. If the hash table entry at index H is unused (all zeroes),
11337 terminate the search: the signature is not present in the table.
80626a55 11338
d2415c6c 11339 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11340
d2415c6c 11341 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11342 to stop at an unused slot or find the match. */
80626a55
DE
11343
11344/* Create a hash table to map DWO IDs to their CU/TU entry in
11345 .debug_{info,types}.dwo in DWP_FILE.
11346 Returns NULL if there isn't one.
11347 Note: This function processes DWP files only, not DWO files. */
11348
11349static struct dwp_hash_table *
ed2dc618
SM
11350create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11351 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11352{
11353 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11354 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11355 const gdb_byte *index_ptr, *index_end;
80626a55 11356 struct dwarf2_section_info *index;
73869dc2 11357 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11358 struct dwp_hash_table *htab;
11359
11360 if (is_debug_types)
11361 index = &dwp_file->sections.tu_index;
11362 else
11363 index = &dwp_file->sections.cu_index;
11364
96b79293 11365 if (index->empty ())
80626a55 11366 return NULL;
96b79293 11367 index->read (objfile);
80626a55
DE
11368
11369 index_ptr = index->buffer;
11370 index_end = index_ptr + index->size;
11371
11372 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11373 index_ptr += 4;
11374 if (version == 2)
11375 nr_columns = read_4_bytes (dbfd, index_ptr);
11376 else
11377 nr_columns = 0;
11378 index_ptr += 4;
80626a55
DE
11379 nr_units = read_4_bytes (dbfd, index_ptr);
11380 index_ptr += 4;
11381 nr_slots = read_4_bytes (dbfd, index_ptr);
11382 index_ptr += 4;
11383
73869dc2 11384 if (version != 1 && version != 2)
80626a55 11385 {
21aa081e 11386 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11387 " [in module %s]"),
21aa081e 11388 pulongest (version), dwp_file->name);
80626a55
DE
11389 }
11390 if (nr_slots != (nr_slots & -nr_slots))
11391 {
21aa081e 11392 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11393 " is not power of 2 [in module %s]"),
21aa081e 11394 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11395 }
11396
11397 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11398 htab->version = version;
11399 htab->nr_columns = nr_columns;
80626a55
DE
11400 htab->nr_units = nr_units;
11401 htab->nr_slots = nr_slots;
11402 htab->hash_table = index_ptr;
11403 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11404
11405 /* Exit early if the table is empty. */
11406 if (nr_slots == 0 || nr_units == 0
11407 || (version == 2 && nr_columns == 0))
11408 {
11409 /* All must be zero. */
11410 if (nr_slots != 0 || nr_units != 0
11411 || (version == 2 && nr_columns != 0))
11412 {
b98664d3 11413 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11414 " all zero [in modules %s]"),
11415 dwp_file->name);
11416 }
11417 return htab;
11418 }
11419
11420 if (version == 1)
11421 {
11422 htab->section_pool.v1.indices =
11423 htab->unit_table + sizeof (uint32_t) * nr_slots;
11424 /* It's harder to decide whether the section is too small in v1.
11425 V1 is deprecated anyway so we punt. */
11426 }
11427 else
11428 {
11429 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11430 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11431 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11432 /* Reverse map for error checking. */
11433 int ids_seen[DW_SECT_MAX + 1];
11434 int i;
11435
11436 if (nr_columns < 2)
11437 {
11438 error (_("Dwarf Error: bad DWP hash table, too few columns"
11439 " in section table [in module %s]"),
11440 dwp_file->name);
11441 }
11442 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11443 {
11444 error (_("Dwarf Error: bad DWP hash table, too many columns"
11445 " in section table [in module %s]"),
11446 dwp_file->name);
11447 }
04fd5eed
GB
11448 memset (ids, 255, sizeof_ids);
11449 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11450 for (i = 0; i < nr_columns; ++i)
11451 {
11452 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11453
11454 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11455 {
11456 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11457 " in section table [in module %s]"),
11458 id, dwp_file->name);
11459 }
11460 if (ids_seen[id] != -1)
11461 {
11462 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11463 " id %d in section table [in module %s]"),
11464 id, dwp_file->name);
11465 }
11466 ids_seen[id] = i;
11467 ids[i] = id;
11468 }
11469 /* Must have exactly one info or types section. */
11470 if (((ids_seen[DW_SECT_INFO] != -1)
11471 + (ids_seen[DW_SECT_TYPES] != -1))
11472 != 1)
11473 {
11474 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11475 " DWO info/types section [in module %s]"),
11476 dwp_file->name);
11477 }
11478 /* Must have an abbrev section. */
11479 if (ids_seen[DW_SECT_ABBREV] == -1)
11480 {
11481 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11482 " section [in module %s]"),
11483 dwp_file->name);
11484 }
11485 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11486 htab->section_pool.v2.sizes =
11487 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11488 * nr_units * nr_columns);
11489 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11490 * nr_units * nr_columns))
11491 > index_end)
11492 {
11493 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11494 " [in module %s]"),
11495 dwp_file->name);
11496 }
11497 }
80626a55
DE
11498
11499 return htab;
11500}
11501
11502/* Update SECTIONS with the data from SECTP.
11503
11504 This function is like the other "locate" section routines that are
11505 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11506 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11507
11508 The result is non-zero for success, or zero if an error was found. */
11509
11510static int
73869dc2
DE
11511locate_v1_virtual_dwo_sections (asection *sectp,
11512 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11513{
11514 const struct dwop_section_names *names = &dwop_section_names;
11515
11516 if (section_is_p (sectp->name, &names->abbrev_dwo))
11517 {
11518 /* There can be only one. */
049412e3 11519 if (sections->abbrev.s.section != NULL)
80626a55 11520 return 0;
049412e3 11521 sections->abbrev.s.section = sectp;
fd361982 11522 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11523 }
11524 else if (section_is_p (sectp->name, &names->info_dwo)
11525 || section_is_p (sectp->name, &names->types_dwo))
11526 {
11527 /* There can be only one. */
049412e3 11528 if (sections->info_or_types.s.section != NULL)
80626a55 11529 return 0;
049412e3 11530 sections->info_or_types.s.section = sectp;
fd361982 11531 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11532 }
11533 else if (section_is_p (sectp->name, &names->line_dwo))
11534 {
11535 /* There can be only one. */
049412e3 11536 if (sections->line.s.section != NULL)
80626a55 11537 return 0;
049412e3 11538 sections->line.s.section = sectp;
fd361982 11539 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11540 }
11541 else if (section_is_p (sectp->name, &names->loc_dwo))
11542 {
11543 /* There can be only one. */
049412e3 11544 if (sections->loc.s.section != NULL)
80626a55 11545 return 0;
049412e3 11546 sections->loc.s.section = sectp;
fd361982 11547 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11548 }
11549 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11550 {
11551 /* There can be only one. */
049412e3 11552 if (sections->macinfo.s.section != NULL)
80626a55 11553 return 0;
049412e3 11554 sections->macinfo.s.section = sectp;
fd361982 11555 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11556 }
11557 else if (section_is_p (sectp->name, &names->macro_dwo))
11558 {
11559 /* There can be only one. */
049412e3 11560 if (sections->macro.s.section != NULL)
80626a55 11561 return 0;
049412e3 11562 sections->macro.s.section = sectp;
fd361982 11563 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11564 }
11565 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11566 {
11567 /* There can be only one. */
049412e3 11568 if (sections->str_offsets.s.section != NULL)
80626a55 11569 return 0;
049412e3 11570 sections->str_offsets.s.section = sectp;
fd361982 11571 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11572 }
11573 else
11574 {
11575 /* No other kind of section is valid. */
11576 return 0;
11577 }
11578
11579 return 1;
11580}
11581
73869dc2
DE
11582/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11583 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11584 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11585 This is for DWP version 1 files. */
80626a55
DE
11586
11587static struct dwo_unit *
ed2dc618
SM
11588create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11589 struct dwp_file *dwp_file,
73869dc2
DE
11590 uint32_t unit_index,
11591 const char *comp_dir,
11592 ULONGEST signature, int is_debug_types)
80626a55
DE
11593{
11594 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11595 const struct dwp_hash_table *dwp_htab =
11596 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11597 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11598 const char *kind = is_debug_types ? "TU" : "CU";
11599 struct dwo_file *dwo_file;
11600 struct dwo_unit *dwo_unit;
73869dc2 11601 struct virtual_v1_dwo_sections sections;
80626a55 11602 void **dwo_file_slot;
80626a55
DE
11603 int i;
11604
73869dc2
DE
11605 gdb_assert (dwp_file->version == 1);
11606
b4f54984 11607 if (dwarf_read_debug)
80626a55 11608 {
73869dc2 11609 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11610 kind,
73869dc2 11611 pulongest (unit_index), hex_string (signature),
80626a55
DE
11612 dwp_file->name);
11613 }
11614
19ac8c2e 11615 /* Fetch the sections of this DWO unit.
80626a55
DE
11616 Put a limit on the number of sections we look for so that bad data
11617 doesn't cause us to loop forever. */
11618
73869dc2 11619#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11620 (1 /* .debug_info or .debug_types */ \
11621 + 1 /* .debug_abbrev */ \
11622 + 1 /* .debug_line */ \
11623 + 1 /* .debug_loc */ \
11624 + 1 /* .debug_str_offsets */ \
19ac8c2e 11625 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11626 + 1 /* trailing zero */)
11627
11628 memset (&sections, 0, sizeof (sections));
80626a55 11629
73869dc2 11630 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11631 {
11632 asection *sectp;
11633 uint32_t section_nr =
11634 read_4_bytes (dbfd,
73869dc2
DE
11635 dwp_htab->section_pool.v1.indices
11636 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11637
11638 if (section_nr == 0)
11639 break;
11640 if (section_nr >= dwp_file->num_sections)
11641 {
11642 error (_("Dwarf Error: bad DWP hash table, section number too large"
11643 " [in module %s]"),
11644 dwp_file->name);
11645 }
11646
11647 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11648 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11649 {
11650 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11651 " [in module %s]"),
11652 dwp_file->name);
11653 }
11654 }
11655
11656 if (i < 2
96b79293
TT
11657 || sections.info_or_types.empty ()
11658 || sections.abbrev.empty ())
80626a55
DE
11659 {
11660 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11661 " [in module %s]"),
11662 dwp_file->name);
11663 }
73869dc2 11664 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11665 {
11666 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11667 " [in module %s]"),
11668 dwp_file->name);
11669 }
11670
11671 /* It's easier for the rest of the code if we fake a struct dwo_file and
11672 have dwo_unit "live" in that. At least for now.
11673
11674 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11675 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11676 file, we can combine them back into a virtual DWO file to save space
11677 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11678 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11679
791afaa2
TT
11680 std::string virtual_dwo_name =
11681 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11682 sections.abbrev.get_id (),
11683 sections.line.get_id (),
11684 sections.loc.get_id (),
11685 sections.str_offsets.get_id ());
80626a55 11686 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11687 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11688 virtual_dwo_name.c_str (),
11689 comp_dir);
80626a55
DE
11690 /* Create one if necessary. */
11691 if (*dwo_file_slot == NULL)
11692 {
b4f54984 11693 if (dwarf_read_debug)
80626a55
DE
11694 {
11695 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11696 virtual_dwo_name.c_str ());
80626a55 11697 }
51ac9db5 11698 dwo_file = new struct dwo_file;
be1e3d3e 11699 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
0ac5b59e 11700 dwo_file->comp_dir = comp_dir;
80626a55
DE
11701 dwo_file->sections.abbrev = sections.abbrev;
11702 dwo_file->sections.line = sections.line;
11703 dwo_file->sections.loc = sections.loc;
11704 dwo_file->sections.macinfo = sections.macinfo;
11705 dwo_file->sections.macro = sections.macro;
11706 dwo_file->sections.str_offsets = sections.str_offsets;
11707 /* The "str" section is global to the entire DWP file. */
11708 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11709 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11710 there's no need to record it in dwo_file.
11711 Also, we can't simply record type sections in dwo_file because
11712 we record a pointer into the vector in dwo_unit. As we collect more
11713 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11714 for it, invalidating all copies of pointers into the previous
11715 contents. */
80626a55
DE
11716 *dwo_file_slot = dwo_file;
11717 }
11718 else
11719 {
b4f54984 11720 if (dwarf_read_debug)
80626a55
DE
11721 {
11722 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11723 virtual_dwo_name.c_str ());
80626a55 11724 }
9a3c8263 11725 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11726 }
80626a55
DE
11727
11728 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11729 dwo_unit->dwo_file = dwo_file;
11730 dwo_unit->signature = signature;
8d749320
SM
11731 dwo_unit->section =
11732 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11733 *dwo_unit->section = sections.info_or_types;
57d63ce2 11734 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11735
11736 return dwo_unit;
11737}
11738
73869dc2
DE
11739/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11740 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11741 piece within that section used by a TU/CU, return a virtual section
11742 of just that piece. */
11743
11744static struct dwarf2_section_info
ed2dc618
SM
11745create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11746 struct dwarf2_section_info *section,
73869dc2
DE
11747 bfd_size_type offset, bfd_size_type size)
11748{
11749 struct dwarf2_section_info result;
11750 asection *sectp;
11751
11752 gdb_assert (section != NULL);
11753 gdb_assert (!section->is_virtual);
11754
11755 memset (&result, 0, sizeof (result));
11756 result.s.containing_section = section;
dc4ccb6f 11757 result.is_virtual = true;
73869dc2
DE
11758
11759 if (size == 0)
11760 return result;
11761
96b79293 11762 sectp = section->get_bfd_section ();
73869dc2
DE
11763
11764 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11765 bounds of the real section. This is a pretty-rare event, so just
11766 flag an error (easier) instead of a warning and trying to cope. */
11767 if (sectp == NULL
fd361982 11768 || offset + size > bfd_section_size (sectp))
73869dc2 11769 {
73869dc2
DE
11770 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11771 " in section %s [in module %s]"),
fd361982 11772 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11773 objfile_name (dwarf2_per_objfile->objfile));
11774 }
11775
11776 result.virtual_offset = offset;
11777 result.size = size;
11778 return result;
11779}
11780
11781/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11782 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11783 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11784 This is for DWP version 2 files. */
11785
11786static struct dwo_unit *
ed2dc618
SM
11787create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11788 struct dwp_file *dwp_file,
73869dc2
DE
11789 uint32_t unit_index,
11790 const char *comp_dir,
11791 ULONGEST signature, int is_debug_types)
11792{
11793 struct objfile *objfile = dwarf2_per_objfile->objfile;
11794 const struct dwp_hash_table *dwp_htab =
11795 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11796 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11797 const char *kind = is_debug_types ? "TU" : "CU";
11798 struct dwo_file *dwo_file;
11799 struct dwo_unit *dwo_unit;
11800 struct virtual_v2_dwo_sections sections;
11801 void **dwo_file_slot;
73869dc2
DE
11802 int i;
11803
11804 gdb_assert (dwp_file->version == 2);
11805
b4f54984 11806 if (dwarf_read_debug)
73869dc2
DE
11807 {
11808 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11809 kind,
11810 pulongest (unit_index), hex_string (signature),
11811 dwp_file->name);
11812 }
11813
11814 /* Fetch the section offsets of this DWO unit. */
11815
11816 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11817
11818 for (i = 0; i < dwp_htab->nr_columns; ++i)
11819 {
11820 uint32_t offset = read_4_bytes (dbfd,
11821 dwp_htab->section_pool.v2.offsets
11822 + (((unit_index - 1) * dwp_htab->nr_columns
11823 + i)
11824 * sizeof (uint32_t)));
11825 uint32_t size = read_4_bytes (dbfd,
11826 dwp_htab->section_pool.v2.sizes
11827 + (((unit_index - 1) * dwp_htab->nr_columns
11828 + i)
11829 * sizeof (uint32_t)));
11830
11831 switch (dwp_htab->section_pool.v2.section_ids[i])
11832 {
11833 case DW_SECT_INFO:
11834 case DW_SECT_TYPES:
11835 sections.info_or_types_offset = offset;
11836 sections.info_or_types_size = size;
11837 break;
11838 case DW_SECT_ABBREV:
11839 sections.abbrev_offset = offset;
11840 sections.abbrev_size = size;
11841 break;
11842 case DW_SECT_LINE:
11843 sections.line_offset = offset;
11844 sections.line_size = size;
11845 break;
11846 case DW_SECT_LOC:
11847 sections.loc_offset = offset;
11848 sections.loc_size = size;
11849 break;
11850 case DW_SECT_STR_OFFSETS:
11851 sections.str_offsets_offset = offset;
11852 sections.str_offsets_size = size;
11853 break;
11854 case DW_SECT_MACINFO:
11855 sections.macinfo_offset = offset;
11856 sections.macinfo_size = size;
11857 break;
11858 case DW_SECT_MACRO:
11859 sections.macro_offset = offset;
11860 sections.macro_size = size;
11861 break;
11862 }
11863 }
11864
11865 /* It's easier for the rest of the code if we fake a struct dwo_file and
11866 have dwo_unit "live" in that. At least for now.
11867
11868 The DWP file can be made up of a random collection of CUs and TUs.
11869 However, for each CU + set of TUs that came from the same original DWO
11870 file, we can combine them back into a virtual DWO file to save space
11871 (fewer struct dwo_file objects to allocate). Remember that for really
11872 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11873
791afaa2
TT
11874 std::string virtual_dwo_name =
11875 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11876 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11877 (long) (sections.line_size ? sections.line_offset : 0),
11878 (long) (sections.loc_size ? sections.loc_offset : 0),
11879 (long) (sections.str_offsets_size
11880 ? sections.str_offsets_offset : 0));
73869dc2 11881 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11882 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11883 virtual_dwo_name.c_str (),
11884 comp_dir);
73869dc2
DE
11885 /* Create one if necessary. */
11886 if (*dwo_file_slot == NULL)
11887 {
b4f54984 11888 if (dwarf_read_debug)
73869dc2
DE
11889 {
11890 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11891 virtual_dwo_name.c_str ());
73869dc2 11892 }
51ac9db5 11893 dwo_file = new struct dwo_file;
be1e3d3e 11894 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
73869dc2
DE
11895 dwo_file->comp_dir = comp_dir;
11896 dwo_file->sections.abbrev =
ed2dc618 11897 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11898 sections.abbrev_offset, sections.abbrev_size);
11899 dwo_file->sections.line =
ed2dc618 11900 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11901 sections.line_offset, sections.line_size);
11902 dwo_file->sections.loc =
ed2dc618 11903 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11904 sections.loc_offset, sections.loc_size);
11905 dwo_file->sections.macinfo =
ed2dc618 11906 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11907 sections.macinfo_offset, sections.macinfo_size);
11908 dwo_file->sections.macro =
ed2dc618 11909 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11910 sections.macro_offset, sections.macro_size);
11911 dwo_file->sections.str_offsets =
ed2dc618
SM
11912 create_dwp_v2_section (dwarf2_per_objfile,
11913 &dwp_file->sections.str_offsets,
73869dc2
DE
11914 sections.str_offsets_offset,
11915 sections.str_offsets_size);
11916 /* The "str" section is global to the entire DWP file. */
11917 dwo_file->sections.str = dwp_file->sections.str;
11918 /* The info or types section is assigned below to dwo_unit,
11919 there's no need to record it in dwo_file.
11920 Also, we can't simply record type sections in dwo_file because
11921 we record a pointer into the vector in dwo_unit. As we collect more
11922 types we'll grow the vector and eventually have to reallocate space
11923 for it, invalidating all copies of pointers into the previous
11924 contents. */
11925 *dwo_file_slot = dwo_file;
11926 }
11927 else
11928 {
b4f54984 11929 if (dwarf_read_debug)
73869dc2
DE
11930 {
11931 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11932 virtual_dwo_name.c_str ());
73869dc2 11933 }
9a3c8263 11934 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11935 }
73869dc2
DE
11936
11937 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11938 dwo_unit->dwo_file = dwo_file;
11939 dwo_unit->signature = signature;
8d749320
SM
11940 dwo_unit->section =
11941 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11942 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11943 is_debug_types
73869dc2
DE
11944 ? &dwp_file->sections.types
11945 : &dwp_file->sections.info,
11946 sections.info_or_types_offset,
11947 sections.info_or_types_size);
11948 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11949
11950 return dwo_unit;
11951}
11952
57d63ce2
DE
11953/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11954 Returns NULL if the signature isn't found. */
80626a55
DE
11955
11956static struct dwo_unit *
ed2dc618
SM
11957lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11958 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11959 ULONGEST signature, int is_debug_types)
80626a55 11960{
57d63ce2
DE
11961 const struct dwp_hash_table *dwp_htab =
11962 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11963 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11964 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11965 uint32_t hash = signature & mask;
11966 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11967 unsigned int i;
11968 void **slot;
870f88f7 11969 struct dwo_unit find_dwo_cu;
80626a55
DE
11970
11971 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11972 find_dwo_cu.signature = signature;
19ac8c2e 11973 slot = htab_find_slot (is_debug_types
48b490f2
TT
11974 ? dwp_file->loaded_tus.get ()
11975 : dwp_file->loaded_cus.get (),
19ac8c2e 11976 &find_dwo_cu, INSERT);
80626a55
DE
11977
11978 if (*slot != NULL)
9a3c8263 11979 return (struct dwo_unit *) *slot;
80626a55
DE
11980
11981 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11982 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11983 {
11984 ULONGEST signature_in_table;
11985
11986 signature_in_table =
57d63ce2 11987 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11988 if (signature_in_table == signature)
11989 {
57d63ce2
DE
11990 uint32_t unit_index =
11991 read_4_bytes (dbfd,
11992 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11993
73869dc2
DE
11994 if (dwp_file->version == 1)
11995 {
ed2dc618
SM
11996 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11997 dwp_file, unit_index,
73869dc2
DE
11998 comp_dir, signature,
11999 is_debug_types);
12000 }
12001 else
12002 {
ed2dc618
SM
12003 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12004 dwp_file, unit_index,
73869dc2
DE
12005 comp_dir, signature,
12006 is_debug_types);
12007 }
9a3c8263 12008 return (struct dwo_unit *) *slot;
80626a55
DE
12009 }
12010 if (signature_in_table == 0)
12011 return NULL;
12012 hash = (hash + hash2) & mask;
12013 }
12014
12015 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12016 " [in module %s]"),
12017 dwp_file->name);
12018}
12019
ab5088bf 12020/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12021 Open the file specified by FILE_NAME and hand it off to BFD for
12022 preliminary analysis. Return a newly initialized bfd *, which
12023 includes a canonicalized copy of FILE_NAME.
80626a55 12024 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12025 SEARCH_CWD is true if the current directory is to be searched.
12026 It will be searched before debug-file-directory.
13aaf454
DE
12027 If successful, the file is added to the bfd include table of the
12028 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12029 If unable to find/open the file, return NULL.
3019eac3
DE
12030 NOTE: This function is derived from symfile_bfd_open. */
12031
192b62ce 12032static gdb_bfd_ref_ptr
ed2dc618
SM
12033try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12034 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12035{
24b9144d 12036 int desc;
9c02c129
DE
12037 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12038 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12039 to debug_file_directory. */
e0cc99a6 12040 const char *search_path;
9c02c129
DE
12041 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12042
e0cc99a6 12043 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12044 if (search_cwd)
12045 {
12046 if (*debug_file_directory != '\0')
e0cc99a6
TT
12047 {
12048 search_path_holder.reset (concat (".", dirname_separator_string,
12049 debug_file_directory,
12050 (char *) NULL));
12051 search_path = search_path_holder.get ();
12052 }
6ac97d4c 12053 else
e0cc99a6 12054 search_path = ".";
6ac97d4c 12055 }
9c02c129 12056 else
e0cc99a6 12057 search_path = debug_file_directory;
3019eac3 12058
24b9144d 12059 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12060 if (is_dwp)
12061 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12062
12063 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12064 desc = openp (search_path, flags, file_name,
3019eac3
DE
12065 O_RDONLY | O_BINARY, &absolute_name);
12066 if (desc < 0)
12067 return NULL;
12068
e0cc99a6
TT
12069 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12070 gnutarget, desc));
9c02c129
DE
12071 if (sym_bfd == NULL)
12072 return NULL;
192b62ce 12073 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12074
192b62ce
TT
12075 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12076 return NULL;
3019eac3 12077
13aaf454
DE
12078 /* Success. Record the bfd as having been included by the objfile's bfd.
12079 This is important because things like demangled_names_hash lives in the
12080 objfile's per_bfd space and may have references to things like symbol
12081 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12082 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12083
3019eac3
DE
12084 return sym_bfd;
12085}
12086
ab5088bf 12087/* Try to open DWO file FILE_NAME.
3019eac3
DE
12088 COMP_DIR is the DW_AT_comp_dir attribute.
12089 The result is the bfd handle of the file.
12090 If there is a problem finding or opening the file, return NULL.
12091 Upon success, the canonicalized path of the file is stored in the bfd,
12092 same as symfile_bfd_open. */
12093
192b62ce 12094static gdb_bfd_ref_ptr
ed2dc618
SM
12095open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12096 const char *file_name, const char *comp_dir)
3019eac3 12097{
80626a55 12098 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12099 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12100 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12101
12102 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12103
12104 if (comp_dir != NULL)
12105 {
43816ebc
TT
12106 gdb::unique_xmalloc_ptr<char> path_to_try
12107 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12108
12109 /* NOTE: If comp_dir is a relative path, this will also try the
12110 search path, which seems useful. */
ed2dc618 12111 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12112 path_to_try.get (),
ed2dc618 12113 0 /*is_dwp*/,
192b62ce 12114 1 /*search_cwd*/));
3019eac3
DE
12115 if (abfd != NULL)
12116 return abfd;
12117 }
12118
12119 /* That didn't work, try debug-file-directory, which, despite its name,
12120 is a list of paths. */
12121
12122 if (*debug_file_directory == '\0')
12123 return NULL;
12124
ed2dc618
SM
12125 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12126 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12127}
12128
80626a55
DE
12129/* This function is mapped across the sections and remembers the offset and
12130 size of each of the DWO debugging sections we are interested in. */
12131
12132static void
12133dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12134{
9a3c8263 12135 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12136 const struct dwop_section_names *names = &dwop_section_names;
12137
12138 if (section_is_p (sectp->name, &names->abbrev_dwo))
12139 {
049412e3 12140 dwo_sections->abbrev.s.section = sectp;
fd361982 12141 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12142 }
12143 else if (section_is_p (sectp->name, &names->info_dwo))
12144 {
049412e3 12145 dwo_sections->info.s.section = sectp;
fd361982 12146 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12147 }
12148 else if (section_is_p (sectp->name, &names->line_dwo))
12149 {
049412e3 12150 dwo_sections->line.s.section = sectp;
fd361982 12151 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12152 }
12153 else if (section_is_p (sectp->name, &names->loc_dwo))
12154 {
049412e3 12155 dwo_sections->loc.s.section = sectp;
fd361982 12156 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12157 }
12158 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12159 {
049412e3 12160 dwo_sections->macinfo.s.section = sectp;
fd361982 12161 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12162 }
12163 else if (section_is_p (sectp->name, &names->macro_dwo))
12164 {
049412e3 12165 dwo_sections->macro.s.section = sectp;
fd361982 12166 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12167 }
12168 else if (section_is_p (sectp->name, &names->str_dwo))
12169 {
049412e3 12170 dwo_sections->str.s.section = sectp;
fd361982 12171 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12172 }
12173 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12174 {
049412e3 12175 dwo_sections->str_offsets.s.section = sectp;
fd361982 12176 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12177 }
12178 else if (section_is_p (sectp->name, &names->types_dwo))
12179 {
12180 struct dwarf2_section_info type_section;
12181
12182 memset (&type_section, 0, sizeof (type_section));
049412e3 12183 type_section.s.section = sectp;
fd361982 12184 type_section.size = bfd_section_size (sectp);
fd5866f6 12185 dwo_sections->types.push_back (type_section);
80626a55
DE
12186 }
12187}
12188
ab5088bf 12189/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12190 by PER_CU. This is for the non-DWP case.
80626a55 12191 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12192
12193static struct dwo_file *
0ac5b59e
DE
12194open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12195 const char *dwo_name, const char *comp_dir)
3019eac3 12196{
ed2dc618 12197 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12198
fb1eb2f9 12199 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12200 if (dbfd == NULL)
12201 {
b4f54984 12202 if (dwarf_read_debug)
80626a55
DE
12203 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12204 return NULL;
12205 }
263db9a1 12206
51ac9db5 12207 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12208 dwo_file->dwo_name = dwo_name;
12209 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12210 dwo_file->dbfd = std::move (dbfd);
3019eac3 12211
fb1eb2f9 12212 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12213 &dwo_file->sections);
3019eac3 12214
18a8505e
AT
12215 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12216 dwo_file->sections.info, dwo_file->cus);
3019eac3 12217
263db9a1 12218 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12219 dwo_file->sections.types, dwo_file->tus);
3019eac3 12220
b4f54984 12221 if (dwarf_read_debug)
80626a55
DE
12222 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12223
263db9a1 12224 return dwo_file.release ();
3019eac3
DE
12225}
12226
80626a55 12227/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12228 size of each of the DWP debugging sections common to version 1 and 2 that
12229 we are interested in. */
3019eac3 12230
80626a55 12231static void
73869dc2
DE
12232dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12233 void *dwp_file_ptr)
3019eac3 12234{
9a3c8263 12235 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12236 const struct dwop_section_names *names = &dwop_section_names;
12237 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12238
80626a55 12239 /* Record the ELF section number for later lookup: this is what the
73869dc2 12240 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12241 gdb_assert (elf_section_nr < dwp_file->num_sections);
12242 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12243
80626a55
DE
12244 /* Look for specific sections that we need. */
12245 if (section_is_p (sectp->name, &names->str_dwo))
12246 {
049412e3 12247 dwp_file->sections.str.s.section = sectp;
fd361982 12248 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12249 }
12250 else if (section_is_p (sectp->name, &names->cu_index))
12251 {
049412e3 12252 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12253 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12254 }
12255 else if (section_is_p (sectp->name, &names->tu_index))
12256 {
049412e3 12257 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12258 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12259 }
12260}
3019eac3 12261
73869dc2
DE
12262/* This function is mapped across the sections and remembers the offset and
12263 size of each of the DWP version 2 debugging sections that we are interested
12264 in. This is split into a separate function because we don't know if we
12265 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12266
12267static void
12268dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12269{
9a3c8263 12270 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12271 const struct dwop_section_names *names = &dwop_section_names;
12272 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12273
12274 /* Record the ELF section number for later lookup: this is what the
12275 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12276 gdb_assert (elf_section_nr < dwp_file->num_sections);
12277 dwp_file->elf_sections[elf_section_nr] = sectp;
12278
12279 /* Look for specific sections that we need. */
12280 if (section_is_p (sectp->name, &names->abbrev_dwo))
12281 {
049412e3 12282 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12283 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12284 }
12285 else if (section_is_p (sectp->name, &names->info_dwo))
12286 {
049412e3 12287 dwp_file->sections.info.s.section = sectp;
fd361982 12288 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12289 }
12290 else if (section_is_p (sectp->name, &names->line_dwo))
12291 {
049412e3 12292 dwp_file->sections.line.s.section = sectp;
fd361982 12293 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12294 }
12295 else if (section_is_p (sectp->name, &names->loc_dwo))
12296 {
049412e3 12297 dwp_file->sections.loc.s.section = sectp;
fd361982 12298 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12299 }
12300 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12301 {
049412e3 12302 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12303 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12304 }
12305 else if (section_is_p (sectp->name, &names->macro_dwo))
12306 {
049412e3 12307 dwp_file->sections.macro.s.section = sectp;
fd361982 12308 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12309 }
12310 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12311 {
049412e3 12312 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12313 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12314 }
12315 else if (section_is_p (sectp->name, &names->types_dwo))
12316 {
049412e3 12317 dwp_file->sections.types.s.section = sectp;
fd361982 12318 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12319 }
12320}
12321
80626a55 12322/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12323
80626a55
DE
12324static hashval_t
12325hash_dwp_loaded_cutus (const void *item)
12326{
9a3c8263 12327 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12328
80626a55
DE
12329 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12330 return dwo_unit->signature;
3019eac3
DE
12331}
12332
80626a55 12333/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12334
80626a55
DE
12335static int
12336eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12337{
9a3c8263
SM
12338 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12339 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12340
80626a55
DE
12341 return dua->signature == dub->signature;
12342}
3019eac3 12343
80626a55 12344/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12345
48b490f2 12346static htab_up
298e9637 12347allocate_dwp_loaded_cutus_table ()
80626a55 12348{
48b490f2
TT
12349 return htab_up (htab_create_alloc (3,
12350 hash_dwp_loaded_cutus,
12351 eq_dwp_loaded_cutus,
12352 NULL, xcalloc, xfree));
80626a55 12353}
3019eac3 12354
ab5088bf
DE
12355/* Try to open DWP file FILE_NAME.
12356 The result is the bfd handle of the file.
12357 If there is a problem finding or opening the file, return NULL.
12358 Upon success, the canonicalized path of the file is stored in the bfd,
12359 same as symfile_bfd_open. */
12360
192b62ce 12361static gdb_bfd_ref_ptr
ed2dc618
SM
12362open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12363 const char *file_name)
ab5088bf 12364{
ed2dc618
SM
12365 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12366 1 /*is_dwp*/,
192b62ce 12367 1 /*search_cwd*/));
6ac97d4c
DE
12368 if (abfd != NULL)
12369 return abfd;
12370
12371 /* Work around upstream bug 15652.
12372 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12373 [Whether that's a "bug" is debatable, but it is getting in our way.]
12374 We have no real idea where the dwp file is, because gdb's realpath-ing
12375 of the executable's path may have discarded the needed info.
12376 [IWBN if the dwp file name was recorded in the executable, akin to
12377 .gnu_debuglink, but that doesn't exist yet.]
12378 Strip the directory from FILE_NAME and search again. */
12379 if (*debug_file_directory != '\0')
12380 {
12381 /* Don't implicitly search the current directory here.
12382 If the user wants to search "." to handle this case,
12383 it must be added to debug-file-directory. */
ed2dc618
SM
12384 return try_open_dwop_file (dwarf2_per_objfile,
12385 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12386 0 /*search_cwd*/);
12387 }
12388
12389 return NULL;
ab5088bf
DE
12390}
12391
80626a55
DE
12392/* Initialize the use of the DWP file for the current objfile.
12393 By convention the name of the DWP file is ${objfile}.dwp.
12394 The result is NULL if it can't be found. */
a766d390 12395
400174b1 12396static std::unique_ptr<struct dwp_file>
ed2dc618 12397open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12398{
12399 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12400
82bf32bc
JK
12401 /* Try to find first .dwp for the binary file before any symbolic links
12402 resolving. */
6c447423
DE
12403
12404 /* If the objfile is a debug file, find the name of the real binary
12405 file and get the name of dwp file from there. */
d721ba37 12406 std::string dwp_name;
6c447423
DE
12407 if (objfile->separate_debug_objfile_backlink != NULL)
12408 {
12409 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12410 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12411
d721ba37 12412 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12413 }
12414 else
d721ba37
PA
12415 dwp_name = objfile->original_name;
12416
12417 dwp_name += ".dwp";
80626a55 12418
ed2dc618 12419 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12420 if (dbfd == NULL
12421 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12422 {
12423 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12424 dwp_name = objfile_name (objfile);
12425 dwp_name += ".dwp";
ed2dc618 12426 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12427 }
12428
80626a55
DE
12429 if (dbfd == NULL)
12430 {
b4f54984 12431 if (dwarf_read_debug)
d721ba37 12432 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12433 return std::unique_ptr<dwp_file> ();
3019eac3 12434 }
400174b1
TT
12435
12436 const char *name = bfd_get_filename (dbfd.get ());
12437 std::unique_ptr<struct dwp_file> dwp_file
12438 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12439
0a0f4c01 12440 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12441 dwp_file->elf_sections =
12442 OBSTACK_CALLOC (&objfile->objfile_obstack,
12443 dwp_file->num_sections, asection *);
12444
400174b1
TT
12445 bfd_map_over_sections (dwp_file->dbfd.get (),
12446 dwarf2_locate_common_dwp_sections,
12447 dwp_file.get ());
80626a55 12448
400174b1
TT
12449 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12450 0);
80626a55 12451
400174b1
TT
12452 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12453 1);
80626a55 12454
73869dc2 12455 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12456 if (dwp_file->cus && dwp_file->tus
12457 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12458 {
12459 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12460 pretty bizarre. We use pulongest here because that's the established
4d65956b 12461 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12462 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12463 " TU version %s [in DWP file %s]"),
12464 pulongest (dwp_file->cus->version),
d721ba37 12465 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12466 }
08302ed2
DE
12467
12468 if (dwp_file->cus)
12469 dwp_file->version = dwp_file->cus->version;
12470 else if (dwp_file->tus)
12471 dwp_file->version = dwp_file->tus->version;
12472 else
12473 dwp_file->version = 2;
73869dc2
DE
12474
12475 if (dwp_file->version == 2)
400174b1
TT
12476 bfd_map_over_sections (dwp_file->dbfd.get (),
12477 dwarf2_locate_v2_dwp_sections,
12478 dwp_file.get ());
73869dc2 12479
298e9637
SM
12480 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12481 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12482
b4f54984 12483 if (dwarf_read_debug)
80626a55
DE
12484 {
12485 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12486 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12487 " %s CUs, %s TUs\n",
12488 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12489 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12490 }
12491
12492 return dwp_file;
3019eac3 12493}
c906108c 12494
ab5088bf
DE
12495/* Wrapper around open_and_init_dwp_file, only open it once. */
12496
12497static struct dwp_file *
ed2dc618 12498get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12499{
12500 if (! dwarf2_per_objfile->dwp_checked)
12501 {
ed2dc618
SM
12502 dwarf2_per_objfile->dwp_file
12503 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12504 dwarf2_per_objfile->dwp_checked = 1;
12505 }
400174b1 12506 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12507}
12508
80626a55
DE
12509/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12510 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12511 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12512 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12513 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12514
12515 This is called, for example, when wanting to read a variable with a
12516 complex location. Therefore we don't want to do file i/o for every call.
12517 Therefore we don't want to look for a DWO file on every call.
12518 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12519 then we check if we've already seen DWO_NAME, and only THEN do we check
12520 for a DWO file.
12521
1c658ad5 12522 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12523 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12524
3019eac3 12525static struct dwo_unit *
80626a55
DE
12526lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12527 const char *dwo_name, const char *comp_dir,
12528 ULONGEST signature, int is_debug_types)
3019eac3 12529{
ed2dc618 12530 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12531 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12532 const char *kind = is_debug_types ? "TU" : "CU";
12533 void **dwo_file_slot;
3019eac3 12534 struct dwo_file *dwo_file;
80626a55 12535 struct dwp_file *dwp_file;
cb1df416 12536
6a506a2d
DE
12537 /* First see if there's a DWP file.
12538 If we have a DWP file but didn't find the DWO inside it, don't
12539 look for the original DWO file. It makes gdb behave differently
12540 depending on whether one is debugging in the build tree. */
cf2c3c16 12541
ed2dc618 12542 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12543 if (dwp_file != NULL)
cf2c3c16 12544 {
80626a55
DE
12545 const struct dwp_hash_table *dwp_htab =
12546 is_debug_types ? dwp_file->tus : dwp_file->cus;
12547
12548 if (dwp_htab != NULL)
12549 {
12550 struct dwo_unit *dwo_cutu =
ed2dc618 12551 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12552 signature, is_debug_types);
80626a55
DE
12553
12554 if (dwo_cutu != NULL)
12555 {
b4f54984 12556 if (dwarf_read_debug)
80626a55
DE
12557 {
12558 fprintf_unfiltered (gdb_stdlog,
12559 "Virtual DWO %s %s found: @%s\n",
12560 kind, hex_string (signature),
12561 host_address_to_string (dwo_cutu));
12562 }
12563 return dwo_cutu;
12564 }
12565 }
12566 }
6a506a2d 12567 else
80626a55 12568 {
6a506a2d 12569 /* No DWP file, look for the DWO file. */
80626a55 12570
ed2dc618
SM
12571 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12572 dwo_name, comp_dir);
6a506a2d 12573 if (*dwo_file_slot == NULL)
80626a55 12574 {
6a506a2d
DE
12575 /* Read in the file and build a table of the CUs/TUs it contains. */
12576 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12577 }
6a506a2d 12578 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12579 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12580
6a506a2d 12581 if (dwo_file != NULL)
19c3d4c9 12582 {
6a506a2d
DE
12583 struct dwo_unit *dwo_cutu = NULL;
12584
12585 if (is_debug_types && dwo_file->tus)
12586 {
12587 struct dwo_unit find_dwo_cutu;
12588
12589 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12590 find_dwo_cutu.signature = signature;
9a3c8263 12591 dwo_cutu
b0b6a987
TT
12592 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12593 &find_dwo_cutu);
6a506a2d 12594 }
33c5cd75 12595 else if (!is_debug_types && dwo_file->cus)
80626a55 12596 {
33c5cd75
DB
12597 struct dwo_unit find_dwo_cutu;
12598
12599 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12600 find_dwo_cutu.signature = signature;
b0b6a987 12601 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12602 &find_dwo_cutu);
6a506a2d
DE
12603 }
12604
12605 if (dwo_cutu != NULL)
12606 {
b4f54984 12607 if (dwarf_read_debug)
6a506a2d
DE
12608 {
12609 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12610 kind, dwo_name, hex_string (signature),
12611 host_address_to_string (dwo_cutu));
12612 }
12613 return dwo_cutu;
80626a55
DE
12614 }
12615 }
2e276125 12616 }
9cdd5dbd 12617
80626a55
DE
12618 /* We didn't find it. This could mean a dwo_id mismatch, or
12619 someone deleted the DWO/DWP file, or the search path isn't set up
12620 correctly to find the file. */
12621
b4f54984 12622 if (dwarf_read_debug)
80626a55
DE
12623 {
12624 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12625 kind, dwo_name, hex_string (signature));
12626 }
3019eac3 12627
6656a72d
DE
12628 /* This is a warning and not a complaint because it can be caused by
12629 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12630 {
12631 /* Print the name of the DWP file if we looked there, helps the user
12632 better diagnose the problem. */
791afaa2 12633 std::string dwp_text;
43942612
DE
12634
12635 if (dwp_file != NULL)
791afaa2
TT
12636 dwp_text = string_printf (" [in DWP file %s]",
12637 lbasename (dwp_file->name));
43942612 12638
9d8780f0 12639 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12640 " [in module %s]"),
12641 kind, dwo_name, hex_string (signature),
791afaa2 12642 dwp_text.c_str (),
43942612 12643 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12644 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12645 }
3019eac3 12646 return NULL;
5fb290d7
DJ
12647}
12648
80626a55
DE
12649/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12650 See lookup_dwo_cutu_unit for details. */
12651
12652static struct dwo_unit *
12653lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12654 const char *dwo_name, const char *comp_dir,
12655 ULONGEST signature)
12656{
12657 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12658}
12659
12660/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12661 See lookup_dwo_cutu_unit for details. */
12662
12663static struct dwo_unit *
12664lookup_dwo_type_unit (struct signatured_type *this_tu,
12665 const char *dwo_name, const char *comp_dir)
12666{
12667 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12668}
12669
89e63ee4
DE
12670/* Traversal function for queue_and_load_all_dwo_tus. */
12671
12672static int
12673queue_and_load_dwo_tu (void **slot, void *info)
12674{
12675 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12676 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12677 ULONGEST signature = dwo_unit->signature;
12678 struct signatured_type *sig_type =
12679 lookup_dwo_signatured_type (per_cu->cu, signature);
12680
12681 if (sig_type != NULL)
12682 {
12683 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12684
12685 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12686 a real dependency of PER_CU on SIG_TYPE. That is detected later
12687 while processing PER_CU. */
12688 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12689 load_full_type_unit (sig_cu);
ae640021 12690 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12691 }
12692
12693 return 1;
12694}
12695
12696/* Queue all TUs contained in the DWO of PER_CU to be read in.
12697 The DWO may have the only definition of the type, though it may not be
12698 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12699 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12700
12701static void
12702queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12703{
12704 struct dwo_unit *dwo_unit;
12705 struct dwo_file *dwo_file;
12706
12707 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12708 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12709 gdb_assert (per_cu->cu != NULL);
12710
12711 dwo_unit = per_cu->cu->dwo_unit;
12712 gdb_assert (dwo_unit != NULL);
12713
12714 dwo_file = dwo_unit->dwo_file;
12715 if (dwo_file->tus != NULL)
b0b6a987
TT
12716 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12717 per_cu);
89e63ee4
DE
12718}
12719
3019eac3 12720/* Read in various DIEs. */
348e048f 12721
d389af10 12722/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12723 Inherit only the children of the DW_AT_abstract_origin DIE not being
12724 already referenced by DW_AT_abstract_origin from the children of the
12725 current DIE. */
d389af10
JK
12726
12727static void
12728inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12729{
12730 struct die_info *child_die;
791afaa2 12731 sect_offset *offsetp;
d389af10
JK
12732 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12733 struct die_info *origin_die;
12734 /* Iterator of the ORIGIN_DIE children. */
12735 struct die_info *origin_child_die;
d389af10 12736 struct attribute *attr;
cd02d79d
PA
12737 struct dwarf2_cu *origin_cu;
12738 struct pending **origin_previous_list_in_scope;
d389af10
JK
12739
12740 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12741 if (!attr)
12742 return;
12743
cd02d79d
PA
12744 /* Note that following die references may follow to a die in a
12745 different cu. */
12746
12747 origin_cu = cu;
12748 origin_die = follow_die_ref (die, attr, &origin_cu);
12749
12750 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12751 symbols in. */
12752 origin_previous_list_in_scope = origin_cu->list_in_scope;
12753 origin_cu->list_in_scope = cu->list_in_scope;
12754
edb3359d
DJ
12755 if (die->tag != origin_die->tag
12756 && !(die->tag == DW_TAG_inlined_subroutine
12757 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12758 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12759 sect_offset_str (die->sect_off),
12760 sect_offset_str (origin_die->sect_off));
d389af10 12761
791afaa2 12762 std::vector<sect_offset> offsets;
d389af10 12763
3ea89b92
PMR
12764 for (child_die = die->child;
12765 child_die && child_die->tag;
12766 child_die = sibling_die (child_die))
12767 {
12768 struct die_info *child_origin_die;
12769 struct dwarf2_cu *child_origin_cu;
12770
12771 /* We are trying to process concrete instance entries:
216f72a1 12772 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12773 it's not relevant to our analysis here. i.e. detecting DIEs that are
12774 present in the abstract instance but not referenced in the concrete
12775 one. */
216f72a1
JK
12776 if (child_die->tag == DW_TAG_call_site
12777 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12778 continue;
12779
c38f313d
DJ
12780 /* For each CHILD_DIE, find the corresponding child of
12781 ORIGIN_DIE. If there is more than one layer of
12782 DW_AT_abstract_origin, follow them all; there shouldn't be,
12783 but GCC versions at least through 4.4 generate this (GCC PR
12784 40573). */
3ea89b92
PMR
12785 child_origin_die = child_die;
12786 child_origin_cu = cu;
c38f313d
DJ
12787 while (1)
12788 {
cd02d79d
PA
12789 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12790 child_origin_cu);
c38f313d
DJ
12791 if (attr == NULL)
12792 break;
cd02d79d
PA
12793 child_origin_die = follow_die_ref (child_origin_die, attr,
12794 &child_origin_cu);
c38f313d
DJ
12795 }
12796
d389af10
JK
12797 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12798 counterpart may exist. */
c38f313d 12799 if (child_origin_die != child_die)
d389af10 12800 {
edb3359d
DJ
12801 if (child_die->tag != child_origin_die->tag
12802 && !(child_die->tag == DW_TAG_inlined_subroutine
12803 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12804 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12805 "different tags"),
9d8780f0
SM
12806 sect_offset_str (child_die->sect_off),
12807 sect_offset_str (child_origin_die->sect_off));
c38f313d 12808 if (child_origin_die->parent != origin_die)
b98664d3 12809 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12810 "different parents"),
9d8780f0
SM
12811 sect_offset_str (child_die->sect_off),
12812 sect_offset_str (child_origin_die->sect_off));
c38f313d 12813 else
791afaa2 12814 offsets.push_back (child_origin_die->sect_off);
d389af10 12815 }
d389af10 12816 }
791afaa2
TT
12817 std::sort (offsets.begin (), offsets.end ());
12818 sect_offset *offsets_end = offsets.data () + offsets.size ();
12819 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12820 if (offsetp[-1] == *offsetp)
b98664d3 12821 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12822 "to DIE %s as their abstract origin"),
12823 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12824
791afaa2 12825 offsetp = offsets.data ();
d389af10
JK
12826 origin_child_die = origin_die->child;
12827 while (origin_child_die && origin_child_die->tag)
12828 {
12829 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12830 while (offsetp < offsets_end
9c541725 12831 && *offsetp < origin_child_die->sect_off)
d389af10 12832 offsetp++;
b64f50a1 12833 if (offsetp >= offsets_end
9c541725 12834 || *offsetp > origin_child_die->sect_off)
d389af10 12835 {
adde2bff
DE
12836 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12837 Check whether we're already processing ORIGIN_CHILD_DIE.
12838 This can happen with mutually referenced abstract_origins.
12839 PR 16581. */
12840 if (!origin_child_die->in_process)
12841 process_die (origin_child_die, origin_cu);
d389af10
JK
12842 }
12843 origin_child_die = sibling_die (origin_child_die);
12844 }
cd02d79d 12845 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12846
12847 if (cu != origin_cu)
12848 compute_delayed_physnames (origin_cu);
d389af10
JK
12849}
12850
c906108c 12851static void
e7c27a73 12852read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12853{
518817b3 12854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12856 struct context_stack *newobj;
c906108c
SS
12857 CORE_ADDR lowpc;
12858 CORE_ADDR highpc;
12859 struct die_info *child_die;
edb3359d 12860 struct attribute *attr, *call_line, *call_file;
15d034d0 12861 const char *name;
e142c38c 12862 CORE_ADDR baseaddr;
801e3a5b 12863 struct block *block;
edb3359d 12864 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12865 std::vector<struct symbol *> template_args;
34eaf542 12866 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12867
12868 if (inlined_func)
12869 {
12870 /* If we do not have call site information, we can't show the
12871 caller of this inlined function. That's too confusing, so
12872 only use the scope for local variables. */
12873 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12874 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12875 if (call_line == NULL || call_file == NULL)
12876 {
12877 read_lexical_block_scope (die, cu);
12878 return;
12879 }
12880 }
c906108c 12881
b3b3bada 12882 baseaddr = objfile->text_section_offset ();
e142c38c 12883
94af9270 12884 name = dwarf2_name (die, cu);
c906108c 12885
e8d05480
JB
12886 /* Ignore functions with missing or empty names. These are actually
12887 illegal according to the DWARF standard. */
12888 if (name == NULL)
12889 {
b98664d3 12890 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12891 sect_offset_str (die->sect_off));
e8d05480
JB
12892 return;
12893 }
12894
12895 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12896 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12897 <= PC_BOUNDS_INVALID)
e8d05480 12898 {
ae4d0c03
PM
12899 attr = dwarf2_attr (die, DW_AT_external, cu);
12900 if (!attr || !DW_UNSND (attr))
b98664d3 12901 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12902 "for subprogram DIE at %s"),
12903 sect_offset_str (die->sect_off));
e8d05480
JB
12904 return;
12905 }
c906108c 12906
3e29f34a
MR
12907 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12908 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12909
34eaf542
TT
12910 /* If we have any template arguments, then we must allocate a
12911 different sort of symbol. */
12912 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12913 {
12914 if (child_die->tag == DW_TAG_template_type_param
12915 || child_die->tag == DW_TAG_template_value_param)
12916 {
e623cf5d 12917 templ_func = allocate_template_symbol (objfile);
cf724bc9 12918 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12919 break;
12920 }
12921 }
12922
c24bdb02 12923 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12924 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12925 (struct symbol *) templ_func);
4c2df51b 12926
81873cc8 12927 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12928 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12929 cu->language);
12930
4cecd739
DJ
12931 /* If there is a location expression for DW_AT_frame_base, record
12932 it. */
e142c38c 12933 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12934 if (attr != nullptr)
fe978cb0 12935 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12936
63e43d3a
PMR
12937 /* If there is a location for the static link, record it. */
12938 newobj->static_link = NULL;
12939 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12940 if (attr != nullptr)
63e43d3a 12941 {
224c3ddb
SM
12942 newobj->static_link
12943 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12944 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12945 cu->per_cu->addr_type ());
63e43d3a
PMR
12946 }
12947
c24bdb02 12948 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12949
639d11d3 12950 if (die->child != NULL)
c906108c 12951 {
639d11d3 12952 child_die = die->child;
c906108c
SS
12953 while (child_die && child_die->tag)
12954 {
34eaf542
TT
12955 if (child_die->tag == DW_TAG_template_type_param
12956 || child_die->tag == DW_TAG_template_value_param)
12957 {
12958 struct symbol *arg = new_symbol (child_die, NULL, cu);
12959
f1078f66 12960 if (arg != NULL)
2f4732b0 12961 template_args.push_back (arg);
34eaf542
TT
12962 }
12963 else
12964 process_die (child_die, cu);
c906108c
SS
12965 child_die = sibling_die (child_die);
12966 }
12967 }
12968
d389af10
JK
12969 inherit_abstract_dies (die, cu);
12970
4a811a97
UW
12971 /* If we have a DW_AT_specification, we might need to import using
12972 directives from the context of the specification DIE. See the
12973 comment in determine_prefix. */
12974 if (cu->language == language_cplus
12975 && dwarf2_attr (die, DW_AT_specification, cu))
12976 {
12977 struct dwarf2_cu *spec_cu = cu;
12978 struct die_info *spec_die = die_specification (die, &spec_cu);
12979
12980 while (spec_die)
12981 {
12982 child_die = spec_die->child;
12983 while (child_die && child_die->tag)
12984 {
12985 if (child_die->tag == DW_TAG_imported_module)
12986 process_die (child_die, spec_cu);
12987 child_die = sibling_die (child_die);
12988 }
12989
12990 /* In some cases, GCC generates specification DIEs that
12991 themselves contain DW_AT_specification attributes. */
12992 spec_die = die_specification (spec_die, &spec_cu);
12993 }
12994 }
12995
c24bdb02 12996 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12997 /* Make a block for the local symbols within. */
c24bdb02 12998 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12999 cstk.static_link, lowpc, highpc);
801e3a5b 13000
df8a16a1 13001 /* For C++, set the block's scope. */
45280282
IB
13002 if ((cu->language == language_cplus
13003 || cu->language == language_fortran
c44af4eb
TT
13004 || cu->language == language_d
13005 || cu->language == language_rust)
4d4ec4e5 13006 && cu->processing_has_namespace_info)
195a3f6c
TT
13007 block_set_scope (block, determine_prefix (die, cu),
13008 &objfile->objfile_obstack);
df8a16a1 13009
801e3a5b
JB
13010 /* If we have address ranges, record them. */
13011 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13012
a60f3166 13013 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13014
34eaf542 13015 /* Attach template arguments to function. */
2f4732b0 13016 if (!template_args.empty ())
34eaf542
TT
13017 {
13018 gdb_assert (templ_func != NULL);
13019
2f4732b0 13020 templ_func->n_template_arguments = template_args.size ();
34eaf542 13021 templ_func->template_arguments
8d749320
SM
13022 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13023 templ_func->n_template_arguments);
34eaf542 13024 memcpy (templ_func->template_arguments,
2f4732b0 13025 template_args.data (),
34eaf542 13026 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13027
13028 /* Make sure that the symtab is set on the new symbols. Even
13029 though they don't appear in this symtab directly, other parts
13030 of gdb assume that symbols do, and this is reasonably
13031 true. */
8634679f 13032 for (symbol *sym : template_args)
3e1d3d8c 13033 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13034 }
13035
208d8187
JB
13036 /* In C++, we can have functions nested inside functions (e.g., when
13037 a function declares a class that has methods). This means that
13038 when we finish processing a function scope, we may need to go
13039 back to building a containing block's symbol lists. */
c24bdb02
KS
13040 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13041 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13042
921e78cf
JB
13043 /* If we've finished processing a top-level function, subsequent
13044 symbols go in the file symbol list. */
c24bdb02
KS
13045 if (cu->get_builder ()->outermost_context_p ())
13046 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13047}
13048
13049/* Process all the DIES contained within a lexical block scope. Start
13050 a new scope, process the dies, and then close the scope. */
13051
13052static void
e7c27a73 13053read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13054{
518817b3 13055 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13056 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13057 CORE_ADDR lowpc, highpc;
13058 struct die_info *child_die;
e142c38c
DJ
13059 CORE_ADDR baseaddr;
13060
b3b3bada 13061 baseaddr = objfile->text_section_offset ();
c906108c
SS
13062
13063 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13064 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13065 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13066 be nasty. Might be easier to properly extend generic blocks to
af34e669 13067 describe ranges. */
e385593e
JK
13068 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13069 {
13070 case PC_BOUNDS_NOT_PRESENT:
13071 /* DW_TAG_lexical_block has no attributes, process its children as if
13072 there was no wrapping by that DW_TAG_lexical_block.
13073 GCC does no longer produces such DWARF since GCC r224161. */
13074 for (child_die = die->child;
13075 child_die != NULL && child_die->tag;
13076 child_die = sibling_die (child_die))
13077 process_die (child_die, cu);
13078 return;
13079 case PC_BOUNDS_INVALID:
13080 return;
13081 }
3e29f34a
MR
13082 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13083 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13084
c24bdb02 13085 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13086 if (die->child != NULL)
c906108c 13087 {
639d11d3 13088 child_die = die->child;
c906108c
SS
13089 while (child_die && child_die->tag)
13090 {
e7c27a73 13091 process_die (child_die, cu);
c906108c
SS
13092 child_die = sibling_die (child_die);
13093 }
13094 }
3ea89b92 13095 inherit_abstract_dies (die, cu);
c24bdb02 13096 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13097
c24bdb02
KS
13098 if (*cu->get_builder ()->get_local_symbols () != NULL
13099 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13100 {
801e3a5b 13101 struct block *block
c24bdb02 13102 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13103 cstk.start_addr, highpc);
801e3a5b
JB
13104
13105 /* Note that recording ranges after traversing children, as we
13106 do here, means that recording a parent's ranges entails
13107 walking across all its children's ranges as they appear in
13108 the address map, which is quadratic behavior.
13109
13110 It would be nicer to record the parent's ranges before
13111 traversing its children, simply overriding whatever you find
13112 there. But since we don't even decide whether to create a
13113 block until after we've traversed its children, that's hard
13114 to do. */
13115 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13116 }
c24bdb02
KS
13117 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13118 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13119}
13120
216f72a1 13121/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13122
13123static void
13124read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13125{
518817b3 13126 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13127 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13128 CORE_ADDR pc, baseaddr;
13129 struct attribute *attr;
13130 struct call_site *call_site, call_site_local;
13131 void **slot;
13132 int nparams;
13133 struct die_info *child_die;
13134
b3b3bada 13135 baseaddr = objfile->text_section_offset ();
96408a79 13136
216f72a1
JK
13137 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13138 if (attr == NULL)
13139 {
13140 /* This was a pre-DWARF-5 GNU extension alias
13141 for DW_AT_call_return_pc. */
13142 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13143 }
96408a79
SA
13144 if (!attr)
13145 {
b98664d3 13146 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13147 "DIE %s [in module %s]"),
13148 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13149 return;
13150 }
cd6c91b4 13151 pc = attr->value_as_address () + baseaddr;
3e29f34a 13152 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13153
13154 if (cu->call_site_htab == NULL)
13155 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13156 NULL, &objfile->objfile_obstack,
13157 hashtab_obstack_allocate, NULL);
13158 call_site_local.pc = pc;
13159 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13160 if (*slot != NULL)
13161 {
b98664d3 13162 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13163 "DIE %s [in module %s]"),
13164 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13165 objfile_name (objfile));
96408a79
SA
13166 return;
13167 }
13168
13169 /* Count parameters at the caller. */
13170
13171 nparams = 0;
13172 for (child_die = die->child; child_die && child_die->tag;
13173 child_die = sibling_die (child_die))
13174 {
216f72a1
JK
13175 if (child_die->tag != DW_TAG_call_site_parameter
13176 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13177 {
b98664d3 13178 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13179 "DW_TAG_call_site child DIE %s [in module %s]"),
13180 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13181 objfile_name (objfile));
96408a79
SA
13182 continue;
13183 }
13184
13185 nparams++;
13186 }
13187
224c3ddb
SM
13188 call_site
13189 = ((struct call_site *)
13190 obstack_alloc (&objfile->objfile_obstack,
13191 sizeof (*call_site)
13192 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13193 *slot = call_site;
13194 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13195 call_site->pc = pc;
13196
216f72a1
JK
13197 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13198 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13199 {
13200 struct die_info *func_die;
13201
13202 /* Skip also over DW_TAG_inlined_subroutine. */
13203 for (func_die = die->parent;
13204 func_die && func_die->tag != DW_TAG_subprogram
13205 && func_die->tag != DW_TAG_subroutine_type;
13206 func_die = func_die->parent);
13207
216f72a1
JK
13208 /* DW_AT_call_all_calls is a superset
13209 of DW_AT_call_all_tail_calls. */
96408a79 13210 if (func_die
216f72a1 13211 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13212 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13213 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13214 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13215 {
13216 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13217 not complete. But keep CALL_SITE for look ups via call_site_htab,
13218 both the initial caller containing the real return address PC and
13219 the final callee containing the current PC of a chain of tail
13220 calls do not need to have the tail call list complete. But any
13221 function candidate for a virtual tail call frame searched via
13222 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13223 determined unambiguously. */
13224 }
13225 else
13226 {
13227 struct type *func_type = NULL;
13228
13229 if (func_die)
13230 func_type = get_die_type (func_die, cu);
13231 if (func_type != NULL)
13232 {
13233 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13234
13235 /* Enlist this call site to the function. */
13236 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13237 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13238 }
13239 else
b98664d3 13240 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13241 "DIE %s [in module %s]"),
13242 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13243 }
13244 }
13245
216f72a1
JK
13246 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13247 if (attr == NULL)
13248 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13249 if (attr == NULL)
13250 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13251 if (attr == NULL)
216f72a1
JK
13252 {
13253 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13254 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13255 }
96408a79 13256 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13257 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13258 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13259 else if (attr->form_is_block ())
96408a79
SA
13260 {
13261 struct dwarf2_locexpr_baton *dlbaton;
13262
8d749320 13263 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13264 dlbaton->data = DW_BLOCK (attr)->data;
13265 dlbaton->size = DW_BLOCK (attr)->size;
13266 dlbaton->per_cu = cu->per_cu;
13267
13268 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13269 }
cd6c91b4 13270 else if (attr->form_is_ref ())
96408a79 13271 {
96408a79
SA
13272 struct dwarf2_cu *target_cu = cu;
13273 struct die_info *target_die;
13274
ac9ec31b 13275 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13276 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13277 if (die_is_declaration (target_die, target_cu))
13278 {
7d45c7c3 13279 const char *target_physname;
9112db09
JK
13280
13281 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13282 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13283 if (target_physname == NULL)
9112db09 13284 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13285 if (target_physname == NULL)
b98664d3 13286 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13287 "physname, for referencing DIE %s [in module %s]"),
13288 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13289 else
7d455152 13290 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13291 }
13292 else
13293 {
13294 CORE_ADDR lowpc;
13295
13296 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13297 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13298 <= PC_BOUNDS_INVALID)
b98664d3 13299 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13300 "low pc, for referencing DIE %s [in module %s]"),
13301 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13302 else
3e29f34a
MR
13303 {
13304 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13305 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13306 }
96408a79
SA
13307 }
13308 }
13309 else
b98664d3 13310 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13311 "block nor reference, for DIE %s [in module %s]"),
13312 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13313
13314 call_site->per_cu = cu->per_cu;
13315
13316 for (child_die = die->child;
13317 child_die && child_die->tag;
13318 child_die = sibling_die (child_die))
13319 {
96408a79 13320 struct call_site_parameter *parameter;
1788b2d3 13321 struct attribute *loc, *origin;
96408a79 13322
216f72a1
JK
13323 if (child_die->tag != DW_TAG_call_site_parameter
13324 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13325 {
13326 /* Already printed the complaint above. */
13327 continue;
13328 }
13329
13330 gdb_assert (call_site->parameter_count < nparams);
13331 parameter = &call_site->parameter[call_site->parameter_count];
13332
1788b2d3
JK
13333 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13334 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13335 register is contained in DW_AT_call_value. */
96408a79 13336
24c5c679 13337 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13338 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13339 if (origin == NULL)
13340 {
13341 /* This was a pre-DWARF-5 GNU extension alias
13342 for DW_AT_call_parameter. */
13343 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13344 }
cd6c91b4 13345 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13346 {
1788b2d3 13347 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13348
13349 sect_offset sect_off
13350 = (sect_offset) dwarf2_get_ref_die_offset (origin);
4057dfde 13351 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13352 {
13353 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13354 binding can be done only inside one CU. Such referenced DIE
13355 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13356 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13357 "DW_TAG_call_site child DIE %s [in module %s]"),
13358 sect_offset_str (child_die->sect_off),
9c541725 13359 objfile_name (objfile));
d76b7dbc
JK
13360 continue;
13361 }
9c541725
PA
13362 parameter->u.param_cu_off
13363 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13364 }
4fc6c0d5 13365 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13366 {
b98664d3 13367 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13368 "DW_TAG_call_site child DIE %s [in module %s]"),
13369 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13370 continue;
13371 }
24c5c679 13372 else
96408a79 13373 {
24c5c679
JK
13374 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13375 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13376 if (parameter->u.dwarf_reg != -1)
13377 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13378 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13379 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13380 &parameter->u.fb_offset))
13381 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13382 else
13383 {
b98664d3 13384 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13385 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13386 "DW_TAG_call_site child DIE %s "
24c5c679 13387 "[in module %s]"),
9d8780f0 13388 sect_offset_str (child_die->sect_off),
9c541725 13389 objfile_name (objfile));
24c5c679
JK
13390 continue;
13391 }
96408a79
SA
13392 }
13393
216f72a1
JK
13394 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13395 if (attr == NULL)
13396 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13397 if (attr == NULL || !attr->form_is_block ())
96408a79 13398 {
b98664d3 13399 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13400 "DW_TAG_call_site child DIE %s [in module %s]"),
13401 sect_offset_str (child_die->sect_off),
9c541725 13402 objfile_name (objfile));
96408a79
SA
13403 continue;
13404 }
13405 parameter->value = DW_BLOCK (attr)->data;
13406 parameter->value_size = DW_BLOCK (attr)->size;
13407
13408 /* Parameters are not pre-cleared by memset above. */
13409 parameter->data_value = NULL;
13410 parameter->data_value_size = 0;
13411 call_site->parameter_count++;
13412
216f72a1
JK
13413 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13414 if (attr == NULL)
13415 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13416 if (attr != nullptr)
96408a79 13417 {
4fc6c0d5 13418 if (!attr->form_is_block ())
b98664d3 13419 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13420 "DW_TAG_call_site child DIE %s [in module %s]"),
13421 sect_offset_str (child_die->sect_off),
9c541725 13422 objfile_name (objfile));
96408a79
SA
13423 else
13424 {
13425 parameter->data_value = DW_BLOCK (attr)->data;
13426 parameter->data_value_size = DW_BLOCK (attr)->size;
13427 }
13428 }
13429 }
13430}
13431
71a3c369
TT
13432/* Helper function for read_variable. If DIE represents a virtual
13433 table, then return the type of the concrete object that is
13434 associated with the virtual table. Otherwise, return NULL. */
13435
13436static struct type *
13437rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13438{
13439 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13440 if (attr == NULL)
13441 return NULL;
13442
13443 /* Find the type DIE. */
13444 struct die_info *type_die = NULL;
13445 struct dwarf2_cu *type_cu = cu;
13446
cd6c91b4 13447 if (attr->form_is_ref ())
71a3c369
TT
13448 type_die = follow_die_ref (die, attr, &type_cu);
13449 if (type_die == NULL)
13450 return NULL;
13451
13452 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13453 return NULL;
13454 return die_containing_type (type_die, type_cu);
13455}
13456
13457/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13458
13459static void
13460read_variable (struct die_info *die, struct dwarf2_cu *cu)
13461{
13462 struct rust_vtable_symbol *storage = NULL;
13463
13464 if (cu->language == language_rust)
13465 {
13466 struct type *containing_type = rust_containing_type (die, cu);
13467
13468 if (containing_type != NULL)
13469 {
518817b3 13470 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13471
468c0cbb 13472 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13473 initialize_objfile_symbol (storage);
13474 storage->concrete_type = containing_type;
cf724bc9 13475 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13476 }
13477 }
13478
e4a62c65
TV
13479 struct symbol *res = new_symbol (die, NULL, cu, storage);
13480 struct attribute *abstract_origin
13481 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13482 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13483 if (res == NULL && loc && abstract_origin)
13484 {
13485 /* We have a variable without a name, but with a location and an abstract
13486 origin. This may be a concrete instance of an abstract variable
13487 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13488 later. */
13489 struct dwarf2_cu *origin_cu = cu;
13490 struct die_info *origin_die
13491 = follow_die_ref (die, abstract_origin, &origin_cu);
13492 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13493 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13494 }
71a3c369
TT
13495}
13496
43988095
JK
13497/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13498 reading .debug_rnglists.
13499 Callback's type should be:
13500 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13501 Return true if the attributes are present and valid, otherwise,
13502 return false. */
13503
13504template <typename Callback>
13505static bool
13506dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13507 Callback &&callback)
13508{
ed2dc618 13509 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13510 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13511 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13512 bfd *obfd = objfile->obfd;
43988095
JK
13513 /* Base address selection entry. */
13514 CORE_ADDR base;
13515 int found_base;
43988095 13516 const gdb_byte *buffer;
43988095
JK
13517 CORE_ADDR baseaddr;
13518 bool overflow = false;
13519
13520 found_base = cu->base_known;
13521 base = cu->base_address;
13522
96b79293 13523 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13524 if (offset >= dwarf2_per_objfile->rnglists.size)
13525 {
b98664d3 13526 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13527 offset);
13528 return false;
13529 }
13530 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13531
b3b3bada 13532 baseaddr = objfile->text_section_offset ();
43988095
JK
13533
13534 while (1)
13535 {
7814882a
JK
13536 /* Initialize it due to a false compiler warning. */
13537 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13538 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13539 + dwarf2_per_objfile->rnglists.size);
13540 unsigned int bytes_read;
13541
13542 if (buffer == buf_end)
13543 {
13544 overflow = true;
13545 break;
13546 }
13547 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13548 switch (rlet)
13549 {
13550 case DW_RLE_end_of_list:
13551 break;
13552 case DW_RLE_base_address:
13553 if (buffer + cu->header.addr_size > buf_end)
13554 {
13555 overflow = true;
13556 break;
13557 }
c8a7a66f 13558 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13559 found_base = 1;
13560 buffer += bytes_read;
13561 break;
13562 case DW_RLE_start_length:
13563 if (buffer + cu->header.addr_size > buf_end)
13564 {
13565 overflow = true;
13566 break;
13567 }
c8a7a66f
TT
13568 range_beginning = cu->header.read_address (obfd, buffer,
13569 &bytes_read);
43988095
JK
13570 buffer += bytes_read;
13571 range_end = (range_beginning
13572 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13573 buffer += bytes_read;
13574 if (buffer > buf_end)
13575 {
13576 overflow = true;
13577 break;
13578 }
13579 break;
13580 case DW_RLE_offset_pair:
13581 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13582 buffer += bytes_read;
13583 if (buffer > buf_end)
13584 {
13585 overflow = true;
13586 break;
13587 }
13588 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13589 buffer += bytes_read;
13590 if (buffer > buf_end)
13591 {
13592 overflow = true;
13593 break;
13594 }
13595 break;
13596 case DW_RLE_start_end:
13597 if (buffer + 2 * cu->header.addr_size > buf_end)
13598 {
13599 overflow = true;
13600 break;
13601 }
c8a7a66f
TT
13602 range_beginning = cu->header.read_address (obfd, buffer,
13603 &bytes_read);
43988095 13604 buffer += bytes_read;
c8a7a66f 13605 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13606 buffer += bytes_read;
13607 break;
13608 default:
b98664d3 13609 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13610 return false;
13611 }
13612 if (rlet == DW_RLE_end_of_list || overflow)
13613 break;
13614 if (rlet == DW_RLE_base_address)
13615 continue;
13616
13617 if (!found_base)
13618 {
13619 /* We have no valid base address for the ranges
13620 data. */
b98664d3 13621 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13622 return false;
13623 }
13624
13625 if (range_beginning > range_end)
13626 {
13627 /* Inverted range entries are invalid. */
b98664d3 13628 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13629 return false;
13630 }
13631
13632 /* Empty range entries have no effect. */
13633 if (range_beginning == range_end)
13634 continue;
13635
13636 range_beginning += base;
13637 range_end += base;
13638
13639 /* A not-uncommon case of bad debug info.
13640 Don't pollute the addrmap with bad data. */
13641 if (range_beginning + baseaddr == 0
13642 && !dwarf2_per_objfile->has_section_at_zero)
13643 {
b98664d3 13644 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13645 " [in module %s]"), objfile_name (objfile));
13646 continue;
13647 }
13648
13649 callback (range_beginning, range_end);
13650 }
13651
13652 if (overflow)
13653 {
b98664d3 13654 complaint (_("Offset %d is not terminated "
43988095
JK
13655 "for DW_AT_ranges attribute"),
13656 offset);
13657 return false;
13658 }
13659
13660 return true;
13661}
13662
13663/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13664 Callback's type should be:
13665 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13666 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13667
43988095 13668template <typename Callback>
43039443 13669static int
5f46c5a5 13670dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13671 Callback &&callback)
43039443 13672{
ed2dc618 13673 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13674 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13675 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13676 struct comp_unit_head *cu_header = &cu->header;
13677 bfd *obfd = objfile->obfd;
13678 unsigned int addr_size = cu_header->addr_size;
13679 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13680 /* Base address selection entry. */
13681 CORE_ADDR base;
13682 int found_base;
13683 unsigned int dummy;
d521ce57 13684 const gdb_byte *buffer;
ff013f42 13685 CORE_ADDR baseaddr;
43039443 13686
43988095
JK
13687 if (cu_header->version >= 5)
13688 return dwarf2_rnglists_process (offset, cu, callback);
13689
d00adf39
DE
13690 found_base = cu->base_known;
13691 base = cu->base_address;
43039443 13692
96b79293 13693 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13694 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13695 {
b98664d3 13696 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13697 offset);
13698 return 0;
13699 }
dce234bc 13700 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13701
b3b3bada 13702 baseaddr = objfile->text_section_offset ();
ff013f42 13703
43039443
JK
13704 while (1)
13705 {
13706 CORE_ADDR range_beginning, range_end;
13707
c8a7a66f 13708 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13709 buffer += addr_size;
c8a7a66f 13710 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13711 buffer += addr_size;
13712 offset += 2 * addr_size;
13713
13714 /* An end of list marker is a pair of zero addresses. */
13715 if (range_beginning == 0 && range_end == 0)
13716 /* Found the end of list entry. */
13717 break;
13718
13719 /* Each base address selection entry is a pair of 2 values.
13720 The first is the largest possible address, the second is
13721 the base address. Check for a base address here. */
13722 if ((range_beginning & mask) == mask)
13723 {
28d2bfb9
AB
13724 /* If we found the largest possible address, then we already
13725 have the base address in range_end. */
13726 base = range_end;
43039443
JK
13727 found_base = 1;
13728 continue;
13729 }
13730
13731 if (!found_base)
13732 {
13733 /* We have no valid base address for the ranges
13734 data. */
b98664d3 13735 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13736 return 0;
13737 }
13738
9277c30c
UW
13739 if (range_beginning > range_end)
13740 {
13741 /* Inverted range entries are invalid. */
b98664d3 13742 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13743 return 0;
13744 }
13745
13746 /* Empty range entries have no effect. */
13747 if (range_beginning == range_end)
13748 continue;
13749
43039443
JK
13750 range_beginning += base;
13751 range_end += base;
13752
01093045
DE
13753 /* A not-uncommon case of bad debug info.
13754 Don't pollute the addrmap with bad data. */
13755 if (range_beginning + baseaddr == 0
13756 && !dwarf2_per_objfile->has_section_at_zero)
13757 {
b98664d3 13758 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13759 " [in module %s]"), objfile_name (objfile));
01093045
DE
13760 continue;
13761 }
13762
5f46c5a5
JK
13763 callback (range_beginning, range_end);
13764 }
13765
13766 return 1;
13767}
13768
13769/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13770 Return 1 if the attributes are present and valid, otherwise, return 0.
13771 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13772
13773static int
13774dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13775 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13776 dwarf2_psymtab *ranges_pst)
5f46c5a5 13777{
518817b3 13778 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13779 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13780 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13781 int low_set = 0;
13782 CORE_ADDR low = 0;
13783 CORE_ADDR high = 0;
13784 int retval;
13785
13786 retval = dwarf2_ranges_process (offset, cu,
13787 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13788 {
9277c30c 13789 if (ranges_pst != NULL)
3e29f34a
MR
13790 {
13791 CORE_ADDR lowpc;
13792 CORE_ADDR highpc;
13793
79748972
TT
13794 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13795 range_beginning + baseaddr)
13796 - baseaddr);
13797 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13798 range_end + baseaddr)
13799 - baseaddr);
d320c2b5
TT
13800 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13801 lowpc, highpc - 1, ranges_pst);
3e29f34a 13802 }
ff013f42 13803
43039443
JK
13804 /* FIXME: This is recording everything as a low-high
13805 segment of consecutive addresses. We should have a
13806 data structure for discontiguous block ranges
13807 instead. */
13808 if (! low_set)
13809 {
13810 low = range_beginning;
13811 high = range_end;
13812 low_set = 1;
13813 }
13814 else
13815 {
13816 if (range_beginning < low)
13817 low = range_beginning;
13818 if (range_end > high)
13819 high = range_end;
13820 }
5f46c5a5
JK
13821 });
13822 if (!retval)
13823 return 0;
43039443
JK
13824
13825 if (! low_set)
13826 /* If the first entry is an end-of-list marker, the range
13827 describes an empty scope, i.e. no instructions. */
13828 return 0;
13829
13830 if (low_return)
13831 *low_return = low;
13832 if (high_return)
13833 *high_return = high;
13834 return 1;
13835}
13836
3a2b436a
JK
13837/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13838 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13839 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13840
3a2b436a 13841static enum pc_bounds_kind
af34e669 13842dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13843 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13844 dwarf2_psymtab *pst)
c906108c 13845{
518817b3
SM
13846 struct dwarf2_per_objfile *dwarf2_per_objfile
13847 = cu->per_cu->dwarf2_per_objfile;
c906108c 13848 struct attribute *attr;
91da1414 13849 struct attribute *attr_high;
af34e669
DJ
13850 CORE_ADDR low = 0;
13851 CORE_ADDR high = 0;
e385593e 13852 enum pc_bounds_kind ret;
c906108c 13853
91da1414
MW
13854 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13855 if (attr_high)
af34e669 13856 {
e142c38c 13857 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13858 if (attr != nullptr)
91da1414 13859 {
cd6c91b4
TT
13860 low = attr->value_as_address ();
13861 high = attr_high->value_as_address ();
13862 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13863 high += low;
91da1414 13864 }
af34e669
DJ
13865 else
13866 /* Found high w/o low attribute. */
e385593e 13867 return PC_BOUNDS_INVALID;
af34e669
DJ
13868
13869 /* Found consecutive range of addresses. */
3a2b436a 13870 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13871 }
c906108c 13872 else
af34e669 13873 {
e142c38c 13874 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13875 if (attr != NULL)
13876 {
18a8505e 13877 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13878 We take advantage of the fact that DW_AT_ranges does not appear
13879 in DW_TAG_compile_unit of DWO files. */
13880 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13881 unsigned int ranges_offset = (DW_UNSND (attr)
13882 + (need_ranges_base
13883 ? cu->ranges_base
13884 : 0));
2e3cf129 13885
af34e669 13886 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13887 .debug_ranges section. */
2e3cf129 13888 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13889 return PC_BOUNDS_INVALID;
43039443 13890 /* Found discontinuous range of addresses. */
3a2b436a 13891 ret = PC_BOUNDS_RANGES;
af34e669 13892 }
e385593e
JK
13893 else
13894 return PC_BOUNDS_NOT_PRESENT;
af34e669 13895 }
c906108c 13896
48fbe735 13897 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13898 if (high <= low)
e385593e 13899 return PC_BOUNDS_INVALID;
c906108c
SS
13900
13901 /* When using the GNU linker, .gnu.linkonce. sections are used to
13902 eliminate duplicate copies of functions and vtables and such.
13903 The linker will arbitrarily choose one and discard the others.
13904 The AT_*_pc values for such functions refer to local labels in
13905 these sections. If the section from that file was discarded, the
13906 labels are not in the output, so the relocs get a value of 0.
13907 If this is a discarded function, mark the pc bounds as invalid,
13908 so that GDB will ignore it. */
72dca2f5 13909 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13910 return PC_BOUNDS_INVALID;
c906108c
SS
13911
13912 *lowpc = low;
96408a79
SA
13913 if (highpc)
13914 *highpc = high;
af34e669 13915 return ret;
c906108c
SS
13916}
13917
b084d499
JB
13918/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13919 its low and high PC addresses. Do nothing if these addresses could not
13920 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13921 and HIGHPC to the high address if greater than HIGHPC. */
13922
13923static void
13924dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13925 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13926 struct dwarf2_cu *cu)
13927{
13928 CORE_ADDR low, high;
13929 struct die_info *child = die->child;
13930
e385593e 13931 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13932 {
325fac50
PA
13933 *lowpc = std::min (*lowpc, low);
13934 *highpc = std::max (*highpc, high);
b084d499
JB
13935 }
13936
13937 /* If the language does not allow nested subprograms (either inside
13938 subprograms or lexical blocks), we're done. */
13939 if (cu->language != language_ada)
13940 return;
6e70227d 13941
b084d499
JB
13942 /* Check all the children of the given DIE. If it contains nested
13943 subprograms, then check their pc bounds. Likewise, we need to
13944 check lexical blocks as well, as they may also contain subprogram
13945 definitions. */
13946 while (child && child->tag)
13947 {
13948 if (child->tag == DW_TAG_subprogram
13949 || child->tag == DW_TAG_lexical_block)
13950 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13951 child = sibling_die (child);
13952 }
13953}
13954
fae299cd
DC
13955/* Get the low and high pc's represented by the scope DIE, and store
13956 them in *LOWPC and *HIGHPC. If the correct values can't be
13957 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13958
13959static void
13960get_scope_pc_bounds (struct die_info *die,
13961 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13962 struct dwarf2_cu *cu)
13963{
13964 CORE_ADDR best_low = (CORE_ADDR) -1;
13965 CORE_ADDR best_high = (CORE_ADDR) 0;
13966 CORE_ADDR current_low, current_high;
13967
3a2b436a 13968 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13969 >= PC_BOUNDS_RANGES)
fae299cd
DC
13970 {
13971 best_low = current_low;
13972 best_high = current_high;
13973 }
13974 else
13975 {
13976 struct die_info *child = die->child;
13977
13978 while (child && child->tag)
13979 {
13980 switch (child->tag) {
13981 case DW_TAG_subprogram:
b084d499 13982 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13983 break;
13984 case DW_TAG_namespace:
f55ee35c 13985 case DW_TAG_module:
fae299cd
DC
13986 /* FIXME: carlton/2004-01-16: Should we do this for
13987 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13988 that current GCC's always emit the DIEs corresponding
13989 to definitions of methods of classes as children of a
13990 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13991 the DIEs giving the declarations, which could be
13992 anywhere). But I don't see any reason why the
13993 standards says that they have to be there. */
13994 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13995
13996 if (current_low != ((CORE_ADDR) -1))
13997 {
325fac50
PA
13998 best_low = std::min (best_low, current_low);
13999 best_high = std::max (best_high, current_high);
fae299cd
DC
14000 }
14001 break;
14002 default:
0963b4bd 14003 /* Ignore. */
fae299cd
DC
14004 break;
14005 }
14006
14007 child = sibling_die (child);
14008 }
14009 }
14010
14011 *lowpc = best_low;
14012 *highpc = best_high;
14013}
14014
801e3a5b
JB
14015/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14016 in DIE. */
380bca97 14017
801e3a5b
JB
14018static void
14019dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14020 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14021{
518817b3 14022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14023 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14024 struct attribute *attr;
91da1414 14025 struct attribute *attr_high;
801e3a5b 14026
91da1414
MW
14027 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14028 if (attr_high)
801e3a5b 14029 {
801e3a5b 14030 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14031 if (attr != nullptr)
801e3a5b 14032 {
cd6c91b4
TT
14033 CORE_ADDR low = attr->value_as_address ();
14034 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14035
cd6c91b4 14036 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14037 high += low;
9a619af0 14038
3e29f34a
MR
14039 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14040 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14041 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14042 }
14043 }
14044
14045 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14046 if (attr != nullptr)
801e3a5b 14047 {
18a8505e 14048 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14049 We take advantage of the fact that DW_AT_ranges does not appear
14050 in DW_TAG_compile_unit of DWO files. */
14051 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14052
14053 /* The value of the DW_AT_ranges attribute is the offset of the
14054 address range list in the .debug_ranges section. */
ab435259
DE
14055 unsigned long offset = (DW_UNSND (attr)
14056 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14057
2d5f09ec 14058 std::vector<blockrange> blockvec;
5f46c5a5
JK
14059 dwarf2_ranges_process (offset, cu,
14060 [&] (CORE_ADDR start, CORE_ADDR end)
14061 {
58fdfd2c
JK
14062 start += baseaddr;
14063 end += baseaddr;
5f46c5a5
JK
14064 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14065 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14066 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14067 blockvec.emplace_back (start, end);
5f46c5a5 14068 });
2d5f09ec
KB
14069
14070 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14071 }
14072}
14073
685b1105
JK
14074/* Check whether the producer field indicates either of GCC < 4.6, or the
14075 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14076
685b1105
JK
14077static void
14078check_producer (struct dwarf2_cu *cu)
60d5a603 14079{
38360086 14080 int major, minor;
60d5a603
JK
14081
14082 if (cu->producer == NULL)
14083 {
14084 /* For unknown compilers expect their behavior is DWARF version
14085 compliant.
14086
14087 GCC started to support .debug_types sections by -gdwarf-4 since
14088 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14089 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14090 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14091 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14092 }
b1ffba5a 14093 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14094 {
38360086
MW
14095 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14096 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14097 }
5230b05a 14098 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14099 {
14100 cu->producer_is_icc = true;
14101 cu->producer_is_icc_lt_14 = major < 14;
14102 }
c258c396
JD
14103 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14104 cu->producer_is_codewarrior = true;
685b1105
JK
14105 else
14106 {
14107 /* For other non-GCC compilers, expect their behavior is DWARF version
14108 compliant. */
60d5a603
JK
14109 }
14110
9068261f 14111 cu->checked_producer = true;
685b1105 14112}
ba919b58 14113
685b1105
JK
14114/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14115 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14116 during 4.6.0 experimental. */
14117
9068261f 14118static bool
685b1105
JK
14119producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14120{
14121 if (!cu->checked_producer)
14122 check_producer (cu);
14123
14124 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14125}
14126
c258c396
JD
14127
14128/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14129 with incorrect is_stmt attributes. */
14130
14131static bool
14132producer_is_codewarrior (struct dwarf2_cu *cu)
14133{
14134 if (!cu->checked_producer)
14135 check_producer (cu);
14136
14137 return cu->producer_is_codewarrior;
14138}
14139
405feb71 14140/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14141 DW_AT_accessibility. */
14142
14143static enum dwarf_access_attribute
14144dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14145{
14146 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14147 {
14148 /* The default DWARF 2 accessibility for members is public, the default
14149 accessibility for inheritance is private. */
14150
14151 if (die->tag != DW_TAG_inheritance)
14152 return DW_ACCESS_public;
14153 else
14154 return DW_ACCESS_private;
14155 }
14156 else
14157 {
14158 /* DWARF 3+ defines the default accessibility a different way. The same
14159 rules apply now for DW_TAG_inheritance as for the members and it only
14160 depends on the container kind. */
14161
14162 if (die->parent->tag == DW_TAG_class_type)
14163 return DW_ACCESS_private;
14164 else
14165 return DW_ACCESS_public;
14166 }
14167}
14168
74ac6d43
TT
14169/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14170 offset. If the attribute was not found return 0, otherwise return
14171 1. If it was found but could not properly be handled, set *OFFSET
14172 to 0. */
14173
14174static int
14175handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14176 LONGEST *offset)
14177{
14178 struct attribute *attr;
14179
14180 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14181 if (attr != NULL)
14182 {
14183 *offset = 0;
14184
14185 /* Note that we do not check for a section offset first here.
14186 This is because DW_AT_data_member_location is new in DWARF 4,
14187 so if we see it, we can assume that a constant form is really
14188 a constant and not a section offset. */
cd6c91b4 14189 if (attr->form_is_constant ())
74ac6d43 14190 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14191 else if (attr->form_is_section_offset ())
74ac6d43 14192 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14193 else if (attr->form_is_block ())
74ac6d43
TT
14194 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14195 else
14196 dwarf2_complex_location_expr_complaint ();
14197
14198 return 1;
14199 }
14200
14201 return 0;
14202}
14203
c906108c
SS
14204/* Add an aggregate field to the field list. */
14205
14206static void
107d2387 14207dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14208 struct dwarf2_cu *cu)
6e70227d 14209{
518817b3 14210 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14211 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14212 struct nextfield *new_field;
14213 struct attribute *attr;
14214 struct field *fp;
15d034d0 14215 const char *fieldname = "";
c906108c 14216
7d0ccb61
DJ
14217 if (die->tag == DW_TAG_inheritance)
14218 {
be2daae6
TT
14219 fip->baseclasses.emplace_back ();
14220 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14221 }
14222 else
14223 {
be2daae6
TT
14224 fip->fields.emplace_back ();
14225 new_field = &fip->fields.back ();
7d0ccb61 14226 }
be2daae6 14227
e142c38c 14228 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14229 if (attr != nullptr)
c906108c 14230 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14231 else
14232 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14233 if (new_field->accessibility != DW_ACCESS_public)
14234 fip->non_public_fields = 1;
60d5a603 14235
e142c38c 14236 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14237 if (attr != nullptr)
c906108c 14238 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14239 else
14240 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14241
14242 fp = &new_field->field;
a9a9bd0f 14243
e142c38c 14244 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14245 {
74ac6d43
TT
14246 LONGEST offset;
14247
a9a9bd0f 14248 /* Data member other than a C++ static data member. */
6e70227d 14249
c906108c 14250 /* Get type of field. */
e7c27a73 14251 fp->type = die_type (die, cu);
c906108c 14252
d6a843b5 14253 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14254
c906108c 14255 /* Get bit size of field (zero if none). */
e142c38c 14256 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14257 if (attr != nullptr)
c906108c
SS
14258 {
14259 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14260 }
14261 else
14262 {
14263 FIELD_BITSIZE (*fp) = 0;
14264 }
14265
14266 /* Get bit offset of field. */
74ac6d43
TT
14267 if (handle_data_member_location (die, cu, &offset))
14268 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14269 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14270 if (attr != nullptr)
c906108c 14271 {
d5a22e77 14272 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14273 {
14274 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14275 additional bit offset from the MSB of the containing
14276 anonymous object to the MSB of the field. We don't
14277 have to do anything special since we don't need to
14278 know the size of the anonymous object. */
f41f5e61 14279 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14280 }
14281 else
14282 {
14283 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14284 MSB of the anonymous object, subtract off the number of
14285 bits from the MSB of the field to the MSB of the
14286 object, and then subtract off the number of bits of
14287 the field itself. The result is the bit offset of
14288 the LSB of the field. */
c906108c
SS
14289 int anonymous_size;
14290 int bit_offset = DW_UNSND (attr);
14291
e142c38c 14292 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14293 if (attr != nullptr)
c906108c
SS
14294 {
14295 /* The size of the anonymous object containing
14296 the bit field is explicit, so use the
14297 indicated size (in bytes). */
14298 anonymous_size = DW_UNSND (attr);
14299 }
14300 else
14301 {
14302 /* The size of the anonymous object containing
14303 the bit field must be inferred from the type
14304 attribute of the data member containing the
14305 bit field. */
14306 anonymous_size = TYPE_LENGTH (fp->type);
14307 }
f41f5e61
PA
14308 SET_FIELD_BITPOS (*fp,
14309 (FIELD_BITPOS (*fp)
14310 + anonymous_size * bits_per_byte
14311 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14312 }
14313 }
da5b30da
AA
14314 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14315 if (attr != NULL)
14316 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14317 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14318
14319 /* Get name of field. */
39cbfefa
DJ
14320 fieldname = dwarf2_name (die, cu);
14321 if (fieldname == NULL)
14322 fieldname = "";
d8151005
DJ
14323
14324 /* The name is already allocated along with this objfile, so we don't
14325 need to duplicate it for the type. */
14326 fp->name = fieldname;
c906108c
SS
14327
14328 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14329 pointer or virtual base class pointer) to private. */
e142c38c 14330 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14331 {
d48cc9dd 14332 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14333 new_field->accessibility = DW_ACCESS_private;
14334 fip->non_public_fields = 1;
14335 }
14336 }
a9a9bd0f 14337 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14338 {
a9a9bd0f
DC
14339 /* C++ static member. */
14340
14341 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14342 is a declaration, but all versions of G++ as of this writing
14343 (so through at least 3.2.1) incorrectly generate
14344 DW_TAG_variable tags. */
6e70227d 14345
ff355380 14346 const char *physname;
c906108c 14347
a9a9bd0f 14348 /* Get name of field. */
39cbfefa
DJ
14349 fieldname = dwarf2_name (die, cu);
14350 if (fieldname == NULL)
c906108c
SS
14351 return;
14352
254e6b9e 14353 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14354 if (attr
14355 /* Only create a symbol if this is an external value.
14356 new_symbol checks this and puts the value in the global symbol
14357 table, which we want. If it is not external, new_symbol
14358 will try to put the value in cu->list_in_scope which is wrong. */
14359 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14360 {
14361 /* A static const member, not much different than an enum as far as
14362 we're concerned, except that we can support more types. */
14363 new_symbol (die, NULL, cu);
14364 }
14365
2df3850c 14366 /* Get physical name. */
ff355380 14367 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14368
d8151005
DJ
14369 /* The name is already allocated along with this objfile, so we don't
14370 need to duplicate it for the type. */
14371 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14372 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14373 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14374 }
14375 else if (die->tag == DW_TAG_inheritance)
14376 {
74ac6d43 14377 LONGEST offset;
d4b96c9a 14378
74ac6d43
TT
14379 /* C++ base class field. */
14380 if (handle_data_member_location (die, cu, &offset))
14381 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14382 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14383 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14384 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14385 }
2ddeaf8a
TT
14386 else if (die->tag == DW_TAG_variant_part)
14387 {
14388 /* process_structure_scope will treat this DIE as a union. */
14389 process_structure_scope (die, cu);
14390
14391 /* The variant part is relative to the start of the enclosing
14392 structure. */
14393 SET_FIELD_BITPOS (*fp, 0);
14394 fp->type = get_die_type (die, cu);
14395 fp->artificial = 1;
14396 fp->name = "<<variant>>";
c8c81635
TT
14397
14398 /* Normally a DW_TAG_variant_part won't have a size, but our
14399 representation requires one, so set it to the maximum of the
489dbda6
TT
14400 child sizes, being sure to account for the offset at which
14401 each child is seen. */
c8c81635
TT
14402 if (TYPE_LENGTH (fp->type) == 0)
14403 {
14404 unsigned max = 0;
14405 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14406 {
14407 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14408 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14409 if (len > max)
14410 max = len;
14411 }
c8c81635
TT
14412 TYPE_LENGTH (fp->type) = max;
14413 }
2ddeaf8a
TT
14414 }
14415 else
14416 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14417}
14418
883fd55a
KS
14419/* Can the type given by DIE define another type? */
14420
14421static bool
14422type_can_define_types (const struct die_info *die)
14423{
14424 switch (die->tag)
14425 {
14426 case DW_TAG_typedef:
14427 case DW_TAG_class_type:
14428 case DW_TAG_structure_type:
14429 case DW_TAG_union_type:
14430 case DW_TAG_enumeration_type:
14431 return true;
14432
14433 default:
14434 return false;
14435 }
14436}
14437
14438/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14439
14440static void
883fd55a
KS
14441dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14442 struct dwarf2_cu *cu)
6e70227d 14443{
be2daae6
TT
14444 struct decl_field fp;
14445 memset (&fp, 0, sizeof (fp));
98751a41 14446
883fd55a 14447 gdb_assert (type_can_define_types (die));
98751a41 14448
883fd55a 14449 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14450 fp.name = dwarf2_name (die, cu);
14451 fp.type = read_type_die (die, cu);
98751a41 14452
c191a687
KS
14453 /* Save accessibility. */
14454 enum dwarf_access_attribute accessibility;
14455 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14456 if (attr != NULL)
14457 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14458 else
14459 accessibility = dwarf2_default_access_attribute (die, cu);
14460 switch (accessibility)
14461 {
14462 case DW_ACCESS_public:
14463 /* The assumed value if neither private nor protected. */
14464 break;
14465 case DW_ACCESS_private:
be2daae6 14466 fp.is_private = 1;
c191a687
KS
14467 break;
14468 case DW_ACCESS_protected:
be2daae6 14469 fp.is_protected = 1;
c191a687
KS
14470 break;
14471 default:
b98664d3 14472 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14473 }
14474
883fd55a 14475 if (die->tag == DW_TAG_typedef)
be2daae6 14476 fip->typedef_field_list.push_back (fp);
883fd55a 14477 else
be2daae6 14478 fip->nested_types_list.push_back (fp);
98751a41
JK
14479}
14480
c906108c
SS
14481/* Create the vector of fields, and attach it to the type. */
14482
14483static void
fba45db2 14484dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14485 struct dwarf2_cu *cu)
c906108c 14486{
317f7127 14487 int nfields = fip->nfields ();
c906108c
SS
14488
14489 /* Record the field count, allocate space for the array of fields,
14490 and create blank accessibility bitfields if necessary. */
14491 TYPE_NFIELDS (type) = nfields;
14492 TYPE_FIELDS (type) = (struct field *)
be2daae6 14493 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14494
b4ba55a1 14495 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14496 {
14497 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14498
14499 TYPE_FIELD_PRIVATE_BITS (type) =
14500 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14501 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14502
14503 TYPE_FIELD_PROTECTED_BITS (type) =
14504 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14505 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14506
774b6a14
TT
14507 TYPE_FIELD_IGNORE_BITS (type) =
14508 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14509 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14510 }
14511
14512 /* If the type has baseclasses, allocate and clear a bit vector for
14513 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14514 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14515 {
be2daae6 14516 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14517 unsigned char *pointer;
c906108c
SS
14518
14519 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14520 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14521 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14522 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14523 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14524 }
14525
2ddeaf8a
TT
14526 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14527 {
14528 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14529
be2daae6 14530 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14531 {
be2daae6
TT
14532 struct nextfield &field = fip->fields[index];
14533
14534 if (field.variant.is_discriminant)
2ddeaf8a 14535 di->discriminant_index = index;
be2daae6 14536 else if (field.variant.default_branch)
2ddeaf8a
TT
14537 di->default_index = index;
14538 else
be2daae6 14539 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14540 }
14541 }
14542
be2daae6
TT
14543 /* Copy the saved-up fields into the field vector. */
14544 for (int i = 0; i < nfields; ++i)
c906108c 14545 {
be2daae6
TT
14546 struct nextfield &field
14547 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14548 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14549
be2daae6
TT
14550 TYPE_FIELD (type, i) = field.field;
14551 switch (field.accessibility)
c906108c 14552 {
c5aa993b 14553 case DW_ACCESS_private:
b4ba55a1 14554 if (cu->language != language_ada)
be2daae6 14555 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14556 break;
c906108c 14557
c5aa993b 14558 case DW_ACCESS_protected:
b4ba55a1 14559 if (cu->language != language_ada)
be2daae6 14560 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14561 break;
c906108c 14562
c5aa993b
JM
14563 case DW_ACCESS_public:
14564 break;
c906108c 14565
c5aa993b
JM
14566 default:
14567 /* Unknown accessibility. Complain and treat it as public. */
14568 {
b98664d3 14569 complaint (_("unsupported accessibility %d"),
be2daae6 14570 field.accessibility);
c5aa993b
JM
14571 }
14572 break;
c906108c 14573 }
be2daae6 14574 if (i < fip->baseclasses.size ())
c906108c 14575 {
be2daae6 14576 switch (field.virtuality)
c906108c 14577 {
c5aa993b
JM
14578 case DW_VIRTUALITY_virtual:
14579 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14580 if (cu->language == language_ada)
a73c6dcd 14581 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14582 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14583 break;
c906108c
SS
14584 }
14585 }
c906108c
SS
14586 }
14587}
14588
7d27a96d
TT
14589/* Return true if this member function is a constructor, false
14590 otherwise. */
14591
14592static int
14593dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14594{
14595 const char *fieldname;
fe978cb0 14596 const char *type_name;
7d27a96d
TT
14597 int len;
14598
14599 if (die->parent == NULL)
14600 return 0;
14601
14602 if (die->parent->tag != DW_TAG_structure_type
14603 && die->parent->tag != DW_TAG_union_type
14604 && die->parent->tag != DW_TAG_class_type)
14605 return 0;
14606
14607 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14608 type_name = dwarf2_name (die->parent, cu);
14609 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14610 return 0;
14611
14612 len = strlen (fieldname);
fe978cb0
PA
14613 return (strncmp (fieldname, type_name, len) == 0
14614 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14615}
14616
e35000a7
TBA
14617/* Check if the given VALUE is a recognized enum
14618 dwarf_defaulted_attribute constant according to DWARF5 spec,
14619 Table 7.24. */
14620
14621static bool
14622is_valid_DW_AT_defaulted (ULONGEST value)
14623{
14624 switch (value)
14625 {
14626 case DW_DEFAULTED_no:
14627 case DW_DEFAULTED_in_class:
14628 case DW_DEFAULTED_out_of_class:
14629 return true;
14630 }
14631
3142e908 14632 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14633 return false;
14634}
14635
c906108c
SS
14636/* Add a member function to the proper fieldlist. */
14637
14638static void
107d2387 14639dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14640 struct type *type, struct dwarf2_cu *cu)
c906108c 14641{
518817b3 14642 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14643 struct attribute *attr;
c906108c 14644 int i;
be2daae6 14645 struct fnfieldlist *flp = nullptr;
c906108c 14646 struct fn_field *fnp;
15d034d0 14647 const char *fieldname;
f792889a 14648 struct type *this_type;
60d5a603 14649 enum dwarf_access_attribute accessibility;
c906108c 14650
b4ba55a1 14651 if (cu->language == language_ada)
a73c6dcd 14652 error (_("unexpected member function in Ada type"));
b4ba55a1 14653
2df3850c 14654 /* Get name of member function. */
39cbfefa
DJ
14655 fieldname = dwarf2_name (die, cu);
14656 if (fieldname == NULL)
2df3850c 14657 return;
c906108c 14658
c906108c 14659 /* Look up member function name in fieldlist. */
be2daae6 14660 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14661 {
27bfe10e 14662 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14663 {
14664 flp = &fip->fnfieldlists[i];
14665 break;
14666 }
c906108c
SS
14667 }
14668
be2daae6
TT
14669 /* Create a new fnfieldlist if necessary. */
14670 if (flp == nullptr)
c906108c 14671 {
be2daae6
TT
14672 fip->fnfieldlists.emplace_back ();
14673 flp = &fip->fnfieldlists.back ();
c906108c 14674 flp->name = fieldname;
be2daae6 14675 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14676 }
14677
be2daae6
TT
14678 /* Create a new member function field and add it to the vector of
14679 fnfieldlists. */
14680 flp->fnfields.emplace_back ();
14681 fnp = &flp->fnfields.back ();
3da10d80
KS
14682
14683 /* Delay processing of the physname until later. */
9c37b5ae 14684 if (cu->language == language_cplus)
be2daae6
TT
14685 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14686 die, cu);
3da10d80
KS
14687 else
14688 {
1d06ead6 14689 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14690 fnp->physname = physname ? physname : "";
14691 }
14692
c906108c 14693 fnp->type = alloc_type (objfile);
f792889a
DJ
14694 this_type = read_type_die (die, cu);
14695 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14696 {
f792889a 14697 int nparams = TYPE_NFIELDS (this_type);
c906108c 14698
f792889a 14699 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14700 of the method itself (TYPE_CODE_METHOD). */
14701 smash_to_method_type (fnp->type, type,
f792889a
DJ
14702 TYPE_TARGET_TYPE (this_type),
14703 TYPE_FIELDS (this_type),
14704 TYPE_NFIELDS (this_type),
14705 TYPE_VARARGS (this_type));
c906108c
SS
14706
14707 /* Handle static member functions.
c5aa993b 14708 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14709 member functions. G++ helps GDB by marking the first
14710 parameter for non-static member functions (which is the this
14711 pointer) as artificial. We obtain this information from
14712 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14713 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14714 fnp->voffset = VOFFSET_STATIC;
14715 }
14716 else
b98664d3 14717 complaint (_("member function type missing for '%s'"),
3da10d80 14718 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14719
14720 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14721 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14722 fnp->fcontext = die_containing_type (die, cu);
c906108c 14723
3e43a32a
MS
14724 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14725 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14726
14727 /* Get accessibility. */
e142c38c 14728 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14729 if (attr != nullptr)
aead7601 14730 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14731 else
14732 accessibility = dwarf2_default_access_attribute (die, cu);
14733 switch (accessibility)
c906108c 14734 {
60d5a603
JK
14735 case DW_ACCESS_private:
14736 fnp->is_private = 1;
14737 break;
14738 case DW_ACCESS_protected:
14739 fnp->is_protected = 1;
14740 break;
c906108c
SS
14741 }
14742
b02dede2 14743 /* Check for artificial methods. */
e142c38c 14744 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14745 if (attr && DW_UNSND (attr) != 0)
14746 fnp->is_artificial = 1;
14747
e35000a7
TBA
14748 /* Check for defaulted methods. */
14749 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14750 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14751 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14752
14753 /* Check for deleted methods. */
14754 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14755 if (attr != nullptr && DW_UNSND (attr) != 0)
14756 fnp->is_deleted = 1;
14757
7d27a96d
TT
14758 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14759
0d564a31 14760 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14761 function. For older versions of GCC, this is an offset in the
14762 appropriate virtual table, as specified by DW_AT_containing_type.
14763 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14764 to the object address. */
14765
e142c38c 14766 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14767 if (attr != nullptr)
8e19ed76 14768 {
4fc6c0d5 14769 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14770 {
aec5aa8b
TT
14771 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14772 {
14773 /* Old-style GCC. */
14774 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14775 }
14776 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14777 || (DW_BLOCK (attr)->size > 1
14778 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14779 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14780 {
aec5aa8b
TT
14781 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14782 if ((fnp->voffset % cu->header.addr_size) != 0)
14783 dwarf2_complex_location_expr_complaint ();
14784 else
14785 fnp->voffset /= cu->header.addr_size;
14786 fnp->voffset += 2;
14787 }
14788 else
14789 dwarf2_complex_location_expr_complaint ();
14790
14791 if (!fnp->fcontext)
7e993ebf
KS
14792 {
14793 /* If there is no `this' field and no DW_AT_containing_type,
14794 we cannot actually find a base class context for the
14795 vtable! */
14796 if (TYPE_NFIELDS (this_type) == 0
14797 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14798 {
b98664d3 14799 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14800 "function \"%s\" (offset %s)"),
14801 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14802 }
14803 else
14804 {
14805 fnp->fcontext
14806 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14807 }
14808 }
aec5aa8b 14809 }
cd6c91b4 14810 else if (attr->form_is_section_offset ())
8e19ed76 14811 {
4d3c2250 14812 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14813 }
14814 else
14815 {
4d3c2250
KB
14816 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14817 fieldname);
8e19ed76 14818 }
0d564a31 14819 }
d48cc9dd
DJ
14820 else
14821 {
14822 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14823 if (attr && DW_UNSND (attr))
14824 {
14825 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14826 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14827 "but the vtable offset is not specified"),
9d8780f0 14828 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14829 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14830 TYPE_CPLUS_DYNAMIC (type) = 1;
14831 }
14832 }
c906108c
SS
14833}
14834
14835/* Create the vector of member function fields, and attach it to the type. */
14836
14837static void
fba45db2 14838dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14839 struct dwarf2_cu *cu)
c906108c 14840{
b4ba55a1 14841 if (cu->language == language_ada)
a73c6dcd 14842 error (_("unexpected member functions in Ada type"));
b4ba55a1 14843
c906108c
SS
14844 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14845 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14846 TYPE_ALLOC (type,
14847 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14848
be2daae6 14849 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14850 {
be2daae6 14851 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14852 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14853
be2daae6
TT
14854 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14855 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14856 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14857 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14858
14859 for (int k = 0; k < nf.fnfields.size (); ++k)
14860 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14861 }
14862
be2daae6 14863 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14864}
14865
1168df01
JB
14866/* Returns non-zero if NAME is the name of a vtable member in CU's
14867 language, zero otherwise. */
14868static int
14869is_vtable_name (const char *name, struct dwarf2_cu *cu)
14870{
14871 static const char vptr[] = "_vptr";
14872
9c37b5ae
TT
14873 /* Look for the C++ form of the vtable. */
14874 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14875 return 1;
14876
14877 return 0;
14878}
14879
c0dd20ea 14880/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14881 functions, with the ABI-specified layout. If TYPE describes
14882 such a structure, smash it into a member function type.
61049d3b
DJ
14883
14884 GCC shouldn't do this; it should just output pointer to member DIEs.
14885 This is GCC PR debug/28767. */
c0dd20ea 14886
0b92b5bb
TT
14887static void
14888quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14889{
09e2d7c7 14890 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14891
14892 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14893 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14894 return;
c0dd20ea
DJ
14895
14896 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14897 if (TYPE_FIELD_NAME (type, 0) == NULL
14898 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14899 || TYPE_FIELD_NAME (type, 1) == NULL
14900 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14901 return;
c0dd20ea
DJ
14902
14903 /* Find the type of the method. */
0b92b5bb 14904 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14905 if (pfn_type == NULL
14906 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14907 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14908 return;
c0dd20ea
DJ
14909
14910 /* Look for the "this" argument. */
14911 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14912 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14913 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14914 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14915 return;
c0dd20ea 14916
09e2d7c7 14917 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14918 new_type = alloc_type (objfile);
09e2d7c7 14919 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14920 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14921 TYPE_VARARGS (pfn_type));
0b92b5bb 14922 smash_to_methodptr_type (type, new_type);
c0dd20ea 14923}
1168df01 14924
2b4424c3
TT
14925/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14926 appropriate error checking and issuing complaints if there is a
14927 problem. */
14928
14929static ULONGEST
14930get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14931{
14932 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14933
14934 if (attr == nullptr)
14935 return 0;
14936
cd6c91b4 14937 if (!attr->form_is_constant ())
2b4424c3 14938 {
b98664d3 14939 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14940 " - DIE at %s [in module %s]"),
14941 sect_offset_str (die->sect_off),
14942 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14943 return 0;
14944 }
14945
14946 ULONGEST align;
14947 if (attr->form == DW_FORM_sdata)
14948 {
14949 LONGEST val = DW_SND (attr);
14950 if (val < 0)
14951 {
b98664d3 14952 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14953 " - DIE at %s [in module %s]"),
14954 sect_offset_str (die->sect_off),
14955 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14956 return 0;
14957 }
14958 align = val;
14959 }
14960 else
14961 align = DW_UNSND (attr);
14962
14963 if (align == 0)
14964 {
b98664d3 14965 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14966 " - DIE at %s [in module %s]"),
14967 sect_offset_str (die->sect_off),
14968 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14969 return 0;
14970 }
14971 if ((align & (align - 1)) != 0)
14972 {
b98664d3 14973 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14974 " - DIE at %s [in module %s]"),
14975 sect_offset_str (die->sect_off),
14976 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14977 return 0;
14978 }
14979
14980 return align;
14981}
14982
14983/* If the DIE has a DW_AT_alignment attribute, use its value to set
14984 the alignment for TYPE. */
14985
14986static void
14987maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14988 struct type *type)
14989{
14990 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14991 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14992 " - DIE at %s [in module %s]"),
14993 sect_offset_str (die->sect_off),
14994 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14995}
685b1105 14996
e35000a7
TBA
14997/* Check if the given VALUE is a valid enum dwarf_calling_convention
14998 constant for a type, according to DWARF5 spec, Table 5.5. */
14999
15000static bool
15001is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15002{
15003 switch (value)
15004 {
15005 case DW_CC_normal:
15006 case DW_CC_pass_by_reference:
15007 case DW_CC_pass_by_value:
15008 return true;
15009
15010 default:
15011 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15012 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15013 return false;
15014 }
15015}
15016
d0922fcf
TBA
15017/* Check if the given VALUE is a valid enum dwarf_calling_convention
15018 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15019 also according to GNU-specific values (see include/dwarf2.h). */
15020
15021static bool
15022is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15023{
15024 switch (value)
15025 {
15026 case DW_CC_normal:
15027 case DW_CC_program:
15028 case DW_CC_nocall:
15029 return true;
15030
15031 case DW_CC_GNU_renesas_sh:
15032 case DW_CC_GNU_borland_fastcall_i386:
15033 case DW_CC_GDB_IBM_OpenCL:
15034 return true;
15035
15036 default:
15037 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15038 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15039 return false;
15040 }
15041}
15042
c906108c 15043/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15044 (definition) to create a type for the structure or union. Fill in
15045 the type's name and general properties; the members will not be
83655187
DE
15046 processed until process_structure_scope. A symbol table entry for
15047 the type will also not be done until process_structure_scope (assuming
15048 the type has a name).
c906108c 15049
c767944b
DJ
15050 NOTE: we need to call these functions regardless of whether or not the
15051 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15052 structure or union. This gets the type entered into our set of
83655187 15053 user defined types. */
c906108c 15054
f792889a 15055static struct type *
134d01f1 15056read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15057{
518817b3 15058 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15059 struct type *type;
15060 struct attribute *attr;
15d034d0 15061 const char *name;
c906108c 15062
348e048f
DE
15063 /* If the definition of this type lives in .debug_types, read that type.
15064 Don't follow DW_AT_specification though, that will take us back up
15065 the chain and we want to go down. */
45e58e77 15066 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15067 if (attr != nullptr)
348e048f 15068 {
ac9ec31b 15069 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15070
ac9ec31b 15071 /* The type's CU may not be the same as CU.
02142a6c 15072 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15073 return set_die_type (die, type, cu);
15074 }
15075
c0dd20ea 15076 type = alloc_type (objfile);
c906108c 15077 INIT_CPLUS_SPECIFIC (type);
93311388 15078
39cbfefa
DJ
15079 name = dwarf2_name (die, cu);
15080 if (name != NULL)
c906108c 15081 {
987504bb 15082 if (cu->language == language_cplus
c44af4eb
TT
15083 || cu->language == language_d
15084 || cu->language == language_rust)
63d06c5c 15085 {
15d034d0 15086 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15087
15088 /* dwarf2_full_name might have already finished building the DIE's
15089 type. If so, there is no need to continue. */
15090 if (get_die_type (die, cu) != NULL)
15091 return get_die_type (die, cu);
15092
e86ca25f 15093 TYPE_NAME (type) = full_name;
63d06c5c
DC
15094 }
15095 else
15096 {
d8151005
DJ
15097 /* The name is already allocated along with this objfile, so
15098 we don't need to duplicate it for the type. */
e86ca25f 15099 TYPE_NAME (type) = name;
63d06c5c 15100 }
c906108c
SS
15101 }
15102
15103 if (die->tag == DW_TAG_structure_type)
15104 {
15105 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15106 }
15107 else if (die->tag == DW_TAG_union_type)
15108 {
15109 TYPE_CODE (type) = TYPE_CODE_UNION;
15110 }
2ddeaf8a
TT
15111 else if (die->tag == DW_TAG_variant_part)
15112 {
15113 TYPE_CODE (type) = TYPE_CODE_UNION;
15114 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15115 }
c906108c
SS
15116 else
15117 {
4753d33b 15118 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15119 }
15120
0cc2414c
TT
15121 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15122 TYPE_DECLARED_CLASS (type) = 1;
15123
e35000a7
TBA
15124 /* Store the calling convention in the type if it's available in
15125 the die. Otherwise the calling convention remains set to
15126 the default value DW_CC_normal. */
15127 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15128 if (attr != nullptr
15129 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15130 {
15131 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15132 TYPE_CPLUS_CALLING_CONVENTION (type)
15133 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15134 }
15135
e142c38c 15136 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15137 if (attr != nullptr)
c906108c 15138 {
cd6c91b4 15139 if (attr->form_is_constant ())
155bfbd3
JB
15140 TYPE_LENGTH (type) = DW_UNSND (attr);
15141 else
15142 {
15143 /* For the moment, dynamic type sizes are not supported
15144 by GDB's struct type. The actual size is determined
15145 on-demand when resolving the type of a given object,
15146 so set the type's length to zero for now. Otherwise,
15147 we record an expression as the length, and that expression
15148 could lead to a very large value, which could eventually
15149 lead to us trying to allocate that much memory when creating
15150 a value of that type. */
15151 TYPE_LENGTH (type) = 0;
15152 }
c906108c
SS
15153 }
15154 else
15155 {
15156 TYPE_LENGTH (type) = 0;
15157 }
15158
2b4424c3
TT
15159 maybe_set_alignment (cu, die, type);
15160
5230b05a 15161 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15162 {
5230b05a
WT
15163 /* ICC<14 does not output the required DW_AT_declaration on
15164 incomplete types, but gives them a size of zero. */
422b1cb0 15165 TYPE_STUB (type) = 1;
685b1105
JK
15166 }
15167 else
15168 TYPE_STUB_SUPPORTED (type) = 1;
15169
dc718098 15170 if (die_is_declaration (die, cu))
876cecd0 15171 TYPE_STUB (type) = 1;
a6c727b2
DJ
15172 else if (attr == NULL && die->child == NULL
15173 && producer_is_realview (cu->producer))
15174 /* RealView does not output the required DW_AT_declaration
15175 on incomplete types. */
15176 TYPE_STUB (type) = 1;
dc718098 15177
c906108c
SS
15178 /* We need to add the type field to the die immediately so we don't
15179 infinitely recurse when dealing with pointers to the structure
0963b4bd 15180 type within the structure itself. */
1c379e20 15181 set_die_type (die, type, cu);
c906108c 15182
7e314c57
JK
15183 /* set_die_type should be already done. */
15184 set_descriptive_type (type, die, cu);
15185
c767944b
DJ
15186 return type;
15187}
15188
2ddeaf8a
TT
15189/* A helper for process_structure_scope that handles a single member
15190 DIE. */
15191
15192static void
15193handle_struct_member_die (struct die_info *child_die, struct type *type,
15194 struct field_info *fi,
15195 std::vector<struct symbol *> *template_args,
15196 struct dwarf2_cu *cu)
15197{
15198 if (child_die->tag == DW_TAG_member
15199 || child_die->tag == DW_TAG_variable
15200 || child_die->tag == DW_TAG_variant_part)
15201 {
15202 /* NOTE: carlton/2002-11-05: A C++ static data member
15203 should be a DW_TAG_member that is a declaration, but
15204 all versions of G++ as of this writing (so through at
15205 least 3.2.1) incorrectly generate DW_TAG_variable
15206 tags for them instead. */
15207 dwarf2_add_field (fi, child_die, cu);
15208 }
15209 else if (child_die->tag == DW_TAG_subprogram)
15210 {
15211 /* Rust doesn't have member functions in the C++ sense.
15212 However, it does emit ordinary functions as children
15213 of a struct DIE. */
15214 if (cu->language == language_rust)
15215 read_func_scope (child_die, cu);
15216 else
15217 {
15218 /* C++ member function. */
15219 dwarf2_add_member_fn (fi, child_die, type, cu);
15220 }
15221 }
15222 else if (child_die->tag == DW_TAG_inheritance)
15223 {
15224 /* C++ base class field. */
15225 dwarf2_add_field (fi, child_die, cu);
15226 }
15227 else if (type_can_define_types (child_die))
15228 dwarf2_add_type_defn (fi, child_die, cu);
15229 else if (child_die->tag == DW_TAG_template_type_param
15230 || child_die->tag == DW_TAG_template_value_param)
15231 {
15232 struct symbol *arg = new_symbol (child_die, NULL, cu);
15233
15234 if (arg != NULL)
15235 template_args->push_back (arg);
15236 }
15237 else if (child_die->tag == DW_TAG_variant)
15238 {
15239 /* In a variant we want to get the discriminant and also add a
15240 field for our sole member child. */
15241 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15242
bde09ab7 15243 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15244 variant_child != NULL;
15245 variant_child = sibling_die (variant_child))
15246 {
15247 if (variant_child->tag == DW_TAG_member)
15248 {
15249 handle_struct_member_die (variant_child, type, fi,
15250 template_args, cu);
15251 /* Only handle the one. */
15252 break;
15253 }
15254 }
15255
15256 /* We don't handle this but we might as well report it if we see
15257 it. */
15258 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15259 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15260 " - DIE at %s [in module %s]"),
15261 sect_offset_str (child_die->sect_off),
15262 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15263
15264 /* The first field was just added, so we can stash the
15265 discriminant there. */
be2daae6 15266 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15267 if (discr == NULL)
be2daae6 15268 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15269 else
be2daae6 15270 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15271 }
15272}
15273
c767944b
DJ
15274/* Finish creating a structure or union type, including filling in
15275 its members and creating a symbol for it. */
15276
15277static void
15278process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15279{
518817b3 15280 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15281 struct die_info *child_die;
c767944b
DJ
15282 struct type *type;
15283
15284 type = get_die_type (die, cu);
15285 if (type == NULL)
15286 type = read_structure_type (die, cu);
15287
2ddeaf8a
TT
15288 /* When reading a DW_TAG_variant_part, we need to notice when we
15289 read the discriminant member, so we can record it later in the
15290 discriminant_info. */
15291 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15292 sect_offset discr_offset {};
3e1d3d8c 15293 bool has_template_parameters = false;
2ddeaf8a
TT
15294
15295 if (is_variant_part)
15296 {
15297 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15298 if (discr == NULL)
15299 {
15300 /* Maybe it's a univariant form, an extension we support.
15301 In this case arrange not to check the offset. */
15302 is_variant_part = false;
15303 }
cd6c91b4 15304 else if (discr->form_is_ref ())
2ddeaf8a
TT
15305 {
15306 struct dwarf2_cu *target_cu = cu;
15307 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15308
15309 discr_offset = target_die->sect_off;
15310 }
15311 else
15312 {
b98664d3 15313 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15314 " - DIE at %s [in module %s]"),
15315 sect_offset_str (die->sect_off),
15316 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15317 is_variant_part = false;
15318 }
15319 }
15320
e142c38c 15321 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15322 {
15323 struct field_info fi;
2f4732b0 15324 std::vector<struct symbol *> template_args;
c906108c 15325
639d11d3 15326 child_die = die->child;
c906108c
SS
15327
15328 while (child_die && child_die->tag)
15329 {
2ddeaf8a 15330 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15331
2ddeaf8a 15332 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15333 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15334
c906108c
SS
15335 child_die = sibling_die (child_die);
15336 }
15337
34eaf542 15338 /* Attach template arguments to type. */
2f4732b0 15339 if (!template_args.empty ())
34eaf542 15340 {
3e1d3d8c 15341 has_template_parameters = true;
34eaf542 15342 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15343 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15344 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15345 = XOBNEWVEC (&objfile->objfile_obstack,
15346 struct symbol *,
15347 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15348 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15349 template_args.data (),
34eaf542
TT
15350 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15351 * sizeof (struct symbol *)));
34eaf542
TT
15352 }
15353
c906108c 15354 /* Attach fields and member functions to the type. */
317f7127 15355 if (fi.nfields () > 0)
e7c27a73 15356 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15357 if (!fi.fnfieldlists.empty ())
c906108c 15358 {
e7c27a73 15359 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15360
c5aa993b 15361 /* Get the type which refers to the base class (possibly this
c906108c 15362 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15363 class from the DW_AT_containing_type attribute. This use of
15364 DW_AT_containing_type is a GNU extension. */
c906108c 15365
e142c38c 15366 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15367 {
e7c27a73 15368 struct type *t = die_containing_type (die, cu);
c906108c 15369
ae6ae975 15370 set_type_vptr_basetype (type, t);
c906108c
SS
15371 if (type == t)
15372 {
c906108c
SS
15373 int i;
15374
15375 /* Our own class provides vtbl ptr. */
15376 for (i = TYPE_NFIELDS (t) - 1;
15377 i >= TYPE_N_BASECLASSES (t);
15378 --i)
15379 {
0d5cff50 15380 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15381
1168df01 15382 if (is_vtable_name (fieldname, cu))
c906108c 15383 {
ae6ae975 15384 set_type_vptr_fieldno (type, i);
c906108c
SS
15385 break;
15386 }
15387 }
15388
15389 /* Complain if virtual function table field not found. */
15390 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15391 complaint (_("virtual function table pointer "
3e43a32a 15392 "not found when defining class '%s'"),
e86ca25f 15393 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15394 }
15395 else
15396 {
ae6ae975 15397 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15398 }
15399 }
f6235d4c 15400 else if (cu->producer
61012eef 15401 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15402 {
15403 /* The IBM XLC compiler does not provide direct indication
15404 of the containing type, but the vtable pointer is
15405 always named __vfp. */
15406
15407 int i;
15408
15409 for (i = TYPE_NFIELDS (type) - 1;
15410 i >= TYPE_N_BASECLASSES (type);
15411 --i)
15412 {
15413 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15414 {
ae6ae975
DE
15415 set_type_vptr_fieldno (type, i);
15416 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15417 break;
15418 }
15419 }
15420 }
c906108c 15421 }
98751a41
JK
15422
15423 /* Copy fi.typedef_field_list linked list elements content into the
15424 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15425 if (!fi.typedef_field_list.empty ())
98751a41 15426 {
be2daae6 15427 int count = fi.typedef_field_list.size ();
98751a41 15428
a0d7a4ff 15429 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15430 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15431 = ((struct decl_field *)
be2daae6
TT
15432 TYPE_ALLOC (type,
15433 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15434 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15435
be2daae6
TT
15436 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15437 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15438 }
c767944b 15439
883fd55a
KS
15440 /* Copy fi.nested_types_list linked list elements content into the
15441 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15442 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15443 {
be2daae6 15444 int count = fi.nested_types_list.size ();
883fd55a
KS
15445
15446 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15447 TYPE_NESTED_TYPES_ARRAY (type)
15448 = ((struct decl_field *)
be2daae6
TT
15449 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15450 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15451
be2daae6
TT
15452 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15453 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15454 }
c906108c 15455 }
63d06c5c 15456
bb5ed363 15457 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15458 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15459 cu->rust_unions.push_back (type);
0b92b5bb 15460
90aeadfc
DC
15461 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15462 snapshots) has been known to create a die giving a declaration
15463 for a class that has, as a child, a die giving a definition for a
15464 nested class. So we have to process our children even if the
15465 current die is a declaration. Normally, of course, a declaration
15466 won't have any children at all. */
134d01f1 15467
ca040673
DE
15468 child_die = die->child;
15469
90aeadfc
DC
15470 while (child_die != NULL && child_die->tag)
15471 {
15472 if (child_die->tag == DW_TAG_member
15473 || child_die->tag == DW_TAG_variable
34eaf542
TT
15474 || child_die->tag == DW_TAG_inheritance
15475 || child_die->tag == DW_TAG_template_value_param
15476 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15477 {
90aeadfc 15478 /* Do nothing. */
134d01f1 15479 }
90aeadfc
DC
15480 else
15481 process_die (child_die, cu);
134d01f1 15482
90aeadfc 15483 child_die = sibling_die (child_die);
134d01f1
DJ
15484 }
15485
fa4028e9
JB
15486 /* Do not consider external references. According to the DWARF standard,
15487 these DIEs are identified by the fact that they have no byte_size
15488 attribute, and a declaration attribute. */
15489 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15490 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15491 {
15492 struct symbol *sym = new_symbol (die, type, cu);
15493
15494 if (has_template_parameters)
15495 {
a776957c
TT
15496 struct symtab *symtab;
15497 if (sym != nullptr)
15498 symtab = symbol_symtab (sym);
15499 else if (cu->line_header != nullptr)
15500 {
15501 /* Any related symtab will do. */
15502 symtab
7ba99d21 15503 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15504 }
15505 else
15506 {
15507 symtab = nullptr;
15508 complaint (_("could not find suitable "
15509 "symtab for template parameter"
15510 " - DIE at %s [in module %s]"),
15511 sect_offset_str (die->sect_off),
15512 objfile_name (objfile));
15513 }
15514
15515 if (symtab != nullptr)
15516 {
15517 /* Make sure that the symtab is set on the new symbols.
15518 Even though they don't appear in this symtab directly,
15519 other parts of gdb assume that symbols do, and this is
15520 reasonably true. */
15521 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15522 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15523 }
3e1d3d8c
TT
15524 }
15525 }
134d01f1
DJ
15526}
15527
55426c9d
JB
15528/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15529 update TYPE using some information only available in DIE's children. */
15530
15531static void
15532update_enumeration_type_from_children (struct die_info *die,
15533 struct type *type,
15534 struct dwarf2_cu *cu)
15535{
60f7655a 15536 struct die_info *child_die;
55426c9d
JB
15537 int unsigned_enum = 1;
15538 int flag_enum = 1;
55426c9d 15539
8268c778 15540 auto_obstack obstack;
55426c9d 15541
60f7655a
DE
15542 for (child_die = die->child;
15543 child_die != NULL && child_die->tag;
15544 child_die = sibling_die (child_die))
55426c9d
JB
15545 {
15546 struct attribute *attr;
15547 LONGEST value;
15548 const gdb_byte *bytes;
15549 struct dwarf2_locexpr_baton *baton;
15550 const char *name;
60f7655a 15551
55426c9d
JB
15552 if (child_die->tag != DW_TAG_enumerator)
15553 continue;
15554
15555 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15556 if (attr == NULL)
15557 continue;
15558
15559 name = dwarf2_name (child_die, cu);
15560 if (name == NULL)
15561 name = "<anonymous enumerator>";
15562
15563 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15564 &value, &bytes, &baton);
15565 if (value < 0)
15566 {
15567 unsigned_enum = 0;
15568 flag_enum = 0;
15569 }
55426c9d 15570 else
edd45eb0
SM
15571 {
15572 if (count_one_bits_ll (value) >= 2)
15573 flag_enum = 0;
edd45eb0 15574 }
55426c9d
JB
15575
15576 /* If we already know that the enum type is neither unsigned, nor
15577 a flag type, no need to look at the rest of the enumerates. */
15578 if (!unsigned_enum && !flag_enum)
15579 break;
55426c9d
JB
15580 }
15581
15582 if (unsigned_enum)
15583 TYPE_UNSIGNED (type) = 1;
15584 if (flag_enum)
15585 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15586}
15587
134d01f1
DJ
15588/* Given a DW_AT_enumeration_type die, set its type. We do not
15589 complete the type's fields yet, or create any symbols. */
c906108c 15590
f792889a 15591static struct type *
134d01f1 15592read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15593{
518817b3 15594 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15595 struct type *type;
c906108c 15596 struct attribute *attr;
0114d602 15597 const char *name;
134d01f1 15598
348e048f
DE
15599 /* If the definition of this type lives in .debug_types, read that type.
15600 Don't follow DW_AT_specification though, that will take us back up
15601 the chain and we want to go down. */
45e58e77 15602 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15603 if (attr != nullptr)
348e048f 15604 {
ac9ec31b 15605 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15606
ac9ec31b 15607 /* The type's CU may not be the same as CU.
02142a6c 15608 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15609 return set_die_type (die, type, cu);
15610 }
15611
c906108c
SS
15612 type = alloc_type (objfile);
15613
15614 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15615 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15616 if (name != NULL)
e86ca25f 15617 TYPE_NAME (type) = name;
c906108c 15618
0626fc76
TT
15619 attr = dwarf2_attr (die, DW_AT_type, cu);
15620 if (attr != NULL)
15621 {
15622 struct type *underlying_type = die_type (die, cu);
15623
15624 TYPE_TARGET_TYPE (type) = underlying_type;
15625 }
15626
e142c38c 15627 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15628 if (attr != nullptr)
c906108c
SS
15629 {
15630 TYPE_LENGTH (type) = DW_UNSND (attr);
15631 }
15632 else
15633 {
15634 TYPE_LENGTH (type) = 0;
15635 }
15636
2b4424c3
TT
15637 maybe_set_alignment (cu, die, type);
15638
137033e9
JB
15639 /* The enumeration DIE can be incomplete. In Ada, any type can be
15640 declared as private in the package spec, and then defined only
15641 inside the package body. Such types are known as Taft Amendment
15642 Types. When another package uses such a type, an incomplete DIE
15643 may be generated by the compiler. */
02eb380e 15644 if (die_is_declaration (die, cu))
876cecd0 15645 TYPE_STUB (type) = 1;
02eb380e 15646
0626fc76
TT
15647 /* Finish the creation of this type by using the enum's children.
15648 We must call this even when the underlying type has been provided
15649 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15650 update_enumeration_type_from_children (die, type, cu);
15651
0626fc76
TT
15652 /* If this type has an underlying type that is not a stub, then we
15653 may use its attributes. We always use the "unsigned" attribute
15654 in this situation, because ordinarily we guess whether the type
15655 is unsigned -- but the guess can be wrong and the underlying type
15656 can tell us the reality. However, we defer to a local size
15657 attribute if one exists, because this lets the compiler override
15658 the underlying type if needed. */
15659 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15660 {
15661 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15662 if (TYPE_LENGTH (type) == 0)
15663 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15664 if (TYPE_RAW_ALIGN (type) == 0
15665 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15666 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15667 }
15668
3d567982
TT
15669 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15670
f792889a 15671 return set_die_type (die, type, cu);
134d01f1
DJ
15672}
15673
15674/* Given a pointer to a die which begins an enumeration, process all
15675 the dies that define the members of the enumeration, and create the
15676 symbol for the enumeration type.
15677
15678 NOTE: We reverse the order of the element list. */
15679
15680static void
15681process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15682{
f792889a 15683 struct type *this_type;
134d01f1 15684
f792889a
DJ
15685 this_type = get_die_type (die, cu);
15686 if (this_type == NULL)
15687 this_type = read_enumeration_type (die, cu);
9dc481d3 15688
639d11d3 15689 if (die->child != NULL)
c906108c 15690 {
9dc481d3
DE
15691 struct die_info *child_die;
15692 struct symbol *sym;
43816ebc 15693 std::vector<struct field> fields;
15d034d0 15694 const char *name;
9dc481d3 15695
639d11d3 15696 child_die = die->child;
c906108c
SS
15697 while (child_die && child_die->tag)
15698 {
15699 if (child_die->tag != DW_TAG_enumerator)
15700 {
e7c27a73 15701 process_die (child_die, cu);
c906108c
SS
15702 }
15703 else
15704 {
39cbfefa
DJ
15705 name = dwarf2_name (child_die, cu);
15706 if (name)
c906108c 15707 {
f792889a 15708 sym = new_symbol (child_die, this_type, cu);
c906108c 15709
43816ebc
TT
15710 fields.emplace_back ();
15711 struct field &field = fields.back ();
c906108c 15712
43816ebc
TT
15713 FIELD_NAME (field) = sym->linkage_name ();
15714 FIELD_TYPE (field) = NULL;
15715 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15716 FIELD_BITSIZE (field) = 0;
c906108c
SS
15717 }
15718 }
15719
15720 child_die = sibling_die (child_die);
15721 }
15722
43816ebc 15723 if (!fields.empty ())
c906108c 15724 {
43816ebc 15725 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15726 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15727 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15728 memcpy (TYPE_FIELDS (this_type), fields.data (),
15729 sizeof (struct field) * fields.size ());
c906108c 15730 }
c906108c 15731 }
134d01f1 15732
6c83ed52
TT
15733 /* If we are reading an enum from a .debug_types unit, and the enum
15734 is a declaration, and the enum is not the signatured type in the
15735 unit, then we do not want to add a symbol for it. Adding a
15736 symbol would in some cases obscure the true definition of the
15737 enum, giving users an incomplete type when the definition is
15738 actually available. Note that we do not want to do this for all
15739 enums which are just declarations, because C++0x allows forward
15740 enum declarations. */
3019eac3 15741 if (cu->per_cu->is_debug_types
6c83ed52
TT
15742 && die_is_declaration (die, cu))
15743 {
52dc124a 15744 struct signatured_type *sig_type;
6c83ed52 15745
c0f78cd4 15746 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15747 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15748 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15749 return;
15750 }
15751
f792889a 15752 new_symbol (die, this_type, cu);
c906108c
SS
15753}
15754
15755/* Extract all information from a DW_TAG_array_type DIE and put it in
15756 the DIE's type field. For now, this only handles one dimensional
15757 arrays. */
15758
f792889a 15759static struct type *
e7c27a73 15760read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15761{
518817b3 15762 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15763 struct die_info *child_die;
7e314c57 15764 struct type *type;
c906108c 15765 struct type *element_type, *range_type, *index_type;
c906108c 15766 struct attribute *attr;
15d034d0 15767 const char *name;
a405673c 15768 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15769 unsigned int bit_stride = 0;
c906108c 15770
e7c27a73 15771 element_type = die_type (die, cu);
c906108c 15772
7e314c57
JK
15773 /* The die_type call above may have already set the type for this DIE. */
15774 type = get_die_type (die, cu);
15775 if (type)
15776 return type;
15777
dc53a7ad
JB
15778 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15779 if (attr != NULL)
a405673c
JB
15780 {
15781 int stride_ok;
09ba997f 15782 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15783
15784 byte_stride_prop
15785 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15786 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15787 prop_type);
a405673c
JB
15788 if (!stride_ok)
15789 {
b98664d3 15790 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15791 " - DIE at %s [in module %s]"),
15792 sect_offset_str (die->sect_off),
518817b3 15793 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15794 /* Ignore this attribute. We will likely not be able to print
15795 arrays of this type correctly, but there is little we can do
15796 to help if we cannot read the attribute's value. */
15797 byte_stride_prop = NULL;
15798 }
15799 }
dc53a7ad
JB
15800
15801 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15802 if (attr != NULL)
15803 bit_stride = DW_UNSND (attr);
15804
c906108c
SS
15805 /* Irix 6.2 native cc creates array types without children for
15806 arrays with unspecified length. */
639d11d3 15807 if (die->child == NULL)
c906108c 15808 {
46bf5051 15809 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15810 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15811 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15812 byte_stride_prop, bit_stride);
f792889a 15813 return set_die_type (die, type, cu);
c906108c
SS
15814 }
15815
791afaa2 15816 std::vector<struct type *> range_types;
639d11d3 15817 child_die = die->child;
c906108c
SS
15818 while (child_die && child_die->tag)
15819 {
15820 if (child_die->tag == DW_TAG_subrange_type)
15821 {
f792889a 15822 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15823
f792889a 15824 if (child_type != NULL)
a02abb62 15825 {
0963b4bd
MS
15826 /* The range type was succesfully read. Save it for the
15827 array type creation. */
791afaa2 15828 range_types.push_back (child_type);
a02abb62 15829 }
c906108c
SS
15830 }
15831 child_die = sibling_die (child_die);
15832 }
15833
15834 /* Dwarf2 dimensions are output from left to right, create the
15835 necessary array types in backwards order. */
7ca2d3a3 15836
c906108c 15837 type = element_type;
7ca2d3a3
DL
15838
15839 if (read_array_order (die, cu) == DW_ORD_col_major)
15840 {
15841 int i = 0;
9a619af0 15842
791afaa2 15843 while (i < range_types.size ())
dc53a7ad 15844 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15845 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15846 }
15847 else
15848 {
791afaa2 15849 size_t ndim = range_types.size ();
7ca2d3a3 15850 while (ndim-- > 0)
dc53a7ad 15851 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15852 byte_stride_prop, bit_stride);
7ca2d3a3 15853 }
c906108c 15854
f5f8a009
EZ
15855 /* Understand Dwarf2 support for vector types (like they occur on
15856 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15857 array type. This is not part of the Dwarf2/3 standard yet, but a
15858 custom vendor extension. The main difference between a regular
15859 array and the vector variant is that vectors are passed by value
15860 to functions. */
e142c38c 15861 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15862 if (attr != nullptr)
ea37ba09 15863 make_vector_type (type);
f5f8a009 15864
dbc98a8b
KW
15865 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15866 implementation may choose to implement triple vectors using this
15867 attribute. */
15868 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15869 if (attr != nullptr)
dbc98a8b
KW
15870 {
15871 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15872 TYPE_LENGTH (type) = DW_UNSND (attr);
15873 else
b98664d3 15874 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15875 "than the total size of elements"));
dbc98a8b
KW
15876 }
15877
39cbfefa
DJ
15878 name = dwarf2_name (die, cu);
15879 if (name)
15880 TYPE_NAME (type) = name;
6e70227d 15881
2b4424c3
TT
15882 maybe_set_alignment (cu, die, type);
15883
0963b4bd 15884 /* Install the type in the die. */
7e314c57
JK
15885 set_die_type (die, type, cu);
15886
15887 /* set_die_type should be already done. */
b4ba55a1
JB
15888 set_descriptive_type (type, die, cu);
15889
7e314c57 15890 return type;
c906108c
SS
15891}
15892
7ca2d3a3 15893static enum dwarf_array_dim_ordering
6e70227d 15894read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15895{
15896 struct attribute *attr;
15897
15898 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15899
435d3d88 15900 if (attr != nullptr)
aead7601 15901 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15902
0963b4bd
MS
15903 /* GNU F77 is a special case, as at 08/2004 array type info is the
15904 opposite order to the dwarf2 specification, but data is still
15905 laid out as per normal fortran.
7ca2d3a3 15906
0963b4bd
MS
15907 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15908 version checking. */
7ca2d3a3 15909
905e0470
PM
15910 if (cu->language == language_fortran
15911 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15912 {
15913 return DW_ORD_row_major;
15914 }
15915
6e70227d 15916 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15917 {
15918 case array_column_major:
15919 return DW_ORD_col_major;
15920 case array_row_major:
15921 default:
15922 return DW_ORD_row_major;
15923 };
15924}
15925
72019c9c 15926/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15927 the DIE's type field. */
72019c9c 15928
f792889a 15929static struct type *
72019c9c
GM
15930read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15931{
7e314c57
JK
15932 struct type *domain_type, *set_type;
15933 struct attribute *attr;
f792889a 15934
7e314c57
JK
15935 domain_type = die_type (die, cu);
15936
15937 /* The die_type call above may have already set the type for this DIE. */
15938 set_type = get_die_type (die, cu);
15939 if (set_type)
15940 return set_type;
15941
15942 set_type = create_set_type (NULL, domain_type);
15943
15944 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15945 if (attr != nullptr)
d09039dd 15946 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15947
2b4424c3
TT
15948 maybe_set_alignment (cu, die, set_type);
15949
f792889a 15950 return set_die_type (die, set_type, cu);
72019c9c 15951}
7ca2d3a3 15952
0971de02
TT
15953/* A helper for read_common_block that creates a locexpr baton.
15954 SYM is the symbol which we are marking as computed.
15955 COMMON_DIE is the DIE for the common block.
15956 COMMON_LOC is the location expression attribute for the common
15957 block itself.
15958 MEMBER_LOC is the location expression attribute for the particular
15959 member of the common block that we are processing.
15960 CU is the CU from which the above come. */
15961
15962static void
15963mark_common_block_symbol_computed (struct symbol *sym,
15964 struct die_info *common_die,
15965 struct attribute *common_loc,
15966 struct attribute *member_loc,
15967 struct dwarf2_cu *cu)
15968{
518817b3
SM
15969 struct dwarf2_per_objfile *dwarf2_per_objfile
15970 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15971 struct objfile *objfile = dwarf2_per_objfile->objfile;
15972 struct dwarf2_locexpr_baton *baton;
15973 gdb_byte *ptr;
15974 unsigned int cu_off;
15975 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15976 LONGEST offset = 0;
15977
15978 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15979 gdb_assert (common_loc->form_is_block ());
15980 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15981 || member_loc->form_is_constant ());
0971de02 15982
8d749320 15983 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15984 baton->per_cu = cu->per_cu;
15985 gdb_assert (baton->per_cu);
15986
15987 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15988
cd6c91b4 15989 if (member_loc->form_is_constant ())
0971de02
TT
15990 {
15991 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15992 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15993 }
15994 else
15995 baton->size += DW_BLOCK (member_loc)->size;
15996
224c3ddb 15997 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15998 baton->data = ptr;
15999
16000 *ptr++ = DW_OP_call4;
9c541725 16001 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16002 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16003 ptr += 4;
16004
cd6c91b4 16005 if (member_loc->form_is_constant ())
0971de02
TT
16006 {
16007 *ptr++ = DW_OP_addr;
16008 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16009 ptr += cu->header.addr_size;
16010 }
16011 else
16012 {
16013 /* We have to copy the data here, because DW_OP_call4 will only
16014 use a DW_AT_location attribute. */
16015 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16016 ptr += DW_BLOCK (member_loc)->size;
16017 }
16018
16019 *ptr++ = DW_OP_plus;
16020 gdb_assert (ptr - baton->data == baton->size);
16021
0971de02 16022 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16023 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16024}
16025
4357ac6c
TT
16026/* Create appropriate locally-scoped variables for all the
16027 DW_TAG_common_block entries. Also create a struct common_block
16028 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16029 is used to separate the common blocks name namespace from regular
4357ac6c 16030 variable names. */
c906108c
SS
16031
16032static void
e7c27a73 16033read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16034{
0971de02
TT
16035 struct attribute *attr;
16036
16037 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16038 if (attr != nullptr)
0971de02
TT
16039 {
16040 /* Support the .debug_loc offsets. */
4fc6c0d5 16041 if (attr->form_is_block ())
0971de02
TT
16042 {
16043 /* Ok. */
16044 }
cd6c91b4 16045 else if (attr->form_is_section_offset ())
0971de02
TT
16046 {
16047 dwarf2_complex_location_expr_complaint ();
16048 attr = NULL;
16049 }
16050 else
16051 {
16052 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16053 "common block member");
16054 attr = NULL;
16055 }
16056 }
16057
639d11d3 16058 if (die->child != NULL)
c906108c 16059 {
518817b3 16060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16061 struct die_info *child_die;
16062 size_t n_entries = 0, size;
16063 struct common_block *common_block;
16064 struct symbol *sym;
74ac6d43 16065
4357ac6c
TT
16066 for (child_die = die->child;
16067 child_die && child_die->tag;
16068 child_die = sibling_die (child_die))
16069 ++n_entries;
16070
16071 size = (sizeof (struct common_block)
16072 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16073 common_block
16074 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16075 size);
4357ac6c
TT
16076 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16077 common_block->n_entries = 0;
16078
16079 for (child_die = die->child;
16080 child_die && child_die->tag;
16081 child_die = sibling_die (child_die))
16082 {
16083 /* Create the symbol in the DW_TAG_common_block block in the current
16084 symbol scope. */
e7c27a73 16085 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16086 if (sym != NULL)
16087 {
16088 struct attribute *member_loc;
16089
16090 common_block->contents[common_block->n_entries++] = sym;
16091
16092 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16093 cu);
16094 if (member_loc)
16095 {
16096 /* GDB has handled this for a long time, but it is
16097 not specified by DWARF. It seems to have been
16098 emitted by gfortran at least as recently as:
16099 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16100 complaint (_("Variable in common block has "
0971de02 16101 "DW_AT_data_member_location "
9d8780f0
SM
16102 "- DIE at %s [in module %s]"),
16103 sect_offset_str (child_die->sect_off),
518817b3 16104 objfile_name (objfile));
0971de02 16105
cd6c91b4 16106 if (member_loc->form_is_section_offset ())
0971de02 16107 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16108 else if (member_loc->form_is_constant ()
4fc6c0d5 16109 || member_loc->form_is_block ())
0971de02 16110 {
435d3d88 16111 if (attr != nullptr)
0971de02
TT
16112 mark_common_block_symbol_computed (sym, die, attr,
16113 member_loc, cu);
16114 }
16115 else
16116 dwarf2_complex_location_expr_complaint ();
16117 }
16118 }
c906108c 16119 }
4357ac6c
TT
16120
16121 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16122 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16123 }
16124}
16125
0114d602 16126/* Create a type for a C++ namespace. */
d9fa45fe 16127
0114d602
DJ
16128static struct type *
16129read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16130{
518817b3 16131 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16132 const char *previous_prefix, *name;
9219021c 16133 int is_anonymous;
0114d602
DJ
16134 struct type *type;
16135
16136 /* For extensions, reuse the type of the original namespace. */
16137 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16138 {
16139 struct die_info *ext_die;
16140 struct dwarf2_cu *ext_cu = cu;
9a619af0 16141
0114d602
DJ
16142 ext_die = dwarf2_extension (die, &ext_cu);
16143 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16144
16145 /* EXT_CU may not be the same as CU.
02142a6c 16146 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16147 return set_die_type (die, type, cu);
16148 }
9219021c 16149
e142c38c 16150 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16151
16152 /* Now build the name of the current namespace. */
16153
0114d602
DJ
16154 previous_prefix = determine_prefix (die, cu);
16155 if (previous_prefix[0] != '\0')
16156 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16157 previous_prefix, name, 0, cu);
0114d602
DJ
16158
16159 /* Create the type. */
19f392bc 16160 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16161
60531b24 16162 return set_die_type (die, type, cu);
0114d602
DJ
16163}
16164
22cee43f 16165/* Read a namespace scope. */
0114d602
DJ
16166
16167static void
16168read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16169{
518817b3 16170 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16171 int is_anonymous;
9219021c 16172
5c4e30ca
DC
16173 /* Add a symbol associated to this if we haven't seen the namespace
16174 before. Also, add a using directive if it's an anonymous
16175 namespace. */
9219021c 16176
f2f0e013 16177 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16178 {
16179 struct type *type;
16180
0114d602 16181 type = read_type_die (die, cu);
e7c27a73 16182 new_symbol (die, type, cu);
5c4e30ca 16183
e8e80198 16184 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16185 if (is_anonymous)
0114d602
DJ
16186 {
16187 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16188
eb1e02fd 16189 std::vector<const char *> excludes;
804d2729 16190 add_using_directive (using_directives (cu),
22cee43f 16191 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16192 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16193 }
5c4e30ca 16194 }
9219021c 16195
639d11d3 16196 if (die->child != NULL)
d9fa45fe 16197 {
639d11d3 16198 struct die_info *child_die = die->child;
6e70227d 16199
d9fa45fe
DC
16200 while (child_die && child_die->tag)
16201 {
e7c27a73 16202 process_die (child_die, cu);
d9fa45fe
DC
16203 child_die = sibling_die (child_die);
16204 }
16205 }
38d518c9
EZ
16206}
16207
f55ee35c
JK
16208/* Read a Fortran module as type. This DIE can be only a declaration used for
16209 imported module. Still we need that type as local Fortran "use ... only"
16210 declaration imports depend on the created type in determine_prefix. */
16211
16212static struct type *
16213read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16214{
518817b3 16215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16216 const char *module_name;
f55ee35c
JK
16217 struct type *type;
16218
16219 module_name = dwarf2_name (die, cu);
19f392bc 16220 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16221
f55ee35c
JK
16222 return set_die_type (die, type, cu);
16223}
16224
5d7cb8df
JK
16225/* Read a Fortran module. */
16226
16227static void
16228read_module (struct die_info *die, struct dwarf2_cu *cu)
16229{
16230 struct die_info *child_die = die->child;
530e8392
KB
16231 struct type *type;
16232
16233 type = read_type_die (die, cu);
16234 new_symbol (die, type, cu);
5d7cb8df 16235
5d7cb8df
JK
16236 while (child_die && child_die->tag)
16237 {
16238 process_die (child_die, cu);
16239 child_die = sibling_die (child_die);
16240 }
16241}
16242
38d518c9
EZ
16243/* Return the name of the namespace represented by DIE. Set
16244 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16245 namespace. */
16246
16247static const char *
e142c38c 16248namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16249{
16250 struct die_info *current_die;
16251 const char *name = NULL;
16252
16253 /* Loop through the extensions until we find a name. */
16254
16255 for (current_die = die;
16256 current_die != NULL;
f2f0e013 16257 current_die = dwarf2_extension (die, &cu))
38d518c9 16258 {
96553a0c
DE
16259 /* We don't use dwarf2_name here so that we can detect the absence
16260 of a name -> anonymous namespace. */
7d45c7c3 16261 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16262
38d518c9
EZ
16263 if (name != NULL)
16264 break;
16265 }
16266
16267 /* Is it an anonymous namespace? */
16268
16269 *is_anonymous = (name == NULL);
16270 if (*is_anonymous)
2b1dbab0 16271 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16272
16273 return name;
d9fa45fe
DC
16274}
16275
c906108c
SS
16276/* Extract all information from a DW_TAG_pointer_type DIE and add to
16277 the user defined type vector. */
16278
f792889a 16279static struct type *
e7c27a73 16280read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16281{
518817b3
SM
16282 struct gdbarch *gdbarch
16283 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16284 struct comp_unit_head *cu_header = &cu->header;
c906108c 16285 struct type *type;
8b2dbe47
KB
16286 struct attribute *attr_byte_size;
16287 struct attribute *attr_address_class;
16288 int byte_size, addr_class;
7e314c57
JK
16289 struct type *target_type;
16290
16291 target_type = die_type (die, cu);
c906108c 16292
7e314c57
JK
16293 /* The die_type call above may have already set the type for this DIE. */
16294 type = get_die_type (die, cu);
16295 if (type)
16296 return type;
16297
16298 type = lookup_pointer_type (target_type);
8b2dbe47 16299
e142c38c 16300 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16301 if (attr_byte_size)
16302 byte_size = DW_UNSND (attr_byte_size);
c906108c 16303 else
8b2dbe47
KB
16304 byte_size = cu_header->addr_size;
16305
e142c38c 16306 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16307 if (attr_address_class)
16308 addr_class = DW_UNSND (attr_address_class);
16309 else
16310 addr_class = DW_ADDR_none;
16311
2b4424c3
TT
16312 ULONGEST alignment = get_alignment (cu, die);
16313
16314 /* If the pointer size, alignment, or address class is different
16315 than the default, create a type variant marked as such and set
16316 the length accordingly. */
16317 if (TYPE_LENGTH (type) != byte_size
16318 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16319 && alignment != TYPE_RAW_ALIGN (type))
16320 || addr_class != DW_ADDR_none)
c906108c 16321 {
5e2b427d 16322 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16323 {
16324 int type_flags;
16325
849957d9 16326 type_flags = gdbarch_address_class_type_flags
5e2b427d 16327 (gdbarch, byte_size, addr_class);
876cecd0
TT
16328 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16329 == 0);
8b2dbe47
KB
16330 type = make_type_with_address_space (type, type_flags);
16331 }
16332 else if (TYPE_LENGTH (type) != byte_size)
16333 {
b98664d3 16334 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16335 }
2b4424c3
TT
16336 else if (TYPE_RAW_ALIGN (type) != alignment)
16337 {
b98664d3 16338 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16339 " - DIE at %s [in module %s]"),
16340 sect_offset_str (die->sect_off),
16341 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16342 }
6e70227d 16343 else
9a619af0
MS
16344 {
16345 /* Should we also complain about unhandled address classes? */
16346 }
c906108c 16347 }
8b2dbe47
KB
16348
16349 TYPE_LENGTH (type) = byte_size;
2b4424c3 16350 set_type_align (type, alignment);
f792889a 16351 return set_die_type (die, type, cu);
c906108c
SS
16352}
16353
16354/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16355 the user defined type vector. */
16356
f792889a 16357static struct type *
e7c27a73 16358read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16359{
16360 struct type *type;
16361 struct type *to_type;
16362 struct type *domain;
16363
e7c27a73
DJ
16364 to_type = die_type (die, cu);
16365 domain = die_containing_type (die, cu);
0d5de010 16366
7e314c57
JK
16367 /* The calls above may have already set the type for this DIE. */
16368 type = get_die_type (die, cu);
16369 if (type)
16370 return type;
16371
0d5de010
DJ
16372 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16373 type = lookup_methodptr_type (to_type);
7078baeb
TT
16374 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16375 {
518817b3
SM
16376 struct type *new_type
16377 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16378
16379 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16380 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16381 TYPE_VARARGS (to_type));
16382 type = lookup_methodptr_type (new_type);
16383 }
0d5de010
DJ
16384 else
16385 type = lookup_memberptr_type (to_type, domain);
c906108c 16386
f792889a 16387 return set_die_type (die, type, cu);
c906108c
SS
16388}
16389
4297a3f0 16390/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16391 the user defined type vector. */
16392
f792889a 16393static struct type *
4297a3f0
AV
16394read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16395 enum type_code refcode)
c906108c 16396{
e7c27a73 16397 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16398 struct type *type, *target_type;
c906108c
SS
16399 struct attribute *attr;
16400
4297a3f0
AV
16401 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16402
7e314c57
JK
16403 target_type = die_type (die, cu);
16404
16405 /* The die_type call above may have already set the type for this DIE. */
16406 type = get_die_type (die, cu);
16407 if (type)
16408 return type;
16409
4297a3f0 16410 type = lookup_reference_type (target_type, refcode);
e142c38c 16411 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16412 if (attr != nullptr)
c906108c
SS
16413 {
16414 TYPE_LENGTH (type) = DW_UNSND (attr);
16415 }
16416 else
16417 {
107d2387 16418 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16419 }
2b4424c3 16420 maybe_set_alignment (cu, die, type);
f792889a 16421 return set_die_type (die, type, cu);
c906108c
SS
16422}
16423
cf363f18
MW
16424/* Add the given cv-qualifiers to the element type of the array. GCC
16425 outputs DWARF type qualifiers that apply to an array, not the
16426 element type. But GDB relies on the array element type to carry
16427 the cv-qualifiers. This mimics section 6.7.3 of the C99
16428 specification. */
16429
16430static struct type *
16431add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16432 struct type *base_type, int cnst, int voltl)
16433{
16434 struct type *el_type, *inner_array;
16435
16436 base_type = copy_type (base_type);
16437 inner_array = base_type;
16438
16439 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16440 {
16441 TYPE_TARGET_TYPE (inner_array) =
16442 copy_type (TYPE_TARGET_TYPE (inner_array));
16443 inner_array = TYPE_TARGET_TYPE (inner_array);
16444 }
16445
16446 el_type = TYPE_TARGET_TYPE (inner_array);
16447 cnst |= TYPE_CONST (el_type);
16448 voltl |= TYPE_VOLATILE (el_type);
16449 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16450
16451 return set_die_type (die, base_type, cu);
16452}
16453
f792889a 16454static struct type *
e7c27a73 16455read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16456{
f792889a 16457 struct type *base_type, *cv_type;
c906108c 16458
e7c27a73 16459 base_type = die_type (die, cu);
7e314c57
JK
16460
16461 /* The die_type call above may have already set the type for this DIE. */
16462 cv_type = get_die_type (die, cu);
16463 if (cv_type)
16464 return cv_type;
16465
2f608a3a
KW
16466 /* In case the const qualifier is applied to an array type, the element type
16467 is so qualified, not the array type (section 6.7.3 of C99). */
16468 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16469 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16470
f792889a
DJ
16471 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16472 return set_die_type (die, cv_type, cu);
c906108c
SS
16473}
16474
f792889a 16475static struct type *
e7c27a73 16476read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16477{
f792889a 16478 struct type *base_type, *cv_type;
c906108c 16479
e7c27a73 16480 base_type = die_type (die, cu);
7e314c57
JK
16481
16482 /* The die_type call above may have already set the type for this DIE. */
16483 cv_type = get_die_type (die, cu);
16484 if (cv_type)
16485 return cv_type;
16486
cf363f18
MW
16487 /* In case the volatile qualifier is applied to an array type, the
16488 element type is so qualified, not the array type (section 6.7.3
16489 of C99). */
16490 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16491 return add_array_cv_type (die, cu, base_type, 0, 1);
16492
f792889a
DJ
16493 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16494 return set_die_type (die, cv_type, cu);
c906108c
SS
16495}
16496
06d66ee9
TT
16497/* Handle DW_TAG_restrict_type. */
16498
16499static struct type *
16500read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16501{
16502 struct type *base_type, *cv_type;
16503
16504 base_type = die_type (die, cu);
16505
16506 /* The die_type call above may have already set the type for this DIE. */
16507 cv_type = get_die_type (die, cu);
16508 if (cv_type)
16509 return cv_type;
16510
16511 cv_type = make_restrict_type (base_type);
16512 return set_die_type (die, cv_type, cu);
16513}
16514
a2c2acaf
MW
16515/* Handle DW_TAG_atomic_type. */
16516
16517static struct type *
16518read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16519{
16520 struct type *base_type, *cv_type;
16521
16522 base_type = die_type (die, cu);
16523
16524 /* The die_type call above may have already set the type for this DIE. */
16525 cv_type = get_die_type (die, cu);
16526 if (cv_type)
16527 return cv_type;
16528
16529 cv_type = make_atomic_type (base_type);
16530 return set_die_type (die, cv_type, cu);
16531}
16532
c906108c
SS
16533/* Extract all information from a DW_TAG_string_type DIE and add to
16534 the user defined type vector. It isn't really a user defined type,
16535 but it behaves like one, with other DIE's using an AT_user_def_type
16536 attribute to reference it. */
16537
f792889a 16538static struct type *
e7c27a73 16539read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16540{
518817b3 16541 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16542 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16543 struct type *type, *range_type, *index_type, *char_type;
16544 struct attribute *attr;
216a7e6b
AB
16545 struct dynamic_prop prop;
16546 bool length_is_constant = true;
16547 LONGEST length;
16548
16549 /* There are a couple of places where bit sizes might be made use of
16550 when parsing a DW_TAG_string_type, however, no producer that we know
16551 of make use of these. Handling bit sizes that are a multiple of the
16552 byte size is easy enough, but what about other bit sizes? Lets deal
16553 with that problem when we have to. Warn about these attributes being
16554 unsupported, then parse the type and ignore them like we always
16555 have. */
16556 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16557 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16558 {
16559 static bool warning_printed = false;
16560 if (!warning_printed)
16561 {
16562 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16563 "currently supported on DW_TAG_string_type."));
16564 warning_printed = true;
16565 }
16566 }
c906108c 16567
e142c38c 16568 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16569 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16570 {
16571 /* The string length describes the location at which the length of
16572 the string can be found. The size of the length field can be
16573 specified with one of the attributes below. */
16574 struct type *prop_type;
16575 struct attribute *len
16576 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16577 if (len == nullptr)
16578 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16579 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16580 {
16581 /* Pass 0 as the default as we know this attribute is constant
16582 and the default value will not be returned. */
16583 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
09ba997f 16584 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16585 }
16586 else
16587 {
16588 /* If the size is not specified then we assume it is the size of
16589 an address on this target. */
09ba997f 16590 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16591 }
16592
16593 /* Convert the attribute into a dynamic property. */
16594 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16595 length = 1;
16596 else
16597 length_is_constant = false;
16598 }
16599 else if (attr != nullptr)
16600 {
16601 /* This DW_AT_string_length just contains the length with no
16602 indirection. There's no need to create a dynamic property in this
16603 case. Pass 0 for the default value as we know it will not be
16604 returned in this case. */
16605 length = dwarf2_get_attr_constant_value (attr, 0);
16606 }
16607 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16608 {
216a7e6b
AB
16609 /* We don't currently support non-constant byte sizes for strings. */
16610 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
16611 }
16612 else
16613 {
216a7e6b
AB
16614 /* Use 1 as a fallback length if we have nothing else. */
16615 length = 1;
c906108c 16616 }
6ccb9162 16617
46bf5051 16618 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16619 if (length_is_constant)
16620 range_type = create_static_range_type (NULL, index_type, 1, length);
16621 else
16622 {
16623 struct dynamic_prop low_bound;
16624
16625 low_bound.kind = PROP_CONST;
16626 low_bound.data.const_val = 1;
16627 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16628 }
3b7538c0
UW
16629 char_type = language_string_char_type (cu->language_defn, gdbarch);
16630 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16631
f792889a 16632 return set_die_type (die, type, cu);
c906108c
SS
16633}
16634
4d804846
JB
16635/* Assuming that DIE corresponds to a function, returns nonzero
16636 if the function is prototyped. */
16637
16638static int
16639prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16640{
16641 struct attribute *attr;
16642
16643 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16644 if (attr && (DW_UNSND (attr) != 0))
16645 return 1;
16646
16647 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16648 is only meaningful for C, but the concept also extends to other
4d804846
JB
16649 languages that allow unprototyped functions (Eg: Objective C).
16650 For all other languages, assume that functions are always
16651 prototyped. */
16652 if (cu->language != language_c
16653 && cu->language != language_objc
16654 && cu->language != language_opencl)
16655 return 1;
16656
16657 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16658 prototyped and unprototyped functions; default to prototyped,
16659 since that is more common in modern code (and RealView warns
16660 about unprototyped functions). */
16661 if (producer_is_realview (cu->producer))
16662 return 1;
16663
16664 return 0;
16665}
16666
c906108c
SS
16667/* Handle DIES due to C code like:
16668
16669 struct foo
c5aa993b
JM
16670 {
16671 int (*funcp)(int a, long l);
16672 int b;
16673 };
c906108c 16674
0963b4bd 16675 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16676
f792889a 16677static struct type *
e7c27a73 16678read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16679{
518817b3 16680 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16681 struct type *type; /* Type that this function returns. */
16682 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16683 struct attribute *attr;
16684
e7c27a73 16685 type = die_type (die, cu);
7e314c57
JK
16686
16687 /* The die_type call above may have already set the type for this DIE. */
16688 ftype = get_die_type (die, cu);
16689 if (ftype)
16690 return ftype;
16691
0c8b41f1 16692 ftype = lookup_function_type (type);
c906108c 16693
4d804846 16694 if (prototyped_function_p (die, cu))
a6c727b2 16695 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16696
c055b101
CV
16697 /* Store the calling convention in the type if it's available in
16698 the subroutine die. Otherwise set the calling convention to
16699 the default value DW_CC_normal. */
16700 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16701 if (attr != nullptr
16702 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16703 TYPE_CALLING_CONVENTION (ftype)
16704 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16705 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16706 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16707 else
16708 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16709
743649fd
MW
16710 /* Record whether the function returns normally to its caller or not
16711 if the DWARF producer set that information. */
16712 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16713 if (attr && (DW_UNSND (attr) != 0))
16714 TYPE_NO_RETURN (ftype) = 1;
16715
76c10ea2
GM
16716 /* We need to add the subroutine type to the die immediately so
16717 we don't infinitely recurse when dealing with parameters
0963b4bd 16718 declared as the same subroutine type. */
76c10ea2 16719 set_die_type (die, ftype, cu);
6e70227d 16720
639d11d3 16721 if (die->child != NULL)
c906108c 16722 {
bb5ed363 16723 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16724 struct die_info *child_die;
8072405b 16725 int nparams, iparams;
c906108c
SS
16726
16727 /* Count the number of parameters.
16728 FIXME: GDB currently ignores vararg functions, but knows about
16729 vararg member functions. */
8072405b 16730 nparams = 0;
639d11d3 16731 child_die = die->child;
c906108c
SS
16732 while (child_die && child_die->tag)
16733 {
16734 if (child_die->tag == DW_TAG_formal_parameter)
16735 nparams++;
16736 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16737 TYPE_VARARGS (ftype) = 1;
c906108c
SS
16738 child_die = sibling_die (child_die);
16739 }
16740
16741 /* Allocate storage for parameters and fill them in. */
16742 TYPE_NFIELDS (ftype) = nparams;
16743 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16744 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16745
8072405b
JK
16746 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16747 even if we error out during the parameters reading below. */
16748 for (iparams = 0; iparams < nparams; iparams++)
16749 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16750
16751 iparams = 0;
639d11d3 16752 child_die = die->child;
c906108c
SS
16753 while (child_die && child_die->tag)
16754 {
16755 if (child_die->tag == DW_TAG_formal_parameter)
16756 {
3ce3b1ba
PA
16757 struct type *arg_type;
16758
16759 /* DWARF version 2 has no clean way to discern C++
16760 static and non-static member functions. G++ helps
16761 GDB by marking the first parameter for non-static
16762 member functions (which is the this pointer) as
16763 artificial. We pass this information to
16764 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16765
16766 DWARF version 3 added DW_AT_object_pointer, which GCC
16767 4.5 does not yet generate. */
e142c38c 16768 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16769 if (attr != nullptr)
c906108c
SS
16770 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16771 else
9c37b5ae 16772 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16773 arg_type = die_type (child_die, cu);
16774
16775 /* RealView does not mark THIS as const, which the testsuite
16776 expects. GCC marks THIS as const in method definitions,
16777 but not in the class specifications (GCC PR 43053). */
16778 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16779 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16780 {
16781 int is_this = 0;
16782 struct dwarf2_cu *arg_cu = cu;
16783 const char *name = dwarf2_name (child_die, cu);
16784
16785 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16786 if (attr != nullptr)
3ce3b1ba
PA
16787 {
16788 /* If the compiler emits this, use it. */
16789 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16790 is_this = 1;
16791 }
16792 else if (name && strcmp (name, "this") == 0)
16793 /* Function definitions will have the argument names. */
16794 is_this = 1;
16795 else if (name == NULL && iparams == 0)
16796 /* Declarations may not have the names, so like
16797 elsewhere in GDB, assume an artificial first
16798 argument is "this". */
16799 is_this = 1;
16800
16801 if (is_this)
16802 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16803 arg_type, 0);
16804 }
16805
16806 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16807 iparams++;
16808 }
16809 child_die = sibling_die (child_die);
16810 }
16811 }
16812
76c10ea2 16813 return ftype;
c906108c
SS
16814}
16815
f792889a 16816static struct type *
e7c27a73 16817read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16818{
518817b3 16819 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16820 const char *name = NULL;
3c8e0968 16821 struct type *this_type, *target_type;
c906108c 16822
94af9270 16823 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16824 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16825 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16826 set_die_type (die, this_type, cu);
3c8e0968
DE
16827 target_type = die_type (die, cu);
16828 if (target_type != this_type)
16829 TYPE_TARGET_TYPE (this_type) = target_type;
16830 else
16831 {
16832 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16833 spec and cause infinite loops in GDB. */
b98664d3 16834 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16835 "- DIE at %s [in module %s]"),
16836 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16837 TYPE_TARGET_TYPE (this_type) = NULL;
16838 }
e4003a34
TV
16839 if (name == NULL)
16840 {
16841 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16842 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16843 Handle these by just returning the target type, rather than
16844 constructing an anonymous typedef type and trying to handle this
16845 elsewhere. */
16846 set_die_type (die, target_type, cu);
16847 return target_type;
16848 }
f792889a 16849 return this_type;
c906108c
SS
16850}
16851
9b790ce7
UW
16852/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16853 (which may be different from NAME) to the architecture back-end to allow
16854 it to guess the correct format if necessary. */
16855
16856static struct type *
16857dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16858 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16859{
16860 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16861 const struct floatformat **format;
16862 struct type *type;
16863
16864 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16865 if (format)
103a685e 16866 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16867 else
77b7c781 16868 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16869
16870 return type;
16871}
16872
eb77c9df
AB
16873/* Allocate an integer type of size BITS and name NAME. */
16874
16875static struct type *
16876dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16877 int bits, int unsigned_p, const char *name)
16878{
16879 struct type *type;
16880
16881 /* Versions of Intel's C Compiler generate an integer type called "void"
16882 instead of using DW_TAG_unspecified_type. This has been seen on
16883 at least versions 14, 17, and 18. */
35ee2dc2
AB
16884 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16885 && strcmp (name, "void") == 0)
eb77c9df
AB
16886 type = objfile_type (objfile)->builtin_void;
16887 else
16888 type = init_integer_type (objfile, bits, unsigned_p, name);
16889
16890 return type;
16891}
16892
8bdc1658
AB
16893/* Initialise and return a floating point type of size BITS suitable for
16894 use as a component of a complex number. The NAME_HINT is passed through
16895 when initialising the floating point type and is the name of the complex
16896 type.
16897
16898 As DWARF doesn't currently provide an explicit name for the components
16899 of a complex number, but it can be helpful to have these components
16900 named, we try to select a suitable name based on the size of the
16901 component. */
16902static struct type *
16903dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16904 struct objfile *objfile,
103a685e
TT
16905 int bits, const char *name_hint,
16906 enum bfd_endian byte_order)
8bdc1658
AB
16907{
16908 gdbarch *gdbarch = get_objfile_arch (objfile);
16909 struct type *tt = nullptr;
16910
35add35e
AB
16911 /* Try to find a suitable floating point builtin type of size BITS.
16912 We're going to use the name of this type as the name for the complex
16913 target type that we are about to create. */
1db455a7 16914 switch (cu->language)
8bdc1658 16915 {
1db455a7
AB
16916 case language_fortran:
16917 switch (bits)
16918 {
16919 case 32:
16920 tt = builtin_f_type (gdbarch)->builtin_real;
16921 break;
16922 case 64:
16923 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16924 break;
16925 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16926 case 128:
16927 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16928 break;
16929 }
8bdc1658 16930 break;
1db455a7
AB
16931 default:
16932 switch (bits)
16933 {
16934 case 32:
16935 tt = builtin_type (gdbarch)->builtin_float;
16936 break;
16937 case 64:
16938 tt = builtin_type (gdbarch)->builtin_double;
16939 break;
16940 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16941 case 128:
16942 tt = builtin_type (gdbarch)->builtin_long_double;
16943 break;
16944 }
8bdc1658
AB
16945 break;
16946 }
16947
35add35e
AB
16948 /* If the type we found doesn't match the size we were looking for, then
16949 pretend we didn't find a type at all, the complex target type we
16950 create will then be nameless. */
a12e5744 16951 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16952 tt = nullptr;
16953
8bdc1658 16954 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16955 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16956}
16957
c906108c
SS
16958/* Find a representation of a given base type and install
16959 it in the TYPE field of the die. */
16960
f792889a 16961static struct type *
e7c27a73 16962read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16963{
518817b3 16964 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16965 struct type *type;
16966 struct attribute *attr;
19f392bc 16967 int encoding = 0, bits = 0;
15d034d0 16968 const char *name;
34877895 16969 gdbarch *arch;
c906108c 16970
e142c38c 16971 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16972 if (attr != nullptr)
34877895 16973 encoding = DW_UNSND (attr);
e142c38c 16974 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16975 if (attr != nullptr)
34877895 16976 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16977 name = dwarf2_name (die, cu);
6ccb9162 16978 if (!name)
34877895 16979 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16980
16981 arch = get_objfile_arch (objfile);
16982 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16983
34877895
PJ
16984 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16985 if (attr)
103a685e
TT
16986 {
16987 int endianity = DW_UNSND (attr);
16988
16989 switch (endianity)
16990 {
16991 case DW_END_big:
16992 byte_order = BFD_ENDIAN_BIG;
16993 break;
16994 case DW_END_little:
16995 byte_order = BFD_ENDIAN_LITTLE;
16996 break;
16997 default:
16998 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16999 break;
17000 }
17001 }
6ccb9162
UW
17002
17003 switch (encoding)
c906108c 17004 {
6ccb9162
UW
17005 case DW_ATE_address:
17006 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17007 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17008 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17009 break;
17010 case DW_ATE_boolean:
19f392bc 17011 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17012 break;
17013 case DW_ATE_complex_float:
103a685e
TT
17014 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17015 byte_order);
19f392bc 17016 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17017 break;
17018 case DW_ATE_decimal_float:
19f392bc 17019 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17020 break;
17021 case DW_ATE_float:
103a685e 17022 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17023 break;
17024 case DW_ATE_signed:
eb77c9df 17025 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17026 break;
17027 case DW_ATE_unsigned:
3b2b8fea
TT
17028 if (cu->language == language_fortran
17029 && name
61012eef 17030 && startswith (name, "character("))
19f392bc
UW
17031 type = init_character_type (objfile, bits, 1, name);
17032 else
eb77c9df 17033 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17034 break;
17035 case DW_ATE_signed_char:
6e70227d 17036 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17037 || cu->language == language_pascal
17038 || cu->language == language_fortran)
19f392bc
UW
17039 type = init_character_type (objfile, bits, 0, name);
17040 else
eb77c9df 17041 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17042 break;
17043 case DW_ATE_unsigned_char:
868a0084 17044 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17045 || cu->language == language_pascal
c44af4eb
TT
17046 || cu->language == language_fortran
17047 || cu->language == language_rust)
19f392bc
UW
17048 type = init_character_type (objfile, bits, 1, name);
17049 else
eb77c9df 17050 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17051 break;
75079b2b 17052 case DW_ATE_UTF:
53e710ac 17053 {
53e710ac
PA
17054 if (bits == 16)
17055 type = builtin_type (arch)->builtin_char16;
17056 else if (bits == 32)
17057 type = builtin_type (arch)->builtin_char32;
17058 else
17059 {
b98664d3 17060 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17061 bits);
eb77c9df 17062 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17063 }
17064 return set_die_type (die, type, cu);
17065 }
75079b2b
TT
17066 break;
17067
6ccb9162 17068 default:
b98664d3 17069 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17070 dwarf_type_encoding_name (encoding));
77b7c781 17071 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17072 break;
c906108c 17073 }
6ccb9162 17074
0114d602 17075 if (name && strcmp (name, "char") == 0)
876cecd0 17076 TYPE_NOSIGN (type) = 1;
0114d602 17077
2b4424c3
TT
17078 maybe_set_alignment (cu, die, type);
17079
103a685e 17080 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17081
f792889a 17082 return set_die_type (die, type, cu);
c906108c
SS
17083}
17084
80180f79
SA
17085/* Parse dwarf attribute if it's a block, reference or constant and put the
17086 resulting value of the attribute into struct bound_prop.
17087 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17088
17089static int
17090attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17091 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17092 struct type *default_type)
80180f79
SA
17093{
17094 struct dwarf2_property_baton *baton;
518817b3
SM
17095 struct obstack *obstack
17096 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17097
9a49df9d
AB
17098 gdb_assert (default_type != NULL);
17099
80180f79
SA
17100 if (attr == NULL || prop == NULL)
17101 return 0;
17102
4fc6c0d5 17103 if (attr->form_is_block ())
80180f79 17104 {
8d749320 17105 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17106 baton->property_type = default_type;
80180f79
SA
17107 baton->locexpr.per_cu = cu->per_cu;
17108 baton->locexpr.size = DW_BLOCK (attr)->size;
17109 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17110 switch (attr->name)
17111 {
17112 case DW_AT_string_length:
17113 baton->locexpr.is_reference = true;
17114 break;
17115 default:
17116 baton->locexpr.is_reference = false;
17117 break;
17118 }
80180f79
SA
17119 prop->data.baton = baton;
17120 prop->kind = PROP_LOCEXPR;
17121 gdb_assert (prop->data.baton != NULL);
17122 }
cd6c91b4 17123 else if (attr->form_is_ref ())
80180f79
SA
17124 {
17125 struct dwarf2_cu *target_cu = cu;
17126 struct die_info *target_die;
17127 struct attribute *target_attr;
17128
17129 target_die = follow_die_ref (die, attr, &target_cu);
17130 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17131 if (target_attr == NULL)
17132 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17133 target_cu);
80180f79
SA
17134 if (target_attr == NULL)
17135 return 0;
17136
df25ebbd 17137 switch (target_attr->name)
80180f79 17138 {
df25ebbd 17139 case DW_AT_location:
cd6c91b4 17140 if (target_attr->form_is_section_offset ())
df25ebbd 17141 {
8d749320 17142 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17143 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17144 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17145 prop->data.baton = baton;
17146 prop->kind = PROP_LOCLIST;
17147 gdb_assert (prop->data.baton != NULL);
17148 }
4fc6c0d5 17149 else if (target_attr->form_is_block ())
df25ebbd 17150 {
8d749320 17151 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17152 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17153 baton->locexpr.per_cu = cu->per_cu;
17154 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17155 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17156 baton->locexpr.is_reference = true;
df25ebbd
JB
17157 prop->data.baton = baton;
17158 prop->kind = PROP_LOCEXPR;
17159 gdb_assert (prop->data.baton != NULL);
17160 }
17161 else
17162 {
17163 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17164 "dynamic property");
17165 return 0;
17166 }
17167 break;
17168 case DW_AT_data_member_location:
17169 {
17170 LONGEST offset;
17171
17172 if (!handle_data_member_location (target_die, target_cu,
17173 &offset))
17174 return 0;
17175
8d749320 17176 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17177 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17178 target_cu);
df25ebbd
JB
17179 baton->offset_info.offset = offset;
17180 baton->offset_info.type = die_type (target_die, target_cu);
17181 prop->data.baton = baton;
17182 prop->kind = PROP_ADDR_OFFSET;
17183 break;
17184 }
80180f79
SA
17185 }
17186 }
cd6c91b4 17187 else if (attr->form_is_constant ())
80180f79
SA
17188 {
17189 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17190 prop->kind = PROP_CONST;
17191 }
17192 else
17193 {
17194 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17195 dwarf2_name (die, cu));
17196 return 0;
17197 }
17198
17199 return 1;
17200}
17201
09ba997f 17202/* See read.h. */
9a49df9d 17203
09ba997f
TT
17204struct type *
17205dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17206{
09ba997f 17207 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17208 struct type *int_type;
17209
17210 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17211#define TRY_TYPE(F) \
17212 int_type = (unsigned_p \
17213 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17214 : objfile_type (objfile)->builtin_ ## F); \
17215 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17216 return int_type
17217
17218 TRY_TYPE (char);
17219 TRY_TYPE (short);
17220 TRY_TYPE (int);
17221 TRY_TYPE (long);
17222 TRY_TYPE (long_long);
17223
17224#undef TRY_TYPE
17225
17226 gdb_assert_not_reached ("unable to find suitable integer type");
17227}
17228
09ba997f 17229/* See read.h. */
11a8b164 17230
09ba997f
TT
17231struct type *
17232dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17233{
09ba997f
TT
17234 int addr_size = this->addr_size ();
17235 return int_type (addr_size, unsigned_p);
11a8b164
AB
17236}
17237
b86352cf
AB
17238/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17239 present (which is valid) then compute the default type based on the
17240 compilation units address size. */
17241
17242static struct type *
17243read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17244{
17245 struct type *index_type = die_type (die, cu);
17246
17247 /* Dwarf-2 specifications explicitly allows to create subrange types
17248 without specifying a base type.
17249 In that case, the base type must be set to the type of
17250 the lower bound, upper bound or count, in that order, if any of these
17251 three attributes references an object that has a type.
17252 If no base type is found, the Dwarf-2 specifications say that
17253 a signed integer type of size equal to the size of an address should
17254 be used.
17255 For the following C code: `extern char gdb_int [];'
17256 GCC produces an empty range DIE.
17257 FIXME: muller/2010-05-28: Possible references to object for low bound,
17258 high bound or count are not yet handled by this code. */
17259 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17260 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17261
17262 return index_type;
17263}
17264
a02abb62
JB
17265/* Read the given DW_AT_subrange DIE. */
17266
f792889a 17267static struct type *
a02abb62
JB
17268read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17269{
4c9ad8c2 17270 struct type *base_type, *orig_base_type;
a02abb62
JB
17271 struct type *range_type;
17272 struct attribute *attr;
729efb13 17273 struct dynamic_prop low, high;
4fae6e18 17274 int low_default_is_valid;
c451ebe5 17275 int high_bound_is_count = 0;
15d034d0 17276 const char *name;
d359392f 17277 ULONGEST negative_mask;
e77813c8 17278
b86352cf
AB
17279 orig_base_type = read_subrange_index_type (die, cu);
17280
4c9ad8c2
TT
17281 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17282 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17283 creating the range type, but we use the result of check_typedef
17284 when examining properties of the type. */
17285 base_type = check_typedef (orig_base_type);
a02abb62 17286
7e314c57
JK
17287 /* The die_type call above may have already set the type for this DIE. */
17288 range_type = get_die_type (die, cu);
17289 if (range_type)
17290 return range_type;
17291
729efb13
SA
17292 low.kind = PROP_CONST;
17293 high.kind = PROP_CONST;
17294 high.data.const_val = 0;
17295
4fae6e18
JK
17296 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17297 omitting DW_AT_lower_bound. */
17298 switch (cu->language)
6e70227d 17299 {
4fae6e18
JK
17300 case language_c:
17301 case language_cplus:
729efb13 17302 low.data.const_val = 0;
4fae6e18
JK
17303 low_default_is_valid = 1;
17304 break;
17305 case language_fortran:
729efb13 17306 low.data.const_val = 1;
4fae6e18
JK
17307 low_default_is_valid = 1;
17308 break;
17309 case language_d:
4fae6e18 17310 case language_objc:
c44af4eb 17311 case language_rust:
729efb13 17312 low.data.const_val = 0;
4fae6e18
JK
17313 low_default_is_valid = (cu->header.version >= 4);
17314 break;
17315 case language_ada:
17316 case language_m2:
17317 case language_pascal:
729efb13 17318 low.data.const_val = 1;
4fae6e18
JK
17319 low_default_is_valid = (cu->header.version >= 4);
17320 break;
17321 default:
729efb13 17322 low.data.const_val = 0;
4fae6e18
JK
17323 low_default_is_valid = 0;
17324 break;
a02abb62
JB
17325 }
17326
e142c38c 17327 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17328 if (attr != nullptr)
9a49df9d 17329 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17330 else if (!low_default_is_valid)
b98664d3 17331 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17332 "- DIE at %s [in module %s]"),
17333 sect_offset_str (die->sect_off),
518817b3 17334 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17335
506f5c41
TV
17336 struct attribute *attr_ub, *attr_count;
17337 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17338 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17339 {
506f5c41 17340 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17341 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17342 {
c451ebe5
SA
17343 /* If bounds are constant do the final calculation here. */
17344 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17345 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17346 else
17347 high_bound_is_count = 1;
c2ff108b 17348 }
506f5c41
TV
17349 else
17350 {
17351 if (attr_ub != NULL)
17352 complaint (_("Unresolved DW_AT_upper_bound "
17353 "- DIE at %s [in module %s]"),
17354 sect_offset_str (die->sect_off),
17355 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17356 if (attr_count != NULL)
17357 complaint (_("Unresolved DW_AT_count "
17358 "- DIE at %s [in module %s]"),
17359 sect_offset_str (die->sect_off),
17360 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17361 }
e77813c8 17362 }
a02abb62 17363
4e962e74
TT
17364 LONGEST bias = 0;
17365 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17366 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17367 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17368
dbb9c2b1
JB
17369 /* Normally, the DWARF producers are expected to use a signed
17370 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17371 But this is unfortunately not always the case, as witnessed
17372 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17373 is used instead. To work around that ambiguity, we treat
17374 the bounds as signed, and thus sign-extend their values, when
17375 the base type is signed. */
6e70227d 17376 negative_mask =
d359392f 17377 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17378 if (low.kind == PROP_CONST
17379 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17380 low.data.const_val |= negative_mask;
17381 if (high.kind == PROP_CONST
17382 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17383 high.data.const_val |= negative_mask;
43bbcdc2 17384
5bbd8269
AB
17385 /* Check for bit and byte strides. */
17386 struct dynamic_prop byte_stride_prop;
17387 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17388 if (attr_byte_stride != nullptr)
17389 {
09ba997f 17390 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17391 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17392 prop_type);
17393 }
17394
17395 struct dynamic_prop bit_stride_prop;
17396 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17397 if (attr_bit_stride != nullptr)
17398 {
17399 /* It only makes sense to have either a bit or byte stride. */
17400 if (attr_byte_stride != nullptr)
17401 {
17402 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17403 "- DIE at %s [in module %s]"),
17404 sect_offset_str (die->sect_off),
17405 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17406 attr_bit_stride = nullptr;
17407 }
17408 else
17409 {
09ba997f 17410 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17411 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17412 prop_type);
17413 }
17414 }
17415
17416 if (attr_byte_stride != nullptr
17417 || attr_bit_stride != nullptr)
17418 {
17419 bool byte_stride_p = (attr_byte_stride != nullptr);
17420 struct dynamic_prop *stride
17421 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17422
17423 range_type
17424 = create_range_type_with_stride (NULL, orig_base_type, &low,
17425 &high, bias, stride, byte_stride_p);
17426 }
17427 else
17428 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17429
c451ebe5
SA
17430 if (high_bound_is_count)
17431 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17432
c2ff108b
JK
17433 /* Ada expects an empty array on no boundary attributes. */
17434 if (attr == NULL && cu->language != language_ada)
729efb13 17435 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17436
39cbfefa
DJ
17437 name = dwarf2_name (die, cu);
17438 if (name)
17439 TYPE_NAME (range_type) = name;
6e70227d 17440
e142c38c 17441 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17442 if (attr != nullptr)
a02abb62
JB
17443 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17444
2b4424c3
TT
17445 maybe_set_alignment (cu, die, range_type);
17446
7e314c57
JK
17447 set_die_type (die, range_type, cu);
17448
17449 /* set_die_type should be already done. */
b4ba55a1
JB
17450 set_descriptive_type (range_type, die, cu);
17451
7e314c57 17452 return range_type;
a02abb62 17453}
6e70227d 17454
f792889a 17455static struct type *
81a17f79
JB
17456read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17457{
17458 struct type *type;
81a17f79 17459
518817b3
SM
17460 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17461 NULL);
0114d602 17462 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17463
74a2f8ff 17464 /* In Ada, an unspecified type is typically used when the description
85102364 17465 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17466 such a type, we treat it as a stub, and try to resolve it later on,
17467 when needed. */
17468 if (cu->language == language_ada)
17469 TYPE_STUB (type) = 1;
17470
f792889a 17471 return set_die_type (die, type, cu);
81a17f79 17472}
a02abb62 17473
639d11d3
DC
17474/* Read a single die and all its descendents. Set the die's sibling
17475 field to NULL; set other fields in the die correctly, and set all
17476 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17477 location of the info_ptr after reading all of those dies. PARENT
17478 is the parent of the die in question. */
17479
17480static struct die_info *
dee91e82 17481read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17482 const gdb_byte *info_ptr,
17483 const gdb_byte **new_info_ptr,
dee91e82 17484 struct die_info *parent)
639d11d3
DC
17485{
17486 struct die_info *die;
d521ce57 17487 const gdb_byte *cur_ptr;
639d11d3 17488
3e225074 17489 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17490 if (die == NULL)
17491 {
17492 *new_info_ptr = cur_ptr;
17493 return NULL;
17494 }
93311388 17495 store_in_ref_table (die, reader->cu);
639d11d3 17496
3e225074 17497 if (die->has_children)
bf6af496 17498 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17499 else
17500 {
17501 die->child = NULL;
17502 *new_info_ptr = cur_ptr;
17503 }
17504
17505 die->sibling = NULL;
17506 die->parent = parent;
17507 return die;
17508}
17509
17510/* Read a die, all of its descendents, and all of its siblings; set
17511 all of the fields of all of the dies correctly. Arguments are as
17512 in read_die_and_children. */
17513
17514static struct die_info *
bf6af496 17515read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17516 const gdb_byte *info_ptr,
17517 const gdb_byte **new_info_ptr,
bf6af496 17518 struct die_info *parent)
639d11d3
DC
17519{
17520 struct die_info *first_die, *last_sibling;
d521ce57 17521 const gdb_byte *cur_ptr;
639d11d3 17522
c906108c 17523 cur_ptr = info_ptr;
639d11d3
DC
17524 first_die = last_sibling = NULL;
17525
17526 while (1)
c906108c 17527 {
639d11d3 17528 struct die_info *die
dee91e82 17529 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17530
1d325ec1 17531 if (die == NULL)
c906108c 17532 {
639d11d3
DC
17533 *new_info_ptr = cur_ptr;
17534 return first_die;
c906108c 17535 }
1d325ec1
DJ
17536
17537 if (!first_die)
17538 first_die = die;
c906108c 17539 else
1d325ec1
DJ
17540 last_sibling->sibling = die;
17541
17542 last_sibling = die;
c906108c 17543 }
c906108c
SS
17544}
17545
bf6af496
DE
17546/* Read a die, all of its descendents, and all of its siblings; set
17547 all of the fields of all of the dies correctly. Arguments are as
17548 in read_die_and_children.
17549 This the main entry point for reading a DIE and all its children. */
17550
17551static struct die_info *
17552read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17553 const gdb_byte *info_ptr,
17554 const gdb_byte **new_info_ptr,
bf6af496
DE
17555 struct die_info *parent)
17556{
17557 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17558 new_info_ptr, parent);
17559
b4f54984 17560 if (dwarf_die_debug)
bf6af496
DE
17561 {
17562 fprintf_unfiltered (gdb_stdlog,
17563 "Read die from %s@0x%x of %s:\n",
96b79293 17564 reader->die_section->get_name (),
bf6af496
DE
17565 (unsigned) (info_ptr - reader->die_section->buffer),
17566 bfd_get_filename (reader->abfd));
b4f54984 17567 dump_die (die, dwarf_die_debug);
bf6af496
DE
17568 }
17569
17570 return die;
17571}
17572
3019eac3
DE
17573/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17574 attributes.
17575 The caller is responsible for filling in the extra attributes
17576 and updating (*DIEP)->num_attrs.
17577 Set DIEP to point to a newly allocated die with its information,
3e225074 17578 except for its child, sibling, and parent fields. */
93311388 17579
d521ce57 17580static const gdb_byte *
3019eac3 17581read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17582 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17583 int num_extra_attrs)
93311388 17584{
b64f50a1 17585 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17586 struct abbrev_info *abbrev;
17587 struct die_info *die;
17588 struct dwarf2_cu *cu = reader->cu;
17589 bfd *abfd = reader->abfd;
17590
9c541725 17591 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17592 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17593 info_ptr += bytes_read;
17594 if (!abbrev_number)
17595 {
17596 *diep = NULL;
93311388
DE
17597 return info_ptr;
17598 }
17599
685af9cd 17600 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17601 if (!abbrev)
348e048f
DE
17602 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17603 abbrev_number,
17604 bfd_get_filename (abfd));
17605
3019eac3 17606 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17607 die->sect_off = sect_off;
93311388
DE
17608 die->tag = abbrev->tag;
17609 die->abbrev = abbrev_number;
3e225074 17610 die->has_children = abbrev->has_children;
93311388 17611
3019eac3
DE
17612 /* Make the result usable.
17613 The caller needs to update num_attrs after adding the extra
17614 attributes. */
93311388
DE
17615 die->num_attrs = abbrev->num_attrs;
17616
18a8505e 17617 std::vector<int> indexes_that_need_reprocess;
93311388 17618 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17619 {
17620 bool need_reprocess;
17621 info_ptr =
17622 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17623 info_ptr, &need_reprocess);
17624 if (need_reprocess)
17625 indexes_that_need_reprocess.push_back (i);
17626 }
17627
17628 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17629 if (attr != nullptr)
17630 cu->str_offsets_base = DW_UNSND (attr);
93311388 17631
18a8505e
AT
17632 auto maybe_addr_base = lookup_addr_base(die);
17633 if (maybe_addr_base.has_value ())
17634 cu->addr_base = *maybe_addr_base;
17635 for (int index : indexes_that_need_reprocess)
17636 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17637 *diep = die;
93311388
DE
17638 return info_ptr;
17639}
17640
3019eac3
DE
17641/* Read a die and all its attributes.
17642 Set DIEP to point to a newly allocated die with its information,
3e225074 17643 except for its child, sibling, and parent fields. */
3019eac3 17644
d521ce57 17645static const gdb_byte *
3019eac3 17646read_full_die (const struct die_reader_specs *reader,
3e225074 17647 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17648{
d521ce57 17649 const gdb_byte *result;
bf6af496 17650
3e225074 17651 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17652
b4f54984 17653 if (dwarf_die_debug)
bf6af496
DE
17654 {
17655 fprintf_unfiltered (gdb_stdlog,
17656 "Read die from %s@0x%x of %s:\n",
96b79293 17657 reader->die_section->get_name (),
bf6af496
DE
17658 (unsigned) (info_ptr - reader->die_section->buffer),
17659 bfd_get_filename (reader->abfd));
b4f54984 17660 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17661 }
17662
17663 return result;
3019eac3 17664}
433df2d4 17665\f
c906108c 17666
72bf9492
DJ
17667/* Returns nonzero if TAG represents a type that we might generate a partial
17668 symbol for. */
17669
17670static int
17671is_type_tag_for_partial (int tag)
17672{
17673 switch (tag)
17674 {
17675#if 0
17676 /* Some types that would be reasonable to generate partial symbols for,
17677 that we don't at present. */
17678 case DW_TAG_array_type:
17679 case DW_TAG_file_type:
17680 case DW_TAG_ptr_to_member_type:
17681 case DW_TAG_set_type:
17682 case DW_TAG_string_type:
17683 case DW_TAG_subroutine_type:
17684#endif
17685 case DW_TAG_base_type:
17686 case DW_TAG_class_type:
680b30c7 17687 case DW_TAG_interface_type:
72bf9492
DJ
17688 case DW_TAG_enumeration_type:
17689 case DW_TAG_structure_type:
17690 case DW_TAG_subrange_type:
17691 case DW_TAG_typedef:
17692 case DW_TAG_union_type:
17693 return 1;
17694 default:
17695 return 0;
17696 }
17697}
17698
17699/* Load all DIEs that are interesting for partial symbols into memory. */
17700
17701static struct partial_die_info *
dee91e82 17702load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17703 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17704{
dee91e82 17705 struct dwarf2_cu *cu = reader->cu;
518817b3 17706 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17707 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17708 unsigned int bytes_read;
5afb4e99 17709 unsigned int load_all = 0;
72bf9492
DJ
17710 int nesting_level = 1;
17711
17712 parent_die = NULL;
17713 last_die = NULL;
17714
7adf1e79
DE
17715 gdb_assert (cu->per_cu != NULL);
17716 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17717 load_all = 1;
17718
72bf9492
DJ
17719 cu->partial_dies
17720 = htab_create_alloc_ex (cu->header.length / 12,
17721 partial_die_hash,
17722 partial_die_eq,
17723 NULL,
17724 &cu->comp_unit_obstack,
17725 hashtab_obstack_allocate,
17726 dummy_obstack_deallocate);
17727
72bf9492
DJ
17728 while (1)
17729 {
685af9cd 17730 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17731
17732 /* A NULL abbrev means the end of a series of children. */
17733 if (abbrev == NULL)
17734 {
17735 if (--nesting_level == 0)
cd9983dd
YQ
17736 return first_die;
17737
72bf9492
DJ
17738 info_ptr += bytes_read;
17739 last_die = parent_die;
17740 parent_die = parent_die->die_parent;
17741 continue;
17742 }
17743
98bfdba5
PA
17744 /* Check for template arguments. We never save these; if
17745 they're seen, we just mark the parent, and go on our way. */
17746 if (parent_die != NULL
17747 && cu->language == language_cplus
17748 && (abbrev->tag == DW_TAG_template_type_param
17749 || abbrev->tag == DW_TAG_template_value_param))
17750 {
17751 parent_die->has_template_arguments = 1;
17752
17753 if (!load_all)
17754 {
17755 /* We don't need a partial DIE for the template argument. */
dee91e82 17756 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17757 continue;
17758 }
17759 }
17760
0d99eb77 17761 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17762 Skip their other children. */
17763 if (!load_all
17764 && cu->language == language_cplus
17765 && parent_die != NULL
17766 && parent_die->tag == DW_TAG_subprogram)
17767 {
dee91e82 17768 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17769 continue;
17770 }
17771
5afb4e99
DJ
17772 /* Check whether this DIE is interesting enough to save. Normally
17773 we would not be interested in members here, but there may be
17774 later variables referencing them via DW_AT_specification (for
17775 static members). */
17776 if (!load_all
17777 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17778 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17779 && abbrev->tag != DW_TAG_enumerator
17780 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17781 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17782 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17783 && abbrev->tag != DW_TAG_variable
5afb4e99 17784 && abbrev->tag != DW_TAG_namespace
f55ee35c 17785 && abbrev->tag != DW_TAG_module
95554aad 17786 && abbrev->tag != DW_TAG_member
74921315
KS
17787 && abbrev->tag != DW_TAG_imported_unit
17788 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17789 {
17790 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17791 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17792 continue;
17793 }
17794
6f06d47b
YQ
17795 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17796 abbrev);
cd9983dd 17797
48fbe735 17798 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17799
17800 /* This two-pass algorithm for processing partial symbols has a
17801 high cost in cache pressure. Thus, handle some simple cases
17802 here which cover the majority of C partial symbols. DIEs
17803 which neither have specification tags in them, nor could have
17804 specification tags elsewhere pointing at them, can simply be
17805 processed and discarded.
17806
17807 This segment is also optional; scan_partial_symbols and
17808 add_partial_symbol will handle these DIEs if we chain
17809 them in normally. When compilers which do not emit large
17810 quantities of duplicate debug information are more common,
17811 this code can probably be removed. */
17812
17813 /* Any complete simple types at the top level (pretty much all
17814 of them, for a language without namespaces), can be processed
17815 directly. */
17816 if (parent_die == NULL
cd9983dd
YQ
17817 && pdi.has_specification == 0
17818 && pdi.is_declaration == 0
17819 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17820 || pdi.tag == DW_TAG_base_type
17821 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17822 {
cd9983dd 17823 if (building_psymtab && pdi.name != NULL)
31edb802 17824 add_psymbol_to_list (pdi.name, false,
79748972 17825 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17826 psymbol_placement::STATIC,
1762568f 17827 0, cu->language, objfile);
cd9983dd 17828 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17829 continue;
17830 }
17831
d8228535
JK
17832 /* The exception for DW_TAG_typedef with has_children above is
17833 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17834 type_name_or_error will error on such types later.
d8228535
JK
17835
17836 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17837 it could not find the child DIEs referenced later, this is checked
17838 above. In correct DWARF DW_TAG_typedef should have no children. */
17839
cd9983dd 17840 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17841 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17842 "- DIE at %s [in module %s]"),
cd9983dd 17843 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17844
72bf9492
DJ
17845 /* If we're at the second level, and we're an enumerator, and
17846 our parent has no specification (meaning possibly lives in a
17847 namespace elsewhere), then we can add the partial symbol now
17848 instead of queueing it. */
cd9983dd 17849 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17850 && parent_die != NULL
17851 && parent_die->die_parent == NULL
17852 && parent_die->tag == DW_TAG_enumeration_type
17853 && parent_die->has_specification == 0)
17854 {
cd9983dd 17855 if (pdi.name == NULL)
b98664d3 17856 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17857 else if (building_psymtab)
31edb802 17858 add_psymbol_to_list (pdi.name, false,
79748972 17859 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17860 cu->language == language_cplus
75aedd27
TT
17861 ? psymbol_placement::GLOBAL
17862 : psymbol_placement::STATIC,
1762568f 17863 0, cu->language, objfile);
72bf9492 17864
cd9983dd 17865 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17866 continue;
17867 }
17868
cd9983dd 17869 struct partial_die_info *part_die
6f06d47b 17870 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17871
72bf9492
DJ
17872 /* We'll save this DIE so link it in. */
17873 part_die->die_parent = parent_die;
17874 part_die->die_sibling = NULL;
17875 part_die->die_child = NULL;
17876
17877 if (last_die && last_die == parent_die)
17878 last_die->die_child = part_die;
17879 else if (last_die)
17880 last_die->die_sibling = part_die;
17881
17882 last_die = part_die;
17883
17884 if (first_die == NULL)
17885 first_die = part_die;
17886
17887 /* Maybe add the DIE to the hash table. Not all DIEs that we
17888 find interesting need to be in the hash table, because we
17889 also have the parent/sibling/child chains; only those that we
17890 might refer to by offset later during partial symbol reading.
17891
17892 For now this means things that might have be the target of a
17893 DW_AT_specification, DW_AT_abstract_origin, or
17894 DW_AT_extension. DW_AT_extension will refer only to
17895 namespaces; DW_AT_abstract_origin refers to functions (and
17896 many things under the function DIE, but we do not recurse
17897 into function DIEs during partial symbol reading) and
17898 possibly variables as well; DW_AT_specification refers to
17899 declarations. Declarations ought to have the DW_AT_declaration
17900 flag. It happens that GCC forgets to put it in sometimes, but
17901 only for functions, not for types.
17902
17903 Adding more things than necessary to the hash table is harmless
17904 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17905 wasted time in find_partial_die, when we reread the compilation
17906 unit with load_all_dies set. */
72bf9492 17907
5afb4e99 17908 if (load_all
72929c62 17909 || abbrev->tag == DW_TAG_constant
5afb4e99 17910 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17911 || abbrev->tag == DW_TAG_variable
17912 || abbrev->tag == DW_TAG_namespace
17913 || part_die->is_declaration)
17914 {
17915 void **slot;
17916
17917 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17918 to_underlying (part_die->sect_off),
17919 INSERT);
72bf9492
DJ
17920 *slot = part_die;
17921 }
17922
72bf9492 17923 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17924 we have no reason to follow the children of structures; for other
98bfdba5
PA
17925 languages we have to, so that we can get at method physnames
17926 to infer fully qualified class names, for DW_AT_specification,
17927 and for C++ template arguments. For C++, we also look one level
17928 inside functions to find template arguments (if the name of the
17929 function does not already contain the template arguments).
bc30ff58 17930
0a4b0913
AB
17931 For Ada and Fortran, we need to scan the children of subprograms
17932 and lexical blocks as well because these languages allow the
17933 definition of nested entities that could be interesting for the
17934 debugger, such as nested subprograms for instance. */
72bf9492 17935 if (last_die->has_children
5afb4e99
DJ
17936 && (load_all
17937 || last_die->tag == DW_TAG_namespace
f55ee35c 17938 || last_die->tag == DW_TAG_module
72bf9492 17939 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17940 || (cu->language == language_cplus
17941 && last_die->tag == DW_TAG_subprogram
17942 && (last_die->name == NULL
17943 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17944 || (cu->language != language_c
17945 && (last_die->tag == DW_TAG_class_type
680b30c7 17946 || last_die->tag == DW_TAG_interface_type
72bf9492 17947 || last_die->tag == DW_TAG_structure_type
bc30ff58 17948 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17949 || ((cu->language == language_ada
17950 || cu->language == language_fortran)
bc30ff58
JB
17951 && (last_die->tag == DW_TAG_subprogram
17952 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17953 {
17954 nesting_level++;
17955 parent_die = last_die;
17956 continue;
17957 }
17958
17959 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17960 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17961
17962 /* Back to the top, do it again. */
17963 }
17964}
17965
6f06d47b
YQ
17966partial_die_info::partial_die_info (sect_offset sect_off_,
17967 struct abbrev_info *abbrev)
17968 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17969{
17970}
17971
35cc7ed7
YQ
17972/* Read a minimal amount of information into the minimal die structure.
17973 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17974
48fbe735
YQ
17975const gdb_byte *
17976partial_die_info::read (const struct die_reader_specs *reader,
17977 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17978{
dee91e82 17979 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17980 struct dwarf2_per_objfile *dwarf2_per_objfile
17981 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17982 unsigned int i;
c5aa993b 17983 int has_low_pc_attr = 0;
c906108c 17984 int has_high_pc_attr = 0;
91da1414 17985 int high_pc_relative = 0;
c906108c 17986
18a8505e 17987 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 17988 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17989 {
18a8505e
AT
17990 bool need_reprocess;
17991 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17992 info_ptr, &need_reprocess);
17993 /* String and address offsets that need to do the reprocessing have
17994 already been read at this point, so there is no need to wait until
17995 the loop terminates to do the reprocessing. */
17996 if (need_reprocess)
17997 read_attribute_reprocess (reader, &attr_vec[i]);
17998 attribute &attr = attr_vec[i];
c906108c 17999 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18000 partial symbol table. */
c906108c
SS
18001 switch (attr.name)
18002 {
18003 case DW_AT_name:
48fbe735 18004 switch (tag)
71c25dea
TT
18005 {
18006 case DW_TAG_compile_unit:
95554aad 18007 case DW_TAG_partial_unit:
348e048f 18008 case DW_TAG_type_unit:
71c25dea
TT
18009 /* Compilation units have a DW_AT_name that is a filename, not
18010 a source language identifier. */
18011 case DW_TAG_enumeration_type:
18012 case DW_TAG_enumerator:
18013 /* These tags always have simple identifiers already; no need
18014 to canonicalize them. */
48fbe735 18015 name = DW_STRING (&attr);
71c25dea
TT
18016 break;
18017 default:
48fbe735
YQ
18018 {
18019 struct objfile *objfile = dwarf2_per_objfile->objfile;
18020
18021 name
be1e3d3e 18022 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18023 }
71c25dea
TT
18024 break;
18025 }
c906108c 18026 break;
31ef98ae 18027 case DW_AT_linkage_name:
c906108c 18028 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18029 /* Note that both forms of linkage name might appear. We
18030 assume they will be the same, and we only store the last
18031 one we see. */
48fbe735 18032 linkage_name = DW_STRING (&attr);
c906108c
SS
18033 break;
18034 case DW_AT_low_pc:
18035 has_low_pc_attr = 1;
cd6c91b4 18036 lowpc = attr.value_as_address ();
c906108c
SS
18037 break;
18038 case DW_AT_high_pc:
18039 has_high_pc_attr = 1;
cd6c91b4
TT
18040 highpc = attr.value_as_address ();
18041 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18042 high_pc_relative = 1;
c906108c
SS
18043 break;
18044 case DW_AT_location:
0963b4bd 18045 /* Support the .debug_loc offsets. */
4fc6c0d5 18046 if (attr.form_is_block ())
8e19ed76 18047 {
48fbe735 18048 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18049 }
cd6c91b4 18050 else if (attr.form_is_section_offset ())
8e19ed76 18051 {
4d3c2250 18052 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18053 }
18054 else
18055 {
4d3c2250
KB
18056 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18057 "partial symbol information");
8e19ed76 18058 }
c906108c 18059 break;
c906108c 18060 case DW_AT_external:
48fbe735 18061 is_external = DW_UNSND (&attr);
c906108c
SS
18062 break;
18063 case DW_AT_declaration:
48fbe735 18064 is_declaration = DW_UNSND (&attr);
c906108c
SS
18065 break;
18066 case DW_AT_type:
48fbe735 18067 has_type = 1;
c906108c
SS
18068 break;
18069 case DW_AT_abstract_origin:
18070 case DW_AT_specification:
72bf9492 18071 case DW_AT_extension:
48fbe735
YQ
18072 has_specification = 1;
18073 spec_offset = dwarf2_get_ref_die_offset (&attr);
18074 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18075 || cu->per_cu->is_dwz);
c906108c
SS
18076 break;
18077 case DW_AT_sibling:
18078 /* Ignore absolute siblings, they might point outside of
18079 the current compile unit. */
18080 if (attr.form == DW_FORM_ref_addr)
b98664d3 18081 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18082 else
b9502d3f 18083 {
48fbe735 18084 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18085 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18086 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18087
18088 if (sibling_ptr < info_ptr)
b98664d3 18089 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18090 else if (sibling_ptr > reader->buffer_end)
18091 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18092 else
48fbe735 18093 sibling = sibling_ptr;
b9502d3f 18094 }
c906108c 18095 break;
fa4028e9 18096 case DW_AT_byte_size:
48fbe735 18097 has_byte_size = 1;
fa4028e9 18098 break;
ff908ebf 18099 case DW_AT_const_value:
48fbe735 18100 has_const_value = 1;
ff908ebf 18101 break;
68511cec
CES
18102 case DW_AT_calling_convention:
18103 /* DWARF doesn't provide a way to identify a program's source-level
18104 entry point. DW_AT_calling_convention attributes are only meant
18105 to describe functions' calling conventions.
18106
18107 However, because it's a necessary piece of information in
0c1b455e
TT
18108 Fortran, and before DWARF 4 DW_CC_program was the only
18109 piece of debugging information whose definition refers to
18110 a 'main program' at all, several compilers marked Fortran
18111 main programs with DW_CC_program --- even when those
18112 functions use the standard calling conventions.
18113
18114 Although DWARF now specifies a way to provide this
18115 information, we support this practice for backward
18116 compatibility. */
68511cec 18117 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18118 && cu->language == language_fortran)
48fbe735 18119 main_subprogram = 1;
68511cec 18120 break;
481860b3
GB
18121 case DW_AT_inline:
18122 if (DW_UNSND (&attr) == DW_INL_inlined
18123 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18124 may_be_inlined = 1;
481860b3 18125 break;
95554aad
TT
18126
18127 case DW_AT_import:
48fbe735 18128 if (tag == DW_TAG_imported_unit)
36586728 18129 {
48fbe735
YQ
18130 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18131 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18132 || cu->per_cu->is_dwz);
18133 }
95554aad
TT
18134 break;
18135
0c1b455e 18136 case DW_AT_main_subprogram:
48fbe735 18137 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18138 break;
18139
05caa1d2
TT
18140 case DW_AT_ranges:
18141 {
18142 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18143 but that requires a full DIE, so instead we just
18144 reimplement it. */
18145 int need_ranges_base = tag != DW_TAG_compile_unit;
18146 unsigned int ranges_offset = (DW_UNSND (&attr)
18147 + (need_ranges_base
18148 ? cu->ranges_base
18149 : 0));
18150
18151 /* Value of the DW_AT_ranges attribute is the offset in the
18152 .debug_ranges section. */
18153 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18154 nullptr))
18155 has_pc_info = 1;
18156 }
18157 break;
18158
c906108c
SS
18159 default:
18160 break;
18161 }
18162 }
18163
10d06d82
TT
18164 /* For Ada, if both the name and the linkage name appear, we prefer
18165 the latter. This lets "catch exception" work better, regardless
18166 of the order in which the name and linkage name were emitted.
18167 Really, though, this is just a workaround for the fact that gdb
18168 doesn't store both the name and the linkage name. */
18169 if (cu->language == language_ada && linkage_name != nullptr)
18170 name = linkage_name;
18171
91da1414 18172 if (high_pc_relative)
48fbe735 18173 highpc += lowpc;
91da1414 18174
9373cf26
JK
18175 if (has_low_pc_attr && has_high_pc_attr)
18176 {
18177 /* When using the GNU linker, .gnu.linkonce. sections are used to
18178 eliminate duplicate copies of functions and vtables and such.
18179 The linker will arbitrarily choose one and discard the others.
18180 The AT_*_pc values for such functions refer to local labels in
18181 these sections. If the section from that file was discarded, the
18182 labels are not in the output, so the relocs get a value of 0.
18183 If this is a discarded function, mark the pc bounds as invalid,
18184 so that GDB will ignore it. */
48fbe735 18185 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18186 {
48fbe735 18187 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18188 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18189
b98664d3 18190 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18191 "for DIE at %s [in module %s]"),
48fbe735
YQ
18192 paddress (gdbarch, lowpc),
18193 sect_offset_str (sect_off),
9d8780f0 18194 objfile_name (objfile));
9373cf26
JK
18195 }
18196 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18197 else if (lowpc >= highpc)
9373cf26 18198 {
48fbe735 18199 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18201
b98664d3 18202 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18203 "for DIE at %s [in module %s]"),
48fbe735
YQ
18204 paddress (gdbarch, lowpc),
18205 paddress (gdbarch, highpc),
18206 sect_offset_str (sect_off),
9c541725 18207 objfile_name (objfile));
9373cf26
JK
18208 }
18209 else
48fbe735 18210 has_pc_info = 1;
9373cf26 18211 }
85cbf3d3 18212
c906108c
SS
18213 return info_ptr;
18214}
18215
72bf9492
DJ
18216/* Find a cached partial DIE at OFFSET in CU. */
18217
d590ff25
YQ
18218struct partial_die_info *
18219dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18220{
18221 struct partial_die_info *lookup_die = NULL;
6f06d47b 18222 struct partial_die_info part_die (sect_off);
72bf9492 18223
9a3c8263 18224 lookup_die = ((struct partial_die_info *)
d590ff25 18225 htab_find_with_hash (partial_dies, &part_die,
9c541725 18226 to_underlying (sect_off)));
72bf9492 18227
72bf9492
DJ
18228 return lookup_die;
18229}
18230
348e048f
DE
18231/* Find a partial DIE at OFFSET, which may or may not be in CU,
18232 except in the case of .debug_types DIEs which do not reference
18233 outside their CU (they do however referencing other types via
55f1336d 18234 DW_FORM_ref_sig8). */
72bf9492 18235
122cf0f2 18236static const struct cu_partial_die_info
9c541725 18237find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18238{
518817b3
SM
18239 struct dwarf2_per_objfile *dwarf2_per_objfile
18240 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18241 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18242 struct dwarf2_per_cu_data *per_cu = NULL;
18243 struct partial_die_info *pd = NULL;
72bf9492 18244
36586728 18245 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18246 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18247 {
d590ff25 18248 pd = cu->find_partial_die (sect_off);
5afb4e99 18249 if (pd != NULL)
fb816e8b 18250 return { cu, pd };
0d99eb77
DE
18251 /* We missed recording what we needed.
18252 Load all dies and try again. */
18253 per_cu = cu->per_cu;
5afb4e99 18254 }
0d99eb77
DE
18255 else
18256 {
18257 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18258 if (cu->per_cu->is_debug_types)
0d99eb77 18259 {
9d8780f0
SM
18260 error (_("Dwarf Error: Type Unit at offset %s contains"
18261 " external reference to offset %s [in module %s].\n"),
18262 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18263 bfd_get_filename (objfile->obfd));
18264 }
9c541725 18265 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18266 dwarf2_per_objfile);
72bf9492 18267
0d99eb77
DE
18268 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18269 load_partial_comp_unit (per_cu);
ae038cb0 18270
0d99eb77 18271 per_cu->cu->last_used = 0;
d590ff25 18272 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18273 }
5afb4e99 18274
dee91e82
DE
18275 /* If we didn't find it, and not all dies have been loaded,
18276 load them all and try again. */
18277
5afb4e99
DJ
18278 if (pd == NULL && per_cu->load_all_dies == 0)
18279 {
5afb4e99 18280 per_cu->load_all_dies = 1;
fd820528
DE
18281
18282 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18283 THIS_CU->cu may already be in use. So we can't just free it and
18284 replace its DIEs with the ones we read in. Instead, we leave those
18285 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18286 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18287 set. */
dee91e82 18288 load_partial_comp_unit (per_cu);
5afb4e99 18289
d590ff25 18290 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18291 }
18292
18293 if (pd == NULL)
18294 internal_error (__FILE__, __LINE__,
9d8780f0 18295 _("could not find partial DIE %s "
3e43a32a 18296 "in cache [from module %s]\n"),
9d8780f0 18297 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18298 return { per_cu->cu, pd };
72bf9492
DJ
18299}
18300
abc72ce4
DE
18301/* See if we can figure out if the class lives in a namespace. We do
18302 this by looking for a member function; its demangled name will
18303 contain namespace info, if there is any. */
18304
18305static void
18306guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18307 struct dwarf2_cu *cu)
18308{
18309 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18310 what template types look like, because the demangler
18311 frequently doesn't give the same name as the debug info. We
18312 could fix this by only using the demangled name to get the
18313 prefix (but see comment in read_structure_type). */
18314
18315 struct partial_die_info *real_pdi;
18316 struct partial_die_info *child_pdi;
18317
18318 /* If this DIE (this DIE's specification, if any) has a parent, then
18319 we should not do this. We'll prepend the parent's fully qualified
18320 name when we create the partial symbol. */
18321
18322 real_pdi = struct_pdi;
18323 while (real_pdi->has_specification)
fb816e8b 18324 {
122cf0f2
AB
18325 auto res = find_partial_die (real_pdi->spec_offset,
18326 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18327 real_pdi = res.pdi;
18328 cu = res.cu;
18329 }
abc72ce4
DE
18330
18331 if (real_pdi->die_parent != NULL)
18332 return;
18333
18334 for (child_pdi = struct_pdi->die_child;
18335 child_pdi != NULL;
18336 child_pdi = child_pdi->die_sibling)
18337 {
18338 if (child_pdi->tag == DW_TAG_subprogram
18339 && child_pdi->linkage_name != NULL)
18340 {
43816ebc
TT
18341 gdb::unique_xmalloc_ptr<char> actual_class_name
18342 (language_class_name_from_physname (cu->language_defn,
18343 child_pdi->linkage_name));
abc72ce4
DE
18344 if (actual_class_name != NULL)
18345 {
518817b3 18346 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18347 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18348 }
18349 break;
18350 }
18351 }
18352}
18353
52356b79
YQ
18354void
18355partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18356{
abc72ce4
DE
18357 /* Once we've fixed up a die, there's no point in doing so again.
18358 This also avoids a memory leak if we were to call
18359 guess_partial_die_structure_name multiple times. */
52356b79 18360 if (fixup_called)
abc72ce4
DE
18361 return;
18362
72bf9492
DJ
18363 /* If we found a reference attribute and the DIE has no name, try
18364 to find a name in the referred to DIE. */
18365
52356b79 18366 if (name == NULL && has_specification)
72bf9492
DJ
18367 {
18368 struct partial_die_info *spec_die;
72bf9492 18369
122cf0f2 18370 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18371 spec_die = res.pdi;
18372 cu = res.cu;
72bf9492 18373
52356b79 18374 spec_die->fixup (cu);
72bf9492
DJ
18375
18376 if (spec_die->name)
18377 {
52356b79 18378 name = spec_die->name;
72bf9492
DJ
18379
18380 /* Copy DW_AT_external attribute if it is set. */
18381 if (spec_die->is_external)
52356b79 18382 is_external = spec_die->is_external;
72bf9492
DJ
18383 }
18384 }
18385
18386 /* Set default names for some unnamed DIEs. */
72bf9492 18387
52356b79
YQ
18388 if (name == NULL && tag == DW_TAG_namespace)
18389 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18390
abc72ce4
DE
18391 /* If there is no parent die to provide a namespace, and there are
18392 children, see if we can determine the namespace from their linkage
122d1940 18393 name. */
abc72ce4 18394 if (cu->language == language_cplus
fd5866f6 18395 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18396 && die_parent == NULL
18397 && has_children
18398 && (tag == DW_TAG_class_type
18399 || tag == DW_TAG_structure_type
18400 || tag == DW_TAG_union_type))
18401 guess_partial_die_structure_name (this, cu);
abc72ce4 18402
53832f31
TT
18403 /* GCC might emit a nameless struct or union that has a linkage
18404 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18405 if (name == NULL
18406 && (tag == DW_TAG_class_type
18407 || tag == DW_TAG_interface_type
18408 || tag == DW_TAG_structure_type
18409 || tag == DW_TAG_union_type)
18410 && linkage_name != NULL)
53832f31 18411 {
43816ebc
TT
18412 gdb::unique_xmalloc_ptr<char> demangled
18413 (gdb_demangle (linkage_name, DMGL_TYPES));
18414 if (demangled != nullptr)
53832f31 18415 {
96408a79
SA
18416 const char *base;
18417
18418 /* Strip any leading namespaces/classes, keep only the base name.
18419 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18420 base = strrchr (demangled.get (), ':');
18421 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18422 base++;
18423 else
43816ebc 18424 base = demangled.get ();
96408a79 18425
518817b3 18426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18427 name = objfile->intern (base);
53832f31
TT
18428 }
18429 }
18430
52356b79 18431 fixup_called = 1;
72bf9492
DJ
18432}
18433
18a8505e
AT
18434/* Process the attributes that had to be skipped in the first round. These
18435 attributes are the ones that need str_offsets_base or addr_base attributes.
18436 They could not have been processed in the first round, because at the time
18437 the values of str_offsets_base or addr_base may not have been known. */
18438void read_attribute_reprocess (const struct die_reader_specs *reader,
18439 struct attribute *attr)
18440{
18441 struct dwarf2_cu *cu = reader->cu;
18442 switch (attr->form)
18443 {
18444 case DW_FORM_addrx:
18445 case DW_FORM_GNU_addr_index:
18446 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18447 break;
18448 case DW_FORM_strx:
18449 case DW_FORM_strx1:
18450 case DW_FORM_strx2:
18451 case DW_FORM_strx3:
18452 case DW_FORM_strx4:
18453 case DW_FORM_GNU_str_index:
18454 {
18455 unsigned int str_index = DW_UNSND (attr);
18456 if (reader->dwo_file != NULL)
18457 {
18458 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18459 DW_STRING_IS_CANONICAL (attr) = 0;
18460 }
18461 else
18462 {
18463 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18464 DW_STRING_IS_CANONICAL (attr) = 0;
18465 }
18466 break;
18467 }
18468 default:
18469 gdb_assert_not_reached (_("Unexpected DWARF form."));
18470 }
18471}
18472
a8329558 18473/* Read an attribute value described by an attribute form. */
c906108c 18474
d521ce57 18475static const gdb_byte *
dee91e82
DE
18476read_attribute_value (const struct die_reader_specs *reader,
18477 struct attribute *attr, unsigned form,
18a8505e
AT
18478 LONGEST implicit_const, const gdb_byte *info_ptr,
18479 bool *need_reprocess)
c906108c 18480{
dee91e82 18481 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18482 struct dwarf2_per_objfile *dwarf2_per_objfile
18483 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18484 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18485 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18486 bfd *abfd = reader->abfd;
e7c27a73 18487 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18488 unsigned int bytes_read;
18489 struct dwarf_block *blk;
18a8505e 18490 *need_reprocess = false;
c906108c 18491
aead7601 18492 attr->form = (enum dwarf_form) form;
a8329558 18493 switch (form)
c906108c 18494 {
c906108c 18495 case DW_FORM_ref_addr:
ae411497 18496 if (cu->header.version == 2)
c8a7a66f
TT
18497 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18498 &bytes_read);
ae411497 18499 else
8266302d
TT
18500 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18501 &bytes_read);
ae411497
TT
18502 info_ptr += bytes_read;
18503 break;
36586728 18504 case DW_FORM_GNU_ref_alt:
8266302d 18505 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18506 info_ptr += bytes_read;
18507 break;
ae411497 18508 case DW_FORM_addr:
c8a7a66f 18509 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18510 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18511 info_ptr += bytes_read;
c906108c
SS
18512 break;
18513 case DW_FORM_block2:
7b5a2f43 18514 blk = dwarf_alloc_block (cu);
c906108c
SS
18515 blk->size = read_2_bytes (abfd, info_ptr);
18516 info_ptr += 2;
18517 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18518 info_ptr += blk->size;
18519 DW_BLOCK (attr) = blk;
18520 break;
18521 case DW_FORM_block4:
7b5a2f43 18522 blk = dwarf_alloc_block (cu);
c906108c
SS
18523 blk->size = read_4_bytes (abfd, info_ptr);
18524 info_ptr += 4;
18525 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18526 info_ptr += blk->size;
18527 DW_BLOCK (attr) = blk;
18528 break;
18529 case DW_FORM_data2:
18530 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18531 info_ptr += 2;
18532 break;
18533 case DW_FORM_data4:
18534 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18535 info_ptr += 4;
18536 break;
18537 case DW_FORM_data8:
18538 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18539 info_ptr += 8;
18540 break;
0224619f
JK
18541 case DW_FORM_data16:
18542 blk = dwarf_alloc_block (cu);
18543 blk->size = 16;
18544 blk->data = read_n_bytes (abfd, info_ptr, 16);
18545 info_ptr += 16;
18546 DW_BLOCK (attr) = blk;
18547 break;
2dc7f7b3 18548 case DW_FORM_sec_offset:
8266302d 18549 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18550 info_ptr += bytes_read;
18551 break;
c906108c 18552 case DW_FORM_string:
9b1c24c8 18553 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18554 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18555 info_ptr += bytes_read;
18556 break;
4bdf3d34 18557 case DW_FORM_strp:
36586728
TT
18558 if (!cu->per_cu->is_dwz)
18559 {
ed2dc618
SM
18560 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18561 abfd, info_ptr, cu_header,
36586728
TT
18562 &bytes_read);
18563 DW_STRING_IS_CANONICAL (attr) = 0;
18564 info_ptr += bytes_read;
18565 break;
18566 }
18567 /* FALLTHROUGH */
43988095
JK
18568 case DW_FORM_line_strp:
18569 if (!cu->per_cu->is_dwz)
18570 {
ed2dc618
SM
18571 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18572 abfd, info_ptr,
43988095
JK
18573 cu_header, &bytes_read);
18574 DW_STRING_IS_CANONICAL (attr) = 0;
18575 info_ptr += bytes_read;
18576 break;
18577 }
18578 /* FALLTHROUGH */
36586728
TT
18579 case DW_FORM_GNU_strp_alt:
18580 {
ed2dc618 18581 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18582 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18583 &bytes_read);
36586728 18584
0314b390 18585 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
18586 DW_STRING_IS_CANONICAL (attr) = 0;
18587 info_ptr += bytes_read;
18588 }
4bdf3d34 18589 break;
2dc7f7b3 18590 case DW_FORM_exprloc:
c906108c 18591 case DW_FORM_block:
7b5a2f43 18592 blk = dwarf_alloc_block (cu);
c906108c
SS
18593 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18594 info_ptr += bytes_read;
18595 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18596 info_ptr += blk->size;
18597 DW_BLOCK (attr) = blk;
18598 break;
18599 case DW_FORM_block1:
7b5a2f43 18600 blk = dwarf_alloc_block (cu);
c906108c
SS
18601 blk->size = read_1_byte (abfd, info_ptr);
18602 info_ptr += 1;
18603 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18604 info_ptr += blk->size;
18605 DW_BLOCK (attr) = blk;
18606 break;
18607 case DW_FORM_data1:
18608 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18609 info_ptr += 1;
18610 break;
18611 case DW_FORM_flag:
18612 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18613 info_ptr += 1;
18614 break;
2dc7f7b3
TT
18615 case DW_FORM_flag_present:
18616 DW_UNSND (attr) = 1;
18617 break;
c906108c
SS
18618 case DW_FORM_sdata:
18619 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18620 info_ptr += bytes_read;
18621 break;
18622 case DW_FORM_udata:
18a8505e 18623 case DW_FORM_rnglistx:
c906108c
SS
18624 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18625 info_ptr += bytes_read;
18626 break;
18627 case DW_FORM_ref1:
9c541725 18628 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18629 + read_1_byte (abfd, info_ptr));
c906108c
SS
18630 info_ptr += 1;
18631 break;
18632 case DW_FORM_ref2:
9c541725 18633 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18634 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18635 info_ptr += 2;
18636 break;
18637 case DW_FORM_ref4:
9c541725 18638 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18639 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18640 info_ptr += 4;
18641 break;
613e1657 18642 case DW_FORM_ref8:
9c541725 18643 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18644 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18645 info_ptr += 8;
18646 break;
55f1336d 18647 case DW_FORM_ref_sig8:
ac9ec31b 18648 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18649 info_ptr += 8;
18650 break;
c906108c 18651 case DW_FORM_ref_udata:
9c541725 18652 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18653 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18654 info_ptr += bytes_read;
18655 break;
c906108c 18656 case DW_FORM_indirect:
a8329558
KW
18657 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18658 info_ptr += bytes_read;
43988095
JK
18659 if (form == DW_FORM_implicit_const)
18660 {
18661 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18662 info_ptr += bytes_read;
18663 }
18664 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18665 info_ptr, need_reprocess);
43988095
JK
18666 break;
18667 case DW_FORM_implicit_const:
18668 DW_SND (attr) = implicit_const;
a8329558 18669 break;
336d760d 18670 case DW_FORM_addrx:
3019eac3 18671 case DW_FORM_GNU_addr_index:
18a8505e
AT
18672 *need_reprocess = true;
18673 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18674 info_ptr += bytes_read;
18675 break;
cf532bd1 18676 case DW_FORM_strx:
15f18d14
AT
18677 case DW_FORM_strx1:
18678 case DW_FORM_strx2:
18679 case DW_FORM_strx3:
18680 case DW_FORM_strx4:
3019eac3 18681 case DW_FORM_GNU_str_index:
3019eac3 18682 {
15f18d14
AT
18683 ULONGEST str_index;
18684 if (form == DW_FORM_strx1)
18685 {
18686 str_index = read_1_byte (abfd, info_ptr);
18687 info_ptr += 1;
18688 }
18689 else if (form == DW_FORM_strx2)
18690 {
18691 str_index = read_2_bytes (abfd, info_ptr);
18692 info_ptr += 2;
18693 }
18694 else if (form == DW_FORM_strx3)
18695 {
18696 str_index = read_3_bytes (abfd, info_ptr);
18697 info_ptr += 3;
18698 }
18699 else if (form == DW_FORM_strx4)
18700 {
18701 str_index = read_4_bytes (abfd, info_ptr);
18702 info_ptr += 4;
18703 }
18704 else
18705 {
18706 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18707 info_ptr += bytes_read;
18708 }
18a8505e
AT
18709 *need_reprocess = true;
18710 DW_UNSND (attr) = str_index;
18711 }
3019eac3 18712 break;
c906108c 18713 default:
8a3fe4f8 18714 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18715 dwarf_form_name (form),
18716 bfd_get_filename (abfd));
c906108c 18717 }
28e94949 18718
36586728 18719 /* Super hack. */
cd6c91b4 18720 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18721 attr->form = DW_FORM_GNU_ref_alt;
18722
28e94949
JB
18723 /* We have seen instances where the compiler tried to emit a byte
18724 size attribute of -1 which ended up being encoded as an unsigned
18725 0xffffffff. Although 0xffffffff is technically a valid size value,
18726 an object of this size seems pretty unlikely so we can relatively
18727 safely treat these cases as if the size attribute was invalid and
18728 treat them as zero by default. */
18729 if (attr->name == DW_AT_byte_size
18730 && form == DW_FORM_data4
18731 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18732 {
18733 complaint
b98664d3 18734 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18735 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18736 DW_UNSND (attr) = 0;
18737 }
28e94949 18738
c906108c
SS
18739 return info_ptr;
18740}
18741
a8329558
KW
18742/* Read an attribute described by an abbreviated attribute. */
18743
d521ce57 18744static const gdb_byte *
dee91e82
DE
18745read_attribute (const struct die_reader_specs *reader,
18746 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18747 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18748{
18749 attr->name = abbrev->name;
43988095 18750 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18751 abbrev->implicit_const, info_ptr,
18752 need_reprocess);
a8329558
KW
18753}
18754
c764a876
DE
18755/* Cover function for read_initial_length.
18756 Returns the length of the object at BUF, and stores the size of the
18757 initial length in *BYTES_READ and stores the size that offsets will be in
18758 *OFFSET_SIZE.
18759 If the initial length size is not equivalent to that specified in
18760 CU_HEADER then issue a complaint.
18761 This is useful when reading non-comp-unit headers. */
dd373385 18762
c764a876 18763static LONGEST
d521ce57 18764read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18765 const struct comp_unit_head *cu_header,
18766 unsigned int *bytes_read,
18767 unsigned int *offset_size)
18768{
18769 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18770
18771 gdb_assert (cu_header->initial_length_size == 4
18772 || cu_header->initial_length_size == 8
18773 || cu_header->initial_length_size == 12);
18774
18775 if (cu_header->initial_length_size != *bytes_read)
b98664d3 18776 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18777
c764a876 18778 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18779 return length;
613e1657
KB
18780}
18781
43988095
JK
18782/* Return pointer to string at section SECT offset STR_OFFSET with error
18783 reporting strings FORM_NAME and SECT_NAME. */
18784
d521ce57 18785static const char *
ed2dc618
SM
18786read_indirect_string_at_offset_from (struct objfile *objfile,
18787 bfd *abfd, LONGEST str_offset,
43988095
JK
18788 struct dwarf2_section_info *sect,
18789 const char *form_name,
18790 const char *sect_name)
18791{
96b79293 18792 sect->read (objfile);
43988095
JK
18793 if (sect->buffer == NULL)
18794 error (_("%s used without %s section [in module %s]"),
18795 form_name, sect_name, bfd_get_filename (abfd));
18796 if (str_offset >= sect->size)
18797 error (_("%s pointing outside of %s section [in module %s]"),
18798 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 18799 gdb_assert (HOST_CHAR_BIT == 8);
43988095 18800 if (sect->buffer[str_offset] == '\0')
4bdf3d34 18801 return NULL;
43988095
JK
18802 return (const char *) (sect->buffer + str_offset);
18803}
18804
18805/* Return pointer to string at .debug_str offset STR_OFFSET. */
18806
18807static const char *
ed2dc618
SM
18808read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18809 bfd *abfd, LONGEST str_offset)
43988095 18810{
ed2dc618
SM
18811 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18812 abfd, str_offset,
43988095
JK
18813 &dwarf2_per_objfile->str,
18814 "DW_FORM_strp", ".debug_str");
18815}
18816
18817/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18818
18819static const char *
ed2dc618
SM
18820read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18821 bfd *abfd, LONGEST str_offset)
43988095 18822{
ed2dc618
SM
18823 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18824 abfd, str_offset,
43988095
JK
18825 &dwarf2_per_objfile->line_str,
18826 "DW_FORM_line_strp",
18827 ".debug_line_str");
c906108c
SS
18828}
18829
43988095
JK
18830/* Return pointer to string at .debug_str offset as read from BUF.
18831 BUF is assumed to be in a compilation unit described by CU_HEADER.
18832 Return *BYTES_READ_PTR count of bytes read from BUF. */
18833
d521ce57 18834static const char *
ed2dc618
SM
18835read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18836 const gdb_byte *buf,
cf2c3c16
TT
18837 const struct comp_unit_head *cu_header,
18838 unsigned int *bytes_read_ptr)
18839{
8266302d 18840 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18841
ed2dc618 18842 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
18843}
18844
43988095
JK
18845/* Return pointer to string at .debug_line_str offset as read from BUF.
18846 BUF is assumed to be in a compilation unit described by CU_HEADER.
18847 Return *BYTES_READ_PTR count of bytes read from BUF. */
18848
18849static const char *
ed2dc618
SM
18850read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18851 bfd *abfd, const gdb_byte *buf,
43988095
JK
18852 const struct comp_unit_head *cu_header,
18853 unsigned int *bytes_read_ptr)
18854{
8266302d 18855 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18856
ed2dc618
SM
18857 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18858 str_offset);
43988095
JK
18859}
18860
3019eac3 18861/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18862 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18863 ADDR_SIZE is the size of addresses from the CU header. */
18864
18865static CORE_ADDR
ed2dc618 18866read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18867 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18868 int addr_size)
3019eac3
DE
18869{
18870 struct objfile *objfile = dwarf2_per_objfile->objfile;
18871 bfd *abfd = objfile->obfd;
18872 const gdb_byte *info_ptr;
18a8505e 18873 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18874
96b79293 18875 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18876 if (dwarf2_per_objfile->addr.buffer == NULL)
18877 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18878 objfile_name (objfile));
18a8505e
AT
18879 if (addr_base_or_zero + addr_index * addr_size
18880 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18881 error (_("DW_FORM_addr_index pointing outside of "
18882 ".debug_addr section [in module %s]"),
4262abfb 18883 objfile_name (objfile));
3019eac3 18884 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18885 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18886 if (addr_size == 4)
18887 return bfd_get_32 (abfd, info_ptr);
18888 else
18889 return bfd_get_64 (abfd, info_ptr);
18890}
18891
18892/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18893
18894static CORE_ADDR
18895read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18896{
518817b3
SM
18897 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18898 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18899}
18900
18901/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18902
18903static CORE_ADDR
d521ce57 18904read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18905 unsigned int *bytes_read)
18906{
518817b3 18907 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18908 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18909
18910 return read_addr_index (cu, addr_index);
18911}
18912
450a1bfc 18913/* See read.h. */
3019eac3
DE
18914
18915CORE_ADDR
450a1bfc 18916dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18917{
ed2dc618 18918 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18919 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18920 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18921 int addr_size;
18922
3019eac3
DE
18923 /* We need addr_base and addr_size.
18924 If we don't have PER_CU->cu, we have to get it.
18925 Nasty, but the alternative is storing the needed info in PER_CU,
18926 which at this point doesn't seem justified: it's not clear how frequently
18927 it would get used and it would increase the size of every PER_CU.
18928 Entry points like dwarf2_per_cu_addr_size do a similar thing
18929 so we're not in uncharted territory here.
18930 Alas we need to be a bit more complicated as addr_base is contained
18931 in the DIE.
18932
18933 We don't need to read the entire CU(/TU).
18934 We just need the header and top level die.
a1b64ce1 18935
3019eac3 18936 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18937 For now we skip this optimization. */
3019eac3
DE
18938
18939 if (cu != NULL)
18940 {
18941 addr_base = cu->addr_base;
18942 addr_size = cu->header.addr_size;
18943 }
18944 else
18945 {
6751ebae 18946 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18947 addr_base = reader.cu->addr_base;
18948 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18949 }
18950
ed2dc618
SM
18951 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18952 addr_size);
3019eac3
DE
18953}
18954
18a8505e
AT
18955/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18956 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18957 DWO file. */
3019eac3 18958
d521ce57 18959static const char *
18a8505e
AT
18960read_str_index (struct dwarf2_cu *cu,
18961 struct dwarf2_section_info *str_section,
18962 struct dwarf2_section_info *str_offsets_section,
18963 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18964{
518817b3
SM
18965 struct dwarf2_per_objfile *dwarf2_per_objfile
18966 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18967 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18968 const char *objf_name = objfile_name (objfile);
3019eac3 18969 bfd *abfd = objfile->obfd;
d521ce57 18970 const gdb_byte *info_ptr;
3019eac3 18971 ULONGEST str_offset;
cf532bd1 18972 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18973
96b79293
TT
18974 str_section->read (objfile);
18975 str_offsets_section->read (objfile);
73869dc2 18976 if (str_section->buffer == NULL)
18a8505e 18977 error (_("%s used without %s section"
9d8780f0 18978 " in CU at offset %s [in module %s]"),
96b79293 18979 form_name, str_section->get_name (),
18a8505e 18980 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18981 if (str_offsets_section->buffer == NULL)
18a8505e 18982 error (_("%s used without %s section"
9d8780f0 18983 " in CU at offset %s [in module %s]"),
96b79293 18984 form_name, str_section->get_name (),
18a8505e 18985 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18986 info_ptr = (str_offsets_section->buffer
18a8505e 18987 + str_offsets_base
3019eac3
DE
18988 + str_index * cu->header.offset_size);
18989 if (cu->header.offset_size == 4)
18990 str_offset = bfd_get_32 (abfd, info_ptr);
18991 else
18992 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18993 if (str_offset >= str_section->size)
57d63ce2 18994 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18995 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18996 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18997 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18998}
18999
18a8505e
AT
19000/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19001
19002static const char *
19003read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19004{
19005 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19006 ? reader->cu->header.addr_size : 0;
19007 return read_str_index (reader->cu,
19008 &reader->dwo_file->sections.str,
19009 &reader->dwo_file->sections.str_offsets,
19010 str_offsets_base, str_index);
19011}
19012
19013/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19014
19015static const char *
19016read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19017{
19018 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19019 const char *objf_name = objfile_name (objfile);
19020 static const char form_name[] = "DW_FORM_GNU_str_index";
19021 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19022
19023 if (!cu->str_offsets_base.has_value ())
19024 error (_("%s used in Fission stub without %s"
19025 " in CU at offset 0x%lx [in module %s]"),
19026 form_name, str_offsets_attr_name,
19027 (long) cu->header.offset_size, objf_name);
19028
19029 return read_str_index (cu,
19030 &cu->per_cu->dwarf2_per_objfile->str,
19031 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19032 *cu->str_offsets_base, str_index);
19033}
19034
3019eac3
DE
19035/* Return the length of an LEB128 number in BUF. */
19036
19037static int
19038leb128_size (const gdb_byte *buf)
19039{
19040 const gdb_byte *begin = buf;
19041 gdb_byte byte;
19042
19043 while (1)
19044 {
19045 byte = *buf++;
19046 if ((byte & 128) == 0)
19047 return buf - begin;
19048 }
19049}
19050
c906108c 19051static void
e142c38c 19052set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19053{
19054 switch (lang)
19055 {
19056 case DW_LANG_C89:
76bee0cc 19057 case DW_LANG_C99:
0cfd832f 19058 case DW_LANG_C11:
c906108c 19059 case DW_LANG_C:
d1be3247 19060 case DW_LANG_UPC:
e142c38c 19061 cu->language = language_c;
c906108c 19062 break;
9c37b5ae 19063 case DW_LANG_Java:
c906108c 19064 case DW_LANG_C_plus_plus:
0cfd832f
MW
19065 case DW_LANG_C_plus_plus_11:
19066 case DW_LANG_C_plus_plus_14:
e142c38c 19067 cu->language = language_cplus;
c906108c 19068 break;
6aecb9c2
JB
19069 case DW_LANG_D:
19070 cu->language = language_d;
19071 break;
c906108c
SS
19072 case DW_LANG_Fortran77:
19073 case DW_LANG_Fortran90:
b21b22e0 19074 case DW_LANG_Fortran95:
f7de9aab
MW
19075 case DW_LANG_Fortran03:
19076 case DW_LANG_Fortran08:
e142c38c 19077 cu->language = language_fortran;
c906108c 19078 break;
a766d390
DE
19079 case DW_LANG_Go:
19080 cu->language = language_go;
19081 break;
c906108c 19082 case DW_LANG_Mips_Assembler:
e142c38c 19083 cu->language = language_asm;
c906108c
SS
19084 break;
19085 case DW_LANG_Ada83:
8aaf0b47 19086 case DW_LANG_Ada95:
bc5f45f8
JB
19087 cu->language = language_ada;
19088 break;
72019c9c
GM
19089 case DW_LANG_Modula2:
19090 cu->language = language_m2;
19091 break;
fe8e67fd
PM
19092 case DW_LANG_Pascal83:
19093 cu->language = language_pascal;
19094 break;
22566fbd
DJ
19095 case DW_LANG_ObjC:
19096 cu->language = language_objc;
19097 break;
c44af4eb
TT
19098 case DW_LANG_Rust:
19099 case DW_LANG_Rust_old:
19100 cu->language = language_rust;
19101 break;
c906108c
SS
19102 case DW_LANG_Cobol74:
19103 case DW_LANG_Cobol85:
c906108c 19104 default:
e142c38c 19105 cu->language = language_minimal;
c906108c
SS
19106 break;
19107 }
e142c38c 19108 cu->language_defn = language_def (cu->language);
c906108c
SS
19109}
19110
19111/* Return the named attribute or NULL if not there. */
19112
19113static struct attribute *
e142c38c 19114dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19115{
a48e046c 19116 for (;;)
c906108c 19117 {
a48e046c
TT
19118 unsigned int i;
19119 struct attribute *spec = NULL;
19120
19121 for (i = 0; i < die->num_attrs; ++i)
19122 {
19123 if (die->attrs[i].name == name)
19124 return &die->attrs[i];
19125 if (die->attrs[i].name == DW_AT_specification
19126 || die->attrs[i].name == DW_AT_abstract_origin)
19127 spec = &die->attrs[i];
19128 }
19129
19130 if (!spec)
19131 break;
c906108c 19132
f2f0e013 19133 die = follow_die_ref (die, spec, &cu);
f2f0e013 19134 }
c5aa993b 19135
c906108c
SS
19136 return NULL;
19137}
19138
348e048f
DE
19139/* Return the named attribute or NULL if not there,
19140 but do not follow DW_AT_specification, etc.
19141 This is for use in contexts where we're reading .debug_types dies.
19142 Following DW_AT_specification, DW_AT_abstract_origin will take us
19143 back up the chain, and we want to go down. */
19144
19145static struct attribute *
45e58e77 19146dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19147{
19148 unsigned int i;
19149
19150 for (i = 0; i < die->num_attrs; ++i)
19151 if (die->attrs[i].name == name)
19152 return &die->attrs[i];
19153
19154 return NULL;
19155}
19156
7d45c7c3
KB
19157/* Return the string associated with a string-typed attribute, or NULL if it
19158 is either not found or is of an incorrect type. */
19159
19160static const char *
19161dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19162{
19163 struct attribute *attr;
19164 const char *str = NULL;
19165
19166 attr = dwarf2_attr (die, name, cu);
19167
19168 if (attr != NULL)
19169 {
43988095 19170 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19171 || attr->form == DW_FORM_string
cf532bd1 19172 || attr->form == DW_FORM_strx
8fe0f950
AT
19173 || attr->form == DW_FORM_strx1
19174 || attr->form == DW_FORM_strx2
19175 || attr->form == DW_FORM_strx3
19176 || attr->form == DW_FORM_strx4
b3340438 19177 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19178 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19179 str = DW_STRING (attr);
19180 else
b98664d3 19181 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19182 "DIE at %s in module %s"),
19183 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19184 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19185 }
19186
19187 return str;
19188}
19189
a084a2a6 19190/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19191 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19192static const char *
19193dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19194{
19195 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19196 if (dwo_name == nullptr)
19197 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19198 return dwo_name;
19199}
19200
05cf31d1
JB
19201/* Return non-zero iff the attribute NAME is defined for the given DIE,
19202 and holds a non-zero value. This function should only be used for
2dc7f7b3 19203 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19204
19205static int
19206dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19207{
19208 struct attribute *attr = dwarf2_attr (die, name, cu);
19209
19210 return (attr && DW_UNSND (attr));
19211}
19212
3ca72b44 19213static int
e142c38c 19214die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19215{
05cf31d1
JB
19216 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19217 which value is non-zero. However, we have to be careful with
19218 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19219 (via dwarf2_flag_true_p) follows this attribute. So we may
19220 end up accidently finding a declaration attribute that belongs
19221 to a different DIE referenced by the specification attribute,
19222 even though the given DIE does not have a declaration attribute. */
19223 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19224 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19225}
19226
63d06c5c 19227/* Return the die giving the specification for DIE, if there is
f2f0e013 19228 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19229 containing the return value on output. If there is no
19230 specification, but there is an abstract origin, that is
19231 returned. */
63d06c5c
DC
19232
19233static struct die_info *
f2f0e013 19234die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19235{
f2f0e013
DJ
19236 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19237 *spec_cu);
63d06c5c 19238
edb3359d
DJ
19239 if (spec_attr == NULL)
19240 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19241
63d06c5c
DC
19242 if (spec_attr == NULL)
19243 return NULL;
19244 else
f2f0e013 19245 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19246}
c906108c 19247
527f3840
JK
19248/* Stub for free_line_header to match void * callback types. */
19249
19250static void
19251free_line_header_voidp (void *arg)
19252{
9a3c8263 19253 struct line_header *lh = (struct line_header *) arg;
527f3840 19254
fff8551c 19255 delete lh;
527f3840
JK
19256}
19257
83769d0b 19258/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19259
19260static struct dwarf2_section_info *
19261get_debug_line_section (struct dwarf2_cu *cu)
19262{
19263 struct dwarf2_section_info *section;
518817b3
SM
19264 struct dwarf2_per_objfile *dwarf2_per_objfile
19265 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19266
19267 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19268 DWO file. */
19269 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19270 section = &cu->dwo_unit->dwo_file->sections.line;
19271 else if (cu->per_cu->is_dwz)
19272 {
ed2dc618 19273 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19274
19275 section = &dwz->line;
19276 }
19277 else
19278 section = &dwarf2_per_objfile->line;
19279
19280 return section;
19281}
19282
43988095
JK
19283/* Read directory or file name entry format, starting with byte of
19284 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19285 entries count and the entries themselves in the described entry
19286 format. */
19287
19288static void
ed2dc618
SM
19289read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19290 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19291 struct line_header *lh,
19292 const struct comp_unit_head *cu_header,
19293 void (*callback) (struct line_header *lh,
19294 const char *name,
ecfb656c 19295 dir_index d_index,
43988095
JK
19296 unsigned int mod_time,
19297 unsigned int length))
19298{
19299 gdb_byte format_count, formati;
19300 ULONGEST data_count, datai;
19301 const gdb_byte *buf = *bufp;
19302 const gdb_byte *format_header_data;
43988095
JK
19303 unsigned int bytes_read;
19304
19305 format_count = read_1_byte (abfd, buf);
19306 buf += 1;
19307 format_header_data = buf;
19308 for (formati = 0; formati < format_count; formati++)
19309 {
19310 read_unsigned_leb128 (abfd, buf, &bytes_read);
19311 buf += bytes_read;
19312 read_unsigned_leb128 (abfd, buf, &bytes_read);
19313 buf += bytes_read;
19314 }
19315
19316 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19317 buf += bytes_read;
19318 for (datai = 0; datai < data_count; datai++)
19319 {
19320 const gdb_byte *format = format_header_data;
19321 struct file_entry fe;
19322
43988095
JK
19323 for (formati = 0; formati < format_count; formati++)
19324 {
ecfb656c 19325 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19326 format += bytes_read;
43988095 19327
ecfb656c 19328 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19329 format += bytes_read;
ecfb656c
PA
19330
19331 gdb::optional<const char *> string;
19332 gdb::optional<unsigned int> uint;
19333
43988095
JK
19334 switch (form)
19335 {
19336 case DW_FORM_string:
ecfb656c 19337 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19338 buf += bytes_read;
19339 break;
19340
19341 case DW_FORM_line_strp:
ed2dc618
SM
19342 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19343 abfd, buf,
ecfb656c
PA
19344 cu_header,
19345 &bytes_read));
43988095
JK
19346 buf += bytes_read;
19347 break;
19348
19349 case DW_FORM_data1:
ecfb656c 19350 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19351 buf += 1;
19352 break;
19353
19354 case DW_FORM_data2:
ecfb656c 19355 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19356 buf += 2;
19357 break;
19358
19359 case DW_FORM_data4:
ecfb656c 19360 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19361 buf += 4;
19362 break;
19363
19364 case DW_FORM_data8:
ecfb656c 19365 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19366 buf += 8;
19367 break;
19368
7ba99d21
AT
19369 case DW_FORM_data16:
19370 /* This is used for MD5, but file_entry does not record MD5s. */
19371 buf += 16;
19372 break;
19373
43988095 19374 case DW_FORM_udata:
ecfb656c 19375 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19376 buf += bytes_read;
19377 break;
19378
19379 case DW_FORM_block:
19380 /* It is valid only for DW_LNCT_timestamp which is ignored by
19381 current GDB. */
19382 break;
19383 }
ecfb656c
PA
19384
19385 switch (content_type)
19386 {
19387 case DW_LNCT_path:
19388 if (string.has_value ())
19389 fe.name = *string;
19390 break;
19391 case DW_LNCT_directory_index:
19392 if (uint.has_value ())
19393 fe.d_index = (dir_index) *uint;
19394 break;
19395 case DW_LNCT_timestamp:
19396 if (uint.has_value ())
19397 fe.mod_time = *uint;
19398 break;
19399 case DW_LNCT_size:
19400 if (uint.has_value ())
19401 fe.length = *uint;
19402 break;
19403 case DW_LNCT_MD5:
19404 break;
19405 default:
b98664d3 19406 complaint (_("Unknown format content type %s"),
ecfb656c
PA
19407 pulongest (content_type));
19408 }
43988095
JK
19409 }
19410
ecfb656c 19411 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
19412 }
19413
19414 *bufp = buf;
19415}
19416
debd256d 19417/* Read the statement program header starting at OFFSET in
3019eac3 19418 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19419 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19420 Returns NULL if there is a problem reading the header, e.g., if it
19421 has a version we don't understand.
debd256d
JB
19422
19423 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19424 the returned object point into the dwarf line section buffer,
19425 and must not be freed. */
ae2de4f8 19426
fff8551c 19427static line_header_up
9c541725 19428dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19429{
d521ce57 19430 const gdb_byte *line_ptr;
c764a876 19431 unsigned int bytes_read, offset_size;
debd256d 19432 int i;
d521ce57 19433 const char *cur_dir, *cur_file;
3019eac3
DE
19434 struct dwarf2_section_info *section;
19435 bfd *abfd;
518817b3
SM
19436 struct dwarf2_per_objfile *dwarf2_per_objfile
19437 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19438
36586728 19439 section = get_debug_line_section (cu);
96b79293 19440 section->read (dwarf2_per_objfile->objfile);
3019eac3 19441 if (section->buffer == NULL)
debd256d 19442 {
3019eac3 19443 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19444 complaint (_("missing .debug_line.dwo section"));
3019eac3 19445 else
b98664d3 19446 complaint (_("missing .debug_line section"));
debd256d
JB
19447 return 0;
19448 }
19449
fceca515
DE
19450 /* We can't do this until we know the section is non-empty.
19451 Only then do we know we have such a section. */
96b79293 19452 abfd = section->get_bfd_owner ();
fceca515 19453
a738430d
MK
19454 /* Make sure that at least there's room for the total_length field.
19455 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 19456 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 19457 {
4d3c2250 19458 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19459 return 0;
19460 }
19461
fff8551c 19462 line_header_up lh (new line_header ());
debd256d 19463
9c541725 19464 lh->sect_off = sect_off;
527f3840
JK
19465 lh->offset_in_dwz = cu->per_cu->is_dwz;
19466
9c541725 19467 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 19468
a738430d 19469 /* Read in the header. */
6e70227d 19470 lh->total_length =
c764a876
DE
19471 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19472 &bytes_read, &offset_size);
debd256d 19473 line_ptr += bytes_read;
7ba99d21
AT
19474
19475 const gdb_byte *start_here = line_ptr;
19476
3019eac3 19477 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 19478 {
4d3c2250 19479 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19480 return 0;
19481 }
7ba99d21 19482 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
19483 lh->version = read_2_bytes (abfd, line_ptr);
19484 line_ptr += 2;
43988095 19485 if (lh->version > 5)
cd366ee8
DE
19486 {
19487 /* This is a version we don't understand. The format could have
19488 changed in ways we don't handle properly so just punt. */
b98664d3 19489 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
19490 return NULL;
19491 }
43988095
JK
19492 if (lh->version >= 5)
19493 {
19494 gdb_byte segment_selector_size;
19495
19496 /* Skip address size. */
19497 read_1_byte (abfd, line_ptr);
19498 line_ptr += 1;
19499
19500 segment_selector_size = read_1_byte (abfd, line_ptr);
19501 line_ptr += 1;
19502 if (segment_selector_size != 0)
19503 {
b98664d3 19504 complaint (_("unsupported segment selector size %u "
43988095
JK
19505 "in .debug_line section"),
19506 segment_selector_size);
19507 return NULL;
19508 }
19509 }
24aa364d 19510 lh->header_length = read_offset (abfd, line_ptr, offset_size);
c764a876 19511 line_ptr += offset_size;
7ba99d21 19512 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
19513 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19514 line_ptr += 1;
2dc7f7b3
TT
19515 if (lh->version >= 4)
19516 {
19517 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19518 line_ptr += 1;
19519 }
19520 else
19521 lh->maximum_ops_per_instruction = 1;
19522
19523 if (lh->maximum_ops_per_instruction == 0)
19524 {
19525 lh->maximum_ops_per_instruction = 1;
b98664d3 19526 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 19527 "in `.debug_line' section"));
2dc7f7b3
TT
19528 }
19529
debd256d
JB
19530 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19531 line_ptr += 1;
19532 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19533 line_ptr += 1;
19534 lh->line_range = read_1_byte (abfd, line_ptr);
19535 line_ptr += 1;
19536 lh->opcode_base = read_1_byte (abfd, line_ptr);
19537 line_ptr += 1;
fff8551c 19538 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
19539
19540 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19541 for (i = 1; i < lh->opcode_base; ++i)
19542 {
19543 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19544 line_ptr += 1;
19545 }
19546
43988095 19547 if (lh->version >= 5)
debd256d 19548 {
43988095 19549 /* Read directory table. */
ed2dc618
SM
19550 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19551 &cu->header,
b926417a 19552 [] (struct line_header *header, const char *name,
ecfb656c 19553 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19554 unsigned int length)
19555 {
b926417a 19556 header->add_include_dir (name);
fff8551c 19557 });
debd256d 19558
43988095 19559 /* Read file name table. */
ed2dc618
SM
19560 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19561 &cu->header,
b926417a 19562 [] (struct line_header *header, const char *name,
ecfb656c 19563 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19564 unsigned int length)
19565 {
b926417a 19566 header->add_file_name (name, d_index, mod_time, length);
fff8551c 19567 });
43988095
JK
19568 }
19569 else
debd256d 19570 {
43988095
JK
19571 /* Read directory table. */
19572 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19573 {
19574 line_ptr += bytes_read;
fff8551c 19575 lh->add_include_dir (cur_dir);
43988095 19576 }
debd256d
JB
19577 line_ptr += bytes_read;
19578
43988095
JK
19579 /* Read file name table. */
19580 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19581 {
ecfb656c
PA
19582 unsigned int mod_time, length;
19583 dir_index d_index;
43988095
JK
19584
19585 line_ptr += bytes_read;
ecfb656c 19586 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
19587 line_ptr += bytes_read;
19588 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19589 line_ptr += bytes_read;
19590 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19591 line_ptr += bytes_read;
19592
ecfb656c 19593 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
19594 }
19595 line_ptr += bytes_read;
debd256d 19596 }
debd256d 19597
3019eac3 19598 if (line_ptr > (section->buffer + section->size))
b98664d3 19599 complaint (_("line number info header doesn't "
3e43a32a 19600 "fit in `.debug_line' section"));
debd256d 19601
debd256d
JB
19602 return lh;
19603}
c906108c 19604
c6da4cef 19605/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19606 Return the file name of the psymtab for the given file_entry.
c6da4cef 19607 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19608 If space for the result is malloc'd, *NAME_HOLDER will be set.
19609 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19610
d521ce57 19611static const char *
7ba99d21 19612psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19613 const dwarf2_psymtab *pst,
c89b44cd
TT
19614 const char *comp_dir,
19615 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19616{
d521ce57
TT
19617 const char *include_name = fe.name;
19618 const char *include_name_to_compare = include_name;
72b9f47f 19619 const char *pst_filename;
c6da4cef
DE
19620 int file_is_pst;
19621
8c43009f 19622 const char *dir_name = fe.include_dir (lh);
c6da4cef 19623
c89b44cd 19624 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19625 if (!IS_ABSOLUTE_PATH (include_name)
19626 && (dir_name != NULL || comp_dir != NULL))
19627 {
19628 /* Avoid creating a duplicate psymtab for PST.
19629 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19630 Before we do the comparison, however, we need to account
19631 for DIR_NAME and COMP_DIR.
19632 First prepend dir_name (if non-NULL). If we still don't
19633 have an absolute path prepend comp_dir (if non-NULL).
19634 However, the directory we record in the include-file's
19635 psymtab does not contain COMP_DIR (to match the
19636 corresponding symtab(s)).
19637
19638 Example:
19639
19640 bash$ cd /tmp
19641 bash$ gcc -g ./hello.c
19642 include_name = "hello.c"
19643 dir_name = "."
19644 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19645 DW_AT_name = "./hello.c"
19646
19647 */
c6da4cef
DE
19648
19649 if (dir_name != NULL)
19650 {
c89b44cd
TT
19651 name_holder->reset (concat (dir_name, SLASH_STRING,
19652 include_name, (char *) NULL));
19653 include_name = name_holder->get ();
c6da4cef 19654 include_name_to_compare = include_name;
c6da4cef
DE
19655 }
19656 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19657 {
c89b44cd
TT
19658 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19659 include_name, (char *) NULL));
19660 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19661 }
19662 }
19663
19664 pst_filename = pst->filename;
c89b44cd 19665 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19666 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19667 {
c89b44cd
TT
19668 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19669 pst_filename, (char *) NULL));
19670 pst_filename = copied_name.get ();
c6da4cef
DE
19671 }
19672
1e3fad37 19673 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19674
c6da4cef
DE
19675 if (file_is_pst)
19676 return NULL;
19677 return include_name;
19678}
19679
d9b3de22
DE
19680/* State machine to track the state of the line number program. */
19681
6f77053d 19682class lnp_state_machine
d9b3de22 19683{
6f77053d
PA
19684public:
19685 /* Initialize a machine state for the start of a line number
19686 program. */
804d2729
TT
19687 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19688 bool record_lines_p);
6f77053d 19689
8c43009f
PA
19690 file_entry *current_file ()
19691 {
19692 /* lh->file_names is 0-based, but the file name numbers in the
19693 statement program are 1-based. */
6f77053d
PA
19694 return m_line_header->file_name_at (m_file);
19695 }
19696
19697 /* Record the line in the state machine. END_SEQUENCE is true if
19698 we're processing the end of a sequence. */
19699 void record_line (bool end_sequence);
19700
7ab6656f
OJ
19701 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19702 nop-out rest of the lines in this sequence. */
6f77053d
PA
19703 void check_line_address (struct dwarf2_cu *cu,
19704 const gdb_byte *line_ptr,
7ab6656f 19705 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19706
19707 void handle_set_discriminator (unsigned int discriminator)
19708 {
19709 m_discriminator = discriminator;
19710 m_line_has_non_zero_discriminator |= discriminator != 0;
19711 }
19712
19713 /* Handle DW_LNE_set_address. */
19714 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19715 {
19716 m_op_index = 0;
19717 address += baseaddr;
19718 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19719 }
19720
19721 /* Handle DW_LNS_advance_pc. */
19722 void handle_advance_pc (CORE_ADDR adjust);
19723
19724 /* Handle a special opcode. */
19725 void handle_special_opcode (unsigned char op_code);
19726
19727 /* Handle DW_LNS_advance_line. */
19728 void handle_advance_line (int line_delta)
19729 {
19730 advance_line (line_delta);
19731 }
19732
19733 /* Handle DW_LNS_set_file. */
19734 void handle_set_file (file_name_index file);
19735
19736 /* Handle DW_LNS_negate_stmt. */
19737 void handle_negate_stmt ()
19738 {
19739 m_is_stmt = !m_is_stmt;
19740 }
19741
19742 /* Handle DW_LNS_const_add_pc. */
19743 void handle_const_add_pc ();
19744
19745 /* Handle DW_LNS_fixed_advance_pc. */
19746 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19747 {
19748 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19749 m_op_index = 0;
19750 }
19751
19752 /* Handle DW_LNS_copy. */
19753 void handle_copy ()
19754 {
19755 record_line (false);
19756 m_discriminator = 0;
19757 }
19758
19759 /* Handle DW_LNE_end_sequence. */
19760 void handle_end_sequence ()
19761 {
804d2729 19762 m_currently_recording_lines = true;
6f77053d
PA
19763 }
19764
19765private:
19766 /* Advance the line by LINE_DELTA. */
19767 void advance_line (int line_delta)
19768 {
19769 m_line += line_delta;
19770
19771 if (line_delta != 0)
19772 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19773 }
19774
804d2729
TT
19775 struct dwarf2_cu *m_cu;
19776
6f77053d
PA
19777 gdbarch *m_gdbarch;
19778
19779 /* True if we're recording lines.
19780 Otherwise we're building partial symtabs and are just interested in
19781 finding include files mentioned by the line number program. */
19782 bool m_record_lines_p;
19783
8c43009f 19784 /* The line number header. */
6f77053d 19785 line_header *m_line_header;
8c43009f 19786
6f77053d
PA
19787 /* These are part of the standard DWARF line number state machine,
19788 and initialized according to the DWARF spec. */
d9b3de22 19789
6f77053d 19790 unsigned char m_op_index = 0;
7ba99d21
AT
19791 /* The line table index of the current file. */
19792 file_name_index m_file = 1;
6f77053d
PA
19793 unsigned int m_line = 1;
19794
19795 /* These are initialized in the constructor. */
19796
19797 CORE_ADDR m_address;
19798 bool m_is_stmt;
19799 unsigned int m_discriminator;
d9b3de22
DE
19800
19801 /* Additional bits of state we need to track. */
19802
19803 /* The last file that we called dwarf2_start_subfile for.
19804 This is only used for TLLs. */
6f77053d 19805 unsigned int m_last_file = 0;
d9b3de22 19806 /* The last file a line number was recorded for. */
6f77053d 19807 struct subfile *m_last_subfile = NULL;
d9b3de22 19808
804d2729
TT
19809 /* When true, record the lines we decode. */
19810 bool m_currently_recording_lines = false;
d9b3de22
DE
19811
19812 /* The last line number that was recorded, used to coalesce
19813 consecutive entries for the same line. This can happen, for
19814 example, when discriminators are present. PR 17276. */
6f77053d
PA
19815 unsigned int m_last_line = 0;
19816 bool m_line_has_non_zero_discriminator = false;
8c43009f 19817};
d9b3de22 19818
6f77053d
PA
19819void
19820lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19821{
19822 CORE_ADDR addr_adj = (((m_op_index + adjust)
19823 / m_line_header->maximum_ops_per_instruction)
19824 * m_line_header->minimum_instruction_length);
19825 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19826 m_op_index = ((m_op_index + adjust)
19827 % m_line_header->maximum_ops_per_instruction);
19828}
d9b3de22 19829
6f77053d
PA
19830void
19831lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19832{
6f77053d 19833 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19834 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19835 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19836 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19837 / m_line_header->maximum_ops_per_instruction)
19838 * m_line_header->minimum_instruction_length);
19839 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19840 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19841 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19842
258bf0ee 19843 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19844 advance_line (line_delta);
19845 record_line (false);
19846 m_discriminator = 0;
19847}
d9b3de22 19848
6f77053d
PA
19849void
19850lnp_state_machine::handle_set_file (file_name_index file)
19851{
19852 m_file = file;
19853
19854 const file_entry *fe = current_file ();
19855 if (fe == NULL)
19856 dwarf2_debug_line_missing_file_complaint ();
19857 else if (m_record_lines_p)
19858 {
19859 const char *dir = fe->include_dir (m_line_header);
19860
c24bdb02 19861 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19862 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19863 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19864 }
19865}
19866
19867void
19868lnp_state_machine::handle_const_add_pc ()
19869{
19870 CORE_ADDR adjust
19871 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19872
19873 CORE_ADDR addr_adj
19874 = (((m_op_index + adjust)
19875 / m_line_header->maximum_ops_per_instruction)
19876 * m_line_header->minimum_instruction_length);
19877
19878 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19879 m_op_index = ((m_op_index + adjust)
19880 % m_line_header->maximum_ops_per_instruction);
19881}
d9b3de22 19882
a05a36a5
DE
19883/* Return non-zero if we should add LINE to the line number table.
19884 LINE is the line to add, LAST_LINE is the last line that was added,
19885 LAST_SUBFILE is the subfile for LAST_LINE.
19886 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19887 had a non-zero discriminator.
19888
19889 We have to be careful in the presence of discriminators.
19890 E.g., for this line:
19891
19892 for (i = 0; i < 100000; i++);
19893
19894 clang can emit four line number entries for that one line,
19895 each with a different discriminator.
19896 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19897
19898 However, we want gdb to coalesce all four entries into one.
19899 Otherwise the user could stepi into the middle of the line and
19900 gdb would get confused about whether the pc really was in the
19901 middle of the line.
19902
19903 Things are further complicated by the fact that two consecutive
19904 line number entries for the same line is a heuristic used by gcc
19905 to denote the end of the prologue. So we can't just discard duplicate
19906 entries, we have to be selective about it. The heuristic we use is
19907 that we only collapse consecutive entries for the same line if at least
19908 one of those entries has a non-zero discriminator. PR 17276.
19909
19910 Note: Addresses in the line number state machine can never go backwards
19911 within one sequence, thus this coalescing is ok. */
19912
19913static int
804d2729
TT
19914dwarf_record_line_p (struct dwarf2_cu *cu,
19915 unsigned int line, unsigned int last_line,
a05a36a5
DE
19916 int line_has_non_zero_discriminator,
19917 struct subfile *last_subfile)
19918{
c24bdb02 19919 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19920 return 1;
19921 if (line != last_line)
19922 return 1;
19923 /* Same line for the same file that we've seen already.
19924 As a last check, for pr 17276, only record the line if the line
19925 has never had a non-zero discriminator. */
19926 if (!line_has_non_zero_discriminator)
19927 return 1;
19928 return 0;
19929}
19930
804d2729
TT
19931/* Use the CU's builder to record line number LINE beginning at
19932 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19933
19934static void
d9b3de22 19935dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 19936 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 19937 struct dwarf2_cu *cu)
252a6764
DE
19938{
19939 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19940
27e0867f
DE
19941 if (dwarf_line_debug)
19942 {
19943 fprintf_unfiltered (gdb_stdlog,
19944 "Recording line %u, file %s, address %s\n",
19945 line, lbasename (subfile->name),
19946 paddress (gdbarch, address));
19947 }
19948
804d2729 19949 if (cu != nullptr)
8c95582d 19950 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
19951}
19952
19953/* Subroutine of dwarf_decode_lines_1 to simplify it.
19954 Mark the end of a set of line number records.
d9b3de22 19955 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19956 If SUBFILE is NULL the request is ignored. */
19957
19958static void
19959dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19960 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19961{
27e0867f
DE
19962 if (subfile == NULL)
19963 return;
19964
19965 if (dwarf_line_debug)
19966 {
19967 fprintf_unfiltered (gdb_stdlog,
19968 "Finishing current line, file %s, address %s\n",
19969 lbasename (subfile->name),
19970 paddress (gdbarch, address));
19971 }
19972
8c95582d 19973 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
19974}
19975
6f77053d
PA
19976void
19977lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19978{
d9b3de22
DE
19979 if (dwarf_line_debug)
19980 {
19981 fprintf_unfiltered (gdb_stdlog,
19982 "Processing actual line %u: file %u,"
94a72be7 19983 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19984 m_line, m_file,
6f77053d 19985 paddress (m_gdbarch, m_address),
94a72be7
AB
19986 m_is_stmt, m_discriminator,
19987 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19988 }
19989
6f77053d 19990 file_entry *fe = current_file ();
8c43009f
PA
19991
19992 if (fe == NULL)
d9b3de22
DE
19993 dwarf2_debug_line_missing_file_complaint ();
19994 /* For now we ignore lines not starting on an instruction boundary.
19995 But not when processing end_sequence for compatibility with the
19996 previous version of the code. */
6f77053d 19997 else if (m_op_index == 0 || end_sequence)
d9b3de22 19998 {
8c43009f 19999 fe->included_p = 1;
8c95582d 20000 if (m_record_lines_p)
d9b3de22 20001 {
c24bdb02 20002 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20003 || end_sequence)
d9b3de22 20004 {
804d2729
TT
20005 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20006 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20007 }
20008
20009 if (!end_sequence)
20010 {
8c95582d
AB
20011 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20012
804d2729 20013 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20014 m_line_has_non_zero_discriminator,
20015 m_last_subfile))
d9b3de22 20016 {
c24bdb02 20017 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20018 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20019 builder->get_current_subfile (),
8c95582d 20020 m_line, m_address, is_stmt,
804d2729 20021 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20022 }
c24bdb02 20023 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20024 m_last_line = m_line;
d9b3de22
DE
20025 }
20026 }
20027 }
20028}
20029
804d2729
TT
20030lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20031 line_header *lh, bool record_lines_p)
d9b3de22 20032{
804d2729 20033 m_cu = cu;
6f77053d
PA
20034 m_gdbarch = arch;
20035 m_record_lines_p = record_lines_p;
20036 m_line_header = lh;
d9b3de22 20037
804d2729 20038 m_currently_recording_lines = true;
d9b3de22 20039
d9b3de22
DE
20040 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20041 was a line entry for it so that the backend has a chance to adjust it
20042 and also record it in case it needs it. This is currently used by MIPS
20043 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20044 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20045 m_is_stmt = lh->default_is_stmt;
20046 m_discriminator = 0;
252a6764
DE
20047}
20048
6f77053d
PA
20049void
20050lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20051 const gdb_byte *line_ptr,
7ab6656f 20052 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20053{
7ab6656f
OJ
20054 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20055 the pc range of the CU. However, we restrict the test to only ADDRESS
20056 values of zero to preserve GDB's previous behaviour which is to handle
20057 the specific case of a function being GC'd by the linker. */
924c2928 20058
7ab6656f 20059 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20060 {
20061 /* This line table is for a function which has been
20062 GCd by the linker. Ignore it. PR gdb/12528 */
20063
518817b3 20064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20065 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20066
b98664d3 20067 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20068 line_offset, objfile_name (objfile));
804d2729
TT
20069 m_currently_recording_lines = false;
20070 /* Note: m_currently_recording_lines is left as false until we see
20071 DW_LNE_end_sequence. */
924c2928
DE
20072 }
20073}
20074
f3f5162e 20075/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20076 Process the line number information in LH.
20077 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20078 program in order to set included_p for every referenced header. */
debd256d 20079
c906108c 20080static void
43f3e411
DE
20081dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20082 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20083{
d521ce57
TT
20084 const gdb_byte *line_ptr, *extended_end;
20085 const gdb_byte *line_end;
a8c50c1f 20086 unsigned int bytes_read, extended_len;
699ca60a 20087 unsigned char op_code, extended_op;
e142c38c 20088 CORE_ADDR baseaddr;
518817b3 20089 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20090 bfd *abfd = objfile->obfd;
fbf65064 20091 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20092 /* True if we're recording line info (as opposed to building partial
20093 symtabs and just interested in finding include files mentioned by
20094 the line number program). */
20095 bool record_lines_p = !decode_for_pst_p;
e142c38c 20096
b3b3bada 20097 baseaddr = objfile->text_section_offset ();
c906108c 20098
debd256d
JB
20099 line_ptr = lh->statement_program_start;
20100 line_end = lh->statement_program_end;
c906108c
SS
20101
20102 /* Read the statement sequences until there's nothing left. */
20103 while (line_ptr < line_end)
20104 {
6f77053d
PA
20105 /* The DWARF line number program state machine. Reset the state
20106 machine at the start of each sequence. */
804d2729 20107 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20108 bool end_sequence = false;
d9b3de22 20109
8c43009f 20110 if (record_lines_p)
c906108c 20111 {
8c43009f
PA
20112 /* Start a subfile for the current file of the state
20113 machine. */
20114 const file_entry *fe = state_machine.current_file ();
20115
20116 if (fe != NULL)
804d2729 20117 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20118 }
20119
a738430d 20120 /* Decode the table. */
d9b3de22 20121 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20122 {
20123 op_code = read_1_byte (abfd, line_ptr);
20124 line_ptr += 1;
9aa1fe7e 20125
debd256d 20126 if (op_code >= lh->opcode_base)
6e70227d 20127 {
8e07a239 20128 /* Special opcode. */
6f77053d 20129 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20130 }
20131 else switch (op_code)
c906108c
SS
20132 {
20133 case DW_LNS_extended_op:
3e43a32a
MS
20134 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20135 &bytes_read);
473b7be6 20136 line_ptr += bytes_read;
a8c50c1f 20137 extended_end = line_ptr + extended_len;
c906108c
SS
20138 extended_op = read_1_byte (abfd, line_ptr);
20139 line_ptr += 1;
20140 switch (extended_op)
20141 {
20142 case DW_LNE_end_sequence:
6f77053d
PA
20143 state_machine.handle_end_sequence ();
20144 end_sequence = true;
c906108c
SS
20145 break;
20146 case DW_LNE_set_address:
d9b3de22
DE
20147 {
20148 CORE_ADDR address
c8a7a66f 20149 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20150 line_ptr += bytes_read;
6f77053d
PA
20151
20152 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20153 lowpc - baseaddr, address);
6f77053d 20154 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20155 }
c906108c
SS
20156 break;
20157 case DW_LNE_define_file:
debd256d 20158 {
d521ce57 20159 const char *cur_file;
ecfb656c
PA
20160 unsigned int mod_time, length;
20161 dir_index dindex;
6e70227d 20162
3e43a32a
MS
20163 cur_file = read_direct_string (abfd, line_ptr,
20164 &bytes_read);
debd256d 20165 line_ptr += bytes_read;
ecfb656c 20166 dindex = (dir_index)
debd256d
JB
20167 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20168 line_ptr += bytes_read;
20169 mod_time =
20170 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20171 line_ptr += bytes_read;
20172 length =
20173 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20174 line_ptr += bytes_read;
ecfb656c 20175 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20176 }
c906108c 20177 break;
d0c6ba3d 20178 case DW_LNE_set_discriminator:
6f77053d
PA
20179 {
20180 /* The discriminator is not interesting to the
20181 debugger; just ignore it. We still need to
20182 check its value though:
20183 if there are consecutive entries for the same
20184 (non-prologue) line we want to coalesce them.
20185 PR 17276. */
20186 unsigned int discr
20187 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20188 line_ptr += bytes_read;
20189
20190 state_machine.handle_set_discriminator (discr);
20191 }
d0c6ba3d 20192 break;
c906108c 20193 default:
b98664d3 20194 complaint (_("mangled .debug_line section"));
debd256d 20195 return;
c906108c 20196 }
a8c50c1f
DJ
20197 /* Make sure that we parsed the extended op correctly. If e.g.
20198 we expected a different address size than the producer used,
20199 we may have read the wrong number of bytes. */
20200 if (line_ptr != extended_end)
20201 {
b98664d3 20202 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20203 return;
20204 }
c906108c
SS
20205 break;
20206 case DW_LNS_copy:
6f77053d 20207 state_machine.handle_copy ();
c906108c
SS
20208 break;
20209 case DW_LNS_advance_pc:
2dc7f7b3
TT
20210 {
20211 CORE_ADDR adjust
20212 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20213 line_ptr += bytes_read;
6f77053d
PA
20214
20215 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20216 }
c906108c
SS
20217 break;
20218 case DW_LNS_advance_line:
a05a36a5
DE
20219 {
20220 int line_delta
20221 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20222 line_ptr += bytes_read;
6f77053d
PA
20223
20224 state_machine.handle_advance_line (line_delta);
a05a36a5 20225 }
c906108c
SS
20226 break;
20227 case DW_LNS_set_file:
d9b3de22 20228 {
6f77053d 20229 file_name_index file
ecfb656c
PA
20230 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20231 &bytes_read);
d9b3de22 20232 line_ptr += bytes_read;
8c43009f 20233
6f77053d 20234 state_machine.handle_set_file (file);
d9b3de22 20235 }
c906108c
SS
20236 break;
20237 case DW_LNS_set_column:
0ad93d4f 20238 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20239 line_ptr += bytes_read;
20240 break;
20241 case DW_LNS_negate_stmt:
6f77053d 20242 state_machine.handle_negate_stmt ();
c906108c
SS
20243 break;
20244 case DW_LNS_set_basic_block:
c906108c 20245 break;
c2c6d25f
JM
20246 /* Add to the address register of the state machine the
20247 address increment value corresponding to special opcode
a738430d
MK
20248 255. I.e., this value is scaled by the minimum
20249 instruction length since special opcode 255 would have
b021a221 20250 scaled the increment. */
c906108c 20251 case DW_LNS_const_add_pc:
6f77053d 20252 state_machine.handle_const_add_pc ();
c906108c
SS
20253 break;
20254 case DW_LNS_fixed_advance_pc:
3e29f34a 20255 {
6f77053d 20256 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20257 line_ptr += 2;
6f77053d
PA
20258
20259 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20260 }
c906108c 20261 break;
9aa1fe7e 20262 default:
a738430d
MK
20263 {
20264 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20265 int i;
a738430d 20266
debd256d 20267 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20268 {
20269 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20270 line_ptr += bytes_read;
20271 }
20272 }
c906108c
SS
20273 }
20274 }
d9b3de22
DE
20275
20276 if (!end_sequence)
20277 dwarf2_debug_line_missing_end_sequence_complaint ();
20278
20279 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20280 in which case we still finish recording the last line). */
6f77053d 20281 state_machine.record_line (true);
c906108c 20282 }
f3f5162e
DE
20283}
20284
20285/* Decode the Line Number Program (LNP) for the given line_header
20286 structure and CU. The actual information extracted and the type
20287 of structures created from the LNP depends on the value of PST.
20288
20289 1. If PST is NULL, then this procedure uses the data from the program
20290 to create all necessary symbol tables, and their linetables.
20291
20292 2. If PST is not NULL, this procedure reads the program to determine
20293 the list of files included by the unit represented by PST, and
20294 builds all the associated partial symbol tables.
20295
20296 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20297 It is used for relative paths in the line table.
20298 NOTE: When processing partial symtabs (pst != NULL),
20299 comp_dir == pst->dirname.
20300
20301 NOTE: It is important that psymtabs have the same file name (via strcmp)
20302 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20303 symtab we don't use it in the name of the psymtabs we create.
20304 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20305 A good testcase for this is mb-inline.exp.
20306
527f3840
JK
20307 LOWPC is the lowest address in CU (or 0 if not known).
20308
20309 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20310 for its PC<->lines mapping information. Otherwise only the filename
20311 table is read in. */
f3f5162e
DE
20312
20313static void
20314dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20315 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20316 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20317{
518817b3 20318 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20319 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20320
527f3840
JK
20321 if (decode_mapping)
20322 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20323
20324 if (decode_for_pst_p)
20325 {
aaa75496
JB
20326 /* Now that we're done scanning the Line Header Program, we can
20327 create the psymtab of each included file. */
7ba99d21
AT
20328 for (auto &file_entry : lh->file_names ())
20329 if (file_entry.included_p == 1)
aaa75496 20330 {
c89b44cd 20331 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20332 const char *include_name =
7ba99d21
AT
20333 psymtab_include_file_name (lh, file_entry, pst,
20334 comp_dir, &name_holder);
c6da4cef 20335 if (include_name != NULL)
aaa75496
JB
20336 dwarf2_create_include_psymtab (include_name, pst, objfile);
20337 }
20338 }
cb1df416
DJ
20339 else
20340 {
20341 /* Make sure a symtab is created for every file, even files
20342 which contain only variables (i.e. no code with associated
20343 line numbers). */
c24bdb02
KS
20344 buildsym_compunit *builder = cu->get_builder ();
20345 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20346
7ba99d21 20347 for (auto &fe : lh->file_names ())
cb1df416 20348 {
804d2729 20349 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20350 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20351 {
c24bdb02 20352 builder->get_current_subfile ()->symtab
804d2729 20353 = allocate_symtab (cust,
c24bdb02 20354 builder->get_current_subfile ()->name);
43f3e411 20355 }
c24bdb02 20356 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20357 }
20358 }
c906108c
SS
20359}
20360
20361/* Start a subfile for DWARF. FILENAME is the name of the file and
20362 DIRNAME the name of the source directory which contains FILENAME
4d663531 20363 or NULL if not known.
c906108c
SS
20364 This routine tries to keep line numbers from identical absolute and
20365 relative file names in a common subfile.
20366
20367 Using the `list' example from the GDB testsuite, which resides in
20368 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20369 of /srcdir/list0.c yields the following debugging information for list0.c:
20370
c5aa993b 20371 DW_AT_name: /srcdir/list0.c
4d663531 20372 DW_AT_comp_dir: /compdir
357e46e7 20373 files.files[0].name: list0.h
c5aa993b 20374 files.files[0].dir: /srcdir
357e46e7 20375 files.files[1].name: list0.c
c5aa993b 20376 files.files[1].dir: /srcdir
c906108c
SS
20377
20378 The line number information for list0.c has to end up in a single
4f1520fb
FR
20379 subfile, so that `break /srcdir/list0.c:1' works as expected.
20380 start_subfile will ensure that this happens provided that we pass the
20381 concatenation of files.files[1].dir and files.files[1].name as the
20382 subfile's name. */
c906108c
SS
20383
20384static void
804d2729
TT
20385dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20386 const char *dirname)
c906108c 20387{
43816ebc 20388 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20389
4d663531 20390 /* In order not to lose the line information directory,
4f1520fb
FR
20391 we concatenate it to the filename when it makes sense.
20392 Note that the Dwarf3 standard says (speaking of filenames in line
20393 information): ``The directory index is ignored for file names
20394 that represent full path names''. Thus ignoring dirname in the
20395 `else' branch below isn't an issue. */
c906108c 20396
d5166ae1 20397 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20398 {
43816ebc
TT
20399 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20400 filename = copy.get ();
d521ce57 20401 }
c906108c 20402
c24bdb02 20403 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20404}
20405
804d2729
TT
20406/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20407 buildsym_compunit constructor. */
f4dc4d17 20408
c24bdb02
KS
20409struct compunit_symtab *
20410dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20411 CORE_ADDR low_pc)
f4dc4d17 20412{
c24bdb02 20413 gdb_assert (m_builder == nullptr);
43f3e411 20414
c24bdb02
KS
20415 m_builder.reset (new struct buildsym_compunit
20416 (per_cu->dwarf2_per_objfile->objfile,
20417 name, comp_dir, language, low_pc));
93b8bea4 20418
c24bdb02 20419 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20420
c24bdb02
KS
20421 get_builder ()->record_debugformat ("DWARF 2");
20422 get_builder ()->record_producer (producer);
f4dc4d17 20423
c24bdb02 20424 processing_has_namespace_info = false;
43f3e411 20425
c24bdb02 20426 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20427}
20428
4c2df51b
DJ
20429static void
20430var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20431 struct dwarf2_cu *cu)
4c2df51b 20432{
518817b3 20433 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20434 struct comp_unit_head *cu_header = &cu->header;
20435
4c2df51b
DJ
20436 /* NOTE drow/2003-01-30: There used to be a comment and some special
20437 code here to turn a symbol with DW_AT_external and a
20438 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20439 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20440 with some versions of binutils) where shared libraries could have
20441 relocations against symbols in their debug information - the
20442 minimal symbol would have the right address, but the debug info
20443 would not. It's no longer necessary, because we will explicitly
20444 apply relocations when we read in the debug information now. */
20445
20446 /* A DW_AT_location attribute with no contents indicates that a
20447 variable has been optimized away. */
4fc6c0d5 20448 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20449 {
f1e6e072 20450 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20451 return;
20452 }
20453
20454 /* Handle one degenerate form of location expression specially, to
20455 preserve GDB's previous behavior when section offsets are
336d760d
AT
20456 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20457 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20458
4fc6c0d5 20459 if (attr->form_is_block ()
3019eac3
DE
20460 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20461 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20462 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20463 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20464 && (DW_BLOCK (attr)->size
20465 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20466 {
891d2f0b 20467 unsigned int dummy;
4c2df51b 20468
3019eac3 20469 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20470 SET_SYMBOL_VALUE_ADDRESS
20471 (sym, cu->header.read_address (objfile->obfd,
20472 DW_BLOCK (attr)->data + 1,
20473 &dummy));
3019eac3 20474 else
38583298
TT
20475 SET_SYMBOL_VALUE_ADDRESS
20476 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20477 &dummy));
f1e6e072 20478 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20479 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20480 SET_SYMBOL_VALUE_ADDRESS
20481 (sym,
20482 SYMBOL_VALUE_ADDRESS (sym)
20483 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20484 return;
20485 }
20486
20487 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20488 expression evaluator, and use LOC_COMPUTED only when necessary
20489 (i.e. when the value of a register or memory location is
20490 referenced, or a thread-local block, etc.). Then again, it might
20491 not be worthwhile. I'm assuming that it isn't unless performance
20492 or memory numbers show me otherwise. */
20493
f1e6e072 20494 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20495
f1e6e072 20496 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20497 cu->has_loclist = true;
4c2df51b
DJ
20498}
20499
c906108c
SS
20500/* Given a pointer to a DWARF information entry, figure out if we need
20501 to make a symbol table entry for it, and if so, create a new entry
20502 and return a pointer to it.
20503 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20504 used the passed type.
20505 If SPACE is not NULL, use it to hold the new symbol. If it is
20506 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20507
20508static struct symbol *
5e2db402
TT
20509new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20510 struct symbol *space)
c906108c 20511{
518817b3
SM
20512 struct dwarf2_per_objfile *dwarf2_per_objfile
20513 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20514 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20515 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20516 struct symbol *sym = NULL;
15d034d0 20517 const char *name;
c906108c
SS
20518 struct attribute *attr = NULL;
20519 struct attribute *attr2 = NULL;
e142c38c 20520 CORE_ADDR baseaddr;
e37fd15a
SW
20521 struct pending **list_to_add = NULL;
20522
edb3359d 20523 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20524
b3b3bada 20525 baseaddr = objfile->text_section_offset ();
c906108c 20526
94af9270 20527 name = dwarf2_name (die, cu);
c906108c
SS
20528 if (name)
20529 {
94af9270 20530 const char *linkagename;
34eaf542 20531 int suppress_add = 0;
94af9270 20532
34eaf542
TT
20533 if (space)
20534 sym = space;
20535 else
e623cf5d 20536 sym = allocate_symbol (objfile);
c906108c 20537 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20538
20539 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20540 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20541 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20542 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20543
f55ee35c
JK
20544 /* Fortran does not have mangling standard and the mangling does differ
20545 between gfortran, iFort etc. */
20546 if (cu->language == language_fortran
468c0cbb
CB
20547 && symbol_get_demangled_name (sym) == NULL)
20548 symbol_set_demangled_name (sym,
cfc594ee 20549 dwarf2_full_name (name, die, cu),
29df156d 20550 NULL);
f55ee35c 20551
c906108c 20552 /* Default assumptions.
c5aa993b 20553 Use the passed type or decode it from the die. */
176620f1 20554 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20555 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20556 if (type != NULL)
20557 SYMBOL_TYPE (sym) = type;
20558 else
e7c27a73 20559 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20560 attr = dwarf2_attr (die,
20561 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20562 cu);
435d3d88 20563 if (attr != nullptr)
c906108c
SS
20564 {
20565 SYMBOL_LINE (sym) = DW_UNSND (attr);
20566 }
cb1df416 20567
edb3359d
DJ
20568 attr = dwarf2_attr (die,
20569 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20570 cu);
435d3d88 20571 if (attr != nullptr)
cb1df416 20572 {
ecfb656c 20573 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20574 struct file_entry *fe;
9a619af0 20575
ecfb656c
PA
20576 if (cu->line_header != NULL)
20577 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20578 else
20579 fe = NULL;
20580
20581 if (fe == NULL)
b98664d3 20582 complaint (_("file index out of range"));
8c43009f
PA
20583 else
20584 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20585 }
20586
c906108c
SS
20587 switch (die->tag)
20588 {
20589 case DW_TAG_label:
e142c38c 20590 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20591 if (attr != nullptr)
3e29f34a
MR
20592 {
20593 CORE_ADDR addr;
20594
cd6c91b4 20595 addr = attr->value_as_address ();
3e29f34a 20596 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20597 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20598 }
0f5238ed
TT
20599 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20600 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20601 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20602 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20603 break;
20604 case DW_TAG_subprogram:
20605 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20606 finish_block. */
f1e6e072 20607 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20608 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20609 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20610 || cu->language == language_ada
20611 || cu->language == language_fortran)
c906108c 20612 {
2cfa0c8d 20613 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20614 Ada and Fortran subprograms, whether marked external or
20615 not, are always stored as a global symbol, because we want
20616 to be able to access them globally. For instance, we want
20617 to be able to break on a nested subprogram without having
20618 to specify the context. */
c24bdb02 20619 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20620 }
20621 else
20622 {
e37fd15a 20623 list_to_add = cu->list_in_scope;
c906108c
SS
20624 }
20625 break;
edb3359d
DJ
20626 case DW_TAG_inlined_subroutine:
20627 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20628 finish_block. */
f1e6e072 20629 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20630 SYMBOL_INLINED (sym) = 1;
481860b3 20631 list_to_add = cu->list_in_scope;
edb3359d 20632 break;
34eaf542
TT
20633 case DW_TAG_template_value_param:
20634 suppress_add = 1;
20635 /* Fall through. */
72929c62 20636 case DW_TAG_constant:
c906108c 20637 case DW_TAG_variable:
254e6b9e 20638 case DW_TAG_member:
0963b4bd
MS
20639 /* Compilation with minimal debug info may result in
20640 variables with missing type entries. Change the
20641 misleading `void' type to something sensible. */
c906108c 20642 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20643 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20644
e142c38c 20645 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20646 /* In the case of DW_TAG_member, we should only be called for
20647 static const members. */
20648 if (die->tag == DW_TAG_member)
20649 {
3863f96c
DE
20650 /* dwarf2_add_field uses die_is_declaration,
20651 so we do the same. */
254e6b9e
DE
20652 gdb_assert (die_is_declaration (die, cu));
20653 gdb_assert (attr);
20654 }
435d3d88 20655 if (attr != nullptr)
c906108c 20656 {
e7c27a73 20657 dwarf2_const_value (attr, sym, cu);
e142c38c 20658 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20659 if (!suppress_add)
34eaf542
TT
20660 {
20661 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20662 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20663 else
e37fd15a 20664 list_to_add = cu->list_in_scope;
34eaf542 20665 }
c906108c
SS
20666 break;
20667 }
e142c38c 20668 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20669 if (attr != nullptr)
c906108c 20670 {
e7c27a73 20671 var_decode_location (attr, sym, cu);
e142c38c 20672 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20673
20674 /* Fortran explicitly imports any global symbols to the local
20675 scope by DW_TAG_common_block. */
20676 if (cu->language == language_fortran && die->parent
20677 && die->parent->tag == DW_TAG_common_block)
20678 attr2 = NULL;
20679
caac4577
JG
20680 if (SYMBOL_CLASS (sym) == LOC_STATIC
20681 && SYMBOL_VALUE_ADDRESS (sym) == 0
20682 && !dwarf2_per_objfile->has_section_at_zero)
20683 {
20684 /* When a static variable is eliminated by the linker,
20685 the corresponding debug information is not stripped
20686 out, but the variable address is set to null;
20687 do not add such variables into symbol table. */
20688 }
20689 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20690 {
4b610737
TT
20691 if (SYMBOL_CLASS (sym) == LOC_STATIC
20692 && (objfile->flags & OBJF_MAINLINE) == 0
20693 && dwarf2_per_objfile->can_copy)
20694 {
20695 /* A global static variable might be subject to
20696 copy relocation. We first check for a local
20697 minsym, though, because maybe the symbol was
20698 marked hidden, in which case this would not
20699 apply. */
20700 bound_minimal_symbol found
20701 = (lookup_minimal_symbol_linkage
987012b8 20702 (sym->linkage_name (), objfile));
4b610737
TT
20703 if (found.minsym != nullptr)
20704 sym->maybe_copied = 1;
20705 }
f55ee35c 20706
1c809c68
TT
20707 /* A variable with DW_AT_external is never static,
20708 but it may be block-scoped. */
804d2729 20709 list_to_add
c24bdb02
KS
20710 = ((cu->list_in_scope
20711 == cu->get_builder ()->get_file_symbols ())
20712 ? cu->get_builder ()->get_global_symbols ()
804d2729 20713 : cu->list_in_scope);
1c809c68 20714 }
c906108c 20715 else
e37fd15a 20716 list_to_add = cu->list_in_scope;
c906108c
SS
20717 }
20718 else
20719 {
20720 /* We do not know the address of this symbol.
c5aa993b
JM
20721 If it is an external symbol and we have type information
20722 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20723 The address of the variable will then be determined from
20724 the minimal symbol table whenever the variable is
20725 referenced. */
e142c38c 20726 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20727
20728 /* Fortran explicitly imports any global symbols to the local
20729 scope by DW_TAG_common_block. */
20730 if (cu->language == language_fortran && die->parent
20731 && die->parent->tag == DW_TAG_common_block)
20732 {
20733 /* SYMBOL_CLASS doesn't matter here because
20734 read_common_block is going to reset it. */
20735 if (!suppress_add)
20736 list_to_add = cu->list_in_scope;
20737 }
20738 else if (attr2 && (DW_UNSND (attr2) != 0)
20739 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20740 {
0fe7935b
DJ
20741 /* A variable with DW_AT_external is never static, but it
20742 may be block-scoped. */
804d2729 20743 list_to_add
c24bdb02
KS
20744 = ((cu->list_in_scope
20745 == cu->get_builder ()->get_file_symbols ())
20746 ? cu->get_builder ()->get_global_symbols ()
804d2729 20747 : cu->list_in_scope);
0fe7935b 20748
f1e6e072 20749 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20750 }
442ddf59
JK
20751 else if (!die_is_declaration (die, cu))
20752 {
20753 /* Use the default LOC_OPTIMIZED_OUT class. */
20754 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20755 if (!suppress_add)
20756 list_to_add = cu->list_in_scope;
442ddf59 20757 }
c906108c
SS
20758 }
20759 break;
20760 case DW_TAG_formal_parameter:
a60f3166
TT
20761 {
20762 /* If we are inside a function, mark this as an argument. If
20763 not, we might be looking at an argument to an inlined function
20764 when we do not have enough information to show inlined frames;
20765 pretend it's a local variable in that case so that the user can
20766 still see it. */
804d2729 20767 struct context_stack *curr
c24bdb02 20768 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20769 if (curr != nullptr && curr->name != nullptr)
20770 SYMBOL_IS_ARGUMENT (sym) = 1;
20771 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20772 if (attr != nullptr)
a60f3166
TT
20773 {
20774 var_decode_location (attr, sym, cu);
20775 }
20776 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20777 if (attr != nullptr)
a60f3166
TT
20778 {
20779 dwarf2_const_value (attr, sym, cu);
20780 }
f346a30d 20781
a60f3166
TT
20782 list_to_add = cu->list_in_scope;
20783 }
c906108c
SS
20784 break;
20785 case DW_TAG_unspecified_parameters:
20786 /* From varargs functions; gdb doesn't seem to have any
20787 interest in this information, so just ignore it for now.
20788 (FIXME?) */
20789 break;
34eaf542
TT
20790 case DW_TAG_template_type_param:
20791 suppress_add = 1;
20792 /* Fall through. */
c906108c 20793 case DW_TAG_class_type:
680b30c7 20794 case DW_TAG_interface_type:
c906108c
SS
20795 case DW_TAG_structure_type:
20796 case DW_TAG_union_type:
72019c9c 20797 case DW_TAG_set_type:
c906108c 20798 case DW_TAG_enumeration_type:
f1e6e072 20799 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20800 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20801
63d06c5c 20802 {
9c37b5ae 20803 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20804 really ever be static objects: otherwise, if you try
20805 to, say, break of a class's method and you're in a file
20806 which doesn't mention that class, it won't work unless
20807 the check for all static symbols in lookup_symbol_aux
20808 saves you. See the OtherFileClass tests in
20809 gdb.c++/namespace.exp. */
20810
e37fd15a 20811 if (!suppress_add)
34eaf542 20812 {
c24bdb02 20813 buildsym_compunit *builder = cu->get_builder ();
804d2729 20814 list_to_add
c24bdb02 20815 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20816 && cu->language == language_cplus
c24bdb02 20817 ? builder->get_global_symbols ()
804d2729 20818 : cu->list_in_scope);
63d06c5c 20819
64382290 20820 /* The semantics of C++ state that "struct foo {
9c37b5ae 20821 ... }" also defines a typedef for "foo". */
64382290 20822 if (cu->language == language_cplus
45280282 20823 || cu->language == language_ada
c44af4eb
TT
20824 || cu->language == language_d
20825 || cu->language == language_rust)
64382290
TT
20826 {
20827 /* The symbol's name is already allocated along
20828 with this objfile, so we don't need to
20829 duplicate it for the type. */
20830 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20831 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20832 }
63d06c5c
DC
20833 }
20834 }
c906108c
SS
20835 break;
20836 case DW_TAG_typedef:
f1e6e072 20837 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20838 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20839 list_to_add = cu->list_in_scope;
63d06c5c 20840 break;
c906108c 20841 case DW_TAG_base_type:
a02abb62 20842 case DW_TAG_subrange_type:
f1e6e072 20843 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20844 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20845 list_to_add = cu->list_in_scope;
c906108c
SS
20846 break;
20847 case DW_TAG_enumerator:
e142c38c 20848 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20849 if (attr != nullptr)
c906108c 20850 {
e7c27a73 20851 dwarf2_const_value (attr, sym, cu);
c906108c 20852 }
63d06c5c
DC
20853 {
20854 /* NOTE: carlton/2003-11-10: See comment above in the
20855 DW_TAG_class_type, etc. block. */
20856
804d2729 20857 list_to_add
c24bdb02 20858 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20859 && cu->language == language_cplus
c24bdb02 20860 ? cu->get_builder ()->get_global_symbols ()
804d2729 20861 : cu->list_in_scope);
63d06c5c 20862 }
c906108c 20863 break;
74921315 20864 case DW_TAG_imported_declaration:
5c4e30ca 20865 case DW_TAG_namespace:
f1e6e072 20866 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20867 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20868 break;
530e8392
KB
20869 case DW_TAG_module:
20870 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20871 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20872 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20873 break;
4357ac6c 20874 case DW_TAG_common_block:
f1e6e072 20875 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20876 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20877 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20878 break;
c906108c
SS
20879 default:
20880 /* Not a tag we recognize. Hopefully we aren't processing
20881 trash data, but since we must specifically ignore things
20882 we don't recognize, there is nothing else we should do at
0963b4bd 20883 this point. */
b98664d3 20884 complaint (_("unsupported tag: '%s'"),
4d3c2250 20885 dwarf_tag_name (die->tag));
c906108c
SS
20886 break;
20887 }
df8a16a1 20888
e37fd15a
SW
20889 if (suppress_add)
20890 {
20891 sym->hash_next = objfile->template_symbols;
20892 objfile->template_symbols = sym;
20893 list_to_add = NULL;
20894 }
20895
20896 if (list_to_add != NULL)
d3cb6808 20897 add_symbol_to_list (sym, list_to_add);
e37fd15a 20898
df8a16a1
DJ
20899 /* For the benefit of old versions of GCC, check for anonymous
20900 namespaces based on the demangled name. */
4d4ec4e5 20901 if (!cu->processing_has_namespace_info
94af9270 20902 && cu->language == language_cplus)
c24bdb02 20903 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20904 }
20905 return (sym);
20906}
20907
98bfdba5
PA
20908/* Given an attr with a DW_FORM_dataN value in host byte order,
20909 zero-extend it as appropriate for the symbol's type. The DWARF
20910 standard (v4) is not entirely clear about the meaning of using
20911 DW_FORM_dataN for a constant with a signed type, where the type is
20912 wider than the data. The conclusion of a discussion on the DWARF
20913 list was that this is unspecified. We choose to always zero-extend
20914 because that is the interpretation long in use by GCC. */
c906108c 20915
98bfdba5 20916static gdb_byte *
ff39bb5e 20917dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20918 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20919{
518817b3 20920 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20921 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20922 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20923 LONGEST l = DW_UNSND (attr);
20924
20925 if (bits < sizeof (*value) * 8)
20926 {
20927 l &= ((LONGEST) 1 << bits) - 1;
20928 *value = l;
20929 }
20930 else if (bits == sizeof (*value) * 8)
20931 *value = l;
20932 else
20933 {
224c3ddb 20934 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20935 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20936 return bytes;
20937 }
20938
20939 return NULL;
20940}
20941
20942/* Read a constant value from an attribute. Either set *VALUE, or if
20943 the value does not fit in *VALUE, set *BYTES - either already
20944 allocated on the objfile obstack, or newly allocated on OBSTACK,
20945 or, set *BATON, if we translated the constant to a location
20946 expression. */
20947
20948static void
ff39bb5e 20949dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20950 const char *name, struct obstack *obstack,
20951 struct dwarf2_cu *cu,
d521ce57 20952 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20953 struct dwarf2_locexpr_baton **baton)
20954{
518817b3 20955 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20956 struct comp_unit_head *cu_header = &cu->header;
c906108c 20957 struct dwarf_block *blk;
98bfdba5
PA
20958 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20959 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20960
20961 *value = 0;
20962 *bytes = NULL;
20963 *baton = NULL;
c906108c
SS
20964
20965 switch (attr->form)
20966 {
20967 case DW_FORM_addr:
336d760d 20968 case DW_FORM_addrx:
3019eac3 20969 case DW_FORM_GNU_addr_index:
ac56253d 20970 {
ac56253d
TT
20971 gdb_byte *data;
20972
98bfdba5
PA
20973 if (TYPE_LENGTH (type) != cu_header->addr_size)
20974 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20975 cu_header->addr_size,
98bfdba5 20976 TYPE_LENGTH (type));
ac56253d
TT
20977 /* Symbols of this form are reasonably rare, so we just
20978 piggyback on the existing location code rather than writing
20979 a new implementation of symbol_computed_ops. */
8d749320 20980 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20981 (*baton)->per_cu = cu->per_cu;
20982 gdb_assert ((*baton)->per_cu);
ac56253d 20983
98bfdba5 20984 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20985 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20986 (*baton)->data = data;
ac56253d
TT
20987
20988 data[0] = DW_OP_addr;
20989 store_unsigned_integer (&data[1], cu_header->addr_size,
20990 byte_order, DW_ADDR (attr));
20991 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20992 }
c906108c 20993 break;
4ac36638 20994 case DW_FORM_string:
93b5768b 20995 case DW_FORM_strp:
cf532bd1 20996 case DW_FORM_strx:
3019eac3 20997 case DW_FORM_GNU_str_index:
36586728 20998 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20999 /* DW_STRING is already allocated on the objfile obstack, point
21000 directly to it. */
d521ce57 21001 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21002 break;
c906108c
SS
21003 case DW_FORM_block1:
21004 case DW_FORM_block2:
21005 case DW_FORM_block4:
21006 case DW_FORM_block:
2dc7f7b3 21007 case DW_FORM_exprloc:
0224619f 21008 case DW_FORM_data16:
c906108c 21009 blk = DW_BLOCK (attr);
98bfdba5
PA
21010 if (TYPE_LENGTH (type) != blk->size)
21011 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21012 TYPE_LENGTH (type));
21013 *bytes = blk->data;
c906108c 21014 break;
2df3850c
JM
21015
21016 /* The DW_AT_const_value attributes are supposed to carry the
21017 symbol's value "represented as it would be on the target
21018 architecture." By the time we get here, it's already been
21019 converted to host endianness, so we just need to sign- or
21020 zero-extend it as appropriate. */
21021 case DW_FORM_data1:
3aef2284 21022 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21023 break;
c906108c 21024 case DW_FORM_data2:
3aef2284 21025 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21026 break;
c906108c 21027 case DW_FORM_data4:
3aef2284 21028 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21029 break;
c906108c 21030 case DW_FORM_data8:
3aef2284 21031 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21032 break;
21033
c906108c 21034 case DW_FORM_sdata:
663c44ac 21035 case DW_FORM_implicit_const:
98bfdba5 21036 *value = DW_SND (attr);
2df3850c
JM
21037 break;
21038
c906108c 21039 case DW_FORM_udata:
98bfdba5 21040 *value = DW_UNSND (attr);
c906108c 21041 break;
2df3850c 21042
c906108c 21043 default:
b98664d3 21044 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21045 dwarf_form_name (attr->form));
98bfdba5 21046 *value = 0;
c906108c
SS
21047 break;
21048 }
21049}
21050
2df3850c 21051
98bfdba5
PA
21052/* Copy constant value from an attribute to a symbol. */
21053
2df3850c 21054static void
ff39bb5e 21055dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21056 struct dwarf2_cu *cu)
2df3850c 21057{
518817b3 21058 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21059 LONGEST value;
d521ce57 21060 const gdb_byte *bytes;
98bfdba5 21061 struct dwarf2_locexpr_baton *baton;
2df3850c 21062
98bfdba5 21063 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21064 sym->print_name (),
98bfdba5
PA
21065 &objfile->objfile_obstack, cu,
21066 &value, &bytes, &baton);
2df3850c 21067
98bfdba5
PA
21068 if (baton != NULL)
21069 {
98bfdba5 21070 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21071 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21072 }
21073 else if (bytes != NULL)
21074 {
21075 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21076 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21077 }
21078 else
21079 {
21080 SYMBOL_VALUE (sym) = value;
f1e6e072 21081 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21082 }
2df3850c
JM
21083}
21084
c906108c
SS
21085/* Return the type of the die in question using its DW_AT_type attribute. */
21086
21087static struct type *
e7c27a73 21088die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21089{
c906108c 21090 struct attribute *type_attr;
c906108c 21091
e142c38c 21092 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21093 if (!type_attr)
21094 {
518817b3 21095 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21096 /* A missing DW_AT_type represents a void type. */
518817b3 21097 return objfile_type (objfile)->builtin_void;
c906108c 21098 }
348e048f 21099
673bfd45 21100 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21101}
21102
b4ba55a1
JB
21103/* True iff CU's producer generates GNAT Ada auxiliary information
21104 that allows to find parallel types through that information instead
21105 of having to do expensive parallel lookups by type name. */
21106
21107static int
21108need_gnat_info (struct dwarf2_cu *cu)
21109{
de4cb04a
JB
21110 /* Assume that the Ada compiler was GNAT, which always produces
21111 the auxiliary information. */
21112 return (cu->language == language_ada);
b4ba55a1
JB
21113}
21114
b4ba55a1
JB
21115/* Return the auxiliary type of the die in question using its
21116 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21117 attribute is not present. */
21118
21119static struct type *
21120die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21121{
b4ba55a1 21122 struct attribute *type_attr;
b4ba55a1
JB
21123
21124 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21125 if (!type_attr)
21126 return NULL;
21127
673bfd45 21128 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21129}
21130
21131/* If DIE has a descriptive_type attribute, then set the TYPE's
21132 descriptive type accordingly. */
21133
21134static void
21135set_descriptive_type (struct type *type, struct die_info *die,
21136 struct dwarf2_cu *cu)
21137{
21138 struct type *descriptive_type = die_descriptive_type (die, cu);
21139
21140 if (descriptive_type)
21141 {
21142 ALLOCATE_GNAT_AUX_TYPE (type);
21143 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21144 }
21145}
21146
c906108c
SS
21147/* Return the containing type of the die in question using its
21148 DW_AT_containing_type attribute. */
21149
21150static struct type *
e7c27a73 21151die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21152{
c906108c 21153 struct attribute *type_attr;
518817b3 21154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21155
e142c38c 21156 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21157 if (!type_attr)
21158 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21159 "[in module %s]"), objfile_name (objfile));
33ac96f0 21160
673bfd45 21161 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21162}
21163
ac9ec31b
DE
21164/* Return an error marker type to use for the ill formed type in DIE/CU. */
21165
21166static struct type *
21167build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21168{
518817b3
SM
21169 struct dwarf2_per_objfile *dwarf2_per_objfile
21170 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21171 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21172 char *saved;
ac9ec31b 21173
528e1572
SM
21174 std::string message
21175 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21176 objfile_name (objfile),
21177 sect_offset_str (cu->header.sect_off),
21178 sect_offset_str (die->sect_off));
efba19b0 21179 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21180
19f392bc 21181 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21182}
21183
673bfd45 21184/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21185 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21186 DW_AT_containing_type.
673bfd45
DE
21187 If there is no type substitute an error marker. */
21188
c906108c 21189static struct type *
ff39bb5e 21190lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21191 struct dwarf2_cu *cu)
c906108c 21192{
518817b3
SM
21193 struct dwarf2_per_objfile *dwarf2_per_objfile
21194 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21195 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21196 struct type *this_type;
21197
ac9ec31b
DE
21198 gdb_assert (attr->name == DW_AT_type
21199 || attr->name == DW_AT_GNAT_descriptive_type
21200 || attr->name == DW_AT_containing_type);
21201
673bfd45
DE
21202 /* First see if we have it cached. */
21203
36586728
TT
21204 if (attr->form == DW_FORM_GNU_ref_alt)
21205 {
21206 struct dwarf2_per_cu_data *per_cu;
9c541725 21207 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21208
ed2dc618
SM
21209 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21210 dwarf2_per_objfile);
9c541725 21211 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21212 }
cd6c91b4 21213 else if (attr->form_is_ref ())
673bfd45 21214 {
9c541725 21215 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21216
9c541725 21217 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21218 }
55f1336d 21219 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21220 {
ac9ec31b 21221 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21222
ac9ec31b 21223 return get_signatured_type (die, signature, cu);
673bfd45
DE
21224 }
21225 else
21226 {
b98664d3 21227 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21228 " at %s [in module %s]"),
21229 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21230 objfile_name (objfile));
ac9ec31b 21231 return build_error_marker_type (cu, die);
673bfd45
DE
21232 }
21233
21234 /* If not cached we need to read it in. */
21235
21236 if (this_type == NULL)
21237 {
ac9ec31b 21238 struct die_info *type_die = NULL;
673bfd45
DE
21239 struct dwarf2_cu *type_cu = cu;
21240
cd6c91b4 21241 if (attr->form_is_ref ())
ac9ec31b
DE
21242 type_die = follow_die_ref (die, attr, &type_cu);
21243 if (type_die == NULL)
21244 return build_error_marker_type (cu, die);
21245 /* If we find the type now, it's probably because the type came
3019eac3
DE
21246 from an inter-CU reference and the type's CU got expanded before
21247 ours. */
ac9ec31b 21248 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21249 }
21250
21251 /* If we still don't have a type use an error marker. */
21252
21253 if (this_type == NULL)
ac9ec31b 21254 return build_error_marker_type (cu, die);
673bfd45 21255
f792889a 21256 return this_type;
c906108c
SS
21257}
21258
673bfd45
DE
21259/* Return the type in DIE, CU.
21260 Returns NULL for invalid types.
21261
02142a6c 21262 This first does a lookup in die_type_hash,
673bfd45
DE
21263 and only reads the die in if necessary.
21264
21265 NOTE: This can be called when reading in partial or full symbols. */
21266
f792889a 21267static struct type *
e7c27a73 21268read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21269{
f792889a
DJ
21270 struct type *this_type;
21271
21272 this_type = get_die_type (die, cu);
21273 if (this_type)
21274 return this_type;
21275
673bfd45
DE
21276 return read_type_die_1 (die, cu);
21277}
21278
21279/* Read the type in DIE, CU.
21280 Returns NULL for invalid types. */
21281
21282static struct type *
21283read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21284{
21285 struct type *this_type = NULL;
21286
c906108c
SS
21287 switch (die->tag)
21288 {
21289 case DW_TAG_class_type:
680b30c7 21290 case DW_TAG_interface_type:
c906108c
SS
21291 case DW_TAG_structure_type:
21292 case DW_TAG_union_type:
f792889a 21293 this_type = read_structure_type (die, cu);
c906108c
SS
21294 break;
21295 case DW_TAG_enumeration_type:
f792889a 21296 this_type = read_enumeration_type (die, cu);
c906108c
SS
21297 break;
21298 case DW_TAG_subprogram:
21299 case DW_TAG_subroutine_type:
edb3359d 21300 case DW_TAG_inlined_subroutine:
f792889a 21301 this_type = read_subroutine_type (die, cu);
c906108c
SS
21302 break;
21303 case DW_TAG_array_type:
f792889a 21304 this_type = read_array_type (die, cu);
c906108c 21305 break;
72019c9c 21306 case DW_TAG_set_type:
f792889a 21307 this_type = read_set_type (die, cu);
72019c9c 21308 break;
c906108c 21309 case DW_TAG_pointer_type:
f792889a 21310 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21311 break;
21312 case DW_TAG_ptr_to_member_type:
f792889a 21313 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21314 break;
21315 case DW_TAG_reference_type:
4297a3f0
AV
21316 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21317 break;
21318 case DW_TAG_rvalue_reference_type:
21319 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21320 break;
21321 case DW_TAG_const_type:
f792889a 21322 this_type = read_tag_const_type (die, cu);
c906108c
SS
21323 break;
21324 case DW_TAG_volatile_type:
f792889a 21325 this_type = read_tag_volatile_type (die, cu);
c906108c 21326 break;
06d66ee9
TT
21327 case DW_TAG_restrict_type:
21328 this_type = read_tag_restrict_type (die, cu);
21329 break;
c906108c 21330 case DW_TAG_string_type:
f792889a 21331 this_type = read_tag_string_type (die, cu);
c906108c
SS
21332 break;
21333 case DW_TAG_typedef:
f792889a 21334 this_type = read_typedef (die, cu);
c906108c 21335 break;
a02abb62 21336 case DW_TAG_subrange_type:
f792889a 21337 this_type = read_subrange_type (die, cu);
a02abb62 21338 break;
c906108c 21339 case DW_TAG_base_type:
f792889a 21340 this_type = read_base_type (die, cu);
c906108c 21341 break;
81a17f79 21342 case DW_TAG_unspecified_type:
f792889a 21343 this_type = read_unspecified_type (die, cu);
81a17f79 21344 break;
0114d602
DJ
21345 case DW_TAG_namespace:
21346 this_type = read_namespace_type (die, cu);
21347 break;
f55ee35c
JK
21348 case DW_TAG_module:
21349 this_type = read_module_type (die, cu);
21350 break;
a2c2acaf
MW
21351 case DW_TAG_atomic_type:
21352 this_type = read_tag_atomic_type (die, cu);
21353 break;
c906108c 21354 default:
b98664d3 21355 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21356 dwarf_tag_name (die->tag));
c906108c
SS
21357 break;
21358 }
63d06c5c 21359
f792889a 21360 return this_type;
63d06c5c
DC
21361}
21362
abc72ce4
DE
21363/* See if we can figure out if the class lives in a namespace. We do
21364 this by looking for a member function; its demangled name will
21365 contain namespace info, if there is any.
21366 Return the computed name or NULL.
21367 Space for the result is allocated on the objfile's obstack.
21368 This is the full-die version of guess_partial_die_structure_name.
21369 In this case we know DIE has no useful parent. */
21370
43816ebc 21371static const char *
abc72ce4
DE
21372guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21373{
21374 struct die_info *spec_die;
21375 struct dwarf2_cu *spec_cu;
21376 struct die_info *child;
518817b3 21377 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21378
21379 spec_cu = cu;
21380 spec_die = die_specification (die, &spec_cu);
21381 if (spec_die != NULL)
21382 {
21383 die = spec_die;
21384 cu = spec_cu;
21385 }
21386
21387 for (child = die->child;
21388 child != NULL;
21389 child = child->sibling)
21390 {
21391 if (child->tag == DW_TAG_subprogram)
21392 {
73b9be8b 21393 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21394
7d45c7c3 21395 if (linkage_name != NULL)
abc72ce4 21396 {
43816ebc
TT
21397 gdb::unique_xmalloc_ptr<char> actual_name
21398 (language_class_name_from_physname (cu->language_defn,
21399 linkage_name));
21400 const char *name = NULL;
abc72ce4
DE
21401
21402 if (actual_name != NULL)
21403 {
15d034d0 21404 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21405
21406 if (die_name != NULL
43816ebc 21407 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21408 {
21409 /* Strip off the class name from the full name.
21410 We want the prefix. */
21411 int die_name_len = strlen (die_name);
43816ebc
TT
21412 int actual_name_len = strlen (actual_name.get ());
21413 const char *ptr = actual_name.get ();
abc72ce4
DE
21414
21415 /* Test for '::' as a sanity check. */
21416 if (actual_name_len > die_name_len + 2
43816ebc 21417 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21418 name = obstack_strndup (
e3b94546 21419 &objfile->per_bfd->storage_obstack,
43816ebc 21420 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21421 }
21422 }
abc72ce4
DE
21423 return name;
21424 }
21425 }
21426 }
21427
21428 return NULL;
21429}
21430
96408a79
SA
21431/* GCC might emit a nameless typedef that has a linkage name. Determine the
21432 prefix part in such case. See
21433 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21434
a121b7c1 21435static const char *
96408a79
SA
21436anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21437{
21438 struct attribute *attr;
e6a959d6 21439 const char *base;
96408a79
SA
21440
21441 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21442 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21443 return NULL;
21444
7d45c7c3 21445 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21446 return NULL;
21447
73b9be8b 21448 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21449 if (attr == NULL || DW_STRING (attr) == NULL)
21450 return NULL;
21451
21452 /* dwarf2_name had to be already called. */
21453 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21454
21455 /* Strip the base name, keep any leading namespaces/classes. */
21456 base = strrchr (DW_STRING (attr), ':');
21457 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21458 return "";
21459
518817b3 21460 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21461 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21462 DW_STRING (attr),
21463 &base[-1] - DW_STRING (attr));
96408a79
SA
21464}
21465
fdde2d81 21466/* Return the name of the namespace/class that DIE is defined within,
0114d602 21467 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21468
0114d602
DJ
21469 For example, if we're within the method foo() in the following
21470 code:
21471
21472 namespace N {
21473 class C {
21474 void foo () {
21475 }
21476 };
21477 }
21478
21479 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21480
0d5cff50 21481static const char *
e142c38c 21482determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21483{
518817b3
SM
21484 struct dwarf2_per_objfile *dwarf2_per_objfile
21485 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21486 struct die_info *parent, *spec_die;
21487 struct dwarf2_cu *spec_cu;
21488 struct type *parent_type;
a121b7c1 21489 const char *retval;
63d06c5c 21490
9c37b5ae 21491 if (cu->language != language_cplus
c44af4eb
TT
21492 && cu->language != language_fortran && cu->language != language_d
21493 && cu->language != language_rust)
0114d602
DJ
21494 return "";
21495
96408a79
SA
21496 retval = anonymous_struct_prefix (die, cu);
21497 if (retval)
21498 return retval;
21499
0114d602
DJ
21500 /* We have to be careful in the presence of DW_AT_specification.
21501 For example, with GCC 3.4, given the code
21502
21503 namespace N {
21504 void foo() {
21505 // Definition of N::foo.
21506 }
21507 }
21508
21509 then we'll have a tree of DIEs like this:
21510
21511 1: DW_TAG_compile_unit
21512 2: DW_TAG_namespace // N
21513 3: DW_TAG_subprogram // declaration of N::foo
21514 4: DW_TAG_subprogram // definition of N::foo
21515 DW_AT_specification // refers to die #3
21516
21517 Thus, when processing die #4, we have to pretend that we're in
21518 the context of its DW_AT_specification, namely the contex of die
21519 #3. */
21520 spec_cu = cu;
21521 spec_die = die_specification (die, &spec_cu);
21522 if (spec_die == NULL)
21523 parent = die->parent;
21524 else
63d06c5c 21525 {
0114d602
DJ
21526 parent = spec_die->parent;
21527 cu = spec_cu;
63d06c5c 21528 }
0114d602
DJ
21529
21530 if (parent == NULL)
21531 return "";
98bfdba5
PA
21532 else if (parent->building_fullname)
21533 {
21534 const char *name;
21535 const char *parent_name;
21536
21537 /* It has been seen on RealView 2.2 built binaries,
21538 DW_TAG_template_type_param types actually _defined_ as
21539 children of the parent class:
21540
21541 enum E {};
21542 template class <class Enum> Class{};
21543 Class<enum E> class_e;
21544
21545 1: DW_TAG_class_type (Class)
21546 2: DW_TAG_enumeration_type (E)
21547 3: DW_TAG_enumerator (enum1:0)
21548 3: DW_TAG_enumerator (enum2:1)
21549 ...
21550 2: DW_TAG_template_type_param
21551 DW_AT_type DW_FORM_ref_udata (E)
21552
21553 Besides being broken debug info, it can put GDB into an
21554 infinite loop. Consider:
21555
21556 When we're building the full name for Class<E>, we'll start
21557 at Class, and go look over its template type parameters,
21558 finding E. We'll then try to build the full name of E, and
21559 reach here. We're now trying to build the full name of E,
21560 and look over the parent DIE for containing scope. In the
21561 broken case, if we followed the parent DIE of E, we'd again
21562 find Class, and once again go look at its template type
21563 arguments, etc., etc. Simply don't consider such parent die
21564 as source-level parent of this die (it can't be, the language
21565 doesn't allow it), and break the loop here. */
21566 name = dwarf2_name (die, cu);
21567 parent_name = dwarf2_name (parent, cu);
b98664d3 21568 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21569 name ? name : "<unknown>",
21570 parent_name ? parent_name : "<unknown>");
21571 return "";
21572 }
63d06c5c 21573 else
0114d602
DJ
21574 switch (parent->tag)
21575 {
63d06c5c 21576 case DW_TAG_namespace:
0114d602 21577 parent_type = read_type_die (parent, cu);
acebe513
UW
21578 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21579 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21580 Work around this problem here. */
21581 if (cu->language == language_cplus
e86ca25f 21582 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21583 return "";
0114d602 21584 /* We give a name to even anonymous namespaces. */
e86ca25f 21585 return TYPE_NAME (parent_type);
63d06c5c 21586 case DW_TAG_class_type:
680b30c7 21587 case DW_TAG_interface_type:
63d06c5c 21588 case DW_TAG_structure_type:
0114d602 21589 case DW_TAG_union_type:
f55ee35c 21590 case DW_TAG_module:
0114d602 21591 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21592 if (TYPE_NAME (parent_type) != NULL)
21593 return TYPE_NAME (parent_type);
0114d602
DJ
21594 else
21595 /* An anonymous structure is only allowed non-static data
21596 members; no typedefs, no member functions, et cetera.
21597 So it does not need a prefix. */
21598 return "";
abc72ce4 21599 case DW_TAG_compile_unit:
95554aad 21600 case DW_TAG_partial_unit:
abc72ce4
DE
21601 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21602 if (cu->language == language_cplus
fd5866f6 21603 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21604 && die->child != NULL
21605 && (die->tag == DW_TAG_class_type
21606 || die->tag == DW_TAG_structure_type
21607 || die->tag == DW_TAG_union_type))
21608 {
43816ebc 21609 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21610 if (name != NULL)
21611 return name;
21612 }
21613 return "";
0a4b0913
AB
21614 case DW_TAG_subprogram:
21615 /* Nested subroutines in Fortran get a prefix with the name
21616 of the parent's subroutine. */
21617 if (cu->language == language_fortran)
21618 {
21619 if ((die->tag == DW_TAG_subprogram)
21620 && (dwarf2_name (parent, cu) != NULL))
21621 return dwarf2_name (parent, cu);
21622 }
21623 return determine_prefix (parent, cu);
3d567982
TT
21624 case DW_TAG_enumeration_type:
21625 parent_type = read_type_die (parent, cu);
21626 if (TYPE_DECLARED_CLASS (parent_type))
21627 {
e86ca25f
TT
21628 if (TYPE_NAME (parent_type) != NULL)
21629 return TYPE_NAME (parent_type);
3d567982
TT
21630 return "";
21631 }
21632 /* Fall through. */
63d06c5c 21633 default:
8176b9b8 21634 return determine_prefix (parent, cu);
63d06c5c 21635 }
63d06c5c
DC
21636}
21637
3e43a32a
MS
21638/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21639 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21640 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21641 an obconcat, otherwise allocate storage for the result. The CU argument is
21642 used to determine the language and hence, the appropriate separator. */
987504bb 21643
f55ee35c 21644#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21645
21646static char *
f55ee35c
JK
21647typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21648 int physname, struct dwarf2_cu *cu)
63d06c5c 21649{
f55ee35c 21650 const char *lead = "";
5c315b68 21651 const char *sep;
63d06c5c 21652
3e43a32a
MS
21653 if (suffix == NULL || suffix[0] == '\0'
21654 || prefix == NULL || prefix[0] == '\0')
987504bb 21655 sep = "";
45280282
IB
21656 else if (cu->language == language_d)
21657 {
21658 /* For D, the 'main' function could be defined in any module, but it
21659 should never be prefixed. */
21660 if (strcmp (suffix, "D main") == 0)
21661 {
21662 prefix = "";
21663 sep = "";
21664 }
21665 else
21666 sep = ".";
21667 }
f55ee35c
JK
21668 else if (cu->language == language_fortran && physname)
21669 {
21670 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21671 DW_AT_MIPS_linkage_name is preferred and used instead. */
21672
21673 lead = "__";
21674 sep = "_MOD_";
21675 }
987504bb
JJ
21676 else
21677 sep = "::";
63d06c5c 21678
6dd47d34
DE
21679 if (prefix == NULL)
21680 prefix = "";
21681 if (suffix == NULL)
21682 suffix = "";
21683
987504bb
JJ
21684 if (obs == NULL)
21685 {
3e43a32a 21686 char *retval
224c3ddb
SM
21687 = ((char *)
21688 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21689
f55ee35c
JK
21690 strcpy (retval, lead);
21691 strcat (retval, prefix);
6dd47d34
DE
21692 strcat (retval, sep);
21693 strcat (retval, suffix);
63d06c5c
DC
21694 return retval;
21695 }
987504bb
JJ
21696 else
21697 {
21698 /* We have an obstack. */
f55ee35c 21699 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21700 }
63d06c5c
DC
21701}
21702
c906108c
SS
21703/* Return sibling of die, NULL if no sibling. */
21704
f9aca02d 21705static struct die_info *
fba45db2 21706sibling_die (struct die_info *die)
c906108c 21707{
639d11d3 21708 return die->sibling;
c906108c
SS
21709}
21710
71c25dea
TT
21711/* Get name of a die, return NULL if not found. */
21712
15d034d0
TT
21713static const char *
21714dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21715 struct objfile *objfile)
71c25dea
TT
21716{
21717 if (name && cu->language == language_cplus)
21718 {
2f408ecb 21719 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21720
2f408ecb 21721 if (!canon_name.empty ())
71c25dea 21722 {
2f408ecb 21723 if (canon_name != name)
be1e3d3e 21724 name = objfile->intern (canon_name);
71c25dea
TT
21725 }
21726 }
21727
21728 return name;
c906108c
SS
21729}
21730
96553a0c
DE
21731/* Get name of a die, return NULL if not found.
21732 Anonymous namespaces are converted to their magic string. */
9219021c 21733
15d034d0 21734static const char *
e142c38c 21735dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21736{
21737 struct attribute *attr;
518817b3 21738 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21739
e142c38c 21740 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21741 if ((!attr || !DW_STRING (attr))
96553a0c 21742 && die->tag != DW_TAG_namespace
53832f31
TT
21743 && die->tag != DW_TAG_class_type
21744 && die->tag != DW_TAG_interface_type
21745 && die->tag != DW_TAG_structure_type
21746 && die->tag != DW_TAG_union_type)
71c25dea
TT
21747 return NULL;
21748
21749 switch (die->tag)
21750 {
21751 case DW_TAG_compile_unit:
95554aad 21752 case DW_TAG_partial_unit:
71c25dea
TT
21753 /* Compilation units have a DW_AT_name that is a filename, not
21754 a source language identifier. */
21755 case DW_TAG_enumeration_type:
21756 case DW_TAG_enumerator:
21757 /* These tags always have simple identifiers already; no need
21758 to canonicalize them. */
21759 return DW_STRING (attr);
907af001 21760
96553a0c
DE
21761 case DW_TAG_namespace:
21762 if (attr != NULL && DW_STRING (attr) != NULL)
21763 return DW_STRING (attr);
21764 return CP_ANONYMOUS_NAMESPACE_STR;
21765
907af001
UW
21766 case DW_TAG_class_type:
21767 case DW_TAG_interface_type:
21768 case DW_TAG_structure_type:
21769 case DW_TAG_union_type:
21770 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21771 structures or unions. These were of the form "._%d" in GCC 4.1,
21772 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21773 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21774 if (attr && DW_STRING (attr)
61012eef
GB
21775 && (startswith (DW_STRING (attr), "._")
21776 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21777 return NULL;
53832f31
TT
21778
21779 /* GCC might emit a nameless typedef that has a linkage name. See
21780 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21781 if (!attr || DW_STRING (attr) == NULL)
21782 {
73b9be8b 21783 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21784 if (attr == NULL || DW_STRING (attr) == NULL)
21785 return NULL;
21786
df5c6c50
JK
21787 /* Avoid demangling DW_STRING (attr) the second time on a second
21788 call for the same DIE. */
21789 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21790 {
43816ebc
TT
21791 gdb::unique_xmalloc_ptr<char> demangled
21792 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21793 if (demangled == nullptr)
21794 return nullptr;
43816ebc 21795
be1e3d3e 21796 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21797 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21798 }
67430cd0
TT
21799
21800 /* Strip any leading namespaces/classes, keep only the base name.
21801 DW_AT_name for named DIEs does not contain the prefixes. */
21802 const char *base = strrchr (DW_STRING (attr), ':');
21803 if (base && base > DW_STRING (attr) && base[-1] == ':')
21804 return &base[1];
21805 else
21806 return DW_STRING (attr);
53832f31 21807 }
907af001
UW
21808 break;
21809
71c25dea 21810 default:
907af001
UW
21811 break;
21812 }
21813
21814 if (!DW_STRING_IS_CANONICAL (attr))
21815 {
be1e3d3e
TT
21816 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21817 objfile);
907af001 21818 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21819 }
907af001 21820 return DW_STRING (attr);
9219021c
DC
21821}
21822
21823/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21824 is none. *EXT_CU is the CU containing DIE on input, and the CU
21825 containing the return value on output. */
9219021c
DC
21826
21827static struct die_info *
f2f0e013 21828dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21829{
21830 struct attribute *attr;
9219021c 21831
f2f0e013 21832 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21833 if (attr == NULL)
21834 return NULL;
21835
f2f0e013 21836 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21837}
21838
fa9c3fa0
TT
21839/* A convenience function that returns an "unknown" DWARF name,
21840 including the value of V. STR is the name of the entity being
21841 printed, e.g., "TAG". */
21842
21843static const char *
21844dwarf_unknown (const char *str, unsigned v)
21845{
21846 char *cell = get_print_cell ();
21847 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21848 return cell;
21849}
21850
c906108c
SS
21851/* Convert a DIE tag into its string name. */
21852
f39c6ffd 21853static const char *
aa1ee363 21854dwarf_tag_name (unsigned tag)
c906108c 21855{
f39c6ffd
TT
21856 const char *name = get_DW_TAG_name (tag);
21857
21858 if (name == NULL)
fa9c3fa0 21859 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
21860
21861 return name;
c906108c
SS
21862}
21863
21864/* Convert a DWARF attribute code into its string name. */
21865
f39c6ffd 21866static const char *
aa1ee363 21867dwarf_attr_name (unsigned attr)
c906108c 21868{
f39c6ffd
TT
21869 const char *name;
21870
c764a876 21871#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21872 if (attr == DW_AT_MIPS_fde)
21873 return "DW_AT_MIPS_fde";
21874#else
21875 if (attr == DW_AT_HP_block_index)
21876 return "DW_AT_HP_block_index";
c764a876 21877#endif
f39c6ffd
TT
21878
21879 name = get_DW_AT_name (attr);
21880
21881 if (name == NULL)
fa9c3fa0 21882 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
21883
21884 return name;
c906108c
SS
21885}
21886
21887/* Convert a DWARF value form code into its string name. */
21888
f39c6ffd 21889static const char *
aa1ee363 21890dwarf_form_name (unsigned form)
c906108c 21891{
f39c6ffd
TT
21892 const char *name = get_DW_FORM_name (form);
21893
21894 if (name == NULL)
fa9c3fa0 21895 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
21896
21897 return name;
c906108c
SS
21898}
21899
a121b7c1 21900static const char *
fba45db2 21901dwarf_bool_name (unsigned mybool)
c906108c
SS
21902{
21903 if (mybool)
21904 return "TRUE";
21905 else
21906 return "FALSE";
21907}
21908
21909/* Convert a DWARF type code into its string name. */
21910
f39c6ffd 21911static const char *
aa1ee363 21912dwarf_type_encoding_name (unsigned enc)
c906108c 21913{
f39c6ffd 21914 const char *name = get_DW_ATE_name (enc);
c906108c 21915
f39c6ffd 21916 if (name == NULL)
fa9c3fa0 21917 return dwarf_unknown ("ATE", enc);
c906108c 21918
f39c6ffd 21919 return name;
c906108c 21920}
c906108c 21921
f9aca02d 21922static void
d97bc12b 21923dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21924{
21925 unsigned int i;
21926
d97bc12b 21927 print_spaces (indent, f);
9d8780f0 21928 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21929 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21930 sect_offset_str (die->sect_off));
d97bc12b
DE
21931
21932 if (die->parent != NULL)
21933 {
21934 print_spaces (indent, f);
9d8780f0
SM
21935 fprintf_unfiltered (f, " parent at offset: %s\n",
21936 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21937 }
21938
21939 print_spaces (indent, f);
21940 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21941 dwarf_bool_name (die->child != NULL));
c906108c 21942
d97bc12b
DE
21943 print_spaces (indent, f);
21944 fprintf_unfiltered (f, " attributes:\n");
21945
c906108c
SS
21946 for (i = 0; i < die->num_attrs; ++i)
21947 {
d97bc12b
DE
21948 print_spaces (indent, f);
21949 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21950 dwarf_attr_name (die->attrs[i].name),
21951 dwarf_form_name (die->attrs[i].form));
d97bc12b 21952
c906108c
SS
21953 switch (die->attrs[i].form)
21954 {
c906108c 21955 case DW_FORM_addr:
336d760d 21956 case DW_FORM_addrx:
3019eac3 21957 case DW_FORM_GNU_addr_index:
d97bc12b 21958 fprintf_unfiltered (f, "address: ");
5af949e3 21959 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21960 break;
21961 case DW_FORM_block2:
21962 case DW_FORM_block4:
21963 case DW_FORM_block:
21964 case DW_FORM_block1:
56eb65bd
SP
21965 fprintf_unfiltered (f, "block: size %s",
21966 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21967 break;
2dc7f7b3 21968 case DW_FORM_exprloc:
56eb65bd
SP
21969 fprintf_unfiltered (f, "expression: size %s",
21970 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21971 break;
0224619f
JK
21972 case DW_FORM_data16:
21973 fprintf_unfiltered (f, "constant of 16 bytes");
21974 break;
4568ecf9
DE
21975 case DW_FORM_ref_addr:
21976 fprintf_unfiltered (f, "ref address: ");
21977 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21978 break;
36586728
TT
21979 case DW_FORM_GNU_ref_alt:
21980 fprintf_unfiltered (f, "alt ref address: ");
21981 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21982 break;
10b3939b
DJ
21983 case DW_FORM_ref1:
21984 case DW_FORM_ref2:
21985 case DW_FORM_ref4:
4568ecf9
DE
21986 case DW_FORM_ref8:
21987 case DW_FORM_ref_udata:
d97bc12b 21988 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21989 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21990 break;
c906108c
SS
21991 case DW_FORM_data1:
21992 case DW_FORM_data2:
21993 case DW_FORM_data4:
ce5d95e1 21994 case DW_FORM_data8:
c906108c
SS
21995 case DW_FORM_udata:
21996 case DW_FORM_sdata:
43bbcdc2
PH
21997 fprintf_unfiltered (f, "constant: %s",
21998 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21999 break;
2dc7f7b3
TT
22000 case DW_FORM_sec_offset:
22001 fprintf_unfiltered (f, "section offset: %s",
22002 pulongest (DW_UNSND (&die->attrs[i])));
22003 break;
55f1336d 22004 case DW_FORM_ref_sig8:
ac9ec31b
DE
22005 fprintf_unfiltered (f, "signature: %s",
22006 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22007 break;
c906108c 22008 case DW_FORM_string:
4bdf3d34 22009 case DW_FORM_strp:
43988095 22010 case DW_FORM_line_strp:
cf532bd1 22011 case DW_FORM_strx:
3019eac3 22012 case DW_FORM_GNU_str_index:
36586728 22013 case DW_FORM_GNU_strp_alt:
8285870a 22014 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22015 DW_STRING (&die->attrs[i])
8285870a
JK
22016 ? DW_STRING (&die->attrs[i]) : "",
22017 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22018 break;
22019 case DW_FORM_flag:
22020 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22021 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22022 else
d97bc12b 22023 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22024 break;
2dc7f7b3
TT
22025 case DW_FORM_flag_present:
22026 fprintf_unfiltered (f, "flag: TRUE");
22027 break;
a8329558 22028 case DW_FORM_indirect:
0963b4bd
MS
22029 /* The reader will have reduced the indirect form to
22030 the "base form" so this form should not occur. */
5f48f8f3 22031 fprintf_unfiltered (f,
3e43a32a 22032 "unexpected attribute form: DW_FORM_indirect");
a8329558 22033 break;
663c44ac
JK
22034 case DW_FORM_implicit_const:
22035 fprintf_unfiltered (f, "constant: %s",
22036 plongest (DW_SND (&die->attrs[i])));
22037 break;
c906108c 22038 default:
d97bc12b 22039 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22040 die->attrs[i].form);
d97bc12b 22041 break;
c906108c 22042 }
d97bc12b 22043 fprintf_unfiltered (f, "\n");
c906108c
SS
22044 }
22045}
22046
f9aca02d 22047static void
d97bc12b 22048dump_die_for_error (struct die_info *die)
c906108c 22049{
d97bc12b
DE
22050 dump_die_shallow (gdb_stderr, 0, die);
22051}
22052
22053static void
22054dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22055{
22056 int indent = level * 4;
22057
22058 gdb_assert (die != NULL);
22059
22060 if (level >= max_level)
22061 return;
22062
22063 dump_die_shallow (f, indent, die);
22064
22065 if (die->child != NULL)
c906108c 22066 {
d97bc12b
DE
22067 print_spaces (indent, f);
22068 fprintf_unfiltered (f, " Children:");
22069 if (level + 1 < max_level)
22070 {
22071 fprintf_unfiltered (f, "\n");
22072 dump_die_1 (f, level + 1, max_level, die->child);
22073 }
22074 else
22075 {
3e43a32a
MS
22076 fprintf_unfiltered (f,
22077 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22078 }
22079 }
22080
22081 if (die->sibling != NULL && level > 0)
22082 {
22083 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22084 }
22085}
22086
d97bc12b
DE
22087/* This is called from the pdie macro in gdbinit.in.
22088 It's not static so gcc will keep a copy callable from gdb. */
22089
22090void
22091dump_die (struct die_info *die, int max_level)
22092{
22093 dump_die_1 (gdb_stdlog, 0, max_level, die);
22094}
22095
f9aca02d 22096static void
51545339 22097store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22098{
51545339 22099 void **slot;
c906108c 22100
9c541725
PA
22101 slot = htab_find_slot_with_hash (cu->die_hash, die,
22102 to_underlying (die->sect_off),
b64f50a1 22103 INSERT);
51545339
DJ
22104
22105 *slot = die;
c906108c
SS
22106}
22107
b64f50a1
JK
22108/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22109 required kind. */
22110
22111static sect_offset
ff39bb5e 22112dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22113{
cd6c91b4 22114 if (attr->form_is_ref ())
9c541725 22115 return (sect_offset) DW_UNSND (attr);
93311388 22116
b98664d3 22117 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22118 dwarf_form_name (attr->form));
9c541725 22119 return {};
c906108c
SS
22120}
22121
43bbcdc2
PH
22122/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22123 * the value held by the attribute is not constant. */
a02abb62 22124
43bbcdc2 22125static LONGEST
ff39bb5e 22126dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22127{
663c44ac 22128 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22129 return DW_SND (attr);
22130 else if (attr->form == DW_FORM_udata
22131 || attr->form == DW_FORM_data1
22132 || attr->form == DW_FORM_data2
22133 || attr->form == DW_FORM_data4
22134 || attr->form == DW_FORM_data8)
22135 return DW_UNSND (attr);
22136 else
22137 {
cd6c91b4 22138 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22139 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22140 dwarf_form_name (attr->form));
22141 return default_value;
22142 }
22143}
22144
348e048f
DE
22145/* Follow reference or signature attribute ATTR of SRC_DIE.
22146 On entry *REF_CU is the CU of SRC_DIE.
22147 On exit *REF_CU is the CU of the result. */
22148
22149static struct die_info *
ff39bb5e 22150follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22151 struct dwarf2_cu **ref_cu)
22152{
22153 struct die_info *die;
22154
cd6c91b4 22155 if (attr->form_is_ref ())
348e048f 22156 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22157 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22158 die = follow_die_sig (src_die, attr, ref_cu);
22159 else
22160 {
22161 dump_die_for_error (src_die);
22162 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22163 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22164 }
22165
22166 return die;
03dd20cc
DJ
22167}
22168
5c631832 22169/* Follow reference OFFSET.
673bfd45
DE
22170 On entry *REF_CU is the CU of the source die referencing OFFSET.
22171 On exit *REF_CU is the CU of the result.
22172 Returns NULL if OFFSET is invalid. */
f504f079 22173
f9aca02d 22174static struct die_info *
9c541725 22175follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22176 struct dwarf2_cu **ref_cu)
c906108c 22177{
10b3939b 22178 struct die_info temp_die;
f2f0e013 22179 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22180 struct dwarf2_per_objfile *dwarf2_per_objfile
22181 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22182
348e048f
DE
22183 gdb_assert (cu->per_cu != NULL);
22184
98bfdba5
PA
22185 target_cu = cu;
22186
3019eac3 22187 if (cu->per_cu->is_debug_types)
348e048f
DE
22188 {
22189 /* .debug_types CUs cannot reference anything outside their CU.
22190 If they need to, they have to reference a signatured type via
55f1336d 22191 DW_FORM_ref_sig8. */
4057dfde 22192 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22193 return NULL;
348e048f 22194 }
36586728 22195 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22196 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22197 {
22198 struct dwarf2_per_cu_data *per_cu;
9a619af0 22199
9c541725 22200 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22201 dwarf2_per_objfile);
03dd20cc
DJ
22202
22203 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22204 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22205 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22206
10b3939b
DJ
22207 target_cu = per_cu->cu;
22208 }
98bfdba5
PA
22209 else if (cu->dies == NULL)
22210 {
22211 /* We're loading full DIEs during partial symbol reading. */
22212 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22213 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22214 }
c906108c 22215
f2f0e013 22216 *ref_cu = target_cu;
9c541725 22217 temp_die.sect_off = sect_off;
c24bdb02
KS
22218
22219 if (target_cu != cu)
22220 target_cu->ancestor = cu;
22221
9a3c8263 22222 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22223 &temp_die,
22224 to_underlying (sect_off));
5c631832 22225}
10b3939b 22226
5c631832
JK
22227/* Follow reference attribute ATTR of SRC_DIE.
22228 On entry *REF_CU is the CU of SRC_DIE.
22229 On exit *REF_CU is the CU of the result. */
22230
22231static struct die_info *
ff39bb5e 22232follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22233 struct dwarf2_cu **ref_cu)
22234{
9c541725 22235 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22236 struct dwarf2_cu *cu = *ref_cu;
22237 struct die_info *die;
22238
9c541725 22239 die = follow_die_offset (sect_off,
36586728
TT
22240 (attr->form == DW_FORM_GNU_ref_alt
22241 || cu->per_cu->is_dwz),
22242 ref_cu);
5c631832 22243 if (!die)
9d8780f0
SM
22244 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22245 "at %s [in module %s]"),
22246 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22247 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22248
5c631832
JK
22249 return die;
22250}
22251
d4c9a4f8 22252/* See read.h. */
5c631832
JK
22253
22254struct dwarf2_locexpr_baton
9c541725 22255dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22256 dwarf2_per_cu_data *per_cu,
8b9737bf 22257 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22258 void *baton, bool resolve_abstract_p)
5c631832 22259{
918dd910 22260 struct dwarf2_cu *cu;
5c631832
JK
22261 struct die_info *die;
22262 struct attribute *attr;
22263 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22264 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22265 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22266
918dd910 22267 if (per_cu->cu == NULL)
58f0c718 22268 load_cu (per_cu, false);
918dd910 22269 cu = per_cu->cu;
cc12ce38
DE
22270 if (cu == NULL)
22271 {
22272 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22273 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22274 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22275 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22276 }
918dd910 22277
9c541725 22278 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22279 if (!die)
9d8780f0
SM
22280 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22281 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22282
22283 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22284 if (!attr && resolve_abstract_p
3360b6e7 22285 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22286 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22287 {
22288 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22289 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 22290 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 22291
3360b6e7
TV
22292 for (const auto &cand_off
22293 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22294 {
3360b6e7
TV
22295 struct dwarf2_cu *cand_cu = cu;
22296 struct die_info *cand
22297 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22298 if (!cand
22299 || !cand->parent
e4a62c65
TV
22300 || cand->parent->tag != DW_TAG_subprogram)
22301 continue;
22302
22303 CORE_ADDR pc_low, pc_high;
22304 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22305 if (pc_low == ((CORE_ADDR) -1))
22306 continue;
22307 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22308 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22309 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22310 continue;
22311
22312 die = cand;
22313 attr = dwarf2_attr (die, DW_AT_location, cu);
22314 break;
22315 }
22316 }
22317
5c631832
JK
22318 if (!attr)
22319 {
e103e986
JK
22320 /* DWARF: "If there is no such attribute, then there is no effect.".
22321 DATA is ignored if SIZE is 0. */
5c631832 22322
e103e986 22323 retval.data = NULL;
5c631832
JK
22324 retval.size = 0;
22325 }
cd6c91b4 22326 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22327 {
22328 struct dwarf2_loclist_baton loclist_baton;
22329 CORE_ADDR pc = (*get_frame_pc) (baton);
22330 size_t size;
22331
22332 fill_in_loclist_baton (cu, &loclist_baton, attr);
22333
22334 retval.data = dwarf2_find_location_expression (&loclist_baton,
22335 &size, pc);
22336 retval.size = size;
22337 }
5c631832
JK
22338 else
22339 {
4fc6c0d5 22340 if (!attr->form_is_block ())
9d8780f0 22341 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22342 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22343 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22344
22345 retval.data = DW_BLOCK (attr)->data;
22346 retval.size = DW_BLOCK (attr)->size;
22347 }
22348 retval.per_cu = cu->per_cu;
918dd910 22349
ed2dc618 22350 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22351
5c631832 22352 return retval;
348e048f
DE
22353}
22354
d4c9a4f8 22355/* See read.h. */
8b9737bf
TT
22356
22357struct dwarf2_locexpr_baton
22358dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22359 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22360 CORE_ADDR (*get_frame_pc) (void *baton),
22361 void *baton)
22362{
9c541725 22363 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22364
9c541725 22365 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22366}
22367
b6807d98
TT
22368/* Write a constant of a given type as target-ordered bytes into
22369 OBSTACK. */
22370
22371static const gdb_byte *
22372write_constant_as_bytes (struct obstack *obstack,
22373 enum bfd_endian byte_order,
22374 struct type *type,
22375 ULONGEST value,
22376 LONGEST *len)
22377{
22378 gdb_byte *result;
22379
22380 *len = TYPE_LENGTH (type);
224c3ddb 22381 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22382 store_unsigned_integer (result, *len, byte_order, value);
22383
22384 return result;
22385}
22386
d4c9a4f8 22387/* See read.h. */
b6807d98
TT
22388
22389const gdb_byte *
9c541725 22390dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22391 dwarf2_per_cu_data *per_cu,
22392 obstack *obstack,
b6807d98
TT
22393 LONGEST *len)
22394{
22395 struct dwarf2_cu *cu;
22396 struct die_info *die;
22397 struct attribute *attr;
22398 const gdb_byte *result = NULL;
22399 struct type *type;
22400 LONGEST value;
22401 enum bfd_endian byte_order;
e3b94546 22402 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22403
b6807d98 22404 if (per_cu->cu == NULL)
58f0c718 22405 load_cu (per_cu, false);
b6807d98 22406 cu = per_cu->cu;
cc12ce38
DE
22407 if (cu == NULL)
22408 {
22409 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22410 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22411 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22412 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22413 }
b6807d98 22414
9c541725 22415 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22416 if (!die)
9d8780f0
SM
22417 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22418 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22419
22420 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22421 if (attr == NULL)
22422 return NULL;
22423
e3b94546 22424 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22425 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22426
22427 switch (attr->form)
22428 {
22429 case DW_FORM_addr:
336d760d 22430 case DW_FORM_addrx:
b6807d98
TT
22431 case DW_FORM_GNU_addr_index:
22432 {
22433 gdb_byte *tem;
22434
22435 *len = cu->header.addr_size;
224c3ddb 22436 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22437 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22438 result = tem;
22439 }
22440 break;
22441 case DW_FORM_string:
22442 case DW_FORM_strp:
cf532bd1 22443 case DW_FORM_strx:
b6807d98
TT
22444 case DW_FORM_GNU_str_index:
22445 case DW_FORM_GNU_strp_alt:
22446 /* DW_STRING is already allocated on the objfile obstack, point
22447 directly to it. */
22448 result = (const gdb_byte *) DW_STRING (attr);
22449 *len = strlen (DW_STRING (attr));
22450 break;
22451 case DW_FORM_block1:
22452 case DW_FORM_block2:
22453 case DW_FORM_block4:
22454 case DW_FORM_block:
22455 case DW_FORM_exprloc:
0224619f 22456 case DW_FORM_data16:
b6807d98
TT
22457 result = DW_BLOCK (attr)->data;
22458 *len = DW_BLOCK (attr)->size;
22459 break;
22460
22461 /* The DW_AT_const_value attributes are supposed to carry the
22462 symbol's value "represented as it would be on the target
22463 architecture." By the time we get here, it's already been
22464 converted to host endianness, so we just need to sign- or
22465 zero-extend it as appropriate. */
22466 case DW_FORM_data1:
22467 type = die_type (die, cu);
22468 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22469 if (result == NULL)
22470 result = write_constant_as_bytes (obstack, byte_order,
22471 type, value, len);
22472 break;
22473 case DW_FORM_data2:
22474 type = die_type (die, cu);
22475 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22476 if (result == NULL)
22477 result = write_constant_as_bytes (obstack, byte_order,
22478 type, value, len);
22479 break;
22480 case DW_FORM_data4:
22481 type = die_type (die, cu);
22482 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22483 if (result == NULL)
22484 result = write_constant_as_bytes (obstack, byte_order,
22485 type, value, len);
22486 break;
22487 case DW_FORM_data8:
22488 type = die_type (die, cu);
22489 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22490 if (result == NULL)
22491 result = write_constant_as_bytes (obstack, byte_order,
22492 type, value, len);
22493 break;
22494
22495 case DW_FORM_sdata:
663c44ac 22496 case DW_FORM_implicit_const:
b6807d98
TT
22497 type = die_type (die, cu);
22498 result = write_constant_as_bytes (obstack, byte_order,
22499 type, DW_SND (attr), len);
22500 break;
22501
22502 case DW_FORM_udata:
22503 type = die_type (die, cu);
22504 result = write_constant_as_bytes (obstack, byte_order,
22505 type, DW_UNSND (attr), len);
22506 break;
22507
22508 default:
b98664d3 22509 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22510 dwarf_form_name (attr->form));
22511 break;
22512 }
22513
22514 return result;
22515}
22516
d4c9a4f8 22517/* See read.h. */
7942e96e
AA
22518
22519struct type *
9c541725 22520dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22521 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22522{
22523 struct dwarf2_cu *cu;
22524 struct die_info *die;
22525
7942e96e 22526 if (per_cu->cu == NULL)
58f0c718 22527 load_cu (per_cu, false);
7942e96e
AA
22528 cu = per_cu->cu;
22529 if (!cu)
22530 return NULL;
22531
9c541725 22532 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22533 if (!die)
22534 return NULL;
22535
22536 return die_type (die, cu);
22537}
22538
8cb5117c 22539/* See read.h. */
8a9b8146
TT
22540
22541struct type *
b64f50a1 22542dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22543 struct dwarf2_per_cu_data *per_cu)
22544{
9c541725 22545 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22546 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22547}
22548
ac9ec31b 22549/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22550 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22551 On exit *REF_CU is the CU of the result.
22552 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22553
22554static struct die_info *
ac9ec31b
DE
22555follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22556 struct dwarf2_cu **ref_cu)
348e048f 22557{
348e048f 22558 struct die_info temp_die;
c24bdb02 22559 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22560 struct die_info *die;
22561
ac9ec31b
DE
22562 /* While it might be nice to assert sig_type->type == NULL here,
22563 we can get here for DW_AT_imported_declaration where we need
22564 the DIE not the type. */
348e048f
DE
22565
22566 /* If necessary, add it to the queue and load its DIEs. */
22567
95554aad 22568 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22569 read_signatured_type (sig_type);
348e048f 22570
348e048f 22571 sig_cu = sig_type->per_cu.cu;
69d751e3 22572 gdb_assert (sig_cu != NULL);
9c541725
PA
22573 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22574 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22575 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22576 to_underlying (temp_die.sect_off));
348e048f
DE
22577 if (die)
22578 {
ed2dc618 22579 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22580 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22581
796a7ff8
DE
22582 /* For .gdb_index version 7 keep track of included TUs.
22583 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22584 if (dwarf2_per_objfile->index_table != NULL
22585 && dwarf2_per_objfile->index_table->version <= 7)
22586 {
ae640021 22587 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22588 }
22589
348e048f 22590 *ref_cu = sig_cu;
c24bdb02
KS
22591 if (sig_cu != cu)
22592 sig_cu->ancestor = cu;
22593
348e048f
DE
22594 return die;
22595 }
22596
ac9ec31b
DE
22597 return NULL;
22598}
22599
22600/* Follow signatured type referenced by ATTR in SRC_DIE.
22601 On entry *REF_CU is the CU of SRC_DIE.
22602 On exit *REF_CU is the CU of the result.
22603 The result is the DIE of the type.
22604 If the referenced type cannot be found an error is thrown. */
22605
22606static struct die_info *
ff39bb5e 22607follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22608 struct dwarf2_cu **ref_cu)
22609{
22610 ULONGEST signature = DW_SIGNATURE (attr);
22611 struct signatured_type *sig_type;
22612 struct die_info *die;
22613
22614 gdb_assert (attr->form == DW_FORM_ref_sig8);
22615
a2ce51a0 22616 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22617 /* sig_type will be NULL if the signatured type is missing from
22618 the debug info. */
22619 if (sig_type == NULL)
22620 {
22621 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22622 " from DIE at %s [in module %s]"),
22623 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22624 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22625 }
22626
22627 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22628 if (die == NULL)
22629 {
22630 dump_die_for_error (src_die);
22631 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22632 " from DIE at %s [in module %s]"),
22633 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22634 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22635 }
22636
22637 return die;
22638}
22639
22640/* Get the type specified by SIGNATURE referenced in DIE/CU,
22641 reading in and processing the type unit if necessary. */
22642
22643static struct type *
22644get_signatured_type (struct die_info *die, ULONGEST signature,
22645 struct dwarf2_cu *cu)
22646{
518817b3
SM
22647 struct dwarf2_per_objfile *dwarf2_per_objfile
22648 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22649 struct signatured_type *sig_type;
22650 struct dwarf2_cu *type_cu;
22651 struct die_info *type_die;
22652 struct type *type;
22653
a2ce51a0 22654 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22655 /* sig_type will be NULL if the signatured type is missing from
22656 the debug info. */
22657 if (sig_type == NULL)
22658 {
b98664d3 22659 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22660 " from DIE at %s [in module %s]"),
22661 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22662 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22663 return build_error_marker_type (cu, die);
22664 }
22665
22666 /* If we already know the type we're done. */
22667 if (sig_type->type != NULL)
22668 return sig_type->type;
22669
22670 type_cu = cu;
22671 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22672 if (type_die != NULL)
22673 {
22674 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22675 is created. This is important, for example, because for c++ classes
22676 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22677 type = read_type_die (type_die, type_cu);
22678 if (type == NULL)
22679 {
b98664d3 22680 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22681 " referenced from DIE at %s [in module %s]"),
22682 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22683 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22684 type = build_error_marker_type (cu, die);
22685 }
22686 }
22687 else
22688 {
b98664d3 22689 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22690 " from DIE at %s [in module %s]"),
22691 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22692 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22693 type = build_error_marker_type (cu, die);
22694 }
22695 sig_type->type = type;
22696
22697 return type;
22698}
22699
22700/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22701 reading in and processing the type unit if necessary. */
22702
22703static struct type *
ff39bb5e 22704get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22705 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22706{
22707 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22708 if (attr->form_is_ref ())
ac9ec31b
DE
22709 {
22710 struct dwarf2_cu *type_cu = cu;
22711 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22712
22713 return read_type_die (type_die, type_cu);
22714 }
22715 else if (attr->form == DW_FORM_ref_sig8)
22716 {
22717 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22718 }
22719 else
22720 {
518817b3
SM
22721 struct dwarf2_per_objfile *dwarf2_per_objfile
22722 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22723
b98664d3 22724 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22725 " at %s [in module %s]"),
22726 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22727 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22728 return build_error_marker_type (cu, die);
22729 }
348e048f
DE
22730}
22731
e5fe5e75 22732/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22733
22734static void
e5fe5e75 22735load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22736{
52dc124a 22737 struct signatured_type *sig_type;
348e048f 22738
f4dc4d17 22739 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22740 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22741
6721b2ec
DE
22742 /* We have the per_cu, but we need the signatured_type.
22743 Fortunately this is an easy translation. */
22744 gdb_assert (per_cu->is_debug_types);
22745 sig_type = (struct signatured_type *) per_cu;
348e048f 22746
6721b2ec 22747 gdb_assert (per_cu->cu == NULL);
348e048f 22748
52dc124a 22749 read_signatured_type (sig_type);
348e048f 22750
6721b2ec 22751 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22752}
22753
3019eac3
DE
22754/* Read in a signatured type and build its CU and DIEs.
22755 If the type is a stub for the real type in a DWO file,
22756 read in the real type from the DWO file as well. */
dee91e82
DE
22757
22758static void
22759read_signatured_type (struct signatured_type *sig_type)
22760{
22761 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22762
3019eac3 22763 gdb_assert (per_cu->is_debug_types);
dee91e82 22764 gdb_assert (per_cu->cu == NULL);
348e048f 22765
6751ebae 22766 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22767
22768 if (!reader.dummy_p)
22769 {
22770 struct dwarf2_cu *cu = reader.cu;
22771 const gdb_byte *info_ptr = reader.info_ptr;
22772
22773 gdb_assert (cu->die_hash == NULL);
22774 cu->die_hash =
22775 htab_create_alloc_ex (cu->header.length / 12,
22776 die_hash,
22777 die_eq,
22778 NULL,
22779 &cu->comp_unit_obstack,
22780 hashtab_obstack_allocate,
22781 dummy_obstack_deallocate);
22782
3e225074 22783 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22784 reader.comp_unit_die->child
22785 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22786 reader.comp_unit_die);
22787 cu->dies = reader.comp_unit_die;
22788 /* comp_unit_die is not stored in die_hash, no need. */
22789
22790 /* We try not to read any attributes in this function, because
22791 not all CUs needed for references have been loaded yet, and
22792 symbol table processing isn't initialized. But we have to
22793 set the CU language, or we won't be able to build types
22794 correctly. Similarly, if we do not read the producer, we can
22795 not apply producer-specific interpretation. */
22796 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22797
22798 reader.keep ();
c0ab21c2
TT
22799 }
22800
7ee85ab1 22801 sig_type->per_cu.tu_read = 1;
c906108c
SS
22802}
22803
c906108c
SS
22804/* Decode simple location descriptions.
22805 Given a pointer to a dwarf block that defines a location, compute
22806 the location and return the value.
22807
4cecd739
DJ
22808 NOTE drow/2003-11-18: This function is called in two situations
22809 now: for the address of static or global variables (partial symbols
22810 only) and for offsets into structures which are expected to be
22811 (more or less) constant. The partial symbol case should go away,
22812 and only the constant case should remain. That will let this
22813 function complain more accurately. A few special modes are allowed
22814 without complaint for global variables (for instance, global
22815 register values and thread-local values).
c906108c
SS
22816
22817 A location description containing no operations indicates that the
4cecd739 22818 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22819 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22820 callers will only want a very basic result and this can become a
21ae7a4d
JK
22821 complaint.
22822
22823 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22824
22825static CORE_ADDR
e7c27a73 22826decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22827{
518817b3 22828 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22829 size_t i;
22830 size_t size = blk->size;
d521ce57 22831 const gdb_byte *data = blk->data;
21ae7a4d
JK
22832 CORE_ADDR stack[64];
22833 int stacki;
22834 unsigned int bytes_read, unsnd;
22835 gdb_byte op;
c906108c 22836
21ae7a4d
JK
22837 i = 0;
22838 stacki = 0;
22839 stack[stacki] = 0;
22840 stack[++stacki] = 0;
22841
22842 while (i < size)
22843 {
22844 op = data[i++];
22845 switch (op)
22846 {
22847 case DW_OP_lit0:
22848 case DW_OP_lit1:
22849 case DW_OP_lit2:
22850 case DW_OP_lit3:
22851 case DW_OP_lit4:
22852 case DW_OP_lit5:
22853 case DW_OP_lit6:
22854 case DW_OP_lit7:
22855 case DW_OP_lit8:
22856 case DW_OP_lit9:
22857 case DW_OP_lit10:
22858 case DW_OP_lit11:
22859 case DW_OP_lit12:
22860 case DW_OP_lit13:
22861 case DW_OP_lit14:
22862 case DW_OP_lit15:
22863 case DW_OP_lit16:
22864 case DW_OP_lit17:
22865 case DW_OP_lit18:
22866 case DW_OP_lit19:
22867 case DW_OP_lit20:
22868 case DW_OP_lit21:
22869 case DW_OP_lit22:
22870 case DW_OP_lit23:
22871 case DW_OP_lit24:
22872 case DW_OP_lit25:
22873 case DW_OP_lit26:
22874 case DW_OP_lit27:
22875 case DW_OP_lit28:
22876 case DW_OP_lit29:
22877 case DW_OP_lit30:
22878 case DW_OP_lit31:
22879 stack[++stacki] = op - DW_OP_lit0;
22880 break;
f1bea926 22881
21ae7a4d
JK
22882 case DW_OP_reg0:
22883 case DW_OP_reg1:
22884 case DW_OP_reg2:
22885 case DW_OP_reg3:
22886 case DW_OP_reg4:
22887 case DW_OP_reg5:
22888 case DW_OP_reg6:
22889 case DW_OP_reg7:
22890 case DW_OP_reg8:
22891 case DW_OP_reg9:
22892 case DW_OP_reg10:
22893 case DW_OP_reg11:
22894 case DW_OP_reg12:
22895 case DW_OP_reg13:
22896 case DW_OP_reg14:
22897 case DW_OP_reg15:
22898 case DW_OP_reg16:
22899 case DW_OP_reg17:
22900 case DW_OP_reg18:
22901 case DW_OP_reg19:
22902 case DW_OP_reg20:
22903 case DW_OP_reg21:
22904 case DW_OP_reg22:
22905 case DW_OP_reg23:
22906 case DW_OP_reg24:
22907 case DW_OP_reg25:
22908 case DW_OP_reg26:
22909 case DW_OP_reg27:
22910 case DW_OP_reg28:
22911 case DW_OP_reg29:
22912 case DW_OP_reg30:
22913 case DW_OP_reg31:
22914 stack[++stacki] = op - DW_OP_reg0;
22915 if (i < size)
22916 dwarf2_complex_location_expr_complaint ();
22917 break;
c906108c 22918
21ae7a4d
JK
22919 case DW_OP_regx:
22920 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22921 i += bytes_read;
22922 stack[++stacki] = unsnd;
22923 if (i < size)
22924 dwarf2_complex_location_expr_complaint ();
22925 break;
c906108c 22926
21ae7a4d 22927 case DW_OP_addr:
c8a7a66f
TT
22928 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22929 &bytes_read);
21ae7a4d
JK
22930 i += bytes_read;
22931 break;
d53d4ac5 22932
21ae7a4d
JK
22933 case DW_OP_const1u:
22934 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22935 i += 1;
22936 break;
22937
22938 case DW_OP_const1s:
22939 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22940 i += 1;
22941 break;
22942
22943 case DW_OP_const2u:
22944 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22945 i += 2;
22946 break;
22947
22948 case DW_OP_const2s:
22949 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22950 i += 2;
22951 break;
d53d4ac5 22952
21ae7a4d
JK
22953 case DW_OP_const4u:
22954 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22955 i += 4;
22956 break;
22957
22958 case DW_OP_const4s:
22959 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22960 i += 4;
22961 break;
22962
585861ea
JK
22963 case DW_OP_const8u:
22964 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22965 i += 8;
22966 break;
22967
21ae7a4d
JK
22968 case DW_OP_constu:
22969 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22970 &bytes_read);
22971 i += bytes_read;
22972 break;
22973
22974 case DW_OP_consts:
22975 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22976 i += bytes_read;
22977 break;
22978
22979 case DW_OP_dup:
22980 stack[stacki + 1] = stack[stacki];
22981 stacki++;
22982 break;
22983
22984 case DW_OP_plus:
22985 stack[stacki - 1] += stack[stacki];
22986 stacki--;
22987 break;
22988
22989 case DW_OP_plus_uconst:
22990 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22991 &bytes_read);
22992 i += bytes_read;
22993 break;
22994
22995 case DW_OP_minus:
22996 stack[stacki - 1] -= stack[stacki];
22997 stacki--;
22998 break;
22999
23000 case DW_OP_deref:
23001 /* If we're not the last op, then we definitely can't encode
23002 this using GDB's address_class enum. This is valid for partial
23003 global symbols, although the variable's address will be bogus
23004 in the psymtab. */
23005 if (i < size)
23006 dwarf2_complex_location_expr_complaint ();
23007 break;
23008
23009 case DW_OP_GNU_push_tls_address:
4aa4e28b 23010 case DW_OP_form_tls_address:
21ae7a4d
JK
23011 /* The top of the stack has the offset from the beginning
23012 of the thread control block at which the variable is located. */
23013 /* Nothing should follow this operator, so the top of stack would
23014 be returned. */
23015 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23016 address will be bogus in the psymtab. Make it always at least
23017 non-zero to not look as a variable garbage collected by linker
23018 which have DW_OP_addr 0. */
21ae7a4d
JK
23019 if (i < size)
23020 dwarf2_complex_location_expr_complaint ();
585861ea 23021 stack[stacki]++;
21ae7a4d
JK
23022 break;
23023
23024 case DW_OP_GNU_uninit:
23025 break;
23026
336d760d 23027 case DW_OP_addrx:
3019eac3 23028 case DW_OP_GNU_addr_index:
49f6c839 23029 case DW_OP_GNU_const_index:
3019eac3
DE
23030 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23031 &bytes_read);
23032 i += bytes_read;
23033 break;
23034
21ae7a4d
JK
23035 default:
23036 {
f39c6ffd 23037 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23038
23039 if (name)
b98664d3 23040 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23041 name);
23042 else
b98664d3 23043 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23044 op);
23045 }
23046
23047 return (stack[stacki]);
d53d4ac5 23048 }
3c6e0cb3 23049
21ae7a4d
JK
23050 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23051 outside of the allocated space. Also enforce minimum>0. */
23052 if (stacki >= ARRAY_SIZE (stack) - 1)
23053 {
b98664d3 23054 complaint (_("location description stack overflow"));
21ae7a4d
JK
23055 return 0;
23056 }
23057
23058 if (stacki <= 0)
23059 {
b98664d3 23060 complaint (_("location description stack underflow"));
21ae7a4d
JK
23061 return 0;
23062 }
23063 }
23064 return (stack[stacki]);
c906108c
SS
23065}
23066
23067/* memory allocation interface */
23068
c906108c 23069static struct dwarf_block *
7b5a2f43 23070dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23071{
8d749320 23072 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23073}
23074
c906108c 23075static struct die_info *
b60c80d6 23076dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23077{
23078 struct die_info *die;
b60c80d6
DJ
23079 size_t size = sizeof (struct die_info);
23080
23081 if (num_attrs > 1)
23082 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23083
b60c80d6 23084 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23085 memset (die, 0, sizeof (struct die_info));
23086 return (die);
23087}
2e276125
JB
23088
23089\f
23090/* Macro support. */
23091
2e276125 23092static struct macro_source_file *
bf80d710 23093macro_start_file (buildsym_compunit *builder,
804d2729 23094 int file, int line,
2e276125 23095 struct macro_source_file *current_file,
43f3e411 23096 struct line_header *lh)
2e276125 23097{
233d95b5 23098 /* File name relative to the compilation directory of this source file. */
03075812 23099 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
2e276125 23100
2e276125 23101 if (! current_file)
abc9d0dc 23102 {
fc474241
DE
23103 /* Note: We don't create a macro table for this compilation unit
23104 at all until we actually get a filename. */
bf80d710 23105 struct macro_table *macro_table = builder->get_macro_table ();
fc474241 23106
abc9d0dc
TT
23107 /* If we have no current file, then this must be the start_file
23108 directive for the compilation unit's main source file. */
03075812 23109 current_file = macro_set_main (macro_table, file_name.get ());
fc474241 23110 macro_define_special (macro_table);
abc9d0dc 23111 }
2e276125 23112 else
03075812 23113 current_file = macro_include (current_file, line, file_name.get ());
6e70227d 23114
2e276125
JB
23115 return current_file;
23116}
23117
2e276125
JB
23118static const char *
23119consume_improper_spaces (const char *p, const char *body)
23120{
23121 if (*p == ' ')
23122 {
b98664d3 23123 complaint (_("macro definition contains spaces "
3e43a32a 23124 "in formal argument list:\n`%s'"),
4d3c2250 23125 body);
2e276125
JB
23126
23127 while (*p == ' ')
23128 p++;
23129 }
23130
23131 return p;
23132}
23133
23134
23135static void
23136parse_macro_definition (struct macro_source_file *file, int line,
23137 const char *body)
23138{
23139 const char *p;
23140
23141 /* The body string takes one of two forms. For object-like macro
23142 definitions, it should be:
23143
23144 <macro name> " " <definition>
23145
23146 For function-like macro definitions, it should be:
23147
23148 <macro name> "() " <definition>
23149 or
23150 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23151
23152 Spaces may appear only where explicitly indicated, and in the
23153 <definition>.
23154
23155 The Dwarf 2 spec says that an object-like macro's name is always
23156 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23157 the space when the macro's definition is the empty string.
2e276125
JB
23158
23159 The Dwarf 2 spec says that there should be no spaces between the
23160 formal arguments in a function-like macro's formal argument list,
23161 but versions of GCC around March 2002 include spaces after the
23162 commas. */
23163
23164
23165 /* Find the extent of the macro name. The macro name is terminated
23166 by either a space or null character (for an object-like macro) or
23167 an opening paren (for a function-like macro). */
23168 for (p = body; *p; p++)
23169 if (*p == ' ' || *p == '(')
23170 break;
23171
23172 if (*p == ' ' || *p == '\0')
23173 {
23174 /* It's an object-like macro. */
23175 int name_len = p - body;
456e800a 23176 std::string name (body, name_len);
2e276125
JB
23177 const char *replacement;
23178
23179 if (*p == ' ')
23180 replacement = body + name_len + 1;
23181 else
23182 {
4d3c2250 23183 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23184 replacement = body + name_len;
23185 }
6e70227d 23186
456e800a 23187 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
23188 }
23189 else if (*p == '(')
23190 {
23191 /* It's a function-like macro. */
456e800a 23192 std::string name (body, p - body);
2e276125
JB
23193 int argc = 0;
23194 int argv_size = 1;
8d749320 23195 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23196
23197 p++;
23198
23199 p = consume_improper_spaces (p, body);
23200
23201 /* Parse the formal argument list. */
23202 while (*p && *p != ')')
23203 {
23204 /* Find the extent of the current argument name. */
23205 const char *arg_start = p;
23206
23207 while (*p && *p != ',' && *p != ')' && *p != ' ')
23208 p++;
23209
23210 if (! *p || p == arg_start)
4d3c2250 23211 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23212 else
23213 {
23214 /* Make sure argv has room for the new argument. */
23215 if (argc >= argv_size)
23216 {
23217 argv_size *= 2;
224c3ddb 23218 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23219 }
23220
3f8a7804 23221 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23222 }
23223
23224 p = consume_improper_spaces (p, body);
23225
23226 /* Consume the comma, if present. */
23227 if (*p == ',')
23228 {
23229 p++;
23230
23231 p = consume_improper_spaces (p, body);
23232 }
23233 }
23234
23235 if (*p == ')')
23236 {
23237 p++;
23238
23239 if (*p == ' ')
23240 /* Perfectly formed definition, no complaints. */
456e800a 23241 macro_define_function (file, line, name.c_str (),
6e70227d 23242 argc, (const char **) argv,
2e276125
JB
23243 p + 1);
23244 else if (*p == '\0')
23245 {
23246 /* Complain, but do define it. */
4d3c2250 23247 dwarf2_macro_malformed_definition_complaint (body);
456e800a 23248 macro_define_function (file, line, name.c_str (),
6e70227d 23249 argc, (const char **) argv,
2e276125
JB
23250 p);
23251 }
23252 else
23253 /* Just complain. */
4d3c2250 23254 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23255 }
23256 else
23257 /* Just complain. */
4d3c2250 23258 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23259
2e276125
JB
23260 {
23261 int i;
23262
23263 for (i = 0; i < argc; i++)
23264 xfree (argv[i]);
23265 }
23266 xfree (argv);
23267 }
23268 else
4d3c2250 23269 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23270}
23271
cf2c3c16
TT
23272/* Skip some bytes from BYTES according to the form given in FORM.
23273 Returns the new pointer. */
2e276125 23274
d521ce57
TT
23275static const gdb_byte *
23276skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23277 enum dwarf_form form,
23278 unsigned int offset_size,
23279 struct dwarf2_section_info *section)
2e276125 23280{
cf2c3c16 23281 unsigned int bytes_read;
2e276125 23282
cf2c3c16 23283 switch (form)
2e276125 23284 {
cf2c3c16
TT
23285 case DW_FORM_data1:
23286 case DW_FORM_flag:
23287 ++bytes;
23288 break;
23289
23290 case DW_FORM_data2:
23291 bytes += 2;
23292 break;
23293
23294 case DW_FORM_data4:
23295 bytes += 4;
23296 break;
23297
23298 case DW_FORM_data8:
23299 bytes += 8;
23300 break;
23301
0224619f
JK
23302 case DW_FORM_data16:
23303 bytes += 16;
23304 break;
23305
cf2c3c16
TT
23306 case DW_FORM_string:
23307 read_direct_string (abfd, bytes, &bytes_read);
23308 bytes += bytes_read;
23309 break;
23310
23311 case DW_FORM_sec_offset:
23312 case DW_FORM_strp:
36586728 23313 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23314 bytes += offset_size;
23315 break;
23316
23317 case DW_FORM_block:
23318 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23319 bytes += bytes_read;
23320 break;
23321
23322 case DW_FORM_block1:
23323 bytes += 1 + read_1_byte (abfd, bytes);
23324 break;
23325 case DW_FORM_block2:
23326 bytes += 2 + read_2_bytes (abfd, bytes);
23327 break;
23328 case DW_FORM_block4:
23329 bytes += 4 + read_4_bytes (abfd, bytes);
23330 break;
23331
336d760d 23332 case DW_FORM_addrx:
cf2c3c16 23333 case DW_FORM_sdata:
cf532bd1 23334 case DW_FORM_strx:
cf2c3c16 23335 case DW_FORM_udata:
3019eac3
DE
23336 case DW_FORM_GNU_addr_index:
23337 case DW_FORM_GNU_str_index:
d521ce57 23338 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23339 if (bytes == NULL)
23340 {
23341 dwarf2_section_buffer_overflow_complaint (section);
23342 return NULL;
23343 }
cf2c3c16
TT
23344 break;
23345
663c44ac
JK
23346 case DW_FORM_implicit_const:
23347 break;
23348
cf2c3c16
TT
23349 default:
23350 {
b98664d3 23351 complaint (_("invalid form 0x%x in `%s'"),
96b79293 23352 form, section->get_name ());
cf2c3c16
TT
23353 return NULL;
23354 }
2e276125
JB
23355 }
23356
cf2c3c16
TT
23357 return bytes;
23358}
757a13d0 23359
cf2c3c16
TT
23360/* A helper for dwarf_decode_macros that handles skipping an unknown
23361 opcode. Returns an updated pointer to the macro data buffer; or,
23362 on error, issues a complaint and returns NULL. */
757a13d0 23363
d521ce57 23364static const gdb_byte *
cf2c3c16 23365skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23366 const gdb_byte **opcode_definitions,
23367 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23368 bfd *abfd,
23369 unsigned int offset_size,
23370 struct dwarf2_section_info *section)
23371{
23372 unsigned int bytes_read, i;
23373 unsigned long arg;
d521ce57 23374 const gdb_byte *defn;
2e276125 23375
cf2c3c16 23376 if (opcode_definitions[opcode] == NULL)
2e276125 23377 {
b98664d3 23378 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
23379 opcode);
23380 return NULL;
23381 }
2e276125 23382
cf2c3c16
TT
23383 defn = opcode_definitions[opcode];
23384 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23385 defn += bytes_read;
2e276125 23386
cf2c3c16
TT
23387 for (i = 0; i < arg; ++i)
23388 {
aead7601
SM
23389 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23390 (enum dwarf_form) defn[i], offset_size,
f664829e 23391 section);
cf2c3c16
TT
23392 if (mac_ptr == NULL)
23393 {
23394 /* skip_form_bytes already issued the complaint. */
23395 return NULL;
23396 }
23397 }
757a13d0 23398
cf2c3c16
TT
23399 return mac_ptr;
23400}
757a13d0 23401
cf2c3c16
TT
23402/* A helper function which parses the header of a macro section.
23403 If the macro section is the extended (for now called "GNU") type,
23404 then this updates *OFFSET_SIZE. Returns a pointer to just after
23405 the header, or issues a complaint and returns NULL on error. */
757a13d0 23406
d521ce57
TT
23407static const gdb_byte *
23408dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 23409 bfd *abfd,
d521ce57 23410 const gdb_byte *mac_ptr,
cf2c3c16
TT
23411 unsigned int *offset_size,
23412 int section_is_gnu)
23413{
23414 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 23415
cf2c3c16
TT
23416 if (section_is_gnu)
23417 {
23418 unsigned int version, flags;
757a13d0 23419
cf2c3c16 23420 version = read_2_bytes (abfd, mac_ptr);
0af92d60 23421 if (version != 4 && version != 5)
cf2c3c16 23422 {
b98664d3 23423 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
23424 version);
23425 return NULL;
23426 }
23427 mac_ptr += 2;
757a13d0 23428
cf2c3c16
TT
23429 flags = read_1_byte (abfd, mac_ptr);
23430 ++mac_ptr;
23431 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 23432
cf2c3c16
TT
23433 if ((flags & 2) != 0)
23434 /* We don't need the line table offset. */
23435 mac_ptr += *offset_size;
757a13d0 23436
cf2c3c16
TT
23437 /* Vendor opcode descriptions. */
23438 if ((flags & 4) != 0)
23439 {
23440 unsigned int i, count;
757a13d0 23441
cf2c3c16
TT
23442 count = read_1_byte (abfd, mac_ptr);
23443 ++mac_ptr;
23444 for (i = 0; i < count; ++i)
23445 {
23446 unsigned int opcode, bytes_read;
23447 unsigned long arg;
23448
23449 opcode = read_1_byte (abfd, mac_ptr);
23450 ++mac_ptr;
23451 opcode_definitions[opcode] = mac_ptr;
23452 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23453 mac_ptr += bytes_read;
23454 mac_ptr += arg;
23455 }
757a13d0 23456 }
cf2c3c16 23457 }
757a13d0 23458
cf2c3c16
TT
23459 return mac_ptr;
23460}
757a13d0 23461
cf2c3c16 23462/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 23463 including DW_MACRO_import. */
cf2c3c16
TT
23464
23465static void
bf80d710
TT
23466dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
23467 buildsym_compunit *builder,
ed2dc618 23468 bfd *abfd,
d521ce57 23469 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 23470 struct macro_source_file *current_file,
43f3e411 23471 struct line_header *lh,
cf2c3c16 23472 struct dwarf2_section_info *section,
36586728 23473 int section_is_gnu, int section_is_dwz,
cf2c3c16 23474 unsigned int offset_size,
8fc3fc34 23475 htab_t include_hash)
cf2c3c16 23476{
4d663531 23477 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
23478 enum dwarf_macro_record_type macinfo_type;
23479 int at_commandline;
d521ce57 23480 const gdb_byte *opcode_definitions[256];
757a13d0 23481
cf2c3c16
TT
23482 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23483 &offset_size, section_is_gnu);
23484 if (mac_ptr == NULL)
23485 {
23486 /* We already issued a complaint. */
23487 return;
23488 }
757a13d0
JK
23489
23490 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23491 GDB is still reading the definitions from command line. First
23492 DW_MACINFO_start_file will need to be ignored as it was already executed
23493 to create CURRENT_FILE for the main source holding also the command line
23494 definitions. On first met DW_MACINFO_start_file this flag is reset to
23495 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23496
23497 at_commandline = 1;
23498
23499 do
23500 {
23501 /* Do we at least have room for a macinfo type byte? */
23502 if (mac_ptr >= mac_end)
23503 {
f664829e 23504 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
23505 break;
23506 }
23507
aead7601 23508 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
23509 mac_ptr++;
23510
cf2c3c16
TT
23511 /* Note that we rely on the fact that the corresponding GNU and
23512 DWARF constants are the same. */
132448f8
SM
23513 DIAGNOSTIC_PUSH
23514 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
23515 switch (macinfo_type)
23516 {
23517 /* A zero macinfo type indicates the end of the macro
23518 information. */
23519 case 0:
23520 break;
2e276125 23521
0af92d60
JK
23522 case DW_MACRO_define:
23523 case DW_MACRO_undef:
23524 case DW_MACRO_define_strp:
23525 case DW_MACRO_undef_strp:
23526 case DW_MACRO_define_sup:
23527 case DW_MACRO_undef_sup:
2e276125 23528 {
891d2f0b 23529 unsigned int bytes_read;
2e276125 23530 int line;
d521ce57 23531 const char *body;
cf2c3c16 23532 int is_define;
2e276125 23533
cf2c3c16
TT
23534 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23535 mac_ptr += bytes_read;
23536
0af92d60
JK
23537 if (macinfo_type == DW_MACRO_define
23538 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
23539 {
23540 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23541 mac_ptr += bytes_read;
23542 }
23543 else
23544 {
23545 LONGEST str_offset;
23546
24aa364d 23547 str_offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16 23548 mac_ptr += offset_size;
2e276125 23549
0af92d60
JK
23550 if (macinfo_type == DW_MACRO_define_sup
23551 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 23552 || section_is_dwz)
36586728 23553 {
ed2dc618
SM
23554 struct dwz_file *dwz
23555 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 23556
0314b390 23557 body = dwz->read_string (objfile, str_offset);
36586728
TT
23558 }
23559 else
ed2dc618
SM
23560 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23561 abfd, str_offset);
cf2c3c16
TT
23562 }
23563
0af92d60
JK
23564 is_define = (macinfo_type == DW_MACRO_define
23565 || macinfo_type == DW_MACRO_define_strp
23566 || macinfo_type == DW_MACRO_define_sup);
2e276125 23567 if (! current_file)
757a13d0
JK
23568 {
23569 /* DWARF violation as no main source is present. */
b98664d3 23570 complaint (_("debug info with no main source gives macro %s "
757a13d0 23571 "on line %d: %s"),
cf2c3c16
TT
23572 is_define ? _("definition") : _("undefinition"),
23573 line, body);
757a13d0
JK
23574 break;
23575 }
3e43a32a
MS
23576 if ((line == 0 && !at_commandline)
23577 || (line != 0 && at_commandline))
b98664d3 23578 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 23579 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 23580 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
23581 line == 0 ? _("zero") : _("non-zero"), line, body);
23582
955b06fa 23583 if (body == NULL)
7bede828 23584 {
955b06fa
SDJ
23585 /* Fedora's rpm-build's "debugedit" binary
23586 corrupted .debug_macro sections.
23587
23588 For more info, see
23589 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23590 complaint (_("debug info gives %s invalid macro %s "
23591 "without body (corrupted?) at line %d "
23592 "on file %s"),
23593 at_commandline ? _("command-line") : _("in-file"),
23594 is_define ? _("definition") : _("undefinition"),
23595 line, current_file->filename);
7bede828 23596 }
955b06fa
SDJ
23597 else if (is_define)
23598 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
23599 else
23600 {
0af92d60
JK
23601 gdb_assert (macinfo_type == DW_MACRO_undef
23602 || macinfo_type == DW_MACRO_undef_strp
23603 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
23604 macro_undef (current_file, line, body);
23605 }
2e276125
JB
23606 }
23607 break;
23608
0af92d60 23609 case DW_MACRO_start_file:
2e276125 23610 {
891d2f0b 23611 unsigned int bytes_read;
2e276125
JB
23612 int line, file;
23613
23614 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23615 mac_ptr += bytes_read;
23616 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23617 mac_ptr += bytes_read;
23618
3e43a32a
MS
23619 if ((line == 0 && !at_commandline)
23620 || (line != 0 && at_commandline))
b98664d3 23621 complaint (_("debug info gives source %d included "
757a13d0
JK
23622 "from %s at %s line %d"),
23623 file, at_commandline ? _("command-line") : _("file"),
23624 line == 0 ? _("zero") : _("non-zero"), line);
23625
23626 if (at_commandline)
23627 {
0af92d60 23628 /* This DW_MACRO_start_file was executed in the
cf2c3c16 23629 pass one. */
757a13d0
JK
23630 at_commandline = 0;
23631 }
23632 else
bf80d710
TT
23633 current_file = macro_start_file (builder, file, line,
23634 current_file, lh);
2e276125
JB
23635 }
23636 break;
23637
0af92d60 23638 case DW_MACRO_end_file:
2e276125 23639 if (! current_file)
b98664d3 23640 complaint (_("macro debug info has an unmatched "
3e43a32a 23641 "`close_file' directive"));
2e276125
JB
23642 else
23643 {
23644 current_file = current_file->included_by;
23645 if (! current_file)
23646 {
cf2c3c16 23647 enum dwarf_macro_record_type next_type;
2e276125
JB
23648
23649 /* GCC circa March 2002 doesn't produce the zero
23650 type byte marking the end of the compilation
23651 unit. Complain if it's not there, but exit no
23652 matter what. */
23653
23654 /* Do we at least have room for a macinfo type byte? */
23655 if (mac_ptr >= mac_end)
23656 {
f664829e 23657 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
23658 return;
23659 }
23660
23661 /* We don't increment mac_ptr here, so this is just
23662 a look-ahead. */
aead7601
SM
23663 next_type
23664 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23665 mac_ptr);
2e276125 23666 if (next_type != 0)
b98664d3 23667 complaint (_("no terminating 0-type entry for "
3e43a32a 23668 "macros in `.debug_macinfo' section"));
2e276125
JB
23669
23670 return;
23671 }
23672 }
23673 break;
23674
0af92d60
JK
23675 case DW_MACRO_import:
23676 case DW_MACRO_import_sup:
cf2c3c16
TT
23677 {
23678 LONGEST offset;
8fc3fc34 23679 void **slot;
a036ba48
TT
23680 bfd *include_bfd = abfd;
23681 struct dwarf2_section_info *include_section = section;
d521ce57 23682 const gdb_byte *include_mac_end = mac_end;
a036ba48 23683 int is_dwz = section_is_dwz;
d521ce57 23684 const gdb_byte *new_mac_ptr;
cf2c3c16 23685
24aa364d 23686 offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16
TT
23687 mac_ptr += offset_size;
23688
0af92d60 23689 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 23690 {
ed2dc618 23691 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 23692
96b79293 23693 dwz->macro.read (objfile);
a036ba48 23694
a036ba48 23695 include_section = &dwz->macro;
96b79293 23696 include_bfd = include_section->get_bfd_owner ();
a036ba48
TT
23697 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23698 is_dwz = 1;
23699 }
23700
23701 new_mac_ptr = include_section->buffer + offset;
23702 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23703
8fc3fc34
TT
23704 if (*slot != NULL)
23705 {
23706 /* This has actually happened; see
23707 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 23708 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
23709 ".debug_macro section"));
23710 }
23711 else
23712 {
d521ce57 23713 *slot = (void *) new_mac_ptr;
36586728 23714
bf80d710
TT
23715 dwarf_decode_macro_bytes (dwarf2_per_objfile, builder,
23716 include_bfd, new_mac_ptr,
43f3e411 23717 include_mac_end, current_file, lh,
36586728 23718 section, section_is_gnu, is_dwz,
4d663531 23719 offset_size, include_hash);
8fc3fc34 23720
d521ce57 23721 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 23722 }
cf2c3c16
TT
23723 }
23724 break;
23725
2e276125 23726 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
23727 if (!section_is_gnu)
23728 {
23729 unsigned int bytes_read;
2e276125 23730
ac298888
TT
23731 /* This reads the constant, but since we don't recognize
23732 any vendor extensions, we ignore it. */
23733 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
23734 mac_ptr += bytes_read;
23735 read_direct_string (abfd, mac_ptr, &bytes_read);
23736 mac_ptr += bytes_read;
2e276125 23737
cf2c3c16
TT
23738 /* We don't recognize any vendor extensions. */
23739 break;
23740 }
23741 /* FALLTHROUGH */
23742
23743 default:
23744 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23745 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23746 section);
23747 if (mac_ptr == NULL)
23748 return;
23749 break;
2e276125 23750 }
132448f8 23751 DIAGNOSTIC_POP
757a13d0 23752 } while (macinfo_type != 0);
2e276125 23753}
8e19ed76 23754
cf2c3c16 23755static void
9eac9650
TT
23756dwarf_decode_macros (struct dwarf2_per_objfile *dwarf2_per_objfile,
23757 buildsym_compunit *builder, dwarf2_section_info *section,
23758 struct line_header *lh, unsigned int offset_size,
23759 unsigned int offset, int section_is_gnu)
cf2c3c16 23760{
09262596 23761 bfd *abfd;
d521ce57 23762 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
23763 struct macro_source_file *current_file = 0;
23764 enum dwarf_macro_record_type macinfo_type;
d521ce57 23765 const gdb_byte *opcode_definitions[256];
8fc3fc34 23766 void **slot;
09262596 23767
96b79293 23768 abfd = section->get_bfd_owner ();
cf2c3c16
TT
23769
23770 /* First pass: Find the name of the base filename.
23771 This filename is needed in order to process all macros whose definition
23772 (or undefinition) comes from the command line. These macros are defined
23773 before the first DW_MACINFO_start_file entry, and yet still need to be
23774 associated to the base file.
23775
23776 To determine the base file name, we scan the macro definitions until we
23777 reach the first DW_MACINFO_start_file entry. We then initialize
23778 CURRENT_FILE accordingly so that any macro definition found before the
23779 first DW_MACINFO_start_file can still be associated to the base file. */
23780
23781 mac_ptr = section->buffer + offset;
23782 mac_end = section->buffer + section->size;
23783
23784 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23785 &offset_size, section_is_gnu);
23786 if (mac_ptr == NULL)
23787 {
23788 /* We already issued a complaint. */
23789 return;
23790 }
23791
23792 do
23793 {
23794 /* Do we at least have room for a macinfo type byte? */
23795 if (mac_ptr >= mac_end)
23796 {
23797 /* Complaint is printed during the second pass as GDB will probably
23798 stop the first pass earlier upon finding
23799 DW_MACINFO_start_file. */
23800 break;
23801 }
23802
aead7601 23803 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
23804 mac_ptr++;
23805
23806 /* Note that we rely on the fact that the corresponding GNU and
23807 DWARF constants are the same. */
132448f8
SM
23808 DIAGNOSTIC_PUSH
23809 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
23810 switch (macinfo_type)
23811 {
23812 /* A zero macinfo type indicates the end of the macro
23813 information. */
23814 case 0:
23815 break;
23816
0af92d60
JK
23817 case DW_MACRO_define:
23818 case DW_MACRO_undef:
cf2c3c16
TT
23819 /* Only skip the data by MAC_PTR. */
23820 {
23821 unsigned int bytes_read;
23822
23823 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23824 mac_ptr += bytes_read;
23825 read_direct_string (abfd, mac_ptr, &bytes_read);
23826 mac_ptr += bytes_read;
23827 }
23828 break;
23829
0af92d60 23830 case DW_MACRO_start_file:
cf2c3c16
TT
23831 {
23832 unsigned int bytes_read;
23833 int line, file;
23834
23835 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23836 mac_ptr += bytes_read;
23837 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23838 mac_ptr += bytes_read;
23839
bf80d710
TT
23840 current_file = macro_start_file (builder, file, line,
23841 current_file, lh);
cf2c3c16
TT
23842 }
23843 break;
23844
0af92d60 23845 case DW_MACRO_end_file:
cf2c3c16
TT
23846 /* No data to skip by MAC_PTR. */
23847 break;
23848
0af92d60
JK
23849 case DW_MACRO_define_strp:
23850 case DW_MACRO_undef_strp:
23851 case DW_MACRO_define_sup:
23852 case DW_MACRO_undef_sup:
cf2c3c16
TT
23853 {
23854 unsigned int bytes_read;
23855
23856 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23857 mac_ptr += bytes_read;
23858 mac_ptr += offset_size;
23859 }
23860 break;
23861
0af92d60
JK
23862 case DW_MACRO_import:
23863 case DW_MACRO_import_sup:
cf2c3c16 23864 /* Note that, according to the spec, a transparent include
0af92d60 23865 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
23866 skip this opcode. */
23867 mac_ptr += offset_size;
23868 break;
23869
23870 case DW_MACINFO_vendor_ext:
23871 /* Only skip the data by MAC_PTR. */
23872 if (!section_is_gnu)
23873 {
23874 unsigned int bytes_read;
23875
23876 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23877 mac_ptr += bytes_read;
23878 read_direct_string (abfd, mac_ptr, &bytes_read);
23879 mac_ptr += bytes_read;
23880 }
23881 /* FALLTHROUGH */
23882
23883 default:
23884 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23885 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23886 section);
23887 if (mac_ptr == NULL)
23888 return;
23889 break;
23890 }
132448f8 23891 DIAGNOSTIC_POP
cf2c3c16
TT
23892 } while (macinfo_type != 0 && current_file == NULL);
23893
23894 /* Second pass: Process all entries.
23895
23896 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23897 command-line macro definitions/undefinitions. This flag is unset when we
23898 reach the first DW_MACINFO_start_file entry. */
23899
fc4007c9
TT
23900 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23901 htab_eq_pointer,
23902 NULL, xcalloc, xfree));
8fc3fc34 23903 mac_ptr = section->buffer + offset;
fc4007c9 23904 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 23905 *slot = (void *) mac_ptr;
bf80d710
TT
23906 dwarf_decode_macro_bytes (dwarf2_per_objfile, builder,
23907 abfd, mac_ptr, mac_end,
43f3e411 23908 current_file, lh, section,
fc4007c9
TT
23909 section_is_gnu, 0, offset_size,
23910 include_hash.get ());
cf2c3c16
TT
23911}
23912
9eac9650
TT
23913/* An overload of dwarf_decode_macros that finds the correct section
23914 and ensures it is read in before calling the other overload. */
23915
23916static void
23917dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23918 int section_is_gnu)
23919{
23920 struct dwarf2_per_objfile *dwarf2_per_objfile
23921 = cu->per_cu->dwarf2_per_objfile;
23922 struct objfile *objfile = dwarf2_per_objfile->objfile;
23923 struct line_header *lh = cu->line_header;
23924 unsigned int offset_size = cu->header.offset_size;
23925 struct dwarf2_section_info *section;
23926 const char *section_name;
23927
23928 if (cu->dwo_unit != nullptr)
23929 {
23930 if (section_is_gnu)
23931 {
23932 section = &cu->dwo_unit->dwo_file->sections.macro;
23933 section_name = ".debug_macro.dwo";
23934 }
23935 else
23936 {
23937 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23938 section_name = ".debug_macinfo.dwo";
23939 }
23940 }
23941 else
23942 {
23943 if (section_is_gnu)
23944 {
23945 section = &dwarf2_per_objfile->macro;
23946 section_name = ".debug_macro";
23947 }
23948 else
23949 {
23950 section = &dwarf2_per_objfile->macinfo;
23951 section_name = ".debug_macinfo";
23952 }
23953 }
23954
23955 section->read (objfile);
23956 if (section->buffer == nullptr)
23957 {
23958 complaint (_("missing %s section"), section_name);
23959 return;
23960 }
23961
23962 buildsym_compunit *builder = cu->get_builder ();
23963
23964 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23965 offset_size, offset, section_is_gnu);
23966}
23967
3019eac3
DE
23968/* Return the .debug_loc section to use for CU.
23969 For DWO files use .debug_loc.dwo. */
23970
23971static struct dwarf2_section_info *
23972cu_debug_loc_section (struct dwarf2_cu *cu)
23973{
518817b3
SM
23974 struct dwarf2_per_objfile *dwarf2_per_objfile
23975 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23976
3019eac3 23977 if (cu->dwo_unit)
43988095
JK
23978 {
23979 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23980
43988095
JK
23981 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23982 }
23983 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23984 : &dwarf2_per_objfile->loc);
3019eac3
DE
23985}
23986
8cf6f0b1
TT
23987/* A helper function that fills in a dwarf2_loclist_baton. */
23988
23989static void
23990fill_in_loclist_baton (struct dwarf2_cu *cu,
23991 struct dwarf2_loclist_baton *baton,
ff39bb5e 23992 const struct attribute *attr)
8cf6f0b1 23993{
518817b3
SM
23994 struct dwarf2_per_objfile *dwarf2_per_objfile
23995 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23996 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23997
96b79293 23998 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23999
24000 baton->per_cu = cu->per_cu;
24001 gdb_assert (baton->per_cu);
24002 /* We don't know how long the location list is, but make sure we
24003 don't run off the edge of the section. */
3019eac3
DE
24004 baton->size = section->size - DW_UNSND (attr);
24005 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24006 baton->base_address = cu->base_address;
f664829e 24007 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24008}
24009
4c2df51b 24010static void
ff39bb5e 24011dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24012 struct dwarf2_cu *cu, int is_block)
4c2df51b 24013{
518817b3
SM
24014 struct dwarf2_per_objfile *dwarf2_per_objfile
24015 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24016 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24017 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24018
cd6c91b4 24019 if (attr->form_is_section_offset ()
3019eac3 24020 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24021 the section. If so, fall through to the complaint in the
24022 other branch. */
2c7d5afc 24023 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 24024 {
0d53c4c4 24025 struct dwarf2_loclist_baton *baton;
4c2df51b 24026
8d749320 24027 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24028
8cf6f0b1 24029 fill_in_loclist_baton (cu, baton, attr);
be391dca 24030
d00adf39 24031 if (cu->base_known == 0)
b98664d3 24032 complaint (_("Location list used without "
3e43a32a 24033 "specifying the CU base address."));
4c2df51b 24034
f1e6e072
TT
24035 SYMBOL_ACLASS_INDEX (sym) = (is_block
24036 ? dwarf2_loclist_block_index
24037 : dwarf2_loclist_index);
0d53c4c4
DJ
24038 SYMBOL_LOCATION_BATON (sym) = baton;
24039 }
24040 else
24041 {
24042 struct dwarf2_locexpr_baton *baton;
24043
8d749320 24044 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24045 baton->per_cu = cu->per_cu;
24046 gdb_assert (baton->per_cu);
0d53c4c4 24047
4fc6c0d5 24048 if (attr->form_is_block ())
0d53c4c4
DJ
24049 {
24050 /* Note that we're just copying the block's data pointer
24051 here, not the actual data. We're still pointing into the
6502dd73
DJ
24052 info_buffer for SYM's objfile; right now we never release
24053 that buffer, but when we do clean up properly this may
24054 need to change. */
0d53c4c4
DJ
24055 baton->size = DW_BLOCK (attr)->size;
24056 baton->data = DW_BLOCK (attr)->data;
24057 }
24058 else
24059 {
24060 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24061 sym->natural_name ());
0d53c4c4 24062 baton->size = 0;
0d53c4c4 24063 }
6e70227d 24064
f1e6e072
TT
24065 SYMBOL_ACLASS_INDEX (sym) = (is_block
24066 ? dwarf2_locexpr_block_index
24067 : dwarf2_locexpr_index);
0d53c4c4
DJ
24068 SYMBOL_LOCATION_BATON (sym) = baton;
24069 }
4c2df51b 24070}
6502dd73 24071
09ba997f 24072/* See read.h. */
ae0d2f24
UW
24073
24074struct objfile *
09ba997f 24075dwarf2_per_cu_data::objfile () const
ae0d2f24 24076{
09ba997f 24077 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
24078
24079 /* Return the master objfile, so that we can report and look up the
24080 correct file containing this variable. */
24081 if (objfile->separate_debug_objfile_backlink)
24082 objfile = objfile->separate_debug_objfile_backlink;
24083
24084 return objfile;
24085}
24086
96408a79
SA
24087/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24088 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24089 CU_HEADERP first. */
24090
24091static const struct comp_unit_head *
24092per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 24093 const struct dwarf2_per_cu_data *per_cu)
96408a79 24094{
d521ce57 24095 const gdb_byte *info_ptr;
96408a79
SA
24096
24097 if (per_cu->cu)
24098 return &per_cu->cu->header;
24099
9c541725 24100 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24101
24102 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24103 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24104 rcuh_kind::COMPILE);
96408a79
SA
24105
24106 return cu_headerp;
24107}
24108
09ba997f 24109/* See read.h. */
ae0d2f24 24110
98714339 24111int
09ba997f 24112dwarf2_per_cu_data::addr_size () const
ae0d2f24 24113{
96408a79
SA
24114 struct comp_unit_head cu_header_local;
24115 const struct comp_unit_head *cu_headerp;
c471e790 24116
09ba997f 24117 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24118
24119 return cu_headerp->addr_size;
ae0d2f24
UW
24120}
24121
09ba997f 24122/* See read.h. */
9eae7c52
TT
24123
24124int
09ba997f 24125dwarf2_per_cu_data::offset_size () const
9eae7c52 24126{
96408a79
SA
24127 struct comp_unit_head cu_header_local;
24128 const struct comp_unit_head *cu_headerp;
9c6c53f7 24129
09ba997f 24130 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24131
24132 return cu_headerp->offset_size;
24133}
24134
09ba997f 24135/* See read.h. */
96408a79
SA
24136
24137int
09ba997f 24138dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
24139{
24140 struct comp_unit_head cu_header_local;
24141 const struct comp_unit_head *cu_headerp;
24142
09ba997f 24143 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24144
24145 if (cu_headerp->version == 2)
24146 return cu_headerp->addr_size;
24147 else
24148 return cu_headerp->offset_size;
181cebd4
JK
24149}
24150
09ba997f 24151/* See read.h. */
9aa1f1e3
TT
24152
24153CORE_ADDR
09ba997f 24154dwarf2_per_cu_data::text_offset () const
9aa1f1e3 24155{
09ba997f
TT
24156 struct objfile *objfile = dwarf2_per_objfile->objfile;
24157
24158 return objfile->text_section_offset ();
9aa1f1e3
TT
24159}
24160
09ba997f
TT
24161/* See read.h. */
24162
24163struct type *
24164dwarf2_per_cu_data::addr_type () const
9a49df9d 24165{
09ba997f 24166 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
24167 struct type *void_type = objfile_type (objfile)->builtin_void;
24168 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 24169 int addr_size = this->addr_size ();
9a49df9d
AB
24170
24171 if (TYPE_LENGTH (addr_type) == addr_size)
24172 return addr_type;
24173
09ba997f 24174 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
24175 return addr_type;
24176}
24177
22b6cd70
TT
24178/* A helper function for dwarf2_find_containing_comp_unit that returns
24179 the index of the result, and that searches a vector. It will
24180 return a result even if the offset in question does not actually
24181 occur in any CU. This is separate so that it can be unit
24182 tested. */
ae038cb0 24183
22b6cd70
TT
24184static int
24185dwarf2_find_containing_comp_unit
24186 (sect_offset sect_off,
24187 unsigned int offset_in_dwz,
24188 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24189{
ae038cb0
DJ
24190 int low, high;
24191
ae038cb0 24192 low = 0;
22b6cd70 24193 high = all_comp_units.size () - 1;
ae038cb0
DJ
24194 while (high > low)
24195 {
36586728 24196 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24197 int mid = low + (high - low) / 2;
9a619af0 24198
22b6cd70 24199 mid_cu = all_comp_units[mid];
36586728 24200 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24201 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24202 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24203 high = mid;
24204 else
24205 low = mid + 1;
24206 }
24207 gdb_assert (low == high);
22b6cd70
TT
24208 return low;
24209}
24210
24211/* Locate the .debug_info compilation unit from CU's objfile which contains
24212 the DIE at OFFSET. Raises an error on failure. */
24213
24214static struct dwarf2_per_cu_data *
24215dwarf2_find_containing_comp_unit (sect_offset sect_off,
24216 unsigned int offset_in_dwz,
24217 struct dwarf2_per_objfile *dwarf2_per_objfile)
24218{
24219 int low
24220 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24221 dwarf2_per_objfile->all_comp_units);
24222 struct dwarf2_per_cu_data *this_cu
24223 = dwarf2_per_objfile->all_comp_units[low];
24224
45b8ae0c 24225 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24226 {
36586728 24227 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24228 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24229 "offset %s [in module %s]"),
24230 sect_offset_str (sect_off),
ed2dc618 24231 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24232
9c541725
PA
24233 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24234 <= sect_off);
ae038cb0
DJ
24235 return dwarf2_per_objfile->all_comp_units[low-1];
24236 }
24237 else
24238 {
b76e467d 24239 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24240 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24241 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24242 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24243 return this_cu;
24244 }
24245}
24246
22b6cd70
TT
24247#if GDB_SELF_TEST
24248
24249namespace selftests {
24250namespace find_containing_comp_unit {
24251
24252static void
24253run_test ()
24254{
24255 struct dwarf2_per_cu_data one {};
24256 struct dwarf2_per_cu_data two {};
24257 struct dwarf2_per_cu_data three {};
24258 struct dwarf2_per_cu_data four {};
24259
24260 one.length = 5;
24261 two.sect_off = sect_offset (one.length);
24262 two.length = 7;
24263
24264 three.length = 5;
24265 three.is_dwz = 1;
24266 four.sect_off = sect_offset (three.length);
24267 four.length = 7;
24268 four.is_dwz = 1;
24269
24270 std::vector<dwarf2_per_cu_data *> units;
24271 units.push_back (&one);
24272 units.push_back (&two);
24273 units.push_back (&three);
24274 units.push_back (&four);
24275
24276 int result;
24277
24278 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24279 SELF_CHECK (units[result] == &one);
24280 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24281 SELF_CHECK (units[result] == &one);
24282 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24283 SELF_CHECK (units[result] == &two);
24284
24285 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24286 SELF_CHECK (units[result] == &three);
24287 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24288 SELF_CHECK (units[result] == &three);
24289 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24290 SELF_CHECK (units[result] == &four);
24291}
24292
24293}
24294}
24295
24296#endif /* GDB_SELF_TEST */
24297
23745b47 24298/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24299
fcd3b13d
SM
24300dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24301 : per_cu (per_cu_),
9068261f
AB
24302 mark (false),
24303 has_loclist (false),
24304 checked_producer (false),
24305 producer_is_gxx_lt_4_6 (false),
24306 producer_is_gcc_lt_4_3 (false),
eb77c9df 24307 producer_is_icc (false),
9068261f 24308 producer_is_icc_lt_14 (false),
c258c396 24309 producer_is_codewarrior (false),
9068261f 24310 processing_has_namespace_info (false)
93311388 24311{
fcd3b13d
SM
24312 per_cu->cu = this;
24313}
24314
24315/* Destroy a dwarf2_cu. */
24316
24317dwarf2_cu::~dwarf2_cu ()
24318{
24319 per_cu->cu = NULL;
9816fde3
JK
24320}
24321
24322/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24323
24324static void
95554aad
TT
24325prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24326 enum language pretend_language)
9816fde3
JK
24327{
24328 struct attribute *attr;
24329
24330 /* Set the language we're debugging. */
24331 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24332 if (attr != nullptr)
9816fde3
JK
24333 set_cu_language (DW_UNSND (attr), cu);
24334 else
9cded63f 24335 {
95554aad 24336 cu->language = pretend_language;
9cded63f
TT
24337 cu->language_defn = language_def (cu->language);
24338 }
dee91e82 24339
7d45c7c3 24340 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24341}
24342
ae038cb0
DJ
24343/* Increase the age counter on each cached compilation unit, and free
24344 any that are too old. */
24345
24346static void
ed2dc618 24347age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24348{
24349 struct dwarf2_per_cu_data *per_cu, **last_chain;
24350
24351 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24352 per_cu = dwarf2_per_objfile->read_in_chain;
24353 while (per_cu != NULL)
24354 {
24355 per_cu->cu->last_used ++;
b4f54984 24356 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24357 dwarf2_mark (per_cu->cu);
24358 per_cu = per_cu->cu->read_in_chain;
24359 }
24360
24361 per_cu = dwarf2_per_objfile->read_in_chain;
24362 last_chain = &dwarf2_per_objfile->read_in_chain;
24363 while (per_cu != NULL)
24364 {
24365 struct dwarf2_per_cu_data *next_cu;
24366
24367 next_cu = per_cu->cu->read_in_chain;
24368
24369 if (!per_cu->cu->mark)
24370 {
fcd3b13d 24371 delete per_cu->cu;
ae038cb0
DJ
24372 *last_chain = next_cu;
24373 }
24374 else
24375 last_chain = &per_cu->cu->read_in_chain;
24376
24377 per_cu = next_cu;
24378 }
24379}
24380
24381/* Remove a single compilation unit from the cache. */
24382
24383static void
dee91e82 24384free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24385{
24386 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24387 struct dwarf2_per_objfile *dwarf2_per_objfile
24388 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24389
24390 per_cu = dwarf2_per_objfile->read_in_chain;
24391 last_chain = &dwarf2_per_objfile->read_in_chain;
24392 while (per_cu != NULL)
24393 {
24394 struct dwarf2_per_cu_data *next_cu;
24395
24396 next_cu = per_cu->cu->read_in_chain;
24397
dee91e82 24398 if (per_cu == target_per_cu)
ae038cb0 24399 {
fcd3b13d 24400 delete per_cu->cu;
dee91e82 24401 per_cu->cu = NULL;
ae038cb0
DJ
24402 *last_chain = next_cu;
24403 break;
24404 }
24405 else
24406 last_chain = &per_cu->cu->read_in_chain;
24407
24408 per_cu = next_cu;
24409 }
24410}
24411
dee91e82
DE
24412/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24413 We store these in a hash table separate from the DIEs, and preserve them
24414 when the DIEs are flushed out of cache.
24415
24416 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24417 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24418 or the type may come from a DWO file. Furthermore, while it's more logical
24419 to use per_cu->section+offset, with Fission the section with the data is in
24420 the DWO file but we don't know that section at the point we need it.
24421 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24422 because we can enter the lookup routine, get_die_type_at_offset, from
24423 outside this file, and thus won't necessarily have PER_CU->cu.
24424 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24425
dee91e82 24426struct dwarf2_per_cu_offset_and_type
1c379e20 24427{
dee91e82 24428 const struct dwarf2_per_cu_data *per_cu;
9c541725 24429 sect_offset sect_off;
1c379e20
DJ
24430 struct type *type;
24431};
24432
dee91e82 24433/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24434
24435static hashval_t
dee91e82 24436per_cu_offset_and_type_hash (const void *item)
1c379e20 24437{
9a3c8263
SM
24438 const struct dwarf2_per_cu_offset_and_type *ofs
24439 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24440
9c541725 24441 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24442}
24443
dee91e82 24444/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24445
24446static int
dee91e82 24447per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24448{
9a3c8263
SM
24449 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24450 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24451 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24452 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24453
dee91e82 24454 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24455 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24456}
24457
24458/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24459 table if necessary. For convenience, return TYPE.
24460
24461 The DIEs reading must have careful ordering to:
85102364 24462 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24463 reading current DIE.
24464 * Not trying to dereference contents of still incompletely read in types
24465 while reading in other DIEs.
24466 * Enable referencing still incompletely read in types just by a pointer to
24467 the type without accessing its fields.
24468
24469 Therefore caller should follow these rules:
24470 * Try to fetch any prerequisite types we may need to build this DIE type
24471 before building the type and calling set_die_type.
e71ec853 24472 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24473 possible before fetching more types to complete the current type.
24474 * Make the type as complete as possible before fetching more types. */
1c379e20 24475
f792889a 24476static struct type *
1c379e20
DJ
24477set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24478{
518817b3
SM
24479 struct dwarf2_per_objfile *dwarf2_per_objfile
24480 = cu->per_cu->dwarf2_per_objfile;
dee91e82 24481 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 24482 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
24483 struct attribute *attr;
24484 struct dynamic_prop prop;
1c379e20 24485
b4ba55a1
JB
24486 /* For Ada types, make sure that the gnat-specific data is always
24487 initialized (if not already set). There are a few types where
24488 we should not be doing so, because the type-specific area is
24489 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24490 where the type-specific area is used to store the floatformat).
24491 But this is not a problem, because the gnat-specific information
24492 is actually not needed for these types. */
24493 if (need_gnat_info (cu)
24494 && TYPE_CODE (type) != TYPE_CODE_FUNC
24495 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
24496 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24497 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24498 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
24499 && !HAVE_GNAT_AUX_INFO (type))
24500 INIT_GNAT_SPECIFIC (type);
24501
3f2f83dd
KB
24502 /* Read DW_AT_allocated and set in type. */
24503 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 24504 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24505 {
09ba997f 24506 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24507 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24508 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
24509 }
24510 else if (attr != NULL)
24511 {
b98664d3 24512 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 24513 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24514 sect_offset_str (die->sect_off));
3f2f83dd
KB
24515 }
24516
24517 /* Read DW_AT_associated and set in type. */
24518 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 24519 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24520 {
09ba997f 24521 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24522 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24523 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
24524 }
24525 else if (attr != NULL)
24526 {
b98664d3 24527 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 24528 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24529 sect_offset_str (die->sect_off));
3f2f83dd
KB
24530 }
24531
3cdcd0ce
JB
24532 /* Read DW_AT_data_location and set in type. */
24533 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 24534 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 24535 cu->per_cu->addr_type ()))
50a82047 24536 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 24537
dee91e82 24538 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
24539 dwarf2_per_objfile->die_type_hash
24540 = htab_up (htab_create_alloc (127,
24541 per_cu_offset_and_type_hash,
24542 per_cu_offset_and_type_eq,
24543 NULL, xcalloc, xfree));
1c379e20 24544
dee91e82 24545 ofs.per_cu = cu->per_cu;
9c541725 24546 ofs.sect_off = die->sect_off;
1c379e20 24547 ofs.type = type;
dee91e82 24548 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 24549 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24550 if (*slot)
b98664d3 24551 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24552 sect_offset_str (die->sect_off));
8d749320
SM
24553 *slot = XOBNEW (&objfile->objfile_obstack,
24554 struct dwarf2_per_cu_offset_and_type);
1c379e20 24555 **slot = ofs;
f792889a 24556 return type;
1c379e20
DJ
24557}
24558
9c541725 24559/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24560 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24561
24562static struct type *
9c541725 24563get_die_type_at_offset (sect_offset sect_off,
673bfd45 24564 struct dwarf2_per_cu_data *per_cu)
1c379e20 24565{
dee91e82 24566 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 24567 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 24568
dee91e82 24569 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 24570 return NULL;
1c379e20 24571
dee91e82 24572 ofs.per_cu = per_cu;
9c541725 24573 ofs.sect_off = sect_off;
9a3c8263 24574 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 24575 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24576 if (slot)
24577 return slot->type;
24578 else
24579 return NULL;
24580}
24581
02142a6c 24582/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24583 or return NULL if DIE does not have a saved type. */
24584
24585static struct type *
24586get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24587{
9c541725 24588 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24589}
24590
10b3939b
DJ
24591/* Add a dependence relationship from CU to REF_PER_CU. */
24592
24593static void
24594dwarf2_add_dependence (struct dwarf2_cu *cu,
24595 struct dwarf2_per_cu_data *ref_per_cu)
24596{
24597 void **slot;
24598
24599 if (cu->dependencies == NULL)
24600 cu->dependencies
24601 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24602 NULL, &cu->comp_unit_obstack,
24603 hashtab_obstack_allocate,
24604 dummy_obstack_deallocate);
24605
24606 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24607 if (*slot == NULL)
24608 *slot = ref_per_cu;
24609}
1c379e20 24610
f504f079
DE
24611/* Subroutine of dwarf2_mark to pass to htab_traverse.
24612 Set the mark field in every compilation unit in the
ae038cb0
DJ
24613 cache that we must keep because we are keeping CU. */
24614
10b3939b
DJ
24615static int
24616dwarf2_mark_helper (void **slot, void *data)
24617{
24618 struct dwarf2_per_cu_data *per_cu;
24619
24620 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
24621
24622 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24623 reading of the chain. As such dependencies remain valid it is not much
24624 useful to track and undo them during QUIT cleanups. */
24625 if (per_cu->cu == NULL)
24626 return 1;
24627
10b3939b
DJ
24628 if (per_cu->cu->mark)
24629 return 1;
9068261f 24630 per_cu->cu->mark = true;
10b3939b
DJ
24631
24632 if (per_cu->cu->dependencies != NULL)
24633 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24634
24635 return 1;
24636}
24637
f504f079
DE
24638/* Set the mark field in CU and in every other compilation unit in the
24639 cache that we must keep because we are keeping CU. */
24640
ae038cb0
DJ
24641static void
24642dwarf2_mark (struct dwarf2_cu *cu)
24643{
24644 if (cu->mark)
24645 return;
9068261f 24646 cu->mark = true;
10b3939b
DJ
24647 if (cu->dependencies != NULL)
24648 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
24649}
24650
24651static void
24652dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24653{
24654 while (per_cu)
24655 {
9068261f 24656 per_cu->cu->mark = false;
ae038cb0
DJ
24657 per_cu = per_cu->cu->read_in_chain;
24658 }
72bf9492
DJ
24659}
24660
72bf9492
DJ
24661/* Trivial hash function for partial_die_info: the hash value of a DIE
24662 is its offset in .debug_info for this objfile. */
24663
24664static hashval_t
24665partial_die_hash (const void *item)
24666{
9a3c8263
SM
24667 const struct partial_die_info *part_die
24668 = (const struct partial_die_info *) item;
9a619af0 24669
9c541725 24670 return to_underlying (part_die->sect_off);
72bf9492
DJ
24671}
24672
24673/* Trivial comparison function for partial_die_info structures: two DIEs
24674 are equal if they have the same offset. */
24675
24676static int
24677partial_die_eq (const void *item_lhs, const void *item_rhs)
24678{
9a3c8263
SM
24679 const struct partial_die_info *part_die_lhs
24680 = (const struct partial_die_info *) item_lhs;
24681 const struct partial_die_info *part_die_rhs
24682 = (const struct partial_die_info *) item_rhs;
9a619af0 24683
9c541725 24684 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24685}
24686
3c3bb058
AB
24687struct cmd_list_element *set_dwarf_cmdlist;
24688struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
24689
24690static void
981a3fb3 24691set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 24692{
b4f54984 24693 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 24694 gdb_stdout);
ae038cb0
DJ
24695}
24696
24697static void
981a3fb3 24698show_dwarf_cmd (const char *args, int from_tty)
6e70227d 24699{
b4f54984 24700 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
24701}
24702
9291a0cd 24703static void
cd4fb1b2
SM
24704show_check_physname (struct ui_file *file, int from_tty,
24705 struct cmd_list_element *c, const char *value)
9291a0cd 24706{
cd4fb1b2
SM
24707 fprintf_filtered (file,
24708 _("Whether to check \"physname\" is %s.\n"),
24709 value);
9291a0cd
TT
24710}
24711
6c265988 24712void _initialize_dwarf2_read ();
cd4fb1b2 24713void
6c265988 24714_initialize_dwarf2_read ()
9291a0cd 24715{
cd4fb1b2
SM
24716 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24717Set DWARF specific variables.\n\
590042fc 24718Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
24719 &set_dwarf_cmdlist, "maintenance set dwarf ",
24720 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24721
cd4fb1b2 24722 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
24723Show DWARF specific variables.\n\
24724Show DWARF variables such as the cache size."),
cd4fb1b2
SM
24725 &show_dwarf_cmdlist, "maintenance show dwarf ",
24726 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24727
cd4fb1b2
SM
24728 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24729 &dwarf_max_cache_age, _("\
24730Set the upper bound on the age of cached DWARF compilation units."), _("\
24731Show the upper bound on the age of cached DWARF compilation units."), _("\
24732A higher limit means that cached compilation units will be stored\n\
24733in memory longer, and more total memory will be used. Zero disables\n\
24734caching, which can slow down startup."),
24735 NULL,
24736 show_dwarf_max_cache_age,
24737 &set_dwarf_cmdlist,
24738 &show_dwarf_cmdlist);
156942c7 24739
cd4fb1b2
SM
24740 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24741Set debugging of the DWARF reader."), _("\
24742Show debugging of the DWARF reader."), _("\
24743When enabled (non-zero), debugging messages are printed during DWARF\n\
24744reading and symtab expansion. A value of 1 (one) provides basic\n\
24745information. A value greater than 1 provides more verbose information."),
24746 NULL,
24747 NULL,
24748 &setdebuglist, &showdebuglist);
9291a0cd 24749
cd4fb1b2
SM
24750 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24751Set debugging of the DWARF DIE reader."), _("\
24752Show debugging of the DWARF DIE reader."), _("\
24753When enabled (non-zero), DIEs are dumped after they are read in.\n\
24754The value is the maximum depth to print."),
24755 NULL,
24756 NULL,
24757 &setdebuglist, &showdebuglist);
9291a0cd 24758
cd4fb1b2
SM
24759 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24760Set debugging of the dwarf line reader."), _("\
24761Show debugging of the dwarf line reader."), _("\
24762When enabled (non-zero), line number entries are dumped as they are read in.\n\
24763A value of 1 (one) provides basic information.\n\
24764A value greater than 1 provides more verbose information."),
24765 NULL,
24766 NULL,
24767 &setdebuglist, &showdebuglist);
437afbb8 24768
cd4fb1b2
SM
24769 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24770Set cross-checking of \"physname\" code against demangler."), _("\
24771Show cross-checking of \"physname\" code against demangler."), _("\
24772When enabled, GDB's internal \"physname\" code is checked against\n\
24773the demangler."),
24774 NULL, show_check_physname,
24775 &setdebuglist, &showdebuglist);
900e11f9 24776
e615022a
DE
24777 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24778 no_class, &use_deprecated_index_sections, _("\
24779Set whether to use deprecated gdb_index sections."), _("\
24780Show whether to use deprecated gdb_index sections."), _("\
24781When enabled, deprecated .gdb_index sections are used anyway.\n\
24782Normally they are ignored either because of a missing feature or\n\
24783performance issue.\n\
24784Warning: This option must be enabled before gdb reads the file."),
24785 NULL,
24786 NULL,
24787 &setlist, &showlist);
24788
f1e6e072
TT
24789 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24790 &dwarf2_locexpr_funcs);
24791 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24792 &dwarf2_loclist_funcs);
24793
24794 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24795 &dwarf2_block_frame_base_locexpr_funcs);
24796 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24797 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24798
24799#if GDB_SELF_TEST
24800 selftests::register_test ("dw2_expand_symtabs_matching",
24801 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24802 selftests::register_test ("dwarf2_find_containing_comp_unit",
24803 selftests::find_containing_comp_unit::run_test);
c62446b1 24804#endif
6502dd73 24805}
This page took 7.663905 seconds and 4 git commands to generate.