gdb: print thread names in thread apply command output
[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"
4de283e4
TT
40#include "bfd.h"
41#include "elf-bfd.h"
42#include "symtab.h"
43#include "gdbtypes.h"
44#include "objfiles.h"
d55e5aa6 45#include "dwarf2.h"
4de283e4
TT
46#include "buildsym.h"
47#include "demangle.h"
48#include "gdb-demangle.h"
4de283e4
TT
49#include "filenames.h" /* for DOSish file names */
50#include "macrotab.h"
51#include "language.h"
52#include "complaints.h"
82ca8957
TT
53#include "dwarf2/expr.h"
54#include "dwarf2/loc.h"
4de283e4
TT
55#include "cp-support.h"
56#include "hashtab.h"
57#include "command.h"
d55e5aa6 58#include "gdbcmd.h"
4de283e4
TT
59#include "block.h"
60#include "addrmap.h"
61#include "typeprint.h"
62#include "psympriv.h"
4de283e4 63#include "c-lang.h"
d55e5aa6 64#include "go-lang.h"
4de283e4
TT
65#include "valprint.h"
66#include "gdbcore.h" /* for gnutarget */
67#include "gdb/gdb-index.h"
4de283e4
TT
68#include "gdb_bfd.h"
69#include "f-lang.h"
70#include "source.h"
4de283e4 71#include "build-id.h"
d55e5aa6 72#include "namespace.h"
268a13a5
TT
73#include "gdbsupport/function-view.h"
74#include "gdbsupport/gdb_optional.h"
75#include "gdbsupport/underlying.h"
268a13a5 76#include "gdbsupport/hash_enum.h"
4de283e4 77#include "filename-seen-cache.h"
b32b108a 78#include "producer.h"
4de283e4 79#include <fcntl.h>
4de283e4 80#include <algorithm>
4de283e4 81#include <unordered_map>
268a13a5 82#include "gdbsupport/selftest.h"
c9317f21 83#include "rust-lang.h"
268a13a5 84#include "gdbsupport/pathstuff.h"
edd45eb0 85#include "count-one-bits.h"
437afbb8 86
73be47f5
DE
87/* When == 1, print basic high level tracing messages.
88 When > 1, be more verbose.
b4f54984
DE
89 This is in contrast to the low level DIE reading of dwarf_die_debug. */
90static unsigned int dwarf_read_debug = 0;
45cfd468 91
d97bc12b 92/* When non-zero, dump DIEs after they are read in. */
b4f54984 93static unsigned int dwarf_die_debug = 0;
d97bc12b 94
27e0867f 95/* When non-zero, dump line number entries as they are read in. */
8fdd972c 96unsigned int dwarf_line_debug = 0;
27e0867f 97
491144b5
CB
98/* When true, cross-check physname against demangler. */
99static bool check_physname = false;
900e11f9 100
491144b5
CB
101/* When true, do not reject deprecated .gdb_index sections. */
102static bool use_deprecated_index_sections = false;
481860b3 103
5bfd760d 104static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 105
f1e6e072
TT
106/* The "aclass" indices for various kinds of computed DWARF symbols. */
107
108static int dwarf2_locexpr_index;
109static int dwarf2_loclist_index;
110static int dwarf2_locexpr_block_index;
111static int dwarf2_loclist_block_index;
112
3f563c84
PA
113/* An index into a (C++) symbol name component in a symbol name as
114 recorded in the mapped_index's symbol table. For each C++ symbol
115 in the symbol table, we record one entry for the start of each
116 component in the symbol in a table of name components, and then
117 sort the table, in order to be able to binary search symbol names,
118 ignoring leading namespaces, both completion and regular look up.
119 For example, for symbol "A::B::C", we'll have an entry that points
120 to "A::B::C", another that points to "B::C", and another for "C".
121 Note that function symbols in GDB index have no parameter
122 information, just the function/method names. You can convert a
123 name_component to a "const char *" using the
124 'mapped_index::symbol_name_at(offset_type)' method. */
125
126struct name_component
127{
128 /* Offset in the symbol name where the component starts. Stored as
129 a (32-bit) offset instead of a pointer to save memory and improve
130 locality on 64-bit architectures. */
131 offset_type name_offset;
132
133 /* The symbol's index in the symbol and constant pool tables of a
134 mapped_index. */
135 offset_type idx;
136};
137
44ed8f3e
PA
138/* Base class containing bits shared by both .gdb_index and
139 .debug_name indexes. */
140
141struct mapped_index_base
142{
22ca247e
TT
143 mapped_index_base () = default;
144 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
145
44ed8f3e
PA
146 /* The name_component table (a sorted vector). See name_component's
147 description above. */
148 std::vector<name_component> name_components;
149
150 /* How NAME_COMPONENTS is sorted. */
151 enum case_sensitivity name_components_casing;
152
153 /* Return the number of names in the symbol table. */
154 virtual size_t symbol_name_count () const = 0;
155
156 /* Get the name of the symbol at IDX in the symbol table. */
157 virtual const char *symbol_name_at (offset_type idx) const = 0;
158
159 /* Return whether the name at IDX in the symbol table should be
160 ignored. */
161 virtual bool symbol_name_slot_invalid (offset_type idx) const
162 {
163 return false;
164 }
165
166 /* Build the symbol name component sorted vector, if we haven't
167 yet. */
168 void build_name_components ();
169
170 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
171 possible matches for LN_NO_PARAMS in the name component
172 vector. */
173 std::pair<std::vector<name_component>::const_iterator,
174 std::vector<name_component>::const_iterator>
3b00ef10
TT
175 find_name_components_bounds (const lookup_name_info &ln_no_params,
176 enum language lang) const;
44ed8f3e
PA
177
178 /* Prevent deleting/destroying via a base class pointer. */
179protected:
180 ~mapped_index_base() = default;
181};
182
9291a0cd
TT
183/* A description of the mapped index. The file format is described in
184 a comment by the code that writes the index. */
fc898b42 185struct mapped_index final : public mapped_index_base
9291a0cd 186{
f00a2de2
PA
187 /* A slot/bucket in the symbol table hash. */
188 struct symbol_table_slot
189 {
190 const offset_type name;
191 const offset_type vec;
192 };
193
559a7a62 194 /* Index data format version. */
3063847f 195 int version = 0;
559a7a62 196
f00a2de2
PA
197 /* The address table data. */
198 gdb::array_view<const gdb_byte> address_table;
b11b1f88 199
3876f04e 200 /* The symbol table, implemented as a hash table. */
f00a2de2 201 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 202
9291a0cd 203 /* A pointer to the constant pool. */
3063847f 204 const char *constant_pool = nullptr;
3f563c84 205
44ed8f3e
PA
206 bool symbol_name_slot_invalid (offset_type idx) const override
207 {
208 const auto &bucket = this->symbol_table[idx];
9ab08412 209 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 210 }
5c58de74 211
3f563c84
PA
212 /* Convenience method to get at the name of the symbol at IDX in the
213 symbol table. */
44ed8f3e 214 const char *symbol_name_at (offset_type idx) const override
f00a2de2 215 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 216
44ed8f3e
PA
217 size_t symbol_name_count () const override
218 { return this->symbol_table.size (); }
9291a0cd
TT
219};
220
927aa2e7
JK
221/* A description of the mapped .debug_names.
222 Uninitialized map has CU_COUNT 0. */
fc898b42 223struct mapped_debug_names final : public mapped_index_base
927aa2e7 224{
ed2dc618
SM
225 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
226 : dwarf2_per_objfile (dwarf2_per_objfile_)
227 {}
228
229 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
230 bfd_endian dwarf5_byte_order;
231 bool dwarf5_is_dwarf64;
232 bool augmentation_is_gdb;
233 uint8_t offset_size;
234 uint32_t cu_count = 0;
235 uint32_t tu_count, bucket_count, name_count;
236 const gdb_byte *cu_table_reordered, *tu_table_reordered;
237 const uint32_t *bucket_table_reordered, *hash_table_reordered;
238 const gdb_byte *name_table_string_offs_reordered;
239 const gdb_byte *name_table_entry_offs_reordered;
240 const gdb_byte *entry_pool;
241
242 struct index_val
243 {
244 ULONGEST dwarf_tag;
245 struct attr
246 {
247 /* Attribute name DW_IDX_*. */
248 ULONGEST dw_idx;
249
250 /* Attribute form DW_FORM_*. */
251 ULONGEST form;
252
253 /* Value if FORM is DW_FORM_implicit_const. */
254 LONGEST implicit_const;
255 };
256 std::vector<attr> attr_vec;
257 };
258
259 std::unordered_map<ULONGEST, index_val> abbrev_map;
260
261 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
262
263 /* Implementation of the mapped_index_base virtual interface, for
264 the name_components cache. */
265
266 const char *symbol_name_at (offset_type idx) const override
267 { return namei_to_name (idx); }
268
269 size_t symbol_name_count () const override
270 { return this->name_count; }
927aa2e7
JK
271};
272
cd4fb1b2 273/* See dwarf2read.h. */
ed2dc618 274
cd4fb1b2 275dwarf2_per_objfile *
ed2dc618
SM
276get_dwarf2_per_objfile (struct objfile *objfile)
277{
5bfd760d 278 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 279}
c906108c 280
251d32d9 281/* Default names of the debugging sections. */
c906108c 282
233a11ab
CS
283/* Note that if the debugging section has been compressed, it might
284 have a name like .zdebug_info. */
285
9cdd5dbd
DE
286static const struct dwarf2_debug_sections dwarf2_elf_names =
287{
251d32d9
TG
288 { ".debug_info", ".zdebug_info" },
289 { ".debug_abbrev", ".zdebug_abbrev" },
290 { ".debug_line", ".zdebug_line" },
291 { ".debug_loc", ".zdebug_loc" },
43988095 292 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 293 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 294 { ".debug_macro", ".zdebug_macro" },
251d32d9 295 { ".debug_str", ".zdebug_str" },
18a8505e 296 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 297 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 298 { ".debug_ranges", ".zdebug_ranges" },
43988095 299 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 300 { ".debug_types", ".zdebug_types" },
3019eac3 301 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
24d3216f 304 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
305 { ".debug_names", ".zdebug_names" },
306 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 307 23
251d32d9 308};
c906108c 309
80626a55 310/* List of DWO/DWP sections. */
3019eac3 311
80626a55 312static const struct dwop_section_names
3019eac3
DE
313{
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
43988095 318 struct dwarf2_section_names loclists_dwo;
09262596
DE
319 struct dwarf2_section_names macinfo_dwo;
320 struct dwarf2_section_names macro_dwo;
3019eac3
DE
321 struct dwarf2_section_names str_dwo;
322 struct dwarf2_section_names str_offsets_dwo;
323 struct dwarf2_section_names types_dwo;
80626a55
DE
324 struct dwarf2_section_names cu_index;
325 struct dwarf2_section_names tu_index;
3019eac3 326}
80626a55 327dwop_section_names =
3019eac3
DE
328{
329 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
330 { ".debug_info.dwo", ".zdebug_info.dwo" },
331 { ".debug_line.dwo", ".zdebug_line.dwo" },
332 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 333 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
334 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
335 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
336 { ".debug_str.dwo", ".zdebug_str.dwo" },
337 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
338 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
339 { ".debug_cu_index", ".zdebug_cu_index" },
340 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
341};
342
c906108c
SS
343/* local data types */
344
3da10d80
KS
345/* Type used for delaying computation of method physnames.
346 See comments for compute_delayed_physnames. */
347struct delayed_method_info
348{
349 /* The type to which the method is attached, i.e., its parent class. */
350 struct type *type;
351
352 /* The index of the method in the type's function fieldlists. */
353 int fnfield_index;
354
355 /* The index of the method in the fieldlist. */
356 int index;
357
358 /* The name of the DIE. */
359 const char *name;
360
361 /* The DIE associated with this method. */
362 struct die_info *die;
363};
364
e7c27a73
DJ
365/* Internal state when decoding a particular compilation unit. */
366struct dwarf2_cu
367{
fcd3b13d
SM
368 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
369 ~dwarf2_cu ();
370
371 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
372
c24bdb02
KS
373 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
374 Create the set of symtabs used by this TU, or if this TU is sharing
375 symtabs with another TU and the symtabs have already been created
376 then restore those symtabs in the line header.
377 We don't need the pc/line-number mapping for type units. */
378 void setup_type_unit_groups (struct die_info *die);
379
380 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
381 buildsym_compunit constructor. */
382 struct compunit_symtab *start_symtab (const char *name,
383 const char *comp_dir,
384 CORE_ADDR low_pc);
385
386 /* Reset the builder. */
387 void reset_builder () { m_builder.reset (); }
388
d00adf39 389 /* The header of the compilation unit. */
fcd3b13d 390 struct comp_unit_head header {};
e142c38c 391
d00adf39 392 /* Base address of this compilation unit. */
fcd3b13d 393 CORE_ADDR base_address = 0;
d00adf39
DE
394
395 /* Non-zero if base_address has been set. */
fcd3b13d 396 int base_known = 0;
d00adf39 397
e142c38c 398 /* The language we are debugging. */
fcd3b13d
SM
399 enum language language = language_unknown;
400 const struct language_defn *language_defn = nullptr;
e142c38c 401
fcd3b13d 402 const char *producer = nullptr;
b0f35d58 403
c24bdb02 404private:
804d2729
TT
405 /* The symtab builder for this CU. This is only non-NULL when full
406 symbols are being read. */
c24bdb02 407 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 408
c24bdb02 409public:
e142c38c
DJ
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
fcd3b13d 419 struct pending **list_in_scope = nullptr;
e142c38c 420
b64f50a1
JK
421 /* Hash table holding all the loaded partial DIEs
422 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 423 htab_t partial_dies = nullptr;
72bf9492
DJ
424
425 /* Storage for things with the same lifetime as this read-in compilation
426 unit, including partial DIEs. */
fcd3b13d 427 auto_obstack comp_unit_obstack;
72bf9492 428
ae038cb0
DJ
429 /* When multiple dwarf2_cu structures are living in memory, this field
430 chains them all together, so that they can be released efficiently.
431 We will probably also want a generation counter so that most-recently-used
432 compilation units are cached... */
fcd3b13d 433 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 434
69d751e3 435 /* Backlink to our per_cu entry. */
ae038cb0
DJ
436 struct dwarf2_per_cu_data *per_cu;
437
438 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 439 int last_used = 0;
ae038cb0 440
b64f50a1
JK
441 /* A hash table of DIE cu_offset for following references with
442 die_info->offset.sect_off as hash. */
fcd3b13d 443 htab_t die_hash = nullptr;
10b3939b
DJ
444
445 /* Full DIEs if read in. */
fcd3b13d 446 struct die_info *dies = nullptr;
10b3939b
DJ
447
448 /* A set of pointers to dwarf2_per_cu_data objects for compilation
449 units referenced by this one. Only set during full symbol processing;
450 partial symbol tables do not have dependencies. */
fcd3b13d 451 htab_t dependencies = nullptr;
10b3939b 452
cb1df416 453 /* Header data from the line table, during full symbol processing. */
fcd3b13d 454 struct line_header *line_header = nullptr;
4c8aa72d
PA
455 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
456 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
457 this is the DW_TAG_compile_unit die for this CU. We'll hold on
458 to the line header as long as this DIE is being processed. See
459 process_die_scope. */
fcd3b13d 460 die_info *line_header_die_owner = nullptr;
cb1df416 461
3da10d80
KS
462 /* A list of methods which need to have physnames computed
463 after all type information has been read. */
c89b44cd 464 std::vector<delayed_method_info> method_list;
3da10d80 465
96408a79 466 /* To be copied to symtab->call_site_htab. */
fcd3b13d 467 htab_t call_site_htab = nullptr;
96408a79 468
034e5797
DE
469 /* Non-NULL if this CU came from a DWO file.
470 There is an invariant here that is important to remember:
471 Except for attributes copied from the top level DIE in the "main"
472 (or "stub") file in preparation for reading the DWO file
18a8505e 473 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
474 Either there isn't a DWO file (in which case this is NULL and the point
475 is moot), or there is and either we're not going to read it (in which
476 case this is NULL) or there is and we are reading it (in which case this
477 is non-NULL). */
fcd3b13d 478 struct dwo_unit *dwo_unit = nullptr;
3019eac3 479
18a8505e 480 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 481 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 482 gdb::optional<ULONGEST> addr_base;
3019eac3 483
18a8505e 484 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 485 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 486 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
487 be used without needing to know whether DWO files are in use or not.
488 N.B. This does not apply to DW_AT_ranges appearing in
489 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
490 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 491 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 492 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 493 ULONGEST ranges_base = 0;
2e3cf129 494
c9317f21
TT
495 /* When reading debug info generated by older versions of rustc, we
496 have to rewrite some union types to be struct types with a
497 variant part. This rewriting must be done after the CU is fully
498 read in, because otherwise at the point of rewriting some struct
499 type might not have been fully processed. So, we keep a list of
500 all such types here and process them after expansion. */
501 std::vector<struct type *> rust_unions;
502
18a8505e
AT
503 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
504 files, the value is implicitly zero. For DWARF 5 version DWO files, the
505 value is often implicit and is the size of the header of
506 .debug_str_offsets section (8 or 4, depending on the address size). */
507 gdb::optional<ULONGEST> str_offsets_base;
508
ae038cb0 509 /* Mark used when releasing cached dies. */
9068261f 510 bool mark : 1;
ae038cb0 511
8be455d7
JK
512 /* This CU references .debug_loc. See the symtab->locations_valid field.
513 This test is imperfect as there may exist optimized debug code not using
514 any location list and still facing inlining issues if handled as
515 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 516 bool has_loclist : 1;
ba919b58 517
9068261f 518 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
519 if all the producer_is_* fields are valid. This information is cached
520 because profiling CU expansion showed excessive time spent in
521 producer_is_gxx_lt_4_6. */
9068261f
AB
522 bool checked_producer : 1;
523 bool producer_is_gxx_lt_4_6 : 1;
524 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 525 bool producer_is_icc : 1;
9068261f 526 bool producer_is_icc_lt_14 : 1;
c258c396 527 bool producer_is_codewarrior : 1;
4d4ec4e5 528
9068261f 529 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
530 debugging info for C++ namespaces. GCC 3.3.x did not produce
531 this information, but later versions do. */
532
9068261f 533 bool processing_has_namespace_info : 1;
d590ff25
YQ
534
535 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
536
537 /* If this CU was inherited by another CU (via specification,
538 abstract_origin, etc), this is the ancestor CU. */
539 dwarf2_cu *ancestor;
540
541 /* Get the buildsym_compunit for this CU. */
542 buildsym_compunit *get_builder ()
543 {
544 /* If this CU has a builder associated with it, use that. */
545 if (m_builder != nullptr)
546 return m_builder.get ();
547
548 /* Otherwise, search ancestors for a valid builder. */
549 if (ancestor != nullptr)
550 return ancestor->get_builder ();
551
552 return nullptr;
553 }
e7c27a73
DJ
554};
555
094b34ac
DE
556/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
557 This includes type_unit_group and quick_file_names. */
558
559struct stmt_list_hash
560{
561 /* The DWO unit this table is from or NULL if there is none. */
562 struct dwo_unit *dwo_unit;
563
564 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 565 sect_offset line_sect_off;
094b34ac
DE
566};
567
f4dc4d17
DE
568/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
569 an object of this type. */
570
571struct type_unit_group
572{
0186c6a7 573 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
574 To simplify things we create an artificial CU that "includes" all the
575 type units using this stmt_list so that the rest of the code still has
197400e8 576 a "per_cu" handle on the symtab. */
094b34ac
DE
577 struct dwarf2_per_cu_data per_cu;
578
0186c6a7
DE
579 /* The TUs that share this DW_AT_stmt_list entry.
580 This is added to while parsing type units to build partial symtabs,
581 and is deleted afterwards and not used again. */
a8b3b8e9 582 std::vector<signatured_type *> *tus;
f4dc4d17 583
43f3e411 584 /* The compunit symtab.
094b34ac 585 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
586 so we create an essentially anonymous symtab as the compunit symtab. */
587 struct compunit_symtab *compunit_symtab;
f4dc4d17 588
094b34ac
DE
589 /* The data used to construct the hash key. */
590 struct stmt_list_hash hash;
f4dc4d17 591
f4dc4d17
DE
592 /* The symbol tables for this TU (obtained from the files listed in
593 DW_AT_stmt_list).
594 WARNING: The order of entries here must match the order of entries
595 in the line header. After the first TU using this type_unit_group, the
596 line header for the subsequent TUs is recreated from this. This is done
597 because we need to use the same symtabs for each TU using the same
598 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
599 there's no guarantee the line header doesn't have duplicate entries. */
600 struct symtab **symtabs;
601};
602
73869dc2 603/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
604
605struct dwo_sections
606{
607 struct dwarf2_section_info abbrev;
3019eac3
DE
608 struct dwarf2_section_info line;
609 struct dwarf2_section_info loc;
43988095 610 struct dwarf2_section_info loclists;
09262596
DE
611 struct dwarf2_section_info macinfo;
612 struct dwarf2_section_info macro;
3019eac3
DE
613 struct dwarf2_section_info str;
614 struct dwarf2_section_info str_offsets;
80626a55
DE
615 /* In the case of a virtual DWO file, these two are unused. */
616 struct dwarf2_section_info info;
fd5866f6 617 std::vector<dwarf2_section_info> types;
3019eac3
DE
618};
619
c88ee1f0 620/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
621
622struct dwo_unit
623{
624 /* Backlink to the containing struct dwo_file. */
625 struct dwo_file *dwo_file;
626
627 /* The "id" that distinguishes this CU/TU.
628 .debug_info calls this "dwo_id", .debug_types calls this "signature".
629 Since signatures came first, we stick with it for consistency. */
630 ULONGEST signature;
631
632 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 633 struct dwarf2_section_info *section;
3019eac3 634
9c541725
PA
635 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
636 sect_offset sect_off;
3019eac3
DE
637 unsigned int length;
638
639 /* For types, offset in the type's DIE of the type defined by this TU. */
640 cu_offset type_offset_in_tu;
641};
642
73869dc2
DE
643/* include/dwarf2.h defines the DWP section codes.
644 It defines a max value but it doesn't define a min value, which we
645 use for error checking, so provide one. */
646
647enum dwp_v2_section_ids
648{
649 DW_SECT_MIN = 1
650};
651
80626a55 652/* Data for one DWO file.
57d63ce2
DE
653
654 This includes virtual DWO files (a virtual DWO file is a DWO file as it
655 appears in a DWP file). DWP files don't really have DWO files per se -
656 comdat folding of types "loses" the DWO file they came from, and from
657 a high level view DWP files appear to contain a mass of random types.
658 However, to maintain consistency with the non-DWP case we pretend DWP
659 files contain virtual DWO files, and we assign each TU with one virtual
660 DWO file (generally based on the line and abbrev section offsets -
661 a heuristic that seems to work in practice). */
3019eac3
DE
662
663struct dwo_file
664{
51ac9db5
SM
665 dwo_file () = default;
666 DISABLE_COPY_AND_ASSIGN (dwo_file);
667
18a8505e 668 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
669 For virtual DWO files the name is constructed from the section offsets
670 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
671 from related CU+TUs. */
51ac9db5 672 const char *dwo_name = nullptr;
0ac5b59e
DE
673
674 /* The DW_AT_comp_dir attribute. */
51ac9db5 675 const char *comp_dir = nullptr;
3019eac3 676
80626a55
DE
677 /* The bfd, when the file is open. Otherwise this is NULL.
678 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 679 gdb_bfd_ref_ptr dbfd;
3019eac3 680
73869dc2
DE
681 /* The sections that make up this DWO file.
682 Remember that for virtual DWO files in DWP V2, these are virtual
683 sections (for lack of a better name). */
51ac9db5 684 struct dwo_sections sections {};
3019eac3 685
33c5cd75
DB
686 /* The CUs in the file.
687 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
688 an extension to handle LLVM's Link Time Optimization output (where
689 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 690 htab_up cus;
3019eac3
DE
691
692 /* Table of TUs in the file.
693 Each element is a struct dwo_unit. */
b0b6a987 694 htab_up tus;
3019eac3
DE
695};
696
80626a55
DE
697/* These sections are what may appear in a DWP file. */
698
699struct dwp_sections
700{
73869dc2 701 /* These are used by both DWP version 1 and 2. */
80626a55
DE
702 struct dwarf2_section_info str;
703 struct dwarf2_section_info cu_index;
704 struct dwarf2_section_info tu_index;
73869dc2
DE
705
706 /* These are only used by DWP version 2 files.
707 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
708 sections are referenced by section number, and are not recorded here.
709 In DWP version 2 there is at most one copy of all these sections, each
710 section being (effectively) comprised of the concatenation of all of the
711 individual sections that exist in the version 1 format.
712 To keep the code simple we treat each of these concatenated pieces as a
713 section itself (a virtual section?). */
714 struct dwarf2_section_info abbrev;
715 struct dwarf2_section_info info;
716 struct dwarf2_section_info line;
717 struct dwarf2_section_info loc;
718 struct dwarf2_section_info macinfo;
719 struct dwarf2_section_info macro;
720 struct dwarf2_section_info str_offsets;
721 struct dwarf2_section_info types;
80626a55
DE
722};
723
73869dc2
DE
724/* These sections are what may appear in a virtual DWO file in DWP version 1.
725 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 726
73869dc2 727struct virtual_v1_dwo_sections
80626a55
DE
728{
729 struct dwarf2_section_info abbrev;
730 struct dwarf2_section_info line;
731 struct dwarf2_section_info loc;
732 struct dwarf2_section_info macinfo;
733 struct dwarf2_section_info macro;
734 struct dwarf2_section_info str_offsets;
735 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 736 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
737 struct dwarf2_section_info info_or_types;
738};
739
73869dc2
DE
740/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
741 In version 2, the sections of the DWO files are concatenated together
742 and stored in one section of that name. Thus each ELF section contains
743 several "virtual" sections. */
744
745struct virtual_v2_dwo_sections
746{
747 bfd_size_type abbrev_offset;
748 bfd_size_type abbrev_size;
749
750 bfd_size_type line_offset;
751 bfd_size_type line_size;
752
753 bfd_size_type loc_offset;
754 bfd_size_type loc_size;
755
756 bfd_size_type macinfo_offset;
757 bfd_size_type macinfo_size;
758
759 bfd_size_type macro_offset;
760 bfd_size_type macro_size;
761
762 bfd_size_type str_offsets_offset;
763 bfd_size_type str_offsets_size;
764
765 /* Each DWP hash table entry records one CU or one TU.
766 That is recorded here, and copied to dwo_unit.section. */
767 bfd_size_type info_or_types_offset;
768 bfd_size_type info_or_types_size;
769};
770
80626a55
DE
771/* Contents of DWP hash tables. */
772
773struct dwp_hash_table
774{
73869dc2 775 uint32_t version, nr_columns;
80626a55 776 uint32_t nr_units, nr_slots;
73869dc2
DE
777 const gdb_byte *hash_table, *unit_table;
778 union
779 {
780 struct
781 {
782 const gdb_byte *indices;
783 } v1;
784 struct
785 {
786 /* This is indexed by column number and gives the id of the section
787 in that column. */
788#define MAX_NR_V2_DWO_SECTIONS \
789 (1 /* .debug_info or .debug_types */ \
790 + 1 /* .debug_abbrev */ \
791 + 1 /* .debug_line */ \
792 + 1 /* .debug_loc */ \
793 + 1 /* .debug_str_offsets */ \
794 + 1 /* .debug_macro or .debug_macinfo */)
795 int section_ids[MAX_NR_V2_DWO_SECTIONS];
796 const gdb_byte *offsets;
797 const gdb_byte *sizes;
798 } v2;
799 } section_pool;
80626a55
DE
800};
801
802/* Data for one DWP file. */
803
804struct dwp_file
805{
400174b1
TT
806 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
807 : name (name_),
808 dbfd (std::move (abfd))
809 {
810 }
811
80626a55
DE
812 /* Name of the file. */
813 const char *name;
814
73869dc2 815 /* File format version. */
400174b1 816 int version = 0;
73869dc2 817
93417882 818 /* The bfd. */
400174b1 819 gdb_bfd_ref_ptr dbfd;
80626a55
DE
820
821 /* Section info for this file. */
400174b1 822 struct dwp_sections sections {};
80626a55 823
57d63ce2 824 /* Table of CUs in the file. */
400174b1 825 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
826
827 /* Table of TUs in the file. */
400174b1 828 const struct dwp_hash_table *tus = nullptr;
80626a55 829
19ac8c2e 830 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
831 htab_up loaded_cus;
832 htab_up loaded_tus;
80626a55 833
73869dc2
DE
834 /* Table to map ELF section numbers to their sections.
835 This is only needed for the DWP V1 file format. */
400174b1
TT
836 unsigned int num_sections = 0;
837 asection **elf_sections = nullptr;
80626a55
DE
838};
839
0963b4bd
MS
840/* Struct used to pass misc. parameters to read_die_and_children, et
841 al. which are used for both .debug_info and .debug_types dies.
842 All parameters here are unchanging for the life of the call. This
dee91e82 843 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
844
845struct die_reader_specs
846{
a32a8923 847 /* The bfd of die_section. */
93311388
DE
848 bfd* abfd;
849
850 /* The CU of the DIE we are parsing. */
851 struct dwarf2_cu *cu;
852
80626a55 853 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
854 struct dwo_file *dwo_file;
855
dee91e82 856 /* The section the die comes from.
3019eac3 857 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
858 struct dwarf2_section_info *die_section;
859
860 /* die_section->buffer. */
d521ce57 861 const gdb_byte *buffer;
f664829e
DE
862
863 /* The end of the buffer. */
864 const gdb_byte *buffer_end;
a2ce51a0 865
685af9cd
TT
866 /* The abbreviation table to use when reading the DIEs. */
867 struct abbrev_table *abbrev_table;
93311388
DE
868};
869
c0ab21c2
TT
870/* A subclass of die_reader_specs that holds storage and has complex
871 constructor and destructor behavior. */
872
873class cutu_reader : public die_reader_specs
874{
875public:
876
877 cutu_reader (struct dwarf2_per_cu_data *this_cu,
878 struct abbrev_table *abbrev_table,
6751ebae 879 int use_existing_cu,
c0ab21c2
TT
880 bool skip_partial);
881
882 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
883 struct dwarf2_cu *parent_cu = nullptr,
884 struct dwo_file *dwo_file = nullptr);
885
c0ab21c2
TT
886 DISABLE_COPY_AND_ASSIGN (cutu_reader);
887
888 const gdb_byte *info_ptr = nullptr;
889 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
890 bool dummy_p = false;
891
6751ebae
TT
892 /* Release the new CU, putting it on the chain. This cannot be done
893 for dummy CUs. */
894 void keep ();
895
c0ab21c2
TT
896private:
897 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 898 int use_existing_cu);
c0ab21c2
TT
899
900 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
901 std::unique_ptr<dwarf2_cu> m_new_cu;
902
903 /* The ordinary abbreviation table. */
904 abbrev_table_up m_abbrev_table_holder;
905
906 /* The DWO abbreviation table. */
907 abbrev_table_up m_dwo_abbrev_table;
908};
dee91e82 909
c906108c 910/* When we construct a partial symbol table entry we only
0963b4bd 911 need this much information. */
6f06d47b 912struct partial_die_info : public allocate_on_obstack
c906108c 913 {
6f06d47b
YQ
914 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
915
916 /* Disable assign but still keep copy ctor, which is needed
917 load_partial_dies. */
918 partial_die_info& operator=(const partial_die_info& rhs) = delete;
919
52356b79
YQ
920 /* Adjust the partial die before generating a symbol for it. This
921 function may set the is_external flag or change the DIE's
922 name. */
923 void fixup (struct dwarf2_cu *cu);
924
48fbe735
YQ
925 /* Read a minimal amount of information into the minimal die
926 structure. */
927 const gdb_byte *read (const struct die_reader_specs *reader,
928 const struct abbrev_info &abbrev,
929 const gdb_byte *info_ptr);
930
72bf9492 931 /* Offset of this DIE. */
6f06d47b 932 const sect_offset sect_off;
72bf9492
DJ
933
934 /* DWARF-2 tag for this DIE. */
6f06d47b 935 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 936
72bf9492 937 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
938 const unsigned int has_children : 1;
939
72bf9492
DJ
940 unsigned int is_external : 1;
941 unsigned int is_declaration : 1;
942 unsigned int has_type : 1;
943 unsigned int has_specification : 1;
944 unsigned int has_pc_info : 1;
481860b3 945 unsigned int may_be_inlined : 1;
72bf9492 946
0c1b455e
TT
947 /* This DIE has been marked DW_AT_main_subprogram. */
948 unsigned int main_subprogram : 1;
949
72bf9492
DJ
950 /* Flag set if the SCOPE field of this structure has been
951 computed. */
952 unsigned int scope_set : 1;
953
fa4028e9
JB
954 /* Flag set if the DIE has a byte_size attribute. */
955 unsigned int has_byte_size : 1;
956
ff908ebf
AW
957 /* Flag set if the DIE has a DW_AT_const_value attribute. */
958 unsigned int has_const_value : 1;
959
98bfdba5
PA
960 /* Flag set if any of the DIE's children are template arguments. */
961 unsigned int has_template_arguments : 1;
962
52356b79 963 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
964 unsigned int fixup_called : 1;
965
36586728
TT
966 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
967 unsigned int is_dwz : 1;
968
969 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
970 unsigned int spec_is_dwz : 1;
971
72bf9492 972 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 973 sometimes a default name for unnamed DIEs. */
6f06d47b 974 const char *name = nullptr;
72bf9492 975
abc72ce4 976 /* The linkage name, if present. */
6f06d47b 977 const char *linkage_name = nullptr;
abc72ce4 978
72bf9492
DJ
979 /* The scope to prepend to our children. This is generally
980 allocated on the comp_unit_obstack, so will disappear
981 when this compilation unit leaves the cache. */
6f06d47b 982 const char *scope = nullptr;
72bf9492 983
95554aad
TT
984 /* Some data associated with the partial DIE. The tag determines
985 which field is live. */
986 union
987 {
988 /* The location description associated with this DIE, if any. */
989 struct dwarf_block *locdesc;
990 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 991 sect_offset sect_off;
6f06d47b 992 } d {};
72bf9492
DJ
993
994 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
995 CORE_ADDR lowpc = 0;
996 CORE_ADDR highpc = 0;
72bf9492 997
93311388 998 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 999 DW_AT_sibling, if any. */
48fbe735
YQ
1000 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1001 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1002 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1003
1004 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1005 DW_AT_specification (or DW_AT_abstract_origin or
1006 DW_AT_extension). */
6f06d47b 1007 sect_offset spec_offset {};
72bf9492
DJ
1008
1009 /* Pointers to this DIE's parent, first child, and next sibling,
1010 if any. */
6f06d47b
YQ
1011 struct partial_die_info *die_parent = nullptr;
1012 struct partial_die_info *die_child = nullptr;
1013 struct partial_die_info *die_sibling = nullptr;
1014
1015 friend struct partial_die_info *
1016 dwarf2_cu::find_partial_die (sect_offset sect_off);
1017
1018 private:
1019 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1020 partial_die_info (sect_offset sect_off)
1021 : partial_die_info (sect_off, DW_TAG_padding, 0)
1022 {
1023 }
1024
1025 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1026 int has_children_)
1027 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1028 {
1029 is_external = 0;
1030 is_declaration = 0;
1031 has_type = 0;
1032 has_specification = 0;
1033 has_pc_info = 0;
1034 may_be_inlined = 0;
1035 main_subprogram = 0;
1036 scope_set = 0;
1037 has_byte_size = 0;
1038 has_const_value = 0;
1039 has_template_arguments = 0;
1040 fixup_called = 0;
1041 is_dwz = 0;
1042 spec_is_dwz = 0;
1043 }
c906108c
SS
1044 };
1045
0963b4bd 1046/* This data structure holds a complete die structure. */
c906108c
SS
1047struct die_info
1048 {
76815b17
DE
1049 /* DWARF-2 tag for this DIE. */
1050 ENUM_BITFIELD(dwarf_tag) tag : 16;
1051
1052 /* Number of attributes */
98bfdba5
PA
1053 unsigned char num_attrs;
1054
1055 /* True if we're presently building the full type name for the
1056 type derived from this DIE. */
1057 unsigned char building_fullname : 1;
76815b17 1058
adde2bff
DE
1059 /* True if this die is in process. PR 16581. */
1060 unsigned char in_process : 1;
1061
3e225074
TT
1062 /* True if this DIE has children. */
1063 unsigned char has_children : 1;
1064
76815b17
DE
1065 /* Abbrev number */
1066 unsigned int abbrev;
1067
93311388 1068 /* Offset in .debug_info or .debug_types section. */
9c541725 1069 sect_offset sect_off;
78ba4af6
JB
1070
1071 /* The dies in a compilation unit form an n-ary tree. PARENT
1072 points to this die's parent; CHILD points to the first child of
1073 this node; and all the children of a given node are chained
4950bc1c 1074 together via their SIBLING fields. */
639d11d3
DC
1075 struct die_info *child; /* Its first child, if any. */
1076 struct die_info *sibling; /* Its next sibling, if any. */
1077 struct die_info *parent; /* Its parent, if any. */
c906108c 1078
b60c80d6
DJ
1079 /* An array of attributes, with NUM_ATTRS elements. There may be
1080 zero, but it's not common and zero-sized arrays are not
1081 sufficiently portable C. */
1082 struct attribute attrs[1];
c906108c
SS
1083 };
1084
c906108c
SS
1085/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1086 but this would require a corresponding change in unpack_field_as_long
1087 and friends. */
1088static int bits_per_byte = 8;
1089
2ddeaf8a
TT
1090/* When reading a variant or variant part, we track a bit more
1091 information about the field, and store it in an object of this
1092 type. */
1093
1094struct variant_field
1095{
1096 /* If we see a DW_TAG_variant, then this will be the discriminant
1097 value. */
1098 ULONGEST discriminant_value;
1099 /* If we see a DW_TAG_variant, then this will be set if this is the
1100 default branch. */
1101 bool default_branch;
1102 /* While reading a DW_TAG_variant_part, this will be set if this
1103 field is the discriminant. */
1104 bool is_discriminant;
1105};
1106
52059ffd
TT
1107struct nextfield
1108{
be2daae6
TT
1109 int accessibility = 0;
1110 int virtuality = 0;
2ddeaf8a 1111 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1112 struct variant_field variant {};
1113 struct field field {};
52059ffd
TT
1114};
1115
1116struct fnfieldlist
1117{
be2daae6
TT
1118 const char *name = nullptr;
1119 std::vector<struct fn_field> fnfields;
52059ffd
TT
1120};
1121
c906108c
SS
1122/* The routines that read and process dies for a C struct or C++ class
1123 pass lists of data member fields and lists of member function fields
1124 in an instance of a field_info structure, as defined below. */
1125struct field_info
c5aa993b 1126 {
0963b4bd 1127 /* List of data member and baseclasses fields. */
be2daae6
TT
1128 std::vector<struct nextfield> fields;
1129 std::vector<struct nextfield> baseclasses;
c906108c 1130
7d0ccb61 1131 /* Number of fields (including baseclasses). */
be2daae6 1132 int nfields = 0;
c906108c 1133
85102364 1134 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1135 int non_public_fields = 0;
c906108c 1136
c5aa993b
JM
1137 /* Member function fieldlist array, contains name of possibly overloaded
1138 member function, number of overloaded member functions and a pointer
1139 to the head of the member function field chain. */
be2daae6 1140 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1141
1142 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1143 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1144 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1145
1146 /* Nested types defined by this class and the number of elements in this
1147 list. */
be2daae6 1148 std::vector<struct decl_field> nested_types_list;
c5aa993b 1149 };
c906108c 1150
ae038cb0
DJ
1151/* Loaded secondary compilation units are kept in memory until they
1152 have not been referenced for the processing of this many
1153 compilation units. Set this to zero to disable caching. Cache
1154 sizes of up to at least twenty will improve startup time for
1155 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1156static int dwarf_max_cache_age = 5;
920d2a44 1157static void
b4f54984
DE
1158show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1159 struct cmd_list_element *c, const char *value)
920d2a44 1160{
3e43a32a 1161 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1162 "DWARF compilation units is %s.\n"),
920d2a44
AC
1163 value);
1164}
4390d890 1165\f
c906108c
SS
1166/* local function prototypes */
1167
918dd910
JK
1168static void dwarf2_find_base_address (struct die_info *die,
1169 struct dwarf2_cu *cu);
1170
891813be 1171static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1172 (struct dwarf2_per_cu_data *per_cu, const char *name);
1173
f1902523
JK
1174static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1175 const gdb_byte *info_ptr,
3e225074 1176 struct die_info *type_unit_die);
f1902523 1177
ed2dc618
SM
1178static void dwarf2_build_psymtabs_hard
1179 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1180
72bf9492
DJ
1181static void scan_partial_symbols (struct partial_die_info *,
1182 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1183 int, struct dwarf2_cu *);
c906108c 1184
72bf9492
DJ
1185static void add_partial_symbol (struct partial_die_info *,
1186 struct dwarf2_cu *);
63d06c5c 1187
72bf9492
DJ
1188static void add_partial_namespace (struct partial_die_info *pdi,
1189 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1190 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1191
5d7cb8df 1192static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1193 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1194 struct dwarf2_cu *cu);
1195
72bf9492
DJ
1196static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1197 struct dwarf2_cu *cu);
91c24f0a 1198
bc30ff58
JB
1199static void add_partial_subprogram (struct partial_die_info *pdi,
1200 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1201 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1202
d521ce57 1203static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1204
dee91e82 1205static struct partial_die_info *load_partial_dies
d521ce57 1206 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1207
fb816e8b
TV
1208/* A pair of partial_die_info and compilation unit. */
1209struct cu_partial_die_info
1210{
1211 /* The compilation unit of the partial_die_info. */
1212 struct dwarf2_cu *cu;
1213 /* A partial_die_info. */
1214 struct partial_die_info *pdi;
122cf0f2
AB
1215
1216 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1217 : cu (cu),
1218 pdi (pdi)
405feb71 1219 { /* Nothing. */ }
122cf0f2
AB
1220
1221private:
1222 cu_partial_die_info () = delete;
fb816e8b
TV
1223};
1224
122cf0f2
AB
1225static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1226 struct dwarf2_cu *);
72bf9492 1227
d521ce57
TT
1228static const gdb_byte *read_attribute (const struct die_reader_specs *,
1229 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1230 const gdb_byte *, bool *need_reprocess);
1231
1232static void read_attribute_reprocess (const struct die_reader_specs *reader,
1233 struct attribute *attr);
1234
1235static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1236
c764a876 1237static LONGEST read_checked_initial_length_and_offset
d521ce57 1238 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1239 unsigned int *, unsigned int *);
613e1657 1240
ed2dc618
SM
1241static sect_offset read_abbrev_offset
1242 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1243 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1244
ed2dc618
SM
1245static const char *read_indirect_string
1246 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1247 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1248
ed2dc618
SM
1249static const char *read_indirect_line_string
1250 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1251 const struct comp_unit_head *, unsigned int *);
36586728 1252
ed2dc618
SM
1253static const char *read_indirect_string_at_offset
1254 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1255 LONGEST str_offset);
927aa2e7 1256
ed2dc618
SM
1257static const char *read_indirect_string_from_dwz
1258 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1259
d521ce57
TT
1260static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1261 const gdb_byte *,
3019eac3
DE
1262 unsigned int *);
1263
18a8505e
AT
1264static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1265 ULONGEST str_index);
1266
1267static const char *read_stub_str_index (struct dwarf2_cu *cu,
1268 ULONGEST str_index);
3019eac3 1269
e142c38c 1270static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1271
e142c38c
DJ
1272static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1273 struct dwarf2_cu *);
c906108c 1274
348e048f 1275static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1276 unsigned int);
348e048f 1277
7d45c7c3
KB
1278static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1279 struct dwarf2_cu *cu);
1280
a084a2a6
AT
1281static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1282
05cf31d1
JB
1283static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1284 struct dwarf2_cu *cu);
1285
e142c38c 1286static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1287
e142c38c 1288static struct die_info *die_specification (struct die_info *die,
f2f0e013 1289 struct dwarf2_cu **);
63d06c5c 1290
9c541725 1291static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1292 struct dwarf2_cu *cu);
debd256d 1293
f3f5162e 1294static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1295 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1296 CORE_ADDR, int decode_mapping);
c906108c 1297
804d2729
TT
1298static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1299 const char *);
c906108c 1300
a14ed312 1301static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1302 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1303
ff39bb5e 1304static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1305 struct dwarf2_cu *);
c906108c 1306
ff39bb5e 1307static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1308 struct type *type,
1309 const char *name,
1310 struct obstack *obstack,
12df843f 1311 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1312 const gdb_byte **bytes,
98bfdba5 1313 struct dwarf2_locexpr_baton **baton);
2df3850c 1314
e7c27a73 1315static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1316
b4ba55a1
JB
1317static int need_gnat_info (struct dwarf2_cu *);
1318
3e43a32a
MS
1319static struct type *die_descriptive_type (struct die_info *,
1320 struct dwarf2_cu *);
b4ba55a1
JB
1321
1322static void set_descriptive_type (struct type *, struct die_info *,
1323 struct dwarf2_cu *);
1324
e7c27a73
DJ
1325static struct type *die_containing_type (struct die_info *,
1326 struct dwarf2_cu *);
c906108c 1327
ff39bb5e 1328static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1329 struct dwarf2_cu *);
c906108c 1330
f792889a 1331static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1332
673bfd45
DE
1333static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1334
0d5cff50 1335static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1336
6e70227d 1337static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1338 const char *suffix, int physname,
1339 struct dwarf2_cu *cu);
63d06c5c 1340
e7c27a73 1341static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1342
348e048f
DE
1343static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1344
e7c27a73 1345static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1346
e7c27a73 1347static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1348
96408a79
SA
1349static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1350
71a3c369
TT
1351static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1352
ff013f42 1353static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1354 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1355
3a2b436a 1356/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1357 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1358enum pc_bounds_kind
1359{
e385593e 1360 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1361 PC_BOUNDS_NOT_PRESENT,
1362
e385593e
JK
1363 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1364 were present but they do not form a valid range of PC addresses. */
1365 PC_BOUNDS_INVALID,
1366
3a2b436a
JK
1367 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1368 PC_BOUNDS_RANGES,
1369
1370 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1371 PC_BOUNDS_HIGH_LOW,
1372};
1373
1374static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1375 CORE_ADDR *, CORE_ADDR *,
1376 struct dwarf2_cu *,
891813be 1377 dwarf2_psymtab *);
c906108c 1378
fae299cd
DC
1379static void get_scope_pc_bounds (struct die_info *,
1380 CORE_ADDR *, CORE_ADDR *,
1381 struct dwarf2_cu *);
1382
801e3a5b
JB
1383static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1384 CORE_ADDR, struct dwarf2_cu *);
1385
a14ed312 1386static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1387 struct dwarf2_cu *);
c906108c 1388
a14ed312 1389static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1390 struct type *, struct dwarf2_cu *);
c906108c 1391
a14ed312 1392static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1393 struct die_info *, struct type *,
e7c27a73 1394 struct dwarf2_cu *);
c906108c 1395
a14ed312 1396static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1397 struct type *,
1398 struct dwarf2_cu *);
c906108c 1399
134d01f1 1400static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1401
e7c27a73 1402static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1403
e7c27a73 1404static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1405
5d7cb8df
JK
1406static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1407
804d2729 1408static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1409
27aa8d6a
SW
1410static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1411
74921315
KS
1412static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1413
f55ee35c
JK
1414static struct type *read_module_type (struct die_info *die,
1415 struct dwarf2_cu *cu);
1416
38d518c9 1417static const char *namespace_name (struct die_info *die,
e142c38c 1418 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1419
134d01f1 1420static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1421
e7c27a73 1422static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1423
6e70227d 1424static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1425 struct dwarf2_cu *);
1426
bf6af496 1427static struct die_info *read_die_and_siblings_1
d521ce57 1428 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1429 struct die_info *);
639d11d3 1430
dee91e82 1431static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1432 const gdb_byte *info_ptr,
1433 const gdb_byte **new_info_ptr,
639d11d3
DC
1434 struct die_info *parent);
1435
d521ce57
TT
1436static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1437 struct die_info **, const gdb_byte *,
3e225074 1438 int);
3019eac3 1439
d521ce57 1440static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1441 struct die_info **, const gdb_byte *);
93311388 1442
e7c27a73 1443static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1444
15d034d0
TT
1445static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1446 struct obstack *);
71c25dea 1447
15d034d0 1448static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1449
15d034d0 1450static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1451 struct die_info *die,
1452 struct dwarf2_cu *cu);
1453
ca69b9e6
DE
1454static const char *dwarf2_physname (const char *name, struct die_info *die,
1455 struct dwarf2_cu *cu);
1456
e142c38c 1457static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1458 struct dwarf2_cu **);
9219021c 1459
f39c6ffd 1460static const char *dwarf_tag_name (unsigned int);
c906108c 1461
f39c6ffd 1462static const char *dwarf_attr_name (unsigned int);
c906108c 1463
f39c6ffd 1464static const char *dwarf_form_name (unsigned int);
c906108c 1465
a121b7c1 1466static const char *dwarf_bool_name (unsigned int);
c906108c 1467
f39c6ffd 1468static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1469
f9aca02d 1470static struct die_info *sibling_die (struct die_info *);
c906108c 1471
d97bc12b
DE
1472static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1473
1474static void dump_die_for_error (struct die_info *);
1475
1476static void dump_die_1 (struct ui_file *, int level, int max_level,
1477 struct die_info *);
c906108c 1478
d97bc12b 1479/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1480
51545339 1481static void store_in_ref_table (struct die_info *,
10b3939b 1482 struct dwarf2_cu *);
c906108c 1483
ff39bb5e 1484static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1485
ff39bb5e 1486static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1487
348e048f 1488static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1489 const struct attribute *,
348e048f
DE
1490 struct dwarf2_cu **);
1491
10b3939b 1492static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1493 const struct attribute *,
f2f0e013 1494 struct dwarf2_cu **);
c906108c 1495
348e048f 1496static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1497 const struct attribute *,
348e048f
DE
1498 struct dwarf2_cu **);
1499
ac9ec31b
DE
1500static struct type *get_signatured_type (struct die_info *, ULONGEST,
1501 struct dwarf2_cu *);
1502
1503static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1504 const struct attribute *,
ac9ec31b
DE
1505 struct dwarf2_cu *);
1506
e5fe5e75 1507static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1508
52dc124a 1509static void read_signatured_type (struct signatured_type *);
348e048f 1510
63e43d3a
PMR
1511static int attr_to_dynamic_prop (const struct attribute *attr,
1512 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1513 struct dynamic_prop *prop, struct type *type);
63e43d3a 1514
c906108c
SS
1515/* memory allocation interface */
1516
7b5a2f43 1517static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1518
b60c80d6 1519static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1520
43f3e411 1521static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1522
8cf6f0b1
TT
1523static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1524 struct dwarf2_loclist_baton *baton,
ff39bb5e 1525 const struct attribute *attr);
8cf6f0b1 1526
ff39bb5e 1527static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1528 struct symbol *sym,
f1e6e072
TT
1529 struct dwarf2_cu *cu,
1530 int is_block);
4c2df51b 1531
d521ce57
TT
1532static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1533 const gdb_byte *info_ptr,
1534 struct abbrev_info *abbrev);
4bb7a0a7 1535
72bf9492
DJ
1536static hashval_t partial_die_hash (const void *item);
1537
1538static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1539
ae038cb0 1540static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1541 (sect_offset sect_off, unsigned int offset_in_dwz,
1542 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1543
9816fde3 1544static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1545 struct die_info *comp_unit_die,
1546 enum language pretend_language);
93311388 1547
ed2dc618 1548static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1549
dee91e82 1550static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1551
f792889a
DJ
1552static struct type *set_die_type (struct die_info *, struct type *,
1553 struct dwarf2_cu *);
1c379e20 1554
ed2dc618 1555static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1556
ed2dc618 1557static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1558
58f0c718 1559static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1560 enum language);
10b3939b 1561
95554aad
TT
1562static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1563 enum language);
10b3939b 1564
f4dc4d17
DE
1565static void process_full_type_unit (struct dwarf2_per_cu_data *,
1566 enum language);
1567
10b3939b
DJ
1568static void dwarf2_add_dependence (struct dwarf2_cu *,
1569 struct dwarf2_per_cu_data *);
1570
ae038cb0
DJ
1571static void dwarf2_mark (struct dwarf2_cu *);
1572
1573static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1574
b64f50a1 1575static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1576 struct dwarf2_per_cu_data *);
673bfd45 1577
f792889a 1578static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1579
95554aad
TT
1580static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1581 enum language pretend_language);
1582
ed2dc618 1583static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1584
b303c6f6
AB
1585/* Class, the destructor of which frees all allocated queue entries. This
1586 will only have work to do if an error was thrown while processing the
1587 dwarf. If no error was thrown then the queue entries should have all
1588 been processed, and freed, as we went along. */
1589
1590class dwarf2_queue_guard
1591{
1592public:
39856def
TT
1593 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1594 : m_per_objfile (per_objfile)
1595 {
1596 }
b303c6f6
AB
1597
1598 /* Free any entries remaining on the queue. There should only be
1599 entries left if we hit an error while processing the dwarf. */
1600 ~dwarf2_queue_guard ()
1601 {
39856def
TT
1602 /* Ensure that no memory is allocated by the queue. */
1603 std::queue<dwarf2_queue_item> empty;
1604 std::swap (m_per_objfile->queue, empty);
1605 }
b303c6f6 1606
39856def 1607 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1608
39856def
TT
1609private:
1610 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1611};
1612
39856def
TT
1613dwarf2_queue_item::~dwarf2_queue_item ()
1614{
1615 /* Anything still marked queued is likely to be in an
1616 inconsistent state, so discard it. */
1617 if (per_cu->queued)
1618 {
1619 if (per_cu->cu != NULL)
1620 free_one_cached_comp_unit (per_cu);
1621 per_cu->queued = 0;
1622 }
1623}
1624
d721ba37
PA
1625/* The return type of find_file_and_directory. Note, the enclosed
1626 string pointers are only valid while this object is valid. */
1627
1628struct file_and_directory
1629{
1630 /* The filename. This is never NULL. */
1631 const char *name;
1632
1633 /* The compilation directory. NULL if not known. If we needed to
1634 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1635 points directly to the DW_AT_comp_dir string attribute owned by
1636 the obstack that owns the DIE. */
1637 const char *comp_dir;
1638
1639 /* If we needed to build a new string for comp_dir, this is what
1640 owns the storage. */
1641 std::string comp_dir_storage;
1642};
1643
1644static file_and_directory find_file_and_directory (struct die_info *die,
1645 struct dwarf2_cu *cu);
9291a0cd 1646
298e9637 1647static htab_up allocate_signatured_type_table ();
1fd400ff 1648
298e9637 1649static htab_up allocate_dwo_unit_table ();
3019eac3 1650
57d63ce2 1651static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1652 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1653 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1654 ULONGEST signature, int is_debug_types);
a2ce51a0 1655
ed2dc618
SM
1656static struct dwp_file *get_dwp_file
1657 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1658
3019eac3 1659static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1660 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1661
1662static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1663 (struct signatured_type *, const char *, const char *);
3019eac3 1664
89e63ee4
DE
1665static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1666
263db9a1
TT
1667/* A unique pointer to a dwo_file. */
1668
51ac9db5 1669typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1670
ed2dc618 1671static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1672
1b80a9fa 1673static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1674
1675static void free_line_header_voidp (void *arg);
4390d890
DE
1676\f
1677/* Various complaints about symbol reading that don't abort the process. */
1678
1679static void
1680dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1681{
b98664d3 1682 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1683}
1684
1685static void
1686dwarf2_debug_line_missing_file_complaint (void)
1687{
b98664d3 1688 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1689}
1690
1691static void
1692dwarf2_debug_line_missing_end_sequence_complaint (void)
1693{
b98664d3 1694 complaint (_(".debug_line section has line "
4390d890
DE
1695 "program sequence without an end"));
1696}
1697
1698static void
1699dwarf2_complex_location_expr_complaint (void)
1700{
b98664d3 1701 complaint (_("location expression too complex"));
4390d890
DE
1702}
1703
1704static void
1705dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1706 int arg3)
1707{
b98664d3 1708 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1709 arg1, arg2, arg3);
1710}
1711
1712static void
1713dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1714{
b98664d3 1715 complaint (_("debug info runs off end of %s section"
4390d890 1716 " [in module %s]"),
96b79293
TT
1717 section->get_name (),
1718 section->get_file_name ());
4390d890 1719}
1b80a9fa 1720
4390d890
DE
1721static void
1722dwarf2_macro_malformed_definition_complaint (const char *arg1)
1723{
b98664d3 1724 complaint (_("macro debug info contains a "
4390d890
DE
1725 "malformed macro definition:\n`%s'"),
1726 arg1);
1727}
1728
1729static void
1730dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1731{
b98664d3 1732 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1733 arg1, arg2);
1734}
527f3840
JK
1735
1736/* Hash function for line_header_hash. */
1737
1738static hashval_t
1739line_header_hash (const struct line_header *ofs)
1740{
9c541725 1741 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1742}
1743
1744/* Hash function for htab_create_alloc_ex for line_header_hash. */
1745
1746static hashval_t
1747line_header_hash_voidp (const void *item)
1748{
9a3c8263 1749 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1750
1751 return line_header_hash (ofs);
1752}
1753
1754/* Equality function for line_header_hash. */
1755
1756static int
1757line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1758{
9a3c8263
SM
1759 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1760 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1761
9c541725 1762 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1763 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1764}
1765
4390d890 1766\f
9291a0cd 1767
330cdd98
PA
1768/* See declaration. */
1769
1770dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1771 const dwarf2_debug_sections *names,
1772 bool can_copy_)
1773 : objfile (objfile_),
1774 can_copy (can_copy_)
330cdd98
PA
1775{
1776 if (names == NULL)
1777 names = &dwarf2_elf_names;
1778
1779 bfd *obfd = objfile->obfd;
1780
1781 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1782 locate_sections (obfd, sec, *names);
1783}
1784
1785dwarf2_per_objfile::~dwarf2_per_objfile ()
1786{
1787 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1788 free_cached_comp_units ();
1789
b76e467d 1790 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1791 per_cu->imported_symtabs_free ();
fc8e7e75 1792
b2bdb8cf 1793 for (signatured_type *sig_type : all_type_units)
ae640021 1794 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1795
330cdd98
PA
1796 /* Everything else should be on the objfile obstack. */
1797}
1798
1799/* See declaration. */
1800
1801void
1802dwarf2_per_objfile::free_cached_comp_units ()
1803{
1804 dwarf2_per_cu_data *per_cu = read_in_chain;
1805 dwarf2_per_cu_data **last_chain = &read_in_chain;
1806 while (per_cu != NULL)
1807 {
1808 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1809
fcd3b13d 1810 delete per_cu->cu;
330cdd98
PA
1811 *last_chain = next_cu;
1812 per_cu = next_cu;
1813 }
1814}
1815
11ed8cad
TT
1816/* A helper class that calls free_cached_comp_units on
1817 destruction. */
1818
1819class free_cached_comp_units
1820{
1821public:
1822
1823 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1824 : m_per_objfile (per_objfile)
1825 {
1826 }
1827
1828 ~free_cached_comp_units ()
1829 {
1830 m_per_objfile->free_cached_comp_units ();
1831 }
1832
1833 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1834
1835private:
1836
1837 dwarf2_per_objfile *m_per_objfile;
1838};
1839
c906108c 1840/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1841 information and return true if we have enough to do something.
1842 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1843 ELF names are used. CAN_COPY is true for formats where symbol
1844 interposition is possible and so symbol values must follow copy
1845 relocation rules. */
c906108c
SS
1846
1847int
251d32d9 1848dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1849 const struct dwarf2_debug_sections *names,
1850 bool can_copy)
c906108c 1851{
97cbe998
SDJ
1852 if (objfile->flags & OBJF_READNEVER)
1853 return 0;
1854
ed2dc618
SM
1855 struct dwarf2_per_objfile *dwarf2_per_objfile
1856 = get_dwarf2_per_objfile (objfile);
1857
1858 if (dwarf2_per_objfile == NULL)
5bfd760d 1859 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1860 names,
1861 can_copy);
5bfd760d 1862
73869dc2 1863 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1864 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1865 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1866 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1867}
1868
251d32d9
TG
1869/* When loading sections, we look either for uncompressed section or for
1870 compressed section names. */
233a11ab
CS
1871
1872static int
251d32d9
TG
1873section_is_p (const char *section_name,
1874 const struct dwarf2_section_names *names)
233a11ab 1875{
251d32d9
TG
1876 if (names->normal != NULL
1877 && strcmp (section_name, names->normal) == 0)
1878 return 1;
1879 if (names->compressed != NULL
1880 && strcmp (section_name, names->compressed) == 0)
1881 return 1;
1882 return 0;
233a11ab
CS
1883}
1884
330cdd98 1885/* See declaration. */
c906108c 1886
330cdd98
PA
1887void
1888dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1889 const dwarf2_debug_sections &names)
c906108c 1890{
fd361982 1891 flagword aflag = bfd_section_flags (sectp);
251d32d9 1892
dc7650b8
JK
1893 if ((aflag & SEC_HAS_CONTENTS) == 0)
1894 {
1895 }
950b7495
KS
1896 else if (elf_section_data (sectp)->this_hdr.sh_size
1897 > bfd_get_file_size (abfd))
1898 {
1899 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1900 warning (_("Discarding section %s which has a section size (%s"
1901 ") larger than the file size [in module %s]"),
1902 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1903 bfd_get_filename (abfd));
1904 }
330cdd98 1905 else if (section_is_p (sectp->name, &names.info))
c906108c 1906 {
330cdd98 1907 this->info.s.section = sectp;
fd361982 1908 this->info.size = bfd_section_size (sectp);
c906108c 1909 }
330cdd98 1910 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1911 {
330cdd98 1912 this->abbrev.s.section = sectp;
fd361982 1913 this->abbrev.size = bfd_section_size (sectp);
c906108c 1914 }
330cdd98 1915 else if (section_is_p (sectp->name, &names.line))
c906108c 1916 {
330cdd98 1917 this->line.s.section = sectp;
fd361982 1918 this->line.size = bfd_section_size (sectp);
c906108c 1919 }
330cdd98 1920 else if (section_is_p (sectp->name, &names.loc))
c906108c 1921 {
330cdd98 1922 this->loc.s.section = sectp;
fd361982 1923 this->loc.size = bfd_section_size (sectp);
c906108c 1924 }
330cdd98 1925 else if (section_is_p (sectp->name, &names.loclists))
43988095 1926 {
330cdd98 1927 this->loclists.s.section = sectp;
fd361982 1928 this->loclists.size = bfd_section_size (sectp);
43988095 1929 }
330cdd98 1930 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1931 {
330cdd98 1932 this->macinfo.s.section = sectp;
fd361982 1933 this->macinfo.size = bfd_section_size (sectp);
c906108c 1934 }
330cdd98 1935 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1936 {
330cdd98 1937 this->macro.s.section = sectp;
fd361982 1938 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1939 }
330cdd98 1940 else if (section_is_p (sectp->name, &names.str))
c906108c 1941 {
330cdd98 1942 this->str.s.section = sectp;
fd361982 1943 this->str.size = bfd_section_size (sectp);
c906108c 1944 }
18a8505e
AT
1945 else if (section_is_p (sectp->name, &names.str_offsets))
1946 {
1947 this->str_offsets.s.section = sectp;
1948 this->str_offsets.size = bfd_section_size (sectp);
1949 }
330cdd98 1950 else if (section_is_p (sectp->name, &names.line_str))
43988095 1951 {
330cdd98 1952 this->line_str.s.section = sectp;
fd361982 1953 this->line_str.size = bfd_section_size (sectp);
43988095 1954 }
330cdd98 1955 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1956 {
330cdd98 1957 this->addr.s.section = sectp;
fd361982 1958 this->addr.size = bfd_section_size (sectp);
3019eac3 1959 }
330cdd98 1960 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1961 {
330cdd98 1962 this->frame.s.section = sectp;
fd361982 1963 this->frame.size = bfd_section_size (sectp);
b6af0555 1964 }
330cdd98 1965 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1966 {
330cdd98 1967 this->eh_frame.s.section = sectp;
fd361982 1968 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1969 }
330cdd98 1970 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1971 {
330cdd98 1972 this->ranges.s.section = sectp;
fd361982 1973 this->ranges.size = bfd_section_size (sectp);
af34e669 1974 }
330cdd98 1975 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1976 {
330cdd98 1977 this->rnglists.s.section = sectp;
fd361982 1978 this->rnglists.size = bfd_section_size (sectp);
43988095 1979 }
330cdd98 1980 else if (section_is_p (sectp->name, &names.types))
348e048f 1981 {
8b70b953
TT
1982 struct dwarf2_section_info type_section;
1983
1984 memset (&type_section, 0, sizeof (type_section));
049412e3 1985 type_section.s.section = sectp;
fd361982 1986 type_section.size = bfd_section_size (sectp);
8b70b953 1987
fd5866f6 1988 this->types.push_back (type_section);
348e048f 1989 }
330cdd98 1990 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1991 {
330cdd98 1992 this->gdb_index.s.section = sectp;
fd361982 1993 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1994 }
927aa2e7
JK
1995 else if (section_is_p (sectp->name, &names.debug_names))
1996 {
1997 this->debug_names.s.section = sectp;
fd361982 1998 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1999 }
2000 else if (section_is_p (sectp->name, &names.debug_aranges))
2001 {
2002 this->debug_aranges.s.section = sectp;
fd361982 2003 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2004 }
dce234bc 2005
fd361982
AM
2006 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2007 && bfd_section_vma (sectp) == 0)
330cdd98 2008 this->has_section_at_zero = true;
c906108c
SS
2009}
2010
dce234bc 2011/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2012 SECTION_NAME. */
af34e669 2013
dce234bc 2014void
3017a003
TG
2015dwarf2_get_section_info (struct objfile *objfile,
2016 enum dwarf2_section_enum sect,
d521ce57 2017 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2018 bfd_size_type *sizep)
2019{
5bfd760d 2020 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2021 struct dwarf2_section_info *info;
a3b2a86b
TT
2022
2023 /* We may see an objfile without any DWARF, in which case we just
2024 return nothing. */
2025 if (data == NULL)
2026 {
2027 *sectp = NULL;
2028 *bufp = NULL;
2029 *sizep = 0;
2030 return;
2031 }
3017a003
TG
2032 switch (sect)
2033 {
2034 case DWARF2_DEBUG_FRAME:
2035 info = &data->frame;
2036 break;
2037 case DWARF2_EH_FRAME:
2038 info = &data->eh_frame;
2039 break;
2040 default:
2041 gdb_assert_not_reached ("unexpected section");
2042 }
dce234bc 2043
96b79293 2044 info->read (objfile);
dce234bc 2045
96b79293 2046 *sectp = info->get_bfd_section ();
dce234bc
PP
2047 *bufp = info->buffer;
2048 *sizep = info->size;
2049}
2050
36586728
TT
2051/* A helper function to find the sections for a .dwz file. */
2052
2053static void
2054locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2055{
9a3c8263 2056 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2057
2058 /* Note that we only support the standard ELF names, because .dwz
2059 is ELF-only (at the time of writing). */
2060 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2061 {
049412e3 2062 dwz_file->abbrev.s.section = sectp;
fd361982 2063 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2064 }
2065 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2066 {
049412e3 2067 dwz_file->info.s.section = sectp;
fd361982 2068 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2071 {
049412e3 2072 dwz_file->str.s.section = sectp;
fd361982 2073 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2076 {
049412e3 2077 dwz_file->line.s.section = sectp;
fd361982 2078 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2081 {
049412e3 2082 dwz_file->macro.s.section = sectp;
fd361982 2083 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2084 }
2ec9a5e0
TT
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2086 {
049412e3 2087 dwz_file->gdb_index.s.section = sectp;
fd361982 2088 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2089 }
927aa2e7
JK
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2091 {
2092 dwz_file->debug_names.s.section = sectp;
fd361982 2093 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2094 }
36586728
TT
2095}
2096
c4973306 2097/* See dwarf2read.h. */
36586728 2098
c4973306 2099struct dwz_file *
ed2dc618 2100dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2101{
36586728 2102 const char *filename;
acd13123 2103 bfd_size_type buildid_len_arg;
dc294be5
TT
2104 size_t buildid_len;
2105 bfd_byte *buildid;
36586728
TT
2106
2107 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2108 return dwarf2_per_objfile->dwz_file.get ();
36586728 2109
4db1a1dc 2110 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2111 gdb::unique_xmalloc_ptr<char> data
2112 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2113 &buildid_len_arg, &buildid));
4db1a1dc
TT
2114 if (data == NULL)
2115 {
2116 if (bfd_get_error () == bfd_error_no_error)
2117 return NULL;
2118 error (_("could not read '.gnu_debugaltlink' section: %s"),
2119 bfd_errmsg (bfd_get_error ()));
2120 }
791afaa2
TT
2121
2122 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2123
acd13123
TT
2124 buildid_len = (size_t) buildid_len_arg;
2125
791afaa2 2126 filename = data.get ();
d721ba37
PA
2127
2128 std::string abs_storage;
36586728
TT
2129 if (!IS_ABSOLUTE_PATH (filename))
2130 {
14278e1f
TT
2131 gdb::unique_xmalloc_ptr<char> abs
2132 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2133
14278e1f 2134 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2135 filename = abs_storage.c_str ();
36586728
TT
2136 }
2137
dc294be5
TT
2138 /* First try the file name given in the section. If that doesn't
2139 work, try to use the build-id instead. */
192b62ce 2140 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2141 if (dwz_bfd != NULL)
36586728 2142 {
192b62ce 2143 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2144 dwz_bfd.reset (nullptr);
36586728
TT
2145 }
2146
dc294be5
TT
2147 if (dwz_bfd == NULL)
2148 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2149
2150 if (dwz_bfd == NULL)
2151 error (_("could not find '.gnu_debugaltlink' file for %s"),
2152 objfile_name (dwarf2_per_objfile->objfile));
2153
7ff8cb8c
TT
2154 std::unique_ptr<struct dwz_file> result
2155 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2156
7ff8cb8c
TT
2157 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2158 result.get ());
36586728 2159
7ff8cb8c
TT
2160 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2161 result->dwz_bfd.get ());
2162 dwarf2_per_objfile->dwz_file = std::move (result);
2163 return dwarf2_per_objfile->dwz_file.get ();
36586728 2164}
9291a0cd 2165\f
7b9f3c50
DE
2166/* DWARF quick_symbols_functions support. */
2167
2168/* TUs can share .debug_line entries, and there can be a lot more TUs than
2169 unique line tables, so we maintain a separate table of all .debug_line
2170 derived entries to support the sharing.
2171 All the quick functions need is the list of file names. We discard the
2172 line_header when we're done and don't need to record it here. */
2173struct quick_file_names
2174{
094b34ac
DE
2175 /* The data used to construct the hash key. */
2176 struct stmt_list_hash hash;
7b9f3c50
DE
2177
2178 /* The number of entries in file_names, real_names. */
2179 unsigned int num_file_names;
2180
2181 /* The file names from the line table, after being run through
2182 file_full_name. */
2183 const char **file_names;
2184
2185 /* The file names from the line table after being run through
2186 gdb_realpath. These are computed lazily. */
2187 const char **real_names;
2188};
2189
2190/* When using the index (and thus not using psymtabs), each CU has an
2191 object of this type. This is used to hold information needed by
2192 the various "quick" methods. */
2193struct dwarf2_per_cu_quick_data
2194{
2195 /* The file table. This can be NULL if there was no file table
2196 or it's currently not read in.
2197 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2198 struct quick_file_names *file_names;
2199
2200 /* The corresponding symbol table. This is NULL if symbols for this
2201 CU have not yet been read. */
43f3e411 2202 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2203
2204 /* A temporary mark bit used when iterating over all CUs in
2205 expand_symtabs_matching. */
2206 unsigned int mark : 1;
2207
2208 /* True if we've tried to read the file table and found there isn't one.
2209 There will be no point in trying to read it again next time. */
2210 unsigned int no_file_data : 1;
2211};
2212
094b34ac
DE
2213/* Utility hash function for a stmt_list_hash. */
2214
2215static hashval_t
2216hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2217{
2218 hashval_t v = 0;
2219
2220 if (stmt_list_hash->dwo_unit != NULL)
2221 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2222 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2223 return v;
2224}
2225
2226/* Utility equality function for a stmt_list_hash. */
2227
2228static int
2229eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2230 const struct stmt_list_hash *rhs)
2231{
2232 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2233 return 0;
2234 if (lhs->dwo_unit != NULL
2235 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2236 return 0;
2237
9c541725 2238 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2239}
2240
7b9f3c50
DE
2241/* Hash function for a quick_file_names. */
2242
2243static hashval_t
2244hash_file_name_entry (const void *e)
2245{
9a3c8263
SM
2246 const struct quick_file_names *file_data
2247 = (const struct quick_file_names *) e;
7b9f3c50 2248
094b34ac 2249 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2250}
2251
2252/* Equality function for a quick_file_names. */
2253
2254static int
2255eq_file_name_entry (const void *a, const void *b)
2256{
9a3c8263
SM
2257 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2258 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2259
094b34ac 2260 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2261}
2262
2263/* Delete function for a quick_file_names. */
2264
2265static void
2266delete_file_name_entry (void *e)
2267{
9a3c8263 2268 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2269 int i;
2270
2271 for (i = 0; i < file_data->num_file_names; ++i)
2272 {
2273 xfree ((void*) file_data->file_names[i]);
2274 if (file_data->real_names)
2275 xfree ((void*) file_data->real_names[i]);
2276 }
2277
2278 /* The space for the struct itself lives on objfile_obstack,
2279 so we don't free it here. */
2280}
2281
2282/* Create a quick_file_names hash table. */
2283
5895093f 2284static htab_up
7b9f3c50
DE
2285create_quick_file_names_table (unsigned int nr_initial_entries)
2286{
5895093f
TT
2287 return htab_up (htab_create_alloc (nr_initial_entries,
2288 hash_file_name_entry, eq_file_name_entry,
2289 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2290}
9291a0cd 2291
918dd910
JK
2292/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2293 have to be created afterwards. You should call age_cached_comp_units after
2294 processing PER_CU->CU. dw2_setup must have been already called. */
2295
2296static void
58f0c718 2297load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2298{
3019eac3 2299 if (per_cu->is_debug_types)
e5fe5e75 2300 load_full_type_unit (per_cu);
918dd910 2301 else
58f0c718 2302 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2303
cc12ce38
DE
2304 if (per_cu->cu == NULL)
2305 return; /* Dummy CU. */
2dc860c0
DE
2306
2307 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2308}
2309
a0f42c21 2310/* Read in the symbols for PER_CU. */
2fdf6df6 2311
9291a0cd 2312static void
58f0c718 2313dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2314{
ed2dc618 2315 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2316
f4dc4d17
DE
2317 /* Skip type_unit_groups, reading the type units they contain
2318 is handled elsewhere. */
197400e8 2319 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2320 return;
2321
b303c6f6
AB
2322 /* The destructor of dwarf2_queue_guard frees any entries left on
2323 the queue. After this point we're guaranteed to leave this function
2324 with the dwarf queue empty. */
39856def 2325 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2326
95554aad 2327 if (dwarf2_per_objfile->using_index
43f3e411 2328 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2329 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2330 {
2331 queue_comp_unit (per_cu, language_minimal);
58f0c718 2332 load_cu (per_cu, skip_partial);
89e63ee4
DE
2333
2334 /* If we just loaded a CU from a DWO, and we're working with an index
2335 that may badly handle TUs, load all the TUs in that DWO as well.
2336 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2337 if (!per_cu->is_debug_types
cc12ce38 2338 && per_cu->cu != NULL
89e63ee4
DE
2339 && per_cu->cu->dwo_unit != NULL
2340 && dwarf2_per_objfile->index_table != NULL
2341 && dwarf2_per_objfile->index_table->version <= 7
2342 /* DWP files aren't supported yet. */
ed2dc618 2343 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2344 queue_and_load_all_dwo_tus (per_cu);
95554aad 2345 }
9291a0cd 2346
ed2dc618 2347 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2348
2349 /* Age the cache, releasing compilation units that have not
2350 been used recently. */
ed2dc618 2351 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2352}
2353
2354/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2355 the objfile from which this CU came. Returns the resulting symbol
2356 table. */
2fdf6df6 2357
43f3e411 2358static struct compunit_symtab *
58f0c718 2359dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2360{
ed2dc618
SM
2361 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2362
95554aad 2363 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2364 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2365 {
11ed8cad 2366 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2367 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2368 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2369 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2370 }
f194fefb 2371
43f3e411 2372 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2373}
2374
ff4c9fec 2375/* See declaration. */
f4dc4d17 2376
ff4c9fec
SM
2377dwarf2_per_cu_data *
2378dwarf2_per_objfile::get_cutu (int index)
2379{
b76e467d 2380 if (index >= this->all_comp_units.size ())
ff4c9fec 2381 {
b76e467d 2382 index -= this->all_comp_units.size ();
b2bdb8cf 2383 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2384 return &this->all_type_units[index]->per_cu;
2385 }
f4dc4d17 2386
ff4c9fec
SM
2387 return this->all_comp_units[index];
2388}
f4dc4d17 2389
ff4c9fec 2390/* See declaration. */
2fdf6df6 2391
ff4c9fec
SM
2392dwarf2_per_cu_data *
2393dwarf2_per_objfile::get_cu (int index)
1fd400ff 2394{
b76e467d 2395 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2396
ff4c9fec 2397 return this->all_comp_units[index];
f4dc4d17
DE
2398}
2399
ff4c9fec 2400/* See declaration. */
f4dc4d17 2401
ff4c9fec
SM
2402signatured_type *
2403dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2404{
b2bdb8cf 2405 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2406
ff4c9fec 2407 return this->all_type_units[index];
1fd400ff
TT
2408}
2409
4b514bc8
JK
2410/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2411 objfile_obstack, and constructed with the specified field
2412 values. */
2413
2414static dwarf2_per_cu_data *
ed2dc618 2415create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2416 struct dwarf2_section_info *section,
2417 int is_dwz,
2418 sect_offset sect_off, ULONGEST length)
2419{
ed2dc618 2420 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2421 dwarf2_per_cu_data *the_cu
2422 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2423 struct dwarf2_per_cu_data);
2424 the_cu->sect_off = sect_off;
2425 the_cu->length = length;
e3b94546 2426 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2427 the_cu->section = section;
2428 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2429 struct dwarf2_per_cu_quick_data);
2430 the_cu->is_dwz = is_dwz;
2431 return the_cu;
2432}
2433
2ec9a5e0
TT
2434/* A helper for create_cus_from_index that handles a given list of
2435 CUs. */
2fdf6df6 2436
74a0d9f6 2437static void
12359b5e 2438create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2439 const gdb_byte *cu_list, offset_type n_elements,
2440 struct dwarf2_section_info *section,
b76e467d 2441 int is_dwz)
9291a0cd 2442{
12359b5e 2443 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2444 {
74a0d9f6 2445 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2446
2447 sect_offset sect_off
2448 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2449 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2450 cu_list += 2 * 8;
2451
b76e467d 2452 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2453 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2454 sect_off, length);
b76e467d 2455 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2456 }
9291a0cd
TT
2457}
2458
2ec9a5e0 2459/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2460 the CU objects for this objfile. */
2ec9a5e0 2461
74a0d9f6 2462static void
12359b5e 2463create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2464 const gdb_byte *cu_list, offset_type cu_list_elements,
2465 const gdb_byte *dwz_list, offset_type dwz_elements)
2466{
b76e467d
SM
2467 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2468 dwarf2_per_objfile->all_comp_units.reserve
2469 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2470
12359b5e 2471 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2472 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2473
2474 if (dwz_elements == 0)
74a0d9f6 2475 return;
2ec9a5e0 2476
12359b5e
SM
2477 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2478 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2479 &dwz->info, 1);
2ec9a5e0
TT
2480}
2481
1fd400ff 2482/* Create the signatured type hash table from the index. */
673bfd45 2483
74a0d9f6 2484static void
12359b5e
SM
2485create_signatured_type_table_from_index
2486 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2487 struct dwarf2_section_info *section,
2488 const gdb_byte *bytes,
2489 offset_type elements)
1fd400ff 2490{
12359b5e 2491 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2492
b2bdb8cf
SM
2493 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2494 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2495
298e9637 2496 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2497
12359b5e 2498 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2499 {
52dc124a 2500 struct signatured_type *sig_type;
9c541725 2501 ULONGEST signature;
1fd400ff 2502 void **slot;
9c541725 2503 cu_offset type_offset_in_tu;
1fd400ff 2504
74a0d9f6 2505 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2506 sect_offset sect_off
2507 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2508 type_offset_in_tu
2509 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2510 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2511 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2512 bytes += 3 * 8;
2513
52dc124a 2514 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2515 struct signatured_type);
52dc124a 2516 sig_type->signature = signature;
9c541725 2517 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2518 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2519 sig_type->per_cu.section = section;
9c541725 2520 sig_type->per_cu.sect_off = sect_off;
e3b94546 2521 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2522 sig_type->per_cu.v.quick
1fd400ff
TT
2523 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2524 struct dwarf2_per_cu_quick_data);
2525
b0b6a987 2526 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2527 *slot = sig_type;
1fd400ff 2528
b2bdb8cf 2529 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2530 }
2531
b0b6a987 2532 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2533}
2534
927aa2e7
JK
2535/* Create the signatured type hash table from .debug_names. */
2536
2537static void
2538create_signatured_type_table_from_debug_names
ed2dc618 2539 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2540 const mapped_debug_names &map,
2541 struct dwarf2_section_info *section,
2542 struct dwarf2_section_info *abbrev_section)
2543{
ed2dc618
SM
2544 struct objfile *objfile = dwarf2_per_objfile->objfile;
2545
96b79293
TT
2546 section->read (objfile);
2547 abbrev_section->read (objfile);
927aa2e7 2548
b2bdb8cf
SM
2549 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2550 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2551
298e9637 2552 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2553
2554 for (uint32_t i = 0; i < map.tu_count; ++i)
2555 {
2556 struct signatured_type *sig_type;
927aa2e7 2557 void **slot;
927aa2e7
JK
2558
2559 sect_offset sect_off
2560 = (sect_offset) (extract_unsigned_integer
2561 (map.tu_table_reordered + i * map.offset_size,
2562 map.offset_size,
2563 map.dwarf5_byte_order));
2564
2565 comp_unit_head cu_header;
ed2dc618
SM
2566 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2567 abbrev_section,
927aa2e7
JK
2568 section->buffer + to_underlying (sect_off),
2569 rcuh_kind::TYPE);
2570
2571 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2572 struct signatured_type);
2573 sig_type->signature = cu_header.signature;
2574 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2575 sig_type->per_cu.is_debug_types = 1;
2576 sig_type->per_cu.section = section;
2577 sig_type->per_cu.sect_off = sect_off;
e3b94546 2578 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2579 sig_type->per_cu.v.quick
2580 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2581 struct dwarf2_per_cu_quick_data);
2582
b0b6a987 2583 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2584 *slot = sig_type;
2585
b2bdb8cf 2586 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2587 }
2588
b0b6a987 2589 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2590}
2591
9291a0cd
TT
2592/* Read the address map data from the mapped index, and use it to
2593 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2594
9291a0cd 2595static void
ed2dc618
SM
2596create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2597 struct mapped_index *index)
9291a0cd 2598{
ed2dc618 2599 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2600 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2601 const gdb_byte *iter, *end;
9291a0cd 2602 struct addrmap *mutable_map;
9291a0cd
TT
2603 CORE_ADDR baseaddr;
2604
8268c778
PA
2605 auto_obstack temp_obstack;
2606
9291a0cd
TT
2607 mutable_map = addrmap_create_mutable (&temp_obstack);
2608
f00a2de2
PA
2609 iter = index->address_table.data ();
2610 end = iter + index->address_table.size ();
9291a0cd 2611
b3b3bada 2612 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2613
2614 while (iter < end)
2615 {
2616 ULONGEST hi, lo, cu_index;
2617 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2618 iter += 8;
2619 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2620 iter += 8;
2621 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2622 iter += 4;
f652bce2 2623
24a55014 2624 if (lo > hi)
f652bce2 2625 {
b98664d3 2626 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2627 hex_string (lo), hex_string (hi));
24a55014 2628 continue;
f652bce2 2629 }
24a55014 2630
b76e467d 2631 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2632 {
b98664d3 2633 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2634 (unsigned) cu_index);
24a55014 2635 continue;
f652bce2 2636 }
24a55014 2637
79748972
TT
2638 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2639 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2640 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2641 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2642 }
2643
d320c2b5 2644 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2645 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2646}
2647
927aa2e7
JK
2648/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2649 populate the objfile's psymtabs_addrmap. */
2650
2651static void
ed2dc618 2652create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2653 struct dwarf2_section_info *section)
2654{
ed2dc618 2655 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2656 bfd *abfd = objfile->obfd;
2657 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2658 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2659
2660 auto_obstack temp_obstack;
2661 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2662
2663 std::unordered_map<sect_offset,
2664 dwarf2_per_cu_data *,
2665 gdb::hash_enum<sect_offset>>
2666 debug_info_offset_to_per_cu;
b76e467d 2667 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2668 {
927aa2e7
JK
2669 const auto insertpair
2670 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2671 if (!insertpair.second)
2672 {
2673 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2674 "debug_info_offset %s, ignoring .debug_aranges."),
2675 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2676 return;
2677 }
2678 }
2679
96b79293 2680 section->read (objfile);
927aa2e7
JK
2681
2682 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2683
2684 const gdb_byte *addr = section->buffer;
2685
2686 while (addr < section->buffer + section->size)
2687 {
2688 const gdb_byte *const entry_addr = addr;
2689 unsigned int bytes_read;
2690
2691 const LONGEST entry_length = read_initial_length (abfd, addr,
2692 &bytes_read);
2693 addr += bytes_read;
2694
2695 const gdb_byte *const entry_end = addr + entry_length;
2696 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2697 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2698 if (addr + entry_length > section->buffer + section->size)
2699 {
47e3f474 2700 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2701 "length %s exceeds section length %s, "
2702 "ignoring .debug_aranges."),
47e3f474
TV
2703 objfile_name (objfile),
2704 plongest (entry_addr - section->buffer),
927aa2e7
JK
2705 plongest (bytes_read + entry_length),
2706 pulongest (section->size));
2707 return;
2708 }
2709
2710 /* The version number. */
2711 const uint16_t version = read_2_bytes (abfd, addr);
2712 addr += 2;
2713 if (version != 2)
2714 {
47e3f474 2715 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2716 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2717 objfile_name (objfile),
2718 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2719 return;
2720 }
2721
2722 const uint64_t debug_info_offset
2723 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2724 addr += offset_size;
2725 const auto per_cu_it
2726 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2727 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2728 {
47e3f474 2729 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2730 "debug_info_offset %s does not exists, "
2731 "ignoring .debug_aranges."),
47e3f474
TV
2732 objfile_name (objfile),
2733 plongest (entry_addr - section->buffer),
927aa2e7
JK
2734 pulongest (debug_info_offset));
2735 return;
2736 }
2737 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2738
2739 const uint8_t address_size = *addr++;
2740 if (address_size < 1 || address_size > 8)
2741 {
47e3f474 2742 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2743 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2744 objfile_name (objfile),
2745 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2746 return;
2747 }
2748
2749 const uint8_t segment_selector_size = *addr++;
2750 if (segment_selector_size != 0)
2751 {
47e3f474 2752 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2753 "segment_selector_size %u is not supported, "
2754 "ignoring .debug_aranges."),
47e3f474
TV
2755 objfile_name (objfile),
2756 plongest (entry_addr - section->buffer),
927aa2e7
JK
2757 segment_selector_size);
2758 return;
2759 }
2760
2761 /* Must pad to an alignment boundary that is twice the address
2762 size. It is undocumented by the DWARF standard but GCC does
2763 use it. */
2764 for (size_t padding = ((-(addr - section->buffer))
2765 & (2 * address_size - 1));
2766 padding > 0; padding--)
2767 if (*addr++ != 0)
2768 {
47e3f474 2769 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2770 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2771 objfile_name (objfile),
2772 plongest (entry_addr - section->buffer));
927aa2e7
JK
2773 return;
2774 }
2775
2776 for (;;)
2777 {
2778 if (addr + 2 * address_size > entry_end)
2779 {
47e3f474 2780 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2781 "address list is not properly terminated, "
2782 "ignoring .debug_aranges."),
47e3f474
TV
2783 objfile_name (objfile),
2784 plongest (entry_addr - section->buffer));
927aa2e7
JK
2785 return;
2786 }
2787 ULONGEST start = extract_unsigned_integer (addr, address_size,
2788 dwarf5_byte_order);
2789 addr += address_size;
2790 ULONGEST length = extract_unsigned_integer (addr, address_size,
2791 dwarf5_byte_order);
2792 addr += address_size;
2793 if (start == 0 && length == 0)
2794 break;
2795 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2796 {
2797 /* Symbol was eliminated due to a COMDAT group. */
2798 continue;
2799 }
2800 ULONGEST end = start + length;
79748972
TT
2801 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2802 - baseaddr);
2803 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2804 - baseaddr);
927aa2e7
JK
2805 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2806 }
2807 }
2808
d320c2b5 2809 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2810 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2811}
2812
9291a0cd
TT
2813/* Find a slot in the mapped index INDEX for the object named NAME.
2814 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2815 constant pool and return true. If NAME cannot be found, return
2816 false. */
2fdf6df6 2817
109483d9 2818static bool
9291a0cd
TT
2819find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2820 offset_type **vec_out)
2821{
0cf03b49 2822 offset_type hash;
9291a0cd 2823 offset_type slot, step;
559a7a62 2824 int (*cmp) (const char *, const char *);
9291a0cd 2825
791afaa2 2826 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2827 if (current_language->la_language == language_cplus
45280282
IB
2828 || current_language->la_language == language_fortran
2829 || current_language->la_language == language_d)
0cf03b49
JK
2830 {
2831 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2832 not contain any. */
a8719064 2833
72998fb3 2834 if (strchr (name, '(') != NULL)
0cf03b49 2835 {
109483d9 2836 without_params = cp_remove_params (name);
0cf03b49 2837
72998fb3 2838 if (without_params != NULL)
791afaa2 2839 name = without_params.get ();
0cf03b49
JK
2840 }
2841 }
2842
559a7a62 2843 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2844 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2845 simulate our NAME being searched is also lowercased. */
2846 hash = mapped_index_string_hash ((index->version == 4
2847 && case_sensitivity == case_sensitive_off
2848 ? 5 : index->version),
2849 name);
2850
f00a2de2
PA
2851 slot = hash & (index->symbol_table.size () - 1);
2852 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2853 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2854
2855 for (;;)
2856 {
9291a0cd 2857 const char *str;
f00a2de2
PA
2858
2859 const auto &bucket = index->symbol_table[slot];
2860 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2861 return false;
9291a0cd 2862
f00a2de2 2863 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2864 if (!cmp (name, str))
9291a0cd
TT
2865 {
2866 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2867 + MAYBE_SWAP (bucket.vec));
109483d9 2868 return true;
9291a0cd
TT
2869 }
2870
f00a2de2 2871 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2872 }
2873}
2874
4485a1c1
SM
2875/* A helper function that reads the .gdb_index from BUFFER and fills
2876 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2877 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2878 ok to use deprecated sections.
2879
2880 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2881 out parameters that are filled in with information about the CU and
2882 TU lists in the section.
2883
4485a1c1 2884 Returns true if all went well, false otherwise. */
2fdf6df6 2885
d33bc52e 2886static bool
4485a1c1
SM
2887read_gdb_index_from_buffer (struct objfile *objfile,
2888 const char *filename,
2889 bool deprecated_ok,
2890 gdb::array_view<const gdb_byte> buffer,
2891 struct mapped_index *map,
2892 const gdb_byte **cu_list,
2893 offset_type *cu_list_elements,
2894 const gdb_byte **types_list,
2895 offset_type *types_list_elements)
2896{
2897 const gdb_byte *addr = &buffer[0];
82430852 2898
9291a0cd 2899 /* Version check. */
4485a1c1 2900 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2901 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2902 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2903 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2904 indices. */
831adc1f 2905 if (version < 4)
481860b3
GB
2906 {
2907 static int warning_printed = 0;
2908 if (!warning_printed)
2909 {
2910 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2911 filename);
481860b3
GB
2912 warning_printed = 1;
2913 }
2914 return 0;
2915 }
2916 /* Index version 4 uses a different hash function than index version
2917 5 and later.
2918
2919 Versions earlier than 6 did not emit psymbols for inlined
2920 functions. Using these files will cause GDB not to be able to
2921 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2922 indices unless the user has done
2923 "set use-deprecated-index-sections on". */
2ec9a5e0 2924 if (version < 6 && !deprecated_ok)
481860b3
GB
2925 {
2926 static int warning_printed = 0;
2927 if (!warning_printed)
2928 {
e615022a
DE
2929 warning (_("\
2930Skipping deprecated .gdb_index section in %s.\n\
2931Do \"set use-deprecated-index-sections on\" before the file is read\n\
2932to use the section anyway."),
2ec9a5e0 2933 filename);
481860b3
GB
2934 warning_printed = 1;
2935 }
2936 return 0;
2937 }
796a7ff8 2938 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2939 of the TU (for symbols coming from TUs),
2940 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2941 Plus gold-generated indices can have duplicate entries for global symbols,
2942 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2943 These are just performance bugs, and we can't distinguish gdb-generated
2944 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2945
481860b3 2946 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2947 longer backward compatible. */
796a7ff8 2948 if (version > 8)
594e8718 2949 return 0;
9291a0cd 2950
559a7a62 2951 map->version = version;
9291a0cd 2952
4485a1c1 2953 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2954
4485a1c1 2955 int i = 0;
2ec9a5e0
TT
2956 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2957 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2958 / 8);
1fd400ff
TT
2959 ++i;
2960
2ec9a5e0
TT
2961 *types_list = addr + MAYBE_SWAP (metadata[i]);
2962 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2963 - MAYBE_SWAP (metadata[i]))
2964 / 8);
987d643c 2965 ++i;
1fd400ff 2966
f00a2de2
PA
2967 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2968 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2969 map->address_table
2970 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2971 ++i;
2972
f00a2de2
PA
2973 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2974 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2975 map->symbol_table
2976 = gdb::array_view<mapped_index::symbol_table_slot>
2977 ((mapped_index::symbol_table_slot *) symbol_table,
2978 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2979
f00a2de2 2980 ++i;
f9d83a0b 2981 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2982
2ec9a5e0
TT
2983 return 1;
2984}
2985
4485a1c1
SM
2986/* Callback types for dwarf2_read_gdb_index. */
2987
2988typedef gdb::function_view
2989 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2990 get_gdb_index_contents_ftype;
2991typedef gdb::function_view
2992 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2993 get_gdb_index_contents_dwz_ftype;
2994
927aa2e7 2995/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2996 elements of all the CUs and return 1. Otherwise, return 0. */
2997
2998static int
4485a1c1
SM
2999dwarf2_read_gdb_index
3000 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3001 get_gdb_index_contents_ftype get_gdb_index_contents,
3002 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3003{
2ec9a5e0
TT
3004 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3005 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3006 struct dwz_file *dwz;
12359b5e 3007 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3008
4485a1c1
SM
3009 gdb::array_view<const gdb_byte> main_index_contents
3010 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3011
3012 if (main_index_contents.empty ())
3013 return 0;
3014
3063847f 3015 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3016 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3017 use_deprecated_index_sections,
3018 main_index_contents, map.get (), &cu_list,
3019 &cu_list_elements, &types_list,
3020 &types_list_elements))
2ec9a5e0
TT
3021 return 0;
3022
0fefef59 3023 /* Don't use the index if it's empty. */
3063847f 3024 if (map->symbol_table.empty ())
0fefef59
DE
3025 return 0;
3026
2ec9a5e0
TT
3027 /* If there is a .dwz file, read it so we can get its CU list as
3028 well. */
ed2dc618 3029 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3030 if (dwz != NULL)
2ec9a5e0 3031 {
2ec9a5e0
TT
3032 struct mapped_index dwz_map;
3033 const gdb_byte *dwz_types_ignore;
3034 offset_type dwz_types_elements_ignore;
3035
4485a1c1
SM
3036 gdb::array_view<const gdb_byte> dwz_index_content
3037 = get_gdb_index_contents_dwz (objfile, dwz);
3038
3039 if (dwz_index_content.empty ())
3040 return 0;
3041
3042 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3043 bfd_get_filename (dwz->dwz_bfd.get ()),
3044 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3045 &dwz_list, &dwz_list_elements,
3046 &dwz_types_ignore,
3047 &dwz_types_elements_ignore))
2ec9a5e0
TT
3048 {
3049 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3050 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3051 return 0;
3052 }
3053 }
3054
12359b5e
SM
3055 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3056 dwz_list, dwz_list_elements);
1fd400ff 3057
8b70b953
TT
3058 if (types_list_elements)
3059 {
8b70b953
TT
3060 /* We can only handle a single .debug_types when we have an
3061 index. */
fd5866f6 3062 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3063 return 0;
3064
fd5866f6 3065 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3066
12359b5e
SM
3067 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3068 types_list, types_list_elements);
8b70b953 3069 }
9291a0cd 3070
3063847f 3071 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3072
3063847f 3073 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3074 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3075 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3076 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3077
3078 return 1;
3079}
3080
dee91e82 3081/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3082
dee91e82
DE
3083static void
3084dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3085 const gdb_byte *info_ptr,
3e225074 3086 struct die_info *comp_unit_die)
9291a0cd 3087{
dee91e82 3088 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3089 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3090 struct dwarf2_per_objfile *dwarf2_per_objfile
3091 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3092 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3093 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3094 struct attribute *attr;
7b9f3c50
DE
3095 void **slot;
3096 struct quick_file_names *qfn;
9291a0cd 3097
0186c6a7
DE
3098 gdb_assert (! this_cu->is_debug_types);
3099
07261596
TT
3100 /* Our callers never want to match partial units -- instead they
3101 will match the enclosing full CU. */
3102 if (comp_unit_die->tag == DW_TAG_partial_unit)
3103 {
3104 this_cu->v.quick->no_file_data = 1;
3105 return;
3106 }
3107
0186c6a7 3108 lh_cu = this_cu;
7b9f3c50 3109 slot = NULL;
dee91e82 3110
fff8551c 3111 line_header_up lh;
9c541725 3112 sect_offset line_offset {};
fff8551c 3113
dee91e82 3114 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3115 if (attr != nullptr)
9291a0cd 3116 {
7b9f3c50
DE
3117 struct quick_file_names find_entry;
3118
9c541725 3119 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3120
3121 /* We may have already read in this line header (TU line header sharing).
3122 If we have we're done. */
094b34ac 3123 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3124 find_entry.hash.line_sect_off = line_offset;
5895093f 3125 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3126 &find_entry, INSERT);
3127 if (*slot != NULL)
3128 {
9a3c8263 3129 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3130 return;
7b9f3c50
DE
3131 }
3132
3019eac3 3133 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3134 }
3135 if (lh == NULL)
3136 {
094b34ac 3137 lh_cu->v.quick->no_file_data = 1;
dee91e82 3138 return;
9291a0cd
TT
3139 }
3140
8d749320 3141 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3142 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3143 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3144 gdb_assert (slot != NULL);
3145 *slot = qfn;
9291a0cd 3146
d721ba37 3147 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3148
aa391654
TT
3149 int offset = 0;
3150 if (strcmp (fnd.name, "<unknown>") != 0)
3151 ++offset;
3152
7ba99d21 3153 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3154 qfn->file_names =
aa391654
TT
3155 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3156 if (offset != 0)
3157 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3158 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3159 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3160 fnd.comp_dir).release ();
7b9f3c50 3161 qfn->real_names = NULL;
9291a0cd 3162
094b34ac 3163 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3164}
3165
3166/* A helper for the "quick" functions which attempts to read the line
3167 table for THIS_CU. */
3168
3169static struct quick_file_names *
e4a48d9d 3170dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3171{
0186c6a7
DE
3172 /* This should never be called for TUs. */
3173 gdb_assert (! this_cu->is_debug_types);
3174 /* Nor type unit groups. */
197400e8 3175 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3176
dee91e82
DE
3177 if (this_cu->v.quick->file_names != NULL)
3178 return this_cu->v.quick->file_names;
3179 /* If we know there is no line data, no point in looking again. */
3180 if (this_cu->v.quick->no_file_data)
3181 return NULL;
3182
c0ab21c2
TT
3183 cutu_reader reader (this_cu);
3184 if (!reader.dummy_p)
3e225074 3185 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3186
3187 if (this_cu->v.quick->no_file_data)
3188 return NULL;
3189 return this_cu->v.quick->file_names;
9291a0cd
TT
3190}
3191
3192/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3193 real path for a given file name from the line table. */
2fdf6df6 3194
9291a0cd 3195static const char *
7b9f3c50
DE
3196dw2_get_real_path (struct objfile *objfile,
3197 struct quick_file_names *qfn, int index)
9291a0cd 3198{
7b9f3c50
DE
3199 if (qfn->real_names == NULL)
3200 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3201 qfn->num_file_names, const char *);
9291a0cd 3202
7b9f3c50 3203 if (qfn->real_names[index] == NULL)
14278e1f 3204 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3205
7b9f3c50 3206 return qfn->real_names[index];
9291a0cd
TT
3207}
3208
3209static struct symtab *
3210dw2_find_last_source_symtab (struct objfile *objfile)
3211{
ed2dc618
SM
3212 struct dwarf2_per_objfile *dwarf2_per_objfile
3213 = get_dwarf2_per_objfile (objfile);
b76e467d 3214 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3215 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3216
43f3e411
DE
3217 if (cust == NULL)
3218 return NULL;
ed2dc618 3219
43f3e411 3220 return compunit_primary_filetab (cust);
9291a0cd
TT
3221}
3222
7b9f3c50
DE
3223/* Traversal function for dw2_forget_cached_source_info. */
3224
3225static int
3226dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3227{
7b9f3c50 3228 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3229
7b9f3c50 3230 if (file_data->real_names)
9291a0cd 3231 {
7b9f3c50 3232 int i;
9291a0cd 3233
7b9f3c50 3234 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3235 {
7b9f3c50
DE
3236 xfree ((void*) file_data->real_names[i]);
3237 file_data->real_names[i] = NULL;
9291a0cd
TT
3238 }
3239 }
7b9f3c50
DE
3240
3241 return 1;
3242}
3243
3244static void
3245dw2_forget_cached_source_info (struct objfile *objfile)
3246{
ed2dc618
SM
3247 struct dwarf2_per_objfile *dwarf2_per_objfile
3248 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3249
5895093f 3250 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3251 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3252}
3253
f8eba3c6
TT
3254/* Helper function for dw2_map_symtabs_matching_filename that expands
3255 the symtabs and calls the iterator. */
3256
3257static int
3258dw2_map_expand_apply (struct objfile *objfile,
3259 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3260 const char *name, const char *real_path,
14bc53a8 3261 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3262{
43f3e411 3263 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3264
3265 /* Don't visit already-expanded CUs. */
43f3e411 3266 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3267 return 0;
3268
3269 /* This may expand more than one symtab, and we want to iterate over
3270 all of them. */
58f0c718 3271 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3272
14bc53a8
PA
3273 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3274 last_made, callback);
f8eba3c6
TT
3275}
3276
3277/* Implementation of the map_symtabs_matching_filename method. */
3278
14bc53a8
PA
3279static bool
3280dw2_map_symtabs_matching_filename
3281 (struct objfile *objfile, const char *name, const char *real_path,
3282 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3283{
c011a4f4 3284 const char *name_basename = lbasename (name);
ed2dc618
SM
3285 struct dwarf2_per_objfile *dwarf2_per_objfile
3286 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3287
848e3e78
DE
3288 /* The rule is CUs specify all the files, including those used by
3289 any TU, so there's no need to scan TUs here. */
f4dc4d17 3290
b76e467d 3291 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3292 {
3d7bb9d9 3293 /* We only need to look at symtabs not already expanded. */
43f3e411 3294 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3295 continue;
3296
b76e467d 3297 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3298 if (file_data == NULL)
9291a0cd
TT
3299 continue;
3300
b76e467d 3301 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3302 {
7b9f3c50 3303 const char *this_name = file_data->file_names[j];
da235a7c 3304 const char *this_real_name;
9291a0cd 3305
af529f8f 3306 if (compare_filenames_for_search (this_name, name))
9291a0cd 3307 {
f5b95b50 3308 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3309 callback))
3310 return true;
288e77a7 3311 continue;
4aac40c8 3312 }
9291a0cd 3313
c011a4f4
DE
3314 /* Before we invoke realpath, which can get expensive when many
3315 files are involved, do a quick comparison of the basenames. */
3316 if (! basenames_may_differ
3317 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3318 continue;
3319
da235a7c
JK
3320 this_real_name = dw2_get_real_path (objfile, file_data, j);
3321 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3322 {
da235a7c 3323 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3324 callback))
3325 return true;
288e77a7 3326 continue;
da235a7c 3327 }
9291a0cd 3328
da235a7c
JK
3329 if (real_path != NULL)
3330 {
af529f8f
JK
3331 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3332 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3333 if (this_real_name != NULL
af529f8f 3334 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3335 {
f5b95b50 3336 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3337 callback))
3338 return true;
288e77a7 3339 continue;
9291a0cd
TT
3340 }
3341 }
3342 }
3343 }
3344
14bc53a8 3345 return false;
9291a0cd
TT
3346}
3347
da51c347
DE
3348/* Struct used to manage iterating over all CUs looking for a symbol. */
3349
3350struct dw2_symtab_iterator
9291a0cd 3351{
ed2dc618
SM
3352 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3353 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3354 /* If set, only look for symbols that match that block. Valid values are
3355 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3356 gdb::optional<block_enum> block_index;
da51c347
DE
3357 /* The kind of symbol we're looking for. */
3358 domain_enum domain;
3359 /* The list of CUs from the index entry of the symbol,
3360 or NULL if not found. */
3361 offset_type *vec;
3362 /* The next element in VEC to look at. */
3363 int next;
3364 /* The number of elements in VEC, or zero if there is no match. */
3365 int length;
8943b874
DE
3366 /* Have we seen a global version of the symbol?
3367 If so we can ignore all further global instances.
3368 This is to work around gold/15646, inefficient gold-generated
3369 indices. */
3370 int global_seen;
da51c347 3371};
9291a0cd 3372
2b79f376 3373/* Initialize the index symtab iterator ITER. */
2fdf6df6 3374
9291a0cd 3375static void
da51c347 3376dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3377 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3378 gdb::optional<block_enum> block_index,
da51c347
DE
3379 domain_enum domain,
3380 const char *name)
3381{
ed2dc618 3382 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3383 iter->block_index = block_index;
3384 iter->domain = domain;
3385 iter->next = 0;
8943b874 3386 iter->global_seen = 0;
da51c347 3387
3063847f 3388 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3389
3390 /* index is NULL if OBJF_READNOW. */
3391 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3392 iter->length = MAYBE_SWAP (*iter->vec);
3393 else
3394 {
3395 iter->vec = NULL;
3396 iter->length = 0;
3397 }
3398}
3399
3400/* Return the next matching CU or NULL if there are no more. */
3401
3402static struct dwarf2_per_cu_data *
3403dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3404{
ed2dc618
SM
3405 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3406
da51c347
DE
3407 for ( ; iter->next < iter->length; ++iter->next)
3408 {
3409 offset_type cu_index_and_attrs =
3410 MAYBE_SWAP (iter->vec[iter->next + 1]);
3411 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3412 gdb_index_symbol_kind symbol_kind =
3413 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3414 /* Only check the symbol attributes if they're present.
3415 Indices prior to version 7 don't record them,
3416 and indices >= 7 may elide them for certain symbols
3417 (gold does this). */
3418 int attrs_valid =
ed2dc618 3419 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3420 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3421
3190f0c6 3422 /* Don't crash on bad data. */
b76e467d 3423 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3424 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3425 {
b98664d3 3426 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3427 " [in module %s]"),
3428 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3429 continue;
3430 }
3431
ff4c9fec 3432 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3433
da51c347 3434 /* Skip if already read in. */
43f3e411 3435 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3436 continue;
3437
8943b874
DE
3438 /* Check static vs global. */
3439 if (attrs_valid)
3440 {
2b79f376
SM
3441 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3442
3443 if (iter->block_index.has_value ())
3444 {
3445 bool want_static = *iter->block_index == STATIC_BLOCK;
3446
3447 if (is_static != want_static)
3448 continue;
3449 }
3450
8943b874
DE
3451 /* Work around gold/15646. */
3452 if (!is_static && iter->global_seen)
3453 continue;
3454 if (!is_static)
3455 iter->global_seen = 1;
3456 }
da51c347
DE
3457
3458 /* Only check the symbol's kind if it has one. */
3459 if (attrs_valid)
3460 {
3461 switch (iter->domain)
3462 {
3463 case VAR_DOMAIN:
3464 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3465 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3466 /* Some types are also in VAR_DOMAIN. */
3467 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3468 continue;
3469 break;
3470 case STRUCT_DOMAIN:
3471 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3472 continue;
3473 break;
3474 case LABEL_DOMAIN:
3475 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3476 continue;
3477 break;
59c35742
AB
3478 case MODULE_DOMAIN:
3479 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3480 continue;
3481 break;
da51c347
DE
3482 default:
3483 break;
3484 }
3485 }
3486
3487 ++iter->next;
3488 return per_cu;
3489 }
3490
3491 return NULL;
3492}
3493
43f3e411 3494static struct compunit_symtab *
c7f839cb 3495dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3496 const char *name, domain_enum domain)
9291a0cd 3497{
43f3e411 3498 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3499 struct dwarf2_per_objfile *dwarf2_per_objfile
3500 = get_dwarf2_per_objfile (objfile);
9291a0cd 3501
b5ec771e
PA
3502 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3503
ed2dc618
SM
3504 struct dw2_symtab_iterator iter;
3505 struct dwarf2_per_cu_data *per_cu;
da51c347 3506
2b79f376 3507 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3508
ed2dc618
SM
3509 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3510 {
3511 struct symbol *sym, *with_opaque = NULL;
58f0c718 3512 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3513 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3514 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3515
ed2dc618
SM
3516 sym = block_find_symbol (block, name, domain,
3517 block_find_non_opaque_type_preferred,
3518 &with_opaque);
b2e2f908 3519
ed2dc618
SM
3520 /* Some caution must be observed with overloaded functions
3521 and methods, since the index will not contain any overload
3522 information (but NAME might contain it). */
da51c347 3523
ed2dc618
SM
3524 if (sym != NULL
3525 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3526 return stab;
3527 if (with_opaque != NULL
3528 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3529 stab_best = stab;
da51c347 3530
ed2dc618 3531 /* Keep looking through other CUs. */
9291a0cd 3532 }
9291a0cd 3533
da51c347 3534 return stab_best;
9291a0cd
TT
3535}
3536
3537static void
3538dw2_print_stats (struct objfile *objfile)
3539{
ed2dc618
SM
3540 struct dwarf2_per_objfile *dwarf2_per_objfile
3541 = get_dwarf2_per_objfile (objfile);
b76e467d 3542 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3543 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3544 int count = 0;
9291a0cd 3545
ed2dc618 3546 for (int i = 0; i < total; ++i)
9291a0cd 3547 {
ff4c9fec 3548 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3549
43f3e411 3550 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3551 ++count;
3552 }
e4a48d9d 3553 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3554 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3555}
3556
779bd270
DE
3557/* This dumps minimal information about the index.
3558 It is called via "mt print objfiles".
3559 One use is to verify .gdb_index has been loaded by the
3560 gdb.dwarf2/gdb-index.exp testcase. */
3561
9291a0cd
TT
3562static void
3563dw2_dump (struct objfile *objfile)
3564{
ed2dc618
SM
3565 struct dwarf2_per_objfile *dwarf2_per_objfile
3566 = get_dwarf2_per_objfile (objfile);
3567
779bd270
DE
3568 gdb_assert (dwarf2_per_objfile->using_index);
3569 printf_filtered (".gdb_index:");
3570 if (dwarf2_per_objfile->index_table != NULL)
3571 {
3572 printf_filtered (" version %d\n",
3573 dwarf2_per_objfile->index_table->version);
3574 }
3575 else
3576 printf_filtered (" faked for \"readnow\"\n");
3577 printf_filtered ("\n");
9291a0cd
TT
3578}
3579
9291a0cd
TT
3580static void
3581dw2_expand_symtabs_for_function (struct objfile *objfile,
3582 const char *func_name)
3583{
ed2dc618
SM
3584 struct dwarf2_per_objfile *dwarf2_per_objfile
3585 = get_dwarf2_per_objfile (objfile);
da51c347 3586
ed2dc618
SM
3587 struct dw2_symtab_iterator iter;
3588 struct dwarf2_per_cu_data *per_cu;
da51c347 3589
2b79f376 3590 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3591
ed2dc618 3592 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3593 dw2_instantiate_symtab (per_cu, false);
da51c347 3594
9291a0cd
TT
3595}
3596
3597static void
3598dw2_expand_all_symtabs (struct objfile *objfile)
3599{
ed2dc618
SM
3600 struct dwarf2_per_objfile *dwarf2_per_objfile
3601 = get_dwarf2_per_objfile (objfile);
b76e467d 3602 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3603 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3604
ed2dc618 3605 for (int i = 0; i < total_units; ++i)
9291a0cd 3606 {
ff4c9fec 3607 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3608
58f0c718
TT
3609 /* We don't want to directly expand a partial CU, because if we
3610 read it with the wrong language, then assertion failures can
3611 be triggered later on. See PR symtab/23010. So, tell
3612 dw2_instantiate_symtab to skip partial CUs -- any important
3613 partial CU will be read via DW_TAG_imported_unit anyway. */
3614 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3615 }
3616}
3617
3618static void
652a8996
JK
3619dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3620 const char *fullname)
9291a0cd 3621{
ed2dc618
SM
3622 struct dwarf2_per_objfile *dwarf2_per_objfile
3623 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3624
3625 /* We don't need to consider type units here.
3626 This is only called for examining code, e.g. expand_line_sal.
3627 There can be an order of magnitude (or more) more type units
3628 than comp units, and we avoid them if we can. */
3629
b76e467d 3630 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3631 {
3d7bb9d9 3632 /* We only need to look at symtabs not already expanded. */
43f3e411 3633 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3634 continue;
3635
b76e467d 3636 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3637 if (file_data == NULL)
9291a0cd
TT
3638 continue;
3639
b76e467d 3640 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3641 {
652a8996
JK
3642 const char *this_fullname = file_data->file_names[j];
3643
3644 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3645 {
58f0c718 3646 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3647 break;
3648 }
3649 }
3650 }
3651}
3652
9291a0cd 3653static void
199b4314
TT
3654dw2_map_matching_symbols
3655 (struct objfile *objfile,
b054970d 3656 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3657 int global,
3658 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3659 symbol_compare_ftype *ordered_compare)
9291a0cd 3660{
40658b94 3661 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3662 current language is Ada for a non-Ada objfile using GNU index. As Ada
3663 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3664}
3665
e1ef7d7a
PA
3666/* Starting from a search name, return the string that finds the upper
3667 bound of all strings that start with SEARCH_NAME in a sorted name
3668 list. Returns the empty string to indicate that the upper bound is
3669 the end of the list. */
3670
3671static std::string
3672make_sort_after_prefix_name (const char *search_name)
3673{
3674 /* When looking to complete "func", we find the upper bound of all
3675 symbols that start with "func" by looking for where we'd insert
3676 the closest string that would follow "func" in lexicographical
3677 order. Usually, that's "func"-with-last-character-incremented,
3678 i.e. "fund". Mind non-ASCII characters, though. Usually those
3679 will be UTF-8 multi-byte sequences, but we can't be certain.
3680 Especially mind the 0xff character, which is a valid character in
3681 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3682 rule out compilers allowing it in identifiers. Note that
3683 conveniently, strcmp/strcasecmp are specified to compare
3684 characters interpreted as unsigned char. So what we do is treat
3685 the whole string as a base 256 number composed of a sequence of
3686 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3687 to 0, and carries 1 to the following more-significant position.
3688 If the very first character in SEARCH_NAME ends up incremented
3689 and carries/overflows, then the upper bound is the end of the
3690 list. The string after the empty string is also the empty
3691 string.
3692
3693 Some examples of this operation:
3694
3695 SEARCH_NAME => "+1" RESULT
3696
3697 "abc" => "abd"
3698 "ab\xff" => "ac"
3699 "\xff" "a" "\xff" => "\xff" "b"
3700 "\xff" => ""
3701 "\xff\xff" => ""
3702 "" => ""
3703
3704 Then, with these symbols for example:
3705
3706 func
3707 func1
3708 fund
3709
3710 completing "func" looks for symbols between "func" and
3711 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3712 which finds "func" and "func1", but not "fund".
3713
3714 And with:
3715
3716 funcÿ (Latin1 'ÿ' [0xff])
3717 funcÿ1
3718 fund
3719
3720 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3721 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3722
3723 And with:
3724
3725 ÿÿ (Latin1 'ÿ' [0xff])
3726 ÿÿ1
3727
3728 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3729 the end of the list.
3730 */
3731 std::string after = search_name;
3732 while (!after.empty () && (unsigned char) after.back () == 0xff)
3733 after.pop_back ();
3734 if (!after.empty ())
3735 after.back () = (unsigned char) after.back () + 1;
3736 return after;
3737}
3738
5c58de74 3739/* See declaration. */
61d96d7e 3740
5c58de74
PA
3741std::pair<std::vector<name_component>::const_iterator,
3742 std::vector<name_component>::const_iterator>
44ed8f3e 3743mapped_index_base::find_name_components_bounds
3b00ef10 3744 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3745{
5c58de74
PA
3746 auto *name_cmp
3747 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3748
3b00ef10
TT
3749 const char *lang_name
3750 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3751
3f563c84
PA
3752 /* Comparison function object for lower_bound that matches against a
3753 given symbol name. */
3754 auto lookup_compare_lower = [&] (const name_component &elem,
3755 const char *name)
3756 {
5c58de74 3757 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3758 const char *elem_name = elem_qualified + elem.name_offset;
3759 return name_cmp (elem_name, name) < 0;
3760 };
3761
3762 /* Comparison function object for upper_bound that matches against a
3763 given symbol name. */
3764 auto lookup_compare_upper = [&] (const char *name,
3765 const name_component &elem)
3766 {
5c58de74 3767 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3768 const char *elem_name = elem_qualified + elem.name_offset;
3769 return name_cmp (name, elem_name) < 0;
3770 };
3771
5c58de74
PA
3772 auto begin = this->name_components.begin ();
3773 auto end = this->name_components.end ();
3f563c84
PA
3774
3775 /* Find the lower bound. */
3776 auto lower = [&] ()
3777 {
3b00ef10 3778 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3779 return begin;
3780 else
3b00ef10 3781 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3782 } ();
3783
3784 /* Find the upper bound. */
3785 auto upper = [&] ()
3786 {
5c58de74 3787 if (lookup_name_without_params.completion_mode ())
3f563c84 3788 {
e1ef7d7a
PA
3789 /* In completion mode, we want UPPER to point past all
3790 symbols names that have the same prefix. I.e., with
3791 these symbols, and completing "func":
3792
3793 function << lower bound
3794 function1
3795 other_function << upper bound
3796
3797 We find the upper bound by looking for the insertion
3798 point of "func"-with-last-character-incremented,
3799 i.e. "fund". */
3b00ef10 3800 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3801 if (after.empty ())
3f563c84 3802 return end;
e6b2f5ef
PA
3803 return std::lower_bound (lower, end, after.c_str (),
3804 lookup_compare_lower);
3f563c84
PA
3805 }
3806 else
3b00ef10 3807 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3808 } ();
3809
5c58de74
PA
3810 return {lower, upper};
3811}
3812
3813/* See declaration. */
3814
3815void
44ed8f3e 3816mapped_index_base::build_name_components ()
5c58de74
PA
3817{
3818 if (!this->name_components.empty ())
3819 return;
3820
3821 this->name_components_casing = case_sensitivity;
3822 auto *name_cmp
3823 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3824
3825 /* The code below only knows how to break apart components of C++
3826 symbol names (and other languages that use '::' as
3b00ef10 3827 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3828 auto count = this->symbol_name_count ();
3829 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3830 {
44ed8f3e 3831 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3832 continue;
3833
3834 const char *name = this->symbol_name_at (idx);
3835
3836 /* Add each name component to the name component table. */
3837 unsigned int previous_len = 0;
3b00ef10
TT
3838
3839 if (strstr (name, "::") != nullptr)
3840 {
3841 for (unsigned int current_len = cp_find_first_component (name);
3842 name[current_len] != '\0';
3843 current_len += cp_find_first_component (name + current_len))
3844 {
3845 gdb_assert (name[current_len] == ':');
3846 this->name_components.push_back ({previous_len, idx});
3847 /* Skip the '::'. */
3848 current_len += 2;
3849 previous_len = current_len;
3850 }
3851 }
3852 else
5c58de74 3853 {
3b00ef10
TT
3854 /* Handle the Ada encoded (aka mangled) form here. */
3855 for (const char *iter = strstr (name, "__");
3856 iter != nullptr;
3857 iter = strstr (iter, "__"))
3858 {
3859 this->name_components.push_back ({previous_len, idx});
3860 iter += 2;
3861 previous_len = iter - name;
3862 }
5c58de74 3863 }
3b00ef10 3864
5c58de74
PA
3865 this->name_components.push_back ({previous_len, idx});
3866 }
3867
3868 /* Sort name_components elements by name. */
3869 auto name_comp_compare = [&] (const name_component &left,
3870 const name_component &right)
3871 {
3872 const char *left_qualified = this->symbol_name_at (left.idx);
3873 const char *right_qualified = this->symbol_name_at (right.idx);
3874
3875 const char *left_name = left_qualified + left.name_offset;
3876 const char *right_name = right_qualified + right.name_offset;
3877
3878 return name_cmp (left_name, right_name) < 0;
3879 };
3880
3881 std::sort (this->name_components.begin (),
3882 this->name_components.end (),
3883 name_comp_compare);
3884}
3885
3886/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3887 mapped_index_base instead of the containing objfile. This is split
3888 to a separate function in order to be able to unit test the
3889 name_components matching using a mock mapped_index_base. For each
5c58de74 3890 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3891 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3892
3893static void
3894dw2_expand_symtabs_matching_symbol
44ed8f3e 3895 (mapped_index_base &index,
5c58de74
PA
3896 const lookup_name_info &lookup_name_in,
3897 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3898 enum search_domain kind,
3b00ef10 3899 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3900{
3901 lookup_name_info lookup_name_without_params
3902 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3903
3904 /* Build the symbol name component sorted vector, if we haven't
3905 yet. */
3906 index.build_name_components ();
3907
3f563c84
PA
3908 /* The same symbol may appear more than once in the range though.
3909 E.g., if we're looking for symbols that complete "w", and we have
3910 a symbol named "w1::w2", we'll find the two name components for
3911 that same symbol in the range. To be sure we only call the
3912 callback once per symbol, we first collect the symbol name
3913 indexes that matched in a temporary vector and ignore
3914 duplicates. */
3915 std::vector<offset_type> matches;
3f563c84 3916
3b00ef10
TT
3917 struct name_and_matcher
3918 {
3919 symbol_name_matcher_ftype *matcher;
3920 const std::string &name;
3921
3922 bool operator== (const name_and_matcher &other) const
3f563c84 3923 {
3b00ef10
TT
3924 return matcher == other.matcher && name == other.name;
3925 }
3926 };
3927
3928 /* A vector holding all the different symbol name matchers, for all
3929 languages. */
3930 std::vector<name_and_matcher> matchers;
3931
3932 for (int i = 0; i < nr_languages; i++)
3933 {
3934 enum language lang_e = (enum language) i;
3935
3936 const language_defn *lang = language_def (lang_e);
3937 symbol_name_matcher_ftype *name_matcher
3938 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3939
3b00ef10
TT
3940 name_and_matcher key {
3941 name_matcher,
3942 lookup_name_without_params.language_lookup_name (lang_e)
3943 };
3944
3945 /* Don't insert the same comparison routine more than once.
3946 Note that we do this linear walk. This is not a problem in
3947 practice because the number of supported languages is
3948 low. */
3949 if (std::find (matchers.begin (), matchers.end (), key)
3950 != matchers.end ())
9291a0cd 3951 continue;
3b00ef10
TT
3952 matchers.push_back (std::move (key));
3953
3954 auto bounds
3955 = index.find_name_components_bounds (lookup_name_without_params,
3956 lang_e);
3957
3958 /* Now for each symbol name in range, check to see if we have a name
3959 match, and if so, call the MATCH_CALLBACK callback. */
3960
3961 for (; bounds.first != bounds.second; ++bounds.first)
3962 {
3963 const char *qualified = index.symbol_name_at (bounds.first->idx);
3964
3965 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3966 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3967 continue;
9291a0cd 3968
3b00ef10
TT
3969 matches.push_back (bounds.first->idx);
3970 }
3f563c84
PA
3971 }
3972
3973 std::sort (matches.begin (), matches.end ());
3974
3975 /* Finally call the callback, once per match. */
3976 ULONGEST prev = -1;
3977 for (offset_type idx : matches)
3978 {
3979 if (prev != idx)
3980 {
3b00ef10
TT
3981 if (!match_callback (idx))
3982 break;
3f563c84
PA
3983 prev = idx;
3984 }
3985 }
3986
3987 /* Above we use a type wider than idx's for 'prev', since 0 and
3988 (offset_type)-1 are both possible values. */
3989 static_assert (sizeof (prev) > sizeof (offset_type), "");
3990}
3991
c62446b1
PA
3992#if GDB_SELF_TEST
3993
3994namespace selftests { namespace dw2_expand_symtabs_matching {
3995
a3c5fafd
PA
3996/* A mock .gdb_index/.debug_names-like name index table, enough to
3997 exercise dw2_expand_symtabs_matching_symbol, which works with the
3998 mapped_index_base interface. Builds an index from the symbol list
3999 passed as parameter to the constructor. */
4000class mock_mapped_index : public mapped_index_base
c62446b1
PA
4001{
4002public:
a3c5fafd
PA
4003 mock_mapped_index (gdb::array_view<const char *> symbols)
4004 : m_symbol_table (symbols)
c62446b1
PA
4005 {}
4006
a3c5fafd 4007 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4008
a3c5fafd 4009 /* Return the number of names in the symbol table. */
632e107b 4010 size_t symbol_name_count () const override
c62446b1 4011 {
a3c5fafd 4012 return m_symbol_table.size ();
c62446b1
PA
4013 }
4014
a3c5fafd 4015 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4016 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4017 {
4018 return m_symbol_table[idx];
4019 }
c62446b1 4020
a3c5fafd
PA
4021private:
4022 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4023};
4024
4025/* Convenience function that converts a NULL pointer to a "<null>"
4026 string, to pass to print routines. */
4027
4028static const char *
4029string_or_null (const char *str)
4030{
4031 return str != NULL ? str : "<null>";
4032}
4033
4034/* Check if a lookup_name_info built from
4035 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4036 index. EXPECTED_LIST is the list of expected matches, in expected
4037 matching order. If no match expected, then an empty list is
4038 specified. Returns true on success. On failure prints a warning
4039 indicating the file:line that failed, and returns false. */
4040
4041static bool
4042check_match (const char *file, int line,
4043 mock_mapped_index &mock_index,
4044 const char *name, symbol_name_match_type match_type,
4045 bool completion_mode,
4046 std::initializer_list<const char *> expected_list)
4047{
4048 lookup_name_info lookup_name (name, match_type, completion_mode);
4049
4050 bool matched = true;
4051
4052 auto mismatch = [&] (const char *expected_str,
4053 const char *got)
4054 {
4055 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4056 "expected=\"%s\", got=\"%s\"\n"),
4057 file, line,
4058 (match_type == symbol_name_match_type::FULL
4059 ? "FULL" : "WILD"),
4060 name, string_or_null (expected_str), string_or_null (got));
4061 matched = false;
4062 };
4063
4064 auto expected_it = expected_list.begin ();
4065 auto expected_end = expected_list.end ();
4066
a3c5fafd 4067 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4068 NULL, ALL_DOMAIN,
4069 [&] (offset_type idx)
4070 {
a3c5fafd 4071 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4072 const char *expected_str
4073 = expected_it == expected_end ? NULL : *expected_it++;
4074
4075 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4076 mismatch (expected_str, matched_name);
3b00ef10 4077 return true;
c62446b1
PA
4078 });
4079
4080 const char *expected_str
4081 = expected_it == expected_end ? NULL : *expected_it++;
4082 if (expected_str != NULL)
4083 mismatch (expected_str, NULL);
4084
4085 return matched;
4086}
4087
4088/* The symbols added to the mock mapped_index for testing (in
4089 canonical form). */
4090static const char *test_symbols[] = {
4091 "function",
4092 "std::bar",
4093 "std::zfunction",
4094 "std::zfunction2",
4095 "w1::w2",
4096 "ns::foo<char*>",
4097 "ns::foo<int>",
4098 "ns::foo<long>",
a20714ff
PA
4099 "ns2::tmpl<int>::foo2",
4100 "(anonymous namespace)::A::B::C",
c62446b1 4101
e1ef7d7a
PA
4102 /* These are used to check that the increment-last-char in the
4103 matching algorithm for completion doesn't match "t1_fund" when
4104 completing "t1_func". */
4105 "t1_func",
4106 "t1_func1",
4107 "t1_fund",
4108 "t1_fund1",
4109
4110 /* A UTF-8 name with multi-byte sequences to make sure that
4111 cp-name-parser understands this as a single identifier ("função"
4112 is "function" in PT). */
4113 u8"u8função",
4114
4115 /* \377 (0xff) is Latin1 'ÿ'. */
4116 "yfunc\377",
4117
4118 /* \377 (0xff) is Latin1 'ÿ'. */
4119 "\377",
4120 "\377\377123",
4121
c62446b1
PA
4122 /* A name with all sorts of complications. Starts with "z" to make
4123 it easier for the completion tests below. */
4124#define Z_SYM_NAME \
4125 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4126 "::tuple<(anonymous namespace)::ui*, " \
4127 "std::default_delete<(anonymous namespace)::ui>, void>"
4128
4129 Z_SYM_NAME
4130};
4131
a3c5fafd
PA
4132/* Returns true if the mapped_index_base::find_name_component_bounds
4133 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4134 in completion mode. */
5c58de74
PA
4135
4136static bool
a3c5fafd 4137check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4138 const char *search_name,
4139 gdb::array_view<const char *> expected_syms)
4140{
4141 lookup_name_info lookup_name (search_name,
4142 symbol_name_match_type::FULL, true);
4143
3b00ef10
TT
4144 auto bounds = index.find_name_components_bounds (lookup_name,
4145 language_cplus);
5c58de74
PA
4146
4147 size_t distance = std::distance (bounds.first, bounds.second);
4148 if (distance != expected_syms.size ())
4149 return false;
4150
4151 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4152 {
4153 auto nc_elem = bounds.first + exp_elem;
4154 const char *qualified = index.symbol_name_at (nc_elem->idx);
4155 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4156 return false;
4157 }
4158
4159 return true;
4160}
4161
4162/* Test the lower-level mapped_index::find_name_component_bounds
4163 method. */
4164
c62446b1 4165static void
5c58de74
PA
4166test_mapped_index_find_name_component_bounds ()
4167{
4168 mock_mapped_index mock_index (test_symbols);
4169
a3c5fafd 4170 mock_index.build_name_components ();
5c58de74
PA
4171
4172 /* Test the lower-level mapped_index::find_name_component_bounds
4173 method in completion mode. */
4174 {
4175 static const char *expected_syms[] = {
4176 "t1_func",
4177 "t1_func1",
5c58de74
PA
4178 };
4179
a3c5fafd 4180 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4181 "t1_func", expected_syms));
4182 }
4183
4184 /* Check that the increment-last-char in the name matching algorithm
4185 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4186 {
4187 static const char *expected_syms1[] = {
4188 "\377",
4189 "\377\377123",
4190 };
a3c5fafd 4191 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4192 "\377", expected_syms1));
4193
4194 static const char *expected_syms2[] = {
4195 "\377\377123",
4196 };
a3c5fafd 4197 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4198 "\377\377", expected_syms2));
4199 }
4200}
4201
4202/* Test dw2_expand_symtabs_matching_symbol. */
4203
4204static void
4205test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4206{
4207 mock_mapped_index mock_index (test_symbols);
4208
4209 /* We let all tests run until the end even if some fails, for debug
4210 convenience. */
4211 bool any_mismatch = false;
4212
4213 /* Create the expected symbols list (an initializer_list). Needed
4214 because lists have commas, and we need to pass them to CHECK,
4215 which is a macro. */
4216#define EXPECT(...) { __VA_ARGS__ }
4217
4218 /* Wrapper for check_match that passes down the current
4219 __FILE__/__LINE__. */
4220#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4221 any_mismatch |= !check_match (__FILE__, __LINE__, \
4222 mock_index, \
4223 NAME, MATCH_TYPE, COMPLETION_MODE, \
4224 EXPECTED_LIST)
4225
4226 /* Identity checks. */
4227 for (const char *sym : test_symbols)
4228 {
4229 /* Should be able to match all existing symbols. */
4230 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4231 EXPECT (sym));
4232
4233 /* Should be able to match all existing symbols with
4234 parameters. */
4235 std::string with_params = std::string (sym) + "(int)";
4236 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4237 EXPECT (sym));
4238
4239 /* Should be able to match all existing symbols with
4240 parameters and qualifiers. */
4241 with_params = std::string (sym) + " ( int ) const";
4242 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4243 EXPECT (sym));
4244
4245 /* This should really find sym, but cp-name-parser.y doesn't
4246 know about lvalue/rvalue qualifiers yet. */
4247 with_params = std::string (sym) + " ( int ) &&";
4248 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4249 {});
4250 }
4251
e1ef7d7a
PA
4252 /* Check that the name matching algorithm for completion doesn't get
4253 confused with Latin1 'ÿ' / 0xff. */
4254 {
4255 static const char str[] = "\377";
4256 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4257 EXPECT ("\377", "\377\377123"));
4258 }
4259
4260 /* Check that the increment-last-char in the matching algorithm for
4261 completion doesn't match "t1_fund" when completing "t1_func". */
4262 {
4263 static const char str[] = "t1_func";
4264 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4265 EXPECT ("t1_func", "t1_func1"));
4266 }
4267
c62446b1
PA
4268 /* Check that completion mode works at each prefix of the expected
4269 symbol name. */
4270 {
4271 static const char str[] = "function(int)";
4272 size_t len = strlen (str);
4273 std::string lookup;
4274
4275 for (size_t i = 1; i < len; i++)
4276 {
4277 lookup.assign (str, i);
4278 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4279 EXPECT ("function"));
4280 }
4281 }
4282
4283 /* While "w" is a prefix of both components, the match function
4284 should still only be called once. */
4285 {
4286 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4287 EXPECT ("w1::w2"));
a20714ff
PA
4288 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4289 EXPECT ("w1::w2"));
c62446b1
PA
4290 }
4291
4292 /* Same, with a "complicated" symbol. */
4293 {
4294 static const char str[] = Z_SYM_NAME;
4295 size_t len = strlen (str);
4296 std::string lookup;
4297
4298 for (size_t i = 1; i < len; i++)
4299 {
4300 lookup.assign (str, i);
4301 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4302 EXPECT (Z_SYM_NAME));
4303 }
4304 }
4305
4306 /* In FULL mode, an incomplete symbol doesn't match. */
4307 {
4308 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4309 {});
4310 }
4311
4312 /* A complete symbol with parameters matches any overload, since the
4313 index has no overload info. */
4314 {
4315 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4316 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4317 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4318 EXPECT ("std::zfunction", "std::zfunction2"));
4319 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4320 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4321 }
4322
4323 /* Check that whitespace is ignored appropriately. A symbol with a
4324 template argument list. */
4325 {
4326 static const char expected[] = "ns::foo<int>";
4327 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4328 EXPECT (expected));
a20714ff
PA
4329 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4330 EXPECT (expected));
c62446b1
PA
4331 }
4332
4333 /* Check that whitespace is ignored appropriately. A symbol with a
4334 template argument list that includes a pointer. */
4335 {
4336 static const char expected[] = "ns::foo<char*>";
4337 /* Try both completion and non-completion modes. */
4338 static const bool completion_mode[2] = {false, true};
4339 for (size_t i = 0; i < 2; i++)
4340 {
4341 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4342 completion_mode[i], EXPECT (expected));
a20714ff
PA
4343 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4344 completion_mode[i], EXPECT (expected));
c62446b1
PA
4345
4346 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4347 completion_mode[i], EXPECT (expected));
a20714ff
PA
4348 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4349 completion_mode[i], EXPECT (expected));
c62446b1
PA
4350 }
4351 }
4352
4353 {
4354 /* Check method qualifiers are ignored. */
4355 static const char expected[] = "ns::foo<char*>";
4356 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4357 symbol_name_match_type::FULL, true, EXPECT (expected));
4358 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4359 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4360 CHECK_MATCH ("foo < char * > ( int ) const",
4361 symbol_name_match_type::WILD, true, EXPECT (expected));
4362 CHECK_MATCH ("foo < char * > ( int ) &&",
4363 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4364 }
4365
4366 /* Test lookup names that don't match anything. */
4367 {
a20714ff
PA
4368 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4369 {});
4370
c62446b1
PA
4371 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4372 {});
4373 }
4374
a20714ff
PA
4375 /* Some wild matching tests, exercising "(anonymous namespace)",
4376 which should not be confused with a parameter list. */
4377 {
4378 static const char *syms[] = {
4379 "A::B::C",
4380 "B::C",
4381 "C",
4382 "A :: B :: C ( int )",
4383 "B :: C ( int )",
4384 "C ( int )",
4385 };
4386
4387 for (const char *s : syms)
4388 {
4389 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4390 EXPECT ("(anonymous namespace)::A::B::C"));
4391 }
4392 }
4393
4394 {
4395 static const char expected[] = "ns2::tmpl<int>::foo2";
4396 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4397 EXPECT (expected));
4398 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4399 EXPECT (expected));
4400 }
4401
c62446b1
PA
4402 SELF_CHECK (!any_mismatch);
4403
4404#undef EXPECT
4405#undef CHECK_MATCH
4406}
4407
5c58de74
PA
4408static void
4409run_test ()
4410{
4411 test_mapped_index_find_name_component_bounds ();
4412 test_dw2_expand_symtabs_matching_symbol ();
4413}
4414
c62446b1
PA
4415}} // namespace selftests::dw2_expand_symtabs_matching
4416
4417#endif /* GDB_SELF_TEST */
4418
4b514bc8
JK
4419/* If FILE_MATCHER is NULL or if PER_CU has
4420 dwarf2_per_cu_quick_data::MARK set (see
4421 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4422 EXPANSION_NOTIFY on it. */
4423
4424static void
4425dw2_expand_symtabs_matching_one
4426 (struct dwarf2_per_cu_data *per_cu,
4427 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4428 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4429{
4430 if (file_matcher == NULL || per_cu->v.quick->mark)
4431 {
4432 bool symtab_was_null
4433 = (per_cu->v.quick->compunit_symtab == NULL);
4434
58f0c718 4435 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4436
4437 if (expansion_notify != NULL
4438 && symtab_was_null
4439 && per_cu->v.quick->compunit_symtab != NULL)
4440 expansion_notify (per_cu->v.quick->compunit_symtab);
4441 }
4442}
4443
3f563c84
PA
4444/* Helper for dw2_expand_matching symtabs. Called on each symbol
4445 matched, to expand corresponding CUs that were marked. IDX is the
4446 index of the symbol name that matched. */
4447
4448static void
4449dw2_expand_marked_cus
ed2dc618 4450 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4451 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4452 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4453 search_domain kind)
4454{
3f563c84
PA
4455 offset_type *vec, vec_len, vec_idx;
4456 bool global_seen = false;
ed2dc618 4457 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4458
61920122 4459 vec = (offset_type *) (index.constant_pool
f00a2de2 4460 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4461 vec_len = MAYBE_SWAP (vec[0]);
4462 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4463 {
61920122
PA
4464 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4465 /* This value is only valid for index versions >= 7. */
4466 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4467 gdb_index_symbol_kind symbol_kind =
4468 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4469 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4470 /* Only check the symbol attributes if they're present.
4471 Indices prior to version 7 don't record them,
4472 and indices >= 7 may elide them for certain symbols
4473 (gold does this). */
4474 int attrs_valid =
4475 (index.version >= 7
4476 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4477
4478 /* Work around gold/15646. */
4479 if (attrs_valid)
9291a0cd 4480 {
61920122
PA
4481 if (!is_static && global_seen)
4482 continue;
4483 if (!is_static)
4484 global_seen = true;
4485 }
3190f0c6 4486
61920122
PA
4487 /* Only check the symbol's kind if it has one. */
4488 if (attrs_valid)
4489 {
4490 switch (kind)
8943b874 4491 {
61920122
PA
4492 case VARIABLES_DOMAIN:
4493 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4494 continue;
4495 break;
4496 case FUNCTIONS_DOMAIN:
4497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4498 continue;
61920122
PA
4499 break;
4500 case TYPES_DOMAIN:
4501 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4502 continue;
4503 break;
59c35742
AB
4504 case MODULES_DOMAIN:
4505 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4506 continue;
4507 break;
61920122
PA
4508 default:
4509 break;
8943b874 4510 }
61920122 4511 }
8943b874 4512
61920122 4513 /* Don't crash on bad data. */
b76e467d 4514 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4515 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4516 {
b98664d3 4517 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4518 " [in module %s]"),
4519 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4520 continue;
4521 }
4522
ff4c9fec 4523 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4524 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4525 expansion_notify);
61920122
PA
4526 }
4527}
4528
4b514bc8
JK
4529/* If FILE_MATCHER is non-NULL, set all the
4530 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4531 that match FILE_MATCHER. */
4532
61920122 4533static void
4b514bc8 4534dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4535 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4536 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4537{
4b514bc8 4538 if (file_matcher == NULL)
61920122
PA
4539 return;
4540
4b514bc8
JK
4541 objfile *const objfile = dwarf2_per_objfile->objfile;
4542
4543 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4544 htab_eq_pointer,
4545 NULL, xcalloc, xfree));
4546 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4547 htab_eq_pointer,
4548 NULL, xcalloc, xfree));
61920122 4549
4b514bc8
JK
4550 /* The rule is CUs specify all the files, including those used by
4551 any TU, so there's no need to scan TUs here. */
61920122 4552
b76e467d 4553 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4554 {
927aa2e7
JK
4555 QUIT;
4556
4557 per_cu->v.quick->mark = 0;
4558
4559 /* We only need to look at symtabs not already expanded. */
4560 if (per_cu->v.quick->compunit_symtab)
4561 continue;
4562
b76e467d 4563 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4564 if (file_data == NULL)
4565 continue;
4566
4567 if (htab_find (visited_not_found.get (), file_data) != NULL)
4568 continue;
4569 else if (htab_find (visited_found.get (), file_data) != NULL)
4570 {
4571 per_cu->v.quick->mark = 1;
4572 continue;
4573 }
4574
b76e467d 4575 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4576 {
4577 const char *this_real_name;
4578
4579 if (file_matcher (file_data->file_names[j], false))
4580 {
4581 per_cu->v.quick->mark = 1;
4582 break;
4583 }
4584
4585 /* Before we invoke realpath, which can get expensive when many
4586 files are involved, do a quick comparison of the basenames. */
4587 if (!basenames_may_differ
4588 && !file_matcher (lbasename (file_data->file_names[j]),
4589 true))
4590 continue;
4591
4592 this_real_name = dw2_get_real_path (objfile, file_data, j);
4593 if (file_matcher (this_real_name, false))
4594 {
4595 per_cu->v.quick->mark = 1;
4596 break;
4597 }
4598 }
4599
b76e467d
SM
4600 void **slot = htab_find_slot (per_cu->v.quick->mark
4601 ? visited_found.get ()
4602 : visited_not_found.get (),
4603 file_data, INSERT);
927aa2e7
JK
4604 *slot = file_data;
4605 }
4606}
4607
4608static void
4609dw2_expand_symtabs_matching
4610 (struct objfile *objfile,
4611 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4612 const lookup_name_info &lookup_name,
4613 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4614 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4615 enum search_domain kind)
4616{
ed2dc618
SM
4617 struct dwarf2_per_objfile *dwarf2_per_objfile
4618 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4619
4620 /* index_table is NULL if OBJF_READNOW. */
4621 if (!dwarf2_per_objfile->index_table)
4622 return;
4623
ed2dc618 4624 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4625
4626 mapped_index &index = *dwarf2_per_objfile->index_table;
4627
4628 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4629 symbol_matcher,
4630 kind, [&] (offset_type idx)
4631 {
ed2dc618 4632 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4633 expansion_notify, kind);
3b00ef10 4634 return true;
927aa2e7
JK
4635 });
4636}
4637
4638/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4639 symtab. */
4640
4641static struct compunit_symtab *
4642recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4643 CORE_ADDR pc)
4644{
4645 int i;
4646
4647 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4648 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4649 return cust;
4650
4651 if (cust->includes == NULL)
4652 return NULL;
4653
4654 for (i = 0; cust->includes[i]; ++i)
4655 {
4656 struct compunit_symtab *s = cust->includes[i];
4657
4658 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4659 if (s != NULL)
4660 return s;
4661 }
4662
4663 return NULL;
4664}
4665
4666static struct compunit_symtab *
4667dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4668 struct bound_minimal_symbol msymbol,
4669 CORE_ADDR pc,
4670 struct obj_section *section,
4671 int warn_if_readin)
4672{
4673 struct dwarf2_per_cu_data *data;
4674 struct compunit_symtab *result;
4675
d320c2b5 4676 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4677 return NULL;
4678
b3b3bada 4679 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4680 data = (struct dwarf2_per_cu_data *) addrmap_find
4681 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4682 if (!data)
4683 return NULL;
4684
4685 if (warn_if_readin && data->v.quick->compunit_symtab)
4686 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4687 paddress (get_objfile_arch (objfile), pc));
4688
4689 result
58f0c718
TT
4690 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4691 false),
927aa2e7
JK
4692 pc);
4693 gdb_assert (result != NULL);
4694 return result;
4695}
4696
4697static void
4698dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4699 void *data, int need_fullname)
4700{
ed2dc618
SM
4701 struct dwarf2_per_objfile *dwarf2_per_objfile
4702 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4703
4704 if (!dwarf2_per_objfile->filenames_cache)
4705 {
4706 dwarf2_per_objfile->filenames_cache.emplace ();
4707
4708 htab_up visited (htab_create_alloc (10,
4709 htab_hash_pointer, htab_eq_pointer,
4710 NULL, xcalloc, xfree));
4711
4712 /* The rule is CUs specify all the files, including those used
4713 by any TU, so there's no need to scan TUs here. We can
4714 ignore file names coming from already-expanded CUs. */
4715
b76e467d 4716 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4717 {
927aa2e7
JK
4718 if (per_cu->v.quick->compunit_symtab)
4719 {
4720 void **slot = htab_find_slot (visited.get (),
4721 per_cu->v.quick->file_names,
4722 INSERT);
4723
4724 *slot = per_cu->v.quick->file_names;
4725 }
4726 }
4727
b76e467d 4728 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4729 {
927aa2e7
JK
4730 /* We only need to look at symtabs not already expanded. */
4731 if (per_cu->v.quick->compunit_symtab)
4732 continue;
4733
b76e467d 4734 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4735 if (file_data == NULL)
4736 continue;
4737
b76e467d 4738 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4739 if (*slot)
4740 {
4741 /* Already visited. */
4742 continue;
4743 }
4744 *slot = file_data;
4745
4746 for (int j = 0; j < file_data->num_file_names; ++j)
4747 {
4748 const char *filename = file_data->file_names[j];
4749 dwarf2_per_objfile->filenames_cache->seen (filename);
4750 }
4751 }
4752 }
4753
4754 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4755 {
4756 gdb::unique_xmalloc_ptr<char> this_real_name;
4757
4758 if (need_fullname)
4759 this_real_name = gdb_realpath (filename);
4760 (*fun) (filename, this_real_name.get (), data);
4761 });
4762}
4763
4764static int
4765dw2_has_symbols (struct objfile *objfile)
4766{
4767 return 1;
4768}
4769
4770const struct quick_symbol_functions dwarf2_gdb_index_functions =
4771{
4772 dw2_has_symbols,
4773 dw2_find_last_source_symtab,
4774 dw2_forget_cached_source_info,
4775 dw2_map_symtabs_matching_filename,
4776 dw2_lookup_symbol,
4777 dw2_print_stats,
4778 dw2_dump,
927aa2e7
JK
4779 dw2_expand_symtabs_for_function,
4780 dw2_expand_all_symtabs,
4781 dw2_expand_symtabs_with_fullname,
4782 dw2_map_matching_symbols,
4783 dw2_expand_symtabs_matching,
4784 dw2_find_pc_sect_compunit_symtab,
4785 NULL,
4786 dw2_map_symbol_filenames
4787};
4788
4789/* DWARF-5 debug_names reader. */
4790
4791/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4792static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4793
4794/* A helper function that reads the .debug_names section in SECTION
4795 and fills in MAP. FILENAME is the name of the file containing the
4796 section; it is used for error reporting.
4797
4798 Returns true if all went well, false otherwise. */
4799
4800static bool
4801read_debug_names_from_section (struct objfile *objfile,
4802 const char *filename,
4803 struct dwarf2_section_info *section,
4804 mapped_debug_names &map)
4805{
96b79293 4806 if (section->empty ())
927aa2e7
JK
4807 return false;
4808
4809 /* Older elfutils strip versions could keep the section in the main
4810 executable while splitting it for the separate debug info file. */
96b79293 4811 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4812 return false;
4813
96b79293 4814 section->read (objfile);
927aa2e7
JK
4815
4816 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4817
4818 const gdb_byte *addr = section->buffer;
4819
96b79293 4820 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4821
4822 unsigned int bytes_read;
4823 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4824 addr += bytes_read;
4825
4826 map.dwarf5_is_dwarf64 = bytes_read != 4;
4827 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4828 if (bytes_read + length != section->size)
4829 {
4830 /* There may be multiple per-CU indices. */
4831 warning (_("Section .debug_names in %s length %s does not match "
4832 "section length %s, ignoring .debug_names."),
4833 filename, plongest (bytes_read + length),
4834 pulongest (section->size));
4835 return false;
4836 }
4837
4838 /* The version number. */
4839 uint16_t version = read_2_bytes (abfd, addr);
4840 addr += 2;
4841 if (version != 5)
4842 {
4843 warning (_("Section .debug_names in %s has unsupported version %d, "
4844 "ignoring .debug_names."),
4845 filename, version);
4846 return false;
4847 }
4848
4849 /* Padding. */
4850 uint16_t padding = read_2_bytes (abfd, addr);
4851 addr += 2;
4852 if (padding != 0)
4853 {
4854 warning (_("Section .debug_names in %s has unsupported padding %d, "
4855 "ignoring .debug_names."),
4856 filename, padding);
4857 return false;
4858 }
4859
4860 /* comp_unit_count - The number of CUs in the CU list. */
4861 map.cu_count = read_4_bytes (abfd, addr);
4862 addr += 4;
4863
4864 /* local_type_unit_count - The number of TUs in the local TU
4865 list. */
4866 map.tu_count = read_4_bytes (abfd, addr);
4867 addr += 4;
4868
4869 /* foreign_type_unit_count - The number of TUs in the foreign TU
4870 list. */
4871 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4872 addr += 4;
4873 if (foreign_tu_count != 0)
4874 {
4875 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4876 "ignoring .debug_names."),
4877 filename, static_cast<unsigned long> (foreign_tu_count));
4878 return false;
4879 }
4880
4881 /* bucket_count - The number of hash buckets in the hash lookup
4882 table. */
4883 map.bucket_count = read_4_bytes (abfd, addr);
4884 addr += 4;
4885
4886 /* name_count - The number of unique names in the index. */
4887 map.name_count = read_4_bytes (abfd, addr);
4888 addr += 4;
4889
4890 /* abbrev_table_size - The size in bytes of the abbreviations
4891 table. */
4892 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4893 addr += 4;
4894
4895 /* augmentation_string_size - The size in bytes of the augmentation
4896 string. This value is rounded up to a multiple of 4. */
4897 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4898 addr += 4;
4899 map.augmentation_is_gdb = ((augmentation_string_size
4900 == sizeof (dwarf5_augmentation))
4901 && memcmp (addr, dwarf5_augmentation,
4902 sizeof (dwarf5_augmentation)) == 0);
4903 augmentation_string_size += (-augmentation_string_size) & 3;
4904 addr += augmentation_string_size;
4905
4906 /* List of CUs */
4907 map.cu_table_reordered = addr;
4908 addr += map.cu_count * map.offset_size;
4909
4910 /* List of Local TUs */
4911 map.tu_table_reordered = addr;
4912 addr += map.tu_count * map.offset_size;
4913
4914 /* Hash Lookup Table */
4915 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4916 addr += map.bucket_count * 4;
4917 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4918 addr += map.name_count * 4;
4919
4920 /* Name Table */
4921 map.name_table_string_offs_reordered = addr;
4922 addr += map.name_count * map.offset_size;
4923 map.name_table_entry_offs_reordered = addr;
4924 addr += map.name_count * map.offset_size;
4925
4926 const gdb_byte *abbrev_table_start = addr;
4927 for (;;)
4928 {
927aa2e7
JK
4929 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4930 addr += bytes_read;
4931 if (index_num == 0)
4932 break;
4933
4934 const auto insertpair
4935 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4936 if (!insertpair.second)
4937 {
4938 warning (_("Section .debug_names in %s has duplicate index %s, "
4939 "ignoring .debug_names."),
4940 filename, pulongest (index_num));
4941 return false;
4942 }
4943 mapped_debug_names::index_val &indexval = insertpair.first->second;
4944 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4945 addr += bytes_read;
4946
4947 for (;;)
4948 {
4949 mapped_debug_names::index_val::attr attr;
4950 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4951 addr += bytes_read;
4952 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4953 addr += bytes_read;
4954 if (attr.form == DW_FORM_implicit_const)
4955 {
4956 attr.implicit_const = read_signed_leb128 (abfd, addr,
4957 &bytes_read);
4958 addr += bytes_read;
4959 }
4960 if (attr.dw_idx == 0 && attr.form == 0)
4961 break;
4962 indexval.attr_vec.push_back (std::move (attr));
4963 }
4964 }
4965 if (addr != abbrev_table_start + abbrev_table_size)
4966 {
4967 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4968 "of size %s vs. written as %u, ignoring .debug_names."),
4969 filename, plongest (addr - abbrev_table_start),
4970 abbrev_table_size);
927aa2e7
JK
4971 return false;
4972 }
4973 map.entry_pool = addr;
4974
4975 return true;
4976}
4977
4978/* A helper for create_cus_from_debug_names that handles the MAP's CU
4979 list. */
4980
4981static void
ed2dc618 4982create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4983 const mapped_debug_names &map,
4984 dwarf2_section_info &section,
b76e467d 4985 bool is_dwz)
927aa2e7
JK
4986{
4987 sect_offset sect_off_prev;
4988 for (uint32_t i = 0; i <= map.cu_count; ++i)
4989 {
4990 sect_offset sect_off_next;
4991 if (i < map.cu_count)
4992 {
4993 sect_off_next
4994 = (sect_offset) (extract_unsigned_integer
4995 (map.cu_table_reordered + i * map.offset_size,
4996 map.offset_size,
4997 map.dwarf5_byte_order));
4998 }
4999 else
5000 sect_off_next = (sect_offset) section.size;
5001 if (i >= 1)
5002 {
5003 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5004 dwarf2_per_cu_data *per_cu
ed2dc618 5005 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5006 sect_off_prev, length);
b76e467d 5007 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5008 }
5009 sect_off_prev = sect_off_next;
5010 }
5011}
5012
5013/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5014 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5015
5016static void
ed2dc618 5017create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5018 const mapped_debug_names &map,
5019 const mapped_debug_names &dwz_map)
5020{
b76e467d
SM
5021 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5022 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5023
ed2dc618
SM
5024 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5025 dwarf2_per_objfile->info,
b76e467d 5026 false /* is_dwz */);
927aa2e7
JK
5027
5028 if (dwz_map.cu_count == 0)
5029 return;
5030
ed2dc618
SM
5031 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5032 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5033 true /* is_dwz */);
927aa2e7
JK
5034}
5035
5036/* Read .debug_names. If everything went ok, initialize the "quick"
5037 elements of all the CUs and return true. Otherwise, return false. */
5038
5039static bool
ed2dc618 5040dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5041{
22ca247e
TT
5042 std::unique_ptr<mapped_debug_names> map
5043 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5044 mapped_debug_names dwz_map (dwarf2_per_objfile);
5045 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5046
5047 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5048 &dwarf2_per_objfile->debug_names,
22ca247e 5049 *map))
927aa2e7
JK
5050 return false;
5051
5052 /* Don't use the index if it's empty. */
22ca247e 5053 if (map->name_count == 0)
927aa2e7
JK
5054 return false;
5055
5056 /* If there is a .dwz file, read it so we can get its CU list as
5057 well. */
ed2dc618 5058 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5059 if (dwz != NULL)
5060 {
5061 if (!read_debug_names_from_section (objfile,
00f93c44 5062 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5063 &dwz->debug_names, dwz_map))
5064 {
5065 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5066 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5067 return false;
5068 }
5069 }
5070
22ca247e 5071 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5072
22ca247e 5073 if (map->tu_count != 0)
927aa2e7
JK
5074 {
5075 /* We can only handle a single .debug_types when we have an
5076 index. */
fd5866f6 5077 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5078 return false;
5079
fd5866f6 5080 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5081
5082 create_signatured_type_table_from_debug_names
22ca247e 5083 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5084 }
5085
ed2dc618
SM
5086 create_addrmap_from_aranges (dwarf2_per_objfile,
5087 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5088
22ca247e 5089 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5090 dwarf2_per_objfile->using_index = 1;
5091 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5092 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5093
5094 return true;
5095}
5096
927aa2e7
JK
5097/* Type used to manage iterating over all CUs looking for a symbol for
5098 .debug_names. */
5099
5100class dw2_debug_names_iterator
5101{
5102public:
927aa2e7 5103 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5104 gdb::optional<block_enum> block_index,
5105 domain_enum domain,
927aa2e7 5106 const char *name)
2b79f376 5107 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5108 m_addr (find_vec_in_debug_names (map, name))
5109 {}
5110
5111 dw2_debug_names_iterator (const mapped_debug_names &map,
5112 search_domain search, uint32_t namei)
5113 : m_map (map),
5114 m_search (search),
5115 m_addr (find_vec_in_debug_names (map, namei))
5116 {}
5117
3b00ef10
TT
5118 dw2_debug_names_iterator (const mapped_debug_names &map,
5119 block_enum block_index, domain_enum domain,
5120 uint32_t namei)
5121 : m_map (map), m_block_index (block_index), m_domain (domain),
5122 m_addr (find_vec_in_debug_names (map, namei))
5123 {}
5124
927aa2e7
JK
5125 /* Return the next matching CU or NULL if there are no more. */
5126 dwarf2_per_cu_data *next ();
5127
5128private:
5129 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5130 const char *name);
5131 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5132 uint32_t namei);
5133
5134 /* The internalized form of .debug_names. */
5135 const mapped_debug_names &m_map;
5136
2b79f376
SM
5137 /* If set, only look for symbols that match that block. Valid values are
5138 GLOBAL_BLOCK and STATIC_BLOCK. */
5139 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5140
5141 /* The kind of symbol we're looking for. */
5142 const domain_enum m_domain = UNDEF_DOMAIN;
5143 const search_domain m_search = ALL_DOMAIN;
5144
5145 /* The list of CUs from the index entry of the symbol, or NULL if
5146 not found. */
5147 const gdb_byte *m_addr;
5148};
5149
5150const char *
5151mapped_debug_names::namei_to_name (uint32_t namei) const
5152{
5153 const ULONGEST namei_string_offs
5154 = extract_unsigned_integer ((name_table_string_offs_reordered
5155 + namei * offset_size),
5156 offset_size,
5157 dwarf5_byte_order);
5158 return read_indirect_string_at_offset
ed2dc618 5159 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5160}
5161
5162/* Find a slot in .debug_names for the object named NAME. If NAME is
5163 found, return pointer to its pool data. If NAME cannot be found,
5164 return NULL. */
5165
5166const gdb_byte *
5167dw2_debug_names_iterator::find_vec_in_debug_names
5168 (const mapped_debug_names &map, const char *name)
5169{
5170 int (*cmp) (const char *, const char *);
5171
54ee4252 5172 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5173 if (current_language->la_language == language_cplus
5174 || current_language->la_language == language_fortran
5175 || current_language->la_language == language_d)
5176 {
5177 /* NAME is already canonical. Drop any qualifiers as
5178 .debug_names does not contain any. */
5179
5180 if (strchr (name, '(') != NULL)
5181 {
54ee4252 5182 without_params = cp_remove_params (name);
927aa2e7 5183 if (without_params != NULL)
54ee4252 5184 name = without_params.get ();
927aa2e7
JK
5185 }
5186 }
5187
5188 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5189
5190 const uint32_t full_hash = dwarf5_djb_hash (name);
5191 uint32_t namei
5192 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5193 (map.bucket_table_reordered
5194 + (full_hash % map.bucket_count)), 4,
5195 map.dwarf5_byte_order);
5196 if (namei == 0)
5197 return NULL;
5198 --namei;
5199 if (namei >= map.name_count)
5200 {
b98664d3 5201 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5202 "[in module %s]"),
5203 namei, map.name_count,
ed2dc618 5204 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5205 return NULL;
5206 }
5207
5208 for (;;)
5209 {
5210 const uint32_t namei_full_hash
5211 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5212 (map.hash_table_reordered + namei), 4,
5213 map.dwarf5_byte_order);
5214 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5215 return NULL;
5216
5217 if (full_hash == namei_full_hash)
5218 {
5219 const char *const namei_string = map.namei_to_name (namei);
5220
5221#if 0 /* An expensive sanity check. */
5222 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5223 {
b98664d3 5224 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5225 "[in module %s]"),
5226 namei, objfile_name (dwarf2_per_objfile->objfile));
5227 return NULL;
5228 }
5229#endif
5230
5231 if (cmp (namei_string, name) == 0)
5232 {
5233 const ULONGEST namei_entry_offs
5234 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5235 + namei * map.offset_size),
5236 map.offset_size, map.dwarf5_byte_order);
5237 return map.entry_pool + namei_entry_offs;
5238 }
5239 }
5240
5241 ++namei;
5242 if (namei >= map.name_count)
5243 return NULL;
5244 }
5245}
5246
5247const gdb_byte *
5248dw2_debug_names_iterator::find_vec_in_debug_names
5249 (const mapped_debug_names &map, uint32_t namei)
5250{
5251 if (namei >= map.name_count)
5252 {
b98664d3 5253 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5254 "[in module %s]"),
5255 namei, map.name_count,
ed2dc618 5256 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5257 return NULL;
5258 }
5259
5260 const ULONGEST namei_entry_offs
5261 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5262 + namei * map.offset_size),
5263 map.offset_size, map.dwarf5_byte_order);
5264 return map.entry_pool + namei_entry_offs;
5265}
5266
5267/* See dw2_debug_names_iterator. */
5268
5269dwarf2_per_cu_data *
5270dw2_debug_names_iterator::next ()
5271{
5272 if (m_addr == NULL)
5273 return NULL;
5274
ed2dc618
SM
5275 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5276 struct objfile *objfile = dwarf2_per_objfile->objfile;
5277 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5278
5279 again:
5280
5281 unsigned int bytes_read;
5282 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5283 m_addr += bytes_read;
5284 if (abbrev == 0)
5285 return NULL;
5286
5287 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5288 if (indexval_it == m_map.abbrev_map.cend ())
5289 {
b98664d3 5290 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5291 "[in module %s]"),
ed2dc618 5292 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5293 return NULL;
5294 }
5295 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5296 enum class symbol_linkage {
5297 unknown,
5298 static_,
5299 extern_,
23c13d42 5300 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5301 dwarf2_per_cu_data *per_cu = NULL;
5302 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5303 {
5304 ULONGEST ull;
5305 switch (attr.form)
5306 {
5307 case DW_FORM_implicit_const:
5308 ull = attr.implicit_const;
5309 break;
5310 case DW_FORM_flag_present:
5311 ull = 1;
5312 break;
5313 case DW_FORM_udata:
5314 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5315 m_addr += bytes_read;
5316 break;
5317 default:
b98664d3 5318 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5319 dwarf_form_name (attr.form),
ed2dc618 5320 objfile_name (objfile));
927aa2e7
JK
5321 return NULL;
5322 }
5323 switch (attr.dw_idx)
5324 {
5325 case DW_IDX_compile_unit:
5326 /* Don't crash on bad data. */
b76e467d 5327 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5328 {
b98664d3 5329 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5330 " [in module %s]"),
5331 pulongest (ull),
5332 objfile_name (dwarf2_per_objfile->objfile));
5333 continue;
5334 }
ff4c9fec 5335 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5336 break;
8af5c486
JK
5337 case DW_IDX_type_unit:
5338 /* Don't crash on bad data. */
b2bdb8cf 5339 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5340 {
b98664d3 5341 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5342 " [in module %s]"),
5343 pulongest (ull),
5344 objfile_name (dwarf2_per_objfile->objfile));
5345 continue;
5346 }
ff4c9fec 5347 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5348 break;
927aa2e7
JK
5349 case DW_IDX_GNU_internal:
5350 if (!m_map.augmentation_is_gdb)
5351 break;
23c13d42 5352 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5353 break;
5354 case DW_IDX_GNU_external:
5355 if (!m_map.augmentation_is_gdb)
5356 break;
23c13d42 5357 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5358 break;
5359 }
5360 }
5361
5362 /* Skip if already read in. */
5363 if (per_cu->v.quick->compunit_symtab)
5364 goto again;
5365
5366 /* Check static vs global. */
23c13d42 5367 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5368 {
2b79f376 5369 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5370 const bool symbol_is_static =
5371 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5372 if (want_static != symbol_is_static)
2b79f376 5373 goto again;
927aa2e7
JK
5374 }
5375
5376 /* Match dw2_symtab_iter_next, symbol_kind
5377 and debug_names::psymbol_tag. */
5378 switch (m_domain)
5379 {
5380 case VAR_DOMAIN:
5381 switch (indexval.dwarf_tag)
5382 {
5383 case DW_TAG_variable:
5384 case DW_TAG_subprogram:
5385 /* Some types are also in VAR_DOMAIN. */
5386 case DW_TAG_typedef:
5387 case DW_TAG_structure_type:
5388 break;
5389 default:
5390 goto again;
5391 }
5392 break;
5393 case STRUCT_DOMAIN:
5394 switch (indexval.dwarf_tag)
5395 {
5396 case DW_TAG_typedef:
5397 case DW_TAG_structure_type:
5398 break;
5399 default:
5400 goto again;
5401 }
5402 break;
5403 case LABEL_DOMAIN:
5404 switch (indexval.dwarf_tag)
5405 {
5406 case 0:
5407 case DW_TAG_variable:
5408 break;
5409 default:
5410 goto again;
5411 }
5412 break;
59c35742
AB
5413 case MODULE_DOMAIN:
5414 switch (indexval.dwarf_tag)
5415 {
5416 case DW_TAG_module:
5417 break;
5418 default:
5419 goto again;
5420 }
5421 break;
927aa2e7
JK
5422 default:
5423 break;
5424 }
5425
5426 /* Match dw2_expand_symtabs_matching, symbol_kind and
5427 debug_names::psymbol_tag. */
5428 switch (m_search)
4b514bc8 5429 {
927aa2e7
JK
5430 case VARIABLES_DOMAIN:
5431 switch (indexval.dwarf_tag)
4b514bc8 5432 {
927aa2e7
JK
5433 case DW_TAG_variable:
5434 break;
5435 default:
5436 goto again;
4b514bc8 5437 }
927aa2e7
JK
5438 break;
5439 case FUNCTIONS_DOMAIN:
5440 switch (indexval.dwarf_tag)
4b514bc8 5441 {
927aa2e7
JK
5442 case DW_TAG_subprogram:
5443 break;
5444 default:
5445 goto again;
4b514bc8 5446 }
927aa2e7
JK
5447 break;
5448 case TYPES_DOMAIN:
5449 switch (indexval.dwarf_tag)
5450 {
5451 case DW_TAG_typedef:
5452 case DW_TAG_structure_type:
5453 break;
5454 default:
5455 goto again;
5456 }
5457 break;
59c35742
AB
5458 case MODULES_DOMAIN:
5459 switch (indexval.dwarf_tag)
5460 {
5461 case DW_TAG_module:
5462 break;
5463 default:
5464 goto again;
5465 }
927aa2e7
JK
5466 default:
5467 break;
4b514bc8 5468 }
927aa2e7
JK
5469
5470 return per_cu;
4b514bc8 5471}
61920122 5472
927aa2e7 5473static struct compunit_symtab *
c7f839cb 5474dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5475 const char *name, domain_enum domain)
4b514bc8 5476{
ed2dc618
SM
5477 struct dwarf2_per_objfile *dwarf2_per_objfile
5478 = get_dwarf2_per_objfile (objfile);
61920122 5479
927aa2e7
JK
5480 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5481 if (!mapp)
61920122 5482 {
927aa2e7
JK
5483 /* index is NULL if OBJF_READNOW. */
5484 return NULL;
5485 }
5486 const auto &map = *mapp;
9291a0cd 5487
2b79f376 5488 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5489
927aa2e7
JK
5490 struct compunit_symtab *stab_best = NULL;
5491 struct dwarf2_per_cu_data *per_cu;
5492 while ((per_cu = iter.next ()) != NULL)
5493 {
5494 struct symbol *sym, *with_opaque = NULL;
58f0c718 5495 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5496 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5497 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5498
927aa2e7
JK
5499 sym = block_find_symbol (block, name, domain,
5500 block_find_non_opaque_type_preferred,
5501 &with_opaque);
9703b513 5502
927aa2e7
JK
5503 /* Some caution must be observed with overloaded functions and
5504 methods, since the index will not contain any overload
5505 information (but NAME might contain it). */
a3ec0bb1 5506
927aa2e7 5507 if (sym != NULL
987012b8 5508 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5509 return stab;
5510 if (with_opaque != NULL
987012b8 5511 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5512 stab_best = stab;
9703b513 5513
927aa2e7 5514 /* Keep looking through other CUs. */
9703b513
TT
5515 }
5516
927aa2e7 5517 return stab_best;
9703b513
TT
5518}
5519
927aa2e7
JK
5520/* This dumps minimal information about .debug_names. It is called
5521 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5522 uses this to verify that .debug_names has been loaded. */
9291a0cd 5523
927aa2e7
JK
5524static void
5525dw2_debug_names_dump (struct objfile *objfile)
5526{
ed2dc618
SM
5527 struct dwarf2_per_objfile *dwarf2_per_objfile
5528 = get_dwarf2_per_objfile (objfile);
5529
927aa2e7
JK
5530 gdb_assert (dwarf2_per_objfile->using_index);
5531 printf_filtered (".debug_names:");
5532 if (dwarf2_per_objfile->debug_names_table)
5533 printf_filtered (" exists\n");
5534 else
5535 printf_filtered (" faked for \"readnow\"\n");
5536 printf_filtered ("\n");
9291a0cd
TT
5537}
5538
9291a0cd 5539static void
927aa2e7
JK
5540dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5541 const char *func_name)
9291a0cd 5542{
ed2dc618
SM
5543 struct dwarf2_per_objfile *dwarf2_per_objfile
5544 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5545
927aa2e7
JK
5546 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5547 if (dwarf2_per_objfile->debug_names_table)
24c79950 5548 {
927aa2e7 5549 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5550
2b79f376 5551 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5552
927aa2e7
JK
5553 struct dwarf2_per_cu_data *per_cu;
5554 while ((per_cu = iter.next ()) != NULL)
58f0c718 5555 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5556 }
5557}
24c79950 5558
3b00ef10
TT
5559static void
5560dw2_debug_names_map_matching_symbols
5561 (struct objfile *objfile,
5562 const lookup_name_info &name, domain_enum domain,
5563 int global,
5564 gdb::function_view<symbol_found_callback_ftype> callback,
5565 symbol_compare_ftype *ordered_compare)
5566{
5567 struct dwarf2_per_objfile *dwarf2_per_objfile
5568 = get_dwarf2_per_objfile (objfile);
5569
5570 /* debug_names_table is NULL if OBJF_READNOW. */
5571 if (!dwarf2_per_objfile->debug_names_table)
5572 return;
5573
5574 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5575 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5576
5577 const char *match_name = name.ada ().lookup_name ().c_str ();
5578 auto matcher = [&] (const char *symname)
5579 {
5580 if (ordered_compare == nullptr)
5581 return true;
5582 return ordered_compare (symname, match_name) == 0;
5583 };
5584
5585 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5586 [&] (offset_type namei)
5587 {
5588 /* The name was matched, now expand corresponding CUs that were
5589 marked. */
5590 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5591
5592 struct dwarf2_per_cu_data *per_cu;
5593 while ((per_cu = iter.next ()) != NULL)
5594 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5595 return true;
5596 });
5597
5598 /* It's a shame we couldn't do this inside the
5599 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5600 that have already been expanded. Instead, this loop matches what
5601 the psymtab code does. */
5602 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5603 {
5604 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5605 if (cust != nullptr)
5606 {
5607 const struct block *block
5608 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5609 if (!iterate_over_symbols_terminated (block, name,
5610 domain, callback))
5611 break;
5612 }
5613 }
5614}
5615
927aa2e7
JK
5616static void
5617dw2_debug_names_expand_symtabs_matching
5618 (struct objfile *objfile,
5619 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5620 const lookup_name_info &lookup_name,
5621 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5622 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5623 enum search_domain kind)
5624{
ed2dc618
SM
5625 struct dwarf2_per_objfile *dwarf2_per_objfile
5626 = get_dwarf2_per_objfile (objfile);
9291a0cd 5627
927aa2e7
JK
5628 /* debug_names_table is NULL if OBJF_READNOW. */
5629 if (!dwarf2_per_objfile->debug_names_table)
5630 return;
9291a0cd 5631
ed2dc618 5632 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5633
44ed8f3e 5634 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5635
44ed8f3e
PA
5636 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5637 symbol_matcher,
5638 kind, [&] (offset_type namei)
927aa2e7 5639 {
927aa2e7
JK
5640 /* The name was matched, now expand corresponding CUs that were
5641 marked. */
5642 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5643
927aa2e7
JK
5644 struct dwarf2_per_cu_data *per_cu;
5645 while ((per_cu = iter.next ()) != NULL)
5646 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5647 expansion_notify);
3b00ef10 5648 return true;
44ed8f3e 5649 });
9291a0cd
TT
5650}
5651
927aa2e7 5652const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5653{
5654 dw2_has_symbols,
5655 dw2_find_last_source_symtab,
5656 dw2_forget_cached_source_info,
f8eba3c6 5657 dw2_map_symtabs_matching_filename,
927aa2e7 5658 dw2_debug_names_lookup_symbol,
9291a0cd 5659 dw2_print_stats,
927aa2e7 5660 dw2_debug_names_dump,
927aa2e7 5661 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5662 dw2_expand_all_symtabs,
652a8996 5663 dw2_expand_symtabs_with_fullname,
3b00ef10 5664 dw2_debug_names_map_matching_symbols,
927aa2e7 5665 dw2_debug_names_expand_symtabs_matching,
43f3e411 5666 dw2_find_pc_sect_compunit_symtab,
71a3c369 5667 NULL,
9291a0cd
TT
5668 dw2_map_symbol_filenames
5669};
5670
4485a1c1
SM
5671/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5672 to either a dwarf2_per_objfile or dwz_file object. */
5673
5674template <typename T>
5675static gdb::array_view<const gdb_byte>
5676get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5677{
5678 dwarf2_section_info *section = &section_owner->gdb_index;
5679
96b79293 5680 if (section->empty ())
4485a1c1
SM
5681 return {};
5682
5683 /* Older elfutils strip versions could keep the section in the main
5684 executable while splitting it for the separate debug info file. */
96b79293 5685 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5686 return {};
5687
96b79293 5688 section->read (obj);
4485a1c1 5689
8bebfcda
PA
5690 /* dwarf2_section_info::size is a bfd_size_type, while
5691 gdb::array_view works with size_t. On 32-bit hosts, with
5692 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5693 is 32-bit. So we need an explicit narrowing conversion here.
5694 This is fine, because it's impossible to allocate or mmap an
5695 array/buffer larger than what size_t can represent. */
5696 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5697}
5698
87d6a7aa
SM
5699/* Lookup the index cache for the contents of the index associated to
5700 DWARF2_OBJ. */
5701
5702static gdb::array_view<const gdb_byte>
5703get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5704{
5705 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5706 if (build_id == nullptr)
5707 return {};
5708
5709 return global_index_cache.lookup_gdb_index (build_id,
5710 &dwarf2_obj->index_cache_res);
5711}
5712
5713/* Same as the above, but for DWZ. */
5714
5715static gdb::array_view<const gdb_byte>
5716get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5717{
5718 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5719 if (build_id == nullptr)
5720 return {};
5721
5722 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5723}
5724
3c0aa29a 5725/* See symfile.h. */
9291a0cd 5726
3c0aa29a
PA
5727bool
5728dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5729{
ed2dc618
SM
5730 struct dwarf2_per_objfile *dwarf2_per_objfile
5731 = get_dwarf2_per_objfile (objfile);
5732
9291a0cd
TT
5733 /* If we're about to read full symbols, don't bother with the
5734 indices. In this case we also don't care if some other debug
5735 format is making psymtabs, because they are all about to be
5736 expanded anyway. */
5737 if ((objfile->flags & OBJF_READNOW))
5738 {
9291a0cd 5739 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5740 create_all_comp_units (dwarf2_per_objfile);
5741 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5742 dwarf2_per_objfile->quick_file_names_table
5743 = create_quick_file_names_table
5744 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5745
b76e467d 5746 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5747 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5748 {
ff4c9fec 5749 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5750
e254ef6a
DE
5751 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5752 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5753 }
5754
5755 /* Return 1 so that gdb sees the "quick" functions. However,
5756 these functions will be no-ops because we will have expanded
5757 all symtabs. */
3c0aa29a
PA
5758 *index_kind = dw_index_kind::GDB_INDEX;
5759 return true;
9291a0cd
TT
5760 }
5761
ed2dc618 5762 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5763 {
5764 *index_kind = dw_index_kind::DEBUG_NAMES;
5765 return true;
5766 }
927aa2e7 5767
4485a1c1
SM
5768 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5769 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5770 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5771 {
5772 *index_kind = dw_index_kind::GDB_INDEX;
5773 return true;
5774 }
9291a0cd 5775
87d6a7aa
SM
5776 /* ... otherwise, try to find the index in the index cache. */
5777 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5778 get_gdb_index_contents_from_cache,
5779 get_gdb_index_contents_from_cache_dwz))
5780 {
5781 global_index_cache.hit ();
5782 *index_kind = dw_index_kind::GDB_INDEX;
5783 return true;
5784 }
5785
5786 global_index_cache.miss ();
3c0aa29a 5787 return false;
9291a0cd
TT
5788}
5789
5790\f
5791
dce234bc
PP
5792/* Build a partial symbol table. */
5793
5794void
f29dff0a 5795dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5796{
ed2dc618
SM
5797 struct dwarf2_per_objfile *dwarf2_per_objfile
5798 = get_dwarf2_per_objfile (objfile);
c9bf0622 5799
6eee24ce 5800 init_psymbol_list (objfile, 1024);
c906108c 5801
a70b8144 5802 try
c9bf0622
TT
5803 {
5804 /* This isn't really ideal: all the data we allocate on the
5805 objfile's obstack is still uselessly kept around. However,
5806 freeing it seems unsafe. */
906768f9 5807 psymtab_discarder psymtabs (objfile);
ed2dc618 5808 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5809 psymtabs.keep ();
87d6a7aa
SM
5810
5811 /* (maybe) store an index in the cache. */
5812 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5813 }
230d2906 5814 catch (const gdb_exception_error &except)
492d29ea
PA
5815 {
5816 exception_print (gdb_stderr, except);
5817 }
c906108c 5818}
c906108c 5819
3b80fe9b
DE
5820/* Find the base address of the compilation unit for range lists and
5821 location lists. It will normally be specified by DW_AT_low_pc.
5822 In DWARF-3 draft 4, the base address could be overridden by
5823 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5824 compilation units with discontinuous ranges. */
5825
5826static void
5827dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5828{
5829 struct attribute *attr;
5830
5831 cu->base_known = 0;
5832 cu->base_address = 0;
5833
5834 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5835 if (attr != nullptr)
3b80fe9b 5836 {
cd6c91b4 5837 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5838 cu->base_known = 1;
5839 }
5840 else
5841 {
5842 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5843 if (attr != nullptr)
3b80fe9b 5844 {
cd6c91b4 5845 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5846 cu->base_known = 1;
5847 }
5848 }
5849}
5850
36586728
TT
5851/* Helper function that returns the proper abbrev section for
5852 THIS_CU. */
5853
5854static struct dwarf2_section_info *
5855get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5856{
5857 struct dwarf2_section_info *abbrev;
ed2dc618 5858 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5859
5860 if (this_cu->is_dwz)
ed2dc618 5861 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5862 else
5863 abbrev = &dwarf2_per_objfile->abbrev;
5864
5865 return abbrev;
5866}
5867
f4dc4d17
DE
5868/* Fetch the abbreviation table offset from a comp or type unit header. */
5869
5870static sect_offset
ed2dc618
SM
5871read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5872 struct dwarf2_section_info *section,
9c541725 5873 sect_offset sect_off)
f4dc4d17 5874{
96b79293 5875 bfd *abfd = section->get_bfd_owner ();
d521ce57 5876 const gdb_byte *info_ptr;
ac298888 5877 unsigned int initial_length_size, offset_size;
43988095 5878 uint16_t version;
f4dc4d17 5879
96b79293 5880 section->read (dwarf2_per_objfile->objfile);
9c541725 5881 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5882 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5883 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5884 info_ptr += initial_length_size;
5885
5886 version = read_2_bytes (abfd, info_ptr);
5887 info_ptr += 2;
5888 if (version >= 5)
5889 {
5890 /* Skip unit type and address size. */
5891 info_ptr += 2;
5892 }
5893
24aa364d 5894 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5895}
5896
aaa75496
JB
5897/* Allocate a new partial symtab for file named NAME and mark this new
5898 partial symtab as being an include of PST. */
5899
5900static void
891813be 5901dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5902 struct objfile *objfile)
5903{
891813be 5904 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
aaa75496 5905
fbd9ab74
JK
5906 if (!IS_ABSOLUTE_PATH (subpst->filename))
5907 {
5908 /* It shares objfile->objfile_obstack. */
5909 subpst->dirname = pst->dirname;
5910 }
5911
a9342b62 5912 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5913 subpst->dependencies[0] = pst;
5914 subpst->number_of_dependencies = 1;
5915
aaa75496
JB
5916 /* No private part is necessary for include psymtabs. This property
5917 can be used to differentiate between such include psymtabs and
10b3939b 5918 the regular ones. */
891813be 5919 subpst->per_cu_data = nullptr;
aaa75496
JB
5920}
5921
5922/* Read the Line Number Program data and extract the list of files
5923 included by the source file represented by PST. Build an include
d85a05f0 5924 partial symtab for each of these included files. */
aaa75496
JB
5925
5926static void
5927dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5928 struct die_info *die,
891813be 5929 dwarf2_psymtab *pst)
aaa75496 5930{
fff8551c 5931 line_header_up lh;
d85a05f0 5932 struct attribute *attr;
aaa75496 5933
d85a05f0 5934 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5935 if (attr != nullptr)
9c541725 5936 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5937 if (lh == NULL)
5938 return; /* No linetable, so no includes. */
5939
79748972
TT
5940 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5941 that we pass in the raw text_low here; that is ok because we're
5942 only decoding the line table to make include partial symtabs, and
5943 so the addresses aren't really used. */
4ae976d1 5944 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5945 pst->raw_text_low (), 1);
aaa75496
JB
5946}
5947
348e048f 5948static hashval_t
52dc124a 5949hash_signatured_type (const void *item)
348e048f 5950{
9a3c8263
SM
5951 const struct signatured_type *sig_type
5952 = (const struct signatured_type *) item;
9a619af0 5953
348e048f 5954 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5955 return sig_type->signature;
348e048f
DE
5956}
5957
5958static int
52dc124a 5959eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5960{
9a3c8263
SM
5961 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5962 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5963
348e048f
DE
5964 return lhs->signature == rhs->signature;
5965}
5966
1fd400ff
TT
5967/* Allocate a hash table for signatured types. */
5968
b0b6a987 5969static htab_up
298e9637 5970allocate_signatured_type_table ()
1fd400ff 5971{
b0b6a987
TT
5972 return htab_up (htab_create_alloc (41,
5973 hash_signatured_type,
5974 eq_signatured_type,
5975 NULL, xcalloc, xfree));
1fd400ff
TT
5976}
5977
d467dd73 5978/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
5979
5980static int
d467dd73 5981add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 5982{
9a3c8263 5983 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
5984 std::vector<signatured_type *> *all_type_units
5985 = (std::vector<signatured_type *> *) datum;
1fd400ff 5986
b2bdb8cf 5987 all_type_units->push_back (sigt);
1fd400ff
TT
5988
5989 return 1;
5990}
5991
78d4d2c5 5992/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5993 and fill them into TYPES_HTAB. It will process only type units,
5994 therefore DW_UT_type. */
c88ee1f0 5995
78d4d2c5 5996static void
ed2dc618
SM
5997create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5998 struct dwo_file *dwo_file,
b0b6a987 5999 dwarf2_section_info *section, htab_up &types_htab,
43988095 6000 rcuh_kind section_kind)
348e048f 6001{
3019eac3 6002 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6003 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6004 bfd *abfd;
6005 const gdb_byte *info_ptr, *end_ptr;
348e048f 6006
4bdcc0c1
DE
6007 abbrev_section = (dwo_file != NULL
6008 ? &dwo_file->sections.abbrev
6009 : &dwarf2_per_objfile->abbrev);
6010
b4f54984 6011 if (dwarf_read_debug)
43988095 6012 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6013 section->get_name (),
6014 abbrev_section->get_file_name ());
09406207 6015
96b79293 6016 section->read (objfile);
78d4d2c5 6017 info_ptr = section->buffer;
348e048f 6018
78d4d2c5
JK
6019 if (info_ptr == NULL)
6020 return;
348e048f 6021
78d4d2c5
JK
6022 /* We can't set abfd until now because the section may be empty or
6023 not present, in which case the bfd is unknown. */
96b79293 6024 abfd = section->get_bfd_owner ();
348e048f 6025
c0ab21c2
TT
6026 /* We don't use cutu_reader here because we don't need to read
6027 any dies: the signature is in the header. */
3019eac3 6028
78d4d2c5
JK
6029 end_ptr = info_ptr + section->size;
6030 while (info_ptr < end_ptr)
6031 {
78d4d2c5
JK
6032 struct signatured_type *sig_type;
6033 struct dwo_unit *dwo_tu;
6034 void **slot;
6035 const gdb_byte *ptr = info_ptr;
6036 struct comp_unit_head header;
6037 unsigned int length;
8b70b953 6038
9c541725 6039 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6040
a49dd8dd
JK
6041 /* Initialize it due to a false compiler warning. */
6042 header.signature = -1;
9c541725 6043 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6044
78d4d2c5
JK
6045 /* We need to read the type's signature in order to build the hash
6046 table, but we don't need anything else just yet. */
348e048f 6047
ed2dc618 6048 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6049 abbrev_section, ptr, section_kind);
348e048f 6050
4057dfde 6051 length = header.get_length ();
6caca83c 6052
78d4d2c5
JK
6053 /* Skip dummy type units. */
6054 if (ptr >= info_ptr + length
43988095
JK
6055 || peek_abbrev_code (abfd, ptr) == 0
6056 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6057 {
6058 info_ptr += length;
6059 continue;
6060 }
dee91e82 6061
78d4d2c5
JK
6062 if (types_htab == NULL)
6063 {
6064 if (dwo_file)
298e9637 6065 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6066 else
298e9637 6067 types_htab = allocate_signatured_type_table ();
78d4d2c5 6068 }
8b70b953 6069
78d4d2c5
JK
6070 if (dwo_file)
6071 {
6072 sig_type = NULL;
6073 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6074 struct dwo_unit);
6075 dwo_tu->dwo_file = dwo_file;
43988095 6076 dwo_tu->signature = header.signature;
9c541725 6077 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6078 dwo_tu->section = section;
9c541725 6079 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6080 dwo_tu->length = length;
6081 }
6082 else
6083 {
6084 /* N.B.: type_offset is not usable if this type uses a DWO file.
6085 The real type_offset is in the DWO file. */
6086 dwo_tu = NULL;
6087 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6088 struct signatured_type);
43988095 6089 sig_type->signature = header.signature;
9c541725 6090 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6091 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6092 sig_type->per_cu.is_debug_types = 1;
6093 sig_type->per_cu.section = section;
9c541725 6094 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6095 sig_type->per_cu.length = length;
6096 }
6097
b0b6a987 6098 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6099 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6100 INSERT);
6101 gdb_assert (slot != NULL);
6102 if (*slot != NULL)
6103 {
9c541725 6104 sect_offset dup_sect_off;
0349ea22 6105
3019eac3
DE
6106 if (dwo_file)
6107 {
78d4d2c5
JK
6108 const struct dwo_unit *dup_tu
6109 = (const struct dwo_unit *) *slot;
6110
9c541725 6111 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6112 }
6113 else
6114 {
78d4d2c5
JK
6115 const struct signatured_type *dup_tu
6116 = (const struct signatured_type *) *slot;
6117
9c541725 6118 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6119 }
8b70b953 6120
b98664d3 6121 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6122 " the entry at offset %s, signature %s"),
6123 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6124 hex_string (header.signature));
78d4d2c5
JK
6125 }
6126 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6127
78d4d2c5 6128 if (dwarf_read_debug > 1)
9d8780f0
SM
6129 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6130 sect_offset_str (sect_off),
43988095 6131 hex_string (header.signature));
3019eac3 6132
78d4d2c5
JK
6133 info_ptr += length;
6134 }
6135}
3019eac3 6136
78d4d2c5
JK
6137/* Create the hash table of all entries in the .debug_types
6138 (or .debug_types.dwo) section(s).
6139 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6140 otherwise it is NULL.
b3c8eb43 6141
78d4d2c5 6142 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6143
78d4d2c5 6144 Note: This function processes DWO files only, not DWP files. */
348e048f 6145
78d4d2c5 6146static void
ed2dc618
SM
6147create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6148 struct dwo_file *dwo_file,
fd5866f6 6149 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6150 htab_up &types_htab)
78d4d2c5 6151{
fd5866f6
SM
6152 for (dwarf2_section_info &section : type_sections)
6153 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6154 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6155}
6156
6157/* Create the hash table of all entries in the .debug_types section,
6158 and initialize all_type_units.
6159 The result is zero if there is an error (e.g. missing .debug_types section),
6160 otherwise non-zero. */
6161
6162static int
ed2dc618 6163create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6164{
b0b6a987 6165 htab_up types_htab;
3019eac3 6166
ed2dc618
SM
6167 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6168 &dwarf2_per_objfile->info, types_htab,
43988095 6169 rcuh_kind::COMPILE);
ed2dc618
SM
6170 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6171 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6172 if (types_htab == NULL)
6173 {
6174 dwarf2_per_objfile->signatured_types = NULL;
6175 return 0;
6176 }
6177
b0b6a987 6178 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6179
b2bdb8cf 6180 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6181 dwarf2_per_objfile->all_type_units.reserve
6182 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6183
b0b6a987
TT
6184 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6185 add_signatured_type_cu_to_table,
b2bdb8cf 6186 &dwarf2_per_objfile->all_type_units);
1fd400ff 6187
348e048f
DE
6188 return 1;
6189}
6190
6aa5f3a6
DE
6191/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6192 If SLOT is non-NULL, it is the entry to use in the hash table.
6193 Otherwise we find one. */
6194
6195static struct signatured_type *
ed2dc618
SM
6196add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6197 void **slot)
6aa5f3a6
DE
6198{
6199 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6200
b2bdb8cf
SM
6201 if (dwarf2_per_objfile->all_type_units.size ()
6202 == dwarf2_per_objfile->all_type_units.capacity ())
6203 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6204
b2bdb8cf
SM
6205 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6206 struct signatured_type);
6207
6208 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6209 sig_type->signature = sig;
6210 sig_type->per_cu.is_debug_types = 1;
6211 if (dwarf2_per_objfile->using_index)
6212 {
6213 sig_type->per_cu.v.quick =
6214 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6215 struct dwarf2_per_cu_quick_data);
6216 }
6217
6218 if (slot == NULL)
6219 {
b0b6a987 6220 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6221 sig_type, INSERT);
6222 }
6223 gdb_assert (*slot == NULL);
6224 *slot = sig_type;
6225 /* The rest of sig_type must be filled in by the caller. */
6226 return sig_type;
6227}
6228
a2ce51a0
DE
6229/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6230 Fill in SIG_ENTRY with DWO_ENTRY. */
6231
6232static void
ed2dc618 6233fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6234 struct signatured_type *sig_entry,
6235 struct dwo_unit *dwo_entry)
6236{
7ee85ab1 6237 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6238 gdb_assert (! sig_entry->per_cu.queued);
6239 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6240 if (dwarf2_per_objfile->using_index)
6241 {
6242 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6243 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6244 }
6245 else
6246 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6247 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6248 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6249 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6250 gdb_assert (sig_entry->dwo_unit == NULL);
6251
6252 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6253 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6254 sig_entry->per_cu.length = dwo_entry->length;
6255 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6256 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6257 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6258 sig_entry->dwo_unit = dwo_entry;
6259}
6260
6261/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6262 If we haven't read the TU yet, create the signatured_type data structure
6263 for a TU to be read in directly from a DWO file, bypassing the stub.
6264 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6265 using .gdb_index, then when reading a CU we want to stay in the DWO file
6266 containing that CU. Otherwise we could end up reading several other DWO
6267 files (due to comdat folding) to process the transitive closure of all the
6268 mentioned TUs, and that can be slow. The current DWO file will have every
6269 type signature that it needs.
a2ce51a0
DE
6270 We only do this for .gdb_index because in the psymtab case we already have
6271 to read all the DWOs to build the type unit groups. */
6272
6273static struct signatured_type *
6274lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6275{
518817b3
SM
6276 struct dwarf2_per_objfile *dwarf2_per_objfile
6277 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6278 struct dwo_file *dwo_file;
6279 struct dwo_unit find_dwo_entry, *dwo_entry;
6280 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6281 void **slot;
a2ce51a0
DE
6282
6283 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6284
6aa5f3a6
DE
6285 /* If TU skeletons have been removed then we may not have read in any
6286 TUs yet. */
6287 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6288 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6289
6290 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6291 Use the global signatured_types array to do our own comdat-folding
6292 of types. If this is the first time we're reading this TU, and
6293 the TU has an entry in .gdb_index, replace the recorded data from
6294 .gdb_index with this TU. */
a2ce51a0 6295
a2ce51a0 6296 find_sig_entry.signature = sig;
b0b6a987 6297 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6298 &find_sig_entry, INSERT);
9a3c8263 6299 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6300
6301 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6302 read. Don't reassign the global entry to point to this DWO if that's
6303 the case. Also note that if the TU is already being read, it may not
6304 have come from a DWO, the program may be a mix of Fission-compiled
6305 code and non-Fission-compiled code. */
6306
6307 /* Have we already tried to read this TU?
6308 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6309 needn't exist in the global table yet). */
6310 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6311 return sig_entry;
6312
6aa5f3a6
DE
6313 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6314 dwo_unit of the TU itself. */
6315 dwo_file = cu->dwo_unit->dwo_file;
6316
a2ce51a0
DE
6317 /* Ok, this is the first time we're reading this TU. */
6318 if (dwo_file->tus == NULL)
6319 return NULL;
6320 find_dwo_entry.signature = sig;
b0b6a987
TT
6321 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6322 &find_dwo_entry);
a2ce51a0
DE
6323 if (dwo_entry == NULL)
6324 return NULL;
6325
6aa5f3a6
DE
6326 /* If the global table doesn't have an entry for this TU, add one. */
6327 if (sig_entry == NULL)
ed2dc618 6328 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6329
ed2dc618 6330 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6331 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6332 return sig_entry;
6333}
6334
a2ce51a0
DE
6335/* Subroutine of lookup_signatured_type.
6336 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6337 then try the DWP file. If the TU stub (skeleton) has been removed then
6338 it won't be in .gdb_index. */
a2ce51a0
DE
6339
6340static struct signatured_type *
6341lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6342{
518817b3
SM
6343 struct dwarf2_per_objfile *dwarf2_per_objfile
6344 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6345 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6346 struct dwo_unit *dwo_entry;
6347 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6348 void **slot;
a2ce51a0
DE
6349
6350 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6351 gdb_assert (dwp_file != NULL);
6352
6aa5f3a6
DE
6353 /* If TU skeletons have been removed then we may not have read in any
6354 TUs yet. */
6355 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6356 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6357
6aa5f3a6 6358 find_sig_entry.signature = sig;
b0b6a987 6359 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6360 &find_sig_entry, INSERT);
9a3c8263 6361 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6362
6363 /* Have we already tried to read this TU?
6364 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6365 needn't exist in the global table yet). */
6366 if (sig_entry != NULL)
6367 return sig_entry;
6368
a2ce51a0
DE
6369 if (dwp_file->tus == NULL)
6370 return NULL;
ed2dc618 6371 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6372 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6373 if (dwo_entry == NULL)
6374 return NULL;
6375
ed2dc618
SM
6376 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6377 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6378
a2ce51a0
DE
6379 return sig_entry;
6380}
6381
380bca97 6382/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6383 Returns NULL if signature SIG is not present in the table.
6384 It is up to the caller to complain about this. */
348e048f
DE
6385
6386static struct signatured_type *
a2ce51a0 6387lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6388{
518817b3
SM
6389 struct dwarf2_per_objfile *dwarf2_per_objfile
6390 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6391
a2ce51a0
DE
6392 if (cu->dwo_unit
6393 && dwarf2_per_objfile->using_index)
6394 {
6395 /* We're in a DWO/DWP file, and we're using .gdb_index.
6396 These cases require special processing. */
ed2dc618 6397 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6398 return lookup_dwo_signatured_type (cu, sig);
6399 else
6400 return lookup_dwp_signatured_type (cu, sig);
6401 }
6402 else
6403 {
6404 struct signatured_type find_entry, *entry;
348e048f 6405
a2ce51a0
DE
6406 if (dwarf2_per_objfile->signatured_types == NULL)
6407 return NULL;
6408 find_entry.signature = sig;
9a3c8263 6409 entry = ((struct signatured_type *)
b0b6a987
TT
6410 htab_find (dwarf2_per_objfile->signatured_types.get (),
6411 &find_entry));
a2ce51a0
DE
6412 return entry;
6413 }
348e048f 6414}
18a8505e
AT
6415
6416/* Return the address base of the compile unit, which, if exists, is stored
6417 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6418static gdb::optional<ULONGEST>
6419lookup_addr_base (struct die_info *comp_unit_die)
6420{
6421 struct attribute *attr;
6422 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6423 if (attr == nullptr)
6424 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6425 if (attr == nullptr)
6426 return gdb::optional<ULONGEST> ();
6427 return DW_UNSND (attr);
6428}
6429
6430/* Return range lists base of the compile unit, which, if exists, is stored
6431 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6432static ULONGEST
6433lookup_ranges_base (struct die_info *comp_unit_die)
6434{
6435 struct attribute *attr;
6436 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6437 if (attr == nullptr)
6438 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6439 if (attr == nullptr)
6440 return 0;
6441 return DW_UNSND (attr);
6442}
6443
42e7ad6c 6444/* Low level DIE reading support. */
348e048f 6445
d85a05f0
DJ
6446/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6447
6448static void
6449init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6450 struct dwarf2_cu *cu,
3019eac3 6451 struct dwarf2_section_info *section,
685af9cd
TT
6452 struct dwo_file *dwo_file,
6453 struct abbrev_table *abbrev_table)
d85a05f0 6454{
fceca515 6455 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6456 reader->abfd = section->get_bfd_owner ();
d85a05f0 6457 reader->cu = cu;
3019eac3 6458 reader->dwo_file = dwo_file;
dee91e82
DE
6459 reader->die_section = section;
6460 reader->buffer = section->buffer;
f664829e 6461 reader->buffer_end = section->buffer + section->size;
685af9cd 6462 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6463}
6464
c0ab21c2 6465/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6466 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6467 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6468 already.
6469
6470 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6471 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6472 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6473 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6474 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6475 STUB_COMP_DIR may be non-NULL.
3e225074 6476 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6477 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6478 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6479 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6480 kept around for at least as long as *RESULT_READER.
6481
b0c7bfa9
DE
6482 The result is non-zero if a valid (non-dummy) DIE was found. */
6483
6484static int
6485read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6486 struct dwo_unit *dwo_unit,
b0c7bfa9 6487 struct die_info *stub_comp_unit_die,
a2ce51a0 6488 const char *stub_comp_dir,
b0c7bfa9 6489 struct die_reader_specs *result_reader,
d521ce57 6490 const gdb_byte **result_info_ptr,
b0c7bfa9 6491 struct die_info **result_comp_unit_die,
685af9cd 6492 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6493{
ed2dc618 6494 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6495 struct objfile *objfile = dwarf2_per_objfile->objfile;
6496 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6497 bfd *abfd;
d521ce57 6498 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6499 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6500 int i,num_extra_attrs;
6501 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6502 struct die_info *comp_unit_die;
6503
b0aeadb3
DE
6504 /* At most one of these may be provided. */
6505 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6506
b0c7bfa9
DE
6507 /* These attributes aren't processed until later:
6508 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6509 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6510 referenced later. However, these attributes are found in the stub
6511 which we won't have later. In order to not impose this complication
6512 on the rest of the code, we read them here and copy them to the
6513 DWO CU/TU die. */
b0c7bfa9
DE
6514
6515 stmt_list = NULL;
6516 low_pc = NULL;
6517 high_pc = NULL;
6518 ranges = NULL;
6519 comp_dir = NULL;
6520
6521 if (stub_comp_unit_die != NULL)
6522 {
6523 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6524 DWO file. */
6525 if (! this_cu->is_debug_types)
6526 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6527 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6528 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6529 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6530 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6531
18a8505e 6532 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6533
18a8505e
AT
6534 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6535 here (if needed). We need the value before we can process
6536 DW_AT_ranges. */
6537 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6538 }
a2ce51a0
DE
6539 else if (stub_comp_dir != NULL)
6540 {
6541 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6542 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6543 comp_dir->name = DW_AT_comp_dir;
6544 comp_dir->form = DW_FORM_string;
6545 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6546 DW_STRING (comp_dir) = stub_comp_dir;
6547 }
b0c7bfa9
DE
6548
6549 /* Set up for reading the DWO CU/TU. */
6550 cu->dwo_unit = dwo_unit;
685af9cd 6551 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6552 section->read (objfile);
6553 abfd = section->get_bfd_owner ();
9c541725
PA
6554 begin_info_ptr = info_ptr = (section->buffer
6555 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6556 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6557
6558 if (this_cu->is_debug_types)
6559 {
b0c7bfa9
DE
6560 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6561
ed2dc618
SM
6562 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6563 &cu->header, section,
b0c7bfa9 6564 dwo_abbrev_section,
43988095 6565 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6566 /* This is not an assert because it can be caused by bad debug info. */
43988095 6567 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6568 {
6569 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6570 " TU at offset %s [in module %s]"),
a2ce51a0 6571 hex_string (sig_type->signature),
43988095 6572 hex_string (cu->header.signature),
9d8780f0 6573 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6574 bfd_get_filename (abfd));
6575 }
9c541725 6576 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6577 /* For DWOs coming from DWP files, we don't know the CU length
6578 nor the type's offset in the TU until now. */
4057dfde 6579 dwo_unit->length = cu->header.get_length ();
9c541725 6580 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6581
6582 /* Establish the type offset that can be used to lookup the type.
6583 For DWO files, we don't know it until now. */
9c541725
PA
6584 sig_type->type_offset_in_section
6585 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6586 }
6587 else
6588 {
ed2dc618
SM
6589 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6590 &cu->header, section,
b0c7bfa9 6591 dwo_abbrev_section,
43988095 6592 info_ptr, rcuh_kind::COMPILE);
9c541725 6593 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6594 /* For DWOs coming from DWP files, we don't know the CU length
6595 until now. */
4057dfde 6596 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6597 }
6598
685af9cd 6599 *result_dwo_abbrev_table
86de1d91
TT
6600 = abbrev_table::read (objfile, dwo_abbrev_section,
6601 cu->header.abbrev_sect_off);
685af9cd
TT
6602 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6603 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6604
6605 /* Read in the die, but leave space to copy over the attributes
6606 from the stub. This has the benefit of simplifying the rest of
6607 the code - all the work to maintain the illusion of a single
6608 DW_TAG_{compile,type}_unit DIE is done here. */
6609 num_extra_attrs = ((stmt_list != NULL)
6610 + (low_pc != NULL)
6611 + (high_pc != NULL)
6612 + (ranges != NULL)
6613 + (comp_dir != NULL));
6614 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6615 num_extra_attrs);
b0c7bfa9
DE
6616
6617 /* Copy over the attributes from the stub to the DIE we just read in. */
6618 comp_unit_die = *result_comp_unit_die;
6619 i = comp_unit_die->num_attrs;
6620 if (stmt_list != NULL)
6621 comp_unit_die->attrs[i++] = *stmt_list;
6622 if (low_pc != NULL)
6623 comp_unit_die->attrs[i++] = *low_pc;
6624 if (high_pc != NULL)
6625 comp_unit_die->attrs[i++] = *high_pc;
6626 if (ranges != NULL)
6627 comp_unit_die->attrs[i++] = *ranges;
6628 if (comp_dir != NULL)
6629 comp_unit_die->attrs[i++] = *comp_dir;
6630 comp_unit_die->num_attrs += num_extra_attrs;
6631
b4f54984 6632 if (dwarf_die_debug)
bf6af496
DE
6633 {
6634 fprintf_unfiltered (gdb_stdlog,
6635 "Read die from %s@0x%x of %s:\n",
96b79293 6636 section->get_name (),
bf6af496
DE
6637 (unsigned) (begin_info_ptr - section->buffer),
6638 bfd_get_filename (abfd));
b4f54984 6639 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6640 }
6641
b0c7bfa9
DE
6642 /* Skip dummy compilation units. */
6643 if (info_ptr >= begin_info_ptr + dwo_unit->length
6644 || peek_abbrev_code (abfd, info_ptr) == 0)
6645 return 0;
6646
6647 *result_info_ptr = info_ptr;
6648 return 1;
6649}
6650
a084a2a6
AT
6651/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6652 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6653 signature is part of the header. */
6654static gdb::optional<ULONGEST>
6655lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6656{
6657 if (cu->header.version >= 5)
6658 return cu->header.signature;
6659 struct attribute *attr;
6660 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6661 if (attr == nullptr)
6662 return gdb::optional<ULONGEST> ();
6663 return DW_UNSND (attr);
6664}
6665
c0ab21c2 6666/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6667 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6668 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6669
6670static struct dwo_unit *
6671lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6672 struct die_info *comp_unit_die,
6673 const char *dwo_name)
b0c7bfa9
DE
6674{
6675 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6676 struct dwo_unit *dwo_unit;
c0ab21c2 6677 const char *comp_dir;
b0c7bfa9 6678
a2ce51a0
DE
6679 gdb_assert (cu != NULL);
6680
b0c7bfa9 6681 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6682 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6683 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6684
6685 if (this_cu->is_debug_types)
6686 {
6687 struct signatured_type *sig_type;
6688
6689 /* Since this_cu is the first member of struct signatured_type,
6690 we can go from a pointer to one to a pointer to the other. */
6691 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6692 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6693 }
6694 else
6695 {
a084a2a6
AT
6696 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6697 if (!signature.has_value ())
b0c7bfa9
DE
6698 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6699 " [in module %s]"),
e3b94546 6700 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6701 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6702 *signature);
b0c7bfa9
DE
6703 }
6704
b0c7bfa9
DE
6705 return dwo_unit;
6706}
6707
c0ab21c2 6708/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6709 See it for a description of the parameters.
fcd3b13d 6710 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6711
c0ab21c2
TT
6712void
6713cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6714 int use_existing_cu)
a2ce51a0 6715{
a2ce51a0 6716 struct signatured_type *sig_type;
a2ce51a0 6717 struct die_reader_specs reader;
a2ce51a0
DE
6718
6719 /* Verify we can do the following downcast, and that we have the
6720 data we need. */
6721 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6722 sig_type = (struct signatured_type *) this_cu;
6723 gdb_assert (sig_type->dwo_unit != NULL);
6724
6aa5f3a6
DE
6725 if (use_existing_cu && this_cu->cu != NULL)
6726 {
6727 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6728 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6729 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6730 }
6731 else
6732 {
6733 /* If !use_existing_cu, this_cu->cu must be NULL. */
6734 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6735 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6736 }
6737
6738 /* A future optimization, if needed, would be to use an existing
6739 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6740 could share abbrev tables. */
a2ce51a0
DE
6741
6742 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6743 NULL /* stub_comp_unit_die */,
6744 sig_type->dwo_unit->dwo_file->comp_dir,
6745 &reader, &info_ptr,
3e225074 6746 &comp_unit_die,
c0ab21c2 6747 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6748 {
6749 /* Dummy die. */
c0ab21c2 6750 dummy_p = true;
a2ce51a0 6751 }
a2ce51a0
DE
6752}
6753
fd820528 6754/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6755 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6756
f4dc4d17
DE
6757 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6758 Otherwise the table specified in the comp unit header is read in and used.
6759 This is an optimization for when we already have the abbrev table.
6760
dee91e82 6761 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6762 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6763
c0ab21c2
TT
6764cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6765 struct abbrev_table *abbrev_table,
6751ebae 6766 int use_existing_cu,
c0ab21c2
TT
6767 bool skip_partial)
6768 : die_reader_specs {},
6751ebae 6769 m_this_cu (this_cu)
c906108c 6770{
ed2dc618 6771 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6772 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6773 struct dwarf2_section_info *section = this_cu->section;
96b79293 6774 bfd *abfd = section->get_bfd_owner ();
dee91e82 6775 struct dwarf2_cu *cu;
c0ab21c2 6776 const gdb_byte *begin_info_ptr;
dee91e82 6777 struct signatured_type *sig_type = NULL;
4bdcc0c1 6778 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6779 /* Non-zero if CU currently points to a DWO file and we need to
6780 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6781 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6782 int rereading_dwo_cu = 0;
c906108c 6783
b4f54984 6784 if (dwarf_die_debug)
9d8780f0 6785 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6786 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6787 sect_offset_str (this_cu->sect_off));
09406207 6788
a2ce51a0
DE
6789 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6790 file (instead of going through the stub), short-circuit all of this. */
6791 if (this_cu->reading_dwo_directly)
6792 {
6793 /* Narrow down the scope of possibilities to have to understand. */
6794 gdb_assert (this_cu->is_debug_types);
6795 gdb_assert (abbrev_table == NULL);
6751ebae 6796 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6797 return;
6798 }
6799
dee91e82 6800 /* This is cheap if the section is already read in. */
96b79293 6801 section->read (objfile);
dee91e82 6802
9c541725 6803 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6804
6805 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6806
6807 if (use_existing_cu && this_cu->cu != NULL)
6808 {
6809 cu = this_cu->cu;
42e7ad6c
DE
6810 /* If this CU is from a DWO file we need to start over, we need to
6811 refetch the attributes from the skeleton CU.
6812 This could be optimized by retrieving those attributes from when we
6813 were here the first time: the previous comp_unit_die was stored in
6814 comp_unit_obstack. But there's no data yet that we need this
6815 optimization. */
6816 if (cu->dwo_unit != NULL)
6817 rereading_dwo_cu = 1;
dee91e82
DE
6818 }
6819 else
6820 {
6821 /* If !use_existing_cu, this_cu->cu must be NULL. */
6822 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6823 m_new_cu.reset (new dwarf2_cu (this_cu));
6824 cu = m_new_cu.get ();
42e7ad6c 6825 }
dee91e82 6826
b0c7bfa9 6827 /* Get the header. */
9c541725 6828 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6829 {
6830 /* We already have the header, there's no need to read it in again. */
9c541725 6831 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6832 }
6833 else
6834 {
3019eac3 6835 if (this_cu->is_debug_types)
dee91e82 6836 {
ed2dc618
SM
6837 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6838 &cu->header, section,
4bdcc0c1 6839 abbrev_section, info_ptr,
43988095 6840 rcuh_kind::TYPE);
dee91e82 6841
42e7ad6c
DE
6842 /* Since per_cu is the first member of struct signatured_type,
6843 we can go from a pointer to one to a pointer to the other. */
6844 sig_type = (struct signatured_type *) this_cu;
43988095 6845 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6846 gdb_assert (sig_type->type_offset_in_tu
6847 == cu->header.type_cu_offset_in_tu);
6848 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6849
42e7ad6c
DE
6850 /* LENGTH has not been set yet for type units if we're
6851 using .gdb_index. */
4057dfde 6852 this_cu->length = cu->header.get_length ();
3019eac3
DE
6853
6854 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6855 sig_type->type_offset_in_section =
6856 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6857
6858 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6859 }
6860 else
6861 {
ed2dc618
SM
6862 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6863 &cu->header, section,
4bdcc0c1 6864 abbrev_section,
43988095
JK
6865 info_ptr,
6866 rcuh_kind::COMPILE);
dee91e82 6867
9c541725 6868 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6869 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6870 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6871 }
6872 }
10b3939b 6873
6caca83c 6874 /* Skip dummy compilation units. */
dee91e82 6875 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6876 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6877 {
6878 dummy_p = true;
6879 return;
6880 }
6caca83c 6881
433df2d4
DE
6882 /* If we don't have them yet, read the abbrevs for this compilation unit.
6883 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6884 done. */
f4dc4d17 6885 if (abbrev_table != NULL)
685af9cd
TT
6886 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6887 else
f4dc4d17 6888 {
c0ab21c2 6889 m_abbrev_table_holder
86de1d91
TT
6890 = abbrev_table::read (objfile, abbrev_section,
6891 cu->header.abbrev_sect_off);
c0ab21c2 6892 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6893 }
af703f96 6894
dee91e82 6895 /* Read the top level CU/TU die. */
c0ab21c2 6896 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6897 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6898
58f0c718 6899 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6900 {
6901 dummy_p = true;
6902 return;
6903 }
58f0c718 6904
b0c7bfa9 6905 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6906 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6907 table from the DWO file and pass the ownership over to us. It will be
6908 referenced from READER, so we must make sure to free it after we're done
6909 with READER.
6910
b0c7bfa9
DE
6911 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6912 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6913 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6914 if (dwo_name != nullptr)
3019eac3 6915 {
3019eac3 6916 struct dwo_unit *dwo_unit;
b0c7bfa9 6917 struct die_info *dwo_comp_unit_die;
3019eac3 6918
3e225074 6919 if (comp_unit_die->has_children)
6a506a2d 6920 {
b98664d3 6921 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6922 " has children (offset %s) [in module %s]"),
6923 sect_offset_str (this_cu->sect_off),
6924 bfd_get_filename (abfd));
6a506a2d 6925 }
c0ab21c2 6926 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6927 if (dwo_unit != NULL)
3019eac3 6928 {
6a506a2d 6929 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6930 comp_unit_die, NULL,
c0ab21c2 6931 this, &info_ptr,
3e225074 6932 &dwo_comp_unit_die,
c0ab21c2 6933 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6934 {
6935 /* Dummy die. */
c0ab21c2 6936 dummy_p = true;
6a506a2d
DE
6937 return;
6938 }
6939 comp_unit_die = dwo_comp_unit_die;
6940 }
6941 else
6942 {
6943 /* Yikes, we couldn't find the rest of the DIE, we only have
6944 the stub. A complaint has already been logged. There's
6945 not much more we can do except pass on the stub DIE to
6946 die_reader_func. We don't want to throw an error on bad
6947 debug info. */
3019eac3
DE
6948 }
6949 }
c0ab21c2 6950}
3019eac3 6951
6751ebae
TT
6952void
6953cutu_reader::keep ()
c0ab21c2 6954{
b0c7bfa9 6955 /* Done, clean up. */
6751ebae
TT
6956 gdb_assert (!dummy_p);
6957 if (m_new_cu != NULL)
348e048f 6958 {
c0ab21c2
TT
6959 struct dwarf2_per_objfile *dwarf2_per_objfile
6960 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 6961 /* Link this CU into read_in_chain. */
c0ab21c2
TT
6962 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6963 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 6964 /* The chain owns it now. */
c0ab21c2 6965 m_new_cu.release ();
348e048f 6966 }
dee91e82
DE
6967}
6968
18a8505e
AT
6969/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6970 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6971 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6972
6973 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6974 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6975
6976 We fill in THIS_CU->length.
6977
dee91e82 6978 THIS_CU->cu is always freed when done.
3019eac3 6979 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6980 to care whether it refers to the "main" CU or the DWO CU.
6981
6982 When parent_cu is passed, it is used to provide a default value for
6983 str_offsets_base and addr_base from the parent. */
dee91e82 6984
c0ab21c2
TT
6985cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6986 struct dwarf2_cu *parent_cu,
6987 struct dwo_file *dwo_file)
6988 : die_reader_specs {},
6989 m_this_cu (this_cu)
dee91e82 6990{
ed2dc618 6991 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6992 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6993 struct dwarf2_section_info *section = this_cu->section;
96b79293 6994 bfd *abfd = section->get_bfd_owner ();
33e80786 6995 struct dwarf2_section_info *abbrev_section;
d521ce57 6996 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6997
b4f54984 6998 if (dwarf_die_debug)
9d8780f0 6999 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7000 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7001 sect_offset_str (this_cu->sect_off));
09406207 7002
dee91e82
DE
7003 gdb_assert (this_cu->cu == NULL);
7004
33e80786
DE
7005 abbrev_section = (dwo_file != NULL
7006 ? &dwo_file->sections.abbrev
7007 : get_abbrev_section_for_cu (this_cu));
7008
dee91e82 7009 /* This is cheap if the section is already read in. */
96b79293 7010 section->read (objfile);
dee91e82 7011
c0ab21c2 7012 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7013
9c541725 7014 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7015 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7016 &m_new_cu->header, section,
4bdcc0c1 7017 abbrev_section, info_ptr,
43988095
JK
7018 (this_cu->is_debug_types
7019 ? rcuh_kind::TYPE
7020 : rcuh_kind::COMPILE));
dee91e82 7021
18a8505e
AT
7022 if (parent_cu != nullptr)
7023 {
c0ab21c2
TT
7024 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7025 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7026 }
4057dfde 7027 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7028
7029 /* Skip dummy compilation units. */
7030 if (info_ptr >= begin_info_ptr + this_cu->length
7031 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7032 {
7033 dummy_p = true;
7034 return;
7035 }
72bf9492 7036
c0ab21c2 7037 m_abbrev_table_holder
86de1d91
TT
7038 = abbrev_table::read (objfile, abbrev_section,
7039 m_new_cu->header.abbrev_sect_off);
dee91e82 7040
c0ab21c2
TT
7041 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7042 m_abbrev_table_holder.get ());
3e225074 7043 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7044}
7045
0018ea6f
DE
7046\f
7047/* Type Unit Groups.
dee91e82 7048
0018ea6f
DE
7049 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7050 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7051 so that all types coming from the same compilation (.o file) are grouped
7052 together. A future step could be to put the types in the same symtab as
7053 the CU the types ultimately came from. */
ff013f42 7054
f4dc4d17
DE
7055static hashval_t
7056hash_type_unit_group (const void *item)
7057{
9a3c8263
SM
7058 const struct type_unit_group *tu_group
7059 = (const struct type_unit_group *) item;
f4dc4d17 7060
094b34ac 7061 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7062}
348e048f
DE
7063
7064static int
f4dc4d17 7065eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7066{
9a3c8263
SM
7067 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7068 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7069
094b34ac 7070 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7071}
348e048f 7072
f4dc4d17
DE
7073/* Allocate a hash table for type unit groups. */
7074
eaa5fa8b 7075static htab_up
298e9637 7076allocate_type_unit_groups_table ()
f4dc4d17 7077{
eaa5fa8b
TT
7078 return htab_up (htab_create_alloc (3,
7079 hash_type_unit_group,
7080 eq_type_unit_group,
7081 NULL, xcalloc, xfree));
f4dc4d17 7082}
dee91e82 7083
f4dc4d17
DE
7084/* Type units that don't have DW_AT_stmt_list are grouped into their own
7085 partial symtabs. We combine several TUs per psymtab to not let the size
7086 of any one psymtab grow too big. */
7087#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7088#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7089
094b34ac 7090/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7091 Create the type_unit_group object used to hold one or more TUs. */
7092
7093static struct type_unit_group *
094b34ac 7094create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7095{
518817b3
SM
7096 struct dwarf2_per_objfile *dwarf2_per_objfile
7097 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7098 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7099 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7100 struct type_unit_group *tu_group;
f4dc4d17
DE
7101
7102 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7103 struct type_unit_group);
094b34ac 7104 per_cu = &tu_group->per_cu;
518817b3 7105 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7106
094b34ac
DE
7107 if (dwarf2_per_objfile->using_index)
7108 {
7109 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7110 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7111 }
7112 else
7113 {
9c541725 7114 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7115 dwarf2_psymtab *pst;
528e1572 7116 std::string name;
094b34ac
DE
7117
7118 /* Give the symtab a useful name for debug purposes. */
7119 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7120 name = string_printf ("<type_units_%d>",
7121 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7122 else
528e1572 7123 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7124
528e1572 7125 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7126 pst->anonymous = true;
094b34ac 7127 }
f4dc4d17 7128
094b34ac 7129 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7130 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7131
7132 return tu_group;
7133}
7134
094b34ac
DE
7135/* Look up the type_unit_group for type unit CU, and create it if necessary.
7136 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7137
7138static struct type_unit_group *
ff39bb5e 7139get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7140{
518817b3
SM
7141 struct dwarf2_per_objfile *dwarf2_per_objfile
7142 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7143 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7144 struct type_unit_group *tu_group;
7145 void **slot;
7146 unsigned int line_offset;
7147 struct type_unit_group type_unit_group_for_lookup;
7148
7149 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7150 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7151
7152 /* Do we need to create a new group, or can we use an existing one? */
7153
7154 if (stmt_list)
7155 {
7156 line_offset = DW_UNSND (stmt_list);
7157 ++tu_stats->nr_symtab_sharers;
7158 }
7159 else
7160 {
7161 /* Ugh, no stmt_list. Rare, but we have to handle it.
7162 We can do various things here like create one group per TU or
7163 spread them over multiple groups to split up the expansion work.
7164 To avoid worst case scenarios (too many groups or too large groups)
7165 we, umm, group them in bunches. */
7166 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7167 | (tu_stats->nr_stmt_less_type_units
7168 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7169 ++tu_stats->nr_stmt_less_type_units;
7170 }
7171
094b34ac 7172 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7173 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7174 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7175 &type_unit_group_for_lookup, INSERT);
7176 if (*slot != NULL)
7177 {
9a3c8263 7178 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7179 gdb_assert (tu_group != NULL);
7180 }
7181 else
7182 {
9c541725 7183 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7184 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7185 *slot = tu_group;
7186 ++tu_stats->nr_symtabs;
7187 }
7188
7189 return tu_group;
7190}
0018ea6f
DE
7191\f
7192/* Partial symbol tables. */
7193
7194/* Create a psymtab named NAME and assign it to PER_CU.
7195
7196 The caller must fill in the following details:
7197 dirname, textlow, texthigh. */
7198
891813be 7199static dwarf2_psymtab *
0018ea6f
DE
7200create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7201{
e3b94546 7202 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7203 dwarf2_psymtab *pst;
0018ea6f 7204
891813be 7205 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7206
6d94535f 7207 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7208
7209 /* This is the glue that links PST into GDB's symbol API. */
891813be 7210 pst->per_cu_data = per_cu;
0018ea6f
DE
7211 per_cu->v.psymtab = pst;
7212
7213 return pst;
7214}
7215
c0ab21c2 7216/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7217
7218static void
7219process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7220 const gdb_byte *info_ptr,
0018ea6f 7221 struct die_info *comp_unit_die,
c0ab21c2 7222 enum language pretend_language)
0018ea6f
DE
7223{
7224 struct dwarf2_cu *cu = reader->cu;
518817b3 7225 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7226 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7227 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7228 CORE_ADDR baseaddr;
7229 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7230 dwarf2_psymtab *pst;
3a2b436a 7231 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7232 const char *filename;
0018ea6f 7233
0018ea6f
DE
7234 gdb_assert (! per_cu->is_debug_types);
7235
c0ab21c2 7236 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7237
0018ea6f 7238 /* Allocate a new partial symbol table structure. */
2e927613
TV
7239 gdb::unique_xmalloc_ptr<char> debug_filename;
7240 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7241 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7242 if (filename == NULL)
0018ea6f 7243 filename = "";
2e927613
TV
7244 else if (strcmp (filename, artificial) == 0)
7245 {
7246 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7247 sect_offset_str (per_cu->sect_off),
7248 (char *) NULL));
2e927613
TV
7249 filename = debug_filename.get ();
7250 }
0018ea6f
DE
7251
7252 pst = create_partial_symtab (per_cu, filename);
7253
7254 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7255 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7256
b3b3bada 7257 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7258
7259 dwarf2_find_base_address (comp_unit_die, cu);
7260
7261 /* Possibly set the default values of LOWPC and HIGHPC from
7262 `DW_AT_ranges'. */
3a2b436a
JK
7263 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7264 &best_highpc, cu, pst);
7265 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7266 {
7267 CORE_ADDR low
7268 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7269 - baseaddr);
7270 CORE_ADDR high
7271 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7272 - baseaddr - 1);
7273 /* Store the contiguous range if it is not empty; it can be
7274 empty for CUs with no code. */
d320c2b5
TT
7275 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7276 low, high, pst);
79748972 7277 }
0018ea6f
DE
7278
7279 /* Check if comp unit has_children.
7280 If so, read the rest of the partial symbols from this comp unit.
7281 If not, there's no more debug_info for this comp unit. */
3e225074 7282 if (comp_unit_die->has_children)
0018ea6f
DE
7283 {
7284 struct partial_die_info *first_die;
7285 CORE_ADDR lowpc, highpc;
7286
7287 lowpc = ((CORE_ADDR) -1);
7288 highpc = ((CORE_ADDR) 0);
7289
7290 first_die = load_partial_dies (reader, info_ptr, 1);
7291
7292 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7293 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7294
7295 /* If we didn't find a lowpc, set it to highpc to avoid
7296 complaints from `maint check'. */
7297 if (lowpc == ((CORE_ADDR) -1))
7298 lowpc = highpc;
7299
7300 /* If the compilation unit didn't have an explicit address range,
7301 then use the information extracted from its child dies. */
e385593e 7302 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7303 {
7304 best_lowpc = lowpc;
7305 best_highpc = highpc;
7306 }
7307 }
4ae976d1 7308 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7309 best_lowpc + baseaddr)
7310 - baseaddr);
4ae976d1 7311 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7312 best_highpc + baseaddr)
7313 - baseaddr);
0018ea6f 7314
8763cede 7315 end_psymtab_common (objfile, pst);
0018ea6f 7316
ae640021 7317 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7318 {
7319 int i;
ae640021 7320 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7321
7322 /* Fill in 'dependencies' here; we fill in 'users' in a
7323 post-pass. */
7324 pst->number_of_dependencies = len;
a9342b62
TT
7325 pst->dependencies
7326 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7327 for (i = 0; i < len; ++i)
7328 {
7329 pst->dependencies[i]
7330 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7331 }
0018ea6f 7332
ae640021 7333 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7334 }
7335
7336 /* Get the list of files included in the current compilation unit,
7337 and build a psymtab for each of them. */
7338 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7339
b4f54984 7340 if (dwarf_read_debug)
b926417a
TT
7341 fprintf_unfiltered (gdb_stdlog,
7342 "Psymtab for %s unit @%s: %s - %s"
7343 ", %d global, %d static syms\n",
7344 per_cu->is_debug_types ? "type" : "comp",
7345 sect_offset_str (per_cu->sect_off),
7346 paddress (gdbarch, pst->text_low (objfile)),
7347 paddress (gdbarch, pst->text_high (objfile)),
7348 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7349}
7350
7351/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7352 Process compilation unit THIS_CU for a psymtab. */
7353
7354static void
7355process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7356 bool want_partial_unit,
b93601f3 7357 enum language pretend_language)
0018ea6f
DE
7358{
7359 /* If this compilation unit was already read in, free the
7360 cached copy in order to read it in again. This is
7361 necessary because we skipped some symbols when we first
7362 read in the compilation unit (see load_partial_dies).
7363 This problem could be avoided, but the benefit is unclear. */
7364 if (this_cu->cu != NULL)
7365 free_one_cached_comp_unit (this_cu);
7366
6751ebae 7367 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2
TT
7368
7369 if (reader.dummy_p)
f1902523 7370 {
c0ab21c2 7371 /* Nothing. */
f1902523 7372 }
c0ab21c2 7373 else if (this_cu->is_debug_types)
3e225074
TT
7374 build_type_psymtabs_reader (&reader, reader.info_ptr,
7375 reader.comp_unit_die);
135f5437
TT
7376 else if (want_partial_unit
7377 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7378 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7379 reader.comp_unit_die,
c0ab21c2 7380 pretend_language);
0018ea6f
DE
7381
7382 /* Age out any secondary CUs. */
ed2dc618 7383 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7384}
f4dc4d17
DE
7385
7386/* Reader function for build_type_psymtabs. */
7387
7388static void
7389build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7390 const gdb_byte *info_ptr,
3e225074 7391 struct die_info *type_unit_die)
f4dc4d17 7392{
ed2dc618 7393 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7394 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7395 struct objfile *objfile = dwarf2_per_objfile->objfile;
7396 struct dwarf2_cu *cu = reader->cu;
7397 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7398 struct signatured_type *sig_type;
f4dc4d17
DE
7399 struct type_unit_group *tu_group;
7400 struct attribute *attr;
7401 struct partial_die_info *first_die;
7402 CORE_ADDR lowpc, highpc;
891813be 7403 dwarf2_psymtab *pst;
f4dc4d17 7404
0186c6a7
DE
7405 gdb_assert (per_cu->is_debug_types);
7406 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7407
3e225074 7408 if (! type_unit_die->has_children)
f4dc4d17
DE
7409 return;
7410
7411 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7412 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7413
df07e2c7 7414 if (tu_group->tus == nullptr)
a8b3b8e9 7415 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7416 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7417
7418 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7419 pst = create_partial_symtab (per_cu, "");
6d94535f 7420 pst->anonymous = true;
f4dc4d17
DE
7421
7422 first_die = load_partial_dies (reader, info_ptr, 1);
7423
7424 lowpc = (CORE_ADDR) -1;
7425 highpc = (CORE_ADDR) 0;
7426 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7427
8763cede 7428 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7429}
7430
73051182
DE
7431/* Struct used to sort TUs by their abbreviation table offset. */
7432
7433struct tu_abbrev_offset
7434{
b2bdb8cf
SM
7435 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7436 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7437 {}
7438
7439 signatured_type *sig_type;
73051182
DE
7440 sect_offset abbrev_offset;
7441};
7442
484cf504 7443/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7444
484cf504
TT
7445static bool
7446sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7447 const struct tu_abbrev_offset &b)
73051182 7448{
484cf504 7449 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7450}
7451
7452/* Efficiently read all the type units.
7453 This does the bulk of the work for build_type_psymtabs.
7454
7455 The efficiency is because we sort TUs by the abbrev table they use and
7456 only read each abbrev table once. In one program there are 200K TUs
7457 sharing 8K abbrev tables.
7458
7459 The main purpose of this function is to support building the
7460 dwarf2_per_objfile->type_unit_groups table.
7461 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7462 can collapse the search space by grouping them by stmt_list.
7463 The savings can be significant, in the same program from above the 200K TUs
7464 share 8K stmt_list tables.
7465
7466 FUNC is expected to call get_type_unit_group, which will create the
7467 struct type_unit_group if necessary and add it to
7468 dwarf2_per_objfile->type_unit_groups. */
7469
7470static void
ed2dc618 7471build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7472{
73051182 7473 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7474 abbrev_table_up abbrev_table;
73051182 7475 sect_offset abbrev_offset;
73051182
DE
7476
7477 /* It's up to the caller to not call us multiple times. */
7478 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7479
b2bdb8cf 7480 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7481 return;
7482
7483 /* TUs typically share abbrev tables, and there can be way more TUs than
7484 abbrev tables. Sort by abbrev table to reduce the number of times we
7485 read each abbrev table in.
7486 Alternatives are to punt or to maintain a cache of abbrev tables.
7487 This is simpler and efficient enough for now.
7488
7489 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7490 symtab to use). Typically TUs with the same abbrev offset have the same
7491 stmt_list value too so in practice this should work well.
7492
7493 The basic algorithm here is:
7494
7495 sort TUs by abbrev table
7496 for each TU with same abbrev table:
7497 read abbrev table if first user
7498 read TU top level DIE
7499 [IWBN if DWO skeletons had DW_AT_stmt_list]
7500 call FUNC */
7501
b4f54984 7502 if (dwarf_read_debug)
73051182
DE
7503 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7504
7505 /* Sort in a separate table to maintain the order of all_type_units
7506 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7507 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7508 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7509
7510 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7511 sorted_by_abbrev.emplace_back
7512 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7513 sig_type->per_cu.section,
7514 sig_type->per_cu.sect_off));
73051182 7515
484cf504
TT
7516 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7517 sort_tu_by_abbrev_offset);
73051182 7518
9c541725 7519 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7520
b2bdb8cf 7521 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7522 {
73051182
DE
7523 /* Switch to the next abbrev table if necessary. */
7524 if (abbrev_table == NULL
b2bdb8cf 7525 || tu.abbrev_offset != abbrev_offset)
73051182 7526 {
b2bdb8cf 7527 abbrev_offset = tu.abbrev_offset;
73051182 7528 abbrev_table =
86de1d91
TT
7529 abbrev_table::read (dwarf2_per_objfile->objfile,
7530 &dwarf2_per_objfile->abbrev,
7531 abbrev_offset);
73051182
DE
7532 ++tu_stats->nr_uniq_abbrev_tables;
7533 }
7534
c0ab21c2 7535 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7536 0, false);
c0ab21c2
TT
7537 if (!reader.dummy_p)
7538 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7539 reader.comp_unit_die);
73051182 7540 }
6aa5f3a6 7541}
73051182 7542
6aa5f3a6
DE
7543/* Print collected type unit statistics. */
7544
7545static void
ed2dc618 7546print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7547{
7548 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7549
7550 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7551 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7552 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7553 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7554 tu_stats->nr_uniq_abbrev_tables);
7555 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7556 tu_stats->nr_symtabs);
7557 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7558 tu_stats->nr_symtab_sharers);
7559 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7560 tu_stats->nr_stmt_less_type_units);
7561 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7562 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7563}
7564
f4dc4d17
DE
7565/* Traversal function for build_type_psymtabs. */
7566
7567static int
7568build_type_psymtab_dependencies (void **slot, void *info)
7569{
ed2dc618
SM
7570 struct dwarf2_per_objfile *dwarf2_per_objfile
7571 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7572 struct objfile *objfile = dwarf2_per_objfile->objfile;
7573 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7574 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7575 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7576 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7577 int i;
7578
7579 gdb_assert (len > 0);
197400e8 7580 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7581
7582 pst->number_of_dependencies = len;
a9342b62 7583 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7584 for (i = 0; i < len; ++i)
f4dc4d17 7585 {
df07e2c7 7586 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7587 gdb_assert (iter->per_cu.is_debug_types);
7588 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7589 iter->type_unit_group = tu_group;
f4dc4d17
DE
7590 }
7591
df07e2c7
AB
7592 delete tu_group->tus;
7593 tu_group->tus = nullptr;
348e048f
DE
7594
7595 return 1;
7596}
7597
7598/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7599 Build partial symbol tables for the .debug_types comp-units. */
7600
7601static void
ed2dc618 7602build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7603{
ed2dc618 7604 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7605 return;
7606
ed2dc618 7607 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7608}
f4dc4d17 7609
6aa5f3a6
DE
7610/* Traversal function for process_skeletonless_type_unit.
7611 Read a TU in a DWO file and build partial symbols for it. */
7612
7613static int
7614process_skeletonless_type_unit (void **slot, void *info)
7615{
7616 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7617 struct dwarf2_per_objfile *dwarf2_per_objfile
7618 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7619 struct signatured_type find_entry, *entry;
7620
7621 /* If this TU doesn't exist in the global table, add it and read it in. */
7622
7623 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7624 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7625
7626 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7627 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7628 &find_entry, INSERT);
6aa5f3a6
DE
7629 /* If we've already seen this type there's nothing to do. What's happening
7630 is we're doing our own version of comdat-folding here. */
7631 if (*slot != NULL)
7632 return 1;
7633
7634 /* This does the job that create_all_type_units would have done for
7635 this TU. */
ed2dc618
SM
7636 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7637 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7638 *slot = entry;
7639
7640 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7641 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7642 if (!reader.dummy_p)
7643 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7644 reader.comp_unit_die);
6aa5f3a6
DE
7645
7646 return 1;
7647}
7648
7649/* Traversal function for process_skeletonless_type_units. */
7650
7651static int
7652process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7653{
7654 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7655
7656 if (dwo_file->tus != NULL)
b0b6a987
TT
7657 htab_traverse_noresize (dwo_file->tus.get (),
7658 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7659
7660 return 1;
7661}
7662
7663/* Scan all TUs of DWO files, verifying we've processed them.
7664 This is needed in case a TU was emitted without its skeleton.
7665 Note: This can't be done until we know what all the DWO files are. */
7666
7667static void
ed2dc618 7668process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7669{
7670 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7671 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7672 && dwarf2_per_objfile->dwo_files != NULL)
7673 {
51ac9db5 7674 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7675 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7676 dwarf2_per_objfile);
6aa5f3a6 7677 }
348e048f
DE
7678}
7679
ed2dc618 7680/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7681
7682static void
ed2dc618 7683set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7684{
b76e467d 7685 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7686 {
891813be 7687 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7688
36586728
TT
7689 if (pst == NULL)
7690 continue;
7691
b76e467d 7692 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7693 {
7694 /* Set the 'user' field only if it is not already set. */
7695 if (pst->dependencies[j]->user == NULL)
7696 pst->dependencies[j]->user = pst;
7697 }
7698 }
7699}
7700
93311388
DE
7701/* Build the partial symbol table by doing a quick pass through the
7702 .debug_info and .debug_abbrev sections. */
72bf9492 7703
93311388 7704static void
ed2dc618 7705dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7706{
ed2dc618 7707 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7708
b4f54984 7709 if (dwarf_read_debug)
45cfd468
DE
7710 {
7711 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7712 objfile_name (objfile));
45cfd468
DE
7713 }
7714
76935768
TT
7715 scoped_restore restore_reading_psyms
7716 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7717 true);
98bfdba5 7718
96b79293 7719 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7720
93311388
DE
7721 /* Any cached compilation units will be linked by the per-objfile
7722 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7723 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7724
ed2dc618 7725 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7726
ed2dc618 7727 create_all_comp_units (dwarf2_per_objfile);
c906108c 7728
60606b2c
TT
7729 /* Create a temporary address map on a temporary obstack. We later
7730 copy this to the final obstack. */
8268c778 7731 auto_obstack temp_obstack;
791afaa2
TT
7732
7733 scoped_restore save_psymtabs_addrmap
d320c2b5 7734 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7735 addrmap_create_mutable (&temp_obstack));
72bf9492 7736
b76e467d 7737 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7738 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7739
6aa5f3a6 7740 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7741 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7742
7743 /* Now that all TUs have been processed we can fill in the dependencies. */
7744 if (dwarf2_per_objfile->type_unit_groups != NULL)
7745 {
eaa5fa8b 7746 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7747 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7748 }
7749
b4f54984 7750 if (dwarf_read_debug)
ed2dc618 7751 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7752
ed2dc618 7753 set_partial_user (dwarf2_per_objfile);
95554aad 7754
d320c2b5
TT
7755 objfile->partial_symtabs->psymtabs_addrmap
7756 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7757 objfile->partial_symtabs->obstack ());
791afaa2
TT
7758 /* At this point we want to keep the address map. */
7759 save_psymtabs_addrmap.release ();
ff013f42 7760
b4f54984 7761 if (dwarf_read_debug)
45cfd468 7762 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7763 objfile_name (objfile));
ae038cb0
DJ
7764}
7765
dee91e82
DE
7766/* Load the partial DIEs for a secondary CU into memory.
7767 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7768
dee91e82
DE
7769static void
7770load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7771{
6751ebae 7772 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7773
7774 if (!reader.dummy_p)
7775 {
7776 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7777 language_minimal);
7778
7779 /* Check if comp unit has_children.
7780 If so, read the rest of the partial symbols from this comp unit.
7781 If not, there's no more debug_info for this comp unit. */
3e225074 7782 if (reader.comp_unit_die->has_children)
c0ab21c2 7783 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7784
7785 reader.keep ();
c0ab21c2 7786 }
ae038cb0
DJ
7787}
7788
ae038cb0 7789static void
ed2dc618 7790read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7791 struct dwarf2_section_info *section,
f1902523 7792 struct dwarf2_section_info *abbrev_section,
b76e467d 7793 unsigned int is_dwz)
ae038cb0 7794{
d521ce57 7795 const gdb_byte *info_ptr;
ed2dc618 7796 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7797
b4f54984 7798 if (dwarf_read_debug)
bf6af496 7799 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7800 section->get_name (),
7801 section->get_file_name ());
bf6af496 7802
96b79293 7803 section->read (objfile);
ae038cb0 7804
36586728 7805 info_ptr = section->buffer;
6e70227d 7806
36586728 7807 while (info_ptr < section->buffer + section->size)
ae038cb0 7808 {
ae038cb0 7809 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7810
9c541725 7811 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7812
f1902523 7813 comp_unit_head cu_header;
ed2dc618
SM
7814 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7815 abbrev_section, info_ptr,
7816 rcuh_kind::COMPILE);
ae038cb0
DJ
7817
7818 /* Save the compilation unit for later lookup. */
f1902523
JK
7819 if (cu_header.unit_type != DW_UT_type)
7820 {
7821 this_cu = XOBNEW (&objfile->objfile_obstack,
7822 struct dwarf2_per_cu_data);
7823 memset (this_cu, 0, sizeof (*this_cu));
7824 }
7825 else
7826 {
7827 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7828 struct signatured_type);
7829 memset (sig_type, 0, sizeof (*sig_type));
7830 sig_type->signature = cu_header.signature;
7831 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7832 this_cu = &sig_type->per_cu;
7833 }
7834 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7835 this_cu->sect_off = sect_off;
f1902523 7836 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7837 this_cu->is_dwz = is_dwz;
e3b94546 7838 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7839 this_cu->section = section;
ae038cb0 7840
b76e467d 7841 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7842
7843 info_ptr = info_ptr + this_cu->length;
7844 }
36586728
TT
7845}
7846
7847/* Create a list of all compilation units in OBJFILE.
7848 This is only done for -readnow and building partial symtabs. */
7849
7850static void
ed2dc618 7851create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7852{
b76e467d 7853 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7854 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7855 &dwarf2_per_objfile->abbrev, 0);
36586728 7856
b76e467d 7857 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7858 if (dwz != NULL)
ed2dc618 7859 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7860 1);
c906108c
SS
7861}
7862
5734ee8b 7863/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7864 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7865 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7866 DW_AT_ranges). See the comments of add_partial_subprogram on how
7867 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7868
72bf9492
DJ
7869static void
7870scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7871 CORE_ADDR *highpc, int set_addrmap,
7872 struct dwarf2_cu *cu)
c906108c 7873{
72bf9492 7874 struct partial_die_info *pdi;
c906108c 7875
91c24f0a
DC
7876 /* Now, march along the PDI's, descending into ones which have
7877 interesting children but skipping the children of the other ones,
7878 until we reach the end of the compilation unit. */
c906108c 7879
72bf9492 7880 pdi = first_die;
91c24f0a 7881
72bf9492
DJ
7882 while (pdi != NULL)
7883 {
52356b79 7884 pdi->fixup (cu);
c906108c 7885
f55ee35c 7886 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7887 children, so we need to look at them. Ditto for anonymous
7888 enums. */
933c6fe4 7889
72bf9492 7890 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7891 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7892 || pdi->tag == DW_TAG_imported_unit
7893 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7894 {
72bf9492 7895 switch (pdi->tag)
c906108c
SS
7896 {
7897 case DW_TAG_subprogram:
b1dc1806 7898 case DW_TAG_inlined_subroutine:
cdc07690 7899 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7900 break;
72929c62 7901 case DW_TAG_constant:
c906108c
SS
7902 case DW_TAG_variable:
7903 case DW_TAG_typedef:
91c24f0a 7904 case DW_TAG_union_type:
72bf9492 7905 if (!pdi->is_declaration)
63d06c5c 7906 {
72bf9492 7907 add_partial_symbol (pdi, cu);
63d06c5c
DC
7908 }
7909 break;
c906108c 7910 case DW_TAG_class_type:
680b30c7 7911 case DW_TAG_interface_type:
c906108c 7912 case DW_TAG_structure_type:
72bf9492 7913 if (!pdi->is_declaration)
c906108c 7914 {
72bf9492 7915 add_partial_symbol (pdi, cu);
c906108c 7916 }
b7fee5a3
KS
7917 if ((cu->language == language_rust
7918 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7919 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7920 set_addrmap, cu);
c906108c 7921 break;
91c24f0a 7922 case DW_TAG_enumeration_type:
72bf9492
DJ
7923 if (!pdi->is_declaration)
7924 add_partial_enumeration (pdi, cu);
c906108c
SS
7925 break;
7926 case DW_TAG_base_type:
a02abb62 7927 case DW_TAG_subrange_type:
c906108c 7928 /* File scope base type definitions are added to the partial
c5aa993b 7929 symbol table. */
72bf9492 7930 add_partial_symbol (pdi, cu);
c906108c 7931 break;
d9fa45fe 7932 case DW_TAG_namespace:
cdc07690 7933 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7934 break;
5d7cb8df 7935 case DW_TAG_module:
59c35742
AB
7936 if (!pdi->is_declaration)
7937 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7938 break;
95554aad
TT
7939 case DW_TAG_imported_unit:
7940 {
7941 struct dwarf2_per_cu_data *per_cu;
7942
f4dc4d17
DE
7943 /* For now we don't handle imported units in type units. */
7944 if (cu->per_cu->is_debug_types)
7945 {
7946 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7947 " supported in type units [in module %s]"),
518817b3 7948 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
7949 }
7950
e3b94546
SM
7951 per_cu = dwarf2_find_containing_comp_unit
7952 (pdi->d.sect_off, pdi->is_dwz,
518817b3 7953 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
7954
7955 /* Go read the partial unit, if needed. */
7956 if (per_cu->v.psymtab == NULL)
135f5437 7957 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 7958
ae640021 7959 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7960 }
7961 break;
74921315
KS
7962 case DW_TAG_imported_declaration:
7963 add_partial_symbol (pdi, cu);
7964 break;
c906108c
SS
7965 default:
7966 break;
7967 }
7968 }
7969
72bf9492
DJ
7970 /* If the die has a sibling, skip to the sibling. */
7971
7972 pdi = pdi->die_sibling;
7973 }
7974}
7975
7976/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7977
72bf9492 7978 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7979 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7980 Enumerators are an exception; they use the scope of their parent
7981 enumeration type, i.e. the name of the enumeration type is not
7982 prepended to the enumerator.
91c24f0a 7983
72bf9492
DJ
7984 There are two complexities. One is DW_AT_specification; in this
7985 case "parent" means the parent of the target of the specification,
7986 instead of the direct parent of the DIE. The other is compilers
7987 which do not emit DW_TAG_namespace; in this case we try to guess
7988 the fully qualified name of structure types from their members'
7989 linkage names. This must be done using the DIE's children rather
7990 than the children of any DW_AT_specification target. We only need
7991 to do this for structures at the top level, i.e. if the target of
7992 any DW_AT_specification (if any; otherwise the DIE itself) does not
7993 have a parent. */
7994
7995/* Compute the scope prefix associated with PDI's parent, in
7996 compilation unit CU. The result will be allocated on CU's
7997 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7998 field. NULL is returned if no prefix is necessary. */
15d034d0 7999static const char *
72bf9492
DJ
8000partial_die_parent_scope (struct partial_die_info *pdi,
8001 struct dwarf2_cu *cu)
8002{
15d034d0 8003 const char *grandparent_scope;
72bf9492 8004 struct partial_die_info *parent, *real_pdi;
91c24f0a 8005
72bf9492
DJ
8006 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8007 then this means the parent of the specification DIE. */
8008
8009 real_pdi = pdi;
72bf9492 8010 while (real_pdi->has_specification)
fb816e8b 8011 {
122cf0f2
AB
8012 auto res = find_partial_die (real_pdi->spec_offset,
8013 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8014 real_pdi = res.pdi;
8015 cu = res.cu;
8016 }
72bf9492
DJ
8017
8018 parent = real_pdi->die_parent;
8019 if (parent == NULL)
8020 return NULL;
8021
8022 if (parent->scope_set)
8023 return parent->scope;
8024
52356b79 8025 parent->fixup (cu);
72bf9492 8026
10b3939b 8027 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8028
acebe513
UW
8029 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8030 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8031 Work around this problem here. */
8032 if (cu->language == language_cplus
6e70227d 8033 && parent->tag == DW_TAG_namespace
acebe513
UW
8034 && strcmp (parent->name, "::") == 0
8035 && grandparent_scope == NULL)
8036 {
8037 parent->scope = NULL;
8038 parent->scope_set = 1;
8039 return NULL;
8040 }
8041
0a4b0913 8042 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8043 if (pdi->tag == DW_TAG_enumerator)
8044 /* Enumerators should not get the name of the enumeration as a prefix. */
8045 parent->scope = grandparent_scope;
8046 else if (parent->tag == DW_TAG_namespace
f55ee35c 8047 || parent->tag == DW_TAG_module
72bf9492
DJ
8048 || parent->tag == DW_TAG_structure_type
8049 || parent->tag == DW_TAG_class_type
680b30c7 8050 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8051 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8052 || parent->tag == DW_TAG_enumeration_type
8053 || (cu->language == language_fortran
8054 && parent->tag == DW_TAG_subprogram
8055 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8056 {
8057 if (grandparent_scope == NULL)
8058 parent->scope = parent->name;
8059 else
3e43a32a
MS
8060 parent->scope = typename_concat (&cu->comp_unit_obstack,
8061 grandparent_scope,
f55ee35c 8062 parent->name, 0, cu);
72bf9492 8063 }
72bf9492
DJ
8064 else
8065 {
8066 /* FIXME drow/2004-04-01: What should we be doing with
8067 function-local names? For partial symbols, we should probably be
8068 ignoring them. */
fa9c3fa0
TT
8069 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8070 dwarf_tag_name (parent->tag),
8071 sect_offset_str (pdi->sect_off));
72bf9492 8072 parent->scope = grandparent_scope;
c906108c
SS
8073 }
8074
72bf9492
DJ
8075 parent->scope_set = 1;
8076 return parent->scope;
8077}
8078
8079/* Return the fully scoped name associated with PDI, from compilation unit
8080 CU. The result will be allocated with malloc. */
4568ecf9 8081
43816ebc 8082static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8083partial_die_full_name (struct partial_die_info *pdi,
8084 struct dwarf2_cu *cu)
8085{
15d034d0 8086 const char *parent_scope;
72bf9492 8087
98bfdba5
PA
8088 /* If this is a template instantiation, we can not work out the
8089 template arguments from partial DIEs. So, unfortunately, we have
8090 to go through the full DIEs. At least any work we do building
8091 types here will be reused if full symbols are loaded later. */
8092 if (pdi->has_template_arguments)
8093 {
52356b79 8094 pdi->fixup (cu);
98bfdba5
PA
8095
8096 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8097 {
8098 struct die_info *die;
8099 struct attribute attr;
8100 struct dwarf2_cu *ref_cu = cu;
8101
b64f50a1 8102 /* DW_FORM_ref_addr is using section offset. */
b4069958 8103 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8104 attr.form = DW_FORM_ref_addr;
9c541725 8105 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8106 die = follow_die_ref (NULL, &attr, &ref_cu);
8107
43816ebc 8108 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8109 }
8110 }
8111
72bf9492
DJ
8112 parent_scope = partial_die_parent_scope (pdi, cu);
8113 if (parent_scope == NULL)
8114 return NULL;
8115 else
43816ebc
TT
8116 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8117 pdi->name, 0, cu));
c906108c
SS
8118}
8119
8120static void
72bf9492 8121add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8122{
518817b3
SM
8123 struct dwarf2_per_objfile *dwarf2_per_objfile
8124 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8125 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8126 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8127 CORE_ADDR addr = 0;
15d034d0 8128 const char *actual_name = NULL;
e142c38c
DJ
8129 CORE_ADDR baseaddr;
8130
b3b3bada 8131 baseaddr = objfile->text_section_offset ();
c906108c 8132
43816ebc
TT
8133 gdb::unique_xmalloc_ptr<char> built_actual_name
8134 = partial_die_full_name (pdi, cu);
15d034d0 8135 if (built_actual_name != NULL)
43816ebc 8136 actual_name = built_actual_name.get ();
63d06c5c 8137
72bf9492
DJ
8138 if (actual_name == NULL)
8139 actual_name = pdi->name;
8140
c906108c
SS
8141 switch (pdi->tag)
8142 {
b1dc1806 8143 case DW_TAG_inlined_subroutine:
c906108c 8144 case DW_TAG_subprogram:
79748972
TT
8145 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8146 - baseaddr);
0a4b0913
AB
8147 if (pdi->is_external
8148 || cu->language == language_ada
8149 || (cu->language == language_fortran
8150 && pdi->die_parent != NULL
8151 && pdi->die_parent->tag == DW_TAG_subprogram))
8152 {
8153 /* Normally, only "external" DIEs are part of the global scope.
8154 But in Ada and Fortran, we want to be able to access nested
8155 procedures globally. So all Ada and Fortran subprograms are
8156 stored in the global scope. */
31edb802 8157 add_psymbol_to_list (actual_name,
15d034d0 8158 built_actual_name != NULL,
f47fb265 8159 VAR_DOMAIN, LOC_BLOCK,
79748972 8160 SECT_OFF_TEXT (objfile),
75aedd27 8161 psymbol_placement::GLOBAL,
79748972
TT
8162 addr,
8163 cu->language, objfile);
c906108c
SS
8164 }
8165 else
8166 {
31edb802 8167 add_psymbol_to_list (actual_name,
15d034d0 8168 built_actual_name != NULL,
f47fb265 8169 VAR_DOMAIN, LOC_BLOCK,
79748972 8170 SECT_OFF_TEXT (objfile),
75aedd27 8171 psymbol_placement::STATIC,
1762568f 8172 addr, cu->language, objfile);
c906108c 8173 }
0c1b455e
TT
8174
8175 if (pdi->main_subprogram && actual_name != NULL)
8176 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8177 break;
72929c62 8178 case DW_TAG_constant:
31edb802 8179 add_psymbol_to_list (actual_name,
75aedd27
TT
8180 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8181 -1, (pdi->is_external
8182 ? psymbol_placement::GLOBAL
8183 : psymbol_placement::STATIC),
8184 0, cu->language, objfile);
72929c62 8185 break;
c906108c 8186 case DW_TAG_variable:
95554aad
TT
8187 if (pdi->d.locdesc)
8188 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8189
95554aad 8190 if (pdi->d.locdesc
caac4577
JG
8191 && addr == 0
8192 && !dwarf2_per_objfile->has_section_at_zero)
8193 {
8194 /* A global or static variable may also have been stripped
8195 out by the linker if unused, in which case its address
8196 will be nullified; do not add such variables into partial
8197 symbol table then. */
8198 }
8199 else if (pdi->is_external)
c906108c
SS
8200 {
8201 /* Global Variable.
8202 Don't enter into the minimal symbol tables as there is
8203 a minimal symbol table entry from the ELF symbols already.
8204 Enter into partial symbol table if it has a location
8205 descriptor or a type.
8206 If the location descriptor is missing, new_symbol will create
8207 a LOC_UNRESOLVED symbol, the address of the variable will then
8208 be determined from the minimal symbol table whenever the variable
8209 is referenced.
8210 The address for the partial symbol table entry is not
8211 used by GDB, but it comes in handy for debugging partial symbol
8212 table building. */
8213
95554aad 8214 if (pdi->d.locdesc || pdi->has_type)
31edb802 8215 add_psymbol_to_list (actual_name,
15d034d0 8216 built_actual_name != NULL,
f47fb265 8217 VAR_DOMAIN, LOC_STATIC,
79748972 8218 SECT_OFF_TEXT (objfile),
75aedd27 8219 psymbol_placement::GLOBAL,
79748972 8220 addr, cu->language, objfile);
c906108c
SS
8221 }
8222 else
8223 {
ff908ebf
AW
8224 int has_loc = pdi->d.locdesc != NULL;
8225
8226 /* Static Variable. Skip symbols whose value we cannot know (those
8227 without location descriptors or constant values). */
8228 if (!has_loc && !pdi->has_const_value)
43816ebc 8229 return;
ff908ebf 8230
31edb802 8231 add_psymbol_to_list (actual_name,
15d034d0 8232 built_actual_name != NULL,
f47fb265 8233 VAR_DOMAIN, LOC_STATIC,
79748972 8234 SECT_OFF_TEXT (objfile),
75aedd27 8235 psymbol_placement::STATIC,
79748972 8236 has_loc ? addr : 0,
f47fb265 8237 cu->language, objfile);
c906108c
SS
8238 }
8239 break;
8240 case DW_TAG_typedef:
8241 case DW_TAG_base_type:
a02abb62 8242 case DW_TAG_subrange_type:
31edb802 8243 add_psymbol_to_list (actual_name,
15d034d0 8244 built_actual_name != NULL,
79748972 8245 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8246 psymbol_placement::STATIC,
1762568f 8247 0, cu->language, objfile);
c906108c 8248 break;
74921315 8249 case DW_TAG_imported_declaration:
72bf9492 8250 case DW_TAG_namespace:
31edb802 8251 add_psymbol_to_list (actual_name,
15d034d0 8252 built_actual_name != NULL,
79748972 8253 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8254 psymbol_placement::GLOBAL,
1762568f 8255 0, cu->language, objfile);
72bf9492 8256 break;
530e8392 8257 case DW_TAG_module:
a5fd13a9
BH
8258 /* With Fortran 77 there might be a "BLOCK DATA" module
8259 available without any name. If so, we skip the module as it
8260 doesn't bring any value. */
8261 if (actual_name != nullptr)
31edb802 8262 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8263 built_actual_name != NULL,
8264 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8265 psymbol_placement::GLOBAL,
8266 0, cu->language, objfile);
530e8392 8267 break;
c906108c 8268 case DW_TAG_class_type:
680b30c7 8269 case DW_TAG_interface_type:
c906108c
SS
8270 case DW_TAG_structure_type:
8271 case DW_TAG_union_type:
8272 case DW_TAG_enumeration_type:
fa4028e9
JB
8273 /* Skip external references. The DWARF standard says in the section
8274 about "Structure, Union, and Class Type Entries": "An incomplete
8275 structure, union or class type is represented by a structure,
8276 union or class entry that does not have a byte size attribute
8277 and that has a DW_AT_declaration attribute." */
8278 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8279 return;
fa4028e9 8280
63d06c5c
DC
8281 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8282 static vs. global. */
31edb802 8283 add_psymbol_to_list (actual_name,
15d034d0 8284 built_actual_name != NULL,
79748972 8285 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8286 cu->language == language_cplus
75aedd27
TT
8287 ? psymbol_placement::GLOBAL
8288 : psymbol_placement::STATIC,
1762568f 8289 0, cu->language, objfile);
c906108c 8290
c906108c
SS
8291 break;
8292 case DW_TAG_enumerator:
31edb802 8293 add_psymbol_to_list (actual_name,
15d034d0 8294 built_actual_name != NULL,
79748972 8295 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8296 cu->language == language_cplus
75aedd27
TT
8297 ? psymbol_placement::GLOBAL
8298 : psymbol_placement::STATIC,
1762568f 8299 0, cu->language, objfile);
c906108c
SS
8300 break;
8301 default:
8302 break;
8303 }
8304}
8305
5c4e30ca
DC
8306/* Read a partial die corresponding to a namespace; also, add a symbol
8307 corresponding to that namespace to the symbol table. NAMESPACE is
8308 the name of the enclosing namespace. */
91c24f0a 8309
72bf9492
DJ
8310static void
8311add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8312 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8313 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8314{
72bf9492 8315 /* Add a symbol for the namespace. */
e7c27a73 8316
72bf9492 8317 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8318
8319 /* Now scan partial symbols in that namespace. */
8320
91c24f0a 8321 if (pdi->has_children)
cdc07690 8322 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8323}
8324
5d7cb8df
JK
8325/* Read a partial die corresponding to a Fortran module. */
8326
8327static void
8328add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8329 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8330{
530e8392
KB
8331 /* Add a symbol for the namespace. */
8332
8333 add_partial_symbol (pdi, cu);
8334
f55ee35c 8335 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8336
8337 if (pdi->has_children)
cdc07690 8338 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8339}
8340
b1dc1806
XR
8341/* Read a partial die corresponding to a subprogram or an inlined
8342 subprogram and create a partial symbol for that subprogram.
8343 When the CU language allows it, this routine also defines a partial
8344 symbol for each nested subprogram that this subprogram contains.
8345 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8346 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8347
cdc07690
YQ
8348 PDI may also be a lexical block, in which case we simply search
8349 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8350 Again, this is only performed when the CU language allows this
8351 type of definitions. */
8352
8353static void
8354add_partial_subprogram (struct partial_die_info *pdi,
8355 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8356 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8357{
b1dc1806 8358 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8359 {
8360 if (pdi->has_pc_info)
8361 {
8362 if (pdi->lowpc < *lowpc)
8363 *lowpc = pdi->lowpc;
8364 if (pdi->highpc > *highpc)
8365 *highpc = pdi->highpc;
cdc07690 8366 if (set_addrmap)
5734ee8b 8367 {
518817b3 8368 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8369 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8370 CORE_ADDR baseaddr;
b926417a
TT
8371 CORE_ADDR this_highpc;
8372 CORE_ADDR this_lowpc;
5734ee8b 8373
b3b3bada 8374 baseaddr = objfile->text_section_offset ();
b926417a
TT
8375 this_lowpc
8376 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8377 pdi->lowpc + baseaddr)
8378 - baseaddr);
8379 this_highpc
8380 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8381 pdi->highpc + baseaddr)
8382 - baseaddr);
d320c2b5 8383 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8384 this_lowpc, this_highpc - 1,
9291a0cd 8385 cu->per_cu->v.psymtab);
5734ee8b 8386 }
481860b3
GB
8387 }
8388
8389 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8390 {
bc30ff58 8391 if (!pdi->is_declaration)
e8d05480
JB
8392 /* Ignore subprogram DIEs that do not have a name, they are
8393 illegal. Do not emit a complaint at this point, we will
8394 do so when we convert this psymtab into a symtab. */
8395 if (pdi->name)
8396 add_partial_symbol (pdi, cu);
bc30ff58
JB
8397 }
8398 }
6e70227d 8399
bc30ff58
JB
8400 if (! pdi->has_children)
8401 return;
8402
0a4b0913 8403 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8404 {
8405 pdi = pdi->die_child;
8406 while (pdi != NULL)
8407 {
52356b79 8408 pdi->fixup (cu);
bc30ff58 8409 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8410 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8411 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8412 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8413 pdi = pdi->die_sibling;
8414 }
8415 }
8416}
8417
91c24f0a
DC
8418/* Read a partial die corresponding to an enumeration type. */
8419
72bf9492
DJ
8420static void
8421add_partial_enumeration (struct partial_die_info *enum_pdi,
8422 struct dwarf2_cu *cu)
91c24f0a 8423{
72bf9492 8424 struct partial_die_info *pdi;
91c24f0a
DC
8425
8426 if (enum_pdi->name != NULL)
72bf9492
DJ
8427 add_partial_symbol (enum_pdi, cu);
8428
8429 pdi = enum_pdi->die_child;
8430 while (pdi)
91c24f0a 8431 {
72bf9492 8432 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8433 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8434 else
72bf9492
DJ
8435 add_partial_symbol (pdi, cu);
8436 pdi = pdi->die_sibling;
91c24f0a 8437 }
91c24f0a
DC
8438}
8439
6caca83c
CC
8440/* Return the initial uleb128 in the die at INFO_PTR. */
8441
8442static unsigned int
d521ce57 8443peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8444{
8445 unsigned int bytes_read;
8446
8447 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8448}
8449
685af9cd
TT
8450/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8451 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8452
4bb7a0a7
DJ
8453 Return the corresponding abbrev, or NULL if the number is zero (indicating
8454 an empty DIE). In either case *BYTES_READ will be set to the length of
8455 the initial number. */
8456
8457static struct abbrev_info *
685af9cd
TT
8458peek_die_abbrev (const die_reader_specs &reader,
8459 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8460{
685af9cd 8461 dwarf2_cu *cu = reader.cu;
518817b3 8462 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8463 unsigned int abbrev_number
8464 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8465
8466 if (abbrev_number == 0)
8467 return NULL;
8468
685af9cd 8469 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8470 if (!abbrev)
8471 {
422b9917 8472 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8473 " at offset %s [in module %s]"),
422b9917 8474 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8475 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8476 }
8477
8478 return abbrev;
8479}
8480
93311388
DE
8481/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8482 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8483 DIE. Any children of the skipped DIEs will also be skipped. */
8484
d521ce57
TT
8485static const gdb_byte *
8486skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8487{
4bb7a0a7
DJ
8488 while (1)
8489 {
685af9cd
TT
8490 unsigned int bytes_read;
8491 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8492
4bb7a0a7
DJ
8493 if (abbrev == NULL)
8494 return info_ptr + bytes_read;
8495 else
dee91e82 8496 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8497 }
8498}
8499
93311388
DE
8500/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8501 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8502 abbrev corresponding to that skipped uleb128 should be passed in
8503 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8504 children. */
8505
d521ce57
TT
8506static const gdb_byte *
8507skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8508 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8509{
8510 unsigned int bytes_read;
8511 struct attribute attr;
dee91e82
DE
8512 bfd *abfd = reader->abfd;
8513 struct dwarf2_cu *cu = reader->cu;
d521ce57 8514 const gdb_byte *buffer = reader->buffer;
f664829e 8515 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8516 unsigned int form, i;
8517
8518 for (i = 0; i < abbrev->num_attrs; i++)
8519 {
8520 /* The only abbrev we care about is DW_AT_sibling. */
8521 if (abbrev->attrs[i].name == DW_AT_sibling)
8522 {
18a8505e
AT
8523 bool ignored;
8524 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8525 &ignored);
4bb7a0a7 8526 if (attr.form == DW_FORM_ref_addr)
b98664d3 8527 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8528 else
b9502d3f 8529 {
9c541725
PA
8530 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8531 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8532
8533 if (sibling_ptr < info_ptr)
b98664d3 8534 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
8535 else if (sibling_ptr > reader->buffer_end)
8536 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8537 else
8538 return sibling_ptr;
8539 }
4bb7a0a7
DJ
8540 }
8541
8542 /* If it isn't DW_AT_sibling, skip this attribute. */
8543 form = abbrev->attrs[i].form;
8544 skip_attribute:
8545 switch (form)
8546 {
4bb7a0a7 8547 case DW_FORM_ref_addr:
ae411497
TT
8548 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8549 and later it is offset sized. */
8550 if (cu->header.version == 2)
8551 info_ptr += cu->header.addr_size;
8552 else
8553 info_ptr += cu->header.offset_size;
8554 break;
36586728
TT
8555 case DW_FORM_GNU_ref_alt:
8556 info_ptr += cu->header.offset_size;
8557 break;
ae411497 8558 case DW_FORM_addr:
4bb7a0a7
DJ
8559 info_ptr += cu->header.addr_size;
8560 break;
8561 case DW_FORM_data1:
8562 case DW_FORM_ref1:
8563 case DW_FORM_flag:
8fe0f950 8564 case DW_FORM_strx1:
4bb7a0a7
DJ
8565 info_ptr += 1;
8566 break;
2dc7f7b3 8567 case DW_FORM_flag_present:
43988095 8568 case DW_FORM_implicit_const:
2dc7f7b3 8569 break;
4bb7a0a7
DJ
8570 case DW_FORM_data2:
8571 case DW_FORM_ref2:
8fe0f950 8572 case DW_FORM_strx2:
4bb7a0a7
DJ
8573 info_ptr += 2;
8574 break;
8fe0f950
AT
8575 case DW_FORM_strx3:
8576 info_ptr += 3;
8577 break;
4bb7a0a7
DJ
8578 case DW_FORM_data4:
8579 case DW_FORM_ref4:
8fe0f950 8580 case DW_FORM_strx4:
4bb7a0a7
DJ
8581 info_ptr += 4;
8582 break;
8583 case DW_FORM_data8:
8584 case DW_FORM_ref8:
55f1336d 8585 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8586 info_ptr += 8;
8587 break;
0224619f
JK
8588 case DW_FORM_data16:
8589 info_ptr += 16;
8590 break;
4bb7a0a7 8591 case DW_FORM_string:
9b1c24c8 8592 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8593 info_ptr += bytes_read;
8594 break;
2dc7f7b3 8595 case DW_FORM_sec_offset:
4bb7a0a7 8596 case DW_FORM_strp:
36586728 8597 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8598 info_ptr += cu->header.offset_size;
8599 break;
2dc7f7b3 8600 case DW_FORM_exprloc:
4bb7a0a7
DJ
8601 case DW_FORM_block:
8602 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8603 info_ptr += bytes_read;
8604 break;
8605 case DW_FORM_block1:
8606 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8607 break;
8608 case DW_FORM_block2:
8609 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8610 break;
8611 case DW_FORM_block4:
8612 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8613 break;
336d760d 8614 case DW_FORM_addrx:
cf532bd1 8615 case DW_FORM_strx:
4bb7a0a7
DJ
8616 case DW_FORM_sdata:
8617 case DW_FORM_udata:
8618 case DW_FORM_ref_udata:
3019eac3
DE
8619 case DW_FORM_GNU_addr_index:
8620 case DW_FORM_GNU_str_index:
18a8505e 8621 case DW_FORM_rnglistx:
d521ce57 8622 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8623 break;
8624 case DW_FORM_indirect:
8625 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8626 info_ptr += bytes_read;
8627 /* We need to continue parsing from here, so just go back to
8628 the top. */
8629 goto skip_attribute;
8630
8631 default:
3e43a32a
MS
8632 error (_("Dwarf Error: Cannot handle %s "
8633 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8634 dwarf_form_name (form),
8635 bfd_get_filename (abfd));
8636 }
8637 }
8638
8639 if (abbrev->has_children)
dee91e82 8640 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8641 else
8642 return info_ptr;
8643}
8644
93311388 8645/* Locate ORIG_PDI's sibling.
dee91e82 8646 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8647
d521ce57 8648static const gdb_byte *
dee91e82
DE
8649locate_pdi_sibling (const struct die_reader_specs *reader,
8650 struct partial_die_info *orig_pdi,
d521ce57 8651 const gdb_byte *info_ptr)
91c24f0a
DC
8652{
8653 /* Do we know the sibling already? */
72bf9492 8654
91c24f0a
DC
8655 if (orig_pdi->sibling)
8656 return orig_pdi->sibling;
8657
8658 /* Are there any children to deal with? */
8659
8660 if (!orig_pdi->has_children)
8661 return info_ptr;
8662
4bb7a0a7 8663 /* Skip the children the long way. */
91c24f0a 8664
dee91e82 8665 return skip_children (reader, info_ptr);
91c24f0a
DC
8666}
8667
257e7a09 8668/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8669 not NULL. */
c906108c 8670
891813be
TT
8671void
8672dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8673{
ed2dc618
SM
8674 struct dwarf2_per_objfile *dwarf2_per_objfile
8675 = get_dwarf2_per_objfile (objfile);
8676
077cbab2
TT
8677 gdb_assert (!readin);
8678 /* If this psymtab is constructed from a debug-only objfile, the
8679 has_section_at_zero flag will not necessarily be correct. We
8680 can get the correct value for this flag by looking at the data
8681 associated with the (presumably stripped) associated objfile. */
8682 if (objfile->separate_debug_objfile_backlink)
c906108c 8683 {
077cbab2
TT
8684 struct dwarf2_per_objfile *dpo_backlink
8685 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8686
077cbab2
TT
8687 dwarf2_per_objfile->has_section_at_zero
8688 = dpo_backlink->has_section_at_zero;
8689 }
98bfdba5 8690
8566b89b 8691 expand_psymtab (objfile);
95554aad 8692
ed2dc618 8693 process_cu_includes (dwarf2_per_objfile);
c906108c 8694}
9cdd5dbd
DE
8695\f
8696/* Reading in full CUs. */
c906108c 8697
10b3939b
DJ
8698/* Add PER_CU to the queue. */
8699
8700static void
95554aad
TT
8701queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8702 enum language pretend_language)
10b3939b 8703{
10b3939b 8704 per_cu->queued = 1;
39856def 8705 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8706}
8707
89e63ee4
DE
8708/* If PER_CU is not yet queued, add it to the queue.
8709 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8710 dependency.
0907af0c 8711 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8712 meaning either PER_CU is already queued or it is already loaded.
8713
8714 N.B. There is an invariant here that if a CU is queued then it is loaded.
8715 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8716
8717static int
89e63ee4 8718maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8719 struct dwarf2_per_cu_data *per_cu,
8720 enum language pretend_language)
8721{
8722 /* We may arrive here during partial symbol reading, if we need full
8723 DIEs to process an unusual case (e.g. template arguments). Do
8724 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8725 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8726 {
8727 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8728 return 1;
8729 return 0;
8730 }
8731
8732 /* Mark the dependence relation so that we don't flush PER_CU
8733 too early. */
89e63ee4
DE
8734 if (dependent_cu != NULL)
8735 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8736
8737 /* If it's already on the queue, we have nothing to do. */
8738 if (per_cu->queued)
8739 return 0;
8740
8741 /* If the compilation unit is already loaded, just mark it as
8742 used. */
8743 if (per_cu->cu != NULL)
8744 {
8745 per_cu->cu->last_used = 0;
8746 return 0;
8747 }
8748
8749 /* Add it to the queue. */
8750 queue_comp_unit (per_cu, pretend_language);
8751
8752 return 1;
8753}
8754
10b3939b
DJ
8755/* Process the queue. */
8756
8757static void
ed2dc618 8758process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8759{
b4f54984 8760 if (dwarf_read_debug)
45cfd468
DE
8761 {
8762 fprintf_unfiltered (gdb_stdlog,
8763 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8764 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8765 }
8766
03dd20cc
DJ
8767 /* The queue starts out with one item, but following a DIE reference
8768 may load a new CU, adding it to the end of the queue. */
39856def 8769 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8770 {
39856def
TT
8771 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8772
cc12ce38 8773 if ((dwarf2_per_objfile->using_index
39856def
TT
8774 ? !item.per_cu->v.quick->compunit_symtab
8775 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8776 /* Skip dummy CUs. */
39856def 8777 && item.per_cu->cu != NULL)
f4dc4d17 8778 {
39856def 8779 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8780 unsigned int debug_print_threshold;
247f5c4f 8781 char buf[100];
f4dc4d17 8782
247f5c4f 8783 if (per_cu->is_debug_types)
f4dc4d17 8784 {
247f5c4f
DE
8785 struct signatured_type *sig_type =
8786 (struct signatured_type *) per_cu;
8787
9d8780f0 8788 sprintf (buf, "TU %s at offset %s",
73be47f5 8789 hex_string (sig_type->signature),
9d8780f0 8790 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8791 /* There can be 100s of TUs.
8792 Only print them in verbose mode. */
8793 debug_print_threshold = 2;
f4dc4d17 8794 }
247f5c4f 8795 else
73be47f5 8796 {
9d8780f0
SM
8797 sprintf (buf, "CU at offset %s",
8798 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8799 debug_print_threshold = 1;
8800 }
247f5c4f 8801
b4f54984 8802 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8803 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8804
8805 if (per_cu->is_debug_types)
39856def 8806 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8807 else
39856def 8808 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8809
b4f54984 8810 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8811 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8812 }
10b3939b 8813
39856def
TT
8814 item.per_cu->queued = 0;
8815 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8816 }
8817
b4f54984 8818 if (dwarf_read_debug)
45cfd468
DE
8819 {
8820 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8821 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8822 }
10b3939b
DJ
8823}
8824
10b3939b
DJ
8825/* Read in full symbols for PST, and anything it depends on. */
8826
8566b89b
TT
8827void
8828dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8829{
10b3939b 8830 struct dwarf2_per_cu_data *per_cu;
aaa75496 8831
8566b89b 8832 if (readin)
95554aad
TT
8833 return;
8834
0494dbec 8835 read_dependencies (objfile);
aaa75496 8836
8566b89b 8837 per_cu = per_cu_data;
10b3939b
DJ
8838
8839 if (per_cu == NULL)
aaa75496
JB
8840 {
8841 /* It's an include file, no symbols to read for it.
8842 Everything is in the parent symtab. */
8566b89b 8843 readin = true;
aaa75496
JB
8844 return;
8845 }
c906108c 8846
58f0c718 8847 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
8848}
8849
dee91e82
DE
8850/* Trivial hash function for die_info: the hash value of a DIE
8851 is its offset in .debug_info for this objfile. */
10b3939b 8852
dee91e82
DE
8853static hashval_t
8854die_hash (const void *item)
10b3939b 8855{
9a3c8263 8856 const struct die_info *die = (const struct die_info *) item;
6502dd73 8857
9c541725 8858 return to_underlying (die->sect_off);
dee91e82 8859}
63d06c5c 8860
dee91e82
DE
8861/* Trivial comparison function for die_info structures: two DIEs
8862 are equal if they have the same offset. */
98bfdba5 8863
dee91e82
DE
8864static int
8865die_eq (const void *item_lhs, const void *item_rhs)
8866{
9a3c8263
SM
8867 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8868 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8869
9c541725 8870 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8871}
c906108c 8872
c0ab21c2 8873/* Load the DIEs associated with PER_CU into memory. */
c906108c 8874
dee91e82 8875static void
c0ab21c2
TT
8876load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8877 bool skip_partial,
8878 enum language pretend_language)
dee91e82 8879{
c0ab21c2
TT
8880 gdb_assert (! this_cu->is_debug_types);
8881
6751ebae 8882 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8883 if (reader.dummy_p)
8884 return;
8885
8886 struct dwarf2_cu *cu = reader.cu;
8887 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8888
dee91e82
DE
8889 gdb_assert (cu->die_hash == NULL);
8890 cu->die_hash =
8891 htab_create_alloc_ex (cu->header.length / 12,
8892 die_hash,
8893 die_eq,
8894 NULL,
8895 &cu->comp_unit_obstack,
8896 hashtab_obstack_allocate,
8897 dummy_obstack_deallocate);
e142c38c 8898
3e225074 8899 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8900 reader.comp_unit_die->child
8901 = read_die_and_siblings (&reader, reader.info_ptr,
8902 &info_ptr, reader.comp_unit_die);
8903 cu->dies = reader.comp_unit_die;
dee91e82 8904 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8905
8906 /* We try not to read any attributes in this function, because not
9cdd5dbd 8907 all CUs needed for references have been loaded yet, and symbol
10b3939b 8908 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8909 or we won't be able to build types correctly.
8910 Similarly, if we do not read the producer, we can not apply
8911 producer-specific interpretation. */
c0ab21c2 8912 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8913
8914 reader.keep ();
10b3939b
DJ
8915}
8916
3da10d80
KS
8917/* Add a DIE to the delayed physname list. */
8918
8919static void
8920add_to_method_list (struct type *type, int fnfield_index, int index,
8921 const char *name, struct die_info *die,
8922 struct dwarf2_cu *cu)
8923{
8924 struct delayed_method_info mi;
8925 mi.type = type;
8926 mi.fnfield_index = fnfield_index;
8927 mi.index = index;
8928 mi.name = name;
8929 mi.die = die;
c89b44cd 8930 cu->method_list.push_back (mi);
3da10d80
KS
8931}
8932
3693fdb3
PA
8933/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8934 "const" / "volatile". If so, decrements LEN by the length of the
8935 modifier and return true. Otherwise return false. */
8936
8937template<size_t N>
8938static bool
8939check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8940{
8941 size_t mod_len = sizeof (mod) - 1;
8942 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8943 {
8944 len -= mod_len;
8945 return true;
8946 }
8947 return false;
8948}
8949
3da10d80
KS
8950/* Compute the physnames of any methods on the CU's method list.
8951
8952 The computation of method physnames is delayed in order to avoid the
8953 (bad) condition that one of the method's formal parameters is of an as yet
8954 incomplete type. */
8955
8956static void
8957compute_delayed_physnames (struct dwarf2_cu *cu)
8958{
3693fdb3 8959 /* Only C++ delays computing physnames. */
c89b44cd 8960 if (cu->method_list.empty ())
3693fdb3
PA
8961 return;
8962 gdb_assert (cu->language == language_cplus);
8963
52941706 8964 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8965 {
1d06ead6 8966 const char *physname;
3da10d80 8967 struct fn_fieldlist *fn_flp
c89b44cd
TT
8968 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8969 physname = dwarf2_physname (mi.name, mi.die, cu);
8970 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8971 = physname ? physname : "";
3693fdb3
PA
8972
8973 /* Since there's no tag to indicate whether a method is a
8974 const/volatile overload, extract that information out of the
8975 demangled name. */
8976 if (physname != NULL)
8977 {
8978 size_t len = strlen (physname);
8979
8980 while (1)
8981 {
8982 if (physname[len] == ')') /* shortcut */
8983 break;
8984 else if (check_modifier (physname, len, " const"))
c89b44cd 8985 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8986 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8987 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8988 else
8989 break;
8990 }
8991 }
3da10d80 8992 }
c89b44cd
TT
8993
8994 /* The list is no longer needed. */
8995 cu->method_list.clear ();
3da10d80
KS
8996}
8997
a766d390
DE
8998/* Go objects should be embedded in a DW_TAG_module DIE,
8999 and it's not clear if/how imported objects will appear.
9000 To keep Go support simple until that's worked out,
9001 go back through what we've read and create something usable.
9002 We could do this while processing each DIE, and feels kinda cleaner,
9003 but that way is more invasive.
9004 This is to, for example, allow the user to type "p var" or "b main"
9005 without having to specify the package name, and allow lookups
9006 of module.object to work in contexts that use the expression
9007 parser. */
9008
9009static void
9010fixup_go_packaging (struct dwarf2_cu *cu)
9011{
421d1616 9012 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9013 struct pending *list;
9014 int i;
9015
c24bdb02 9016 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9017 list != NULL;
9018 list = list->next)
a766d390
DE
9019 {
9020 for (i = 0; i < list->nsyms; ++i)
9021 {
9022 struct symbol *sym = list->symbol[i];
9023
c1b5c1eb 9024 if (sym->language () == language_go
a766d390
DE
9025 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9026 {
421d1616
TT
9027 gdb::unique_xmalloc_ptr<char> this_package_name
9028 (go_symbol_package_name (sym));
a766d390
DE
9029
9030 if (this_package_name == NULL)
9031 continue;
9032 if (package_name == NULL)
421d1616 9033 package_name = std::move (this_package_name);
a766d390
DE
9034 else
9035 {
518817b3
SM
9036 struct objfile *objfile
9037 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9038 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9039 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9040 (symbol_symtab (sym) != NULL
9041 ? symtab_to_filename_for_display
9042 (symbol_symtab (sym))
e3b94546 9043 : objfile_name (objfile)),
421d1616 9044 this_package_name.get (), package_name.get ());
a766d390
DE
9045 }
9046 }
9047 }
9048 }
9049
9050 if (package_name != NULL)
9051 {
518817b3 9052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9053 const char *saved_package_name
421d1616 9054 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9055 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9056 saved_package_name);
a766d390
DE
9057 struct symbol *sym;
9058
e623cf5d 9059 sym = allocate_symbol (objfile);
d3ecddab 9060 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9061 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9062 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9063 e.g., "main" finds the "main" module and not C's main(). */
9064 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9065 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9066 SYMBOL_TYPE (sym) = type;
9067
c24bdb02 9068 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9069 }
9070}
9071
c9317f21
TT
9072/* Allocate a fully-qualified name consisting of the two parts on the
9073 obstack. */
9074
9075static const char *
9076rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9077{
9078 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9079}
9080
9081/* A helper that allocates a struct discriminant_info to attach to a
9082 union type. */
9083
9084static struct discriminant_info *
9085alloc_discriminant_info (struct type *type, int discriminant_index,
9086 int default_index)
9087{
9088 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9089 gdb_assert (discriminant_index == -1
9090 || (discriminant_index >= 0
9091 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9092 gdb_assert (default_index == -1
c7b15a66 9093 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9094
9095 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9096
9097 struct discriminant_info *disc
9098 = ((struct discriminant_info *)
9099 TYPE_ZALLOC (type,
9100 offsetof (struct discriminant_info, discriminants)
9101 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9102 disc->default_index = default_index;
9103 disc->discriminant_index = discriminant_index;
9104
9105 struct dynamic_prop prop;
9106 prop.kind = PROP_UNDEFINED;
9107 prop.data.baton = disc;
9108
9109 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9110
9111 return disc;
9112}
9113
9114/* Some versions of rustc emitted enums in an unusual way.
9115
9116 Ordinary enums were emitted as unions. The first element of each
9117 structure in the union was named "RUST$ENUM$DISR". This element
9118 held the discriminant.
9119
9120 These versions of Rust also implemented the "non-zero"
9121 optimization. When the enum had two values, and one is empty and
9122 the other holds a pointer that cannot be zero, the pointer is used
9123 as the discriminant, with a zero value meaning the empty variant.
9124 Here, the union's first member is of the form
9125 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9126 where the fieldnos are the indices of the fields that should be
9127 traversed in order to find the field (which may be several fields deep)
9128 and the variantname is the name of the variant of the case when the
9129 field is zero.
9130
9131 This function recognizes whether TYPE is of one of these forms,
9132 and, if so, smashes it to be a variant type. */
9133
9134static void
9135quirk_rust_enum (struct type *type, struct objfile *objfile)
9136{
9137 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9138
9139 /* We don't need to deal with empty enums. */
9140 if (TYPE_NFIELDS (type) == 0)
9141 return;
9142
9143#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9144 if (TYPE_NFIELDS (type) == 1
9145 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9146 {
9147 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9148
9149 /* Decode the field name to find the offset of the
9150 discriminant. */
9151 ULONGEST bit_offset = 0;
9152 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9153 while (name[0] >= '0' && name[0] <= '9')
9154 {
9155 char *tail;
9156 unsigned long index = strtoul (name, &tail, 10);
9157 name = tail;
9158 if (*name != '$'
9159 || index >= TYPE_NFIELDS (field_type)
9160 || (TYPE_FIELD_LOC_KIND (field_type, index)
9161 != FIELD_LOC_KIND_BITPOS))
9162 {
b98664d3 9163 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9164 "[in module %s]"),
9165 TYPE_FIELD_NAME (type, 0),
9166 objfile_name (objfile));
9167 return;
9168 }
9169 ++name;
9170
9171 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9172 field_type = TYPE_FIELD_TYPE (field_type, index);
9173 }
9174
9175 /* Make a union to hold the variants. */
9176 struct type *union_type = alloc_type (objfile);
9177 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9178 TYPE_NFIELDS (union_type) = 3;
9179 TYPE_FIELDS (union_type)
9180 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9181 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9182 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9183
9184 /* Put the discriminant must at index 0. */
9185 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9186 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9187 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9188 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9189
9190 /* The order of fields doesn't really matter, so put the real
9191 field at index 1 and the data-less field at index 2. */
9192 struct discriminant_info *disc
9193 = alloc_discriminant_info (union_type, 0, 1);
9194 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9195 TYPE_FIELD_NAME (union_type, 1)
9196 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9197 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9198 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9199 TYPE_FIELD_NAME (union_type, 1));
9200
9201 const char *dataless_name
9202 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9203 name);
9204 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9205 dataless_name);
9206 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9207 /* NAME points into the original discriminant name, which
9208 already has the correct lifetime. */
9209 TYPE_FIELD_NAME (union_type, 2) = name;
9210 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9211 disc->discriminants[2] = 0;
9212
9213 /* Smash this type to be a structure type. We have to do this
9214 because the type has already been recorded. */
9215 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9216 TYPE_NFIELDS (type) = 1;
9217 TYPE_FIELDS (type)
9218 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9219
9220 /* Install the variant part. */
9221 TYPE_FIELD_TYPE (type, 0) = union_type;
9222 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9223 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9224 }
77c2dba3
TT
9225 /* A union with a single anonymous field is probably an old-style
9226 univariant enum. */
9227 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9228 {
c9317f21
TT
9229 /* Smash this type to be a structure type. We have to do this
9230 because the type has already been recorded. */
9231 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9232
9233 /* Make a union to hold the variants. */
9234 struct type *union_type = alloc_type (objfile);
9235 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9236 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9237 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9238 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9239 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9240
9241 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9242 const char *variant_name
9243 = rust_last_path_segment (TYPE_NAME (field_type));
9244 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9245 TYPE_NAME (field_type)
9246 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9247 TYPE_NAME (type), variant_name);
c9317f21
TT
9248
9249 /* Install the union in the outer struct type. */
9250 TYPE_NFIELDS (type) = 1;
9251 TYPE_FIELDS (type)
9252 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9253 TYPE_FIELD_TYPE (type, 0) = union_type;
9254 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9255 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9256
9257 alloc_discriminant_info (union_type, -1, 0);
9258 }
9259 else
9260 {
9261 struct type *disr_type = nullptr;
9262 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9263 {
9264 disr_type = TYPE_FIELD_TYPE (type, i);
9265
a037790e
TT
9266 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9267 {
9268 /* All fields of a true enum will be structs. */
9269 return;
9270 }
9271 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9272 {
9273 /* Could be data-less variant, so keep going. */
a037790e 9274 disr_type = nullptr;
c9317f21
TT
9275 }
9276 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9277 "RUST$ENUM$DISR") != 0)
9278 {
9279 /* Not a Rust enum. */
9280 return;
9281 }
9282 else
9283 {
9284 /* Found one. */
9285 break;
9286 }
9287 }
9288
9289 /* If we got here without a discriminant, then it's probably
9290 just a union. */
9291 if (disr_type == nullptr)
9292 return;
9293
9294 /* Smash this type to be a structure type. We have to do this
9295 because the type has already been recorded. */
9296 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9297
9298 /* Make a union to hold the variants. */
9299 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9300 struct type *union_type = alloc_type (objfile);
9301 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9302 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9303 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9304 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9305 TYPE_FIELDS (union_type)
9306 = (struct field *) TYPE_ZALLOC (union_type,
9307 (TYPE_NFIELDS (union_type)
9308 * sizeof (struct field)));
9309
9310 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9311 TYPE_NFIELDS (type) * sizeof (struct field));
9312
9313 /* Install the discriminant at index 0 in the union. */
9314 TYPE_FIELD (union_type, 0) = *disr_field;
9315 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9316 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9317
9318 /* Install the union in the outer struct type. */
9319 TYPE_FIELD_TYPE (type, 0) = union_type;
9320 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9321 TYPE_NFIELDS (type) = 1;
9322
9323 /* Set the size and offset of the union type. */
9324 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9325
9326 /* We need a way to find the correct discriminant given a
9327 variant name. For convenience we build a map here. */
9328 struct type *enum_type = FIELD_TYPE (*disr_field);
9329 std::unordered_map<std::string, ULONGEST> discriminant_map;
9330 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9331 {
9332 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9333 {
9334 const char *name
9335 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9336 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9337 }
9338 }
9339
9340 int n_fields = TYPE_NFIELDS (union_type);
9341 struct discriminant_info *disc
9342 = alloc_discriminant_info (union_type, 0, -1);
9343 /* Skip the discriminant here. */
9344 for (int i = 1; i < n_fields; ++i)
9345 {
9346 /* Find the final word in the name of this variant's type.
9347 That name can be used to look up the correct
9348 discriminant. */
9349 const char *variant_name
9350 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9351 i)));
9352
9353 auto iter = discriminant_map.find (variant_name);
9354 if (iter != discriminant_map.end ())
9355 disc->discriminants[i] = iter->second;
9356
bedda9ac 9357 /* Remove the discriminant field, if it exists. */
c9317f21 9358 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9359 if (TYPE_NFIELDS (sub_type) > 0)
9360 {
9361 --TYPE_NFIELDS (sub_type);
9362 ++TYPE_FIELDS (sub_type);
9363 }
c9317f21
TT
9364 TYPE_FIELD_NAME (union_type, i) = variant_name;
9365 TYPE_NAME (sub_type)
9366 = rust_fully_qualify (&objfile->objfile_obstack,
9367 TYPE_NAME (type), variant_name);
9368 }
9369 }
9370}
9371
9372/* Rewrite some Rust unions to be structures with variants parts. */
9373
9374static void
9375rust_union_quirks (struct dwarf2_cu *cu)
9376{
9377 gdb_assert (cu->language == language_rust);
52941706
SM
9378 for (type *type_ : cu->rust_unions)
9379 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9380 /* We don't need this any more. */
9381 cu->rust_unions.clear ();
c9317f21
TT
9382}
9383
95554aad
TT
9384/* Return the symtab for PER_CU. This works properly regardless of
9385 whether we're using the index or psymtabs. */
9386
43f3e411
DE
9387static struct compunit_symtab *
9388get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9389{
ed2dc618 9390 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9391 ? per_cu->v.quick->compunit_symtab
9392 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9393}
9394
9395/* A helper function for computing the list of all symbol tables
9396 included by PER_CU. */
9397
9398static void
4c39bc03 9399recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9400 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9401 struct dwarf2_per_cu_data *per_cu,
43f3e411 9402 struct compunit_symtab *immediate_parent)
95554aad
TT
9403{
9404 void **slot;
43f3e411 9405 struct compunit_symtab *cust;
95554aad
TT
9406
9407 slot = htab_find_slot (all_children, per_cu, INSERT);
9408 if (*slot != NULL)
9409 {
9410 /* This inclusion and its children have been processed. */
9411 return;
9412 }
9413
9414 *slot = per_cu;
9415 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9416 cust = get_compunit_symtab (per_cu);
9417 if (cust != NULL)
ec94af83
DE
9418 {
9419 /* If this is a type unit only add its symbol table if we haven't
9420 seen it yet (type unit per_cu's can share symtabs). */
9421 if (per_cu->is_debug_types)
9422 {
43f3e411 9423 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9424 if (*slot == NULL)
9425 {
43f3e411 9426 *slot = cust;
4c39bc03 9427 result->push_back (cust);
43f3e411
DE
9428 if (cust->user == NULL)
9429 cust->user = immediate_parent;
ec94af83
DE
9430 }
9431 }
9432 else
f9125b6c 9433 {
4c39bc03 9434 result->push_back (cust);
43f3e411
DE
9435 if (cust->user == NULL)
9436 cust->user = immediate_parent;
f9125b6c 9437 }
ec94af83 9438 }
95554aad 9439
ae640021
AB
9440 if (!per_cu->imported_symtabs_empty ())
9441 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9442 {
9443 recursively_compute_inclusions (result, all_children,
9444 all_type_symtabs, ptr, cust);
9445 }
95554aad
TT
9446}
9447
43f3e411 9448/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9449 PER_CU. */
9450
9451static void
43f3e411 9452compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9453{
f4dc4d17
DE
9454 gdb_assert (! per_cu->is_debug_types);
9455
ae640021 9456 if (!per_cu->imported_symtabs_empty ())
95554aad 9457 {
ae640021 9458 int len;
4c39bc03 9459 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9460 htab_t all_children, all_type_symtabs;
43f3e411 9461 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9462
9463 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9464 if (cust == NULL)
95554aad
TT
9465 return;
9466
9467 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9468 NULL, xcalloc, xfree);
ec94af83
DE
9469 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9470 NULL, xcalloc, xfree);
95554aad 9471
ae640021 9472 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9473 {
9474 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9475 all_type_symtabs, ptr, cust);
ec94af83 9476 }
95554aad 9477
ec94af83 9478 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9479 len = result_symtabs.size ();
43f3e411 9480 cust->includes
ed2dc618 9481 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9482 struct compunit_symtab *, len + 1);
4c39bc03
TT
9483 memcpy (cust->includes, result_symtabs.data (),
9484 len * sizeof (compunit_symtab *));
43f3e411 9485 cust->includes[len] = NULL;
95554aad 9486
95554aad 9487 htab_delete (all_children);
ec94af83 9488 htab_delete (all_type_symtabs);
95554aad
TT
9489 }
9490}
9491
9492/* Compute the 'includes' field for the symtabs of all the CUs we just
9493 read. */
9494
9495static void
ed2dc618 9496process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9497{
71b73764 9498 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9499 {
9500 if (! iter->is_debug_types)
43f3e411 9501 compute_compunit_symtab_includes (iter);
f4dc4d17 9502 }
95554aad 9503
c5d0225d 9504 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9505}
9506
9cdd5dbd 9507/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9508 already been loaded into memory. */
9509
9510static void
95554aad
TT
9511process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9512 enum language pretend_language)
10b3939b 9513{
10b3939b 9514 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9515 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9516 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9517 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9518 CORE_ADDR lowpc, highpc;
43f3e411 9519 struct compunit_symtab *cust;
10b3939b 9520 CORE_ADDR baseaddr;
4359dff1 9521 struct block *static_block;
3e29f34a 9522 CORE_ADDR addr;
10b3939b 9523
b3b3bada 9524 baseaddr = objfile->text_section_offset ();
10b3939b 9525
c89b44cd
TT
9526 /* Clear the list here in case something was left over. */
9527 cu->method_list.clear ();
10b3939b 9528
95554aad
TT
9529 cu->language = pretend_language;
9530 cu->language_defn = language_def (cu->language);
9531
c906108c 9532 /* Do line number decoding in read_file_scope () */
10b3939b 9533 process_die (cu->dies, cu);
c906108c 9534
a766d390
DE
9535 /* For now fudge the Go package. */
9536 if (cu->language == language_go)
9537 fixup_go_packaging (cu);
9538
5f48f8f3 9539 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9540 should be complete, and it should now be safe to compute all of the
9541 physnames. */
9542 compute_delayed_physnames (cu);
3da10d80 9543
c9317f21
TT
9544 if (cu->language == language_rust)
9545 rust_union_quirks (cu);
9546
fae299cd
DC
9547 /* Some compilers don't define a DW_AT_high_pc attribute for the
9548 compilation unit. If the DW_AT_high_pc is missing, synthesize
9549 it, by scanning the DIE's below the compilation unit. */
10b3939b 9550 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9551
3e29f34a 9552 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9553 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9554
9555 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9556 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9557 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9558 addrmap to help ensure it has an accurate map of pc values belonging to
9559 this comp unit. */
9560 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9561
c24bdb02 9562 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9563 SECT_OFF_TEXT (objfile),
9564 0);
c906108c 9565
43f3e411 9566 if (cust != NULL)
c906108c 9567 {
df15bd07 9568 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9569
8be455d7
JK
9570 /* Set symtab language to language from DW_AT_language. If the
9571 compilation is from a C file generated by language preprocessors, do
9572 not set the language if it was already deduced by start_subfile. */
43f3e411 9573 if (!(cu->language == language_c
40e3ad0e 9574 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9575 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9576
9577 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9578 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9579 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9580 there were bugs in prologue debug info, fixed later in GCC-4.5
9581 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9582
9583 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9584 needed, it would be wrong due to missing DW_AT_producer there.
9585
9586 Still one can confuse GDB by using non-standard GCC compilation
9587 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9588 */
ab260dad 9589 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9590 cust->locations_valid = 1;
e0d00bc7
JK
9591
9592 if (gcc_4_minor >= 5)
43f3e411 9593 cust->epilogue_unwind_valid = 1;
96408a79 9594
43f3e411 9595 cust->call_site_htab = cu->call_site_htab;
c906108c 9596 }
9291a0cd
TT
9597
9598 if (dwarf2_per_objfile->using_index)
43f3e411 9599 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9600 else
9601 {
891813be 9602 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9603 pst->compunit_symtab = cust;
6d94535f 9604 pst->readin = true;
9291a0cd 9605 }
c906108c 9606
95554aad 9607 /* Push it for inclusion processing later. */
c5d0225d 9608 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9609
9610 /* Not needed any more. */
c24bdb02 9611 cu->reset_builder ();
f4dc4d17 9612}
45cfd468 9613
f4dc4d17
DE
9614/* Generate full symbol information for type unit PER_CU, whose DIEs have
9615 already been loaded into memory. */
9616
9617static void
9618process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9619 enum language pretend_language)
9620{
9621 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9622 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9623 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9624 struct compunit_symtab *cust;
0186c6a7
DE
9625 struct signatured_type *sig_type;
9626
9627 gdb_assert (per_cu->is_debug_types);
9628 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9629
c89b44cd
TT
9630 /* Clear the list here in case something was left over. */
9631 cu->method_list.clear ();
f4dc4d17 9632
f4dc4d17
DE
9633 cu->language = pretend_language;
9634 cu->language_defn = language_def (cu->language);
9635
9636 /* The symbol tables are set up in read_type_unit_scope. */
9637 process_die (cu->dies, cu);
9638
9639 /* For now fudge the Go package. */
9640 if (cu->language == language_go)
9641 fixup_go_packaging (cu);
9642
5f48f8f3 9643 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9644 should be complete, and it should now be safe to compute all of the
9645 physnames. */
9646 compute_delayed_physnames (cu);
f4dc4d17 9647
c9317f21
TT
9648 if (cu->language == language_rust)
9649 rust_union_quirks (cu);
9650
f4dc4d17
DE
9651 /* TUs share symbol tables.
9652 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9653 of it with end_expandable_symtab. Otherwise, complete the addition of
9654 this TU's symbols to the existing symtab. */
43f3e411 9655 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9656 {
c24bdb02
KS
9657 buildsym_compunit *builder = cu->get_builder ();
9658 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9659 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9660
43f3e411 9661 if (cust != NULL)
f4dc4d17
DE
9662 {
9663 /* Set symtab language to language from DW_AT_language. If the
9664 compilation is from a C file generated by language preprocessors,
9665 do not set the language if it was already deduced by
9666 start_subfile. */
43f3e411
DE
9667 if (!(cu->language == language_c
9668 && COMPUNIT_FILETABS (cust)->language != language_c))
9669 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9670 }
9671 }
9672 else
9673 {
c24bdb02 9674 cu->get_builder ()->augment_type_symtab ();
43f3e411 9675 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9676 }
9677
9678 if (dwarf2_per_objfile->using_index)
43f3e411 9679 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9680 else
9681 {
891813be 9682 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9683 pst->compunit_symtab = cust;
6d94535f 9684 pst->readin = true;
45cfd468 9685 }
804d2729
TT
9686
9687 /* Not needed any more. */
c24bdb02 9688 cu->reset_builder ();
c906108c
SS
9689}
9690
95554aad
TT
9691/* Process an imported unit DIE. */
9692
9693static void
9694process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9695{
9696 struct attribute *attr;
9697
f4dc4d17
DE
9698 /* For now we don't handle imported units in type units. */
9699 if (cu->per_cu->is_debug_types)
9700 {
9701 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9702 " supported in type units [in module %s]"),
518817b3 9703 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9704 }
9705
95554aad
TT
9706 attr = dwarf2_attr (die, DW_AT_import, cu);
9707 if (attr != NULL)
9708 {
9c541725
PA
9709 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9710 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9711 dwarf2_per_cu_data *per_cu
e3b94546 9712 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9713 cu->per_cu->dwarf2_per_objfile);
95554aad 9714
69d751e3 9715 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9716 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9717 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9718
ae640021 9719 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9720 }
9721}
9722
4c8aa72d
PA
9723/* RAII object that represents a process_die scope: i.e.,
9724 starts/finishes processing a DIE. */
9725class process_die_scope
adde2bff 9726{
4c8aa72d
PA
9727public:
9728 process_die_scope (die_info *die, dwarf2_cu *cu)
9729 : m_die (die), m_cu (cu)
9730 {
9731 /* We should only be processing DIEs not already in process. */
9732 gdb_assert (!m_die->in_process);
9733 m_die->in_process = true;
9734 }
8c3cb9fa 9735
4c8aa72d
PA
9736 ~process_die_scope ()
9737 {
9738 m_die->in_process = false;
9739
9740 /* If we're done processing the DIE for the CU that owns the line
9741 header, we don't need the line header anymore. */
9742 if (m_cu->line_header_die_owner == m_die)
9743 {
9744 delete m_cu->line_header;
9745 m_cu->line_header = NULL;
9746 m_cu->line_header_die_owner = NULL;
9747 }
9748 }
9749
9750private:
9751 die_info *m_die;
9752 dwarf2_cu *m_cu;
9753};
adde2bff 9754
c906108c
SS
9755/* Process a die and its children. */
9756
9757static void
e7c27a73 9758process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9759{
4c8aa72d 9760 process_die_scope scope (die, cu);
adde2bff 9761
c906108c
SS
9762 switch (die->tag)
9763 {
9764 case DW_TAG_padding:
9765 break;
9766 case DW_TAG_compile_unit:
95554aad 9767 case DW_TAG_partial_unit:
e7c27a73 9768 read_file_scope (die, cu);
c906108c 9769 break;
348e048f
DE
9770 case DW_TAG_type_unit:
9771 read_type_unit_scope (die, cu);
9772 break;
c906108c 9773 case DW_TAG_subprogram:
0a4b0913
AB
9774 /* Nested subprograms in Fortran get a prefix. */
9775 if (cu->language == language_fortran
9776 && die->parent != NULL
9777 && die->parent->tag == DW_TAG_subprogram)
9778 cu->processing_has_namespace_info = true;
9779 /* Fall through. */
c906108c 9780 case DW_TAG_inlined_subroutine:
edb3359d 9781 read_func_scope (die, cu);
c906108c
SS
9782 break;
9783 case DW_TAG_lexical_block:
14898363
L
9784 case DW_TAG_try_block:
9785 case DW_TAG_catch_block:
e7c27a73 9786 read_lexical_block_scope (die, cu);
c906108c 9787 break;
216f72a1 9788 case DW_TAG_call_site:
96408a79
SA
9789 case DW_TAG_GNU_call_site:
9790 read_call_site_scope (die, cu);
9791 break;
c906108c 9792 case DW_TAG_class_type:
680b30c7 9793 case DW_TAG_interface_type:
c906108c
SS
9794 case DW_TAG_structure_type:
9795 case DW_TAG_union_type:
134d01f1 9796 process_structure_scope (die, cu);
c906108c
SS
9797 break;
9798 case DW_TAG_enumeration_type:
134d01f1 9799 process_enumeration_scope (die, cu);
c906108c 9800 break;
134d01f1 9801
f792889a
DJ
9802 /* These dies have a type, but processing them does not create
9803 a symbol or recurse to process the children. Therefore we can
9804 read them on-demand through read_type_die. */
c906108c 9805 case DW_TAG_subroutine_type:
72019c9c 9806 case DW_TAG_set_type:
c906108c 9807 case DW_TAG_array_type:
c906108c 9808 case DW_TAG_pointer_type:
c906108c 9809 case DW_TAG_ptr_to_member_type:
c906108c 9810 case DW_TAG_reference_type:
4297a3f0 9811 case DW_TAG_rvalue_reference_type:
c906108c 9812 case DW_TAG_string_type:
c906108c 9813 break;
134d01f1 9814
c906108c 9815 case DW_TAG_base_type:
a02abb62 9816 case DW_TAG_subrange_type:
cb249c71 9817 case DW_TAG_typedef:
134d01f1
DJ
9818 /* Add a typedef symbol for the type definition, if it has a
9819 DW_AT_name. */
f792889a 9820 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9821 break;
c906108c 9822 case DW_TAG_common_block:
e7c27a73 9823 read_common_block (die, cu);
c906108c
SS
9824 break;
9825 case DW_TAG_common_inclusion:
9826 break;
d9fa45fe 9827 case DW_TAG_namespace:
9068261f 9828 cu->processing_has_namespace_info = true;
e7c27a73 9829 read_namespace (die, cu);
d9fa45fe 9830 break;
5d7cb8df 9831 case DW_TAG_module:
9068261f 9832 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9833 read_module (die, cu);
9834 break;
d9fa45fe 9835 case DW_TAG_imported_declaration:
9068261f 9836 cu->processing_has_namespace_info = true;
74921315
KS
9837 if (read_namespace_alias (die, cu))
9838 break;
86a73007
TT
9839 /* The declaration is not a global namespace alias. */
9840 /* Fall through. */
d9fa45fe 9841 case DW_TAG_imported_module:
9068261f 9842 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9843 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9844 || cu->language != language_fortran))
b98664d3 9845 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9846 dwarf_tag_name (die->tag));
9847 read_import_statement (die, cu);
d9fa45fe 9848 break;
95554aad
TT
9849
9850 case DW_TAG_imported_unit:
9851 process_imported_unit_die (die, cu);
9852 break;
9853
71a3c369
TT
9854 case DW_TAG_variable:
9855 read_variable (die, cu);
9856 break;
9857
c906108c 9858 default:
e7c27a73 9859 new_symbol (die, NULL, cu);
c906108c
SS
9860 break;
9861 }
9862}
ca69b9e6
DE
9863\f
9864/* DWARF name computation. */
c906108c 9865
94af9270
KS
9866/* A helper function for dwarf2_compute_name which determines whether DIE
9867 needs to have the name of the scope prepended to the name listed in the
9868 die. */
9869
9870static int
9871die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9872{
1c809c68
TT
9873 struct attribute *attr;
9874
94af9270
KS
9875 switch (die->tag)
9876 {
9877 case DW_TAG_namespace:
9878 case DW_TAG_typedef:
9879 case DW_TAG_class_type:
9880 case DW_TAG_interface_type:
9881 case DW_TAG_structure_type:
9882 case DW_TAG_union_type:
9883 case DW_TAG_enumeration_type:
9884 case DW_TAG_enumerator:
9885 case DW_TAG_subprogram:
08a76f8a 9886 case DW_TAG_inlined_subroutine:
94af9270 9887 case DW_TAG_member:
74921315 9888 case DW_TAG_imported_declaration:
94af9270
KS
9889 return 1;
9890
9891 case DW_TAG_variable:
c2b0a229 9892 case DW_TAG_constant:
94af9270
KS
9893 /* We only need to prefix "globally" visible variables. These include
9894 any variable marked with DW_AT_external or any variable that
9895 lives in a namespace. [Variables in anonymous namespaces
9896 require prefixing, but they are not DW_AT_external.] */
9897
9898 if (dwarf2_attr (die, DW_AT_specification, cu))
9899 {
9900 struct dwarf2_cu *spec_cu = cu;
9a619af0 9901
94af9270
KS
9902 return die_needs_namespace (die_specification (die, &spec_cu),
9903 spec_cu);
9904 }
9905
1c809c68 9906 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9907 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9908 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9909 return 0;
9910 /* A variable in a lexical block of some kind does not need a
9911 namespace, even though in C++ such variables may be external
9912 and have a mangled name. */
9913 if (die->parent->tag == DW_TAG_lexical_block
9914 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9915 || die->parent->tag == DW_TAG_catch_block
9916 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9917 return 0;
9918 return 1;
94af9270
KS
9919
9920 default:
9921 return 0;
9922 }
9923}
9924
73b9be8b
KS
9925/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9926 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9927 defined for the given DIE. */
9928
9929static struct attribute *
9930dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9931{
9932 struct attribute *attr;
9933
9934 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9935 if (attr == NULL)
9936 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9937
9938 return attr;
9939}
9940
9941/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9942 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9943 defined for the given DIE. */
9944
9945static const char *
9946dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9947{
9948 const char *linkage_name;
9949
9950 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9951 if (linkage_name == NULL)
9952 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9953
9954 return linkage_name;
9955}
9956
94af9270 9957/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9958 compute the physname for the object, which include a method's:
9c37b5ae 9959 - formal parameters (C++),
a766d390 9960 - receiver type (Go),
a766d390
DE
9961
9962 The term "physname" is a bit confusing.
9963 For C++, for example, it is the demangled name.
9964 For Go, for example, it's the mangled name.
94af9270 9965
af6b7be1
JB
9966 For Ada, return the DIE's linkage name rather than the fully qualified
9967 name. PHYSNAME is ignored..
9968
94af9270
KS
9969 The result is allocated on the objfile_obstack and canonicalized. */
9970
9971static const char *
15d034d0
TT
9972dwarf2_compute_name (const char *name,
9973 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9974 int physname)
9975{
518817b3 9976 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 9977
94af9270
KS
9978 if (name == NULL)
9979 name = dwarf2_name (die, cu);
9980
2ee7123e
DE
9981 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9982 but otherwise compute it by typename_concat inside GDB.
9983 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 9984 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 9985 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
9986 will set the demangled name to the result of dwarf2_full_name, and it is
9987 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9988 if (cu->language == language_ada
9989 || (cu->language == language_fortran && physname))
9990 {
9991 /* For Ada unit, we prefer the linkage name over the name, as
9992 the former contains the exported name, which the user expects
9993 to be able to reference. Ideally, we want the user to be able
9994 to reference this entity using either natural or linkage name,
9995 but we haven't started looking at this enhancement yet. */
73b9be8b 9996 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9997
2ee7123e
DE
9998 if (linkage_name != NULL)
9999 return linkage_name;
f55ee35c
JK
10000 }
10001
94af9270
KS
10002 /* These are the only languages we know how to qualify names in. */
10003 if (name != NULL
9c37b5ae 10004 && (cu->language == language_cplus
c44af4eb
TT
10005 || cu->language == language_fortran || cu->language == language_d
10006 || cu->language == language_rust))
94af9270
KS
10007 {
10008 if (die_needs_namespace (die, cu))
10009 {
0d5cff50 10010 const char *prefix;
34a68019 10011 const char *canonical_name = NULL;
94af9270 10012
d7e74731
PA
10013 string_file buf;
10014
94af9270 10015 prefix = determine_prefix (die, cu);
94af9270
KS
10016 if (*prefix != '\0')
10017 {
43816ebc
TT
10018 gdb::unique_xmalloc_ptr<char> prefixed_name
10019 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10020
43816ebc 10021 buf.puts (prefixed_name.get ());
94af9270
KS
10022 }
10023 else
d7e74731 10024 buf.puts (name);
94af9270 10025
98bfdba5
PA
10026 /* Template parameters may be specified in the DIE's DW_AT_name, or
10027 as children with DW_TAG_template_type_param or
10028 DW_TAG_value_type_param. If the latter, add them to the name
10029 here. If the name already has template parameters, then
10030 skip this step; some versions of GCC emit both, and
10031 it is more efficient to use the pre-computed name.
10032
10033 Something to keep in mind about this process: it is very
10034 unlikely, or in some cases downright impossible, to produce
10035 something that will match the mangled name of a function.
10036 If the definition of the function has the same debug info,
10037 we should be able to match up with it anyway. But fallbacks
10038 using the minimal symbol, for instance to find a method
10039 implemented in a stripped copy of libstdc++, will not work.
10040 If we do not have debug info for the definition, we will have to
10041 match them up some other way.
10042
10043 When we do name matching there is a related problem with function
10044 templates; two instantiated function templates are allowed to
10045 differ only by their return types, which we do not add here. */
10046
10047 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10048 {
10049 struct attribute *attr;
10050 struct die_info *child;
10051 int first = 1;
10052
10053 die->building_fullname = 1;
10054
10055 for (child = die->child; child != NULL; child = child->sibling)
10056 {
10057 struct type *type;
12df843f 10058 LONGEST value;
d521ce57 10059 const gdb_byte *bytes;
98bfdba5
PA
10060 struct dwarf2_locexpr_baton *baton;
10061 struct value *v;
10062
10063 if (child->tag != DW_TAG_template_type_param
10064 && child->tag != DW_TAG_template_value_param)
10065 continue;
10066
10067 if (first)
10068 {
d7e74731 10069 buf.puts ("<");
98bfdba5
PA
10070 first = 0;
10071 }
10072 else
d7e74731 10073 buf.puts (", ");
98bfdba5
PA
10074
10075 attr = dwarf2_attr (child, DW_AT_type, cu);
10076 if (attr == NULL)
10077 {
b98664d3 10078 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10079 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10080 continue;
10081 }
10082 type = die_type (child, cu);
10083
10084 if (child->tag == DW_TAG_template_type_param)
10085 {
c1ec8cea
TT
10086 c_print_type (type, "", &buf, -1, 0, cu->language,
10087 &type_print_raw_options);
98bfdba5
PA
10088 continue;
10089 }
10090
10091 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10092 if (attr == NULL)
10093 {
b98664d3 10094 complaint (_("template parameter missing "
3e43a32a 10095 "DW_AT_const_value"));
d7e74731 10096 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10097 continue;
10098 }
10099
10100 dwarf2_const_value_attr (attr, type, name,
10101 &cu->comp_unit_obstack, cu,
10102 &value, &bytes, &baton);
10103
10104 if (TYPE_NOSIGN (type))
10105 /* GDB prints characters as NUMBER 'CHAR'. If that's
10106 changed, this can use value_print instead. */
d7e74731 10107 c_printchar (value, type, &buf);
98bfdba5
PA
10108 else
10109 {
10110 struct value_print_options opts;
10111
10112 if (baton != NULL)
10113 v = dwarf2_evaluate_loc_desc (type, NULL,
10114 baton->data,
10115 baton->size,
10116 baton->per_cu);
10117 else if (bytes != NULL)
10118 {
10119 v = allocate_value (type);
10120 memcpy (value_contents_writeable (v), bytes,
10121 TYPE_LENGTH (type));
10122 }
10123 else
10124 v = value_from_longest (type, value);
10125
3e43a32a
MS
10126 /* Specify decimal so that we do not depend on
10127 the radix. */
98bfdba5
PA
10128 get_formatted_print_options (&opts, 'd');
10129 opts.raw = 1;
d7e74731 10130 value_print (v, &buf, &opts);
98bfdba5 10131 release_value (v);
98bfdba5
PA
10132 }
10133 }
10134
10135 die->building_fullname = 0;
10136
10137 if (!first)
10138 {
10139 /* Close the argument list, with a space if necessary
10140 (nested templates). */
d7e74731
PA
10141 if (!buf.empty () && buf.string ().back () == '>')
10142 buf.puts (" >");
98bfdba5 10143 else
d7e74731 10144 buf.puts (">");
98bfdba5
PA
10145 }
10146 }
10147
9c37b5ae 10148 /* For C++ methods, append formal parameter type
94af9270 10149 information, if PHYSNAME. */
6e70227d 10150
94af9270 10151 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10152 && cu->language == language_cplus)
94af9270
KS
10153 {
10154 struct type *type = read_type_die (die, cu);
10155
d7e74731 10156 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10157 &type_print_raw_options);
94af9270 10158
9c37b5ae 10159 if (cu->language == language_cplus)
94af9270 10160 {
60430eff
DJ
10161 /* Assume that an artificial first parameter is
10162 "this", but do not crash if it is not. RealView
10163 marks unnamed (and thus unused) parameters as
10164 artificial; there is no way to differentiate
10165 the two cases. */
94af9270
KS
10166 if (TYPE_NFIELDS (type) > 0
10167 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10168 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10169 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10170 0))))
d7e74731 10171 buf.puts (" const");
94af9270
KS
10172 }
10173 }
10174
d7e74731 10175 const std::string &intermediate_name = buf.string ();
94af9270
KS
10176
10177 if (cu->language == language_cplus)
34a68019 10178 canonical_name
322a8516 10179 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10180 &objfile->per_bfd->storage_obstack);
10181
10182 /* If we only computed INTERMEDIATE_NAME, or if
10183 INTERMEDIATE_NAME is already canonical, then we need to
10184 copy it to the appropriate obstack. */
322a8516 10185 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
10186 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10187 intermediate_name);
34a68019
TT
10188 else
10189 name = canonical_name;
94af9270
KS
10190 }
10191 }
10192
10193 return name;
10194}
10195
0114d602
DJ
10196/* Return the fully qualified name of DIE, based on its DW_AT_name.
10197 If scope qualifiers are appropriate they will be added. The result
34a68019 10198 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10199 not have a name. NAME may either be from a previous call to
10200 dwarf2_name or NULL.
10201
9c37b5ae 10202 The output string will be canonicalized (if C++). */
0114d602
DJ
10203
10204static const char *
15d034d0 10205dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10206{
94af9270
KS
10207 return dwarf2_compute_name (name, die, cu, 0);
10208}
0114d602 10209
94af9270
KS
10210/* Construct a physname for the given DIE in CU. NAME may either be
10211 from a previous call to dwarf2_name or NULL. The result will be
10212 allocated on the objfile_objstack or NULL if the DIE does not have a
10213 name.
0114d602 10214
9c37b5ae 10215 The output string will be canonicalized (if C++). */
0114d602 10216
94af9270 10217static const char *
15d034d0 10218dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10219{
518817b3 10220 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10221 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10222 int need_copy = 1;
10223
10224 /* In this case dwarf2_compute_name is just a shortcut not building anything
10225 on its own. */
10226 if (!die_needs_namespace (die, cu))
10227 return dwarf2_compute_name (name, die, cu, 1);
10228
73b9be8b 10229 mangled = dw2_linkage_name (die, cu);
900e11f9 10230
e98c9e7c
TT
10231 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10232 See https://github.com/rust-lang/rust/issues/32925. */
10233 if (cu->language == language_rust && mangled != NULL
10234 && strchr (mangled, '{') != NULL)
10235 mangled = NULL;
10236
900e11f9
JK
10237 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10238 has computed. */
791afaa2 10239 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10240 if (mangled != NULL)
900e11f9 10241 {
900e11f9 10242
59cc4834
JB
10243 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10244 {
10245 /* Do nothing (do not demangle the symbol name). */
10246 }
10247 else if (cu->language == language_go)
a766d390 10248 {
5e2db402
TT
10249 /* This is a lie, but we already lie to the caller new_symbol.
10250 new_symbol assumes we return the mangled name.
a766d390 10251 This just undoes that lie until things are cleaned up. */
a766d390
DE
10252 }
10253 else
10254 {
0eb876f5
JB
10255 /* Use DMGL_RET_DROP for C++ template functions to suppress
10256 their return type. It is easier for GDB users to search
10257 for such functions as `name(params)' than `long name(params)'.
10258 In such case the minimal symbol names do not match the full
10259 symbol names but for template functions there is never a need
10260 to look up their definition from their declaration so
10261 the only disadvantage remains the minimal symbol variant
10262 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10263 demangled.reset (gdb_demangle (mangled,
10264 (DMGL_PARAMS | DMGL_ANSI
10265 | DMGL_RET_DROP)));
a766d390 10266 }
900e11f9 10267 if (demangled)
791afaa2 10268 canon = demangled.get ();
900e11f9
JK
10269 else
10270 {
10271 canon = mangled;
10272 need_copy = 0;
10273 }
10274 }
10275
10276 if (canon == NULL || check_physname)
10277 {
10278 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10279
10280 if (canon != NULL && strcmp (physname, canon) != 0)
10281 {
10282 /* It may not mean a bug in GDB. The compiler could also
10283 compute DW_AT_linkage_name incorrectly. But in such case
10284 GDB would need to be bug-to-bug compatible. */
10285
b98664d3 10286 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10287 "(from linkage <%s>) - DIE at %s [in module %s]"),
10288 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10289 objfile_name (objfile));
900e11f9
JK
10290
10291 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10292 is available here - over computed PHYSNAME. It is safer
10293 against both buggy GDB and buggy compilers. */
10294
10295 retval = canon;
10296 }
10297 else
10298 {
10299 retval = physname;
10300 need_copy = 0;
10301 }
10302 }
10303 else
10304 retval = canon;
10305
10306 if (need_copy)
021887d8 10307 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 10308
900e11f9 10309 return retval;
0114d602
DJ
10310}
10311
74921315
KS
10312/* Inspect DIE in CU for a namespace alias. If one exists, record
10313 a new symbol for it.
10314
10315 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10316
10317static int
10318read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10319{
10320 struct attribute *attr;
10321
10322 /* If the die does not have a name, this is not a namespace
10323 alias. */
10324 attr = dwarf2_attr (die, DW_AT_name, cu);
10325 if (attr != NULL)
10326 {
10327 int num;
10328 struct die_info *d = die;
10329 struct dwarf2_cu *imported_cu = cu;
10330
10331 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10332 keep inspecting DIEs until we hit the underlying import. */
10333#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10334 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10335 {
10336 attr = dwarf2_attr (d, DW_AT_import, cu);
10337 if (attr == NULL)
10338 break;
10339
10340 d = follow_die_ref (d, attr, &imported_cu);
10341 if (d->tag != DW_TAG_imported_declaration)
10342 break;
10343 }
10344
10345 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10346 {
b98664d3 10347 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10348 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10349 return 0;
10350 }
10351
10352 if (attr != NULL)
10353 {
10354 struct type *type;
9c541725 10355 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10356
9c541725 10357 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10358 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10359 {
10360 /* This declaration is a global namespace alias. Add
10361 a symbol for it whose type is the aliased namespace. */
10362 new_symbol (die, type, cu);
10363 return 1;
10364 }
10365 }
10366 }
10367
10368 return 0;
10369}
10370
22cee43f 10371/* Return the using directives repository (global or local?) to use in the
804d2729 10372 current context for CU.
22cee43f
PMR
10373
10374 For Ada, imported declarations can materialize renamings, which *may* be
10375 global. However it is impossible (for now?) in DWARF to distinguish
10376 "external" imported declarations and "static" ones. As all imported
10377 declarations seem to be static in all other languages, make them all CU-wide
10378 global only in Ada. */
10379
10380static struct using_direct **
804d2729 10381using_directives (struct dwarf2_cu *cu)
22cee43f 10382{
c24bdb02
KS
10383 if (cu->language == language_ada
10384 && cu->get_builder ()->outermost_context_p ())
10385 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10386 else
c24bdb02 10387 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10388}
10389
27aa8d6a
SW
10390/* Read the import statement specified by the given die and record it. */
10391
10392static void
10393read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10394{
518817b3 10395 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10396 struct attribute *import_attr;
32019081 10397 struct die_info *imported_die, *child_die;
de4affc9 10398 struct dwarf2_cu *imported_cu;
27aa8d6a 10399 const char *imported_name;
794684b6 10400 const char *imported_name_prefix;
13387711
SW
10401 const char *canonical_name;
10402 const char *import_alias;
10403 const char *imported_declaration = NULL;
794684b6 10404 const char *import_prefix;
eb1e02fd 10405 std::vector<const char *> excludes;
13387711 10406
27aa8d6a
SW
10407 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10408 if (import_attr == NULL)
10409 {
b98664d3 10410 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10411 dwarf_tag_name (die->tag));
10412 return;
10413 }
10414
de4affc9
CC
10415 imported_cu = cu;
10416 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10417 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10418 if (imported_name == NULL)
10419 {
10420 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10421
10422 The import in the following code:
10423 namespace A
10424 {
10425 typedef int B;
10426 }
10427
10428 int main ()
10429 {
10430 using A::B;
10431 B b;
10432 return b;
10433 }
10434
10435 ...
10436 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10437 <52> DW_AT_decl_file : 1
10438 <53> DW_AT_decl_line : 6
10439 <54> DW_AT_import : <0x75>
10440 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10441 <59> DW_AT_name : B
10442 <5b> DW_AT_decl_file : 1
10443 <5c> DW_AT_decl_line : 2
10444 <5d> DW_AT_type : <0x6e>
10445 ...
10446 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10447 <76> DW_AT_byte_size : 4
10448 <77> DW_AT_encoding : 5 (signed)
10449
10450 imports the wrong die ( 0x75 instead of 0x58 ).
10451 This case will be ignored until the gcc bug is fixed. */
10452 return;
10453 }
10454
82856980
SW
10455 /* Figure out the local name after import. */
10456 import_alias = dwarf2_name (die, cu);
27aa8d6a 10457
794684b6
SW
10458 /* Figure out where the statement is being imported to. */
10459 import_prefix = determine_prefix (die, cu);
10460
10461 /* Figure out what the scope of the imported die is and prepend it
10462 to the name of the imported die. */
de4affc9 10463 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10464
f55ee35c
JK
10465 if (imported_die->tag != DW_TAG_namespace
10466 && imported_die->tag != DW_TAG_module)
794684b6 10467 {
13387711
SW
10468 imported_declaration = imported_name;
10469 canonical_name = imported_name_prefix;
794684b6 10470 }
13387711 10471 else if (strlen (imported_name_prefix) > 0)
12aaed36 10472 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10473 imported_name_prefix,
10474 (cu->language == language_d ? "." : "::"),
10475 imported_name, (char *) NULL);
13387711
SW
10476 else
10477 canonical_name = imported_name;
794684b6 10478
32019081
JK
10479 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10480 for (child_die = die->child; child_die && child_die->tag;
10481 child_die = sibling_die (child_die))
10482 {
10483 /* DWARF-4: A Fortran use statement with a “rename list” may be
10484 represented by an imported module entry with an import attribute
10485 referring to the module and owned entries corresponding to those
10486 entities that are renamed as part of being imported. */
10487
10488 if (child_die->tag != DW_TAG_imported_declaration)
10489 {
b98664d3 10490 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10491 "- DIE at %s [in module %s]"),
10492 sect_offset_str (child_die->sect_off),
10493 objfile_name (objfile));
32019081
JK
10494 continue;
10495 }
10496
10497 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10498 if (import_attr == NULL)
10499 {
b98664d3 10500 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10501 dwarf_tag_name (child_die->tag));
10502 continue;
10503 }
10504
10505 imported_cu = cu;
10506 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10507 &imported_cu);
10508 imported_name = dwarf2_name (imported_die, imported_cu);
10509 if (imported_name == NULL)
10510 {
b98664d3 10511 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10512 "imported name - DIE at %s [in module %s]"),
10513 sect_offset_str (child_die->sect_off),
10514 objfile_name (objfile));
32019081
JK
10515 continue;
10516 }
10517
eb1e02fd 10518 excludes.push_back (imported_name);
32019081
JK
10519
10520 process_die (child_die, cu);
10521 }
10522
804d2729 10523 add_using_directive (using_directives (cu),
22cee43f
PMR
10524 import_prefix,
10525 canonical_name,
10526 import_alias,
10527 imported_declaration,
10528 excludes,
10529 0,
10530 &objfile->objfile_obstack);
27aa8d6a
SW
10531}
10532
5230b05a
WT
10533/* ICC<14 does not output the required DW_AT_declaration on incomplete
10534 types, but gives them a size of zero. Starting with version 14,
10535 ICC is compatible with GCC. */
10536
9068261f 10537static bool
5230b05a
WT
10538producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10539{
10540 if (!cu->checked_producer)
10541 check_producer (cu);
10542
10543 return cu->producer_is_icc_lt_14;
10544}
10545
eb77c9df
AB
10546/* ICC generates a DW_AT_type for C void functions. This was observed on
10547 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10548 which says that void functions should not have a DW_AT_type. */
10549
10550static bool
10551producer_is_icc (struct dwarf2_cu *cu)
10552{
10553 if (!cu->checked_producer)
10554 check_producer (cu);
10555
10556 return cu->producer_is_icc;
10557}
10558
1b80a9fa
JK
10559/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10560 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10561 this, it was first present in GCC release 4.3.0. */
10562
9068261f 10563static bool
1b80a9fa
JK
10564producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10565{
10566 if (!cu->checked_producer)
10567 check_producer (cu);
10568
10569 return cu->producer_is_gcc_lt_4_3;
10570}
10571
d721ba37
PA
10572static file_and_directory
10573find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10574{
d721ba37
PA
10575 file_and_directory res;
10576
9291a0cd
TT
10577 /* Find the filename. Do not use dwarf2_name here, since the filename
10578 is not a source language identifier. */
d721ba37
PA
10579 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10580 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10581
d721ba37
PA
10582 if (res.comp_dir == NULL
10583 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10584 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10585 {
d721ba37
PA
10586 res.comp_dir_storage = ldirname (res.name);
10587 if (!res.comp_dir_storage.empty ())
10588 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10589 }
d721ba37 10590 if (res.comp_dir != NULL)
9291a0cd
TT
10591 {
10592 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10593 directory, get rid of it. */
d721ba37 10594 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10595
d721ba37
PA
10596 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10597 res.comp_dir = cp + 1;
9291a0cd
TT
10598 }
10599
d721ba37
PA
10600 if (res.name == NULL)
10601 res.name = "<unknown>";
10602
10603 return res;
9291a0cd
TT
10604}
10605
f4dc4d17
DE
10606/* Handle DW_AT_stmt_list for a compilation unit.
10607 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10608 COMP_DIR is the compilation directory. LOWPC is passed to
10609 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10610
10611static void
10612handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10613 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10614{
518817b3
SM
10615 struct dwarf2_per_objfile *dwarf2_per_objfile
10616 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10617 struct attribute *attr;
527f3840
JK
10618 struct line_header line_header_local;
10619 hashval_t line_header_local_hash;
527f3840
JK
10620 void **slot;
10621 int decode_mapping;
2ab95328 10622
f4dc4d17
DE
10623 gdb_assert (! cu->per_cu->is_debug_types);
10624
2ab95328 10625 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10626 if (attr == NULL)
10627 return;
10628
9c541725 10629 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10630
10631 /* The line header hash table is only created if needed (it exists to
10632 prevent redundant reading of the line table for partial_units).
10633 If we're given a partial_unit, we'll need it. If we're given a
10634 compile_unit, then use the line header hash table if it's already
10635 created, but don't create one just yet. */
10636
10637 if (dwarf2_per_objfile->line_header_hash == NULL
10638 && die->tag == DW_TAG_partial_unit)
2ab95328 10639 {
527f3840 10640 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10641 .reset (htab_create_alloc (127, line_header_hash_voidp,
10642 line_header_eq_voidp,
10643 free_line_header_voidp,
10644 xcalloc, xfree));
527f3840 10645 }
2ab95328 10646
9c541725 10647 line_header_local.sect_off = line_offset;
527f3840
JK
10648 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10649 line_header_local_hash = line_header_hash (&line_header_local);
10650 if (dwarf2_per_objfile->line_header_hash != NULL)
10651 {
d15acc42 10652 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10653 &line_header_local,
10654 line_header_local_hash, NO_INSERT);
10655
10656 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10657 is not present in *SLOT (since if there is something in *SLOT then
10658 it will be for a partial_unit). */
10659 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10660 {
527f3840 10661 gdb_assert (*slot != NULL);
9a3c8263 10662 cu->line_header = (struct line_header *) *slot;
527f3840 10663 return;
dee91e82 10664 }
2ab95328 10665 }
527f3840
JK
10666
10667 /* dwarf_decode_line_header does not yet provide sufficient information.
10668 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10669 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10670 if (lh == NULL)
527f3840 10671 return;
4c8aa72d
PA
10672
10673 cu->line_header = lh.release ();
10674 cu->line_header_die_owner = die;
527f3840
JK
10675
10676 if (dwarf2_per_objfile->line_header_hash == NULL)
10677 slot = NULL;
10678 else
10679 {
d15acc42 10680 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10681 &line_header_local,
10682 line_header_local_hash, INSERT);
10683 gdb_assert (slot != NULL);
10684 }
10685 if (slot != NULL && *slot == NULL)
10686 {
10687 /* This newly decoded line number information unit will be owned
10688 by line_header_hash hash table. */
10689 *slot = cu->line_header;
4c8aa72d 10690 cu->line_header_die_owner = NULL;
527f3840
JK
10691 }
10692 else
10693 {
10694 /* We cannot free any current entry in (*slot) as that struct line_header
10695 may be already used by multiple CUs. Create only temporary decoded
10696 line_header for this CU - it may happen at most once for each line
10697 number information unit. And if we're not using line_header_hash
10698 then this is what we want as well. */
10699 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10700 }
10701 decode_mapping = (die->tag != DW_TAG_partial_unit);
10702 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10703 decode_mapping);
fff8551c 10704
2ab95328
TT
10705}
10706
95554aad 10707/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10708
c906108c 10709static void
e7c27a73 10710read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10711{
518817b3
SM
10712 struct dwarf2_per_objfile *dwarf2_per_objfile
10713 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10714 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10715 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10716 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10717 CORE_ADDR highpc = ((CORE_ADDR) 0);
10718 struct attribute *attr;
c906108c 10719 struct die_info *child_die;
e142c38c 10720 CORE_ADDR baseaddr;
6e70227d 10721
380618d6 10722 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10723 baseaddr = objfile->text_section_offset ();
c906108c 10724
fae299cd 10725 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10726
10727 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10728 from finish_block. */
2acceee2 10729 if (lowpc == ((CORE_ADDR) -1))
c906108c 10730 lowpc = highpc;
3e29f34a 10731 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10732
d721ba37 10733 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10734
f4b8a18d
KW
10735 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10736 standardised yet. As a workaround for the language detection we fall
10737 back to the DW_AT_producer string. */
10738 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10739 cu->language = language_opencl;
10740
3019eac3
DE
10741 /* Similar hack for Go. */
10742 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10743 set_cu_language (DW_LANG_Go, cu);
10744
c24bdb02 10745 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10746
10747 /* Decode line number information if present. We do this before
10748 processing child DIEs, so that the line header table is available
10749 for DW_AT_decl_file. */
d721ba37 10750 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10751
10752 /* Process all dies in compilation unit. */
10753 if (die->child != NULL)
10754 {
10755 child_die = die->child;
10756 while (child_die && child_die->tag)
10757 {
10758 process_die (child_die, cu);
10759 child_die = sibling_die (child_die);
10760 }
10761 }
10762
10763 /* Decode macro information, if present. Dwarf 2 macro information
10764 refers to information in the line number info statement program
10765 header, so we can only read it if we've read the header
10766 successfully. */
0af92d60
JK
10767 attr = dwarf2_attr (die, DW_AT_macros, cu);
10768 if (attr == NULL)
10769 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10770 if (attr && cu->line_header)
10771 {
10772 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10773 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10774
43f3e411 10775 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10776 }
10777 else
10778 {
10779 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10780 if (attr && cu->line_header)
10781 {
10782 unsigned int macro_offset = DW_UNSND (attr);
10783
43f3e411 10784 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10785 }
10786 }
3019eac3
DE
10787}
10788
c24bdb02
KS
10789void
10790dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10791{
f4dc4d17
DE
10792 struct type_unit_group *tu_group;
10793 int first_time;
3019eac3 10794 struct attribute *attr;
9c541725 10795 unsigned int i;
0186c6a7 10796 struct signatured_type *sig_type;
3019eac3 10797
f4dc4d17 10798 gdb_assert (per_cu->is_debug_types);
0186c6a7 10799 sig_type = (struct signatured_type *) per_cu;
3019eac3 10800
c24bdb02 10801 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10802
f4dc4d17 10803 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10804 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10805 if (sig_type->type_unit_group == NULL)
c24bdb02 10806 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10807 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10808
10809 /* If we've already processed this stmt_list there's no real need to
10810 do it again, we could fake it and just recreate the part we need
10811 (file name,index -> symtab mapping). If data shows this optimization
10812 is useful we can do it then. */
43f3e411 10813 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10814
10815 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10816 debug info. */
fff8551c 10817 line_header_up lh;
f4dc4d17 10818 if (attr != NULL)
3019eac3 10819 {
9c541725 10820 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10821 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10822 }
10823 if (lh == NULL)
10824 {
10825 if (first_time)
c24bdb02 10826 start_symtab ("", NULL, 0);
f4dc4d17
DE
10827 else
10828 {
10829 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10830 gdb_assert (m_builder == nullptr);
804d2729 10831 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10832 m_builder.reset (new struct buildsym_compunit
10833 (COMPUNIT_OBJFILE (cust), "",
10834 COMPUNIT_DIRNAME (cust),
10835 compunit_language (cust),
10836 0, cust));
f4dc4d17 10837 }
f4dc4d17 10838 return;
3019eac3
DE
10839 }
10840
c24bdb02
KS
10841 line_header = lh.release ();
10842 line_header_die_owner = die;
3019eac3 10843
f4dc4d17
DE
10844 if (first_time)
10845 {
c24bdb02 10846 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10847
1fd60fc0
DE
10848 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10849 still initializing it, and our caller (a few levels up)
10850 process_full_type_unit still needs to know if this is the first
10851 time. */
10852
4ac93832
TT
10853 tu_group->symtabs
10854 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10855 struct symtab *, line_header->file_names_size ());
3019eac3 10856
7ba99d21
AT
10857 auto &file_names = line_header->file_names ();
10858 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10859 {
7ba99d21 10860 file_entry &fe = file_names[i];
c24bdb02
KS
10861 dwarf2_start_subfile (this, fe.name,
10862 fe.include_dir (line_header));
10863 buildsym_compunit *b = get_builder ();
10864 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10865 {
4c8aa72d
PA
10866 /* NOTE: start_subfile will recognize when it's been
10867 passed a file it has already seen. So we can't
10868 assume there's a simple mapping from
10869 cu->line_header->file_names to subfiles, plus
10870 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10871 b->get_current_subfile ()->symtab
10872 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10873 }
10874
c24bdb02 10875 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10876 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10877 }
10878 }
10879 else
3019eac3 10880 {
c24bdb02 10881 gdb_assert (m_builder == nullptr);
804d2729 10882 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10883 m_builder.reset (new struct buildsym_compunit
10884 (COMPUNIT_OBJFILE (cust), "",
10885 COMPUNIT_DIRNAME (cust),
10886 compunit_language (cust),
10887 0, cust));
f4dc4d17 10888
7ba99d21
AT
10889 auto &file_names = line_header->file_names ();
10890 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10891 {
7ba99d21 10892 file_entry &fe = file_names[i];
4c8aa72d 10893 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10894 }
3019eac3
DE
10895 }
10896
f4dc4d17
DE
10897 /* The main symtab is allocated last. Type units don't have DW_AT_name
10898 so they don't have a "real" (so to speak) symtab anyway.
10899 There is later code that will assign the main symtab to all symbols
10900 that don't have one. We need to handle the case of a symbol with a
10901 missing symtab (DW_AT_decl_file) anyway. */
10902}
3019eac3 10903
f4dc4d17
DE
10904/* Process DW_TAG_type_unit.
10905 For TUs we want to skip the first top level sibling if it's not the
10906 actual type being defined by this TU. In this case the first top
10907 level sibling is there to provide context only. */
3019eac3 10908
f4dc4d17
DE
10909static void
10910read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10911{
10912 struct die_info *child_die;
3019eac3 10913
f4dc4d17
DE
10914 prepare_one_comp_unit (cu, die, language_minimal);
10915
10916 /* Initialize (or reinitialize) the machinery for building symtabs.
10917 We do this before processing child DIEs, so that the line header table
10918 is available for DW_AT_decl_file. */
c24bdb02 10919 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10920
10921 if (die->child != NULL)
10922 {
10923 child_die = die->child;
10924 while (child_die && child_die->tag)
10925 {
10926 process_die (child_die, cu);
10927 child_die = sibling_die (child_die);
10928 }
10929 }
3019eac3
DE
10930}
10931\f
80626a55
DE
10932/* DWO/DWP files.
10933
10934 http://gcc.gnu.org/wiki/DebugFission
10935 http://gcc.gnu.org/wiki/DebugFissionDWP
10936
10937 To simplify handling of both DWO files ("object" files with the DWARF info)
10938 and DWP files (a file with the DWOs packaged up into one file), we treat
10939 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10940
10941static hashval_t
10942hash_dwo_file (const void *item)
10943{
9a3c8263 10944 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10945 hashval_t hash;
3019eac3 10946
a2ce51a0
DE
10947 hash = htab_hash_string (dwo_file->dwo_name);
10948 if (dwo_file->comp_dir != NULL)
10949 hash += htab_hash_string (dwo_file->comp_dir);
10950 return hash;
3019eac3
DE
10951}
10952
10953static int
10954eq_dwo_file (const void *item_lhs, const void *item_rhs)
10955{
9a3c8263
SM
10956 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10957 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10958
a2ce51a0
DE
10959 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10960 return 0;
10961 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10962 return lhs->comp_dir == rhs->comp_dir;
10963 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10964}
10965
10966/* Allocate a hash table for DWO files. */
10967
51ac9db5 10968static htab_up
298e9637 10969allocate_dwo_file_hash_table ()
3019eac3 10970{
51ac9db5
SM
10971 auto delete_dwo_file = [] (void *item)
10972 {
10973 struct dwo_file *dwo_file = (struct dwo_file *) item;
10974
10975 delete dwo_file;
10976 };
10977
bc68fb19
TT
10978 return htab_up (htab_create_alloc (41,
10979 hash_dwo_file,
10980 eq_dwo_file,
10981 delete_dwo_file,
10982 xcalloc, xfree));
3019eac3
DE
10983}
10984
80626a55
DE
10985/* Lookup DWO file DWO_NAME. */
10986
10987static void **
ed2dc618
SM
10988lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10989 const char *dwo_name,
10990 const char *comp_dir)
80626a55
DE
10991{
10992 struct dwo_file find_entry;
10993 void **slot;
10994
10995 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 10996 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 10997
0ac5b59e
DE
10998 find_entry.dwo_name = dwo_name;
10999 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11000 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11001 INSERT);
80626a55
DE
11002
11003 return slot;
11004}
11005
3019eac3
DE
11006static hashval_t
11007hash_dwo_unit (const void *item)
11008{
9a3c8263 11009 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11010
11011 /* This drops the top 32 bits of the id, but is ok for a hash. */
11012 return dwo_unit->signature;
11013}
11014
11015static int
11016eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11017{
9a3c8263
SM
11018 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11019 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11020
11021 /* The signature is assumed to be unique within the DWO file.
11022 So while object file CU dwo_id's always have the value zero,
11023 that's OK, assuming each object file DWO file has only one CU,
11024 and that's the rule for now. */
11025 return lhs->signature == rhs->signature;
11026}
11027
11028/* Allocate a hash table for DWO CUs,TUs.
11029 There is one of these tables for each of CUs,TUs for each DWO file. */
11030
b0b6a987 11031static htab_up
298e9637 11032allocate_dwo_unit_table ()
3019eac3
DE
11033{
11034 /* Start out with a pretty small number.
11035 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11036 return htab_up (htab_create_alloc (3,
11037 hash_dwo_unit,
11038 eq_dwo_unit,
11039 NULL, xcalloc, xfree));
3019eac3
DE
11040}
11041
19c3d4c9 11042/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11043
11044static void
19c3d4c9
DE
11045create_dwo_cu_reader (const struct die_reader_specs *reader,
11046 const gdb_byte *info_ptr,
11047 struct die_info *comp_unit_die,
c0ab21c2
TT
11048 struct dwo_file *dwo_file,
11049 struct dwo_unit *dwo_unit)
3019eac3
DE
11050{
11051 struct dwarf2_cu *cu = reader->cu;
9c541725 11052 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11053 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11054
a084a2a6
AT
11055 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11056 if (!signature.has_value ())
3019eac3 11057 {
b98664d3 11058 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11059 " its dwo_id [in module %s]"),
9d8780f0 11060 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11061 return;
11062 }
11063
3019eac3 11064 dwo_unit->dwo_file = dwo_file;
a084a2a6 11065 dwo_unit->signature = *signature;
8a0459fd 11066 dwo_unit->section = section;
9c541725 11067 dwo_unit->sect_off = sect_off;
3019eac3
DE
11068 dwo_unit->length = cu->per_cu->length;
11069
b4f54984 11070 if (dwarf_read_debug)
9d8780f0
SM
11071 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11072 sect_offset_str (sect_off),
9c541725 11073 hex_string (dwo_unit->signature));
3019eac3
DE
11074}
11075
33c5cd75 11076/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11077 Note: This function processes DWO files only, not DWP files. */
3019eac3 11078
33c5cd75 11079static void
ed2dc618 11080create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11081 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11082 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11083{
11084 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11085 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11086
96b79293 11087 section.read (objfile);
33c5cd75 11088 info_ptr = section.buffer;
3019eac3
DE
11089
11090 if (info_ptr == NULL)
33c5cd75 11091 return;
3019eac3 11092
b4f54984 11093 if (dwarf_read_debug)
19c3d4c9
DE
11094 {
11095 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11096 section.get_name (),
11097 section.get_file_name ());
19c3d4c9 11098 }
3019eac3 11099
33c5cd75 11100 end_ptr = info_ptr + section.size;
3019eac3
DE
11101 while (info_ptr < end_ptr)
11102 {
11103 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11104 struct dwo_unit read_unit {};
33c5cd75
DB
11105 struct dwo_unit *dwo_unit;
11106 void **slot;
11107 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11108
11109 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11110 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11111 per_cu.is_debug_types = 0;
33c5cd75
DB
11112 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11113 per_cu.section = &section;
11114
c0ab21c2
TT
11115 cutu_reader reader (&per_cu, cu, &dwo_file);
11116 if (!reader.dummy_p)
11117 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11118 &dwo_file, &read_unit);
33c5cd75
DB
11119 info_ptr += per_cu.length;
11120
11121 // If the unit could not be parsed, skip it.
c0ab21c2 11122 if (read_unit.dwo_file == NULL)
33c5cd75 11123 continue;
3019eac3 11124
33c5cd75 11125 if (cus_htab == NULL)
298e9637 11126 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11127
33c5cd75 11128 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11129 *dwo_unit = read_unit;
b0b6a987 11130 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11131 gdb_assert (slot != NULL);
11132 if (*slot != NULL)
19c3d4c9 11133 {
33c5cd75
DB
11134 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11135 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11136
b98664d3 11137 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11138 " the entry at offset %s, signature %s"),
11139 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11140 hex_string (dwo_unit->signature));
19c3d4c9 11141 }
33c5cd75 11142 *slot = (void *)dwo_unit;
3019eac3 11143 }
3019eac3
DE
11144}
11145
80626a55
DE
11146/* DWP file .debug_{cu,tu}_index section format:
11147 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11148
d2415c6c
DE
11149 DWP Version 1:
11150
80626a55
DE
11151 Both index sections have the same format, and serve to map a 64-bit
11152 signature to a set of section numbers. Each section begins with a header,
11153 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11154 indexes, and a pool of 32-bit section numbers. The index sections will be
11155 aligned at 8-byte boundaries in the file.
11156
d2415c6c
DE
11157 The index section header consists of:
11158
11159 V, 32 bit version number
11160 -, 32 bits unused
11161 N, 32 bit number of compilation units or type units in the index
11162 M, 32 bit number of slots in the hash table
80626a55 11163
d2415c6c 11164 Numbers are recorded using the byte order of the application binary.
80626a55 11165
d2415c6c
DE
11166 The hash table begins at offset 16 in the section, and consists of an array
11167 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11168 order of the application binary). Unused slots in the hash table are 0.
11169 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11170
d2415c6c
DE
11171 The parallel table begins immediately after the hash table
11172 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11173 array of 32-bit indexes (using the byte order of the application binary),
11174 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11175 table contains a 32-bit index into the pool of section numbers. For unused
11176 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11177
73869dc2
DE
11178 The pool of section numbers begins immediately following the hash table
11179 (at offset 16 + 12 * M from the beginning of the section). The pool of
11180 section numbers consists of an array of 32-bit words (using the byte order
11181 of the application binary). Each item in the array is indexed starting
11182 from 0. The hash table entry provides the index of the first section
11183 number in the set. Additional section numbers in the set follow, and the
11184 set is terminated by a 0 entry (section number 0 is not used in ELF).
11185
11186 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11187 section must be the first entry in the set, and the .debug_abbrev.dwo must
11188 be the second entry. Other members of the set may follow in any order.
11189
11190 ---
11191
11192 DWP Version 2:
11193
11194 DWP Version 2 combines all the .debug_info, etc. sections into one,
11195 and the entries in the index tables are now offsets into these sections.
11196 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11197 section.
11198
11199 Index Section Contents:
11200 Header
11201 Hash Table of Signatures dwp_hash_table.hash_table
11202 Parallel Table of Indices dwp_hash_table.unit_table
11203 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11204 Table of Section Sizes dwp_hash_table.v2.sizes
11205
11206 The index section header consists of:
11207
11208 V, 32 bit version number
11209 L, 32 bit number of columns in the table of section offsets
11210 N, 32 bit number of compilation units or type units in the index
11211 M, 32 bit number of slots in the hash table
11212
11213 Numbers are recorded using the byte order of the application binary.
11214
11215 The hash table has the same format as version 1.
11216 The parallel table of indices has the same format as version 1,
11217 except that the entries are origin-1 indices into the table of sections
11218 offsets and the table of section sizes.
11219
11220 The table of offsets begins immediately following the parallel table
11221 (at offset 16 + 12 * M from the beginning of the section). The table is
11222 a two-dimensional array of 32-bit words (using the byte order of the
11223 application binary), with L columns and N+1 rows, in row-major order.
11224 Each row in the array is indexed starting from 0. The first row provides
11225 a key to the remaining rows: each column in this row provides an identifier
11226 for a debug section, and the offsets in the same column of subsequent rows
11227 refer to that section. The section identifiers are:
11228
11229 DW_SECT_INFO 1 .debug_info.dwo
11230 DW_SECT_TYPES 2 .debug_types.dwo
11231 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11232 DW_SECT_LINE 4 .debug_line.dwo
11233 DW_SECT_LOC 5 .debug_loc.dwo
11234 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11235 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11236 DW_SECT_MACRO 8 .debug_macro.dwo
11237
11238 The offsets provided by the CU and TU index sections are the base offsets
11239 for the contributions made by each CU or TU to the corresponding section
11240 in the package file. Each CU and TU header contains an abbrev_offset
11241 field, used to find the abbreviations table for that CU or TU within the
11242 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11243 be interpreted as relative to the base offset given in the index section.
11244 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11245 should be interpreted as relative to the base offset for .debug_line.dwo,
11246 and offsets into other debug sections obtained from DWARF attributes should
11247 also be interpreted as relative to the corresponding base offset.
11248
11249 The table of sizes begins immediately following the table of offsets.
11250 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11251 with L columns and N rows, in row-major order. Each row in the array is
11252 indexed starting from 1 (row 0 is shared by the two tables).
11253
11254 ---
11255
11256 Hash table lookup is handled the same in version 1 and 2:
11257
11258 We assume that N and M will not exceed 2^32 - 1.
11259 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11260
d2415c6c
DE
11261 Given a 64-bit compilation unit signature or a type signature S, an entry
11262 in the hash table is located as follows:
80626a55 11263
d2415c6c
DE
11264 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11265 the low-order k bits all set to 1.
80626a55 11266
d2415c6c 11267 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11268
d2415c6c
DE
11269 3) If the hash table entry at index H matches the signature, use that
11270 entry. If the hash table entry at index H is unused (all zeroes),
11271 terminate the search: the signature is not present in the table.
80626a55 11272
d2415c6c 11273 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11274
d2415c6c 11275 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11276 to stop at an unused slot or find the match. */
80626a55
DE
11277
11278/* Create a hash table to map DWO IDs to their CU/TU entry in
11279 .debug_{info,types}.dwo in DWP_FILE.
11280 Returns NULL if there isn't one.
11281 Note: This function processes DWP files only, not DWO files. */
11282
11283static struct dwp_hash_table *
ed2dc618
SM
11284create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11285 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11286{
11287 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11288 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11289 const gdb_byte *index_ptr, *index_end;
80626a55 11290 struct dwarf2_section_info *index;
73869dc2 11291 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11292 struct dwp_hash_table *htab;
11293
11294 if (is_debug_types)
11295 index = &dwp_file->sections.tu_index;
11296 else
11297 index = &dwp_file->sections.cu_index;
11298
96b79293 11299 if (index->empty ())
80626a55 11300 return NULL;
96b79293 11301 index->read (objfile);
80626a55
DE
11302
11303 index_ptr = index->buffer;
11304 index_end = index_ptr + index->size;
11305
11306 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11307 index_ptr += 4;
11308 if (version == 2)
11309 nr_columns = read_4_bytes (dbfd, index_ptr);
11310 else
11311 nr_columns = 0;
11312 index_ptr += 4;
80626a55
DE
11313 nr_units = read_4_bytes (dbfd, index_ptr);
11314 index_ptr += 4;
11315 nr_slots = read_4_bytes (dbfd, index_ptr);
11316 index_ptr += 4;
11317
73869dc2 11318 if (version != 1 && version != 2)
80626a55 11319 {
21aa081e 11320 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11321 " [in module %s]"),
21aa081e 11322 pulongest (version), dwp_file->name);
80626a55
DE
11323 }
11324 if (nr_slots != (nr_slots & -nr_slots))
11325 {
21aa081e 11326 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11327 " is not power of 2 [in module %s]"),
21aa081e 11328 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11329 }
11330
11331 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11332 htab->version = version;
11333 htab->nr_columns = nr_columns;
80626a55
DE
11334 htab->nr_units = nr_units;
11335 htab->nr_slots = nr_slots;
11336 htab->hash_table = index_ptr;
11337 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11338
11339 /* Exit early if the table is empty. */
11340 if (nr_slots == 0 || nr_units == 0
11341 || (version == 2 && nr_columns == 0))
11342 {
11343 /* All must be zero. */
11344 if (nr_slots != 0 || nr_units != 0
11345 || (version == 2 && nr_columns != 0))
11346 {
b98664d3 11347 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11348 " all zero [in modules %s]"),
11349 dwp_file->name);
11350 }
11351 return htab;
11352 }
11353
11354 if (version == 1)
11355 {
11356 htab->section_pool.v1.indices =
11357 htab->unit_table + sizeof (uint32_t) * nr_slots;
11358 /* It's harder to decide whether the section is too small in v1.
11359 V1 is deprecated anyway so we punt. */
11360 }
11361 else
11362 {
11363 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11364 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11365 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11366 /* Reverse map for error checking. */
11367 int ids_seen[DW_SECT_MAX + 1];
11368 int i;
11369
11370 if (nr_columns < 2)
11371 {
11372 error (_("Dwarf Error: bad DWP hash table, too few columns"
11373 " in section table [in module %s]"),
11374 dwp_file->name);
11375 }
11376 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11377 {
11378 error (_("Dwarf Error: bad DWP hash table, too many columns"
11379 " in section table [in module %s]"),
11380 dwp_file->name);
11381 }
04fd5eed
GB
11382 memset (ids, 255, sizeof_ids);
11383 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11384 for (i = 0; i < nr_columns; ++i)
11385 {
11386 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11387
11388 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11389 {
11390 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11391 " in section table [in module %s]"),
11392 id, dwp_file->name);
11393 }
11394 if (ids_seen[id] != -1)
11395 {
11396 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11397 " id %d in section table [in module %s]"),
11398 id, dwp_file->name);
11399 }
11400 ids_seen[id] = i;
11401 ids[i] = id;
11402 }
11403 /* Must have exactly one info or types section. */
11404 if (((ids_seen[DW_SECT_INFO] != -1)
11405 + (ids_seen[DW_SECT_TYPES] != -1))
11406 != 1)
11407 {
11408 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11409 " DWO info/types section [in module %s]"),
11410 dwp_file->name);
11411 }
11412 /* Must have an abbrev section. */
11413 if (ids_seen[DW_SECT_ABBREV] == -1)
11414 {
11415 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11416 " section [in module %s]"),
11417 dwp_file->name);
11418 }
11419 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11420 htab->section_pool.v2.sizes =
11421 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11422 * nr_units * nr_columns);
11423 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11424 * nr_units * nr_columns))
11425 > index_end)
11426 {
11427 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11428 " [in module %s]"),
11429 dwp_file->name);
11430 }
11431 }
80626a55
DE
11432
11433 return htab;
11434}
11435
11436/* Update SECTIONS with the data from SECTP.
11437
11438 This function is like the other "locate" section routines that are
11439 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11440 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11441
11442 The result is non-zero for success, or zero if an error was found. */
11443
11444static int
73869dc2
DE
11445locate_v1_virtual_dwo_sections (asection *sectp,
11446 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11447{
11448 const struct dwop_section_names *names = &dwop_section_names;
11449
11450 if (section_is_p (sectp->name, &names->abbrev_dwo))
11451 {
11452 /* There can be only one. */
049412e3 11453 if (sections->abbrev.s.section != NULL)
80626a55 11454 return 0;
049412e3 11455 sections->abbrev.s.section = sectp;
fd361982 11456 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11457 }
11458 else if (section_is_p (sectp->name, &names->info_dwo)
11459 || section_is_p (sectp->name, &names->types_dwo))
11460 {
11461 /* There can be only one. */
049412e3 11462 if (sections->info_or_types.s.section != NULL)
80626a55 11463 return 0;
049412e3 11464 sections->info_or_types.s.section = sectp;
fd361982 11465 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11466 }
11467 else if (section_is_p (sectp->name, &names->line_dwo))
11468 {
11469 /* There can be only one. */
049412e3 11470 if (sections->line.s.section != NULL)
80626a55 11471 return 0;
049412e3 11472 sections->line.s.section = sectp;
fd361982 11473 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11474 }
11475 else if (section_is_p (sectp->name, &names->loc_dwo))
11476 {
11477 /* There can be only one. */
049412e3 11478 if (sections->loc.s.section != NULL)
80626a55 11479 return 0;
049412e3 11480 sections->loc.s.section = sectp;
fd361982 11481 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11482 }
11483 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11484 {
11485 /* There can be only one. */
049412e3 11486 if (sections->macinfo.s.section != NULL)
80626a55 11487 return 0;
049412e3 11488 sections->macinfo.s.section = sectp;
fd361982 11489 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11490 }
11491 else if (section_is_p (sectp->name, &names->macro_dwo))
11492 {
11493 /* There can be only one. */
049412e3 11494 if (sections->macro.s.section != NULL)
80626a55 11495 return 0;
049412e3 11496 sections->macro.s.section = sectp;
fd361982 11497 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11498 }
11499 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11500 {
11501 /* There can be only one. */
049412e3 11502 if (sections->str_offsets.s.section != NULL)
80626a55 11503 return 0;
049412e3 11504 sections->str_offsets.s.section = sectp;
fd361982 11505 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11506 }
11507 else
11508 {
11509 /* No other kind of section is valid. */
11510 return 0;
11511 }
11512
11513 return 1;
11514}
11515
73869dc2
DE
11516/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11517 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11518 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11519 This is for DWP version 1 files. */
80626a55
DE
11520
11521static struct dwo_unit *
ed2dc618
SM
11522create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11523 struct dwp_file *dwp_file,
73869dc2
DE
11524 uint32_t unit_index,
11525 const char *comp_dir,
11526 ULONGEST signature, int is_debug_types)
80626a55
DE
11527{
11528 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11529 const struct dwp_hash_table *dwp_htab =
11530 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11531 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11532 const char *kind = is_debug_types ? "TU" : "CU";
11533 struct dwo_file *dwo_file;
11534 struct dwo_unit *dwo_unit;
73869dc2 11535 struct virtual_v1_dwo_sections sections;
80626a55 11536 void **dwo_file_slot;
80626a55
DE
11537 int i;
11538
73869dc2
DE
11539 gdb_assert (dwp_file->version == 1);
11540
b4f54984 11541 if (dwarf_read_debug)
80626a55 11542 {
73869dc2 11543 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11544 kind,
73869dc2 11545 pulongest (unit_index), hex_string (signature),
80626a55
DE
11546 dwp_file->name);
11547 }
11548
19ac8c2e 11549 /* Fetch the sections of this DWO unit.
80626a55
DE
11550 Put a limit on the number of sections we look for so that bad data
11551 doesn't cause us to loop forever. */
11552
73869dc2 11553#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11554 (1 /* .debug_info or .debug_types */ \
11555 + 1 /* .debug_abbrev */ \
11556 + 1 /* .debug_line */ \
11557 + 1 /* .debug_loc */ \
11558 + 1 /* .debug_str_offsets */ \
19ac8c2e 11559 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11560 + 1 /* trailing zero */)
11561
11562 memset (&sections, 0, sizeof (sections));
80626a55 11563
73869dc2 11564 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11565 {
11566 asection *sectp;
11567 uint32_t section_nr =
11568 read_4_bytes (dbfd,
73869dc2
DE
11569 dwp_htab->section_pool.v1.indices
11570 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11571
11572 if (section_nr == 0)
11573 break;
11574 if (section_nr >= dwp_file->num_sections)
11575 {
11576 error (_("Dwarf Error: bad DWP hash table, section number too large"
11577 " [in module %s]"),
11578 dwp_file->name);
11579 }
11580
11581 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11582 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11583 {
11584 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11585 " [in module %s]"),
11586 dwp_file->name);
11587 }
11588 }
11589
11590 if (i < 2
96b79293
TT
11591 || sections.info_or_types.empty ()
11592 || sections.abbrev.empty ())
80626a55
DE
11593 {
11594 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11595 " [in module %s]"),
11596 dwp_file->name);
11597 }
73869dc2 11598 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11599 {
11600 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11601 " [in module %s]"),
11602 dwp_file->name);
11603 }
11604
11605 /* It's easier for the rest of the code if we fake a struct dwo_file and
11606 have dwo_unit "live" in that. At least for now.
11607
11608 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11609 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11610 file, we can combine them back into a virtual DWO file to save space
11611 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11612 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11613
791afaa2
TT
11614 std::string virtual_dwo_name =
11615 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11616 sections.abbrev.get_id (),
11617 sections.line.get_id (),
11618 sections.loc.get_id (),
11619 sections.str_offsets.get_id ());
80626a55 11620 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11621 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11622 virtual_dwo_name.c_str (),
11623 comp_dir);
80626a55
DE
11624 /* Create one if necessary. */
11625 if (*dwo_file_slot == NULL)
11626 {
b4f54984 11627 if (dwarf_read_debug)
80626a55
DE
11628 {
11629 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11630 virtual_dwo_name.c_str ());
80626a55 11631 }
51ac9db5 11632 dwo_file = new struct dwo_file;
efba19b0
TT
11633 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11634 virtual_dwo_name);
0ac5b59e 11635 dwo_file->comp_dir = comp_dir;
80626a55
DE
11636 dwo_file->sections.abbrev = sections.abbrev;
11637 dwo_file->sections.line = sections.line;
11638 dwo_file->sections.loc = sections.loc;
11639 dwo_file->sections.macinfo = sections.macinfo;
11640 dwo_file->sections.macro = sections.macro;
11641 dwo_file->sections.str_offsets = sections.str_offsets;
11642 /* The "str" section is global to the entire DWP file. */
11643 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11644 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11645 there's no need to record it in dwo_file.
11646 Also, we can't simply record type sections in dwo_file because
11647 we record a pointer into the vector in dwo_unit. As we collect more
11648 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11649 for it, invalidating all copies of pointers into the previous
11650 contents. */
80626a55
DE
11651 *dwo_file_slot = dwo_file;
11652 }
11653 else
11654 {
b4f54984 11655 if (dwarf_read_debug)
80626a55
DE
11656 {
11657 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11658 virtual_dwo_name.c_str ());
80626a55 11659 }
9a3c8263 11660 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11661 }
80626a55
DE
11662
11663 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11664 dwo_unit->dwo_file = dwo_file;
11665 dwo_unit->signature = signature;
8d749320
SM
11666 dwo_unit->section =
11667 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11668 *dwo_unit->section = sections.info_or_types;
57d63ce2 11669 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11670
11671 return dwo_unit;
11672}
11673
73869dc2
DE
11674/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11675 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11676 piece within that section used by a TU/CU, return a virtual section
11677 of just that piece. */
11678
11679static struct dwarf2_section_info
ed2dc618
SM
11680create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11681 struct dwarf2_section_info *section,
73869dc2
DE
11682 bfd_size_type offset, bfd_size_type size)
11683{
11684 struct dwarf2_section_info result;
11685 asection *sectp;
11686
11687 gdb_assert (section != NULL);
11688 gdb_assert (!section->is_virtual);
11689
11690 memset (&result, 0, sizeof (result));
11691 result.s.containing_section = section;
dc4ccb6f 11692 result.is_virtual = true;
73869dc2
DE
11693
11694 if (size == 0)
11695 return result;
11696
96b79293 11697 sectp = section->get_bfd_section ();
73869dc2
DE
11698
11699 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11700 bounds of the real section. This is a pretty-rare event, so just
11701 flag an error (easier) instead of a warning and trying to cope. */
11702 if (sectp == NULL
fd361982 11703 || offset + size > bfd_section_size (sectp))
73869dc2 11704 {
73869dc2
DE
11705 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11706 " in section %s [in module %s]"),
fd361982 11707 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11708 objfile_name (dwarf2_per_objfile->objfile));
11709 }
11710
11711 result.virtual_offset = offset;
11712 result.size = size;
11713 return result;
11714}
11715
11716/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11717 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11718 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11719 This is for DWP version 2 files. */
11720
11721static struct dwo_unit *
ed2dc618
SM
11722create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11723 struct dwp_file *dwp_file,
73869dc2
DE
11724 uint32_t unit_index,
11725 const char *comp_dir,
11726 ULONGEST signature, int is_debug_types)
11727{
11728 struct objfile *objfile = dwarf2_per_objfile->objfile;
11729 const struct dwp_hash_table *dwp_htab =
11730 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11731 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11732 const char *kind = is_debug_types ? "TU" : "CU";
11733 struct dwo_file *dwo_file;
11734 struct dwo_unit *dwo_unit;
11735 struct virtual_v2_dwo_sections sections;
11736 void **dwo_file_slot;
73869dc2
DE
11737 int i;
11738
11739 gdb_assert (dwp_file->version == 2);
11740
b4f54984 11741 if (dwarf_read_debug)
73869dc2
DE
11742 {
11743 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11744 kind,
11745 pulongest (unit_index), hex_string (signature),
11746 dwp_file->name);
11747 }
11748
11749 /* Fetch the section offsets of this DWO unit. */
11750
11751 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11752
11753 for (i = 0; i < dwp_htab->nr_columns; ++i)
11754 {
11755 uint32_t offset = read_4_bytes (dbfd,
11756 dwp_htab->section_pool.v2.offsets
11757 + (((unit_index - 1) * dwp_htab->nr_columns
11758 + i)
11759 * sizeof (uint32_t)));
11760 uint32_t size = read_4_bytes (dbfd,
11761 dwp_htab->section_pool.v2.sizes
11762 + (((unit_index - 1) * dwp_htab->nr_columns
11763 + i)
11764 * sizeof (uint32_t)));
11765
11766 switch (dwp_htab->section_pool.v2.section_ids[i])
11767 {
11768 case DW_SECT_INFO:
11769 case DW_SECT_TYPES:
11770 sections.info_or_types_offset = offset;
11771 sections.info_or_types_size = size;
11772 break;
11773 case DW_SECT_ABBREV:
11774 sections.abbrev_offset = offset;
11775 sections.abbrev_size = size;
11776 break;
11777 case DW_SECT_LINE:
11778 sections.line_offset = offset;
11779 sections.line_size = size;
11780 break;
11781 case DW_SECT_LOC:
11782 sections.loc_offset = offset;
11783 sections.loc_size = size;
11784 break;
11785 case DW_SECT_STR_OFFSETS:
11786 sections.str_offsets_offset = offset;
11787 sections.str_offsets_size = size;
11788 break;
11789 case DW_SECT_MACINFO:
11790 sections.macinfo_offset = offset;
11791 sections.macinfo_size = size;
11792 break;
11793 case DW_SECT_MACRO:
11794 sections.macro_offset = offset;
11795 sections.macro_size = size;
11796 break;
11797 }
11798 }
11799
11800 /* It's easier for the rest of the code if we fake a struct dwo_file and
11801 have dwo_unit "live" in that. At least for now.
11802
11803 The DWP file can be made up of a random collection of CUs and TUs.
11804 However, for each CU + set of TUs that came from the same original DWO
11805 file, we can combine them back into a virtual DWO file to save space
11806 (fewer struct dwo_file objects to allocate). Remember that for really
11807 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11808
791afaa2
TT
11809 std::string virtual_dwo_name =
11810 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11811 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11812 (long) (sections.line_size ? sections.line_offset : 0),
11813 (long) (sections.loc_size ? sections.loc_offset : 0),
11814 (long) (sections.str_offsets_size
11815 ? sections.str_offsets_offset : 0));
73869dc2 11816 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11817 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11818 virtual_dwo_name.c_str (),
11819 comp_dir);
73869dc2
DE
11820 /* Create one if necessary. */
11821 if (*dwo_file_slot == NULL)
11822 {
b4f54984 11823 if (dwarf_read_debug)
73869dc2
DE
11824 {
11825 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11826 virtual_dwo_name.c_str ());
73869dc2 11827 }
51ac9db5 11828 dwo_file = new struct dwo_file;
efba19b0
TT
11829 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11830 virtual_dwo_name);
73869dc2
DE
11831 dwo_file->comp_dir = comp_dir;
11832 dwo_file->sections.abbrev =
ed2dc618 11833 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11834 sections.abbrev_offset, sections.abbrev_size);
11835 dwo_file->sections.line =
ed2dc618 11836 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11837 sections.line_offset, sections.line_size);
11838 dwo_file->sections.loc =
ed2dc618 11839 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11840 sections.loc_offset, sections.loc_size);
11841 dwo_file->sections.macinfo =
ed2dc618 11842 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11843 sections.macinfo_offset, sections.macinfo_size);
11844 dwo_file->sections.macro =
ed2dc618 11845 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11846 sections.macro_offset, sections.macro_size);
11847 dwo_file->sections.str_offsets =
ed2dc618
SM
11848 create_dwp_v2_section (dwarf2_per_objfile,
11849 &dwp_file->sections.str_offsets,
73869dc2
DE
11850 sections.str_offsets_offset,
11851 sections.str_offsets_size);
11852 /* The "str" section is global to the entire DWP file. */
11853 dwo_file->sections.str = dwp_file->sections.str;
11854 /* The info or types section is assigned below to dwo_unit,
11855 there's no need to record it in dwo_file.
11856 Also, we can't simply record type sections in dwo_file because
11857 we record a pointer into the vector in dwo_unit. As we collect more
11858 types we'll grow the vector and eventually have to reallocate space
11859 for it, invalidating all copies of pointers into the previous
11860 contents. */
11861 *dwo_file_slot = dwo_file;
11862 }
11863 else
11864 {
b4f54984 11865 if (dwarf_read_debug)
73869dc2
DE
11866 {
11867 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11868 virtual_dwo_name.c_str ());
73869dc2 11869 }
9a3c8263 11870 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11871 }
73869dc2
DE
11872
11873 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11874 dwo_unit->dwo_file = dwo_file;
11875 dwo_unit->signature = signature;
8d749320
SM
11876 dwo_unit->section =
11877 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11878 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11879 is_debug_types
73869dc2
DE
11880 ? &dwp_file->sections.types
11881 : &dwp_file->sections.info,
11882 sections.info_or_types_offset,
11883 sections.info_or_types_size);
11884 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11885
11886 return dwo_unit;
11887}
11888
57d63ce2
DE
11889/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11890 Returns NULL if the signature isn't found. */
80626a55
DE
11891
11892static struct dwo_unit *
ed2dc618
SM
11893lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11894 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11895 ULONGEST signature, int is_debug_types)
80626a55 11896{
57d63ce2
DE
11897 const struct dwp_hash_table *dwp_htab =
11898 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11899 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11900 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11901 uint32_t hash = signature & mask;
11902 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11903 unsigned int i;
11904 void **slot;
870f88f7 11905 struct dwo_unit find_dwo_cu;
80626a55
DE
11906
11907 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11908 find_dwo_cu.signature = signature;
19ac8c2e 11909 slot = htab_find_slot (is_debug_types
48b490f2
TT
11910 ? dwp_file->loaded_tus.get ()
11911 : dwp_file->loaded_cus.get (),
19ac8c2e 11912 &find_dwo_cu, INSERT);
80626a55
DE
11913
11914 if (*slot != NULL)
9a3c8263 11915 return (struct dwo_unit *) *slot;
80626a55
DE
11916
11917 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11918 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11919 {
11920 ULONGEST signature_in_table;
11921
11922 signature_in_table =
57d63ce2 11923 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11924 if (signature_in_table == signature)
11925 {
57d63ce2
DE
11926 uint32_t unit_index =
11927 read_4_bytes (dbfd,
11928 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11929
73869dc2
DE
11930 if (dwp_file->version == 1)
11931 {
ed2dc618
SM
11932 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11933 dwp_file, unit_index,
73869dc2
DE
11934 comp_dir, signature,
11935 is_debug_types);
11936 }
11937 else
11938 {
ed2dc618
SM
11939 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11940 dwp_file, unit_index,
73869dc2
DE
11941 comp_dir, signature,
11942 is_debug_types);
11943 }
9a3c8263 11944 return (struct dwo_unit *) *slot;
80626a55
DE
11945 }
11946 if (signature_in_table == 0)
11947 return NULL;
11948 hash = (hash + hash2) & mask;
11949 }
11950
11951 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11952 " [in module %s]"),
11953 dwp_file->name);
11954}
11955
ab5088bf 11956/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11957 Open the file specified by FILE_NAME and hand it off to BFD for
11958 preliminary analysis. Return a newly initialized bfd *, which
11959 includes a canonicalized copy of FILE_NAME.
80626a55 11960 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11961 SEARCH_CWD is true if the current directory is to be searched.
11962 It will be searched before debug-file-directory.
13aaf454
DE
11963 If successful, the file is added to the bfd include table of the
11964 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11965 If unable to find/open the file, return NULL.
3019eac3
DE
11966 NOTE: This function is derived from symfile_bfd_open. */
11967
192b62ce 11968static gdb_bfd_ref_ptr
ed2dc618
SM
11969try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11970 const char *file_name, int is_dwp, int search_cwd)
3019eac3 11971{
24b9144d 11972 int desc;
9c02c129
DE
11973 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11974 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11975 to debug_file_directory. */
e0cc99a6 11976 const char *search_path;
9c02c129
DE
11977 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11978
e0cc99a6 11979 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
11980 if (search_cwd)
11981 {
11982 if (*debug_file_directory != '\0')
e0cc99a6
TT
11983 {
11984 search_path_holder.reset (concat (".", dirname_separator_string,
11985 debug_file_directory,
11986 (char *) NULL));
11987 search_path = search_path_holder.get ();
11988 }
6ac97d4c 11989 else
e0cc99a6 11990 search_path = ".";
6ac97d4c 11991 }
9c02c129 11992 else
e0cc99a6 11993 search_path = debug_file_directory;
3019eac3 11994
24b9144d 11995 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
11996 if (is_dwp)
11997 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
11998
11999 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12000 desc = openp (search_path, flags, file_name,
3019eac3
DE
12001 O_RDONLY | O_BINARY, &absolute_name);
12002 if (desc < 0)
12003 return NULL;
12004
e0cc99a6
TT
12005 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12006 gnutarget, desc));
9c02c129
DE
12007 if (sym_bfd == NULL)
12008 return NULL;
192b62ce 12009 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12010
192b62ce
TT
12011 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12012 return NULL;
3019eac3 12013
13aaf454
DE
12014 /* Success. Record the bfd as having been included by the objfile's bfd.
12015 This is important because things like demangled_names_hash lives in the
12016 objfile's per_bfd space and may have references to things like symbol
12017 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12018 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12019
3019eac3
DE
12020 return sym_bfd;
12021}
12022
ab5088bf 12023/* Try to open DWO file FILE_NAME.
3019eac3
DE
12024 COMP_DIR is the DW_AT_comp_dir attribute.
12025 The result is the bfd handle of the file.
12026 If there is a problem finding or opening the file, return NULL.
12027 Upon success, the canonicalized path of the file is stored in the bfd,
12028 same as symfile_bfd_open. */
12029
192b62ce 12030static gdb_bfd_ref_ptr
ed2dc618
SM
12031open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12032 const char *file_name, const char *comp_dir)
3019eac3 12033{
80626a55 12034 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12035 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12036 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12037
12038 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12039
12040 if (comp_dir != NULL)
12041 {
43816ebc
TT
12042 gdb::unique_xmalloc_ptr<char> path_to_try
12043 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12044
12045 /* NOTE: If comp_dir is a relative path, this will also try the
12046 search path, which seems useful. */
ed2dc618 12047 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12048 path_to_try.get (),
ed2dc618 12049 0 /*is_dwp*/,
192b62ce 12050 1 /*search_cwd*/));
3019eac3
DE
12051 if (abfd != NULL)
12052 return abfd;
12053 }
12054
12055 /* That didn't work, try debug-file-directory, which, despite its name,
12056 is a list of paths. */
12057
12058 if (*debug_file_directory == '\0')
12059 return NULL;
12060
ed2dc618
SM
12061 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12062 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12063}
12064
80626a55
DE
12065/* This function is mapped across the sections and remembers the offset and
12066 size of each of the DWO debugging sections we are interested in. */
12067
12068static void
12069dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12070{
9a3c8263 12071 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12072 const struct dwop_section_names *names = &dwop_section_names;
12073
12074 if (section_is_p (sectp->name, &names->abbrev_dwo))
12075 {
049412e3 12076 dwo_sections->abbrev.s.section = sectp;
fd361982 12077 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12078 }
12079 else if (section_is_p (sectp->name, &names->info_dwo))
12080 {
049412e3 12081 dwo_sections->info.s.section = sectp;
fd361982 12082 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12083 }
12084 else if (section_is_p (sectp->name, &names->line_dwo))
12085 {
049412e3 12086 dwo_sections->line.s.section = sectp;
fd361982 12087 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12088 }
12089 else if (section_is_p (sectp->name, &names->loc_dwo))
12090 {
049412e3 12091 dwo_sections->loc.s.section = sectp;
fd361982 12092 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12093 }
12094 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12095 {
049412e3 12096 dwo_sections->macinfo.s.section = sectp;
fd361982 12097 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12098 }
12099 else if (section_is_p (sectp->name, &names->macro_dwo))
12100 {
049412e3 12101 dwo_sections->macro.s.section = sectp;
fd361982 12102 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12103 }
12104 else if (section_is_p (sectp->name, &names->str_dwo))
12105 {
049412e3 12106 dwo_sections->str.s.section = sectp;
fd361982 12107 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12108 }
12109 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12110 {
049412e3 12111 dwo_sections->str_offsets.s.section = sectp;
fd361982 12112 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12113 }
12114 else if (section_is_p (sectp->name, &names->types_dwo))
12115 {
12116 struct dwarf2_section_info type_section;
12117
12118 memset (&type_section, 0, sizeof (type_section));
049412e3 12119 type_section.s.section = sectp;
fd361982 12120 type_section.size = bfd_section_size (sectp);
fd5866f6 12121 dwo_sections->types.push_back (type_section);
80626a55
DE
12122 }
12123}
12124
ab5088bf 12125/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12126 by PER_CU. This is for the non-DWP case.
80626a55 12127 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12128
12129static struct dwo_file *
0ac5b59e
DE
12130open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12131 const char *dwo_name, const char *comp_dir)
3019eac3 12132{
ed2dc618 12133 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12134
fb1eb2f9 12135 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12136 if (dbfd == NULL)
12137 {
b4f54984 12138 if (dwarf_read_debug)
80626a55
DE
12139 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12140 return NULL;
12141 }
263db9a1 12142
51ac9db5 12143 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12144 dwo_file->dwo_name = dwo_name;
12145 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12146 dwo_file->dbfd = std::move (dbfd);
3019eac3 12147
fb1eb2f9 12148 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12149 &dwo_file->sections);
3019eac3 12150
18a8505e
AT
12151 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12152 dwo_file->sections.info, dwo_file->cus);
3019eac3 12153
263db9a1 12154 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12155 dwo_file->sections.types, dwo_file->tus);
3019eac3 12156
b4f54984 12157 if (dwarf_read_debug)
80626a55
DE
12158 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12159
263db9a1 12160 return dwo_file.release ();
3019eac3
DE
12161}
12162
80626a55 12163/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12164 size of each of the DWP debugging sections common to version 1 and 2 that
12165 we are interested in. */
3019eac3 12166
80626a55 12167static void
73869dc2
DE
12168dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12169 void *dwp_file_ptr)
3019eac3 12170{
9a3c8263 12171 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12172 const struct dwop_section_names *names = &dwop_section_names;
12173 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12174
80626a55 12175 /* Record the ELF section number for later lookup: this is what the
73869dc2 12176 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12177 gdb_assert (elf_section_nr < dwp_file->num_sections);
12178 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12179
80626a55
DE
12180 /* Look for specific sections that we need. */
12181 if (section_is_p (sectp->name, &names->str_dwo))
12182 {
049412e3 12183 dwp_file->sections.str.s.section = sectp;
fd361982 12184 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12185 }
12186 else if (section_is_p (sectp->name, &names->cu_index))
12187 {
049412e3 12188 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12189 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12190 }
12191 else if (section_is_p (sectp->name, &names->tu_index))
12192 {
049412e3 12193 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12194 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12195 }
12196}
3019eac3 12197
73869dc2
DE
12198/* This function is mapped across the sections and remembers the offset and
12199 size of each of the DWP version 2 debugging sections that we are interested
12200 in. This is split into a separate function because we don't know if we
12201 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12202
12203static void
12204dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12205{
9a3c8263 12206 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12207 const struct dwop_section_names *names = &dwop_section_names;
12208 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12209
12210 /* Record the ELF section number for later lookup: this is what the
12211 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12212 gdb_assert (elf_section_nr < dwp_file->num_sections);
12213 dwp_file->elf_sections[elf_section_nr] = sectp;
12214
12215 /* Look for specific sections that we need. */
12216 if (section_is_p (sectp->name, &names->abbrev_dwo))
12217 {
049412e3 12218 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12219 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12220 }
12221 else if (section_is_p (sectp->name, &names->info_dwo))
12222 {
049412e3 12223 dwp_file->sections.info.s.section = sectp;
fd361982 12224 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12225 }
12226 else if (section_is_p (sectp->name, &names->line_dwo))
12227 {
049412e3 12228 dwp_file->sections.line.s.section = sectp;
fd361982 12229 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12230 }
12231 else if (section_is_p (sectp->name, &names->loc_dwo))
12232 {
049412e3 12233 dwp_file->sections.loc.s.section = sectp;
fd361982 12234 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12235 }
12236 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12237 {
049412e3 12238 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12239 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12240 }
12241 else if (section_is_p (sectp->name, &names->macro_dwo))
12242 {
049412e3 12243 dwp_file->sections.macro.s.section = sectp;
fd361982 12244 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12245 }
12246 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12247 {
049412e3 12248 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12249 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12250 }
12251 else if (section_is_p (sectp->name, &names->types_dwo))
12252 {
049412e3 12253 dwp_file->sections.types.s.section = sectp;
fd361982 12254 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12255 }
12256}
12257
80626a55 12258/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12259
80626a55
DE
12260static hashval_t
12261hash_dwp_loaded_cutus (const void *item)
12262{
9a3c8263 12263 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12264
80626a55
DE
12265 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12266 return dwo_unit->signature;
3019eac3
DE
12267}
12268
80626a55 12269/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12270
80626a55
DE
12271static int
12272eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12273{
9a3c8263
SM
12274 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12275 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12276
80626a55
DE
12277 return dua->signature == dub->signature;
12278}
3019eac3 12279
80626a55 12280/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12281
48b490f2 12282static htab_up
298e9637 12283allocate_dwp_loaded_cutus_table ()
80626a55 12284{
48b490f2
TT
12285 return htab_up (htab_create_alloc (3,
12286 hash_dwp_loaded_cutus,
12287 eq_dwp_loaded_cutus,
12288 NULL, xcalloc, xfree));
80626a55 12289}
3019eac3 12290
ab5088bf
DE
12291/* Try to open DWP file FILE_NAME.
12292 The result is the bfd handle of the file.
12293 If there is a problem finding or opening the file, return NULL.
12294 Upon success, the canonicalized path of the file is stored in the bfd,
12295 same as symfile_bfd_open. */
12296
192b62ce 12297static gdb_bfd_ref_ptr
ed2dc618
SM
12298open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12299 const char *file_name)
ab5088bf 12300{
ed2dc618
SM
12301 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12302 1 /*is_dwp*/,
192b62ce 12303 1 /*search_cwd*/));
6ac97d4c
DE
12304 if (abfd != NULL)
12305 return abfd;
12306
12307 /* Work around upstream bug 15652.
12308 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12309 [Whether that's a "bug" is debatable, but it is getting in our way.]
12310 We have no real idea where the dwp file is, because gdb's realpath-ing
12311 of the executable's path may have discarded the needed info.
12312 [IWBN if the dwp file name was recorded in the executable, akin to
12313 .gnu_debuglink, but that doesn't exist yet.]
12314 Strip the directory from FILE_NAME and search again. */
12315 if (*debug_file_directory != '\0')
12316 {
12317 /* Don't implicitly search the current directory here.
12318 If the user wants to search "." to handle this case,
12319 it must be added to debug-file-directory. */
ed2dc618
SM
12320 return try_open_dwop_file (dwarf2_per_objfile,
12321 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12322 0 /*search_cwd*/);
12323 }
12324
12325 return NULL;
ab5088bf
DE
12326}
12327
80626a55
DE
12328/* Initialize the use of the DWP file for the current objfile.
12329 By convention the name of the DWP file is ${objfile}.dwp.
12330 The result is NULL if it can't be found. */
a766d390 12331
400174b1 12332static std::unique_ptr<struct dwp_file>
ed2dc618 12333open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12334{
12335 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12336
82bf32bc
JK
12337 /* Try to find first .dwp for the binary file before any symbolic links
12338 resolving. */
6c447423
DE
12339
12340 /* If the objfile is a debug file, find the name of the real binary
12341 file and get the name of dwp file from there. */
d721ba37 12342 std::string dwp_name;
6c447423
DE
12343 if (objfile->separate_debug_objfile_backlink != NULL)
12344 {
12345 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12346 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12347
d721ba37 12348 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12349 }
12350 else
d721ba37
PA
12351 dwp_name = objfile->original_name;
12352
12353 dwp_name += ".dwp";
80626a55 12354
ed2dc618 12355 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12356 if (dbfd == NULL
12357 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12358 {
12359 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12360 dwp_name = objfile_name (objfile);
12361 dwp_name += ".dwp";
ed2dc618 12362 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12363 }
12364
80626a55
DE
12365 if (dbfd == NULL)
12366 {
b4f54984 12367 if (dwarf_read_debug)
d721ba37 12368 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12369 return std::unique_ptr<dwp_file> ();
3019eac3 12370 }
400174b1
TT
12371
12372 const char *name = bfd_get_filename (dbfd.get ());
12373 std::unique_ptr<struct dwp_file> dwp_file
12374 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12375
0a0f4c01 12376 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12377 dwp_file->elf_sections =
12378 OBSTACK_CALLOC (&objfile->objfile_obstack,
12379 dwp_file->num_sections, asection *);
12380
400174b1
TT
12381 bfd_map_over_sections (dwp_file->dbfd.get (),
12382 dwarf2_locate_common_dwp_sections,
12383 dwp_file.get ());
80626a55 12384
400174b1
TT
12385 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12386 0);
80626a55 12387
400174b1
TT
12388 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12389 1);
80626a55 12390
73869dc2 12391 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12392 if (dwp_file->cus && dwp_file->tus
12393 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12394 {
12395 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12396 pretty bizarre. We use pulongest here because that's the established
4d65956b 12397 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12398 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12399 " TU version %s [in DWP file %s]"),
12400 pulongest (dwp_file->cus->version),
d721ba37 12401 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12402 }
08302ed2
DE
12403
12404 if (dwp_file->cus)
12405 dwp_file->version = dwp_file->cus->version;
12406 else if (dwp_file->tus)
12407 dwp_file->version = dwp_file->tus->version;
12408 else
12409 dwp_file->version = 2;
73869dc2
DE
12410
12411 if (dwp_file->version == 2)
400174b1
TT
12412 bfd_map_over_sections (dwp_file->dbfd.get (),
12413 dwarf2_locate_v2_dwp_sections,
12414 dwp_file.get ());
73869dc2 12415
298e9637
SM
12416 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12417 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12418
b4f54984 12419 if (dwarf_read_debug)
80626a55
DE
12420 {
12421 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12422 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12423 " %s CUs, %s TUs\n",
12424 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12425 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12426 }
12427
12428 return dwp_file;
3019eac3 12429}
c906108c 12430
ab5088bf
DE
12431/* Wrapper around open_and_init_dwp_file, only open it once. */
12432
12433static struct dwp_file *
ed2dc618 12434get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12435{
12436 if (! dwarf2_per_objfile->dwp_checked)
12437 {
ed2dc618
SM
12438 dwarf2_per_objfile->dwp_file
12439 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12440 dwarf2_per_objfile->dwp_checked = 1;
12441 }
400174b1 12442 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12443}
12444
80626a55
DE
12445/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12446 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12447 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12448 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12449 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12450
12451 This is called, for example, when wanting to read a variable with a
12452 complex location. Therefore we don't want to do file i/o for every call.
12453 Therefore we don't want to look for a DWO file on every call.
12454 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12455 then we check if we've already seen DWO_NAME, and only THEN do we check
12456 for a DWO file.
12457
1c658ad5 12458 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12459 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12460
3019eac3 12461static struct dwo_unit *
80626a55
DE
12462lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12463 const char *dwo_name, const char *comp_dir,
12464 ULONGEST signature, int is_debug_types)
3019eac3 12465{
ed2dc618 12466 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12467 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12468 const char *kind = is_debug_types ? "TU" : "CU";
12469 void **dwo_file_slot;
3019eac3 12470 struct dwo_file *dwo_file;
80626a55 12471 struct dwp_file *dwp_file;
cb1df416 12472
6a506a2d
DE
12473 /* First see if there's a DWP file.
12474 If we have a DWP file but didn't find the DWO inside it, don't
12475 look for the original DWO file. It makes gdb behave differently
12476 depending on whether one is debugging in the build tree. */
cf2c3c16 12477
ed2dc618 12478 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12479 if (dwp_file != NULL)
cf2c3c16 12480 {
80626a55
DE
12481 const struct dwp_hash_table *dwp_htab =
12482 is_debug_types ? dwp_file->tus : dwp_file->cus;
12483
12484 if (dwp_htab != NULL)
12485 {
12486 struct dwo_unit *dwo_cutu =
ed2dc618 12487 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12488 signature, is_debug_types);
80626a55
DE
12489
12490 if (dwo_cutu != NULL)
12491 {
b4f54984 12492 if (dwarf_read_debug)
80626a55
DE
12493 {
12494 fprintf_unfiltered (gdb_stdlog,
12495 "Virtual DWO %s %s found: @%s\n",
12496 kind, hex_string (signature),
12497 host_address_to_string (dwo_cutu));
12498 }
12499 return dwo_cutu;
12500 }
12501 }
12502 }
6a506a2d 12503 else
80626a55 12504 {
6a506a2d 12505 /* No DWP file, look for the DWO file. */
80626a55 12506
ed2dc618
SM
12507 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12508 dwo_name, comp_dir);
6a506a2d 12509 if (*dwo_file_slot == NULL)
80626a55 12510 {
6a506a2d
DE
12511 /* Read in the file and build a table of the CUs/TUs it contains. */
12512 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12513 }
6a506a2d 12514 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12515 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12516
6a506a2d 12517 if (dwo_file != NULL)
19c3d4c9 12518 {
6a506a2d
DE
12519 struct dwo_unit *dwo_cutu = NULL;
12520
12521 if (is_debug_types && dwo_file->tus)
12522 {
12523 struct dwo_unit find_dwo_cutu;
12524
12525 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12526 find_dwo_cutu.signature = signature;
9a3c8263 12527 dwo_cutu
b0b6a987
TT
12528 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12529 &find_dwo_cutu);
6a506a2d 12530 }
33c5cd75 12531 else if (!is_debug_types && dwo_file->cus)
80626a55 12532 {
33c5cd75
DB
12533 struct dwo_unit find_dwo_cutu;
12534
12535 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12536 find_dwo_cutu.signature = signature;
b0b6a987 12537 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12538 &find_dwo_cutu);
6a506a2d
DE
12539 }
12540
12541 if (dwo_cutu != NULL)
12542 {
b4f54984 12543 if (dwarf_read_debug)
6a506a2d
DE
12544 {
12545 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12546 kind, dwo_name, hex_string (signature),
12547 host_address_to_string (dwo_cutu));
12548 }
12549 return dwo_cutu;
80626a55
DE
12550 }
12551 }
2e276125 12552 }
9cdd5dbd 12553
80626a55
DE
12554 /* We didn't find it. This could mean a dwo_id mismatch, or
12555 someone deleted the DWO/DWP file, or the search path isn't set up
12556 correctly to find the file. */
12557
b4f54984 12558 if (dwarf_read_debug)
80626a55
DE
12559 {
12560 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12561 kind, dwo_name, hex_string (signature));
12562 }
3019eac3 12563
6656a72d
DE
12564 /* This is a warning and not a complaint because it can be caused by
12565 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12566 {
12567 /* Print the name of the DWP file if we looked there, helps the user
12568 better diagnose the problem. */
791afaa2 12569 std::string dwp_text;
43942612
DE
12570
12571 if (dwp_file != NULL)
791afaa2
TT
12572 dwp_text = string_printf (" [in DWP file %s]",
12573 lbasename (dwp_file->name));
43942612 12574
9d8780f0 12575 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12576 " [in module %s]"),
12577 kind, dwo_name, hex_string (signature),
791afaa2 12578 dwp_text.c_str (),
43942612 12579 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12580 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12581 }
3019eac3 12582 return NULL;
5fb290d7
DJ
12583}
12584
80626a55
DE
12585/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12586 See lookup_dwo_cutu_unit for details. */
12587
12588static struct dwo_unit *
12589lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12590 const char *dwo_name, const char *comp_dir,
12591 ULONGEST signature)
12592{
12593 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12594}
12595
12596/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12597 See lookup_dwo_cutu_unit for details. */
12598
12599static struct dwo_unit *
12600lookup_dwo_type_unit (struct signatured_type *this_tu,
12601 const char *dwo_name, const char *comp_dir)
12602{
12603 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12604}
12605
89e63ee4
DE
12606/* Traversal function for queue_and_load_all_dwo_tus. */
12607
12608static int
12609queue_and_load_dwo_tu (void **slot, void *info)
12610{
12611 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12612 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12613 ULONGEST signature = dwo_unit->signature;
12614 struct signatured_type *sig_type =
12615 lookup_dwo_signatured_type (per_cu->cu, signature);
12616
12617 if (sig_type != NULL)
12618 {
12619 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12620
12621 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12622 a real dependency of PER_CU on SIG_TYPE. That is detected later
12623 while processing PER_CU. */
12624 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12625 load_full_type_unit (sig_cu);
ae640021 12626 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12627 }
12628
12629 return 1;
12630}
12631
12632/* Queue all TUs contained in the DWO of PER_CU to be read in.
12633 The DWO may have the only definition of the type, though it may not be
12634 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12635 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12636
12637static void
12638queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12639{
12640 struct dwo_unit *dwo_unit;
12641 struct dwo_file *dwo_file;
12642
12643 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12644 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12645 gdb_assert (per_cu->cu != NULL);
12646
12647 dwo_unit = per_cu->cu->dwo_unit;
12648 gdb_assert (dwo_unit != NULL);
12649
12650 dwo_file = dwo_unit->dwo_file;
12651 if (dwo_file->tus != NULL)
b0b6a987
TT
12652 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12653 per_cu);
89e63ee4
DE
12654}
12655
3019eac3 12656/* Read in various DIEs. */
348e048f 12657
d389af10 12658/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12659 Inherit only the children of the DW_AT_abstract_origin DIE not being
12660 already referenced by DW_AT_abstract_origin from the children of the
12661 current DIE. */
d389af10
JK
12662
12663static void
12664inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12665{
12666 struct die_info *child_die;
791afaa2 12667 sect_offset *offsetp;
d389af10
JK
12668 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12669 struct die_info *origin_die;
12670 /* Iterator of the ORIGIN_DIE children. */
12671 struct die_info *origin_child_die;
d389af10 12672 struct attribute *attr;
cd02d79d
PA
12673 struct dwarf2_cu *origin_cu;
12674 struct pending **origin_previous_list_in_scope;
d389af10
JK
12675
12676 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12677 if (!attr)
12678 return;
12679
cd02d79d
PA
12680 /* Note that following die references may follow to a die in a
12681 different cu. */
12682
12683 origin_cu = cu;
12684 origin_die = follow_die_ref (die, attr, &origin_cu);
12685
12686 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12687 symbols in. */
12688 origin_previous_list_in_scope = origin_cu->list_in_scope;
12689 origin_cu->list_in_scope = cu->list_in_scope;
12690
edb3359d
DJ
12691 if (die->tag != origin_die->tag
12692 && !(die->tag == DW_TAG_inlined_subroutine
12693 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12694 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12695 sect_offset_str (die->sect_off),
12696 sect_offset_str (origin_die->sect_off));
d389af10 12697
791afaa2 12698 std::vector<sect_offset> offsets;
d389af10 12699
3ea89b92
PMR
12700 for (child_die = die->child;
12701 child_die && child_die->tag;
12702 child_die = sibling_die (child_die))
12703 {
12704 struct die_info *child_origin_die;
12705 struct dwarf2_cu *child_origin_cu;
12706
12707 /* We are trying to process concrete instance entries:
216f72a1 12708 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12709 it's not relevant to our analysis here. i.e. detecting DIEs that are
12710 present in the abstract instance but not referenced in the concrete
12711 one. */
216f72a1
JK
12712 if (child_die->tag == DW_TAG_call_site
12713 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12714 continue;
12715
c38f313d
DJ
12716 /* For each CHILD_DIE, find the corresponding child of
12717 ORIGIN_DIE. If there is more than one layer of
12718 DW_AT_abstract_origin, follow them all; there shouldn't be,
12719 but GCC versions at least through 4.4 generate this (GCC PR
12720 40573). */
3ea89b92
PMR
12721 child_origin_die = child_die;
12722 child_origin_cu = cu;
c38f313d
DJ
12723 while (1)
12724 {
cd02d79d
PA
12725 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12726 child_origin_cu);
c38f313d
DJ
12727 if (attr == NULL)
12728 break;
cd02d79d
PA
12729 child_origin_die = follow_die_ref (child_origin_die, attr,
12730 &child_origin_cu);
c38f313d
DJ
12731 }
12732
d389af10
JK
12733 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12734 counterpart may exist. */
c38f313d 12735 if (child_origin_die != child_die)
d389af10 12736 {
edb3359d
DJ
12737 if (child_die->tag != child_origin_die->tag
12738 && !(child_die->tag == DW_TAG_inlined_subroutine
12739 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12740 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12741 "different tags"),
9d8780f0
SM
12742 sect_offset_str (child_die->sect_off),
12743 sect_offset_str (child_origin_die->sect_off));
c38f313d 12744 if (child_origin_die->parent != origin_die)
b98664d3 12745 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12746 "different parents"),
9d8780f0
SM
12747 sect_offset_str (child_die->sect_off),
12748 sect_offset_str (child_origin_die->sect_off));
c38f313d 12749 else
791afaa2 12750 offsets.push_back (child_origin_die->sect_off);
d389af10 12751 }
d389af10 12752 }
791afaa2
TT
12753 std::sort (offsets.begin (), offsets.end ());
12754 sect_offset *offsets_end = offsets.data () + offsets.size ();
12755 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12756 if (offsetp[-1] == *offsetp)
b98664d3 12757 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12758 "to DIE %s as their abstract origin"),
12759 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12760
791afaa2 12761 offsetp = offsets.data ();
d389af10
JK
12762 origin_child_die = origin_die->child;
12763 while (origin_child_die && origin_child_die->tag)
12764 {
12765 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12766 while (offsetp < offsets_end
9c541725 12767 && *offsetp < origin_child_die->sect_off)
d389af10 12768 offsetp++;
b64f50a1 12769 if (offsetp >= offsets_end
9c541725 12770 || *offsetp > origin_child_die->sect_off)
d389af10 12771 {
adde2bff
DE
12772 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12773 Check whether we're already processing ORIGIN_CHILD_DIE.
12774 This can happen with mutually referenced abstract_origins.
12775 PR 16581. */
12776 if (!origin_child_die->in_process)
12777 process_die (origin_child_die, origin_cu);
d389af10
JK
12778 }
12779 origin_child_die = sibling_die (origin_child_die);
12780 }
cd02d79d 12781 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12782
12783 if (cu != origin_cu)
12784 compute_delayed_physnames (origin_cu);
d389af10
JK
12785}
12786
c906108c 12787static void
e7c27a73 12788read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12789{
518817b3 12790 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12791 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12792 struct context_stack *newobj;
c906108c
SS
12793 CORE_ADDR lowpc;
12794 CORE_ADDR highpc;
12795 struct die_info *child_die;
edb3359d 12796 struct attribute *attr, *call_line, *call_file;
15d034d0 12797 const char *name;
e142c38c 12798 CORE_ADDR baseaddr;
801e3a5b 12799 struct block *block;
edb3359d 12800 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12801 std::vector<struct symbol *> template_args;
34eaf542 12802 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12803
12804 if (inlined_func)
12805 {
12806 /* If we do not have call site information, we can't show the
12807 caller of this inlined function. That's too confusing, so
12808 only use the scope for local variables. */
12809 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12810 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12811 if (call_line == NULL || call_file == NULL)
12812 {
12813 read_lexical_block_scope (die, cu);
12814 return;
12815 }
12816 }
c906108c 12817
b3b3bada 12818 baseaddr = objfile->text_section_offset ();
e142c38c 12819
94af9270 12820 name = dwarf2_name (die, cu);
c906108c 12821
e8d05480
JB
12822 /* Ignore functions with missing or empty names. These are actually
12823 illegal according to the DWARF standard. */
12824 if (name == NULL)
12825 {
b98664d3 12826 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12827 sect_offset_str (die->sect_off));
e8d05480
JB
12828 return;
12829 }
12830
12831 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12832 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12833 <= PC_BOUNDS_INVALID)
e8d05480 12834 {
ae4d0c03
PM
12835 attr = dwarf2_attr (die, DW_AT_external, cu);
12836 if (!attr || !DW_UNSND (attr))
b98664d3 12837 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12838 "for subprogram DIE at %s"),
12839 sect_offset_str (die->sect_off));
e8d05480
JB
12840 return;
12841 }
c906108c 12842
3e29f34a
MR
12843 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12844 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12845
34eaf542
TT
12846 /* If we have any template arguments, then we must allocate a
12847 different sort of symbol. */
12848 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12849 {
12850 if (child_die->tag == DW_TAG_template_type_param
12851 || child_die->tag == DW_TAG_template_value_param)
12852 {
e623cf5d 12853 templ_func = allocate_template_symbol (objfile);
cf724bc9 12854 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12855 break;
12856 }
12857 }
12858
c24bdb02 12859 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12860 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12861 (struct symbol *) templ_func);
4c2df51b 12862
81873cc8 12863 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12864 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12865 cu->language);
12866
4cecd739
DJ
12867 /* If there is a location expression for DW_AT_frame_base, record
12868 it. */
e142c38c 12869 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12870 if (attr != nullptr)
fe978cb0 12871 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12872
63e43d3a
PMR
12873 /* If there is a location for the static link, record it. */
12874 newobj->static_link = NULL;
12875 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12876 if (attr != nullptr)
63e43d3a 12877 {
224c3ddb
SM
12878 newobj->static_link
12879 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12880 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12881 cu->per_cu->addr_type ());
63e43d3a
PMR
12882 }
12883
c24bdb02 12884 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12885
639d11d3 12886 if (die->child != NULL)
c906108c 12887 {
639d11d3 12888 child_die = die->child;
c906108c
SS
12889 while (child_die && child_die->tag)
12890 {
34eaf542
TT
12891 if (child_die->tag == DW_TAG_template_type_param
12892 || child_die->tag == DW_TAG_template_value_param)
12893 {
12894 struct symbol *arg = new_symbol (child_die, NULL, cu);
12895
f1078f66 12896 if (arg != NULL)
2f4732b0 12897 template_args.push_back (arg);
34eaf542
TT
12898 }
12899 else
12900 process_die (child_die, cu);
c906108c
SS
12901 child_die = sibling_die (child_die);
12902 }
12903 }
12904
d389af10
JK
12905 inherit_abstract_dies (die, cu);
12906
4a811a97
UW
12907 /* If we have a DW_AT_specification, we might need to import using
12908 directives from the context of the specification DIE. See the
12909 comment in determine_prefix. */
12910 if (cu->language == language_cplus
12911 && dwarf2_attr (die, DW_AT_specification, cu))
12912 {
12913 struct dwarf2_cu *spec_cu = cu;
12914 struct die_info *spec_die = die_specification (die, &spec_cu);
12915
12916 while (spec_die)
12917 {
12918 child_die = spec_die->child;
12919 while (child_die && child_die->tag)
12920 {
12921 if (child_die->tag == DW_TAG_imported_module)
12922 process_die (child_die, spec_cu);
12923 child_die = sibling_die (child_die);
12924 }
12925
12926 /* In some cases, GCC generates specification DIEs that
12927 themselves contain DW_AT_specification attributes. */
12928 spec_die = die_specification (spec_die, &spec_cu);
12929 }
12930 }
12931
c24bdb02 12932 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12933 /* Make a block for the local symbols within. */
c24bdb02 12934 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12935 cstk.static_link, lowpc, highpc);
801e3a5b 12936
df8a16a1 12937 /* For C++, set the block's scope. */
45280282
IB
12938 if ((cu->language == language_cplus
12939 || cu->language == language_fortran
c44af4eb
TT
12940 || cu->language == language_d
12941 || cu->language == language_rust)
4d4ec4e5 12942 && cu->processing_has_namespace_info)
195a3f6c
TT
12943 block_set_scope (block, determine_prefix (die, cu),
12944 &objfile->objfile_obstack);
df8a16a1 12945
801e3a5b
JB
12946 /* If we have address ranges, record them. */
12947 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12948
a60f3166 12949 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12950
34eaf542 12951 /* Attach template arguments to function. */
2f4732b0 12952 if (!template_args.empty ())
34eaf542
TT
12953 {
12954 gdb_assert (templ_func != NULL);
12955
2f4732b0 12956 templ_func->n_template_arguments = template_args.size ();
34eaf542 12957 templ_func->template_arguments
8d749320
SM
12958 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12959 templ_func->n_template_arguments);
34eaf542 12960 memcpy (templ_func->template_arguments,
2f4732b0 12961 template_args.data (),
34eaf542 12962 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
12963
12964 /* Make sure that the symtab is set on the new symbols. Even
12965 though they don't appear in this symtab directly, other parts
12966 of gdb assume that symbols do, and this is reasonably
12967 true. */
8634679f 12968 for (symbol *sym : template_args)
3e1d3d8c 12969 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
12970 }
12971
208d8187
JB
12972 /* In C++, we can have functions nested inside functions (e.g., when
12973 a function declares a class that has methods). This means that
12974 when we finish processing a function scope, we may need to go
12975 back to building a containing block's symbol lists. */
c24bdb02
KS
12976 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12977 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 12978
921e78cf
JB
12979 /* If we've finished processing a top-level function, subsequent
12980 symbols go in the file symbol list. */
c24bdb02
KS
12981 if (cu->get_builder ()->outermost_context_p ())
12982 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
12983}
12984
12985/* Process all the DIES contained within a lexical block scope. Start
12986 a new scope, process the dies, and then close the scope. */
12987
12988static void
e7c27a73 12989read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12990{
518817b3 12991 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12992 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12993 CORE_ADDR lowpc, highpc;
12994 struct die_info *child_die;
e142c38c
DJ
12995 CORE_ADDR baseaddr;
12996
b3b3bada 12997 baseaddr = objfile->text_section_offset ();
c906108c
SS
12998
12999 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13000 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13001 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13002 be nasty. Might be easier to properly extend generic blocks to
af34e669 13003 describe ranges. */
e385593e
JK
13004 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13005 {
13006 case PC_BOUNDS_NOT_PRESENT:
13007 /* DW_TAG_lexical_block has no attributes, process its children as if
13008 there was no wrapping by that DW_TAG_lexical_block.
13009 GCC does no longer produces such DWARF since GCC r224161. */
13010 for (child_die = die->child;
13011 child_die != NULL && child_die->tag;
13012 child_die = sibling_die (child_die))
13013 process_die (child_die, cu);
13014 return;
13015 case PC_BOUNDS_INVALID:
13016 return;
13017 }
3e29f34a
MR
13018 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13019 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13020
c24bdb02 13021 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13022 if (die->child != NULL)
c906108c 13023 {
639d11d3 13024 child_die = die->child;
c906108c
SS
13025 while (child_die && child_die->tag)
13026 {
e7c27a73 13027 process_die (child_die, cu);
c906108c
SS
13028 child_die = sibling_die (child_die);
13029 }
13030 }
3ea89b92 13031 inherit_abstract_dies (die, cu);
c24bdb02 13032 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13033
c24bdb02
KS
13034 if (*cu->get_builder ()->get_local_symbols () != NULL
13035 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13036 {
801e3a5b 13037 struct block *block
c24bdb02 13038 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13039 cstk.start_addr, highpc);
801e3a5b
JB
13040
13041 /* Note that recording ranges after traversing children, as we
13042 do here, means that recording a parent's ranges entails
13043 walking across all its children's ranges as they appear in
13044 the address map, which is quadratic behavior.
13045
13046 It would be nicer to record the parent's ranges before
13047 traversing its children, simply overriding whatever you find
13048 there. But since we don't even decide whether to create a
13049 block until after we've traversed its children, that's hard
13050 to do. */
13051 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13052 }
c24bdb02
KS
13053 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13054 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13055}
13056
216f72a1 13057/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13058
13059static void
13060read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13061{
518817b3 13062 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13063 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13064 CORE_ADDR pc, baseaddr;
13065 struct attribute *attr;
13066 struct call_site *call_site, call_site_local;
13067 void **slot;
13068 int nparams;
13069 struct die_info *child_die;
13070
b3b3bada 13071 baseaddr = objfile->text_section_offset ();
96408a79 13072
216f72a1
JK
13073 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13074 if (attr == NULL)
13075 {
13076 /* This was a pre-DWARF-5 GNU extension alias
13077 for DW_AT_call_return_pc. */
13078 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13079 }
96408a79
SA
13080 if (!attr)
13081 {
b98664d3 13082 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13083 "DIE %s [in module %s]"),
13084 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13085 return;
13086 }
cd6c91b4 13087 pc = attr->value_as_address () + baseaddr;
3e29f34a 13088 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13089
13090 if (cu->call_site_htab == NULL)
13091 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13092 NULL, &objfile->objfile_obstack,
13093 hashtab_obstack_allocate, NULL);
13094 call_site_local.pc = pc;
13095 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13096 if (*slot != NULL)
13097 {
b98664d3 13098 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13099 "DIE %s [in module %s]"),
13100 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13101 objfile_name (objfile));
96408a79
SA
13102 return;
13103 }
13104
13105 /* Count parameters at the caller. */
13106
13107 nparams = 0;
13108 for (child_die = die->child; child_die && child_die->tag;
13109 child_die = sibling_die (child_die))
13110 {
216f72a1
JK
13111 if (child_die->tag != DW_TAG_call_site_parameter
13112 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13113 {
b98664d3 13114 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13115 "DW_TAG_call_site child DIE %s [in module %s]"),
13116 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13117 objfile_name (objfile));
96408a79
SA
13118 continue;
13119 }
13120
13121 nparams++;
13122 }
13123
224c3ddb
SM
13124 call_site
13125 = ((struct call_site *)
13126 obstack_alloc (&objfile->objfile_obstack,
13127 sizeof (*call_site)
13128 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13129 *slot = call_site;
13130 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13131 call_site->pc = pc;
13132
216f72a1
JK
13133 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13134 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13135 {
13136 struct die_info *func_die;
13137
13138 /* Skip also over DW_TAG_inlined_subroutine. */
13139 for (func_die = die->parent;
13140 func_die && func_die->tag != DW_TAG_subprogram
13141 && func_die->tag != DW_TAG_subroutine_type;
13142 func_die = func_die->parent);
13143
216f72a1
JK
13144 /* DW_AT_call_all_calls is a superset
13145 of DW_AT_call_all_tail_calls. */
96408a79 13146 if (func_die
216f72a1 13147 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13148 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13149 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13150 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13151 {
13152 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13153 not complete. But keep CALL_SITE for look ups via call_site_htab,
13154 both the initial caller containing the real return address PC and
13155 the final callee containing the current PC of a chain of tail
13156 calls do not need to have the tail call list complete. But any
13157 function candidate for a virtual tail call frame searched via
13158 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13159 determined unambiguously. */
13160 }
13161 else
13162 {
13163 struct type *func_type = NULL;
13164
13165 if (func_die)
13166 func_type = get_die_type (func_die, cu);
13167 if (func_type != NULL)
13168 {
13169 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13170
13171 /* Enlist this call site to the function. */
13172 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13173 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13174 }
13175 else
b98664d3 13176 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13177 "DIE %s [in module %s]"),
13178 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13179 }
13180 }
13181
216f72a1
JK
13182 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13183 if (attr == NULL)
13184 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13185 if (attr == NULL)
13186 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13187 if (attr == NULL)
216f72a1
JK
13188 {
13189 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13190 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13191 }
96408a79 13192 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13193 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13194 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13195 else if (attr->form_is_block ())
96408a79
SA
13196 {
13197 struct dwarf2_locexpr_baton *dlbaton;
13198
8d749320 13199 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13200 dlbaton->data = DW_BLOCK (attr)->data;
13201 dlbaton->size = DW_BLOCK (attr)->size;
13202 dlbaton->per_cu = cu->per_cu;
13203
13204 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13205 }
cd6c91b4 13206 else if (attr->form_is_ref ())
96408a79 13207 {
96408a79
SA
13208 struct dwarf2_cu *target_cu = cu;
13209 struct die_info *target_die;
13210
ac9ec31b 13211 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13212 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13213 if (die_is_declaration (target_die, target_cu))
13214 {
7d45c7c3 13215 const char *target_physname;
9112db09
JK
13216
13217 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13218 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13219 if (target_physname == NULL)
9112db09 13220 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13221 if (target_physname == NULL)
b98664d3 13222 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13223 "physname, for referencing DIE %s [in module %s]"),
13224 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13225 else
7d455152 13226 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13227 }
13228 else
13229 {
13230 CORE_ADDR lowpc;
13231
13232 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13233 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13234 <= PC_BOUNDS_INVALID)
b98664d3 13235 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13236 "low pc, for referencing DIE %s [in module %s]"),
13237 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13238 else
3e29f34a
MR
13239 {
13240 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13241 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13242 }
96408a79
SA
13243 }
13244 }
13245 else
b98664d3 13246 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13247 "block nor reference, for DIE %s [in module %s]"),
13248 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13249
13250 call_site->per_cu = cu->per_cu;
13251
13252 for (child_die = die->child;
13253 child_die && child_die->tag;
13254 child_die = sibling_die (child_die))
13255 {
96408a79 13256 struct call_site_parameter *parameter;
1788b2d3 13257 struct attribute *loc, *origin;
96408a79 13258
216f72a1
JK
13259 if (child_die->tag != DW_TAG_call_site_parameter
13260 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13261 {
13262 /* Already printed the complaint above. */
13263 continue;
13264 }
13265
13266 gdb_assert (call_site->parameter_count < nparams);
13267 parameter = &call_site->parameter[call_site->parameter_count];
13268
1788b2d3
JK
13269 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13270 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13271 register is contained in DW_AT_call_value. */
96408a79 13272
24c5c679 13273 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13274 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13275 if (origin == NULL)
13276 {
13277 /* This was a pre-DWARF-5 GNU extension alias
13278 for DW_AT_call_parameter. */
13279 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13280 }
cd6c91b4 13281 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13282 {
1788b2d3 13283 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13284
13285 sect_offset sect_off
13286 = (sect_offset) dwarf2_get_ref_die_offset (origin);
4057dfde 13287 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13288 {
13289 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13290 binding can be done only inside one CU. Such referenced DIE
13291 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13292 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13293 "DW_TAG_call_site child DIE %s [in module %s]"),
13294 sect_offset_str (child_die->sect_off),
9c541725 13295 objfile_name (objfile));
d76b7dbc
JK
13296 continue;
13297 }
9c541725
PA
13298 parameter->u.param_cu_off
13299 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13300 }
4fc6c0d5 13301 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13302 {
b98664d3 13303 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13304 "DW_TAG_call_site child DIE %s [in module %s]"),
13305 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13306 continue;
13307 }
24c5c679 13308 else
96408a79 13309 {
24c5c679
JK
13310 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13311 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13312 if (parameter->u.dwarf_reg != -1)
13313 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13314 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13315 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13316 &parameter->u.fb_offset))
13317 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13318 else
13319 {
b98664d3 13320 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13321 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13322 "DW_TAG_call_site child DIE %s "
24c5c679 13323 "[in module %s]"),
9d8780f0 13324 sect_offset_str (child_die->sect_off),
9c541725 13325 objfile_name (objfile));
24c5c679
JK
13326 continue;
13327 }
96408a79
SA
13328 }
13329
216f72a1
JK
13330 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13331 if (attr == NULL)
13332 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13333 if (attr == NULL || !attr->form_is_block ())
96408a79 13334 {
b98664d3 13335 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13336 "DW_TAG_call_site child DIE %s [in module %s]"),
13337 sect_offset_str (child_die->sect_off),
9c541725 13338 objfile_name (objfile));
96408a79
SA
13339 continue;
13340 }
13341 parameter->value = DW_BLOCK (attr)->data;
13342 parameter->value_size = DW_BLOCK (attr)->size;
13343
13344 /* Parameters are not pre-cleared by memset above. */
13345 parameter->data_value = NULL;
13346 parameter->data_value_size = 0;
13347 call_site->parameter_count++;
13348
216f72a1
JK
13349 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13350 if (attr == NULL)
13351 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13352 if (attr != nullptr)
96408a79 13353 {
4fc6c0d5 13354 if (!attr->form_is_block ())
b98664d3 13355 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13356 "DW_TAG_call_site child DIE %s [in module %s]"),
13357 sect_offset_str (child_die->sect_off),
9c541725 13358 objfile_name (objfile));
96408a79
SA
13359 else
13360 {
13361 parameter->data_value = DW_BLOCK (attr)->data;
13362 parameter->data_value_size = DW_BLOCK (attr)->size;
13363 }
13364 }
13365 }
13366}
13367
71a3c369
TT
13368/* Helper function for read_variable. If DIE represents a virtual
13369 table, then return the type of the concrete object that is
13370 associated with the virtual table. Otherwise, return NULL. */
13371
13372static struct type *
13373rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13374{
13375 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13376 if (attr == NULL)
13377 return NULL;
13378
13379 /* Find the type DIE. */
13380 struct die_info *type_die = NULL;
13381 struct dwarf2_cu *type_cu = cu;
13382
cd6c91b4 13383 if (attr->form_is_ref ())
71a3c369
TT
13384 type_die = follow_die_ref (die, attr, &type_cu);
13385 if (type_die == NULL)
13386 return NULL;
13387
13388 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13389 return NULL;
13390 return die_containing_type (type_die, type_cu);
13391}
13392
13393/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13394
13395static void
13396read_variable (struct die_info *die, struct dwarf2_cu *cu)
13397{
13398 struct rust_vtable_symbol *storage = NULL;
13399
13400 if (cu->language == language_rust)
13401 {
13402 struct type *containing_type = rust_containing_type (die, cu);
13403
13404 if (containing_type != NULL)
13405 {
518817b3 13406 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13407
468c0cbb 13408 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13409 initialize_objfile_symbol (storage);
13410 storage->concrete_type = containing_type;
cf724bc9 13411 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13412 }
13413 }
13414
e4a62c65
TV
13415 struct symbol *res = new_symbol (die, NULL, cu, storage);
13416 struct attribute *abstract_origin
13417 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13418 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13419 if (res == NULL && loc && abstract_origin)
13420 {
13421 /* We have a variable without a name, but with a location and an abstract
13422 origin. This may be a concrete instance of an abstract variable
13423 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13424 later. */
13425 struct dwarf2_cu *origin_cu = cu;
13426 struct die_info *origin_die
13427 = follow_die_ref (die, abstract_origin, &origin_cu);
13428 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13429 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13430 }
71a3c369
TT
13431}
13432
43988095
JK
13433/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13434 reading .debug_rnglists.
13435 Callback's type should be:
13436 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13437 Return true if the attributes are present and valid, otherwise,
13438 return false. */
13439
13440template <typename Callback>
13441static bool
13442dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13443 Callback &&callback)
13444{
ed2dc618 13445 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13446 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13447 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13448 bfd *obfd = objfile->obfd;
43988095
JK
13449 /* Base address selection entry. */
13450 CORE_ADDR base;
13451 int found_base;
43988095 13452 const gdb_byte *buffer;
43988095
JK
13453 CORE_ADDR baseaddr;
13454 bool overflow = false;
13455
13456 found_base = cu->base_known;
13457 base = cu->base_address;
13458
96b79293 13459 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13460 if (offset >= dwarf2_per_objfile->rnglists.size)
13461 {
b98664d3 13462 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13463 offset);
13464 return false;
13465 }
13466 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13467
b3b3bada 13468 baseaddr = objfile->text_section_offset ();
43988095
JK
13469
13470 while (1)
13471 {
7814882a
JK
13472 /* Initialize it due to a false compiler warning. */
13473 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13474 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13475 + dwarf2_per_objfile->rnglists.size);
13476 unsigned int bytes_read;
13477
13478 if (buffer == buf_end)
13479 {
13480 overflow = true;
13481 break;
13482 }
13483 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13484 switch (rlet)
13485 {
13486 case DW_RLE_end_of_list:
13487 break;
13488 case DW_RLE_base_address:
13489 if (buffer + cu->header.addr_size > buf_end)
13490 {
13491 overflow = true;
13492 break;
13493 }
c8a7a66f 13494 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13495 found_base = 1;
13496 buffer += bytes_read;
13497 break;
13498 case DW_RLE_start_length:
13499 if (buffer + cu->header.addr_size > buf_end)
13500 {
13501 overflow = true;
13502 break;
13503 }
c8a7a66f
TT
13504 range_beginning = cu->header.read_address (obfd, buffer,
13505 &bytes_read);
43988095
JK
13506 buffer += bytes_read;
13507 range_end = (range_beginning
13508 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13509 buffer += bytes_read;
13510 if (buffer > buf_end)
13511 {
13512 overflow = true;
13513 break;
13514 }
13515 break;
13516 case DW_RLE_offset_pair:
13517 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13518 buffer += bytes_read;
13519 if (buffer > buf_end)
13520 {
13521 overflow = true;
13522 break;
13523 }
13524 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13525 buffer += bytes_read;
13526 if (buffer > buf_end)
13527 {
13528 overflow = true;
13529 break;
13530 }
13531 break;
13532 case DW_RLE_start_end:
13533 if (buffer + 2 * cu->header.addr_size > buf_end)
13534 {
13535 overflow = true;
13536 break;
13537 }
c8a7a66f
TT
13538 range_beginning = cu->header.read_address (obfd, buffer,
13539 &bytes_read);
43988095 13540 buffer += bytes_read;
c8a7a66f 13541 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13542 buffer += bytes_read;
13543 break;
13544 default:
b98664d3 13545 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13546 return false;
13547 }
13548 if (rlet == DW_RLE_end_of_list || overflow)
13549 break;
13550 if (rlet == DW_RLE_base_address)
13551 continue;
13552
13553 if (!found_base)
13554 {
13555 /* We have no valid base address for the ranges
13556 data. */
b98664d3 13557 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13558 return false;
13559 }
13560
13561 if (range_beginning > range_end)
13562 {
13563 /* Inverted range entries are invalid. */
b98664d3 13564 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13565 return false;
13566 }
13567
13568 /* Empty range entries have no effect. */
13569 if (range_beginning == range_end)
13570 continue;
13571
13572 range_beginning += base;
13573 range_end += base;
13574
13575 /* A not-uncommon case of bad debug info.
13576 Don't pollute the addrmap with bad data. */
13577 if (range_beginning + baseaddr == 0
13578 && !dwarf2_per_objfile->has_section_at_zero)
13579 {
b98664d3 13580 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13581 " [in module %s]"), objfile_name (objfile));
13582 continue;
13583 }
13584
13585 callback (range_beginning, range_end);
13586 }
13587
13588 if (overflow)
13589 {
b98664d3 13590 complaint (_("Offset %d is not terminated "
43988095
JK
13591 "for DW_AT_ranges attribute"),
13592 offset);
13593 return false;
13594 }
13595
13596 return true;
13597}
13598
13599/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13600 Callback's type should be:
13601 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13602 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13603
43988095 13604template <typename Callback>
43039443 13605static int
5f46c5a5 13606dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13607 Callback &&callback)
43039443 13608{
ed2dc618 13609 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13610 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13611 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13612 struct comp_unit_head *cu_header = &cu->header;
13613 bfd *obfd = objfile->obfd;
13614 unsigned int addr_size = cu_header->addr_size;
13615 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13616 /* Base address selection entry. */
13617 CORE_ADDR base;
13618 int found_base;
13619 unsigned int dummy;
d521ce57 13620 const gdb_byte *buffer;
ff013f42 13621 CORE_ADDR baseaddr;
43039443 13622
43988095
JK
13623 if (cu_header->version >= 5)
13624 return dwarf2_rnglists_process (offset, cu, callback);
13625
d00adf39
DE
13626 found_base = cu->base_known;
13627 base = cu->base_address;
43039443 13628
96b79293 13629 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13630 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13631 {
b98664d3 13632 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13633 offset);
13634 return 0;
13635 }
dce234bc 13636 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13637
b3b3bada 13638 baseaddr = objfile->text_section_offset ();
ff013f42 13639
43039443
JK
13640 while (1)
13641 {
13642 CORE_ADDR range_beginning, range_end;
13643
c8a7a66f 13644 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13645 buffer += addr_size;
c8a7a66f 13646 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13647 buffer += addr_size;
13648 offset += 2 * addr_size;
13649
13650 /* An end of list marker is a pair of zero addresses. */
13651 if (range_beginning == 0 && range_end == 0)
13652 /* Found the end of list entry. */
13653 break;
13654
13655 /* Each base address selection entry is a pair of 2 values.
13656 The first is the largest possible address, the second is
13657 the base address. Check for a base address here. */
13658 if ((range_beginning & mask) == mask)
13659 {
28d2bfb9
AB
13660 /* If we found the largest possible address, then we already
13661 have the base address in range_end. */
13662 base = range_end;
43039443
JK
13663 found_base = 1;
13664 continue;
13665 }
13666
13667 if (!found_base)
13668 {
13669 /* We have no valid base address for the ranges
13670 data. */
b98664d3 13671 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13672 return 0;
13673 }
13674
9277c30c
UW
13675 if (range_beginning > range_end)
13676 {
13677 /* Inverted range entries are invalid. */
b98664d3 13678 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13679 return 0;
13680 }
13681
13682 /* Empty range entries have no effect. */
13683 if (range_beginning == range_end)
13684 continue;
13685
43039443
JK
13686 range_beginning += base;
13687 range_end += base;
13688
01093045
DE
13689 /* A not-uncommon case of bad debug info.
13690 Don't pollute the addrmap with bad data. */
13691 if (range_beginning + baseaddr == 0
13692 && !dwarf2_per_objfile->has_section_at_zero)
13693 {
b98664d3 13694 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13695 " [in module %s]"), objfile_name (objfile));
01093045
DE
13696 continue;
13697 }
13698
5f46c5a5
JK
13699 callback (range_beginning, range_end);
13700 }
13701
13702 return 1;
13703}
13704
13705/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13706 Return 1 if the attributes are present and valid, otherwise, return 0.
13707 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13708
13709static int
13710dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13711 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13712 dwarf2_psymtab *ranges_pst)
5f46c5a5 13713{
518817b3 13714 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13715 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13716 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13717 int low_set = 0;
13718 CORE_ADDR low = 0;
13719 CORE_ADDR high = 0;
13720 int retval;
13721
13722 retval = dwarf2_ranges_process (offset, cu,
13723 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13724 {
9277c30c 13725 if (ranges_pst != NULL)
3e29f34a
MR
13726 {
13727 CORE_ADDR lowpc;
13728 CORE_ADDR highpc;
13729
79748972
TT
13730 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13731 range_beginning + baseaddr)
13732 - baseaddr);
13733 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13734 range_end + baseaddr)
13735 - baseaddr);
d320c2b5
TT
13736 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13737 lowpc, highpc - 1, ranges_pst);
3e29f34a 13738 }
ff013f42 13739
43039443
JK
13740 /* FIXME: This is recording everything as a low-high
13741 segment of consecutive addresses. We should have a
13742 data structure for discontiguous block ranges
13743 instead. */
13744 if (! low_set)
13745 {
13746 low = range_beginning;
13747 high = range_end;
13748 low_set = 1;
13749 }
13750 else
13751 {
13752 if (range_beginning < low)
13753 low = range_beginning;
13754 if (range_end > high)
13755 high = range_end;
13756 }
5f46c5a5
JK
13757 });
13758 if (!retval)
13759 return 0;
43039443
JK
13760
13761 if (! low_set)
13762 /* If the first entry is an end-of-list marker, the range
13763 describes an empty scope, i.e. no instructions. */
13764 return 0;
13765
13766 if (low_return)
13767 *low_return = low;
13768 if (high_return)
13769 *high_return = high;
13770 return 1;
13771}
13772
3a2b436a
JK
13773/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13774 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13775 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13776
3a2b436a 13777static enum pc_bounds_kind
af34e669 13778dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13779 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13780 dwarf2_psymtab *pst)
c906108c 13781{
518817b3
SM
13782 struct dwarf2_per_objfile *dwarf2_per_objfile
13783 = cu->per_cu->dwarf2_per_objfile;
c906108c 13784 struct attribute *attr;
91da1414 13785 struct attribute *attr_high;
af34e669
DJ
13786 CORE_ADDR low = 0;
13787 CORE_ADDR high = 0;
e385593e 13788 enum pc_bounds_kind ret;
c906108c 13789
91da1414
MW
13790 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13791 if (attr_high)
af34e669 13792 {
e142c38c 13793 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13794 if (attr != nullptr)
91da1414 13795 {
cd6c91b4
TT
13796 low = attr->value_as_address ();
13797 high = attr_high->value_as_address ();
13798 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13799 high += low;
91da1414 13800 }
af34e669
DJ
13801 else
13802 /* Found high w/o low attribute. */
e385593e 13803 return PC_BOUNDS_INVALID;
af34e669
DJ
13804
13805 /* Found consecutive range of addresses. */
3a2b436a 13806 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13807 }
c906108c 13808 else
af34e669 13809 {
e142c38c 13810 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13811 if (attr != NULL)
13812 {
18a8505e 13813 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13814 We take advantage of the fact that DW_AT_ranges does not appear
13815 in DW_TAG_compile_unit of DWO files. */
13816 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13817 unsigned int ranges_offset = (DW_UNSND (attr)
13818 + (need_ranges_base
13819 ? cu->ranges_base
13820 : 0));
2e3cf129 13821
af34e669 13822 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13823 .debug_ranges section. */
2e3cf129 13824 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13825 return PC_BOUNDS_INVALID;
43039443 13826 /* Found discontinuous range of addresses. */
3a2b436a 13827 ret = PC_BOUNDS_RANGES;
af34e669 13828 }
e385593e
JK
13829 else
13830 return PC_BOUNDS_NOT_PRESENT;
af34e669 13831 }
c906108c 13832
48fbe735 13833 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13834 if (high <= low)
e385593e 13835 return PC_BOUNDS_INVALID;
c906108c
SS
13836
13837 /* When using the GNU linker, .gnu.linkonce. sections are used to
13838 eliminate duplicate copies of functions and vtables and such.
13839 The linker will arbitrarily choose one and discard the others.
13840 The AT_*_pc values for such functions refer to local labels in
13841 these sections. If the section from that file was discarded, the
13842 labels are not in the output, so the relocs get a value of 0.
13843 If this is a discarded function, mark the pc bounds as invalid,
13844 so that GDB will ignore it. */
72dca2f5 13845 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13846 return PC_BOUNDS_INVALID;
c906108c
SS
13847
13848 *lowpc = low;
96408a79
SA
13849 if (highpc)
13850 *highpc = high;
af34e669 13851 return ret;
c906108c
SS
13852}
13853
b084d499
JB
13854/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13855 its low and high PC addresses. Do nothing if these addresses could not
13856 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13857 and HIGHPC to the high address if greater than HIGHPC. */
13858
13859static void
13860dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13861 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13862 struct dwarf2_cu *cu)
13863{
13864 CORE_ADDR low, high;
13865 struct die_info *child = die->child;
13866
e385593e 13867 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13868 {
325fac50
PA
13869 *lowpc = std::min (*lowpc, low);
13870 *highpc = std::max (*highpc, high);
b084d499
JB
13871 }
13872
13873 /* If the language does not allow nested subprograms (either inside
13874 subprograms or lexical blocks), we're done. */
13875 if (cu->language != language_ada)
13876 return;
6e70227d 13877
b084d499
JB
13878 /* Check all the children of the given DIE. If it contains nested
13879 subprograms, then check their pc bounds. Likewise, we need to
13880 check lexical blocks as well, as they may also contain subprogram
13881 definitions. */
13882 while (child && child->tag)
13883 {
13884 if (child->tag == DW_TAG_subprogram
13885 || child->tag == DW_TAG_lexical_block)
13886 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13887 child = sibling_die (child);
13888 }
13889}
13890
fae299cd
DC
13891/* Get the low and high pc's represented by the scope DIE, and store
13892 them in *LOWPC and *HIGHPC. If the correct values can't be
13893 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13894
13895static void
13896get_scope_pc_bounds (struct die_info *die,
13897 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13898 struct dwarf2_cu *cu)
13899{
13900 CORE_ADDR best_low = (CORE_ADDR) -1;
13901 CORE_ADDR best_high = (CORE_ADDR) 0;
13902 CORE_ADDR current_low, current_high;
13903
3a2b436a 13904 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13905 >= PC_BOUNDS_RANGES)
fae299cd
DC
13906 {
13907 best_low = current_low;
13908 best_high = current_high;
13909 }
13910 else
13911 {
13912 struct die_info *child = die->child;
13913
13914 while (child && child->tag)
13915 {
13916 switch (child->tag) {
13917 case DW_TAG_subprogram:
b084d499 13918 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13919 break;
13920 case DW_TAG_namespace:
f55ee35c 13921 case DW_TAG_module:
fae299cd
DC
13922 /* FIXME: carlton/2004-01-16: Should we do this for
13923 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13924 that current GCC's always emit the DIEs corresponding
13925 to definitions of methods of classes as children of a
13926 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13927 the DIEs giving the declarations, which could be
13928 anywhere). But I don't see any reason why the
13929 standards says that they have to be there. */
13930 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13931
13932 if (current_low != ((CORE_ADDR) -1))
13933 {
325fac50
PA
13934 best_low = std::min (best_low, current_low);
13935 best_high = std::max (best_high, current_high);
fae299cd
DC
13936 }
13937 break;
13938 default:
0963b4bd 13939 /* Ignore. */
fae299cd
DC
13940 break;
13941 }
13942
13943 child = sibling_die (child);
13944 }
13945 }
13946
13947 *lowpc = best_low;
13948 *highpc = best_high;
13949}
13950
801e3a5b
JB
13951/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13952 in DIE. */
380bca97 13953
801e3a5b
JB
13954static void
13955dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13956 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13957{
518817b3 13958 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13960 struct attribute *attr;
91da1414 13961 struct attribute *attr_high;
801e3a5b 13962
91da1414
MW
13963 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13964 if (attr_high)
801e3a5b 13965 {
801e3a5b 13966 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13967 if (attr != nullptr)
801e3a5b 13968 {
cd6c91b4
TT
13969 CORE_ADDR low = attr->value_as_address ();
13970 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 13971
cd6c91b4 13972 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13973 high += low;
9a619af0 13974
3e29f34a
MR
13975 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13976 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 13977 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
13978 }
13979 }
13980
13981 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 13982 if (attr != nullptr)
801e3a5b 13983 {
18a8505e 13984 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13985 We take advantage of the fact that DW_AT_ranges does not appear
13986 in DW_TAG_compile_unit of DWO files. */
13987 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13988
13989 /* The value of the DW_AT_ranges attribute is the offset of the
13990 address range list in the .debug_ranges section. */
ab435259
DE
13991 unsigned long offset = (DW_UNSND (attr)
13992 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 13993
2d5f09ec 13994 std::vector<blockrange> blockvec;
5f46c5a5
JK
13995 dwarf2_ranges_process (offset, cu,
13996 [&] (CORE_ADDR start, CORE_ADDR end)
13997 {
58fdfd2c
JK
13998 start += baseaddr;
13999 end += baseaddr;
5f46c5a5
JK
14000 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14001 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14002 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14003 blockvec.emplace_back (start, end);
5f46c5a5 14004 });
2d5f09ec
KB
14005
14006 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14007 }
14008}
14009
685b1105
JK
14010/* Check whether the producer field indicates either of GCC < 4.6, or the
14011 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14012
685b1105
JK
14013static void
14014check_producer (struct dwarf2_cu *cu)
60d5a603 14015{
38360086 14016 int major, minor;
60d5a603
JK
14017
14018 if (cu->producer == NULL)
14019 {
14020 /* For unknown compilers expect their behavior is DWARF version
14021 compliant.
14022
14023 GCC started to support .debug_types sections by -gdwarf-4 since
14024 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14025 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14026 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14027 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14028 }
b1ffba5a 14029 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14030 {
38360086
MW
14031 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14032 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14033 }
5230b05a 14034 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14035 {
14036 cu->producer_is_icc = true;
14037 cu->producer_is_icc_lt_14 = major < 14;
14038 }
c258c396
JD
14039 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14040 cu->producer_is_codewarrior = true;
685b1105
JK
14041 else
14042 {
14043 /* For other non-GCC compilers, expect their behavior is DWARF version
14044 compliant. */
60d5a603
JK
14045 }
14046
9068261f 14047 cu->checked_producer = true;
685b1105 14048}
ba919b58 14049
685b1105
JK
14050/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14051 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14052 during 4.6.0 experimental. */
14053
9068261f 14054static bool
685b1105
JK
14055producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14056{
14057 if (!cu->checked_producer)
14058 check_producer (cu);
14059
14060 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14061}
14062
c258c396
JD
14063
14064/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14065 with incorrect is_stmt attributes. */
14066
14067static bool
14068producer_is_codewarrior (struct dwarf2_cu *cu)
14069{
14070 if (!cu->checked_producer)
14071 check_producer (cu);
14072
14073 return cu->producer_is_codewarrior;
14074}
14075
405feb71 14076/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14077 DW_AT_accessibility. */
14078
14079static enum dwarf_access_attribute
14080dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14081{
14082 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14083 {
14084 /* The default DWARF 2 accessibility for members is public, the default
14085 accessibility for inheritance is private. */
14086
14087 if (die->tag != DW_TAG_inheritance)
14088 return DW_ACCESS_public;
14089 else
14090 return DW_ACCESS_private;
14091 }
14092 else
14093 {
14094 /* DWARF 3+ defines the default accessibility a different way. The same
14095 rules apply now for DW_TAG_inheritance as for the members and it only
14096 depends on the container kind. */
14097
14098 if (die->parent->tag == DW_TAG_class_type)
14099 return DW_ACCESS_private;
14100 else
14101 return DW_ACCESS_public;
14102 }
14103}
14104
74ac6d43
TT
14105/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14106 offset. If the attribute was not found return 0, otherwise return
14107 1. If it was found but could not properly be handled, set *OFFSET
14108 to 0. */
14109
14110static int
14111handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14112 LONGEST *offset)
14113{
14114 struct attribute *attr;
14115
14116 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14117 if (attr != NULL)
14118 {
14119 *offset = 0;
14120
14121 /* Note that we do not check for a section offset first here.
14122 This is because DW_AT_data_member_location is new in DWARF 4,
14123 so if we see it, we can assume that a constant form is really
14124 a constant and not a section offset. */
cd6c91b4 14125 if (attr->form_is_constant ())
74ac6d43 14126 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14127 else if (attr->form_is_section_offset ())
74ac6d43 14128 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14129 else if (attr->form_is_block ())
74ac6d43
TT
14130 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14131 else
14132 dwarf2_complex_location_expr_complaint ();
14133
14134 return 1;
14135 }
14136
14137 return 0;
14138}
14139
c906108c
SS
14140/* Add an aggregate field to the field list. */
14141
14142static void
107d2387 14143dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14144 struct dwarf2_cu *cu)
6e70227d 14145{
518817b3 14146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14147 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14148 struct nextfield *new_field;
14149 struct attribute *attr;
14150 struct field *fp;
15d034d0 14151 const char *fieldname = "";
c906108c 14152
7d0ccb61
DJ
14153 if (die->tag == DW_TAG_inheritance)
14154 {
be2daae6
TT
14155 fip->baseclasses.emplace_back ();
14156 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14157 }
14158 else
14159 {
be2daae6
TT
14160 fip->fields.emplace_back ();
14161 new_field = &fip->fields.back ();
7d0ccb61 14162 }
be2daae6 14163
c906108c
SS
14164 fip->nfields++;
14165
e142c38c 14166 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14167 if (attr != nullptr)
c906108c 14168 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14169 else
14170 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14171 if (new_field->accessibility != DW_ACCESS_public)
14172 fip->non_public_fields = 1;
60d5a603 14173
e142c38c 14174 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14175 if (attr != nullptr)
c906108c 14176 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14177 else
14178 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14179
14180 fp = &new_field->field;
a9a9bd0f 14181
e142c38c 14182 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14183 {
74ac6d43
TT
14184 LONGEST offset;
14185
a9a9bd0f 14186 /* Data member other than a C++ static data member. */
6e70227d 14187
c906108c 14188 /* Get type of field. */
e7c27a73 14189 fp->type = die_type (die, cu);
c906108c 14190
d6a843b5 14191 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14192
c906108c 14193 /* Get bit size of field (zero if none). */
e142c38c 14194 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14195 if (attr != nullptr)
c906108c
SS
14196 {
14197 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14198 }
14199 else
14200 {
14201 FIELD_BITSIZE (*fp) = 0;
14202 }
14203
14204 /* Get bit offset of field. */
74ac6d43
TT
14205 if (handle_data_member_location (die, cu, &offset))
14206 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14207 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14208 if (attr != nullptr)
c906108c 14209 {
d5a22e77 14210 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14211 {
14212 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14213 additional bit offset from the MSB of the containing
14214 anonymous object to the MSB of the field. We don't
14215 have to do anything special since we don't need to
14216 know the size of the anonymous object. */
f41f5e61 14217 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14218 }
14219 else
14220 {
14221 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14222 MSB of the anonymous object, subtract off the number of
14223 bits from the MSB of the field to the MSB of the
14224 object, and then subtract off the number of bits of
14225 the field itself. The result is the bit offset of
14226 the LSB of the field. */
c906108c
SS
14227 int anonymous_size;
14228 int bit_offset = DW_UNSND (attr);
14229
e142c38c 14230 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14231 if (attr != nullptr)
c906108c
SS
14232 {
14233 /* The size of the anonymous object containing
14234 the bit field is explicit, so use the
14235 indicated size (in bytes). */
14236 anonymous_size = DW_UNSND (attr);
14237 }
14238 else
14239 {
14240 /* The size of the anonymous object containing
14241 the bit field must be inferred from the type
14242 attribute of the data member containing the
14243 bit field. */
14244 anonymous_size = TYPE_LENGTH (fp->type);
14245 }
f41f5e61
PA
14246 SET_FIELD_BITPOS (*fp,
14247 (FIELD_BITPOS (*fp)
14248 + anonymous_size * bits_per_byte
14249 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14250 }
14251 }
da5b30da
AA
14252 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14253 if (attr != NULL)
14254 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14255 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14256
14257 /* Get name of field. */
39cbfefa
DJ
14258 fieldname = dwarf2_name (die, cu);
14259 if (fieldname == NULL)
14260 fieldname = "";
d8151005
DJ
14261
14262 /* The name is already allocated along with this objfile, so we don't
14263 need to duplicate it for the type. */
14264 fp->name = fieldname;
c906108c
SS
14265
14266 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14267 pointer or virtual base class pointer) to private. */
e142c38c 14268 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14269 {
d48cc9dd 14270 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14271 new_field->accessibility = DW_ACCESS_private;
14272 fip->non_public_fields = 1;
14273 }
14274 }
a9a9bd0f 14275 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14276 {
a9a9bd0f
DC
14277 /* C++ static member. */
14278
14279 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14280 is a declaration, but all versions of G++ as of this writing
14281 (so through at least 3.2.1) incorrectly generate
14282 DW_TAG_variable tags. */
6e70227d 14283
ff355380 14284 const char *physname;
c906108c 14285
a9a9bd0f 14286 /* Get name of field. */
39cbfefa
DJ
14287 fieldname = dwarf2_name (die, cu);
14288 if (fieldname == NULL)
c906108c
SS
14289 return;
14290
254e6b9e 14291 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14292 if (attr
14293 /* Only create a symbol if this is an external value.
14294 new_symbol checks this and puts the value in the global symbol
14295 table, which we want. If it is not external, new_symbol
14296 will try to put the value in cu->list_in_scope which is wrong. */
14297 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14298 {
14299 /* A static const member, not much different than an enum as far as
14300 we're concerned, except that we can support more types. */
14301 new_symbol (die, NULL, cu);
14302 }
14303
2df3850c 14304 /* Get physical name. */
ff355380 14305 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14306
d8151005
DJ
14307 /* The name is already allocated along with this objfile, so we don't
14308 need to duplicate it for the type. */
14309 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14310 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14311 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14312 }
14313 else if (die->tag == DW_TAG_inheritance)
14314 {
74ac6d43 14315 LONGEST offset;
d4b96c9a 14316
74ac6d43
TT
14317 /* C++ base class field. */
14318 if (handle_data_member_location (die, cu, &offset))
14319 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14320 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14321 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14322 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14323 }
2ddeaf8a
TT
14324 else if (die->tag == DW_TAG_variant_part)
14325 {
14326 /* process_structure_scope will treat this DIE as a union. */
14327 process_structure_scope (die, cu);
14328
14329 /* The variant part is relative to the start of the enclosing
14330 structure. */
14331 SET_FIELD_BITPOS (*fp, 0);
14332 fp->type = get_die_type (die, cu);
14333 fp->artificial = 1;
14334 fp->name = "<<variant>>";
c8c81635
TT
14335
14336 /* Normally a DW_TAG_variant_part won't have a size, but our
14337 representation requires one, so set it to the maximum of the
489dbda6
TT
14338 child sizes, being sure to account for the offset at which
14339 each child is seen. */
c8c81635
TT
14340 if (TYPE_LENGTH (fp->type) == 0)
14341 {
14342 unsigned max = 0;
14343 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14344 {
14345 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14346 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14347 if (len > max)
14348 max = len;
14349 }
c8c81635
TT
14350 TYPE_LENGTH (fp->type) = max;
14351 }
2ddeaf8a
TT
14352 }
14353 else
14354 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14355}
14356
883fd55a
KS
14357/* Can the type given by DIE define another type? */
14358
14359static bool
14360type_can_define_types (const struct die_info *die)
14361{
14362 switch (die->tag)
14363 {
14364 case DW_TAG_typedef:
14365 case DW_TAG_class_type:
14366 case DW_TAG_structure_type:
14367 case DW_TAG_union_type:
14368 case DW_TAG_enumeration_type:
14369 return true;
14370
14371 default:
14372 return false;
14373 }
14374}
14375
14376/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14377
14378static void
883fd55a
KS
14379dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14380 struct dwarf2_cu *cu)
6e70227d 14381{
be2daae6
TT
14382 struct decl_field fp;
14383 memset (&fp, 0, sizeof (fp));
98751a41 14384
883fd55a 14385 gdb_assert (type_can_define_types (die));
98751a41 14386
883fd55a 14387 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14388 fp.name = dwarf2_name (die, cu);
14389 fp.type = read_type_die (die, cu);
98751a41 14390
c191a687
KS
14391 /* Save accessibility. */
14392 enum dwarf_access_attribute accessibility;
14393 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14394 if (attr != NULL)
14395 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14396 else
14397 accessibility = dwarf2_default_access_attribute (die, cu);
14398 switch (accessibility)
14399 {
14400 case DW_ACCESS_public:
14401 /* The assumed value if neither private nor protected. */
14402 break;
14403 case DW_ACCESS_private:
be2daae6 14404 fp.is_private = 1;
c191a687
KS
14405 break;
14406 case DW_ACCESS_protected:
be2daae6 14407 fp.is_protected = 1;
c191a687
KS
14408 break;
14409 default:
b98664d3 14410 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14411 }
14412
883fd55a 14413 if (die->tag == DW_TAG_typedef)
be2daae6 14414 fip->typedef_field_list.push_back (fp);
883fd55a 14415 else
be2daae6 14416 fip->nested_types_list.push_back (fp);
98751a41
JK
14417}
14418
c906108c
SS
14419/* Create the vector of fields, and attach it to the type. */
14420
14421static void
fba45db2 14422dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14423 struct dwarf2_cu *cu)
c906108c
SS
14424{
14425 int nfields = fip->nfields;
14426
14427 /* Record the field count, allocate space for the array of fields,
14428 and create blank accessibility bitfields if necessary. */
14429 TYPE_NFIELDS (type) = nfields;
14430 TYPE_FIELDS (type) = (struct field *)
be2daae6 14431 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14432
b4ba55a1 14433 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14434 {
14435 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14436
14437 TYPE_FIELD_PRIVATE_BITS (type) =
14438 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14439 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14440
14441 TYPE_FIELD_PROTECTED_BITS (type) =
14442 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14443 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14444
774b6a14
TT
14445 TYPE_FIELD_IGNORE_BITS (type) =
14446 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14447 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14448 }
14449
14450 /* If the type has baseclasses, allocate and clear a bit vector for
14451 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14452 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14453 {
be2daae6 14454 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14455 unsigned char *pointer;
c906108c
SS
14456
14457 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14458 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14459 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14460 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14461 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14462 }
14463
2ddeaf8a
TT
14464 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14465 {
14466 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14467
be2daae6 14468 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14469 {
be2daae6
TT
14470 struct nextfield &field = fip->fields[index];
14471
14472 if (field.variant.is_discriminant)
2ddeaf8a 14473 di->discriminant_index = index;
be2daae6 14474 else if (field.variant.default_branch)
2ddeaf8a
TT
14475 di->default_index = index;
14476 else
be2daae6 14477 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14478 }
14479 }
14480
be2daae6
TT
14481 /* Copy the saved-up fields into the field vector. */
14482 for (int i = 0; i < nfields; ++i)
c906108c 14483 {
be2daae6
TT
14484 struct nextfield &field
14485 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14486 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14487
be2daae6
TT
14488 TYPE_FIELD (type, i) = field.field;
14489 switch (field.accessibility)
c906108c 14490 {
c5aa993b 14491 case DW_ACCESS_private:
b4ba55a1 14492 if (cu->language != language_ada)
be2daae6 14493 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14494 break;
c906108c 14495
c5aa993b 14496 case DW_ACCESS_protected:
b4ba55a1 14497 if (cu->language != language_ada)
be2daae6 14498 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14499 break;
c906108c 14500
c5aa993b
JM
14501 case DW_ACCESS_public:
14502 break;
c906108c 14503
c5aa993b
JM
14504 default:
14505 /* Unknown accessibility. Complain and treat it as public. */
14506 {
b98664d3 14507 complaint (_("unsupported accessibility %d"),
be2daae6 14508 field.accessibility);
c5aa993b
JM
14509 }
14510 break;
c906108c 14511 }
be2daae6 14512 if (i < fip->baseclasses.size ())
c906108c 14513 {
be2daae6 14514 switch (field.virtuality)
c906108c 14515 {
c5aa993b
JM
14516 case DW_VIRTUALITY_virtual:
14517 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14518 if (cu->language == language_ada)
a73c6dcd 14519 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14520 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14521 break;
c906108c
SS
14522 }
14523 }
c906108c
SS
14524 }
14525}
14526
7d27a96d
TT
14527/* Return true if this member function is a constructor, false
14528 otherwise. */
14529
14530static int
14531dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14532{
14533 const char *fieldname;
fe978cb0 14534 const char *type_name;
7d27a96d
TT
14535 int len;
14536
14537 if (die->parent == NULL)
14538 return 0;
14539
14540 if (die->parent->tag != DW_TAG_structure_type
14541 && die->parent->tag != DW_TAG_union_type
14542 && die->parent->tag != DW_TAG_class_type)
14543 return 0;
14544
14545 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14546 type_name = dwarf2_name (die->parent, cu);
14547 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14548 return 0;
14549
14550 len = strlen (fieldname);
fe978cb0
PA
14551 return (strncmp (fieldname, type_name, len) == 0
14552 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14553}
14554
e35000a7
TBA
14555/* Check if the given VALUE is a recognized enum
14556 dwarf_defaulted_attribute constant according to DWARF5 spec,
14557 Table 7.24. */
14558
14559static bool
14560is_valid_DW_AT_defaulted (ULONGEST value)
14561{
14562 switch (value)
14563 {
14564 case DW_DEFAULTED_no:
14565 case DW_DEFAULTED_in_class:
14566 case DW_DEFAULTED_out_of_class:
14567 return true;
14568 }
14569
3142e908 14570 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14571 return false;
14572}
14573
c906108c
SS
14574/* Add a member function to the proper fieldlist. */
14575
14576static void
107d2387 14577dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14578 struct type *type, struct dwarf2_cu *cu)
c906108c 14579{
518817b3 14580 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14581 struct attribute *attr;
c906108c 14582 int i;
be2daae6 14583 struct fnfieldlist *flp = nullptr;
c906108c 14584 struct fn_field *fnp;
15d034d0 14585 const char *fieldname;
f792889a 14586 struct type *this_type;
60d5a603 14587 enum dwarf_access_attribute accessibility;
c906108c 14588
b4ba55a1 14589 if (cu->language == language_ada)
a73c6dcd 14590 error (_("unexpected member function in Ada type"));
b4ba55a1 14591
2df3850c 14592 /* Get name of member function. */
39cbfefa
DJ
14593 fieldname = dwarf2_name (die, cu);
14594 if (fieldname == NULL)
2df3850c 14595 return;
c906108c 14596
c906108c 14597 /* Look up member function name in fieldlist. */
be2daae6 14598 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14599 {
27bfe10e 14600 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14601 {
14602 flp = &fip->fnfieldlists[i];
14603 break;
14604 }
c906108c
SS
14605 }
14606
be2daae6
TT
14607 /* Create a new fnfieldlist if necessary. */
14608 if (flp == nullptr)
c906108c 14609 {
be2daae6
TT
14610 fip->fnfieldlists.emplace_back ();
14611 flp = &fip->fnfieldlists.back ();
c906108c 14612 flp->name = fieldname;
be2daae6 14613 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14614 }
14615
be2daae6
TT
14616 /* Create a new member function field and add it to the vector of
14617 fnfieldlists. */
14618 flp->fnfields.emplace_back ();
14619 fnp = &flp->fnfields.back ();
3da10d80
KS
14620
14621 /* Delay processing of the physname until later. */
9c37b5ae 14622 if (cu->language == language_cplus)
be2daae6
TT
14623 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14624 die, cu);
3da10d80
KS
14625 else
14626 {
1d06ead6 14627 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14628 fnp->physname = physname ? physname : "";
14629 }
14630
c906108c 14631 fnp->type = alloc_type (objfile);
f792889a
DJ
14632 this_type = read_type_die (die, cu);
14633 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14634 {
f792889a 14635 int nparams = TYPE_NFIELDS (this_type);
c906108c 14636
f792889a 14637 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14638 of the method itself (TYPE_CODE_METHOD). */
14639 smash_to_method_type (fnp->type, type,
f792889a
DJ
14640 TYPE_TARGET_TYPE (this_type),
14641 TYPE_FIELDS (this_type),
14642 TYPE_NFIELDS (this_type),
14643 TYPE_VARARGS (this_type));
c906108c
SS
14644
14645 /* Handle static member functions.
c5aa993b 14646 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14647 member functions. G++ helps GDB by marking the first
14648 parameter for non-static member functions (which is the this
14649 pointer) as artificial. We obtain this information from
14650 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14651 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14652 fnp->voffset = VOFFSET_STATIC;
14653 }
14654 else
b98664d3 14655 complaint (_("member function type missing for '%s'"),
3da10d80 14656 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14657
14658 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14659 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14660 fnp->fcontext = die_containing_type (die, cu);
c906108c 14661
3e43a32a
MS
14662 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14663 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14664
14665 /* Get accessibility. */
e142c38c 14666 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14667 if (attr != nullptr)
aead7601 14668 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14669 else
14670 accessibility = dwarf2_default_access_attribute (die, cu);
14671 switch (accessibility)
c906108c 14672 {
60d5a603
JK
14673 case DW_ACCESS_private:
14674 fnp->is_private = 1;
14675 break;
14676 case DW_ACCESS_protected:
14677 fnp->is_protected = 1;
14678 break;
c906108c
SS
14679 }
14680
b02dede2 14681 /* Check for artificial methods. */
e142c38c 14682 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14683 if (attr && DW_UNSND (attr) != 0)
14684 fnp->is_artificial = 1;
14685
e35000a7
TBA
14686 /* Check for defaulted methods. */
14687 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14688 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14689 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14690
14691 /* Check for deleted methods. */
14692 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14693 if (attr != nullptr && DW_UNSND (attr) != 0)
14694 fnp->is_deleted = 1;
14695
7d27a96d
TT
14696 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14697
0d564a31 14698 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14699 function. For older versions of GCC, this is an offset in the
14700 appropriate virtual table, as specified by DW_AT_containing_type.
14701 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14702 to the object address. */
14703
e142c38c 14704 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14705 if (attr != nullptr)
8e19ed76 14706 {
4fc6c0d5 14707 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14708 {
aec5aa8b
TT
14709 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14710 {
14711 /* Old-style GCC. */
14712 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14713 }
14714 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14715 || (DW_BLOCK (attr)->size > 1
14716 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14717 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14718 {
aec5aa8b
TT
14719 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14720 if ((fnp->voffset % cu->header.addr_size) != 0)
14721 dwarf2_complex_location_expr_complaint ();
14722 else
14723 fnp->voffset /= cu->header.addr_size;
14724 fnp->voffset += 2;
14725 }
14726 else
14727 dwarf2_complex_location_expr_complaint ();
14728
14729 if (!fnp->fcontext)
7e993ebf
KS
14730 {
14731 /* If there is no `this' field and no DW_AT_containing_type,
14732 we cannot actually find a base class context for the
14733 vtable! */
14734 if (TYPE_NFIELDS (this_type) == 0
14735 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14736 {
b98664d3 14737 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14738 "function \"%s\" (offset %s)"),
14739 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14740 }
14741 else
14742 {
14743 fnp->fcontext
14744 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14745 }
14746 }
aec5aa8b 14747 }
cd6c91b4 14748 else if (attr->form_is_section_offset ())
8e19ed76 14749 {
4d3c2250 14750 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14751 }
14752 else
14753 {
4d3c2250
KB
14754 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14755 fieldname);
8e19ed76 14756 }
0d564a31 14757 }
d48cc9dd
DJ
14758 else
14759 {
14760 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14761 if (attr && DW_UNSND (attr))
14762 {
14763 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14764 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14765 "but the vtable offset is not specified"),
9d8780f0 14766 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14767 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14768 TYPE_CPLUS_DYNAMIC (type) = 1;
14769 }
14770 }
c906108c
SS
14771}
14772
14773/* Create the vector of member function fields, and attach it to the type. */
14774
14775static void
fba45db2 14776dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14777 struct dwarf2_cu *cu)
c906108c 14778{
b4ba55a1 14779 if (cu->language == language_ada)
a73c6dcd 14780 error (_("unexpected member functions in Ada type"));
b4ba55a1 14781
c906108c
SS
14782 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14783 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14784 TYPE_ALLOC (type,
14785 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14786
be2daae6 14787 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14788 {
be2daae6 14789 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14790 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14791
be2daae6
TT
14792 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14793 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14794 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14795 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14796
14797 for (int k = 0; k < nf.fnfields.size (); ++k)
14798 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14799 }
14800
be2daae6 14801 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14802}
14803
1168df01
JB
14804/* Returns non-zero if NAME is the name of a vtable member in CU's
14805 language, zero otherwise. */
14806static int
14807is_vtable_name (const char *name, struct dwarf2_cu *cu)
14808{
14809 static const char vptr[] = "_vptr";
14810
9c37b5ae
TT
14811 /* Look for the C++ form of the vtable. */
14812 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14813 return 1;
14814
14815 return 0;
14816}
14817
c0dd20ea 14818/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14819 functions, with the ABI-specified layout. If TYPE describes
14820 such a structure, smash it into a member function type.
61049d3b
DJ
14821
14822 GCC shouldn't do this; it should just output pointer to member DIEs.
14823 This is GCC PR debug/28767. */
c0dd20ea 14824
0b92b5bb
TT
14825static void
14826quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14827{
09e2d7c7 14828 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14829
14830 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14831 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14832 return;
c0dd20ea
DJ
14833
14834 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14835 if (TYPE_FIELD_NAME (type, 0) == NULL
14836 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14837 || TYPE_FIELD_NAME (type, 1) == NULL
14838 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14839 return;
c0dd20ea
DJ
14840
14841 /* Find the type of the method. */
0b92b5bb 14842 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14843 if (pfn_type == NULL
14844 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14845 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14846 return;
c0dd20ea
DJ
14847
14848 /* Look for the "this" argument. */
14849 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14850 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14851 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14852 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14853 return;
c0dd20ea 14854
09e2d7c7 14855 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14856 new_type = alloc_type (objfile);
09e2d7c7 14857 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14858 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14859 TYPE_VARARGS (pfn_type));
0b92b5bb 14860 smash_to_methodptr_type (type, new_type);
c0dd20ea 14861}
1168df01 14862
2b4424c3
TT
14863/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14864 appropriate error checking and issuing complaints if there is a
14865 problem. */
14866
14867static ULONGEST
14868get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14869{
14870 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14871
14872 if (attr == nullptr)
14873 return 0;
14874
cd6c91b4 14875 if (!attr->form_is_constant ())
2b4424c3 14876 {
b98664d3 14877 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14878 " - DIE at %s [in module %s]"),
14879 sect_offset_str (die->sect_off),
14880 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14881 return 0;
14882 }
14883
14884 ULONGEST align;
14885 if (attr->form == DW_FORM_sdata)
14886 {
14887 LONGEST val = DW_SND (attr);
14888 if (val < 0)
14889 {
b98664d3 14890 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14891 " - DIE at %s [in module %s]"),
14892 sect_offset_str (die->sect_off),
14893 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14894 return 0;
14895 }
14896 align = val;
14897 }
14898 else
14899 align = DW_UNSND (attr);
14900
14901 if (align == 0)
14902 {
b98664d3 14903 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14904 " - DIE at %s [in module %s]"),
14905 sect_offset_str (die->sect_off),
14906 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14907 return 0;
14908 }
14909 if ((align & (align - 1)) != 0)
14910 {
b98664d3 14911 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14912 " - DIE at %s [in module %s]"),
14913 sect_offset_str (die->sect_off),
14914 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14915 return 0;
14916 }
14917
14918 return align;
14919}
14920
14921/* If the DIE has a DW_AT_alignment attribute, use its value to set
14922 the alignment for TYPE. */
14923
14924static void
14925maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14926 struct type *type)
14927{
14928 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14929 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14930 " - DIE at %s [in module %s]"),
14931 sect_offset_str (die->sect_off),
14932 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14933}
685b1105 14934
e35000a7
TBA
14935/* Check if the given VALUE is a valid enum dwarf_calling_convention
14936 constant for a type, according to DWARF5 spec, Table 5.5. */
14937
14938static bool
14939is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14940{
14941 switch (value)
14942 {
14943 case DW_CC_normal:
14944 case DW_CC_pass_by_reference:
14945 case DW_CC_pass_by_value:
14946 return true;
14947
14948 default:
14949 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14950 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14951 return false;
14952 }
14953}
14954
d0922fcf
TBA
14955/* Check if the given VALUE is a valid enum dwarf_calling_convention
14956 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14957 also according to GNU-specific values (see include/dwarf2.h). */
14958
14959static bool
14960is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14961{
14962 switch (value)
14963 {
14964 case DW_CC_normal:
14965 case DW_CC_program:
14966 case DW_CC_nocall:
14967 return true;
14968
14969 case DW_CC_GNU_renesas_sh:
14970 case DW_CC_GNU_borland_fastcall_i386:
14971 case DW_CC_GDB_IBM_OpenCL:
14972 return true;
14973
14974 default:
14975 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14976 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
14977 return false;
14978 }
14979}
14980
c906108c 14981/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14982 (definition) to create a type for the structure or union. Fill in
14983 the type's name and general properties; the members will not be
83655187
DE
14984 processed until process_structure_scope. A symbol table entry for
14985 the type will also not be done until process_structure_scope (assuming
14986 the type has a name).
c906108c 14987
c767944b
DJ
14988 NOTE: we need to call these functions regardless of whether or not the
14989 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 14990 structure or union. This gets the type entered into our set of
83655187 14991 user defined types. */
c906108c 14992
f792889a 14993static struct type *
134d01f1 14994read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14995{
518817b3 14996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
14997 struct type *type;
14998 struct attribute *attr;
15d034d0 14999 const char *name;
c906108c 15000
348e048f
DE
15001 /* If the definition of this type lives in .debug_types, read that type.
15002 Don't follow DW_AT_specification though, that will take us back up
15003 the chain and we want to go down. */
45e58e77 15004 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15005 if (attr != nullptr)
348e048f 15006 {
ac9ec31b 15007 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15008
ac9ec31b 15009 /* The type's CU may not be the same as CU.
02142a6c 15010 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15011 return set_die_type (die, type, cu);
15012 }
15013
c0dd20ea 15014 type = alloc_type (objfile);
c906108c 15015 INIT_CPLUS_SPECIFIC (type);
93311388 15016
39cbfefa
DJ
15017 name = dwarf2_name (die, cu);
15018 if (name != NULL)
c906108c 15019 {
987504bb 15020 if (cu->language == language_cplus
c44af4eb
TT
15021 || cu->language == language_d
15022 || cu->language == language_rust)
63d06c5c 15023 {
15d034d0 15024 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15025
15026 /* dwarf2_full_name might have already finished building the DIE's
15027 type. If so, there is no need to continue. */
15028 if (get_die_type (die, cu) != NULL)
15029 return get_die_type (die, cu);
15030
e86ca25f 15031 TYPE_NAME (type) = full_name;
63d06c5c
DC
15032 }
15033 else
15034 {
d8151005
DJ
15035 /* The name is already allocated along with this objfile, so
15036 we don't need to duplicate it for the type. */
e86ca25f 15037 TYPE_NAME (type) = name;
63d06c5c 15038 }
c906108c
SS
15039 }
15040
15041 if (die->tag == DW_TAG_structure_type)
15042 {
15043 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15044 }
15045 else if (die->tag == DW_TAG_union_type)
15046 {
15047 TYPE_CODE (type) = TYPE_CODE_UNION;
15048 }
2ddeaf8a
TT
15049 else if (die->tag == DW_TAG_variant_part)
15050 {
15051 TYPE_CODE (type) = TYPE_CODE_UNION;
15052 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15053 }
c906108c
SS
15054 else
15055 {
4753d33b 15056 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15057 }
15058
0cc2414c
TT
15059 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15060 TYPE_DECLARED_CLASS (type) = 1;
15061
e35000a7
TBA
15062 /* Store the calling convention in the type if it's available in
15063 the die. Otherwise the calling convention remains set to
15064 the default value DW_CC_normal. */
15065 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15066 if (attr != nullptr
15067 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15068 {
15069 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15070 TYPE_CPLUS_CALLING_CONVENTION (type)
15071 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15072 }
15073
e142c38c 15074 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15075 if (attr != nullptr)
c906108c 15076 {
cd6c91b4 15077 if (attr->form_is_constant ())
155bfbd3
JB
15078 TYPE_LENGTH (type) = DW_UNSND (attr);
15079 else
15080 {
15081 /* For the moment, dynamic type sizes are not supported
15082 by GDB's struct type. The actual size is determined
15083 on-demand when resolving the type of a given object,
15084 so set the type's length to zero for now. Otherwise,
15085 we record an expression as the length, and that expression
15086 could lead to a very large value, which could eventually
15087 lead to us trying to allocate that much memory when creating
15088 a value of that type. */
15089 TYPE_LENGTH (type) = 0;
15090 }
c906108c
SS
15091 }
15092 else
15093 {
15094 TYPE_LENGTH (type) = 0;
15095 }
15096
2b4424c3
TT
15097 maybe_set_alignment (cu, die, type);
15098
5230b05a 15099 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15100 {
5230b05a
WT
15101 /* ICC<14 does not output the required DW_AT_declaration on
15102 incomplete types, but gives them a size of zero. */
422b1cb0 15103 TYPE_STUB (type) = 1;
685b1105
JK
15104 }
15105 else
15106 TYPE_STUB_SUPPORTED (type) = 1;
15107
dc718098 15108 if (die_is_declaration (die, cu))
876cecd0 15109 TYPE_STUB (type) = 1;
a6c727b2
DJ
15110 else if (attr == NULL && die->child == NULL
15111 && producer_is_realview (cu->producer))
15112 /* RealView does not output the required DW_AT_declaration
15113 on incomplete types. */
15114 TYPE_STUB (type) = 1;
dc718098 15115
c906108c
SS
15116 /* We need to add the type field to the die immediately so we don't
15117 infinitely recurse when dealing with pointers to the structure
0963b4bd 15118 type within the structure itself. */
1c379e20 15119 set_die_type (die, type, cu);
c906108c 15120
7e314c57
JK
15121 /* set_die_type should be already done. */
15122 set_descriptive_type (type, die, cu);
15123
c767944b
DJ
15124 return type;
15125}
15126
2ddeaf8a
TT
15127/* A helper for process_structure_scope that handles a single member
15128 DIE. */
15129
15130static void
15131handle_struct_member_die (struct die_info *child_die, struct type *type,
15132 struct field_info *fi,
15133 std::vector<struct symbol *> *template_args,
15134 struct dwarf2_cu *cu)
15135{
15136 if (child_die->tag == DW_TAG_member
15137 || child_die->tag == DW_TAG_variable
15138 || child_die->tag == DW_TAG_variant_part)
15139 {
15140 /* NOTE: carlton/2002-11-05: A C++ static data member
15141 should be a DW_TAG_member that is a declaration, but
15142 all versions of G++ as of this writing (so through at
15143 least 3.2.1) incorrectly generate DW_TAG_variable
15144 tags for them instead. */
15145 dwarf2_add_field (fi, child_die, cu);
15146 }
15147 else if (child_die->tag == DW_TAG_subprogram)
15148 {
15149 /* Rust doesn't have member functions in the C++ sense.
15150 However, it does emit ordinary functions as children
15151 of a struct DIE. */
15152 if (cu->language == language_rust)
15153 read_func_scope (child_die, cu);
15154 else
15155 {
15156 /* C++ member function. */
15157 dwarf2_add_member_fn (fi, child_die, type, cu);
15158 }
15159 }
15160 else if (child_die->tag == DW_TAG_inheritance)
15161 {
15162 /* C++ base class field. */
15163 dwarf2_add_field (fi, child_die, cu);
15164 }
15165 else if (type_can_define_types (child_die))
15166 dwarf2_add_type_defn (fi, child_die, cu);
15167 else if (child_die->tag == DW_TAG_template_type_param
15168 || child_die->tag == DW_TAG_template_value_param)
15169 {
15170 struct symbol *arg = new_symbol (child_die, NULL, cu);
15171
15172 if (arg != NULL)
15173 template_args->push_back (arg);
15174 }
15175 else if (child_die->tag == DW_TAG_variant)
15176 {
15177 /* In a variant we want to get the discriminant and also add a
15178 field for our sole member child. */
15179 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15180
bde09ab7 15181 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15182 variant_child != NULL;
15183 variant_child = sibling_die (variant_child))
15184 {
15185 if (variant_child->tag == DW_TAG_member)
15186 {
15187 handle_struct_member_die (variant_child, type, fi,
15188 template_args, cu);
15189 /* Only handle the one. */
15190 break;
15191 }
15192 }
15193
15194 /* We don't handle this but we might as well report it if we see
15195 it. */
15196 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15197 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15198 " - DIE at %s [in module %s]"),
15199 sect_offset_str (child_die->sect_off),
15200 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15201
15202 /* The first field was just added, so we can stash the
15203 discriminant there. */
be2daae6 15204 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15205 if (discr == NULL)
be2daae6 15206 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15207 else
be2daae6 15208 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15209 }
15210}
15211
c767944b
DJ
15212/* Finish creating a structure or union type, including filling in
15213 its members and creating a symbol for it. */
15214
15215static void
15216process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15217{
518817b3 15218 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15219 struct die_info *child_die;
c767944b
DJ
15220 struct type *type;
15221
15222 type = get_die_type (die, cu);
15223 if (type == NULL)
15224 type = read_structure_type (die, cu);
15225
2ddeaf8a
TT
15226 /* When reading a DW_TAG_variant_part, we need to notice when we
15227 read the discriminant member, so we can record it later in the
15228 discriminant_info. */
15229 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15230 sect_offset discr_offset {};
3e1d3d8c 15231 bool has_template_parameters = false;
2ddeaf8a
TT
15232
15233 if (is_variant_part)
15234 {
15235 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15236 if (discr == NULL)
15237 {
15238 /* Maybe it's a univariant form, an extension we support.
15239 In this case arrange not to check the offset. */
15240 is_variant_part = false;
15241 }
cd6c91b4 15242 else if (discr->form_is_ref ())
2ddeaf8a
TT
15243 {
15244 struct dwarf2_cu *target_cu = cu;
15245 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15246
15247 discr_offset = target_die->sect_off;
15248 }
15249 else
15250 {
b98664d3 15251 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15252 " - DIE at %s [in module %s]"),
15253 sect_offset_str (die->sect_off),
15254 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15255 is_variant_part = false;
15256 }
15257 }
15258
e142c38c 15259 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15260 {
15261 struct field_info fi;
2f4732b0 15262 std::vector<struct symbol *> template_args;
c906108c 15263
639d11d3 15264 child_die = die->child;
c906108c
SS
15265
15266 while (child_die && child_die->tag)
15267 {
2ddeaf8a 15268 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15269
2ddeaf8a 15270 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15271 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15272
c906108c
SS
15273 child_die = sibling_die (child_die);
15274 }
15275
34eaf542 15276 /* Attach template arguments to type. */
2f4732b0 15277 if (!template_args.empty ())
34eaf542 15278 {
3e1d3d8c 15279 has_template_parameters = true;
34eaf542 15280 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15281 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15282 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15283 = XOBNEWVEC (&objfile->objfile_obstack,
15284 struct symbol *,
15285 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15286 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15287 template_args.data (),
34eaf542
TT
15288 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15289 * sizeof (struct symbol *)));
34eaf542
TT
15290 }
15291
c906108c
SS
15292 /* Attach fields and member functions to the type. */
15293 if (fi.nfields)
e7c27a73 15294 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15295 if (!fi.fnfieldlists.empty ())
c906108c 15296 {
e7c27a73 15297 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15298
c5aa993b 15299 /* Get the type which refers to the base class (possibly this
c906108c 15300 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15301 class from the DW_AT_containing_type attribute. This use of
15302 DW_AT_containing_type is a GNU extension. */
c906108c 15303
e142c38c 15304 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15305 {
e7c27a73 15306 struct type *t = die_containing_type (die, cu);
c906108c 15307
ae6ae975 15308 set_type_vptr_basetype (type, t);
c906108c
SS
15309 if (type == t)
15310 {
c906108c
SS
15311 int i;
15312
15313 /* Our own class provides vtbl ptr. */
15314 for (i = TYPE_NFIELDS (t) - 1;
15315 i >= TYPE_N_BASECLASSES (t);
15316 --i)
15317 {
0d5cff50 15318 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15319
1168df01 15320 if (is_vtable_name (fieldname, cu))
c906108c 15321 {
ae6ae975 15322 set_type_vptr_fieldno (type, i);
c906108c
SS
15323 break;
15324 }
15325 }
15326
15327 /* Complain if virtual function table field not found. */
15328 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15329 complaint (_("virtual function table pointer "
3e43a32a 15330 "not found when defining class '%s'"),
e86ca25f 15331 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15332 }
15333 else
15334 {
ae6ae975 15335 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15336 }
15337 }
f6235d4c 15338 else if (cu->producer
61012eef 15339 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15340 {
15341 /* The IBM XLC compiler does not provide direct indication
15342 of the containing type, but the vtable pointer is
15343 always named __vfp. */
15344
15345 int i;
15346
15347 for (i = TYPE_NFIELDS (type) - 1;
15348 i >= TYPE_N_BASECLASSES (type);
15349 --i)
15350 {
15351 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15352 {
ae6ae975
DE
15353 set_type_vptr_fieldno (type, i);
15354 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15355 break;
15356 }
15357 }
15358 }
c906108c 15359 }
98751a41
JK
15360
15361 /* Copy fi.typedef_field_list linked list elements content into the
15362 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15363 if (!fi.typedef_field_list.empty ())
98751a41 15364 {
be2daae6 15365 int count = fi.typedef_field_list.size ();
98751a41 15366
a0d7a4ff 15367 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15368 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15369 = ((struct decl_field *)
be2daae6
TT
15370 TYPE_ALLOC (type,
15371 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15372 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15373
be2daae6
TT
15374 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15375 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15376 }
c767944b 15377
883fd55a
KS
15378 /* Copy fi.nested_types_list linked list elements content into the
15379 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15380 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15381 {
be2daae6 15382 int count = fi.nested_types_list.size ();
883fd55a
KS
15383
15384 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15385 TYPE_NESTED_TYPES_ARRAY (type)
15386 = ((struct decl_field *)
be2daae6
TT
15387 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15388 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15389
be2daae6
TT
15390 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15391 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15392 }
c906108c 15393 }
63d06c5c 15394
bb5ed363 15395 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15396 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15397 cu->rust_unions.push_back (type);
0b92b5bb 15398
90aeadfc
DC
15399 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15400 snapshots) has been known to create a die giving a declaration
15401 for a class that has, as a child, a die giving a definition for a
15402 nested class. So we have to process our children even if the
15403 current die is a declaration. Normally, of course, a declaration
15404 won't have any children at all. */
134d01f1 15405
ca040673
DE
15406 child_die = die->child;
15407
90aeadfc
DC
15408 while (child_die != NULL && child_die->tag)
15409 {
15410 if (child_die->tag == DW_TAG_member
15411 || child_die->tag == DW_TAG_variable
34eaf542
TT
15412 || child_die->tag == DW_TAG_inheritance
15413 || child_die->tag == DW_TAG_template_value_param
15414 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15415 {
90aeadfc 15416 /* Do nothing. */
134d01f1 15417 }
90aeadfc
DC
15418 else
15419 process_die (child_die, cu);
134d01f1 15420
90aeadfc 15421 child_die = sibling_die (child_die);
134d01f1
DJ
15422 }
15423
fa4028e9
JB
15424 /* Do not consider external references. According to the DWARF standard,
15425 these DIEs are identified by the fact that they have no byte_size
15426 attribute, and a declaration attribute. */
15427 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15428 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15429 {
15430 struct symbol *sym = new_symbol (die, type, cu);
15431
15432 if (has_template_parameters)
15433 {
a776957c
TT
15434 struct symtab *symtab;
15435 if (sym != nullptr)
15436 symtab = symbol_symtab (sym);
15437 else if (cu->line_header != nullptr)
15438 {
15439 /* Any related symtab will do. */
15440 symtab
7ba99d21 15441 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15442 }
15443 else
15444 {
15445 symtab = nullptr;
15446 complaint (_("could not find suitable "
15447 "symtab for template parameter"
15448 " - DIE at %s [in module %s]"),
15449 sect_offset_str (die->sect_off),
15450 objfile_name (objfile));
15451 }
15452
15453 if (symtab != nullptr)
15454 {
15455 /* Make sure that the symtab is set on the new symbols.
15456 Even though they don't appear in this symtab directly,
15457 other parts of gdb assume that symbols do, and this is
15458 reasonably true. */
15459 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15460 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15461 }
3e1d3d8c
TT
15462 }
15463 }
134d01f1
DJ
15464}
15465
55426c9d
JB
15466/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15467 update TYPE using some information only available in DIE's children. */
15468
15469static void
15470update_enumeration_type_from_children (struct die_info *die,
15471 struct type *type,
15472 struct dwarf2_cu *cu)
15473{
60f7655a 15474 struct die_info *child_die;
55426c9d
JB
15475 int unsigned_enum = 1;
15476 int flag_enum = 1;
55426c9d 15477
8268c778 15478 auto_obstack obstack;
55426c9d 15479
60f7655a
DE
15480 for (child_die = die->child;
15481 child_die != NULL && child_die->tag;
15482 child_die = sibling_die (child_die))
55426c9d
JB
15483 {
15484 struct attribute *attr;
15485 LONGEST value;
15486 const gdb_byte *bytes;
15487 struct dwarf2_locexpr_baton *baton;
15488 const char *name;
60f7655a 15489
55426c9d
JB
15490 if (child_die->tag != DW_TAG_enumerator)
15491 continue;
15492
15493 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15494 if (attr == NULL)
15495 continue;
15496
15497 name = dwarf2_name (child_die, cu);
15498 if (name == NULL)
15499 name = "<anonymous enumerator>";
15500
15501 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15502 &value, &bytes, &baton);
15503 if (value < 0)
15504 {
15505 unsigned_enum = 0;
15506 flag_enum = 0;
15507 }
55426c9d 15508 else
edd45eb0
SM
15509 {
15510 if (count_one_bits_ll (value) >= 2)
15511 flag_enum = 0;
edd45eb0 15512 }
55426c9d
JB
15513
15514 /* If we already know that the enum type is neither unsigned, nor
15515 a flag type, no need to look at the rest of the enumerates. */
15516 if (!unsigned_enum && !flag_enum)
15517 break;
55426c9d
JB
15518 }
15519
15520 if (unsigned_enum)
15521 TYPE_UNSIGNED (type) = 1;
15522 if (flag_enum)
15523 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15524}
15525
134d01f1
DJ
15526/* Given a DW_AT_enumeration_type die, set its type. We do not
15527 complete the type's fields yet, or create any symbols. */
c906108c 15528
f792889a 15529static struct type *
134d01f1 15530read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15531{
518817b3 15532 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15533 struct type *type;
c906108c 15534 struct attribute *attr;
0114d602 15535 const char *name;
134d01f1 15536
348e048f
DE
15537 /* If the definition of this type lives in .debug_types, read that type.
15538 Don't follow DW_AT_specification though, that will take us back up
15539 the chain and we want to go down. */
45e58e77 15540 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15541 if (attr != nullptr)
348e048f 15542 {
ac9ec31b 15543 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15544
ac9ec31b 15545 /* The type's CU may not be the same as CU.
02142a6c 15546 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15547 return set_die_type (die, type, cu);
15548 }
15549
c906108c
SS
15550 type = alloc_type (objfile);
15551
15552 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15553 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15554 if (name != NULL)
e86ca25f 15555 TYPE_NAME (type) = name;
c906108c 15556
0626fc76
TT
15557 attr = dwarf2_attr (die, DW_AT_type, cu);
15558 if (attr != NULL)
15559 {
15560 struct type *underlying_type = die_type (die, cu);
15561
15562 TYPE_TARGET_TYPE (type) = underlying_type;
15563 }
15564
e142c38c 15565 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15566 if (attr != nullptr)
c906108c
SS
15567 {
15568 TYPE_LENGTH (type) = DW_UNSND (attr);
15569 }
15570 else
15571 {
15572 TYPE_LENGTH (type) = 0;
15573 }
15574
2b4424c3
TT
15575 maybe_set_alignment (cu, die, type);
15576
137033e9
JB
15577 /* The enumeration DIE can be incomplete. In Ada, any type can be
15578 declared as private in the package spec, and then defined only
15579 inside the package body. Such types are known as Taft Amendment
15580 Types. When another package uses such a type, an incomplete DIE
15581 may be generated by the compiler. */
02eb380e 15582 if (die_is_declaration (die, cu))
876cecd0 15583 TYPE_STUB (type) = 1;
02eb380e 15584
0626fc76
TT
15585 /* Finish the creation of this type by using the enum's children.
15586 We must call this even when the underlying type has been provided
15587 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15588 update_enumeration_type_from_children (die, type, cu);
15589
0626fc76
TT
15590 /* If this type has an underlying type that is not a stub, then we
15591 may use its attributes. We always use the "unsigned" attribute
15592 in this situation, because ordinarily we guess whether the type
15593 is unsigned -- but the guess can be wrong and the underlying type
15594 can tell us the reality. However, we defer to a local size
15595 attribute if one exists, because this lets the compiler override
15596 the underlying type if needed. */
15597 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15598 {
15599 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15600 if (TYPE_LENGTH (type) == 0)
15601 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15602 if (TYPE_RAW_ALIGN (type) == 0
15603 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15604 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15605 }
15606
3d567982
TT
15607 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15608
f792889a 15609 return set_die_type (die, type, cu);
134d01f1
DJ
15610}
15611
15612/* Given a pointer to a die which begins an enumeration, process all
15613 the dies that define the members of the enumeration, and create the
15614 symbol for the enumeration type.
15615
15616 NOTE: We reverse the order of the element list. */
15617
15618static void
15619process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15620{
f792889a 15621 struct type *this_type;
134d01f1 15622
f792889a
DJ
15623 this_type = get_die_type (die, cu);
15624 if (this_type == NULL)
15625 this_type = read_enumeration_type (die, cu);
9dc481d3 15626
639d11d3 15627 if (die->child != NULL)
c906108c 15628 {
9dc481d3
DE
15629 struct die_info *child_die;
15630 struct symbol *sym;
43816ebc 15631 std::vector<struct field> fields;
15d034d0 15632 const char *name;
9dc481d3 15633
639d11d3 15634 child_die = die->child;
c906108c
SS
15635 while (child_die && child_die->tag)
15636 {
15637 if (child_die->tag != DW_TAG_enumerator)
15638 {
e7c27a73 15639 process_die (child_die, cu);
c906108c
SS
15640 }
15641 else
15642 {
39cbfefa
DJ
15643 name = dwarf2_name (child_die, cu);
15644 if (name)
c906108c 15645 {
f792889a 15646 sym = new_symbol (child_die, this_type, cu);
c906108c 15647
43816ebc
TT
15648 fields.emplace_back ();
15649 struct field &field = fields.back ();
c906108c 15650
43816ebc
TT
15651 FIELD_NAME (field) = sym->linkage_name ();
15652 FIELD_TYPE (field) = NULL;
15653 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15654 FIELD_BITSIZE (field) = 0;
c906108c
SS
15655 }
15656 }
15657
15658 child_die = sibling_die (child_die);
15659 }
15660
43816ebc 15661 if (!fields.empty ())
c906108c 15662 {
43816ebc 15663 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15664 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15665 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15666 memcpy (TYPE_FIELDS (this_type), fields.data (),
15667 sizeof (struct field) * fields.size ());
c906108c 15668 }
c906108c 15669 }
134d01f1 15670
6c83ed52
TT
15671 /* If we are reading an enum from a .debug_types unit, and the enum
15672 is a declaration, and the enum is not the signatured type in the
15673 unit, then we do not want to add a symbol for it. Adding a
15674 symbol would in some cases obscure the true definition of the
15675 enum, giving users an incomplete type when the definition is
15676 actually available. Note that we do not want to do this for all
15677 enums which are just declarations, because C++0x allows forward
15678 enum declarations. */
3019eac3 15679 if (cu->per_cu->is_debug_types
6c83ed52
TT
15680 && die_is_declaration (die, cu))
15681 {
52dc124a 15682 struct signatured_type *sig_type;
6c83ed52 15683
c0f78cd4 15684 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15685 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15686 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15687 return;
15688 }
15689
f792889a 15690 new_symbol (die, this_type, cu);
c906108c
SS
15691}
15692
15693/* Extract all information from a DW_TAG_array_type DIE and put it in
15694 the DIE's type field. For now, this only handles one dimensional
15695 arrays. */
15696
f792889a 15697static struct type *
e7c27a73 15698read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15699{
518817b3 15700 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15701 struct die_info *child_die;
7e314c57 15702 struct type *type;
c906108c 15703 struct type *element_type, *range_type, *index_type;
c906108c 15704 struct attribute *attr;
15d034d0 15705 const char *name;
a405673c 15706 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15707 unsigned int bit_stride = 0;
c906108c 15708
e7c27a73 15709 element_type = die_type (die, cu);
c906108c 15710
7e314c57
JK
15711 /* The die_type call above may have already set the type for this DIE. */
15712 type = get_die_type (die, cu);
15713 if (type)
15714 return type;
15715
dc53a7ad
JB
15716 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15717 if (attr != NULL)
a405673c
JB
15718 {
15719 int stride_ok;
09ba997f 15720 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15721
15722 byte_stride_prop
15723 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15724 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15725 prop_type);
a405673c
JB
15726 if (!stride_ok)
15727 {
b98664d3 15728 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15729 " - DIE at %s [in module %s]"),
15730 sect_offset_str (die->sect_off),
518817b3 15731 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15732 /* Ignore this attribute. We will likely not be able to print
15733 arrays of this type correctly, but there is little we can do
15734 to help if we cannot read the attribute's value. */
15735 byte_stride_prop = NULL;
15736 }
15737 }
dc53a7ad
JB
15738
15739 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15740 if (attr != NULL)
15741 bit_stride = DW_UNSND (attr);
15742
c906108c
SS
15743 /* Irix 6.2 native cc creates array types without children for
15744 arrays with unspecified length. */
639d11d3 15745 if (die->child == NULL)
c906108c 15746 {
46bf5051 15747 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15748 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15749 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15750 byte_stride_prop, bit_stride);
f792889a 15751 return set_die_type (die, type, cu);
c906108c
SS
15752 }
15753
791afaa2 15754 std::vector<struct type *> range_types;
639d11d3 15755 child_die = die->child;
c906108c
SS
15756 while (child_die && child_die->tag)
15757 {
15758 if (child_die->tag == DW_TAG_subrange_type)
15759 {
f792889a 15760 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15761
f792889a 15762 if (child_type != NULL)
a02abb62 15763 {
0963b4bd
MS
15764 /* The range type was succesfully read. Save it for the
15765 array type creation. */
791afaa2 15766 range_types.push_back (child_type);
a02abb62 15767 }
c906108c
SS
15768 }
15769 child_die = sibling_die (child_die);
15770 }
15771
15772 /* Dwarf2 dimensions are output from left to right, create the
15773 necessary array types in backwards order. */
7ca2d3a3 15774
c906108c 15775 type = element_type;
7ca2d3a3
DL
15776
15777 if (read_array_order (die, cu) == DW_ORD_col_major)
15778 {
15779 int i = 0;
9a619af0 15780
791afaa2 15781 while (i < range_types.size ())
dc53a7ad 15782 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15783 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15784 }
15785 else
15786 {
791afaa2 15787 size_t ndim = range_types.size ();
7ca2d3a3 15788 while (ndim-- > 0)
dc53a7ad 15789 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15790 byte_stride_prop, bit_stride);
7ca2d3a3 15791 }
c906108c 15792
f5f8a009
EZ
15793 /* Understand Dwarf2 support for vector types (like they occur on
15794 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15795 array type. This is not part of the Dwarf2/3 standard yet, but a
15796 custom vendor extension. The main difference between a regular
15797 array and the vector variant is that vectors are passed by value
15798 to functions. */
e142c38c 15799 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15800 if (attr != nullptr)
ea37ba09 15801 make_vector_type (type);
f5f8a009 15802
dbc98a8b
KW
15803 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15804 implementation may choose to implement triple vectors using this
15805 attribute. */
15806 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15807 if (attr != nullptr)
dbc98a8b
KW
15808 {
15809 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15810 TYPE_LENGTH (type) = DW_UNSND (attr);
15811 else
b98664d3 15812 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15813 "than the total size of elements"));
dbc98a8b
KW
15814 }
15815
39cbfefa
DJ
15816 name = dwarf2_name (die, cu);
15817 if (name)
15818 TYPE_NAME (type) = name;
6e70227d 15819
2b4424c3
TT
15820 maybe_set_alignment (cu, die, type);
15821
0963b4bd 15822 /* Install the type in the die. */
7e314c57
JK
15823 set_die_type (die, type, cu);
15824
15825 /* set_die_type should be already done. */
b4ba55a1
JB
15826 set_descriptive_type (type, die, cu);
15827
7e314c57 15828 return type;
c906108c
SS
15829}
15830
7ca2d3a3 15831static enum dwarf_array_dim_ordering
6e70227d 15832read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15833{
15834 struct attribute *attr;
15835
15836 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15837
435d3d88 15838 if (attr != nullptr)
aead7601 15839 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15840
0963b4bd
MS
15841 /* GNU F77 is a special case, as at 08/2004 array type info is the
15842 opposite order to the dwarf2 specification, but data is still
15843 laid out as per normal fortran.
7ca2d3a3 15844
0963b4bd
MS
15845 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15846 version checking. */
7ca2d3a3 15847
905e0470
PM
15848 if (cu->language == language_fortran
15849 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15850 {
15851 return DW_ORD_row_major;
15852 }
15853
6e70227d 15854 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15855 {
15856 case array_column_major:
15857 return DW_ORD_col_major;
15858 case array_row_major:
15859 default:
15860 return DW_ORD_row_major;
15861 };
15862}
15863
72019c9c 15864/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15865 the DIE's type field. */
72019c9c 15866
f792889a 15867static struct type *
72019c9c
GM
15868read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15869{
7e314c57
JK
15870 struct type *domain_type, *set_type;
15871 struct attribute *attr;
f792889a 15872
7e314c57
JK
15873 domain_type = die_type (die, cu);
15874
15875 /* The die_type call above may have already set the type for this DIE. */
15876 set_type = get_die_type (die, cu);
15877 if (set_type)
15878 return set_type;
15879
15880 set_type = create_set_type (NULL, domain_type);
15881
15882 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15883 if (attr != nullptr)
d09039dd 15884 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15885
2b4424c3
TT
15886 maybe_set_alignment (cu, die, set_type);
15887
f792889a 15888 return set_die_type (die, set_type, cu);
72019c9c 15889}
7ca2d3a3 15890
0971de02
TT
15891/* A helper for read_common_block that creates a locexpr baton.
15892 SYM is the symbol which we are marking as computed.
15893 COMMON_DIE is the DIE for the common block.
15894 COMMON_LOC is the location expression attribute for the common
15895 block itself.
15896 MEMBER_LOC is the location expression attribute for the particular
15897 member of the common block that we are processing.
15898 CU is the CU from which the above come. */
15899
15900static void
15901mark_common_block_symbol_computed (struct symbol *sym,
15902 struct die_info *common_die,
15903 struct attribute *common_loc,
15904 struct attribute *member_loc,
15905 struct dwarf2_cu *cu)
15906{
518817b3
SM
15907 struct dwarf2_per_objfile *dwarf2_per_objfile
15908 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15909 struct objfile *objfile = dwarf2_per_objfile->objfile;
15910 struct dwarf2_locexpr_baton *baton;
15911 gdb_byte *ptr;
15912 unsigned int cu_off;
15913 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15914 LONGEST offset = 0;
15915
15916 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15917 gdb_assert (common_loc->form_is_block ());
15918 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15919 || member_loc->form_is_constant ());
0971de02 15920
8d749320 15921 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15922 baton->per_cu = cu->per_cu;
15923 gdb_assert (baton->per_cu);
15924
15925 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15926
cd6c91b4 15927 if (member_loc->form_is_constant ())
0971de02
TT
15928 {
15929 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15930 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15931 }
15932 else
15933 baton->size += DW_BLOCK (member_loc)->size;
15934
224c3ddb 15935 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15936 baton->data = ptr;
15937
15938 *ptr++ = DW_OP_call4;
9c541725 15939 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15940 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15941 ptr += 4;
15942
cd6c91b4 15943 if (member_loc->form_is_constant ())
0971de02
TT
15944 {
15945 *ptr++ = DW_OP_addr;
15946 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15947 ptr += cu->header.addr_size;
15948 }
15949 else
15950 {
15951 /* We have to copy the data here, because DW_OP_call4 will only
15952 use a DW_AT_location attribute. */
15953 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15954 ptr += DW_BLOCK (member_loc)->size;
15955 }
15956
15957 *ptr++ = DW_OP_plus;
15958 gdb_assert (ptr - baton->data == baton->size);
15959
0971de02 15960 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15961 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15962}
15963
4357ac6c
TT
15964/* Create appropriate locally-scoped variables for all the
15965 DW_TAG_common_block entries. Also create a struct common_block
15966 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 15967 is used to separate the common blocks name namespace from regular
4357ac6c 15968 variable names. */
c906108c
SS
15969
15970static void
e7c27a73 15971read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15972{
0971de02
TT
15973 struct attribute *attr;
15974
15975 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 15976 if (attr != nullptr)
0971de02
TT
15977 {
15978 /* Support the .debug_loc offsets. */
4fc6c0d5 15979 if (attr->form_is_block ())
0971de02
TT
15980 {
15981 /* Ok. */
15982 }
cd6c91b4 15983 else if (attr->form_is_section_offset ())
0971de02
TT
15984 {
15985 dwarf2_complex_location_expr_complaint ();
15986 attr = NULL;
15987 }
15988 else
15989 {
15990 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15991 "common block member");
15992 attr = NULL;
15993 }
15994 }
15995
639d11d3 15996 if (die->child != NULL)
c906108c 15997 {
518817b3 15998 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
15999 struct die_info *child_die;
16000 size_t n_entries = 0, size;
16001 struct common_block *common_block;
16002 struct symbol *sym;
74ac6d43 16003
4357ac6c
TT
16004 for (child_die = die->child;
16005 child_die && child_die->tag;
16006 child_die = sibling_die (child_die))
16007 ++n_entries;
16008
16009 size = (sizeof (struct common_block)
16010 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16011 common_block
16012 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16013 size);
4357ac6c
TT
16014 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16015 common_block->n_entries = 0;
16016
16017 for (child_die = die->child;
16018 child_die && child_die->tag;
16019 child_die = sibling_die (child_die))
16020 {
16021 /* Create the symbol in the DW_TAG_common_block block in the current
16022 symbol scope. */
e7c27a73 16023 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16024 if (sym != NULL)
16025 {
16026 struct attribute *member_loc;
16027
16028 common_block->contents[common_block->n_entries++] = sym;
16029
16030 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16031 cu);
16032 if (member_loc)
16033 {
16034 /* GDB has handled this for a long time, but it is
16035 not specified by DWARF. It seems to have been
16036 emitted by gfortran at least as recently as:
16037 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16038 complaint (_("Variable in common block has "
0971de02 16039 "DW_AT_data_member_location "
9d8780f0
SM
16040 "- DIE at %s [in module %s]"),
16041 sect_offset_str (child_die->sect_off),
518817b3 16042 objfile_name (objfile));
0971de02 16043
cd6c91b4 16044 if (member_loc->form_is_section_offset ())
0971de02 16045 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16046 else if (member_loc->form_is_constant ()
4fc6c0d5 16047 || member_loc->form_is_block ())
0971de02 16048 {
435d3d88 16049 if (attr != nullptr)
0971de02
TT
16050 mark_common_block_symbol_computed (sym, die, attr,
16051 member_loc, cu);
16052 }
16053 else
16054 dwarf2_complex_location_expr_complaint ();
16055 }
16056 }
c906108c 16057 }
4357ac6c
TT
16058
16059 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16060 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16061 }
16062}
16063
0114d602 16064/* Create a type for a C++ namespace. */
d9fa45fe 16065
0114d602
DJ
16066static struct type *
16067read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16068{
518817b3 16069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16070 const char *previous_prefix, *name;
9219021c 16071 int is_anonymous;
0114d602
DJ
16072 struct type *type;
16073
16074 /* For extensions, reuse the type of the original namespace. */
16075 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16076 {
16077 struct die_info *ext_die;
16078 struct dwarf2_cu *ext_cu = cu;
9a619af0 16079
0114d602
DJ
16080 ext_die = dwarf2_extension (die, &ext_cu);
16081 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16082
16083 /* EXT_CU may not be the same as CU.
02142a6c 16084 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16085 return set_die_type (die, type, cu);
16086 }
9219021c 16087
e142c38c 16088 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16089
16090 /* Now build the name of the current namespace. */
16091
0114d602
DJ
16092 previous_prefix = determine_prefix (die, cu);
16093 if (previous_prefix[0] != '\0')
16094 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16095 previous_prefix, name, 0, cu);
0114d602
DJ
16096
16097 /* Create the type. */
19f392bc 16098 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16099
60531b24 16100 return set_die_type (die, type, cu);
0114d602
DJ
16101}
16102
22cee43f 16103/* Read a namespace scope. */
0114d602
DJ
16104
16105static void
16106read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16107{
518817b3 16108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16109 int is_anonymous;
9219021c 16110
5c4e30ca
DC
16111 /* Add a symbol associated to this if we haven't seen the namespace
16112 before. Also, add a using directive if it's an anonymous
16113 namespace. */
9219021c 16114
f2f0e013 16115 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16116 {
16117 struct type *type;
16118
0114d602 16119 type = read_type_die (die, cu);
e7c27a73 16120 new_symbol (die, type, cu);
5c4e30ca 16121
e8e80198 16122 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16123 if (is_anonymous)
0114d602
DJ
16124 {
16125 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16126
eb1e02fd 16127 std::vector<const char *> excludes;
804d2729 16128 add_using_directive (using_directives (cu),
22cee43f 16129 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16130 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16131 }
5c4e30ca 16132 }
9219021c 16133
639d11d3 16134 if (die->child != NULL)
d9fa45fe 16135 {
639d11d3 16136 struct die_info *child_die = die->child;
6e70227d 16137
d9fa45fe
DC
16138 while (child_die && child_die->tag)
16139 {
e7c27a73 16140 process_die (child_die, cu);
d9fa45fe
DC
16141 child_die = sibling_die (child_die);
16142 }
16143 }
38d518c9
EZ
16144}
16145
f55ee35c
JK
16146/* Read a Fortran module as type. This DIE can be only a declaration used for
16147 imported module. Still we need that type as local Fortran "use ... only"
16148 declaration imports depend on the created type in determine_prefix. */
16149
16150static struct type *
16151read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16152{
518817b3 16153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16154 const char *module_name;
f55ee35c
JK
16155 struct type *type;
16156
16157 module_name = dwarf2_name (die, cu);
19f392bc 16158 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16159
f55ee35c
JK
16160 return set_die_type (die, type, cu);
16161}
16162
5d7cb8df
JK
16163/* Read a Fortran module. */
16164
16165static void
16166read_module (struct die_info *die, struct dwarf2_cu *cu)
16167{
16168 struct die_info *child_die = die->child;
530e8392
KB
16169 struct type *type;
16170
16171 type = read_type_die (die, cu);
16172 new_symbol (die, type, cu);
5d7cb8df 16173
5d7cb8df
JK
16174 while (child_die && child_die->tag)
16175 {
16176 process_die (child_die, cu);
16177 child_die = sibling_die (child_die);
16178 }
16179}
16180
38d518c9
EZ
16181/* Return the name of the namespace represented by DIE. Set
16182 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16183 namespace. */
16184
16185static const char *
e142c38c 16186namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16187{
16188 struct die_info *current_die;
16189 const char *name = NULL;
16190
16191 /* Loop through the extensions until we find a name. */
16192
16193 for (current_die = die;
16194 current_die != NULL;
f2f0e013 16195 current_die = dwarf2_extension (die, &cu))
38d518c9 16196 {
96553a0c
DE
16197 /* We don't use dwarf2_name here so that we can detect the absence
16198 of a name -> anonymous namespace. */
7d45c7c3 16199 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16200
38d518c9
EZ
16201 if (name != NULL)
16202 break;
16203 }
16204
16205 /* Is it an anonymous namespace? */
16206
16207 *is_anonymous = (name == NULL);
16208 if (*is_anonymous)
2b1dbab0 16209 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16210
16211 return name;
d9fa45fe
DC
16212}
16213
c906108c
SS
16214/* Extract all information from a DW_TAG_pointer_type DIE and add to
16215 the user defined type vector. */
16216
f792889a 16217static struct type *
e7c27a73 16218read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16219{
518817b3
SM
16220 struct gdbarch *gdbarch
16221 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16222 struct comp_unit_head *cu_header = &cu->header;
c906108c 16223 struct type *type;
8b2dbe47
KB
16224 struct attribute *attr_byte_size;
16225 struct attribute *attr_address_class;
16226 int byte_size, addr_class;
7e314c57
JK
16227 struct type *target_type;
16228
16229 target_type = die_type (die, cu);
c906108c 16230
7e314c57
JK
16231 /* The die_type call above may have already set the type for this DIE. */
16232 type = get_die_type (die, cu);
16233 if (type)
16234 return type;
16235
16236 type = lookup_pointer_type (target_type);
8b2dbe47 16237
e142c38c 16238 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16239 if (attr_byte_size)
16240 byte_size = DW_UNSND (attr_byte_size);
c906108c 16241 else
8b2dbe47
KB
16242 byte_size = cu_header->addr_size;
16243
e142c38c 16244 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16245 if (attr_address_class)
16246 addr_class = DW_UNSND (attr_address_class);
16247 else
16248 addr_class = DW_ADDR_none;
16249
2b4424c3
TT
16250 ULONGEST alignment = get_alignment (cu, die);
16251
16252 /* If the pointer size, alignment, or address class is different
16253 than the default, create a type variant marked as such and set
16254 the length accordingly. */
16255 if (TYPE_LENGTH (type) != byte_size
16256 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16257 && alignment != TYPE_RAW_ALIGN (type))
16258 || addr_class != DW_ADDR_none)
c906108c 16259 {
5e2b427d 16260 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16261 {
16262 int type_flags;
16263
849957d9 16264 type_flags = gdbarch_address_class_type_flags
5e2b427d 16265 (gdbarch, byte_size, addr_class);
876cecd0
TT
16266 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16267 == 0);
8b2dbe47
KB
16268 type = make_type_with_address_space (type, type_flags);
16269 }
16270 else if (TYPE_LENGTH (type) != byte_size)
16271 {
b98664d3 16272 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16273 }
2b4424c3
TT
16274 else if (TYPE_RAW_ALIGN (type) != alignment)
16275 {
b98664d3 16276 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16277 " - DIE at %s [in module %s]"),
16278 sect_offset_str (die->sect_off),
16279 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16280 }
6e70227d 16281 else
9a619af0
MS
16282 {
16283 /* Should we also complain about unhandled address classes? */
16284 }
c906108c 16285 }
8b2dbe47
KB
16286
16287 TYPE_LENGTH (type) = byte_size;
2b4424c3 16288 set_type_align (type, alignment);
f792889a 16289 return set_die_type (die, type, cu);
c906108c
SS
16290}
16291
16292/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16293 the user defined type vector. */
16294
f792889a 16295static struct type *
e7c27a73 16296read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16297{
16298 struct type *type;
16299 struct type *to_type;
16300 struct type *domain;
16301
e7c27a73
DJ
16302 to_type = die_type (die, cu);
16303 domain = die_containing_type (die, cu);
0d5de010 16304
7e314c57
JK
16305 /* The calls above may have already set the type for this DIE. */
16306 type = get_die_type (die, cu);
16307 if (type)
16308 return type;
16309
0d5de010
DJ
16310 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16311 type = lookup_methodptr_type (to_type);
7078baeb
TT
16312 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16313 {
518817b3
SM
16314 struct type *new_type
16315 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16316
16317 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16318 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16319 TYPE_VARARGS (to_type));
16320 type = lookup_methodptr_type (new_type);
16321 }
0d5de010
DJ
16322 else
16323 type = lookup_memberptr_type (to_type, domain);
c906108c 16324
f792889a 16325 return set_die_type (die, type, cu);
c906108c
SS
16326}
16327
4297a3f0 16328/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16329 the user defined type vector. */
16330
f792889a 16331static struct type *
4297a3f0
AV
16332read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16333 enum type_code refcode)
c906108c 16334{
e7c27a73 16335 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16336 struct type *type, *target_type;
c906108c
SS
16337 struct attribute *attr;
16338
4297a3f0
AV
16339 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16340
7e314c57
JK
16341 target_type = die_type (die, cu);
16342
16343 /* The die_type call above may have already set the type for this DIE. */
16344 type = get_die_type (die, cu);
16345 if (type)
16346 return type;
16347
4297a3f0 16348 type = lookup_reference_type (target_type, refcode);
e142c38c 16349 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16350 if (attr != nullptr)
c906108c
SS
16351 {
16352 TYPE_LENGTH (type) = DW_UNSND (attr);
16353 }
16354 else
16355 {
107d2387 16356 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16357 }
2b4424c3 16358 maybe_set_alignment (cu, die, type);
f792889a 16359 return set_die_type (die, type, cu);
c906108c
SS
16360}
16361
cf363f18
MW
16362/* Add the given cv-qualifiers to the element type of the array. GCC
16363 outputs DWARF type qualifiers that apply to an array, not the
16364 element type. But GDB relies on the array element type to carry
16365 the cv-qualifiers. This mimics section 6.7.3 of the C99
16366 specification. */
16367
16368static struct type *
16369add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16370 struct type *base_type, int cnst, int voltl)
16371{
16372 struct type *el_type, *inner_array;
16373
16374 base_type = copy_type (base_type);
16375 inner_array = base_type;
16376
16377 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16378 {
16379 TYPE_TARGET_TYPE (inner_array) =
16380 copy_type (TYPE_TARGET_TYPE (inner_array));
16381 inner_array = TYPE_TARGET_TYPE (inner_array);
16382 }
16383
16384 el_type = TYPE_TARGET_TYPE (inner_array);
16385 cnst |= TYPE_CONST (el_type);
16386 voltl |= TYPE_VOLATILE (el_type);
16387 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16388
16389 return set_die_type (die, base_type, cu);
16390}
16391
f792889a 16392static struct type *
e7c27a73 16393read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16394{
f792889a 16395 struct type *base_type, *cv_type;
c906108c 16396
e7c27a73 16397 base_type = die_type (die, cu);
7e314c57
JK
16398
16399 /* The die_type call above may have already set the type for this DIE. */
16400 cv_type = get_die_type (die, cu);
16401 if (cv_type)
16402 return cv_type;
16403
2f608a3a
KW
16404 /* In case the const qualifier is applied to an array type, the element type
16405 is so qualified, not the array type (section 6.7.3 of C99). */
16406 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16407 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16408
f792889a
DJ
16409 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16410 return set_die_type (die, cv_type, cu);
c906108c
SS
16411}
16412
f792889a 16413static struct type *
e7c27a73 16414read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16415{
f792889a 16416 struct type *base_type, *cv_type;
c906108c 16417
e7c27a73 16418 base_type = die_type (die, cu);
7e314c57
JK
16419
16420 /* The die_type call above may have already set the type for this DIE. */
16421 cv_type = get_die_type (die, cu);
16422 if (cv_type)
16423 return cv_type;
16424
cf363f18
MW
16425 /* In case the volatile qualifier is applied to an array type, the
16426 element type is so qualified, not the array type (section 6.7.3
16427 of C99). */
16428 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16429 return add_array_cv_type (die, cu, base_type, 0, 1);
16430
f792889a
DJ
16431 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16432 return set_die_type (die, cv_type, cu);
c906108c
SS
16433}
16434
06d66ee9
TT
16435/* Handle DW_TAG_restrict_type. */
16436
16437static struct type *
16438read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16439{
16440 struct type *base_type, *cv_type;
16441
16442 base_type = die_type (die, cu);
16443
16444 /* The die_type call above may have already set the type for this DIE. */
16445 cv_type = get_die_type (die, cu);
16446 if (cv_type)
16447 return cv_type;
16448
16449 cv_type = make_restrict_type (base_type);
16450 return set_die_type (die, cv_type, cu);
16451}
16452
a2c2acaf
MW
16453/* Handle DW_TAG_atomic_type. */
16454
16455static struct type *
16456read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16457{
16458 struct type *base_type, *cv_type;
16459
16460 base_type = die_type (die, cu);
16461
16462 /* The die_type call above may have already set the type for this DIE. */
16463 cv_type = get_die_type (die, cu);
16464 if (cv_type)
16465 return cv_type;
16466
16467 cv_type = make_atomic_type (base_type);
16468 return set_die_type (die, cv_type, cu);
16469}
16470
c906108c
SS
16471/* Extract all information from a DW_TAG_string_type DIE and add to
16472 the user defined type vector. It isn't really a user defined type,
16473 but it behaves like one, with other DIE's using an AT_user_def_type
16474 attribute to reference it. */
16475
f792889a 16476static struct type *
e7c27a73 16477read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16478{
518817b3 16479 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16480 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16481 struct type *type, *range_type, *index_type, *char_type;
16482 struct attribute *attr;
216a7e6b
AB
16483 struct dynamic_prop prop;
16484 bool length_is_constant = true;
16485 LONGEST length;
16486
16487 /* There are a couple of places where bit sizes might be made use of
16488 when parsing a DW_TAG_string_type, however, no producer that we know
16489 of make use of these. Handling bit sizes that are a multiple of the
16490 byte size is easy enough, but what about other bit sizes? Lets deal
16491 with that problem when we have to. Warn about these attributes being
16492 unsupported, then parse the type and ignore them like we always
16493 have. */
16494 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16495 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16496 {
16497 static bool warning_printed = false;
16498 if (!warning_printed)
16499 {
16500 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16501 "currently supported on DW_TAG_string_type."));
16502 warning_printed = true;
16503 }
16504 }
c906108c 16505
e142c38c 16506 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16507 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16508 {
16509 /* The string length describes the location at which the length of
16510 the string can be found. The size of the length field can be
16511 specified with one of the attributes below. */
16512 struct type *prop_type;
16513 struct attribute *len
16514 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16515 if (len == nullptr)
16516 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16517 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16518 {
16519 /* Pass 0 as the default as we know this attribute is constant
16520 and the default value will not be returned. */
16521 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
09ba997f 16522 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16523 }
16524 else
16525 {
16526 /* If the size is not specified then we assume it is the size of
16527 an address on this target. */
09ba997f 16528 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16529 }
16530
16531 /* Convert the attribute into a dynamic property. */
16532 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16533 length = 1;
16534 else
16535 length_is_constant = false;
16536 }
16537 else if (attr != nullptr)
16538 {
16539 /* This DW_AT_string_length just contains the length with no
16540 indirection. There's no need to create a dynamic property in this
16541 case. Pass 0 for the default value as we know it will not be
16542 returned in this case. */
16543 length = dwarf2_get_attr_constant_value (attr, 0);
16544 }
16545 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16546 {
216a7e6b
AB
16547 /* We don't currently support non-constant byte sizes for strings. */
16548 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
16549 }
16550 else
16551 {
216a7e6b
AB
16552 /* Use 1 as a fallback length if we have nothing else. */
16553 length = 1;
c906108c 16554 }
6ccb9162 16555
46bf5051 16556 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16557 if (length_is_constant)
16558 range_type = create_static_range_type (NULL, index_type, 1, length);
16559 else
16560 {
16561 struct dynamic_prop low_bound;
16562
16563 low_bound.kind = PROP_CONST;
16564 low_bound.data.const_val = 1;
16565 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16566 }
3b7538c0
UW
16567 char_type = language_string_char_type (cu->language_defn, gdbarch);
16568 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16569
f792889a 16570 return set_die_type (die, type, cu);
c906108c
SS
16571}
16572
4d804846
JB
16573/* Assuming that DIE corresponds to a function, returns nonzero
16574 if the function is prototyped. */
16575
16576static int
16577prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16578{
16579 struct attribute *attr;
16580
16581 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16582 if (attr && (DW_UNSND (attr) != 0))
16583 return 1;
16584
16585 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16586 is only meaningful for C, but the concept also extends to other
4d804846
JB
16587 languages that allow unprototyped functions (Eg: Objective C).
16588 For all other languages, assume that functions are always
16589 prototyped. */
16590 if (cu->language != language_c
16591 && cu->language != language_objc
16592 && cu->language != language_opencl)
16593 return 1;
16594
16595 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16596 prototyped and unprototyped functions; default to prototyped,
16597 since that is more common in modern code (and RealView warns
16598 about unprototyped functions). */
16599 if (producer_is_realview (cu->producer))
16600 return 1;
16601
16602 return 0;
16603}
16604
c906108c
SS
16605/* Handle DIES due to C code like:
16606
16607 struct foo
c5aa993b
JM
16608 {
16609 int (*funcp)(int a, long l);
16610 int b;
16611 };
c906108c 16612
0963b4bd 16613 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16614
f792889a 16615static struct type *
e7c27a73 16616read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16617{
518817b3 16618 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16619 struct type *type; /* Type that this function returns. */
16620 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16621 struct attribute *attr;
16622
e7c27a73 16623 type = die_type (die, cu);
7e314c57
JK
16624
16625 /* The die_type call above may have already set the type for this DIE. */
16626 ftype = get_die_type (die, cu);
16627 if (ftype)
16628 return ftype;
16629
0c8b41f1 16630 ftype = lookup_function_type (type);
c906108c 16631
4d804846 16632 if (prototyped_function_p (die, cu))
a6c727b2 16633 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16634
c055b101
CV
16635 /* Store the calling convention in the type if it's available in
16636 the subroutine die. Otherwise set the calling convention to
16637 the default value DW_CC_normal. */
16638 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16639 if (attr != nullptr
16640 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16641 TYPE_CALLING_CONVENTION (ftype)
16642 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16643 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16644 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16645 else
16646 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16647
743649fd
MW
16648 /* Record whether the function returns normally to its caller or not
16649 if the DWARF producer set that information. */
16650 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16651 if (attr && (DW_UNSND (attr) != 0))
16652 TYPE_NO_RETURN (ftype) = 1;
16653
76c10ea2
GM
16654 /* We need to add the subroutine type to the die immediately so
16655 we don't infinitely recurse when dealing with parameters
0963b4bd 16656 declared as the same subroutine type. */
76c10ea2 16657 set_die_type (die, ftype, cu);
6e70227d 16658
639d11d3 16659 if (die->child != NULL)
c906108c 16660 {
bb5ed363 16661 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16662 struct die_info *child_die;
8072405b 16663 int nparams, iparams;
c906108c
SS
16664
16665 /* Count the number of parameters.
16666 FIXME: GDB currently ignores vararg functions, but knows about
16667 vararg member functions. */
8072405b 16668 nparams = 0;
639d11d3 16669 child_die = die->child;
c906108c
SS
16670 while (child_die && child_die->tag)
16671 {
16672 if (child_die->tag == DW_TAG_formal_parameter)
16673 nparams++;
16674 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16675 TYPE_VARARGS (ftype) = 1;
c906108c
SS
16676 child_die = sibling_die (child_die);
16677 }
16678
16679 /* Allocate storage for parameters and fill them in. */
16680 TYPE_NFIELDS (ftype) = nparams;
16681 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16682 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16683
8072405b
JK
16684 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16685 even if we error out during the parameters reading below. */
16686 for (iparams = 0; iparams < nparams; iparams++)
16687 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16688
16689 iparams = 0;
639d11d3 16690 child_die = die->child;
c906108c
SS
16691 while (child_die && child_die->tag)
16692 {
16693 if (child_die->tag == DW_TAG_formal_parameter)
16694 {
3ce3b1ba
PA
16695 struct type *arg_type;
16696
16697 /* DWARF version 2 has no clean way to discern C++
16698 static and non-static member functions. G++ helps
16699 GDB by marking the first parameter for non-static
16700 member functions (which is the this pointer) as
16701 artificial. We pass this information to
16702 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16703
16704 DWARF version 3 added DW_AT_object_pointer, which GCC
16705 4.5 does not yet generate. */
e142c38c 16706 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16707 if (attr != nullptr)
c906108c
SS
16708 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16709 else
9c37b5ae 16710 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16711 arg_type = die_type (child_die, cu);
16712
16713 /* RealView does not mark THIS as const, which the testsuite
16714 expects. GCC marks THIS as const in method definitions,
16715 but not in the class specifications (GCC PR 43053). */
16716 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16717 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16718 {
16719 int is_this = 0;
16720 struct dwarf2_cu *arg_cu = cu;
16721 const char *name = dwarf2_name (child_die, cu);
16722
16723 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16724 if (attr != nullptr)
3ce3b1ba
PA
16725 {
16726 /* If the compiler emits this, use it. */
16727 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16728 is_this = 1;
16729 }
16730 else if (name && strcmp (name, "this") == 0)
16731 /* Function definitions will have the argument names. */
16732 is_this = 1;
16733 else if (name == NULL && iparams == 0)
16734 /* Declarations may not have the names, so like
16735 elsewhere in GDB, assume an artificial first
16736 argument is "this". */
16737 is_this = 1;
16738
16739 if (is_this)
16740 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16741 arg_type, 0);
16742 }
16743
16744 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16745 iparams++;
16746 }
16747 child_die = sibling_die (child_die);
16748 }
16749 }
16750
76c10ea2 16751 return ftype;
c906108c
SS
16752}
16753
f792889a 16754static struct type *
e7c27a73 16755read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16756{
518817b3 16757 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16758 const char *name = NULL;
3c8e0968 16759 struct type *this_type, *target_type;
c906108c 16760
94af9270 16761 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16762 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16763 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16764 set_die_type (die, this_type, cu);
3c8e0968
DE
16765 target_type = die_type (die, cu);
16766 if (target_type != this_type)
16767 TYPE_TARGET_TYPE (this_type) = target_type;
16768 else
16769 {
16770 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16771 spec and cause infinite loops in GDB. */
b98664d3 16772 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16773 "- DIE at %s [in module %s]"),
16774 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16775 TYPE_TARGET_TYPE (this_type) = NULL;
16776 }
f792889a 16777 return this_type;
c906108c
SS
16778}
16779
9b790ce7
UW
16780/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16781 (which may be different from NAME) to the architecture back-end to allow
16782 it to guess the correct format if necessary. */
16783
16784static struct type *
16785dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16786 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16787{
16788 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16789 const struct floatformat **format;
16790 struct type *type;
16791
16792 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16793 if (format)
103a685e 16794 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16795 else
77b7c781 16796 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16797
16798 return type;
16799}
16800
eb77c9df
AB
16801/* Allocate an integer type of size BITS and name NAME. */
16802
16803static struct type *
16804dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16805 int bits, int unsigned_p, const char *name)
16806{
16807 struct type *type;
16808
16809 /* Versions of Intel's C Compiler generate an integer type called "void"
16810 instead of using DW_TAG_unspecified_type. This has been seen on
16811 at least versions 14, 17, and 18. */
35ee2dc2
AB
16812 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16813 && strcmp (name, "void") == 0)
eb77c9df
AB
16814 type = objfile_type (objfile)->builtin_void;
16815 else
16816 type = init_integer_type (objfile, bits, unsigned_p, name);
16817
16818 return type;
16819}
16820
8bdc1658
AB
16821/* Initialise and return a floating point type of size BITS suitable for
16822 use as a component of a complex number. The NAME_HINT is passed through
16823 when initialising the floating point type and is the name of the complex
16824 type.
16825
16826 As DWARF doesn't currently provide an explicit name for the components
16827 of a complex number, but it can be helpful to have these components
16828 named, we try to select a suitable name based on the size of the
16829 component. */
16830static struct type *
16831dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16832 struct objfile *objfile,
103a685e
TT
16833 int bits, const char *name_hint,
16834 enum bfd_endian byte_order)
8bdc1658
AB
16835{
16836 gdbarch *gdbarch = get_objfile_arch (objfile);
16837 struct type *tt = nullptr;
16838
35add35e
AB
16839 /* Try to find a suitable floating point builtin type of size BITS.
16840 We're going to use the name of this type as the name for the complex
16841 target type that we are about to create. */
1db455a7 16842 switch (cu->language)
8bdc1658 16843 {
1db455a7
AB
16844 case language_fortran:
16845 switch (bits)
16846 {
16847 case 32:
16848 tt = builtin_f_type (gdbarch)->builtin_real;
16849 break;
16850 case 64:
16851 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16852 break;
16853 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16854 case 128:
16855 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16856 break;
16857 }
8bdc1658 16858 break;
1db455a7
AB
16859 default:
16860 switch (bits)
16861 {
16862 case 32:
16863 tt = builtin_type (gdbarch)->builtin_float;
16864 break;
16865 case 64:
16866 tt = builtin_type (gdbarch)->builtin_double;
16867 break;
16868 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16869 case 128:
16870 tt = builtin_type (gdbarch)->builtin_long_double;
16871 break;
16872 }
8bdc1658
AB
16873 break;
16874 }
16875
35add35e
AB
16876 /* If the type we found doesn't match the size we were looking for, then
16877 pretend we didn't find a type at all, the complex target type we
16878 create will then be nameless. */
a12e5744 16879 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16880 tt = nullptr;
16881
8bdc1658 16882 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16883 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16884}
16885
c906108c
SS
16886/* Find a representation of a given base type and install
16887 it in the TYPE field of the die. */
16888
f792889a 16889static struct type *
e7c27a73 16890read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16891{
518817b3 16892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16893 struct type *type;
16894 struct attribute *attr;
19f392bc 16895 int encoding = 0, bits = 0;
15d034d0 16896 const char *name;
34877895 16897 gdbarch *arch;
c906108c 16898
e142c38c 16899 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16900 if (attr != nullptr)
34877895 16901 encoding = DW_UNSND (attr);
e142c38c 16902 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16903 if (attr != nullptr)
34877895 16904 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16905 name = dwarf2_name (die, cu);
6ccb9162 16906 if (!name)
34877895 16907 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16908
16909 arch = get_objfile_arch (objfile);
16910 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16911
34877895
PJ
16912 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16913 if (attr)
103a685e
TT
16914 {
16915 int endianity = DW_UNSND (attr);
16916
16917 switch (endianity)
16918 {
16919 case DW_END_big:
16920 byte_order = BFD_ENDIAN_BIG;
16921 break;
16922 case DW_END_little:
16923 byte_order = BFD_ENDIAN_LITTLE;
16924 break;
16925 default:
16926 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16927 break;
16928 }
16929 }
6ccb9162
UW
16930
16931 switch (encoding)
c906108c 16932 {
6ccb9162
UW
16933 case DW_ATE_address:
16934 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16935 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16936 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16937 break;
16938 case DW_ATE_boolean:
19f392bc 16939 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16940 break;
16941 case DW_ATE_complex_float:
103a685e
TT
16942 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16943 byte_order);
19f392bc 16944 type = init_complex_type (objfile, name, type);
6ccb9162
UW
16945 break;
16946 case DW_ATE_decimal_float:
19f392bc 16947 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16948 break;
16949 case DW_ATE_float:
103a685e 16950 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
16951 break;
16952 case DW_ATE_signed:
eb77c9df 16953 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16954 break;
16955 case DW_ATE_unsigned:
3b2b8fea
TT
16956 if (cu->language == language_fortran
16957 && name
61012eef 16958 && startswith (name, "character("))
19f392bc
UW
16959 type = init_character_type (objfile, bits, 1, name);
16960 else
eb77c9df 16961 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
16962 break;
16963 case DW_ATE_signed_char:
6e70227d 16964 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16965 || cu->language == language_pascal
16966 || cu->language == language_fortran)
19f392bc
UW
16967 type = init_character_type (objfile, bits, 0, name);
16968 else
eb77c9df 16969 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16970 break;
16971 case DW_ATE_unsigned_char:
868a0084 16972 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16973 || cu->language == language_pascal
c44af4eb
TT
16974 || cu->language == language_fortran
16975 || cu->language == language_rust)
19f392bc
UW
16976 type = init_character_type (objfile, bits, 1, name);
16977 else
eb77c9df 16978 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 16979 break;
75079b2b 16980 case DW_ATE_UTF:
53e710ac 16981 {
53e710ac
PA
16982 if (bits == 16)
16983 type = builtin_type (arch)->builtin_char16;
16984 else if (bits == 32)
16985 type = builtin_type (arch)->builtin_char32;
16986 else
16987 {
b98664d3 16988 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 16989 bits);
eb77c9df 16990 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
16991 }
16992 return set_die_type (die, type, cu);
16993 }
75079b2b
TT
16994 break;
16995
6ccb9162 16996 default:
b98664d3 16997 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 16998 dwarf_type_encoding_name (encoding));
77b7c781 16999 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17000 break;
c906108c 17001 }
6ccb9162 17002
0114d602 17003 if (name && strcmp (name, "char") == 0)
876cecd0 17004 TYPE_NOSIGN (type) = 1;
0114d602 17005
2b4424c3
TT
17006 maybe_set_alignment (cu, die, type);
17007
103a685e 17008 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17009
f792889a 17010 return set_die_type (die, type, cu);
c906108c
SS
17011}
17012
80180f79
SA
17013/* Parse dwarf attribute if it's a block, reference or constant and put the
17014 resulting value of the attribute into struct bound_prop.
17015 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17016
17017static int
17018attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17019 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17020 struct type *default_type)
80180f79
SA
17021{
17022 struct dwarf2_property_baton *baton;
518817b3
SM
17023 struct obstack *obstack
17024 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17025
9a49df9d
AB
17026 gdb_assert (default_type != NULL);
17027
80180f79
SA
17028 if (attr == NULL || prop == NULL)
17029 return 0;
17030
4fc6c0d5 17031 if (attr->form_is_block ())
80180f79 17032 {
8d749320 17033 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17034 baton->property_type = default_type;
80180f79
SA
17035 baton->locexpr.per_cu = cu->per_cu;
17036 baton->locexpr.size = DW_BLOCK (attr)->size;
17037 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17038 switch (attr->name)
17039 {
17040 case DW_AT_string_length:
17041 baton->locexpr.is_reference = true;
17042 break;
17043 default:
17044 baton->locexpr.is_reference = false;
17045 break;
17046 }
80180f79
SA
17047 prop->data.baton = baton;
17048 prop->kind = PROP_LOCEXPR;
17049 gdb_assert (prop->data.baton != NULL);
17050 }
cd6c91b4 17051 else if (attr->form_is_ref ())
80180f79
SA
17052 {
17053 struct dwarf2_cu *target_cu = cu;
17054 struct die_info *target_die;
17055 struct attribute *target_attr;
17056
17057 target_die = follow_die_ref (die, attr, &target_cu);
17058 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17059 if (target_attr == NULL)
17060 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17061 target_cu);
80180f79
SA
17062 if (target_attr == NULL)
17063 return 0;
17064
df25ebbd 17065 switch (target_attr->name)
80180f79 17066 {
df25ebbd 17067 case DW_AT_location:
cd6c91b4 17068 if (target_attr->form_is_section_offset ())
df25ebbd 17069 {
8d749320 17070 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17071 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17072 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17073 prop->data.baton = baton;
17074 prop->kind = PROP_LOCLIST;
17075 gdb_assert (prop->data.baton != NULL);
17076 }
4fc6c0d5 17077 else if (target_attr->form_is_block ())
df25ebbd 17078 {
8d749320 17079 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17080 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17081 baton->locexpr.per_cu = cu->per_cu;
17082 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17083 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17084 baton->locexpr.is_reference = true;
df25ebbd
JB
17085 prop->data.baton = baton;
17086 prop->kind = PROP_LOCEXPR;
17087 gdb_assert (prop->data.baton != NULL);
17088 }
17089 else
17090 {
17091 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17092 "dynamic property");
17093 return 0;
17094 }
17095 break;
17096 case DW_AT_data_member_location:
17097 {
17098 LONGEST offset;
17099
17100 if (!handle_data_member_location (target_die, target_cu,
17101 &offset))
17102 return 0;
17103
8d749320 17104 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17105 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17106 target_cu);
df25ebbd
JB
17107 baton->offset_info.offset = offset;
17108 baton->offset_info.type = die_type (target_die, target_cu);
17109 prop->data.baton = baton;
17110 prop->kind = PROP_ADDR_OFFSET;
17111 break;
17112 }
80180f79
SA
17113 }
17114 }
cd6c91b4 17115 else if (attr->form_is_constant ())
80180f79
SA
17116 {
17117 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17118 prop->kind = PROP_CONST;
17119 }
17120 else
17121 {
17122 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17123 dwarf2_name (die, cu));
17124 return 0;
17125 }
17126
17127 return 1;
17128}
17129
09ba997f 17130/* See read.h. */
9a49df9d 17131
09ba997f
TT
17132struct type *
17133dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17134{
09ba997f 17135 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17136 struct type *int_type;
17137
17138 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17139#define TRY_TYPE(F) \
17140 int_type = (unsigned_p \
17141 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17142 : objfile_type (objfile)->builtin_ ## F); \
17143 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17144 return int_type
17145
17146 TRY_TYPE (char);
17147 TRY_TYPE (short);
17148 TRY_TYPE (int);
17149 TRY_TYPE (long);
17150 TRY_TYPE (long_long);
17151
17152#undef TRY_TYPE
17153
17154 gdb_assert_not_reached ("unable to find suitable integer type");
17155}
17156
09ba997f 17157/* See read.h. */
11a8b164 17158
09ba997f
TT
17159struct type *
17160dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17161{
09ba997f
TT
17162 int addr_size = this->addr_size ();
17163 return int_type (addr_size, unsigned_p);
11a8b164
AB
17164}
17165
b86352cf
AB
17166/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17167 present (which is valid) then compute the default type based on the
17168 compilation units address size. */
17169
17170static struct type *
17171read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17172{
17173 struct type *index_type = die_type (die, cu);
17174
17175 /* Dwarf-2 specifications explicitly allows to create subrange types
17176 without specifying a base type.
17177 In that case, the base type must be set to the type of
17178 the lower bound, upper bound or count, in that order, if any of these
17179 three attributes references an object that has a type.
17180 If no base type is found, the Dwarf-2 specifications say that
17181 a signed integer type of size equal to the size of an address should
17182 be used.
17183 For the following C code: `extern char gdb_int [];'
17184 GCC produces an empty range DIE.
17185 FIXME: muller/2010-05-28: Possible references to object for low bound,
17186 high bound or count are not yet handled by this code. */
17187 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17188 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17189
17190 return index_type;
17191}
17192
a02abb62
JB
17193/* Read the given DW_AT_subrange DIE. */
17194
f792889a 17195static struct type *
a02abb62
JB
17196read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17197{
4c9ad8c2 17198 struct type *base_type, *orig_base_type;
a02abb62
JB
17199 struct type *range_type;
17200 struct attribute *attr;
729efb13 17201 struct dynamic_prop low, high;
4fae6e18 17202 int low_default_is_valid;
c451ebe5 17203 int high_bound_is_count = 0;
15d034d0 17204 const char *name;
d359392f 17205 ULONGEST negative_mask;
e77813c8 17206
b86352cf
AB
17207 orig_base_type = read_subrange_index_type (die, cu);
17208
4c9ad8c2
TT
17209 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17210 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17211 creating the range type, but we use the result of check_typedef
17212 when examining properties of the type. */
17213 base_type = check_typedef (orig_base_type);
a02abb62 17214
7e314c57
JK
17215 /* The die_type call above may have already set the type for this DIE. */
17216 range_type = get_die_type (die, cu);
17217 if (range_type)
17218 return range_type;
17219
729efb13
SA
17220 low.kind = PROP_CONST;
17221 high.kind = PROP_CONST;
17222 high.data.const_val = 0;
17223
4fae6e18
JK
17224 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17225 omitting DW_AT_lower_bound. */
17226 switch (cu->language)
6e70227d 17227 {
4fae6e18
JK
17228 case language_c:
17229 case language_cplus:
729efb13 17230 low.data.const_val = 0;
4fae6e18
JK
17231 low_default_is_valid = 1;
17232 break;
17233 case language_fortran:
729efb13 17234 low.data.const_val = 1;
4fae6e18
JK
17235 low_default_is_valid = 1;
17236 break;
17237 case language_d:
4fae6e18 17238 case language_objc:
c44af4eb 17239 case language_rust:
729efb13 17240 low.data.const_val = 0;
4fae6e18
JK
17241 low_default_is_valid = (cu->header.version >= 4);
17242 break;
17243 case language_ada:
17244 case language_m2:
17245 case language_pascal:
729efb13 17246 low.data.const_val = 1;
4fae6e18
JK
17247 low_default_is_valid = (cu->header.version >= 4);
17248 break;
17249 default:
729efb13 17250 low.data.const_val = 0;
4fae6e18
JK
17251 low_default_is_valid = 0;
17252 break;
a02abb62
JB
17253 }
17254
e142c38c 17255 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17256 if (attr != nullptr)
9a49df9d 17257 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17258 else if (!low_default_is_valid)
b98664d3 17259 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17260 "- DIE at %s [in module %s]"),
17261 sect_offset_str (die->sect_off),
518817b3 17262 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17263
506f5c41
TV
17264 struct attribute *attr_ub, *attr_count;
17265 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17266 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17267 {
506f5c41 17268 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17269 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17270 {
c451ebe5
SA
17271 /* If bounds are constant do the final calculation here. */
17272 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17273 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17274 else
17275 high_bound_is_count = 1;
c2ff108b 17276 }
506f5c41
TV
17277 else
17278 {
17279 if (attr_ub != NULL)
17280 complaint (_("Unresolved DW_AT_upper_bound "
17281 "- DIE at %s [in module %s]"),
17282 sect_offset_str (die->sect_off),
17283 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17284 if (attr_count != NULL)
17285 complaint (_("Unresolved DW_AT_count "
17286 "- DIE at %s [in module %s]"),
17287 sect_offset_str (die->sect_off),
17288 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17289 }
e77813c8 17290 }
a02abb62 17291
4e962e74
TT
17292 LONGEST bias = 0;
17293 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17294 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17295 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17296
dbb9c2b1
JB
17297 /* Normally, the DWARF producers are expected to use a signed
17298 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17299 But this is unfortunately not always the case, as witnessed
17300 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17301 is used instead. To work around that ambiguity, we treat
17302 the bounds as signed, and thus sign-extend their values, when
17303 the base type is signed. */
6e70227d 17304 negative_mask =
d359392f 17305 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17306 if (low.kind == PROP_CONST
17307 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17308 low.data.const_val |= negative_mask;
17309 if (high.kind == PROP_CONST
17310 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17311 high.data.const_val |= negative_mask;
43bbcdc2 17312
5bbd8269
AB
17313 /* Check for bit and byte strides. */
17314 struct dynamic_prop byte_stride_prop;
17315 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17316 if (attr_byte_stride != nullptr)
17317 {
09ba997f 17318 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17319 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17320 prop_type);
17321 }
17322
17323 struct dynamic_prop bit_stride_prop;
17324 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17325 if (attr_bit_stride != nullptr)
17326 {
17327 /* It only makes sense to have either a bit or byte stride. */
17328 if (attr_byte_stride != nullptr)
17329 {
17330 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17331 "- DIE at %s [in module %s]"),
17332 sect_offset_str (die->sect_off),
17333 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17334 attr_bit_stride = nullptr;
17335 }
17336 else
17337 {
09ba997f 17338 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17339 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17340 prop_type);
17341 }
17342 }
17343
17344 if (attr_byte_stride != nullptr
17345 || attr_bit_stride != nullptr)
17346 {
17347 bool byte_stride_p = (attr_byte_stride != nullptr);
17348 struct dynamic_prop *stride
17349 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17350
17351 range_type
17352 = create_range_type_with_stride (NULL, orig_base_type, &low,
17353 &high, bias, stride, byte_stride_p);
17354 }
17355 else
17356 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17357
c451ebe5
SA
17358 if (high_bound_is_count)
17359 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17360
c2ff108b
JK
17361 /* Ada expects an empty array on no boundary attributes. */
17362 if (attr == NULL && cu->language != language_ada)
729efb13 17363 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17364
39cbfefa
DJ
17365 name = dwarf2_name (die, cu);
17366 if (name)
17367 TYPE_NAME (range_type) = name;
6e70227d 17368
e142c38c 17369 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17370 if (attr != nullptr)
a02abb62
JB
17371 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17372
2b4424c3
TT
17373 maybe_set_alignment (cu, die, range_type);
17374
7e314c57
JK
17375 set_die_type (die, range_type, cu);
17376
17377 /* set_die_type should be already done. */
b4ba55a1
JB
17378 set_descriptive_type (range_type, die, cu);
17379
7e314c57 17380 return range_type;
a02abb62 17381}
6e70227d 17382
f792889a 17383static struct type *
81a17f79
JB
17384read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17385{
17386 struct type *type;
81a17f79 17387
518817b3
SM
17388 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17389 NULL);
0114d602 17390 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17391
74a2f8ff 17392 /* In Ada, an unspecified type is typically used when the description
85102364 17393 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17394 such a type, we treat it as a stub, and try to resolve it later on,
17395 when needed. */
17396 if (cu->language == language_ada)
17397 TYPE_STUB (type) = 1;
17398
f792889a 17399 return set_die_type (die, type, cu);
81a17f79 17400}
a02abb62 17401
639d11d3
DC
17402/* Read a single die and all its descendents. Set the die's sibling
17403 field to NULL; set other fields in the die correctly, and set all
17404 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17405 location of the info_ptr after reading all of those dies. PARENT
17406 is the parent of the die in question. */
17407
17408static struct die_info *
dee91e82 17409read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17410 const gdb_byte *info_ptr,
17411 const gdb_byte **new_info_ptr,
dee91e82 17412 struct die_info *parent)
639d11d3
DC
17413{
17414 struct die_info *die;
d521ce57 17415 const gdb_byte *cur_ptr;
639d11d3 17416
3e225074 17417 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17418 if (die == NULL)
17419 {
17420 *new_info_ptr = cur_ptr;
17421 return NULL;
17422 }
93311388 17423 store_in_ref_table (die, reader->cu);
639d11d3 17424
3e225074 17425 if (die->has_children)
bf6af496 17426 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17427 else
17428 {
17429 die->child = NULL;
17430 *new_info_ptr = cur_ptr;
17431 }
17432
17433 die->sibling = NULL;
17434 die->parent = parent;
17435 return die;
17436}
17437
17438/* Read a die, all of its descendents, and all of its siblings; set
17439 all of the fields of all of the dies correctly. Arguments are as
17440 in read_die_and_children. */
17441
17442static struct die_info *
bf6af496 17443read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17444 const gdb_byte *info_ptr,
17445 const gdb_byte **new_info_ptr,
bf6af496 17446 struct die_info *parent)
639d11d3
DC
17447{
17448 struct die_info *first_die, *last_sibling;
d521ce57 17449 const gdb_byte *cur_ptr;
639d11d3 17450
c906108c 17451 cur_ptr = info_ptr;
639d11d3
DC
17452 first_die = last_sibling = NULL;
17453
17454 while (1)
c906108c 17455 {
639d11d3 17456 struct die_info *die
dee91e82 17457 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17458
1d325ec1 17459 if (die == NULL)
c906108c 17460 {
639d11d3
DC
17461 *new_info_ptr = cur_ptr;
17462 return first_die;
c906108c 17463 }
1d325ec1
DJ
17464
17465 if (!first_die)
17466 first_die = die;
c906108c 17467 else
1d325ec1
DJ
17468 last_sibling->sibling = die;
17469
17470 last_sibling = die;
c906108c 17471 }
c906108c
SS
17472}
17473
bf6af496
DE
17474/* Read a die, all of its descendents, and all of its siblings; set
17475 all of the fields of all of the dies correctly. Arguments are as
17476 in read_die_and_children.
17477 This the main entry point for reading a DIE and all its children. */
17478
17479static struct die_info *
17480read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17481 const gdb_byte *info_ptr,
17482 const gdb_byte **new_info_ptr,
bf6af496
DE
17483 struct die_info *parent)
17484{
17485 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17486 new_info_ptr, parent);
17487
b4f54984 17488 if (dwarf_die_debug)
bf6af496
DE
17489 {
17490 fprintf_unfiltered (gdb_stdlog,
17491 "Read die from %s@0x%x of %s:\n",
96b79293 17492 reader->die_section->get_name (),
bf6af496
DE
17493 (unsigned) (info_ptr - reader->die_section->buffer),
17494 bfd_get_filename (reader->abfd));
b4f54984 17495 dump_die (die, dwarf_die_debug);
bf6af496
DE
17496 }
17497
17498 return die;
17499}
17500
3019eac3
DE
17501/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17502 attributes.
17503 The caller is responsible for filling in the extra attributes
17504 and updating (*DIEP)->num_attrs.
17505 Set DIEP to point to a newly allocated die with its information,
3e225074 17506 except for its child, sibling, and parent fields. */
93311388 17507
d521ce57 17508static const gdb_byte *
3019eac3 17509read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17510 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17511 int num_extra_attrs)
93311388 17512{
b64f50a1 17513 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17514 struct abbrev_info *abbrev;
17515 struct die_info *die;
17516 struct dwarf2_cu *cu = reader->cu;
17517 bfd *abfd = reader->abfd;
17518
9c541725 17519 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17520 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17521 info_ptr += bytes_read;
17522 if (!abbrev_number)
17523 {
17524 *diep = NULL;
93311388
DE
17525 return info_ptr;
17526 }
17527
685af9cd 17528 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17529 if (!abbrev)
348e048f
DE
17530 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17531 abbrev_number,
17532 bfd_get_filename (abfd));
17533
3019eac3 17534 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17535 die->sect_off = sect_off;
93311388
DE
17536 die->tag = abbrev->tag;
17537 die->abbrev = abbrev_number;
3e225074 17538 die->has_children = abbrev->has_children;
93311388 17539
3019eac3
DE
17540 /* Make the result usable.
17541 The caller needs to update num_attrs after adding the extra
17542 attributes. */
93311388
DE
17543 die->num_attrs = abbrev->num_attrs;
17544
18a8505e 17545 std::vector<int> indexes_that_need_reprocess;
93311388 17546 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17547 {
17548 bool need_reprocess;
17549 info_ptr =
17550 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17551 info_ptr, &need_reprocess);
17552 if (need_reprocess)
17553 indexes_that_need_reprocess.push_back (i);
17554 }
17555
17556 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17557 if (attr != nullptr)
17558 cu->str_offsets_base = DW_UNSND (attr);
93311388 17559
18a8505e
AT
17560 auto maybe_addr_base = lookup_addr_base(die);
17561 if (maybe_addr_base.has_value ())
17562 cu->addr_base = *maybe_addr_base;
17563 for (int index : indexes_that_need_reprocess)
17564 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17565 *diep = die;
93311388
DE
17566 return info_ptr;
17567}
17568
3019eac3
DE
17569/* Read a die and all its attributes.
17570 Set DIEP to point to a newly allocated die with its information,
3e225074 17571 except for its child, sibling, and parent fields. */
3019eac3 17572
d521ce57 17573static const gdb_byte *
3019eac3 17574read_full_die (const struct die_reader_specs *reader,
3e225074 17575 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17576{
d521ce57 17577 const gdb_byte *result;
bf6af496 17578
3e225074 17579 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17580
b4f54984 17581 if (dwarf_die_debug)
bf6af496
DE
17582 {
17583 fprintf_unfiltered (gdb_stdlog,
17584 "Read die from %s@0x%x of %s:\n",
96b79293 17585 reader->die_section->get_name (),
bf6af496
DE
17586 (unsigned) (info_ptr - reader->die_section->buffer),
17587 bfd_get_filename (reader->abfd));
b4f54984 17588 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17589 }
17590
17591 return result;
3019eac3 17592}
433df2d4 17593\f
c906108c 17594
72bf9492
DJ
17595/* Returns nonzero if TAG represents a type that we might generate a partial
17596 symbol for. */
17597
17598static int
17599is_type_tag_for_partial (int tag)
17600{
17601 switch (tag)
17602 {
17603#if 0
17604 /* Some types that would be reasonable to generate partial symbols for,
17605 that we don't at present. */
17606 case DW_TAG_array_type:
17607 case DW_TAG_file_type:
17608 case DW_TAG_ptr_to_member_type:
17609 case DW_TAG_set_type:
17610 case DW_TAG_string_type:
17611 case DW_TAG_subroutine_type:
17612#endif
17613 case DW_TAG_base_type:
17614 case DW_TAG_class_type:
680b30c7 17615 case DW_TAG_interface_type:
72bf9492
DJ
17616 case DW_TAG_enumeration_type:
17617 case DW_TAG_structure_type:
17618 case DW_TAG_subrange_type:
17619 case DW_TAG_typedef:
17620 case DW_TAG_union_type:
17621 return 1;
17622 default:
17623 return 0;
17624 }
17625}
17626
17627/* Load all DIEs that are interesting for partial symbols into memory. */
17628
17629static struct partial_die_info *
dee91e82 17630load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17631 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17632{
dee91e82 17633 struct dwarf2_cu *cu = reader->cu;
518817b3 17634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17635 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17636 unsigned int bytes_read;
5afb4e99 17637 unsigned int load_all = 0;
72bf9492
DJ
17638 int nesting_level = 1;
17639
17640 parent_die = NULL;
17641 last_die = NULL;
17642
7adf1e79
DE
17643 gdb_assert (cu->per_cu != NULL);
17644 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17645 load_all = 1;
17646
72bf9492
DJ
17647 cu->partial_dies
17648 = htab_create_alloc_ex (cu->header.length / 12,
17649 partial_die_hash,
17650 partial_die_eq,
17651 NULL,
17652 &cu->comp_unit_obstack,
17653 hashtab_obstack_allocate,
17654 dummy_obstack_deallocate);
17655
72bf9492
DJ
17656 while (1)
17657 {
685af9cd 17658 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17659
17660 /* A NULL abbrev means the end of a series of children. */
17661 if (abbrev == NULL)
17662 {
17663 if (--nesting_level == 0)
cd9983dd
YQ
17664 return first_die;
17665
72bf9492
DJ
17666 info_ptr += bytes_read;
17667 last_die = parent_die;
17668 parent_die = parent_die->die_parent;
17669 continue;
17670 }
17671
98bfdba5
PA
17672 /* Check for template arguments. We never save these; if
17673 they're seen, we just mark the parent, and go on our way. */
17674 if (parent_die != NULL
17675 && cu->language == language_cplus
17676 && (abbrev->tag == DW_TAG_template_type_param
17677 || abbrev->tag == DW_TAG_template_value_param))
17678 {
17679 parent_die->has_template_arguments = 1;
17680
17681 if (!load_all)
17682 {
17683 /* We don't need a partial DIE for the template argument. */
dee91e82 17684 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17685 continue;
17686 }
17687 }
17688
0d99eb77 17689 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17690 Skip their other children. */
17691 if (!load_all
17692 && cu->language == language_cplus
17693 && parent_die != NULL
17694 && parent_die->tag == DW_TAG_subprogram)
17695 {
dee91e82 17696 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17697 continue;
17698 }
17699
5afb4e99
DJ
17700 /* Check whether this DIE is interesting enough to save. Normally
17701 we would not be interested in members here, but there may be
17702 later variables referencing them via DW_AT_specification (for
17703 static members). */
17704 if (!load_all
17705 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17706 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17707 && abbrev->tag != DW_TAG_enumerator
17708 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17709 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17710 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17711 && abbrev->tag != DW_TAG_variable
5afb4e99 17712 && abbrev->tag != DW_TAG_namespace
f55ee35c 17713 && abbrev->tag != DW_TAG_module
95554aad 17714 && abbrev->tag != DW_TAG_member
74921315
KS
17715 && abbrev->tag != DW_TAG_imported_unit
17716 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17717 {
17718 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17719 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17720 continue;
17721 }
17722
6f06d47b
YQ
17723 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17724 abbrev);
cd9983dd 17725
48fbe735 17726 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17727
17728 /* This two-pass algorithm for processing partial symbols has a
17729 high cost in cache pressure. Thus, handle some simple cases
17730 here which cover the majority of C partial symbols. DIEs
17731 which neither have specification tags in them, nor could have
17732 specification tags elsewhere pointing at them, can simply be
17733 processed and discarded.
17734
17735 This segment is also optional; scan_partial_symbols and
17736 add_partial_symbol will handle these DIEs if we chain
17737 them in normally. When compilers which do not emit large
17738 quantities of duplicate debug information are more common,
17739 this code can probably be removed. */
17740
17741 /* Any complete simple types at the top level (pretty much all
17742 of them, for a language without namespaces), can be processed
17743 directly. */
17744 if (parent_die == NULL
cd9983dd
YQ
17745 && pdi.has_specification == 0
17746 && pdi.is_declaration == 0
17747 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17748 || pdi.tag == DW_TAG_base_type
17749 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17750 {
cd9983dd 17751 if (building_psymtab && pdi.name != NULL)
31edb802 17752 add_psymbol_to_list (pdi.name, false,
79748972 17753 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17754 psymbol_placement::STATIC,
1762568f 17755 0, cu->language, objfile);
cd9983dd 17756 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17757 continue;
17758 }
17759
d8228535
JK
17760 /* The exception for DW_TAG_typedef with has_children above is
17761 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17762 type_name_or_error will error on such types later.
d8228535
JK
17763
17764 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17765 it could not find the child DIEs referenced later, this is checked
17766 above. In correct DWARF DW_TAG_typedef should have no children. */
17767
cd9983dd 17768 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17769 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17770 "- DIE at %s [in module %s]"),
cd9983dd 17771 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17772
72bf9492
DJ
17773 /* If we're at the second level, and we're an enumerator, and
17774 our parent has no specification (meaning possibly lives in a
17775 namespace elsewhere), then we can add the partial symbol now
17776 instead of queueing it. */
cd9983dd 17777 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17778 && parent_die != NULL
17779 && parent_die->die_parent == NULL
17780 && parent_die->tag == DW_TAG_enumeration_type
17781 && parent_die->has_specification == 0)
17782 {
cd9983dd 17783 if (pdi.name == NULL)
b98664d3 17784 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17785 else if (building_psymtab)
31edb802 17786 add_psymbol_to_list (pdi.name, false,
79748972 17787 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17788 cu->language == language_cplus
75aedd27
TT
17789 ? psymbol_placement::GLOBAL
17790 : psymbol_placement::STATIC,
1762568f 17791 0, cu->language, objfile);
72bf9492 17792
cd9983dd 17793 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17794 continue;
17795 }
17796
cd9983dd 17797 struct partial_die_info *part_die
6f06d47b 17798 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17799
72bf9492
DJ
17800 /* We'll save this DIE so link it in. */
17801 part_die->die_parent = parent_die;
17802 part_die->die_sibling = NULL;
17803 part_die->die_child = NULL;
17804
17805 if (last_die && last_die == parent_die)
17806 last_die->die_child = part_die;
17807 else if (last_die)
17808 last_die->die_sibling = part_die;
17809
17810 last_die = part_die;
17811
17812 if (first_die == NULL)
17813 first_die = part_die;
17814
17815 /* Maybe add the DIE to the hash table. Not all DIEs that we
17816 find interesting need to be in the hash table, because we
17817 also have the parent/sibling/child chains; only those that we
17818 might refer to by offset later during partial symbol reading.
17819
17820 For now this means things that might have be the target of a
17821 DW_AT_specification, DW_AT_abstract_origin, or
17822 DW_AT_extension. DW_AT_extension will refer only to
17823 namespaces; DW_AT_abstract_origin refers to functions (and
17824 many things under the function DIE, but we do not recurse
17825 into function DIEs during partial symbol reading) and
17826 possibly variables as well; DW_AT_specification refers to
17827 declarations. Declarations ought to have the DW_AT_declaration
17828 flag. It happens that GCC forgets to put it in sometimes, but
17829 only for functions, not for types.
17830
17831 Adding more things than necessary to the hash table is harmless
17832 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17833 wasted time in find_partial_die, when we reread the compilation
17834 unit with load_all_dies set. */
72bf9492 17835
5afb4e99 17836 if (load_all
72929c62 17837 || abbrev->tag == DW_TAG_constant
5afb4e99 17838 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17839 || abbrev->tag == DW_TAG_variable
17840 || abbrev->tag == DW_TAG_namespace
17841 || part_die->is_declaration)
17842 {
17843 void **slot;
17844
17845 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17846 to_underlying (part_die->sect_off),
17847 INSERT);
72bf9492
DJ
17848 *slot = part_die;
17849 }
17850
72bf9492 17851 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17852 we have no reason to follow the children of structures; for other
98bfdba5
PA
17853 languages we have to, so that we can get at method physnames
17854 to infer fully qualified class names, for DW_AT_specification,
17855 and for C++ template arguments. For C++, we also look one level
17856 inside functions to find template arguments (if the name of the
17857 function does not already contain the template arguments).
bc30ff58 17858
0a4b0913
AB
17859 For Ada and Fortran, we need to scan the children of subprograms
17860 and lexical blocks as well because these languages allow the
17861 definition of nested entities that could be interesting for the
17862 debugger, such as nested subprograms for instance. */
72bf9492 17863 if (last_die->has_children
5afb4e99
DJ
17864 && (load_all
17865 || last_die->tag == DW_TAG_namespace
f55ee35c 17866 || last_die->tag == DW_TAG_module
72bf9492 17867 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17868 || (cu->language == language_cplus
17869 && last_die->tag == DW_TAG_subprogram
17870 && (last_die->name == NULL
17871 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17872 || (cu->language != language_c
17873 && (last_die->tag == DW_TAG_class_type
680b30c7 17874 || last_die->tag == DW_TAG_interface_type
72bf9492 17875 || last_die->tag == DW_TAG_structure_type
bc30ff58 17876 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17877 || ((cu->language == language_ada
17878 || cu->language == language_fortran)
bc30ff58
JB
17879 && (last_die->tag == DW_TAG_subprogram
17880 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17881 {
17882 nesting_level++;
17883 parent_die = last_die;
17884 continue;
17885 }
17886
17887 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17888 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17889
17890 /* Back to the top, do it again. */
17891 }
17892}
17893
6f06d47b
YQ
17894partial_die_info::partial_die_info (sect_offset sect_off_,
17895 struct abbrev_info *abbrev)
17896 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17897{
17898}
17899
35cc7ed7
YQ
17900/* Read a minimal amount of information into the minimal die structure.
17901 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17902
48fbe735
YQ
17903const gdb_byte *
17904partial_die_info::read (const struct die_reader_specs *reader,
17905 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17906{
dee91e82 17907 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17908 struct dwarf2_per_objfile *dwarf2_per_objfile
17909 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17910 unsigned int i;
c5aa993b 17911 int has_low_pc_attr = 0;
c906108c 17912 int has_high_pc_attr = 0;
91da1414 17913 int high_pc_relative = 0;
c906108c 17914
18a8505e 17915 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 17916 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17917 {
18a8505e
AT
17918 bool need_reprocess;
17919 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17920 info_ptr, &need_reprocess);
17921 /* String and address offsets that need to do the reprocessing have
17922 already been read at this point, so there is no need to wait until
17923 the loop terminates to do the reprocessing. */
17924 if (need_reprocess)
17925 read_attribute_reprocess (reader, &attr_vec[i]);
17926 attribute &attr = attr_vec[i];
c906108c 17927 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17928 partial symbol table. */
c906108c
SS
17929 switch (attr.name)
17930 {
17931 case DW_AT_name:
48fbe735 17932 switch (tag)
71c25dea
TT
17933 {
17934 case DW_TAG_compile_unit:
95554aad 17935 case DW_TAG_partial_unit:
348e048f 17936 case DW_TAG_type_unit:
71c25dea
TT
17937 /* Compilation units have a DW_AT_name that is a filename, not
17938 a source language identifier. */
17939 case DW_TAG_enumeration_type:
17940 case DW_TAG_enumerator:
17941 /* These tags always have simple identifiers already; no need
17942 to canonicalize them. */
48fbe735 17943 name = DW_STRING (&attr);
71c25dea
TT
17944 break;
17945 default:
48fbe735
YQ
17946 {
17947 struct objfile *objfile = dwarf2_per_objfile->objfile;
17948
17949 name
17950 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17951 &objfile->per_bfd->storage_obstack);
17952 }
71c25dea
TT
17953 break;
17954 }
c906108c 17955 break;
31ef98ae 17956 case DW_AT_linkage_name:
c906108c 17957 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17958 /* Note that both forms of linkage name might appear. We
17959 assume they will be the same, and we only store the last
17960 one we see. */
48fbe735 17961 linkage_name = DW_STRING (&attr);
c906108c
SS
17962 break;
17963 case DW_AT_low_pc:
17964 has_low_pc_attr = 1;
cd6c91b4 17965 lowpc = attr.value_as_address ();
c906108c
SS
17966 break;
17967 case DW_AT_high_pc:
17968 has_high_pc_attr = 1;
cd6c91b4
TT
17969 highpc = attr.value_as_address ();
17970 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 17971 high_pc_relative = 1;
c906108c
SS
17972 break;
17973 case DW_AT_location:
0963b4bd 17974 /* Support the .debug_loc offsets. */
4fc6c0d5 17975 if (attr.form_is_block ())
8e19ed76 17976 {
48fbe735 17977 d.locdesc = DW_BLOCK (&attr);
8e19ed76 17978 }
cd6c91b4 17979 else if (attr.form_is_section_offset ())
8e19ed76 17980 {
4d3c2250 17981 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
17982 }
17983 else
17984 {
4d3c2250
KB
17985 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17986 "partial symbol information");
8e19ed76 17987 }
c906108c 17988 break;
c906108c 17989 case DW_AT_external:
48fbe735 17990 is_external = DW_UNSND (&attr);
c906108c
SS
17991 break;
17992 case DW_AT_declaration:
48fbe735 17993 is_declaration = DW_UNSND (&attr);
c906108c
SS
17994 break;
17995 case DW_AT_type:
48fbe735 17996 has_type = 1;
c906108c
SS
17997 break;
17998 case DW_AT_abstract_origin:
17999 case DW_AT_specification:
72bf9492 18000 case DW_AT_extension:
48fbe735
YQ
18001 has_specification = 1;
18002 spec_offset = dwarf2_get_ref_die_offset (&attr);
18003 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18004 || cu->per_cu->is_dwz);
c906108c
SS
18005 break;
18006 case DW_AT_sibling:
18007 /* Ignore absolute siblings, they might point outside of
18008 the current compile unit. */
18009 if (attr.form == DW_FORM_ref_addr)
b98664d3 18010 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18011 else
b9502d3f 18012 {
48fbe735 18013 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18014 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18015 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18016
18017 if (sibling_ptr < info_ptr)
b98664d3 18018 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18019 else if (sibling_ptr > reader->buffer_end)
18020 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18021 else
48fbe735 18022 sibling = sibling_ptr;
b9502d3f 18023 }
c906108c 18024 break;
fa4028e9 18025 case DW_AT_byte_size:
48fbe735 18026 has_byte_size = 1;
fa4028e9 18027 break;
ff908ebf 18028 case DW_AT_const_value:
48fbe735 18029 has_const_value = 1;
ff908ebf 18030 break;
68511cec
CES
18031 case DW_AT_calling_convention:
18032 /* DWARF doesn't provide a way to identify a program's source-level
18033 entry point. DW_AT_calling_convention attributes are only meant
18034 to describe functions' calling conventions.
18035
18036 However, because it's a necessary piece of information in
0c1b455e
TT
18037 Fortran, and before DWARF 4 DW_CC_program was the only
18038 piece of debugging information whose definition refers to
18039 a 'main program' at all, several compilers marked Fortran
18040 main programs with DW_CC_program --- even when those
18041 functions use the standard calling conventions.
18042
18043 Although DWARF now specifies a way to provide this
18044 information, we support this practice for backward
18045 compatibility. */
68511cec 18046 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18047 && cu->language == language_fortran)
48fbe735 18048 main_subprogram = 1;
68511cec 18049 break;
481860b3
GB
18050 case DW_AT_inline:
18051 if (DW_UNSND (&attr) == DW_INL_inlined
18052 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18053 may_be_inlined = 1;
481860b3 18054 break;
95554aad
TT
18055
18056 case DW_AT_import:
48fbe735 18057 if (tag == DW_TAG_imported_unit)
36586728 18058 {
48fbe735
YQ
18059 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18060 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18061 || cu->per_cu->is_dwz);
18062 }
95554aad
TT
18063 break;
18064
0c1b455e 18065 case DW_AT_main_subprogram:
48fbe735 18066 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18067 break;
18068
05caa1d2
TT
18069 case DW_AT_ranges:
18070 {
18071 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18072 but that requires a full DIE, so instead we just
18073 reimplement it. */
18074 int need_ranges_base = tag != DW_TAG_compile_unit;
18075 unsigned int ranges_offset = (DW_UNSND (&attr)
18076 + (need_ranges_base
18077 ? cu->ranges_base
18078 : 0));
18079
18080 /* Value of the DW_AT_ranges attribute is the offset in the
18081 .debug_ranges section. */
18082 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18083 nullptr))
18084 has_pc_info = 1;
18085 }
18086 break;
18087
c906108c
SS
18088 default:
18089 break;
18090 }
18091 }
18092
10d06d82
TT
18093 /* For Ada, if both the name and the linkage name appear, we prefer
18094 the latter. This lets "catch exception" work better, regardless
18095 of the order in which the name and linkage name were emitted.
18096 Really, though, this is just a workaround for the fact that gdb
18097 doesn't store both the name and the linkage name. */
18098 if (cu->language == language_ada && linkage_name != nullptr)
18099 name = linkage_name;
18100
91da1414 18101 if (high_pc_relative)
48fbe735 18102 highpc += lowpc;
91da1414 18103
9373cf26
JK
18104 if (has_low_pc_attr && has_high_pc_attr)
18105 {
18106 /* When using the GNU linker, .gnu.linkonce. sections are used to
18107 eliminate duplicate copies of functions and vtables and such.
18108 The linker will arbitrarily choose one and discard the others.
18109 The AT_*_pc values for such functions refer to local labels in
18110 these sections. If the section from that file was discarded, the
18111 labels are not in the output, so the relocs get a value of 0.
18112 If this is a discarded function, mark the pc bounds as invalid,
18113 so that GDB will ignore it. */
48fbe735 18114 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18115 {
48fbe735 18116 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18118
b98664d3 18119 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18120 "for DIE at %s [in module %s]"),
48fbe735
YQ
18121 paddress (gdbarch, lowpc),
18122 sect_offset_str (sect_off),
9d8780f0 18123 objfile_name (objfile));
9373cf26
JK
18124 }
18125 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18126 else if (lowpc >= highpc)
9373cf26 18127 {
48fbe735 18128 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18129 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18130
b98664d3 18131 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18132 "for DIE at %s [in module %s]"),
48fbe735
YQ
18133 paddress (gdbarch, lowpc),
18134 paddress (gdbarch, highpc),
18135 sect_offset_str (sect_off),
9c541725 18136 objfile_name (objfile));
9373cf26
JK
18137 }
18138 else
48fbe735 18139 has_pc_info = 1;
9373cf26 18140 }
85cbf3d3 18141
c906108c
SS
18142 return info_ptr;
18143}
18144
72bf9492
DJ
18145/* Find a cached partial DIE at OFFSET in CU. */
18146
d590ff25
YQ
18147struct partial_die_info *
18148dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18149{
18150 struct partial_die_info *lookup_die = NULL;
6f06d47b 18151 struct partial_die_info part_die (sect_off);
72bf9492 18152
9a3c8263 18153 lookup_die = ((struct partial_die_info *)
d590ff25 18154 htab_find_with_hash (partial_dies, &part_die,
9c541725 18155 to_underlying (sect_off)));
72bf9492 18156
72bf9492
DJ
18157 return lookup_die;
18158}
18159
348e048f
DE
18160/* Find a partial DIE at OFFSET, which may or may not be in CU,
18161 except in the case of .debug_types DIEs which do not reference
18162 outside their CU (they do however referencing other types via
55f1336d 18163 DW_FORM_ref_sig8). */
72bf9492 18164
122cf0f2 18165static const struct cu_partial_die_info
9c541725 18166find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18167{
518817b3
SM
18168 struct dwarf2_per_objfile *dwarf2_per_objfile
18169 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18170 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18171 struct dwarf2_per_cu_data *per_cu = NULL;
18172 struct partial_die_info *pd = NULL;
72bf9492 18173
36586728 18174 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18175 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18176 {
d590ff25 18177 pd = cu->find_partial_die (sect_off);
5afb4e99 18178 if (pd != NULL)
fb816e8b 18179 return { cu, pd };
0d99eb77
DE
18180 /* We missed recording what we needed.
18181 Load all dies and try again. */
18182 per_cu = cu->per_cu;
5afb4e99 18183 }
0d99eb77
DE
18184 else
18185 {
18186 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18187 if (cu->per_cu->is_debug_types)
0d99eb77 18188 {
9d8780f0
SM
18189 error (_("Dwarf Error: Type Unit at offset %s contains"
18190 " external reference to offset %s [in module %s].\n"),
18191 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18192 bfd_get_filename (objfile->obfd));
18193 }
9c541725 18194 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18195 dwarf2_per_objfile);
72bf9492 18196
0d99eb77
DE
18197 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18198 load_partial_comp_unit (per_cu);
ae038cb0 18199
0d99eb77 18200 per_cu->cu->last_used = 0;
d590ff25 18201 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18202 }
5afb4e99 18203
dee91e82
DE
18204 /* If we didn't find it, and not all dies have been loaded,
18205 load them all and try again. */
18206
5afb4e99
DJ
18207 if (pd == NULL && per_cu->load_all_dies == 0)
18208 {
5afb4e99 18209 per_cu->load_all_dies = 1;
fd820528
DE
18210
18211 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18212 THIS_CU->cu may already be in use. So we can't just free it and
18213 replace its DIEs with the ones we read in. Instead, we leave those
18214 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18215 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18216 set. */
dee91e82 18217 load_partial_comp_unit (per_cu);
5afb4e99 18218
d590ff25 18219 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18220 }
18221
18222 if (pd == NULL)
18223 internal_error (__FILE__, __LINE__,
9d8780f0 18224 _("could not find partial DIE %s "
3e43a32a 18225 "in cache [from module %s]\n"),
9d8780f0 18226 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18227 return { per_cu->cu, pd };
72bf9492
DJ
18228}
18229
abc72ce4
DE
18230/* See if we can figure out if the class lives in a namespace. We do
18231 this by looking for a member function; its demangled name will
18232 contain namespace info, if there is any. */
18233
18234static void
18235guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18236 struct dwarf2_cu *cu)
18237{
18238 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18239 what template types look like, because the demangler
18240 frequently doesn't give the same name as the debug info. We
18241 could fix this by only using the demangled name to get the
18242 prefix (but see comment in read_structure_type). */
18243
18244 struct partial_die_info *real_pdi;
18245 struct partial_die_info *child_pdi;
18246
18247 /* If this DIE (this DIE's specification, if any) has a parent, then
18248 we should not do this. We'll prepend the parent's fully qualified
18249 name when we create the partial symbol. */
18250
18251 real_pdi = struct_pdi;
18252 while (real_pdi->has_specification)
fb816e8b 18253 {
122cf0f2
AB
18254 auto res = find_partial_die (real_pdi->spec_offset,
18255 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18256 real_pdi = res.pdi;
18257 cu = res.cu;
18258 }
abc72ce4
DE
18259
18260 if (real_pdi->die_parent != NULL)
18261 return;
18262
18263 for (child_pdi = struct_pdi->die_child;
18264 child_pdi != NULL;
18265 child_pdi = child_pdi->die_sibling)
18266 {
18267 if (child_pdi->tag == DW_TAG_subprogram
18268 && child_pdi->linkage_name != NULL)
18269 {
43816ebc
TT
18270 gdb::unique_xmalloc_ptr<char> actual_class_name
18271 (language_class_name_from_physname (cu->language_defn,
18272 child_pdi->linkage_name));
abc72ce4
DE
18273 if (actual_class_name != NULL)
18274 {
518817b3 18275 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18276 struct_pdi->name
021887d8 18277 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 18278 actual_class_name.get ());
abc72ce4
DE
18279 }
18280 break;
18281 }
18282 }
18283}
18284
52356b79
YQ
18285void
18286partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18287{
abc72ce4
DE
18288 /* Once we've fixed up a die, there's no point in doing so again.
18289 This also avoids a memory leak if we were to call
18290 guess_partial_die_structure_name multiple times. */
52356b79 18291 if (fixup_called)
abc72ce4
DE
18292 return;
18293
72bf9492
DJ
18294 /* If we found a reference attribute and the DIE has no name, try
18295 to find a name in the referred to DIE. */
18296
52356b79 18297 if (name == NULL && has_specification)
72bf9492
DJ
18298 {
18299 struct partial_die_info *spec_die;
72bf9492 18300
122cf0f2 18301 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18302 spec_die = res.pdi;
18303 cu = res.cu;
72bf9492 18304
52356b79 18305 spec_die->fixup (cu);
72bf9492
DJ
18306
18307 if (spec_die->name)
18308 {
52356b79 18309 name = spec_die->name;
72bf9492
DJ
18310
18311 /* Copy DW_AT_external attribute if it is set. */
18312 if (spec_die->is_external)
52356b79 18313 is_external = spec_die->is_external;
72bf9492
DJ
18314 }
18315 }
18316
18317 /* Set default names for some unnamed DIEs. */
72bf9492 18318
52356b79
YQ
18319 if (name == NULL && tag == DW_TAG_namespace)
18320 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18321
abc72ce4
DE
18322 /* If there is no parent die to provide a namespace, and there are
18323 children, see if we can determine the namespace from their linkage
122d1940 18324 name. */
abc72ce4 18325 if (cu->language == language_cplus
fd5866f6 18326 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18327 && die_parent == NULL
18328 && has_children
18329 && (tag == DW_TAG_class_type
18330 || tag == DW_TAG_structure_type
18331 || tag == DW_TAG_union_type))
18332 guess_partial_die_structure_name (this, cu);
abc72ce4 18333
53832f31
TT
18334 /* GCC might emit a nameless struct or union that has a linkage
18335 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18336 if (name == NULL
18337 && (tag == DW_TAG_class_type
18338 || tag == DW_TAG_interface_type
18339 || tag == DW_TAG_structure_type
18340 || tag == DW_TAG_union_type)
18341 && linkage_name != NULL)
53832f31 18342 {
43816ebc
TT
18343 gdb::unique_xmalloc_ptr<char> demangled
18344 (gdb_demangle (linkage_name, DMGL_TYPES));
18345 if (demangled != nullptr)
53832f31 18346 {
96408a79
SA
18347 const char *base;
18348
18349 /* Strip any leading namespaces/classes, keep only the base name.
18350 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18351 base = strrchr (demangled.get (), ':');
18352 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18353 base++;
18354 else
43816ebc 18355 base = demangled.get ();
96408a79 18356
518817b3 18357 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 18358 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
18359 }
18360 }
18361
52356b79 18362 fixup_called = 1;
72bf9492
DJ
18363}
18364
18a8505e
AT
18365/* Process the attributes that had to be skipped in the first round. These
18366 attributes are the ones that need str_offsets_base or addr_base attributes.
18367 They could not have been processed in the first round, because at the time
18368 the values of str_offsets_base or addr_base may not have been known. */
18369void read_attribute_reprocess (const struct die_reader_specs *reader,
18370 struct attribute *attr)
18371{
18372 struct dwarf2_cu *cu = reader->cu;
18373 switch (attr->form)
18374 {
18375 case DW_FORM_addrx:
18376 case DW_FORM_GNU_addr_index:
18377 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18378 break;
18379 case DW_FORM_strx:
18380 case DW_FORM_strx1:
18381 case DW_FORM_strx2:
18382 case DW_FORM_strx3:
18383 case DW_FORM_strx4:
18384 case DW_FORM_GNU_str_index:
18385 {
18386 unsigned int str_index = DW_UNSND (attr);
18387 if (reader->dwo_file != NULL)
18388 {
18389 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18390 DW_STRING_IS_CANONICAL (attr) = 0;
18391 }
18392 else
18393 {
18394 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18395 DW_STRING_IS_CANONICAL (attr) = 0;
18396 }
18397 break;
18398 }
18399 default:
18400 gdb_assert_not_reached (_("Unexpected DWARF form."));
18401 }
18402}
18403
a8329558 18404/* Read an attribute value described by an attribute form. */
c906108c 18405
d521ce57 18406static const gdb_byte *
dee91e82
DE
18407read_attribute_value (const struct die_reader_specs *reader,
18408 struct attribute *attr, unsigned form,
18a8505e
AT
18409 LONGEST implicit_const, const gdb_byte *info_ptr,
18410 bool *need_reprocess)
c906108c 18411{
dee91e82 18412 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18413 struct dwarf2_per_objfile *dwarf2_per_objfile
18414 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18415 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18416 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18417 bfd *abfd = reader->abfd;
e7c27a73 18418 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18419 unsigned int bytes_read;
18420 struct dwarf_block *blk;
18a8505e 18421 *need_reprocess = false;
c906108c 18422
aead7601 18423 attr->form = (enum dwarf_form) form;
a8329558 18424 switch (form)
c906108c 18425 {
c906108c 18426 case DW_FORM_ref_addr:
ae411497 18427 if (cu->header.version == 2)
c8a7a66f
TT
18428 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18429 &bytes_read);
ae411497 18430 else
8266302d
TT
18431 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18432 &bytes_read);
ae411497
TT
18433 info_ptr += bytes_read;
18434 break;
36586728 18435 case DW_FORM_GNU_ref_alt:
8266302d 18436 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18437 info_ptr += bytes_read;
18438 break;
ae411497 18439 case DW_FORM_addr:
c8a7a66f 18440 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18441 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18442 info_ptr += bytes_read;
c906108c
SS
18443 break;
18444 case DW_FORM_block2:
7b5a2f43 18445 blk = dwarf_alloc_block (cu);
c906108c
SS
18446 blk->size = read_2_bytes (abfd, info_ptr);
18447 info_ptr += 2;
18448 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18449 info_ptr += blk->size;
18450 DW_BLOCK (attr) = blk;
18451 break;
18452 case DW_FORM_block4:
7b5a2f43 18453 blk = dwarf_alloc_block (cu);
c906108c
SS
18454 blk->size = read_4_bytes (abfd, info_ptr);
18455 info_ptr += 4;
18456 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18457 info_ptr += blk->size;
18458 DW_BLOCK (attr) = blk;
18459 break;
18460 case DW_FORM_data2:
18461 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18462 info_ptr += 2;
18463 break;
18464 case DW_FORM_data4:
18465 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18466 info_ptr += 4;
18467 break;
18468 case DW_FORM_data8:
18469 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18470 info_ptr += 8;
18471 break;
0224619f
JK
18472 case DW_FORM_data16:
18473 blk = dwarf_alloc_block (cu);
18474 blk->size = 16;
18475 blk->data = read_n_bytes (abfd, info_ptr, 16);
18476 info_ptr += 16;
18477 DW_BLOCK (attr) = blk;
18478 break;
2dc7f7b3 18479 case DW_FORM_sec_offset:
8266302d 18480 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18481 info_ptr += bytes_read;
18482 break;
c906108c 18483 case DW_FORM_string:
9b1c24c8 18484 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18485 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18486 info_ptr += bytes_read;
18487 break;
4bdf3d34 18488 case DW_FORM_strp:
36586728
TT
18489 if (!cu->per_cu->is_dwz)
18490 {
ed2dc618
SM
18491 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18492 abfd, info_ptr, cu_header,
36586728
TT
18493 &bytes_read);
18494 DW_STRING_IS_CANONICAL (attr) = 0;
18495 info_ptr += bytes_read;
18496 break;
18497 }
18498 /* FALLTHROUGH */
43988095
JK
18499 case DW_FORM_line_strp:
18500 if (!cu->per_cu->is_dwz)
18501 {
ed2dc618
SM
18502 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18503 abfd, info_ptr,
43988095
JK
18504 cu_header, &bytes_read);
18505 DW_STRING_IS_CANONICAL (attr) = 0;
18506 info_ptr += bytes_read;
18507 break;
18508 }
18509 /* FALLTHROUGH */
36586728
TT
18510 case DW_FORM_GNU_strp_alt:
18511 {
ed2dc618 18512 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18513 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18514 &bytes_read);
36586728 18515
ed2dc618
SM
18516 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18517 dwz, str_offset);
36586728
TT
18518 DW_STRING_IS_CANONICAL (attr) = 0;
18519 info_ptr += bytes_read;
18520 }
4bdf3d34 18521 break;
2dc7f7b3 18522 case DW_FORM_exprloc:
c906108c 18523 case DW_FORM_block:
7b5a2f43 18524 blk = dwarf_alloc_block (cu);
c906108c
SS
18525 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18526 info_ptr += bytes_read;
18527 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18528 info_ptr += blk->size;
18529 DW_BLOCK (attr) = blk;
18530 break;
18531 case DW_FORM_block1:
7b5a2f43 18532 blk = dwarf_alloc_block (cu);
c906108c
SS
18533 blk->size = read_1_byte (abfd, info_ptr);
18534 info_ptr += 1;
18535 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18536 info_ptr += blk->size;
18537 DW_BLOCK (attr) = blk;
18538 break;
18539 case DW_FORM_data1:
18540 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18541 info_ptr += 1;
18542 break;
18543 case DW_FORM_flag:
18544 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18545 info_ptr += 1;
18546 break;
2dc7f7b3
TT
18547 case DW_FORM_flag_present:
18548 DW_UNSND (attr) = 1;
18549 break;
c906108c
SS
18550 case DW_FORM_sdata:
18551 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18552 info_ptr += bytes_read;
18553 break;
18554 case DW_FORM_udata:
18a8505e 18555 case DW_FORM_rnglistx:
c906108c
SS
18556 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18557 info_ptr += bytes_read;
18558 break;
18559 case DW_FORM_ref1:
9c541725 18560 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18561 + read_1_byte (abfd, info_ptr));
c906108c
SS
18562 info_ptr += 1;
18563 break;
18564 case DW_FORM_ref2:
9c541725 18565 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18566 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18567 info_ptr += 2;
18568 break;
18569 case DW_FORM_ref4:
9c541725 18570 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18571 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18572 info_ptr += 4;
18573 break;
613e1657 18574 case DW_FORM_ref8:
9c541725 18575 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18576 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18577 info_ptr += 8;
18578 break;
55f1336d 18579 case DW_FORM_ref_sig8:
ac9ec31b 18580 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18581 info_ptr += 8;
18582 break;
c906108c 18583 case DW_FORM_ref_udata:
9c541725 18584 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18585 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18586 info_ptr += bytes_read;
18587 break;
c906108c 18588 case DW_FORM_indirect:
a8329558
KW
18589 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18590 info_ptr += bytes_read;
43988095
JK
18591 if (form == DW_FORM_implicit_const)
18592 {
18593 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18594 info_ptr += bytes_read;
18595 }
18596 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18597 info_ptr, need_reprocess);
43988095
JK
18598 break;
18599 case DW_FORM_implicit_const:
18600 DW_SND (attr) = implicit_const;
a8329558 18601 break;
336d760d 18602 case DW_FORM_addrx:
3019eac3 18603 case DW_FORM_GNU_addr_index:
18a8505e
AT
18604 *need_reprocess = true;
18605 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18606 info_ptr += bytes_read;
18607 break;
cf532bd1 18608 case DW_FORM_strx:
15f18d14
AT
18609 case DW_FORM_strx1:
18610 case DW_FORM_strx2:
18611 case DW_FORM_strx3:
18612 case DW_FORM_strx4:
3019eac3 18613 case DW_FORM_GNU_str_index:
3019eac3 18614 {
15f18d14
AT
18615 ULONGEST str_index;
18616 if (form == DW_FORM_strx1)
18617 {
18618 str_index = read_1_byte (abfd, info_ptr);
18619 info_ptr += 1;
18620 }
18621 else if (form == DW_FORM_strx2)
18622 {
18623 str_index = read_2_bytes (abfd, info_ptr);
18624 info_ptr += 2;
18625 }
18626 else if (form == DW_FORM_strx3)
18627 {
18628 str_index = read_3_bytes (abfd, info_ptr);
18629 info_ptr += 3;
18630 }
18631 else if (form == DW_FORM_strx4)
18632 {
18633 str_index = read_4_bytes (abfd, info_ptr);
18634 info_ptr += 4;
18635 }
18636 else
18637 {
18638 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18639 info_ptr += bytes_read;
18640 }
18a8505e
AT
18641 *need_reprocess = true;
18642 DW_UNSND (attr) = str_index;
18643 }
3019eac3 18644 break;
c906108c 18645 default:
8a3fe4f8 18646 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18647 dwarf_form_name (form),
18648 bfd_get_filename (abfd));
c906108c 18649 }
28e94949 18650
36586728 18651 /* Super hack. */
cd6c91b4 18652 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18653 attr->form = DW_FORM_GNU_ref_alt;
18654
28e94949
JB
18655 /* We have seen instances where the compiler tried to emit a byte
18656 size attribute of -1 which ended up being encoded as an unsigned
18657 0xffffffff. Although 0xffffffff is technically a valid size value,
18658 an object of this size seems pretty unlikely so we can relatively
18659 safely treat these cases as if the size attribute was invalid and
18660 treat them as zero by default. */
18661 if (attr->name == DW_AT_byte_size
18662 && form == DW_FORM_data4
18663 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18664 {
18665 complaint
b98664d3 18666 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18667 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18668 DW_UNSND (attr) = 0;
18669 }
28e94949 18670
c906108c
SS
18671 return info_ptr;
18672}
18673
a8329558
KW
18674/* Read an attribute described by an abbreviated attribute. */
18675
d521ce57 18676static const gdb_byte *
dee91e82
DE
18677read_attribute (const struct die_reader_specs *reader,
18678 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18679 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18680{
18681 attr->name = abbrev->name;
43988095 18682 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18683 abbrev->implicit_const, info_ptr,
18684 need_reprocess);
a8329558
KW
18685}
18686
c764a876
DE
18687/* Cover function for read_initial_length.
18688 Returns the length of the object at BUF, and stores the size of the
18689 initial length in *BYTES_READ and stores the size that offsets will be in
18690 *OFFSET_SIZE.
18691 If the initial length size is not equivalent to that specified in
18692 CU_HEADER then issue a complaint.
18693 This is useful when reading non-comp-unit headers. */
dd373385 18694
c764a876 18695static LONGEST
d521ce57 18696read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
18697 const struct comp_unit_head *cu_header,
18698 unsigned int *bytes_read,
18699 unsigned int *offset_size)
18700{
18701 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18702
18703 gdb_assert (cu_header->initial_length_size == 4
18704 || cu_header->initial_length_size == 8
18705 || cu_header->initial_length_size == 12);
18706
18707 if (cu_header->initial_length_size != *bytes_read)
b98664d3 18708 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 18709
c764a876 18710 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 18711 return length;
613e1657
KB
18712}
18713
43988095
JK
18714/* Return pointer to string at section SECT offset STR_OFFSET with error
18715 reporting strings FORM_NAME and SECT_NAME. */
18716
d521ce57 18717static const char *
ed2dc618
SM
18718read_indirect_string_at_offset_from (struct objfile *objfile,
18719 bfd *abfd, LONGEST str_offset,
43988095
JK
18720 struct dwarf2_section_info *sect,
18721 const char *form_name,
18722 const char *sect_name)
18723{
96b79293 18724 sect->read (objfile);
43988095
JK
18725 if (sect->buffer == NULL)
18726 error (_("%s used without %s section [in module %s]"),
18727 form_name, sect_name, bfd_get_filename (abfd));
18728 if (str_offset >= sect->size)
18729 error (_("%s pointing outside of %s section [in module %s]"),
18730 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 18731 gdb_assert (HOST_CHAR_BIT == 8);
43988095 18732 if (sect->buffer[str_offset] == '\0')
4bdf3d34 18733 return NULL;
43988095
JK
18734 return (const char *) (sect->buffer + str_offset);
18735}
18736
18737/* Return pointer to string at .debug_str offset STR_OFFSET. */
18738
18739static const char *
ed2dc618
SM
18740read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18741 bfd *abfd, LONGEST str_offset)
43988095 18742{
ed2dc618
SM
18743 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18744 abfd, str_offset,
43988095
JK
18745 &dwarf2_per_objfile->str,
18746 "DW_FORM_strp", ".debug_str");
18747}
18748
18749/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18750
18751static const char *
ed2dc618
SM
18752read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18753 bfd *abfd, LONGEST str_offset)
43988095 18754{
ed2dc618
SM
18755 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18756 abfd, str_offset,
43988095
JK
18757 &dwarf2_per_objfile->line_str,
18758 "DW_FORM_line_strp",
18759 ".debug_line_str");
c906108c
SS
18760}
18761
36586728
TT
18762/* Read a string at offset STR_OFFSET in the .debug_str section from
18763 the .dwz file DWZ. Throw an error if the offset is too large. If
18764 the string consists of a single NUL byte, return NULL; otherwise
18765 return a pointer to the string. */
18766
d521ce57 18767static const char *
ed2dc618
SM
18768read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18769 LONGEST str_offset)
36586728 18770{
96b79293 18771 dwz->str.read (objfile);
36586728
TT
18772
18773 if (dwz->str.buffer == NULL)
18774 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18775 "section [in module %s]"),
00f93c44 18776 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18777 if (str_offset >= dwz->str.size)
18778 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18779 ".debug_str section [in module %s]"),
00f93c44 18780 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
18781 gdb_assert (HOST_CHAR_BIT == 8);
18782 if (dwz->str.buffer[str_offset] == '\0')
18783 return NULL;
d521ce57 18784 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
18785}
18786
43988095
JK
18787/* Return pointer to string at .debug_str offset as read from BUF.
18788 BUF is assumed to be in a compilation unit described by CU_HEADER.
18789 Return *BYTES_READ_PTR count of bytes read from BUF. */
18790
d521ce57 18791static const char *
ed2dc618
SM
18792read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18793 const gdb_byte *buf,
cf2c3c16
TT
18794 const struct comp_unit_head *cu_header,
18795 unsigned int *bytes_read_ptr)
18796{
8266302d 18797 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18798
ed2dc618 18799 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
18800}
18801
43988095
JK
18802/* Return pointer to string at .debug_line_str offset as read from BUF.
18803 BUF is assumed to be in a compilation unit described by CU_HEADER.
18804 Return *BYTES_READ_PTR count of bytes read from BUF. */
18805
18806static const char *
ed2dc618
SM
18807read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18808 bfd *abfd, const gdb_byte *buf,
43988095
JK
18809 const struct comp_unit_head *cu_header,
18810 unsigned int *bytes_read_ptr)
18811{
8266302d 18812 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18813
ed2dc618
SM
18814 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18815 str_offset);
43988095
JK
18816}
18817
3019eac3 18818/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18819 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18820 ADDR_SIZE is the size of addresses from the CU header. */
18821
18822static CORE_ADDR
ed2dc618 18823read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18824 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18825 int addr_size)
3019eac3
DE
18826{
18827 struct objfile *objfile = dwarf2_per_objfile->objfile;
18828 bfd *abfd = objfile->obfd;
18829 const gdb_byte *info_ptr;
18a8505e 18830 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18831
96b79293 18832 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18833 if (dwarf2_per_objfile->addr.buffer == NULL)
18834 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18835 objfile_name (objfile));
18a8505e
AT
18836 if (addr_base_or_zero + addr_index * addr_size
18837 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18838 error (_("DW_FORM_addr_index pointing outside of "
18839 ".debug_addr section [in module %s]"),
4262abfb 18840 objfile_name (objfile));
3019eac3 18841 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18842 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18843 if (addr_size == 4)
18844 return bfd_get_32 (abfd, info_ptr);
18845 else
18846 return bfd_get_64 (abfd, info_ptr);
18847}
18848
18849/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18850
18851static CORE_ADDR
18852read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18853{
518817b3
SM
18854 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18855 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18856}
18857
18858/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18859
18860static CORE_ADDR
d521ce57 18861read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18862 unsigned int *bytes_read)
18863{
518817b3 18864 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18865 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18866
18867 return read_addr_index (cu, addr_index);
18868}
18869
450a1bfc 18870/* See read.h. */
3019eac3
DE
18871
18872CORE_ADDR
450a1bfc 18873dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18874{
ed2dc618 18875 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18876 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18877 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18878 int addr_size;
18879
3019eac3
DE
18880 /* We need addr_base and addr_size.
18881 If we don't have PER_CU->cu, we have to get it.
18882 Nasty, but the alternative is storing the needed info in PER_CU,
18883 which at this point doesn't seem justified: it's not clear how frequently
18884 it would get used and it would increase the size of every PER_CU.
18885 Entry points like dwarf2_per_cu_addr_size do a similar thing
18886 so we're not in uncharted territory here.
18887 Alas we need to be a bit more complicated as addr_base is contained
18888 in the DIE.
18889
18890 We don't need to read the entire CU(/TU).
18891 We just need the header and top level die.
a1b64ce1 18892
3019eac3 18893 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18894 For now we skip this optimization. */
3019eac3
DE
18895
18896 if (cu != NULL)
18897 {
18898 addr_base = cu->addr_base;
18899 addr_size = cu->header.addr_size;
18900 }
18901 else
18902 {
6751ebae 18903 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18904 addr_base = reader.cu->addr_base;
18905 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18906 }
18907
ed2dc618
SM
18908 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18909 addr_size);
3019eac3
DE
18910}
18911
18a8505e
AT
18912/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18913 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18914 DWO file. */
3019eac3 18915
d521ce57 18916static const char *
18a8505e
AT
18917read_str_index (struct dwarf2_cu *cu,
18918 struct dwarf2_section_info *str_section,
18919 struct dwarf2_section_info *str_offsets_section,
18920 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18921{
518817b3
SM
18922 struct dwarf2_per_objfile *dwarf2_per_objfile
18923 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18924 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18925 const char *objf_name = objfile_name (objfile);
3019eac3 18926 bfd *abfd = objfile->obfd;
d521ce57 18927 const gdb_byte *info_ptr;
3019eac3 18928 ULONGEST str_offset;
cf532bd1 18929 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18930
96b79293
TT
18931 str_section->read (objfile);
18932 str_offsets_section->read (objfile);
73869dc2 18933 if (str_section->buffer == NULL)
18a8505e 18934 error (_("%s used without %s section"
9d8780f0 18935 " in CU at offset %s [in module %s]"),
96b79293 18936 form_name, str_section->get_name (),
18a8505e 18937 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18938 if (str_offsets_section->buffer == NULL)
18a8505e 18939 error (_("%s used without %s section"
9d8780f0 18940 " in CU at offset %s [in module %s]"),
96b79293 18941 form_name, str_section->get_name (),
18a8505e 18942 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18943 info_ptr = (str_offsets_section->buffer
18a8505e 18944 + str_offsets_base
3019eac3
DE
18945 + str_index * cu->header.offset_size);
18946 if (cu->header.offset_size == 4)
18947 str_offset = bfd_get_32 (abfd, info_ptr);
18948 else
18949 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18950 if (str_offset >= str_section->size)
57d63ce2 18951 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18952 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18953 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18954 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18955}
18956
18a8505e
AT
18957/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18958
18959static const char *
18960read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18961{
18962 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18963 ? reader->cu->header.addr_size : 0;
18964 return read_str_index (reader->cu,
18965 &reader->dwo_file->sections.str,
18966 &reader->dwo_file->sections.str_offsets,
18967 str_offsets_base, str_index);
18968}
18969
18970/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18971
18972static const char *
18973read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18974{
18975 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18976 const char *objf_name = objfile_name (objfile);
18977 static const char form_name[] = "DW_FORM_GNU_str_index";
18978 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18979
18980 if (!cu->str_offsets_base.has_value ())
18981 error (_("%s used in Fission stub without %s"
18982 " in CU at offset 0x%lx [in module %s]"),
18983 form_name, str_offsets_attr_name,
18984 (long) cu->header.offset_size, objf_name);
18985
18986 return read_str_index (cu,
18987 &cu->per_cu->dwarf2_per_objfile->str,
18988 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18989 *cu->str_offsets_base, str_index);
18990}
18991
3019eac3
DE
18992/* Return the length of an LEB128 number in BUF. */
18993
18994static int
18995leb128_size (const gdb_byte *buf)
18996{
18997 const gdb_byte *begin = buf;
18998 gdb_byte byte;
18999
19000 while (1)
19001 {
19002 byte = *buf++;
19003 if ((byte & 128) == 0)
19004 return buf - begin;
19005 }
19006}
19007
c906108c 19008static void
e142c38c 19009set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19010{
19011 switch (lang)
19012 {
19013 case DW_LANG_C89:
76bee0cc 19014 case DW_LANG_C99:
0cfd832f 19015 case DW_LANG_C11:
c906108c 19016 case DW_LANG_C:
d1be3247 19017 case DW_LANG_UPC:
e142c38c 19018 cu->language = language_c;
c906108c 19019 break;
9c37b5ae 19020 case DW_LANG_Java:
c906108c 19021 case DW_LANG_C_plus_plus:
0cfd832f
MW
19022 case DW_LANG_C_plus_plus_11:
19023 case DW_LANG_C_plus_plus_14:
e142c38c 19024 cu->language = language_cplus;
c906108c 19025 break;
6aecb9c2
JB
19026 case DW_LANG_D:
19027 cu->language = language_d;
19028 break;
c906108c
SS
19029 case DW_LANG_Fortran77:
19030 case DW_LANG_Fortran90:
b21b22e0 19031 case DW_LANG_Fortran95:
f7de9aab
MW
19032 case DW_LANG_Fortran03:
19033 case DW_LANG_Fortran08:
e142c38c 19034 cu->language = language_fortran;
c906108c 19035 break;
a766d390
DE
19036 case DW_LANG_Go:
19037 cu->language = language_go;
19038 break;
c906108c 19039 case DW_LANG_Mips_Assembler:
e142c38c 19040 cu->language = language_asm;
c906108c
SS
19041 break;
19042 case DW_LANG_Ada83:
8aaf0b47 19043 case DW_LANG_Ada95:
bc5f45f8
JB
19044 cu->language = language_ada;
19045 break;
72019c9c
GM
19046 case DW_LANG_Modula2:
19047 cu->language = language_m2;
19048 break;
fe8e67fd
PM
19049 case DW_LANG_Pascal83:
19050 cu->language = language_pascal;
19051 break;
22566fbd
DJ
19052 case DW_LANG_ObjC:
19053 cu->language = language_objc;
19054 break;
c44af4eb
TT
19055 case DW_LANG_Rust:
19056 case DW_LANG_Rust_old:
19057 cu->language = language_rust;
19058 break;
c906108c
SS
19059 case DW_LANG_Cobol74:
19060 case DW_LANG_Cobol85:
c906108c 19061 default:
e142c38c 19062 cu->language = language_minimal;
c906108c
SS
19063 break;
19064 }
e142c38c 19065 cu->language_defn = language_def (cu->language);
c906108c
SS
19066}
19067
19068/* Return the named attribute or NULL if not there. */
19069
19070static struct attribute *
e142c38c 19071dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19072{
a48e046c 19073 for (;;)
c906108c 19074 {
a48e046c
TT
19075 unsigned int i;
19076 struct attribute *spec = NULL;
19077
19078 for (i = 0; i < die->num_attrs; ++i)
19079 {
19080 if (die->attrs[i].name == name)
19081 return &die->attrs[i];
19082 if (die->attrs[i].name == DW_AT_specification
19083 || die->attrs[i].name == DW_AT_abstract_origin)
19084 spec = &die->attrs[i];
19085 }
19086
19087 if (!spec)
19088 break;
c906108c 19089
f2f0e013 19090 die = follow_die_ref (die, spec, &cu);
f2f0e013 19091 }
c5aa993b 19092
c906108c
SS
19093 return NULL;
19094}
19095
348e048f
DE
19096/* Return the named attribute or NULL if not there,
19097 but do not follow DW_AT_specification, etc.
19098 This is for use in contexts where we're reading .debug_types dies.
19099 Following DW_AT_specification, DW_AT_abstract_origin will take us
19100 back up the chain, and we want to go down. */
19101
19102static struct attribute *
45e58e77 19103dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19104{
19105 unsigned int i;
19106
19107 for (i = 0; i < die->num_attrs; ++i)
19108 if (die->attrs[i].name == name)
19109 return &die->attrs[i];
19110
19111 return NULL;
19112}
19113
7d45c7c3
KB
19114/* Return the string associated with a string-typed attribute, or NULL if it
19115 is either not found or is of an incorrect type. */
19116
19117static const char *
19118dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19119{
19120 struct attribute *attr;
19121 const char *str = NULL;
19122
19123 attr = dwarf2_attr (die, name, cu);
19124
19125 if (attr != NULL)
19126 {
43988095 19127 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19128 || attr->form == DW_FORM_string
cf532bd1 19129 || attr->form == DW_FORM_strx
8fe0f950
AT
19130 || attr->form == DW_FORM_strx1
19131 || attr->form == DW_FORM_strx2
19132 || attr->form == DW_FORM_strx3
19133 || attr->form == DW_FORM_strx4
b3340438 19134 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19135 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19136 str = DW_STRING (attr);
19137 else
b98664d3 19138 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19139 "DIE at %s in module %s"),
19140 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19141 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19142 }
19143
19144 return str;
19145}
19146
a084a2a6 19147/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19148 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19149static const char *
19150dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19151{
19152 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19153 if (dwo_name == nullptr)
19154 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19155 return dwo_name;
19156}
19157
05cf31d1
JB
19158/* Return non-zero iff the attribute NAME is defined for the given DIE,
19159 and holds a non-zero value. This function should only be used for
2dc7f7b3 19160 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19161
19162static int
19163dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19164{
19165 struct attribute *attr = dwarf2_attr (die, name, cu);
19166
19167 return (attr && DW_UNSND (attr));
19168}
19169
3ca72b44 19170static int
e142c38c 19171die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19172{
05cf31d1
JB
19173 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19174 which value is non-zero. However, we have to be careful with
19175 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19176 (via dwarf2_flag_true_p) follows this attribute. So we may
19177 end up accidently finding a declaration attribute that belongs
19178 to a different DIE referenced by the specification attribute,
19179 even though the given DIE does not have a declaration attribute. */
19180 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19181 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19182}
19183
63d06c5c 19184/* Return the die giving the specification for DIE, if there is
f2f0e013 19185 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19186 containing the return value on output. If there is no
19187 specification, but there is an abstract origin, that is
19188 returned. */
63d06c5c
DC
19189
19190static struct die_info *
f2f0e013 19191die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19192{
f2f0e013
DJ
19193 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19194 *spec_cu);
63d06c5c 19195
edb3359d
DJ
19196 if (spec_attr == NULL)
19197 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19198
63d06c5c
DC
19199 if (spec_attr == NULL)
19200 return NULL;
19201 else
f2f0e013 19202 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19203}
c906108c 19204
527f3840
JK
19205/* Stub for free_line_header to match void * callback types. */
19206
19207static void
19208free_line_header_voidp (void *arg)
19209{
9a3c8263 19210 struct line_header *lh = (struct line_header *) arg;
527f3840 19211
fff8551c 19212 delete lh;
527f3840
JK
19213}
19214
83769d0b 19215/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19216
19217static struct dwarf2_section_info *
19218get_debug_line_section (struct dwarf2_cu *cu)
19219{
19220 struct dwarf2_section_info *section;
518817b3
SM
19221 struct dwarf2_per_objfile *dwarf2_per_objfile
19222 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19223
19224 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19225 DWO file. */
19226 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19227 section = &cu->dwo_unit->dwo_file->sections.line;
19228 else if (cu->per_cu->is_dwz)
19229 {
ed2dc618 19230 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19231
19232 section = &dwz->line;
19233 }
19234 else
19235 section = &dwarf2_per_objfile->line;
19236
19237 return section;
19238}
19239
43988095
JK
19240/* Read directory or file name entry format, starting with byte of
19241 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19242 entries count and the entries themselves in the described entry
19243 format. */
19244
19245static void
ed2dc618
SM
19246read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19247 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19248 struct line_header *lh,
19249 const struct comp_unit_head *cu_header,
19250 void (*callback) (struct line_header *lh,
19251 const char *name,
ecfb656c 19252 dir_index d_index,
43988095
JK
19253 unsigned int mod_time,
19254 unsigned int length))
19255{
19256 gdb_byte format_count, formati;
19257 ULONGEST data_count, datai;
19258 const gdb_byte *buf = *bufp;
19259 const gdb_byte *format_header_data;
43988095
JK
19260 unsigned int bytes_read;
19261
19262 format_count = read_1_byte (abfd, buf);
19263 buf += 1;
19264 format_header_data = buf;
19265 for (formati = 0; formati < format_count; formati++)
19266 {
19267 read_unsigned_leb128 (abfd, buf, &bytes_read);
19268 buf += bytes_read;
19269 read_unsigned_leb128 (abfd, buf, &bytes_read);
19270 buf += bytes_read;
19271 }
19272
19273 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19274 buf += bytes_read;
19275 for (datai = 0; datai < data_count; datai++)
19276 {
19277 const gdb_byte *format = format_header_data;
19278 struct file_entry fe;
19279
43988095
JK
19280 for (formati = 0; formati < format_count; formati++)
19281 {
ecfb656c 19282 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19283 format += bytes_read;
43988095 19284
ecfb656c 19285 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19286 format += bytes_read;
ecfb656c
PA
19287
19288 gdb::optional<const char *> string;
19289 gdb::optional<unsigned int> uint;
19290
43988095
JK
19291 switch (form)
19292 {
19293 case DW_FORM_string:
ecfb656c 19294 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19295 buf += bytes_read;
19296 break;
19297
19298 case DW_FORM_line_strp:
ed2dc618
SM
19299 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19300 abfd, buf,
ecfb656c
PA
19301 cu_header,
19302 &bytes_read));
43988095
JK
19303 buf += bytes_read;
19304 break;
19305
19306 case DW_FORM_data1:
ecfb656c 19307 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19308 buf += 1;
19309 break;
19310
19311 case DW_FORM_data2:
ecfb656c 19312 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19313 buf += 2;
19314 break;
19315
19316 case DW_FORM_data4:
ecfb656c 19317 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19318 buf += 4;
19319 break;
19320
19321 case DW_FORM_data8:
ecfb656c 19322 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19323 buf += 8;
19324 break;
19325
7ba99d21
AT
19326 case DW_FORM_data16:
19327 /* This is used for MD5, but file_entry does not record MD5s. */
19328 buf += 16;
19329 break;
19330
43988095 19331 case DW_FORM_udata:
ecfb656c 19332 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19333 buf += bytes_read;
19334 break;
19335
19336 case DW_FORM_block:
19337 /* It is valid only for DW_LNCT_timestamp which is ignored by
19338 current GDB. */
19339 break;
19340 }
ecfb656c
PA
19341
19342 switch (content_type)
19343 {
19344 case DW_LNCT_path:
19345 if (string.has_value ())
19346 fe.name = *string;
19347 break;
19348 case DW_LNCT_directory_index:
19349 if (uint.has_value ())
19350 fe.d_index = (dir_index) *uint;
19351 break;
19352 case DW_LNCT_timestamp:
19353 if (uint.has_value ())
19354 fe.mod_time = *uint;
19355 break;
19356 case DW_LNCT_size:
19357 if (uint.has_value ())
19358 fe.length = *uint;
19359 break;
19360 case DW_LNCT_MD5:
19361 break;
19362 default:
b98664d3 19363 complaint (_("Unknown format content type %s"),
ecfb656c
PA
19364 pulongest (content_type));
19365 }
43988095
JK
19366 }
19367
ecfb656c 19368 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
19369 }
19370
19371 *bufp = buf;
19372}
19373
debd256d 19374/* Read the statement program header starting at OFFSET in
3019eac3 19375 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19376 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19377 Returns NULL if there is a problem reading the header, e.g., if it
19378 has a version we don't understand.
debd256d
JB
19379
19380 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19381 the returned object point into the dwarf line section buffer,
19382 and must not be freed. */
ae2de4f8 19383
fff8551c 19384static line_header_up
9c541725 19385dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19386{
d521ce57 19387 const gdb_byte *line_ptr;
c764a876 19388 unsigned int bytes_read, offset_size;
debd256d 19389 int i;
d521ce57 19390 const char *cur_dir, *cur_file;
3019eac3
DE
19391 struct dwarf2_section_info *section;
19392 bfd *abfd;
518817b3
SM
19393 struct dwarf2_per_objfile *dwarf2_per_objfile
19394 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19395
36586728 19396 section = get_debug_line_section (cu);
96b79293 19397 section->read (dwarf2_per_objfile->objfile);
3019eac3 19398 if (section->buffer == NULL)
debd256d 19399 {
3019eac3 19400 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19401 complaint (_("missing .debug_line.dwo section"));
3019eac3 19402 else
b98664d3 19403 complaint (_("missing .debug_line section"));
debd256d
JB
19404 return 0;
19405 }
19406
fceca515
DE
19407 /* We can't do this until we know the section is non-empty.
19408 Only then do we know we have such a section. */
96b79293 19409 abfd = section->get_bfd_owner ();
fceca515 19410
a738430d
MK
19411 /* Make sure that at least there's room for the total_length field.
19412 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 19413 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 19414 {
4d3c2250 19415 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19416 return 0;
19417 }
19418
fff8551c 19419 line_header_up lh (new line_header ());
debd256d 19420
9c541725 19421 lh->sect_off = sect_off;
527f3840
JK
19422 lh->offset_in_dwz = cu->per_cu->is_dwz;
19423
9c541725 19424 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 19425
a738430d 19426 /* Read in the header. */
6e70227d 19427 lh->total_length =
c764a876
DE
19428 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19429 &bytes_read, &offset_size);
debd256d 19430 line_ptr += bytes_read;
7ba99d21
AT
19431
19432 const gdb_byte *start_here = line_ptr;
19433
3019eac3 19434 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 19435 {
4d3c2250 19436 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19437 return 0;
19438 }
7ba99d21 19439 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
19440 lh->version = read_2_bytes (abfd, line_ptr);
19441 line_ptr += 2;
43988095 19442 if (lh->version > 5)
cd366ee8
DE
19443 {
19444 /* This is a version we don't understand. The format could have
19445 changed in ways we don't handle properly so just punt. */
b98664d3 19446 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
19447 return NULL;
19448 }
43988095
JK
19449 if (lh->version >= 5)
19450 {
19451 gdb_byte segment_selector_size;
19452
19453 /* Skip address size. */
19454 read_1_byte (abfd, line_ptr);
19455 line_ptr += 1;
19456
19457 segment_selector_size = read_1_byte (abfd, line_ptr);
19458 line_ptr += 1;
19459 if (segment_selector_size != 0)
19460 {
b98664d3 19461 complaint (_("unsupported segment selector size %u "
43988095
JK
19462 "in .debug_line section"),
19463 segment_selector_size);
19464 return NULL;
19465 }
19466 }
24aa364d 19467 lh->header_length = read_offset (abfd, line_ptr, offset_size);
c764a876 19468 line_ptr += offset_size;
7ba99d21 19469 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
19470 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19471 line_ptr += 1;
2dc7f7b3
TT
19472 if (lh->version >= 4)
19473 {
19474 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19475 line_ptr += 1;
19476 }
19477 else
19478 lh->maximum_ops_per_instruction = 1;
19479
19480 if (lh->maximum_ops_per_instruction == 0)
19481 {
19482 lh->maximum_ops_per_instruction = 1;
b98664d3 19483 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 19484 "in `.debug_line' section"));
2dc7f7b3
TT
19485 }
19486
debd256d
JB
19487 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19488 line_ptr += 1;
19489 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19490 line_ptr += 1;
19491 lh->line_range = read_1_byte (abfd, line_ptr);
19492 line_ptr += 1;
19493 lh->opcode_base = read_1_byte (abfd, line_ptr);
19494 line_ptr += 1;
fff8551c 19495 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
19496
19497 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19498 for (i = 1; i < lh->opcode_base; ++i)
19499 {
19500 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19501 line_ptr += 1;
19502 }
19503
43988095 19504 if (lh->version >= 5)
debd256d 19505 {
43988095 19506 /* Read directory table. */
ed2dc618
SM
19507 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19508 &cu->header,
b926417a 19509 [] (struct line_header *header, const char *name,
ecfb656c 19510 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19511 unsigned int length)
19512 {
b926417a 19513 header->add_include_dir (name);
fff8551c 19514 });
debd256d 19515
43988095 19516 /* Read file name table. */
ed2dc618
SM
19517 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19518 &cu->header,
b926417a 19519 [] (struct line_header *header, const char *name,
ecfb656c 19520 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19521 unsigned int length)
19522 {
b926417a 19523 header->add_file_name (name, d_index, mod_time, length);
fff8551c 19524 });
43988095
JK
19525 }
19526 else
debd256d 19527 {
43988095
JK
19528 /* Read directory table. */
19529 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19530 {
19531 line_ptr += bytes_read;
fff8551c 19532 lh->add_include_dir (cur_dir);
43988095 19533 }
debd256d
JB
19534 line_ptr += bytes_read;
19535
43988095
JK
19536 /* Read file name table. */
19537 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19538 {
ecfb656c
PA
19539 unsigned int mod_time, length;
19540 dir_index d_index;
43988095
JK
19541
19542 line_ptr += bytes_read;
ecfb656c 19543 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
19544 line_ptr += bytes_read;
19545 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19546 line_ptr += bytes_read;
19547 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19548 line_ptr += bytes_read;
19549
ecfb656c 19550 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
19551 }
19552 line_ptr += bytes_read;
debd256d 19553 }
debd256d 19554
3019eac3 19555 if (line_ptr > (section->buffer + section->size))
b98664d3 19556 complaint (_("line number info header doesn't "
3e43a32a 19557 "fit in `.debug_line' section"));
debd256d 19558
debd256d
JB
19559 return lh;
19560}
c906108c 19561
c6da4cef 19562/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19563 Return the file name of the psymtab for the given file_entry.
c6da4cef 19564 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19565 If space for the result is malloc'd, *NAME_HOLDER will be set.
19566 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19567
d521ce57 19568static const char *
7ba99d21 19569psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19570 const dwarf2_psymtab *pst,
c89b44cd
TT
19571 const char *comp_dir,
19572 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19573{
d521ce57
TT
19574 const char *include_name = fe.name;
19575 const char *include_name_to_compare = include_name;
72b9f47f 19576 const char *pst_filename;
c6da4cef
DE
19577 int file_is_pst;
19578
8c43009f 19579 const char *dir_name = fe.include_dir (lh);
c6da4cef 19580
c89b44cd 19581 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19582 if (!IS_ABSOLUTE_PATH (include_name)
19583 && (dir_name != NULL || comp_dir != NULL))
19584 {
19585 /* Avoid creating a duplicate psymtab for PST.
19586 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19587 Before we do the comparison, however, we need to account
19588 for DIR_NAME and COMP_DIR.
19589 First prepend dir_name (if non-NULL). If we still don't
19590 have an absolute path prepend comp_dir (if non-NULL).
19591 However, the directory we record in the include-file's
19592 psymtab does not contain COMP_DIR (to match the
19593 corresponding symtab(s)).
19594
19595 Example:
19596
19597 bash$ cd /tmp
19598 bash$ gcc -g ./hello.c
19599 include_name = "hello.c"
19600 dir_name = "."
19601 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19602 DW_AT_name = "./hello.c"
19603
19604 */
c6da4cef
DE
19605
19606 if (dir_name != NULL)
19607 {
c89b44cd
TT
19608 name_holder->reset (concat (dir_name, SLASH_STRING,
19609 include_name, (char *) NULL));
19610 include_name = name_holder->get ();
c6da4cef 19611 include_name_to_compare = include_name;
c6da4cef
DE
19612 }
19613 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19614 {
c89b44cd
TT
19615 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19616 include_name, (char *) NULL));
19617 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19618 }
19619 }
19620
19621 pst_filename = pst->filename;
c89b44cd 19622 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19623 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19624 {
c89b44cd
TT
19625 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19626 pst_filename, (char *) NULL));
19627 pst_filename = copied_name.get ();
c6da4cef
DE
19628 }
19629
1e3fad37 19630 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19631
c6da4cef
DE
19632 if (file_is_pst)
19633 return NULL;
19634 return include_name;
19635}
19636
d9b3de22
DE
19637/* State machine to track the state of the line number program. */
19638
6f77053d 19639class lnp_state_machine
d9b3de22 19640{
6f77053d
PA
19641public:
19642 /* Initialize a machine state for the start of a line number
19643 program. */
804d2729
TT
19644 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19645 bool record_lines_p);
6f77053d 19646
8c43009f
PA
19647 file_entry *current_file ()
19648 {
19649 /* lh->file_names is 0-based, but the file name numbers in the
19650 statement program are 1-based. */
6f77053d
PA
19651 return m_line_header->file_name_at (m_file);
19652 }
19653
19654 /* Record the line in the state machine. END_SEQUENCE is true if
19655 we're processing the end of a sequence. */
19656 void record_line (bool end_sequence);
19657
7ab6656f
OJ
19658 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19659 nop-out rest of the lines in this sequence. */
6f77053d
PA
19660 void check_line_address (struct dwarf2_cu *cu,
19661 const gdb_byte *line_ptr,
7ab6656f 19662 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19663
19664 void handle_set_discriminator (unsigned int discriminator)
19665 {
19666 m_discriminator = discriminator;
19667 m_line_has_non_zero_discriminator |= discriminator != 0;
19668 }
19669
19670 /* Handle DW_LNE_set_address. */
19671 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19672 {
19673 m_op_index = 0;
19674 address += baseaddr;
19675 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19676 }
19677
19678 /* Handle DW_LNS_advance_pc. */
19679 void handle_advance_pc (CORE_ADDR adjust);
19680
19681 /* Handle a special opcode. */
19682 void handle_special_opcode (unsigned char op_code);
19683
19684 /* Handle DW_LNS_advance_line. */
19685 void handle_advance_line (int line_delta)
19686 {
19687 advance_line (line_delta);
19688 }
19689
19690 /* Handle DW_LNS_set_file. */
19691 void handle_set_file (file_name_index file);
19692
19693 /* Handle DW_LNS_negate_stmt. */
19694 void handle_negate_stmt ()
19695 {
19696 m_is_stmt = !m_is_stmt;
19697 }
19698
19699 /* Handle DW_LNS_const_add_pc. */
19700 void handle_const_add_pc ();
19701
19702 /* Handle DW_LNS_fixed_advance_pc. */
19703 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19704 {
19705 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19706 m_op_index = 0;
19707 }
19708
19709 /* Handle DW_LNS_copy. */
19710 void handle_copy ()
19711 {
19712 record_line (false);
19713 m_discriminator = 0;
19714 }
19715
19716 /* Handle DW_LNE_end_sequence. */
19717 void handle_end_sequence ()
19718 {
804d2729 19719 m_currently_recording_lines = true;
6f77053d
PA
19720 }
19721
19722private:
19723 /* Advance the line by LINE_DELTA. */
19724 void advance_line (int line_delta)
19725 {
19726 m_line += line_delta;
19727
19728 if (line_delta != 0)
19729 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19730 }
19731
804d2729
TT
19732 struct dwarf2_cu *m_cu;
19733
6f77053d
PA
19734 gdbarch *m_gdbarch;
19735
19736 /* True if we're recording lines.
19737 Otherwise we're building partial symtabs and are just interested in
19738 finding include files mentioned by the line number program. */
19739 bool m_record_lines_p;
19740
8c43009f 19741 /* The line number header. */
6f77053d 19742 line_header *m_line_header;
8c43009f 19743
6f77053d
PA
19744 /* These are part of the standard DWARF line number state machine,
19745 and initialized according to the DWARF spec. */
d9b3de22 19746
6f77053d 19747 unsigned char m_op_index = 0;
7ba99d21
AT
19748 /* The line table index of the current file. */
19749 file_name_index m_file = 1;
6f77053d
PA
19750 unsigned int m_line = 1;
19751
19752 /* These are initialized in the constructor. */
19753
19754 CORE_ADDR m_address;
19755 bool m_is_stmt;
19756 unsigned int m_discriminator;
d9b3de22
DE
19757
19758 /* Additional bits of state we need to track. */
19759
19760 /* The last file that we called dwarf2_start_subfile for.
19761 This is only used for TLLs. */
6f77053d 19762 unsigned int m_last_file = 0;
d9b3de22 19763 /* The last file a line number was recorded for. */
6f77053d 19764 struct subfile *m_last_subfile = NULL;
d9b3de22 19765
804d2729
TT
19766 /* When true, record the lines we decode. */
19767 bool m_currently_recording_lines = false;
d9b3de22
DE
19768
19769 /* The last line number that was recorded, used to coalesce
19770 consecutive entries for the same line. This can happen, for
19771 example, when discriminators are present. PR 17276. */
6f77053d
PA
19772 unsigned int m_last_line = 0;
19773 bool m_line_has_non_zero_discriminator = false;
8c43009f 19774};
d9b3de22 19775
6f77053d
PA
19776void
19777lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19778{
19779 CORE_ADDR addr_adj = (((m_op_index + adjust)
19780 / m_line_header->maximum_ops_per_instruction)
19781 * m_line_header->minimum_instruction_length);
19782 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19783 m_op_index = ((m_op_index + adjust)
19784 % m_line_header->maximum_ops_per_instruction);
19785}
d9b3de22 19786
6f77053d
PA
19787void
19788lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19789{
6f77053d 19790 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19791 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19792 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19793 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19794 / m_line_header->maximum_ops_per_instruction)
19795 * m_line_header->minimum_instruction_length);
19796 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19797 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19798 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19799
258bf0ee 19800 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19801 advance_line (line_delta);
19802 record_line (false);
19803 m_discriminator = 0;
19804}
d9b3de22 19805
6f77053d
PA
19806void
19807lnp_state_machine::handle_set_file (file_name_index file)
19808{
19809 m_file = file;
19810
19811 const file_entry *fe = current_file ();
19812 if (fe == NULL)
19813 dwarf2_debug_line_missing_file_complaint ();
19814 else if (m_record_lines_p)
19815 {
19816 const char *dir = fe->include_dir (m_line_header);
19817
c24bdb02 19818 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19819 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19820 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19821 }
19822}
19823
19824void
19825lnp_state_machine::handle_const_add_pc ()
19826{
19827 CORE_ADDR adjust
19828 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19829
19830 CORE_ADDR addr_adj
19831 = (((m_op_index + adjust)
19832 / m_line_header->maximum_ops_per_instruction)
19833 * m_line_header->minimum_instruction_length);
19834
19835 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19836 m_op_index = ((m_op_index + adjust)
19837 % m_line_header->maximum_ops_per_instruction);
19838}
d9b3de22 19839
a05a36a5
DE
19840/* Return non-zero if we should add LINE to the line number table.
19841 LINE is the line to add, LAST_LINE is the last line that was added,
19842 LAST_SUBFILE is the subfile for LAST_LINE.
19843 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19844 had a non-zero discriminator.
19845
19846 We have to be careful in the presence of discriminators.
19847 E.g., for this line:
19848
19849 for (i = 0; i < 100000; i++);
19850
19851 clang can emit four line number entries for that one line,
19852 each with a different discriminator.
19853 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19854
19855 However, we want gdb to coalesce all four entries into one.
19856 Otherwise the user could stepi into the middle of the line and
19857 gdb would get confused about whether the pc really was in the
19858 middle of the line.
19859
19860 Things are further complicated by the fact that two consecutive
19861 line number entries for the same line is a heuristic used by gcc
19862 to denote the end of the prologue. So we can't just discard duplicate
19863 entries, we have to be selective about it. The heuristic we use is
19864 that we only collapse consecutive entries for the same line if at least
19865 one of those entries has a non-zero discriminator. PR 17276.
19866
19867 Note: Addresses in the line number state machine can never go backwards
19868 within one sequence, thus this coalescing is ok. */
19869
19870static int
804d2729
TT
19871dwarf_record_line_p (struct dwarf2_cu *cu,
19872 unsigned int line, unsigned int last_line,
a05a36a5
DE
19873 int line_has_non_zero_discriminator,
19874 struct subfile *last_subfile)
19875{
c24bdb02 19876 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19877 return 1;
19878 if (line != last_line)
19879 return 1;
19880 /* Same line for the same file that we've seen already.
19881 As a last check, for pr 17276, only record the line if the line
19882 has never had a non-zero discriminator. */
19883 if (!line_has_non_zero_discriminator)
19884 return 1;
19885 return 0;
19886}
19887
804d2729
TT
19888/* Use the CU's builder to record line number LINE beginning at
19889 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19890
19891static void
d9b3de22
DE
19892dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19893 unsigned int line, CORE_ADDR address,
804d2729 19894 struct dwarf2_cu *cu)
252a6764
DE
19895{
19896 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19897
27e0867f
DE
19898 if (dwarf_line_debug)
19899 {
19900 fprintf_unfiltered (gdb_stdlog,
19901 "Recording line %u, file %s, address %s\n",
19902 line, lbasename (subfile->name),
19903 paddress (gdbarch, address));
19904 }
19905
804d2729 19906 if (cu != nullptr)
c24bdb02 19907 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
19908}
19909
19910/* Subroutine of dwarf_decode_lines_1 to simplify it.
19911 Mark the end of a set of line number records.
d9b3de22 19912 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19913 If SUBFILE is NULL the request is ignored. */
19914
19915static void
19916dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19917 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19918{
27e0867f
DE
19919 if (subfile == NULL)
19920 return;
19921
19922 if (dwarf_line_debug)
19923 {
19924 fprintf_unfiltered (gdb_stdlog,
19925 "Finishing current line, file %s, address %s\n",
19926 lbasename (subfile->name),
19927 paddress (gdbarch, address));
19928 }
19929
804d2729 19930 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
19931}
19932
6f77053d
PA
19933void
19934lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19935{
d9b3de22
DE
19936 if (dwarf_line_debug)
19937 {
19938 fprintf_unfiltered (gdb_stdlog,
19939 "Processing actual line %u: file %u,"
94a72be7 19940 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19941 m_line, m_file,
6f77053d 19942 paddress (m_gdbarch, m_address),
94a72be7
AB
19943 m_is_stmt, m_discriminator,
19944 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19945 }
19946
6f77053d 19947 file_entry *fe = current_file ();
8c43009f
PA
19948
19949 if (fe == NULL)
d9b3de22
DE
19950 dwarf2_debug_line_missing_file_complaint ();
19951 /* For now we ignore lines not starting on an instruction boundary.
19952 But not when processing end_sequence for compatibility with the
19953 previous version of the code. */
6f77053d 19954 else if (m_op_index == 0 || end_sequence)
d9b3de22 19955 {
8c43009f 19956 fe->included_p = 1;
94a72be7
AB
19957 if (m_record_lines_p
19958 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 19959 {
c24bdb02 19960 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 19961 || end_sequence)
d9b3de22 19962 {
804d2729
TT
19963 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19964 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
19965 }
19966
19967 if (!end_sequence)
19968 {
804d2729 19969 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
19970 m_line_has_non_zero_discriminator,
19971 m_last_subfile))
d9b3de22 19972 {
c24bdb02 19973 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 19974 dwarf_record_line_1 (m_gdbarch,
c24bdb02 19975 builder->get_current_subfile (),
6f77053d 19976 m_line, m_address,
804d2729 19977 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 19978 }
c24bdb02 19979 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19980 m_last_line = m_line;
d9b3de22
DE
19981 }
19982 }
19983 }
19984}
19985
804d2729
TT
19986lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19987 line_header *lh, bool record_lines_p)
d9b3de22 19988{
804d2729 19989 m_cu = cu;
6f77053d
PA
19990 m_gdbarch = arch;
19991 m_record_lines_p = record_lines_p;
19992 m_line_header = lh;
d9b3de22 19993
804d2729 19994 m_currently_recording_lines = true;
d9b3de22 19995
d9b3de22
DE
19996 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19997 was a line entry for it so that the backend has a chance to adjust it
19998 and also record it in case it needs it. This is currently used by MIPS
19999 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20000 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20001 m_is_stmt = lh->default_is_stmt;
20002 m_discriminator = 0;
252a6764
DE
20003}
20004
6f77053d
PA
20005void
20006lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20007 const gdb_byte *line_ptr,
7ab6656f 20008 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20009{
7ab6656f
OJ
20010 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20011 the pc range of the CU. However, we restrict the test to only ADDRESS
20012 values of zero to preserve GDB's previous behaviour which is to handle
20013 the specific case of a function being GC'd by the linker. */
924c2928 20014
7ab6656f 20015 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20016 {
20017 /* This line table is for a function which has been
20018 GCd by the linker. Ignore it. PR gdb/12528 */
20019
518817b3 20020 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20021 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20022
b98664d3 20023 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20024 line_offset, objfile_name (objfile));
804d2729
TT
20025 m_currently_recording_lines = false;
20026 /* Note: m_currently_recording_lines is left as false until we see
20027 DW_LNE_end_sequence. */
924c2928
DE
20028 }
20029}
20030
f3f5162e 20031/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20032 Process the line number information in LH.
20033 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20034 program in order to set included_p for every referenced header. */
debd256d 20035
c906108c 20036static void
43f3e411
DE
20037dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20038 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20039{
d521ce57
TT
20040 const gdb_byte *line_ptr, *extended_end;
20041 const gdb_byte *line_end;
a8c50c1f 20042 unsigned int bytes_read, extended_len;
699ca60a 20043 unsigned char op_code, extended_op;
e142c38c 20044 CORE_ADDR baseaddr;
518817b3 20045 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20046 bfd *abfd = objfile->obfd;
fbf65064 20047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20048 /* True if we're recording line info (as opposed to building partial
20049 symtabs and just interested in finding include files mentioned by
20050 the line number program). */
20051 bool record_lines_p = !decode_for_pst_p;
e142c38c 20052
b3b3bada 20053 baseaddr = objfile->text_section_offset ();
c906108c 20054
debd256d
JB
20055 line_ptr = lh->statement_program_start;
20056 line_end = lh->statement_program_end;
c906108c
SS
20057
20058 /* Read the statement sequences until there's nothing left. */
20059 while (line_ptr < line_end)
20060 {
6f77053d
PA
20061 /* The DWARF line number program state machine. Reset the state
20062 machine at the start of each sequence. */
804d2729 20063 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20064 bool end_sequence = false;
d9b3de22 20065
8c43009f 20066 if (record_lines_p)
c906108c 20067 {
8c43009f
PA
20068 /* Start a subfile for the current file of the state
20069 machine. */
20070 const file_entry *fe = state_machine.current_file ();
20071
20072 if (fe != NULL)
804d2729 20073 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20074 }
20075
a738430d 20076 /* Decode the table. */
d9b3de22 20077 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20078 {
20079 op_code = read_1_byte (abfd, line_ptr);
20080 line_ptr += 1;
9aa1fe7e 20081
debd256d 20082 if (op_code >= lh->opcode_base)
6e70227d 20083 {
8e07a239 20084 /* Special opcode. */
6f77053d 20085 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20086 }
20087 else switch (op_code)
c906108c
SS
20088 {
20089 case DW_LNS_extended_op:
3e43a32a
MS
20090 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20091 &bytes_read);
473b7be6 20092 line_ptr += bytes_read;
a8c50c1f 20093 extended_end = line_ptr + extended_len;
c906108c
SS
20094 extended_op = read_1_byte (abfd, line_ptr);
20095 line_ptr += 1;
20096 switch (extended_op)
20097 {
20098 case DW_LNE_end_sequence:
6f77053d
PA
20099 state_machine.handle_end_sequence ();
20100 end_sequence = true;
c906108c
SS
20101 break;
20102 case DW_LNE_set_address:
d9b3de22
DE
20103 {
20104 CORE_ADDR address
c8a7a66f 20105 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20106 line_ptr += bytes_read;
6f77053d
PA
20107
20108 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20109 lowpc - baseaddr, address);
6f77053d 20110 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20111 }
c906108c
SS
20112 break;
20113 case DW_LNE_define_file:
debd256d 20114 {
d521ce57 20115 const char *cur_file;
ecfb656c
PA
20116 unsigned int mod_time, length;
20117 dir_index dindex;
6e70227d 20118
3e43a32a
MS
20119 cur_file = read_direct_string (abfd, line_ptr,
20120 &bytes_read);
debd256d 20121 line_ptr += bytes_read;
ecfb656c 20122 dindex = (dir_index)
debd256d
JB
20123 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20124 line_ptr += bytes_read;
20125 mod_time =
20126 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20127 line_ptr += bytes_read;
20128 length =
20129 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20130 line_ptr += bytes_read;
ecfb656c 20131 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20132 }
c906108c 20133 break;
d0c6ba3d 20134 case DW_LNE_set_discriminator:
6f77053d
PA
20135 {
20136 /* The discriminator is not interesting to the
20137 debugger; just ignore it. We still need to
20138 check its value though:
20139 if there are consecutive entries for the same
20140 (non-prologue) line we want to coalesce them.
20141 PR 17276. */
20142 unsigned int discr
20143 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20144 line_ptr += bytes_read;
20145
20146 state_machine.handle_set_discriminator (discr);
20147 }
d0c6ba3d 20148 break;
c906108c 20149 default:
b98664d3 20150 complaint (_("mangled .debug_line section"));
debd256d 20151 return;
c906108c 20152 }
a8c50c1f
DJ
20153 /* Make sure that we parsed the extended op correctly. If e.g.
20154 we expected a different address size than the producer used,
20155 we may have read the wrong number of bytes. */
20156 if (line_ptr != extended_end)
20157 {
b98664d3 20158 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20159 return;
20160 }
c906108c
SS
20161 break;
20162 case DW_LNS_copy:
6f77053d 20163 state_machine.handle_copy ();
c906108c
SS
20164 break;
20165 case DW_LNS_advance_pc:
2dc7f7b3
TT
20166 {
20167 CORE_ADDR adjust
20168 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20169 line_ptr += bytes_read;
6f77053d
PA
20170
20171 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20172 }
c906108c
SS
20173 break;
20174 case DW_LNS_advance_line:
a05a36a5
DE
20175 {
20176 int line_delta
20177 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20178 line_ptr += bytes_read;
6f77053d
PA
20179
20180 state_machine.handle_advance_line (line_delta);
a05a36a5 20181 }
c906108c
SS
20182 break;
20183 case DW_LNS_set_file:
d9b3de22 20184 {
6f77053d 20185 file_name_index file
ecfb656c
PA
20186 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20187 &bytes_read);
d9b3de22 20188 line_ptr += bytes_read;
8c43009f 20189
6f77053d 20190 state_machine.handle_set_file (file);
d9b3de22 20191 }
c906108c
SS
20192 break;
20193 case DW_LNS_set_column:
0ad93d4f 20194 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20195 line_ptr += bytes_read;
20196 break;
20197 case DW_LNS_negate_stmt:
6f77053d 20198 state_machine.handle_negate_stmt ();
c906108c
SS
20199 break;
20200 case DW_LNS_set_basic_block:
c906108c 20201 break;
c2c6d25f
JM
20202 /* Add to the address register of the state machine the
20203 address increment value corresponding to special opcode
a738430d
MK
20204 255. I.e., this value is scaled by the minimum
20205 instruction length since special opcode 255 would have
b021a221 20206 scaled the increment. */
c906108c 20207 case DW_LNS_const_add_pc:
6f77053d 20208 state_machine.handle_const_add_pc ();
c906108c
SS
20209 break;
20210 case DW_LNS_fixed_advance_pc:
3e29f34a 20211 {
6f77053d 20212 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20213 line_ptr += 2;
6f77053d
PA
20214
20215 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20216 }
c906108c 20217 break;
9aa1fe7e 20218 default:
a738430d
MK
20219 {
20220 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20221 int i;
a738430d 20222
debd256d 20223 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20224 {
20225 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20226 line_ptr += bytes_read;
20227 }
20228 }
c906108c
SS
20229 }
20230 }
d9b3de22
DE
20231
20232 if (!end_sequence)
20233 dwarf2_debug_line_missing_end_sequence_complaint ();
20234
20235 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20236 in which case we still finish recording the last line). */
6f77053d 20237 state_machine.record_line (true);
c906108c 20238 }
f3f5162e
DE
20239}
20240
20241/* Decode the Line Number Program (LNP) for the given line_header
20242 structure and CU. The actual information extracted and the type
20243 of structures created from the LNP depends on the value of PST.
20244
20245 1. If PST is NULL, then this procedure uses the data from the program
20246 to create all necessary symbol tables, and their linetables.
20247
20248 2. If PST is not NULL, this procedure reads the program to determine
20249 the list of files included by the unit represented by PST, and
20250 builds all the associated partial symbol tables.
20251
20252 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20253 It is used for relative paths in the line table.
20254 NOTE: When processing partial symtabs (pst != NULL),
20255 comp_dir == pst->dirname.
20256
20257 NOTE: It is important that psymtabs have the same file name (via strcmp)
20258 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20259 symtab we don't use it in the name of the psymtabs we create.
20260 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20261 A good testcase for this is mb-inline.exp.
20262
527f3840
JK
20263 LOWPC is the lowest address in CU (or 0 if not known).
20264
20265 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20266 for its PC<->lines mapping information. Otherwise only the filename
20267 table is read in. */
f3f5162e
DE
20268
20269static void
20270dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20271 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20272 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20273{
518817b3 20274 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20275 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20276
527f3840
JK
20277 if (decode_mapping)
20278 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20279
20280 if (decode_for_pst_p)
20281 {
aaa75496
JB
20282 /* Now that we're done scanning the Line Header Program, we can
20283 create the psymtab of each included file. */
7ba99d21
AT
20284 for (auto &file_entry : lh->file_names ())
20285 if (file_entry.included_p == 1)
aaa75496 20286 {
c89b44cd 20287 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20288 const char *include_name =
7ba99d21
AT
20289 psymtab_include_file_name (lh, file_entry, pst,
20290 comp_dir, &name_holder);
c6da4cef 20291 if (include_name != NULL)
aaa75496
JB
20292 dwarf2_create_include_psymtab (include_name, pst, objfile);
20293 }
20294 }
cb1df416
DJ
20295 else
20296 {
20297 /* Make sure a symtab is created for every file, even files
20298 which contain only variables (i.e. no code with associated
20299 line numbers). */
c24bdb02
KS
20300 buildsym_compunit *builder = cu->get_builder ();
20301 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20302
7ba99d21 20303 for (auto &fe : lh->file_names ())
cb1df416 20304 {
804d2729 20305 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20306 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20307 {
c24bdb02 20308 builder->get_current_subfile ()->symtab
804d2729 20309 = allocate_symtab (cust,
c24bdb02 20310 builder->get_current_subfile ()->name);
43f3e411 20311 }
c24bdb02 20312 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20313 }
20314 }
c906108c
SS
20315}
20316
20317/* Start a subfile for DWARF. FILENAME is the name of the file and
20318 DIRNAME the name of the source directory which contains FILENAME
4d663531 20319 or NULL if not known.
c906108c
SS
20320 This routine tries to keep line numbers from identical absolute and
20321 relative file names in a common subfile.
20322
20323 Using the `list' example from the GDB testsuite, which resides in
20324 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20325 of /srcdir/list0.c yields the following debugging information for list0.c:
20326
c5aa993b 20327 DW_AT_name: /srcdir/list0.c
4d663531 20328 DW_AT_comp_dir: /compdir
357e46e7 20329 files.files[0].name: list0.h
c5aa993b 20330 files.files[0].dir: /srcdir
357e46e7 20331 files.files[1].name: list0.c
c5aa993b 20332 files.files[1].dir: /srcdir
c906108c
SS
20333
20334 The line number information for list0.c has to end up in a single
4f1520fb
FR
20335 subfile, so that `break /srcdir/list0.c:1' works as expected.
20336 start_subfile will ensure that this happens provided that we pass the
20337 concatenation of files.files[1].dir and files.files[1].name as the
20338 subfile's name. */
c906108c
SS
20339
20340static void
804d2729
TT
20341dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20342 const char *dirname)
c906108c 20343{
43816ebc 20344 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20345
4d663531 20346 /* In order not to lose the line information directory,
4f1520fb
FR
20347 we concatenate it to the filename when it makes sense.
20348 Note that the Dwarf3 standard says (speaking of filenames in line
20349 information): ``The directory index is ignored for file names
20350 that represent full path names''. Thus ignoring dirname in the
20351 `else' branch below isn't an issue. */
c906108c 20352
d5166ae1 20353 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20354 {
43816ebc
TT
20355 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20356 filename = copy.get ();
d521ce57 20357 }
c906108c 20358
c24bdb02 20359 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20360}
20361
804d2729
TT
20362/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20363 buildsym_compunit constructor. */
f4dc4d17 20364
c24bdb02
KS
20365struct compunit_symtab *
20366dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20367 CORE_ADDR low_pc)
f4dc4d17 20368{
c24bdb02 20369 gdb_assert (m_builder == nullptr);
43f3e411 20370
c24bdb02
KS
20371 m_builder.reset (new struct buildsym_compunit
20372 (per_cu->dwarf2_per_objfile->objfile,
20373 name, comp_dir, language, low_pc));
93b8bea4 20374
c24bdb02 20375 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20376
c24bdb02
KS
20377 get_builder ()->record_debugformat ("DWARF 2");
20378 get_builder ()->record_producer (producer);
f4dc4d17 20379
c24bdb02 20380 processing_has_namespace_info = false;
43f3e411 20381
c24bdb02 20382 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20383}
20384
4c2df51b
DJ
20385static void
20386var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20387 struct dwarf2_cu *cu)
4c2df51b 20388{
518817b3 20389 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20390 struct comp_unit_head *cu_header = &cu->header;
20391
4c2df51b
DJ
20392 /* NOTE drow/2003-01-30: There used to be a comment and some special
20393 code here to turn a symbol with DW_AT_external and a
20394 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20395 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20396 with some versions of binutils) where shared libraries could have
20397 relocations against symbols in their debug information - the
20398 minimal symbol would have the right address, but the debug info
20399 would not. It's no longer necessary, because we will explicitly
20400 apply relocations when we read in the debug information now. */
20401
20402 /* A DW_AT_location attribute with no contents indicates that a
20403 variable has been optimized away. */
4fc6c0d5 20404 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20405 {
f1e6e072 20406 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20407 return;
20408 }
20409
20410 /* Handle one degenerate form of location expression specially, to
20411 preserve GDB's previous behavior when section offsets are
336d760d
AT
20412 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20413 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20414
4fc6c0d5 20415 if (attr->form_is_block ()
3019eac3
DE
20416 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20417 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20418 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20419 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20420 && (DW_BLOCK (attr)->size
20421 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20422 {
891d2f0b 20423 unsigned int dummy;
4c2df51b 20424
3019eac3 20425 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20426 SET_SYMBOL_VALUE_ADDRESS
20427 (sym, cu->header.read_address (objfile->obfd,
20428 DW_BLOCK (attr)->data + 1,
20429 &dummy));
3019eac3 20430 else
38583298
TT
20431 SET_SYMBOL_VALUE_ADDRESS
20432 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20433 &dummy));
f1e6e072 20434 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20435 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20436 SET_SYMBOL_VALUE_ADDRESS
20437 (sym,
20438 SYMBOL_VALUE_ADDRESS (sym)
20439 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20440 return;
20441 }
20442
20443 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20444 expression evaluator, and use LOC_COMPUTED only when necessary
20445 (i.e. when the value of a register or memory location is
20446 referenced, or a thread-local block, etc.). Then again, it might
20447 not be worthwhile. I'm assuming that it isn't unless performance
20448 or memory numbers show me otherwise. */
20449
f1e6e072 20450 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20451
f1e6e072 20452 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20453 cu->has_loclist = true;
4c2df51b
DJ
20454}
20455
c906108c
SS
20456/* Given a pointer to a DWARF information entry, figure out if we need
20457 to make a symbol table entry for it, and if so, create a new entry
20458 and return a pointer to it.
20459 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20460 used the passed type.
20461 If SPACE is not NULL, use it to hold the new symbol. If it is
20462 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20463
20464static struct symbol *
5e2db402
TT
20465new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20466 struct symbol *space)
c906108c 20467{
518817b3
SM
20468 struct dwarf2_per_objfile *dwarf2_per_objfile
20469 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20470 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20471 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20472 struct symbol *sym = NULL;
15d034d0 20473 const char *name;
c906108c
SS
20474 struct attribute *attr = NULL;
20475 struct attribute *attr2 = NULL;
e142c38c 20476 CORE_ADDR baseaddr;
e37fd15a
SW
20477 struct pending **list_to_add = NULL;
20478
edb3359d 20479 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20480
b3b3bada 20481 baseaddr = objfile->text_section_offset ();
c906108c 20482
94af9270 20483 name = dwarf2_name (die, cu);
c906108c
SS
20484 if (name)
20485 {
94af9270 20486 const char *linkagename;
34eaf542 20487 int suppress_add = 0;
94af9270 20488
34eaf542
TT
20489 if (space)
20490 sym = space;
20491 else
e623cf5d 20492 sym = allocate_symbol (objfile);
c906108c 20493 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20494
20495 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20496 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20497 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20498 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20499
f55ee35c
JK
20500 /* Fortran does not have mangling standard and the mangling does differ
20501 between gfortran, iFort etc. */
20502 if (cu->language == language_fortran
468c0cbb
CB
20503 && symbol_get_demangled_name (sym) == NULL)
20504 symbol_set_demangled_name (sym,
cfc594ee 20505 dwarf2_full_name (name, die, cu),
29df156d 20506 NULL);
f55ee35c 20507
c906108c 20508 /* Default assumptions.
c5aa993b 20509 Use the passed type or decode it from the die. */
176620f1 20510 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20511 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20512 if (type != NULL)
20513 SYMBOL_TYPE (sym) = type;
20514 else
e7c27a73 20515 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20516 attr = dwarf2_attr (die,
20517 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20518 cu);
435d3d88 20519 if (attr != nullptr)
c906108c
SS
20520 {
20521 SYMBOL_LINE (sym) = DW_UNSND (attr);
20522 }
cb1df416 20523
edb3359d
DJ
20524 attr = dwarf2_attr (die,
20525 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20526 cu);
435d3d88 20527 if (attr != nullptr)
cb1df416 20528 {
ecfb656c 20529 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20530 struct file_entry *fe;
9a619af0 20531
ecfb656c
PA
20532 if (cu->line_header != NULL)
20533 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20534 else
20535 fe = NULL;
20536
20537 if (fe == NULL)
b98664d3 20538 complaint (_("file index out of range"));
8c43009f
PA
20539 else
20540 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20541 }
20542
c906108c
SS
20543 switch (die->tag)
20544 {
20545 case DW_TAG_label:
e142c38c 20546 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20547 if (attr != nullptr)
3e29f34a
MR
20548 {
20549 CORE_ADDR addr;
20550
cd6c91b4 20551 addr = attr->value_as_address ();
3e29f34a 20552 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20553 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20554 }
0f5238ed
TT
20555 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20556 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20557 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20558 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20559 break;
20560 case DW_TAG_subprogram:
20561 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20562 finish_block. */
f1e6e072 20563 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20564 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20565 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20566 || cu->language == language_ada
20567 || cu->language == language_fortran)
c906108c 20568 {
2cfa0c8d 20569 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20570 Ada and Fortran subprograms, whether marked external or
20571 not, are always stored as a global symbol, because we want
20572 to be able to access them globally. For instance, we want
20573 to be able to break on a nested subprogram without having
20574 to specify the context. */
c24bdb02 20575 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20576 }
20577 else
20578 {
e37fd15a 20579 list_to_add = cu->list_in_scope;
c906108c
SS
20580 }
20581 break;
edb3359d
DJ
20582 case DW_TAG_inlined_subroutine:
20583 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20584 finish_block. */
f1e6e072 20585 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20586 SYMBOL_INLINED (sym) = 1;
481860b3 20587 list_to_add = cu->list_in_scope;
edb3359d 20588 break;
34eaf542
TT
20589 case DW_TAG_template_value_param:
20590 suppress_add = 1;
20591 /* Fall through. */
72929c62 20592 case DW_TAG_constant:
c906108c 20593 case DW_TAG_variable:
254e6b9e 20594 case DW_TAG_member:
0963b4bd
MS
20595 /* Compilation with minimal debug info may result in
20596 variables with missing type entries. Change the
20597 misleading `void' type to something sensible. */
c906108c 20598 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20599 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20600
e142c38c 20601 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20602 /* In the case of DW_TAG_member, we should only be called for
20603 static const members. */
20604 if (die->tag == DW_TAG_member)
20605 {
3863f96c
DE
20606 /* dwarf2_add_field uses die_is_declaration,
20607 so we do the same. */
254e6b9e
DE
20608 gdb_assert (die_is_declaration (die, cu));
20609 gdb_assert (attr);
20610 }
435d3d88 20611 if (attr != nullptr)
c906108c 20612 {
e7c27a73 20613 dwarf2_const_value (attr, sym, cu);
e142c38c 20614 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20615 if (!suppress_add)
34eaf542
TT
20616 {
20617 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20618 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20619 else
e37fd15a 20620 list_to_add = cu->list_in_scope;
34eaf542 20621 }
c906108c
SS
20622 break;
20623 }
e142c38c 20624 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20625 if (attr != nullptr)
c906108c 20626 {
e7c27a73 20627 var_decode_location (attr, sym, cu);
e142c38c 20628 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20629
20630 /* Fortran explicitly imports any global symbols to the local
20631 scope by DW_TAG_common_block. */
20632 if (cu->language == language_fortran && die->parent
20633 && die->parent->tag == DW_TAG_common_block)
20634 attr2 = NULL;
20635
caac4577
JG
20636 if (SYMBOL_CLASS (sym) == LOC_STATIC
20637 && SYMBOL_VALUE_ADDRESS (sym) == 0
20638 && !dwarf2_per_objfile->has_section_at_zero)
20639 {
20640 /* When a static variable is eliminated by the linker,
20641 the corresponding debug information is not stripped
20642 out, but the variable address is set to null;
20643 do not add such variables into symbol table. */
20644 }
20645 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20646 {
4b610737
TT
20647 if (SYMBOL_CLASS (sym) == LOC_STATIC
20648 && (objfile->flags & OBJF_MAINLINE) == 0
20649 && dwarf2_per_objfile->can_copy)
20650 {
20651 /* A global static variable might be subject to
20652 copy relocation. We first check for a local
20653 minsym, though, because maybe the symbol was
20654 marked hidden, in which case this would not
20655 apply. */
20656 bound_minimal_symbol found
20657 = (lookup_minimal_symbol_linkage
987012b8 20658 (sym->linkage_name (), objfile));
4b610737
TT
20659 if (found.minsym != nullptr)
20660 sym->maybe_copied = 1;
20661 }
f55ee35c 20662
1c809c68
TT
20663 /* A variable with DW_AT_external is never static,
20664 but it may be block-scoped. */
804d2729 20665 list_to_add
c24bdb02
KS
20666 = ((cu->list_in_scope
20667 == cu->get_builder ()->get_file_symbols ())
20668 ? cu->get_builder ()->get_global_symbols ()
804d2729 20669 : cu->list_in_scope);
1c809c68 20670 }
c906108c 20671 else
e37fd15a 20672 list_to_add = cu->list_in_scope;
c906108c
SS
20673 }
20674 else
20675 {
20676 /* We do not know the address of this symbol.
c5aa993b
JM
20677 If it is an external symbol and we have type information
20678 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20679 The address of the variable will then be determined from
20680 the minimal symbol table whenever the variable is
20681 referenced. */
e142c38c 20682 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20683
20684 /* Fortran explicitly imports any global symbols to the local
20685 scope by DW_TAG_common_block. */
20686 if (cu->language == language_fortran && die->parent
20687 && die->parent->tag == DW_TAG_common_block)
20688 {
20689 /* SYMBOL_CLASS doesn't matter here because
20690 read_common_block is going to reset it. */
20691 if (!suppress_add)
20692 list_to_add = cu->list_in_scope;
20693 }
20694 else if (attr2 && (DW_UNSND (attr2) != 0)
20695 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20696 {
0fe7935b
DJ
20697 /* A variable with DW_AT_external is never static, but it
20698 may be block-scoped. */
804d2729 20699 list_to_add
c24bdb02
KS
20700 = ((cu->list_in_scope
20701 == cu->get_builder ()->get_file_symbols ())
20702 ? cu->get_builder ()->get_global_symbols ()
804d2729 20703 : cu->list_in_scope);
0fe7935b 20704
f1e6e072 20705 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20706 }
442ddf59
JK
20707 else if (!die_is_declaration (die, cu))
20708 {
20709 /* Use the default LOC_OPTIMIZED_OUT class. */
20710 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20711 if (!suppress_add)
20712 list_to_add = cu->list_in_scope;
442ddf59 20713 }
c906108c
SS
20714 }
20715 break;
20716 case DW_TAG_formal_parameter:
a60f3166
TT
20717 {
20718 /* If we are inside a function, mark this as an argument. If
20719 not, we might be looking at an argument to an inlined function
20720 when we do not have enough information to show inlined frames;
20721 pretend it's a local variable in that case so that the user can
20722 still see it. */
804d2729 20723 struct context_stack *curr
c24bdb02 20724 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20725 if (curr != nullptr && curr->name != nullptr)
20726 SYMBOL_IS_ARGUMENT (sym) = 1;
20727 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20728 if (attr != nullptr)
a60f3166
TT
20729 {
20730 var_decode_location (attr, sym, cu);
20731 }
20732 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20733 if (attr != nullptr)
a60f3166
TT
20734 {
20735 dwarf2_const_value (attr, sym, cu);
20736 }
f346a30d 20737
a60f3166
TT
20738 list_to_add = cu->list_in_scope;
20739 }
c906108c
SS
20740 break;
20741 case DW_TAG_unspecified_parameters:
20742 /* From varargs functions; gdb doesn't seem to have any
20743 interest in this information, so just ignore it for now.
20744 (FIXME?) */
20745 break;
34eaf542
TT
20746 case DW_TAG_template_type_param:
20747 suppress_add = 1;
20748 /* Fall through. */
c906108c 20749 case DW_TAG_class_type:
680b30c7 20750 case DW_TAG_interface_type:
c906108c
SS
20751 case DW_TAG_structure_type:
20752 case DW_TAG_union_type:
72019c9c 20753 case DW_TAG_set_type:
c906108c 20754 case DW_TAG_enumeration_type:
f1e6e072 20755 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20756 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20757
63d06c5c 20758 {
9c37b5ae 20759 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20760 really ever be static objects: otherwise, if you try
20761 to, say, break of a class's method and you're in a file
20762 which doesn't mention that class, it won't work unless
20763 the check for all static symbols in lookup_symbol_aux
20764 saves you. See the OtherFileClass tests in
20765 gdb.c++/namespace.exp. */
20766
e37fd15a 20767 if (!suppress_add)
34eaf542 20768 {
c24bdb02 20769 buildsym_compunit *builder = cu->get_builder ();
804d2729 20770 list_to_add
c24bdb02 20771 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20772 && cu->language == language_cplus
c24bdb02 20773 ? builder->get_global_symbols ()
804d2729 20774 : cu->list_in_scope);
63d06c5c 20775
64382290 20776 /* The semantics of C++ state that "struct foo {
9c37b5ae 20777 ... }" also defines a typedef for "foo". */
64382290 20778 if (cu->language == language_cplus
45280282 20779 || cu->language == language_ada
c44af4eb
TT
20780 || cu->language == language_d
20781 || cu->language == language_rust)
64382290
TT
20782 {
20783 /* The symbol's name is already allocated along
20784 with this objfile, so we don't need to
20785 duplicate it for the type. */
20786 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20787 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20788 }
63d06c5c
DC
20789 }
20790 }
c906108c
SS
20791 break;
20792 case DW_TAG_typedef:
f1e6e072 20793 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20794 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20795 list_to_add = cu->list_in_scope;
63d06c5c 20796 break;
c906108c 20797 case DW_TAG_base_type:
a02abb62 20798 case DW_TAG_subrange_type:
f1e6e072 20799 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20800 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20801 list_to_add = cu->list_in_scope;
c906108c
SS
20802 break;
20803 case DW_TAG_enumerator:
e142c38c 20804 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20805 if (attr != nullptr)
c906108c 20806 {
e7c27a73 20807 dwarf2_const_value (attr, sym, cu);
c906108c 20808 }
63d06c5c
DC
20809 {
20810 /* NOTE: carlton/2003-11-10: See comment above in the
20811 DW_TAG_class_type, etc. block. */
20812
804d2729 20813 list_to_add
c24bdb02 20814 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20815 && cu->language == language_cplus
c24bdb02 20816 ? cu->get_builder ()->get_global_symbols ()
804d2729 20817 : cu->list_in_scope);
63d06c5c 20818 }
c906108c 20819 break;
74921315 20820 case DW_TAG_imported_declaration:
5c4e30ca 20821 case DW_TAG_namespace:
f1e6e072 20822 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20823 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20824 break;
530e8392
KB
20825 case DW_TAG_module:
20826 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20827 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20828 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20829 break;
4357ac6c 20830 case DW_TAG_common_block:
f1e6e072 20831 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20832 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20833 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20834 break;
c906108c
SS
20835 default:
20836 /* Not a tag we recognize. Hopefully we aren't processing
20837 trash data, but since we must specifically ignore things
20838 we don't recognize, there is nothing else we should do at
0963b4bd 20839 this point. */
b98664d3 20840 complaint (_("unsupported tag: '%s'"),
4d3c2250 20841 dwarf_tag_name (die->tag));
c906108c
SS
20842 break;
20843 }
df8a16a1 20844
e37fd15a
SW
20845 if (suppress_add)
20846 {
20847 sym->hash_next = objfile->template_symbols;
20848 objfile->template_symbols = sym;
20849 list_to_add = NULL;
20850 }
20851
20852 if (list_to_add != NULL)
d3cb6808 20853 add_symbol_to_list (sym, list_to_add);
e37fd15a 20854
df8a16a1
DJ
20855 /* For the benefit of old versions of GCC, check for anonymous
20856 namespaces based on the demangled name. */
4d4ec4e5 20857 if (!cu->processing_has_namespace_info
94af9270 20858 && cu->language == language_cplus)
c24bdb02 20859 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20860 }
20861 return (sym);
20862}
20863
98bfdba5
PA
20864/* Given an attr with a DW_FORM_dataN value in host byte order,
20865 zero-extend it as appropriate for the symbol's type. The DWARF
20866 standard (v4) is not entirely clear about the meaning of using
20867 DW_FORM_dataN for a constant with a signed type, where the type is
20868 wider than the data. The conclusion of a discussion on the DWARF
20869 list was that this is unspecified. We choose to always zero-extend
20870 because that is the interpretation long in use by GCC. */
c906108c 20871
98bfdba5 20872static gdb_byte *
ff39bb5e 20873dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20874 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20875{
518817b3 20876 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20877 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20878 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20879 LONGEST l = DW_UNSND (attr);
20880
20881 if (bits < sizeof (*value) * 8)
20882 {
20883 l &= ((LONGEST) 1 << bits) - 1;
20884 *value = l;
20885 }
20886 else if (bits == sizeof (*value) * 8)
20887 *value = l;
20888 else
20889 {
224c3ddb 20890 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20891 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20892 return bytes;
20893 }
20894
20895 return NULL;
20896}
20897
20898/* Read a constant value from an attribute. Either set *VALUE, or if
20899 the value does not fit in *VALUE, set *BYTES - either already
20900 allocated on the objfile obstack, or newly allocated on OBSTACK,
20901 or, set *BATON, if we translated the constant to a location
20902 expression. */
20903
20904static void
ff39bb5e 20905dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20906 const char *name, struct obstack *obstack,
20907 struct dwarf2_cu *cu,
d521ce57 20908 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20909 struct dwarf2_locexpr_baton **baton)
20910{
518817b3 20911 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20912 struct comp_unit_head *cu_header = &cu->header;
c906108c 20913 struct dwarf_block *blk;
98bfdba5
PA
20914 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20915 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20916
20917 *value = 0;
20918 *bytes = NULL;
20919 *baton = NULL;
c906108c
SS
20920
20921 switch (attr->form)
20922 {
20923 case DW_FORM_addr:
336d760d 20924 case DW_FORM_addrx:
3019eac3 20925 case DW_FORM_GNU_addr_index:
ac56253d 20926 {
ac56253d
TT
20927 gdb_byte *data;
20928
98bfdba5
PA
20929 if (TYPE_LENGTH (type) != cu_header->addr_size)
20930 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20931 cu_header->addr_size,
98bfdba5 20932 TYPE_LENGTH (type));
ac56253d
TT
20933 /* Symbols of this form are reasonably rare, so we just
20934 piggyback on the existing location code rather than writing
20935 a new implementation of symbol_computed_ops. */
8d749320 20936 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20937 (*baton)->per_cu = cu->per_cu;
20938 gdb_assert ((*baton)->per_cu);
ac56253d 20939
98bfdba5 20940 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20941 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20942 (*baton)->data = data;
ac56253d
TT
20943
20944 data[0] = DW_OP_addr;
20945 store_unsigned_integer (&data[1], cu_header->addr_size,
20946 byte_order, DW_ADDR (attr));
20947 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20948 }
c906108c 20949 break;
4ac36638 20950 case DW_FORM_string:
93b5768b 20951 case DW_FORM_strp:
cf532bd1 20952 case DW_FORM_strx:
3019eac3 20953 case DW_FORM_GNU_str_index:
36586728 20954 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20955 /* DW_STRING is already allocated on the objfile obstack, point
20956 directly to it. */
d521ce57 20957 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20958 break;
c906108c
SS
20959 case DW_FORM_block1:
20960 case DW_FORM_block2:
20961 case DW_FORM_block4:
20962 case DW_FORM_block:
2dc7f7b3 20963 case DW_FORM_exprloc:
0224619f 20964 case DW_FORM_data16:
c906108c 20965 blk = DW_BLOCK (attr);
98bfdba5
PA
20966 if (TYPE_LENGTH (type) != blk->size)
20967 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20968 TYPE_LENGTH (type));
20969 *bytes = blk->data;
c906108c 20970 break;
2df3850c
JM
20971
20972 /* The DW_AT_const_value attributes are supposed to carry the
20973 symbol's value "represented as it would be on the target
20974 architecture." By the time we get here, it's already been
20975 converted to host endianness, so we just need to sign- or
20976 zero-extend it as appropriate. */
20977 case DW_FORM_data1:
3aef2284 20978 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20979 break;
c906108c 20980 case DW_FORM_data2:
3aef2284 20981 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20982 break;
c906108c 20983 case DW_FORM_data4:
3aef2284 20984 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20985 break;
c906108c 20986 case DW_FORM_data8:
3aef2284 20987 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20988 break;
20989
c906108c 20990 case DW_FORM_sdata:
663c44ac 20991 case DW_FORM_implicit_const:
98bfdba5 20992 *value = DW_SND (attr);
2df3850c
JM
20993 break;
20994
c906108c 20995 case DW_FORM_udata:
98bfdba5 20996 *value = DW_UNSND (attr);
c906108c 20997 break;
2df3850c 20998
c906108c 20999 default:
b98664d3 21000 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21001 dwarf_form_name (attr->form));
98bfdba5 21002 *value = 0;
c906108c
SS
21003 break;
21004 }
21005}
21006
2df3850c 21007
98bfdba5
PA
21008/* Copy constant value from an attribute to a symbol. */
21009
2df3850c 21010static void
ff39bb5e 21011dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21012 struct dwarf2_cu *cu)
2df3850c 21013{
518817b3 21014 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21015 LONGEST value;
d521ce57 21016 const gdb_byte *bytes;
98bfdba5 21017 struct dwarf2_locexpr_baton *baton;
2df3850c 21018
98bfdba5 21019 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21020 sym->print_name (),
98bfdba5
PA
21021 &objfile->objfile_obstack, cu,
21022 &value, &bytes, &baton);
2df3850c 21023
98bfdba5
PA
21024 if (baton != NULL)
21025 {
98bfdba5 21026 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21027 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21028 }
21029 else if (bytes != NULL)
21030 {
21031 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21032 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21033 }
21034 else
21035 {
21036 SYMBOL_VALUE (sym) = value;
f1e6e072 21037 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21038 }
2df3850c
JM
21039}
21040
c906108c
SS
21041/* Return the type of the die in question using its DW_AT_type attribute. */
21042
21043static struct type *
e7c27a73 21044die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21045{
c906108c 21046 struct attribute *type_attr;
c906108c 21047
e142c38c 21048 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21049 if (!type_attr)
21050 {
518817b3 21051 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21052 /* A missing DW_AT_type represents a void type. */
518817b3 21053 return objfile_type (objfile)->builtin_void;
c906108c 21054 }
348e048f 21055
673bfd45 21056 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21057}
21058
b4ba55a1
JB
21059/* True iff CU's producer generates GNAT Ada auxiliary information
21060 that allows to find parallel types through that information instead
21061 of having to do expensive parallel lookups by type name. */
21062
21063static int
21064need_gnat_info (struct dwarf2_cu *cu)
21065{
de4cb04a
JB
21066 /* Assume that the Ada compiler was GNAT, which always produces
21067 the auxiliary information. */
21068 return (cu->language == language_ada);
b4ba55a1
JB
21069}
21070
b4ba55a1
JB
21071/* Return the auxiliary type of the die in question using its
21072 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21073 attribute is not present. */
21074
21075static struct type *
21076die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21077{
b4ba55a1 21078 struct attribute *type_attr;
b4ba55a1
JB
21079
21080 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21081 if (!type_attr)
21082 return NULL;
21083
673bfd45 21084 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21085}
21086
21087/* If DIE has a descriptive_type attribute, then set the TYPE's
21088 descriptive type accordingly. */
21089
21090static void
21091set_descriptive_type (struct type *type, struct die_info *die,
21092 struct dwarf2_cu *cu)
21093{
21094 struct type *descriptive_type = die_descriptive_type (die, cu);
21095
21096 if (descriptive_type)
21097 {
21098 ALLOCATE_GNAT_AUX_TYPE (type);
21099 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21100 }
21101}
21102
c906108c
SS
21103/* Return the containing type of the die in question using its
21104 DW_AT_containing_type attribute. */
21105
21106static struct type *
e7c27a73 21107die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21108{
c906108c 21109 struct attribute *type_attr;
518817b3 21110 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21111
e142c38c 21112 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21113 if (!type_attr)
21114 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21115 "[in module %s]"), objfile_name (objfile));
33ac96f0 21116
673bfd45 21117 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21118}
21119
ac9ec31b
DE
21120/* Return an error marker type to use for the ill formed type in DIE/CU. */
21121
21122static struct type *
21123build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21124{
518817b3
SM
21125 struct dwarf2_per_objfile *dwarf2_per_objfile
21126 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21127 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21128 char *saved;
ac9ec31b 21129
528e1572
SM
21130 std::string message
21131 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21132 objfile_name (objfile),
21133 sect_offset_str (cu->header.sect_off),
21134 sect_offset_str (die->sect_off));
efba19b0 21135 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21136
19f392bc 21137 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21138}
21139
673bfd45 21140/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21141 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21142 DW_AT_containing_type.
673bfd45
DE
21143 If there is no type substitute an error marker. */
21144
c906108c 21145static struct type *
ff39bb5e 21146lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21147 struct dwarf2_cu *cu)
c906108c 21148{
518817b3
SM
21149 struct dwarf2_per_objfile *dwarf2_per_objfile
21150 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21151 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21152 struct type *this_type;
21153
ac9ec31b
DE
21154 gdb_assert (attr->name == DW_AT_type
21155 || attr->name == DW_AT_GNAT_descriptive_type
21156 || attr->name == DW_AT_containing_type);
21157
673bfd45
DE
21158 /* First see if we have it cached. */
21159
36586728
TT
21160 if (attr->form == DW_FORM_GNU_ref_alt)
21161 {
21162 struct dwarf2_per_cu_data *per_cu;
9c541725 21163 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21164
ed2dc618
SM
21165 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21166 dwarf2_per_objfile);
9c541725 21167 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21168 }
cd6c91b4 21169 else if (attr->form_is_ref ())
673bfd45 21170 {
9c541725 21171 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21172
9c541725 21173 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21174 }
55f1336d 21175 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21176 {
ac9ec31b 21177 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21178
ac9ec31b 21179 return get_signatured_type (die, signature, cu);
673bfd45
DE
21180 }
21181 else
21182 {
b98664d3 21183 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21184 " at %s [in module %s]"),
21185 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21186 objfile_name (objfile));
ac9ec31b 21187 return build_error_marker_type (cu, die);
673bfd45
DE
21188 }
21189
21190 /* If not cached we need to read it in. */
21191
21192 if (this_type == NULL)
21193 {
ac9ec31b 21194 struct die_info *type_die = NULL;
673bfd45
DE
21195 struct dwarf2_cu *type_cu = cu;
21196
cd6c91b4 21197 if (attr->form_is_ref ())
ac9ec31b
DE
21198 type_die = follow_die_ref (die, attr, &type_cu);
21199 if (type_die == NULL)
21200 return build_error_marker_type (cu, die);
21201 /* If we find the type now, it's probably because the type came
3019eac3
DE
21202 from an inter-CU reference and the type's CU got expanded before
21203 ours. */
ac9ec31b 21204 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21205 }
21206
21207 /* If we still don't have a type use an error marker. */
21208
21209 if (this_type == NULL)
ac9ec31b 21210 return build_error_marker_type (cu, die);
673bfd45 21211
f792889a 21212 return this_type;
c906108c
SS
21213}
21214
673bfd45
DE
21215/* Return the type in DIE, CU.
21216 Returns NULL for invalid types.
21217
02142a6c 21218 This first does a lookup in die_type_hash,
673bfd45
DE
21219 and only reads the die in if necessary.
21220
21221 NOTE: This can be called when reading in partial or full symbols. */
21222
f792889a 21223static struct type *
e7c27a73 21224read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21225{
f792889a
DJ
21226 struct type *this_type;
21227
21228 this_type = get_die_type (die, cu);
21229 if (this_type)
21230 return this_type;
21231
673bfd45
DE
21232 return read_type_die_1 (die, cu);
21233}
21234
21235/* Read the type in DIE, CU.
21236 Returns NULL for invalid types. */
21237
21238static struct type *
21239read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21240{
21241 struct type *this_type = NULL;
21242
c906108c
SS
21243 switch (die->tag)
21244 {
21245 case DW_TAG_class_type:
680b30c7 21246 case DW_TAG_interface_type:
c906108c
SS
21247 case DW_TAG_structure_type:
21248 case DW_TAG_union_type:
f792889a 21249 this_type = read_structure_type (die, cu);
c906108c
SS
21250 break;
21251 case DW_TAG_enumeration_type:
f792889a 21252 this_type = read_enumeration_type (die, cu);
c906108c
SS
21253 break;
21254 case DW_TAG_subprogram:
21255 case DW_TAG_subroutine_type:
edb3359d 21256 case DW_TAG_inlined_subroutine:
f792889a 21257 this_type = read_subroutine_type (die, cu);
c906108c
SS
21258 break;
21259 case DW_TAG_array_type:
f792889a 21260 this_type = read_array_type (die, cu);
c906108c 21261 break;
72019c9c 21262 case DW_TAG_set_type:
f792889a 21263 this_type = read_set_type (die, cu);
72019c9c 21264 break;
c906108c 21265 case DW_TAG_pointer_type:
f792889a 21266 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21267 break;
21268 case DW_TAG_ptr_to_member_type:
f792889a 21269 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21270 break;
21271 case DW_TAG_reference_type:
4297a3f0
AV
21272 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21273 break;
21274 case DW_TAG_rvalue_reference_type:
21275 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21276 break;
21277 case DW_TAG_const_type:
f792889a 21278 this_type = read_tag_const_type (die, cu);
c906108c
SS
21279 break;
21280 case DW_TAG_volatile_type:
f792889a 21281 this_type = read_tag_volatile_type (die, cu);
c906108c 21282 break;
06d66ee9
TT
21283 case DW_TAG_restrict_type:
21284 this_type = read_tag_restrict_type (die, cu);
21285 break;
c906108c 21286 case DW_TAG_string_type:
f792889a 21287 this_type = read_tag_string_type (die, cu);
c906108c
SS
21288 break;
21289 case DW_TAG_typedef:
f792889a 21290 this_type = read_typedef (die, cu);
c906108c 21291 break;
a02abb62 21292 case DW_TAG_subrange_type:
f792889a 21293 this_type = read_subrange_type (die, cu);
a02abb62 21294 break;
c906108c 21295 case DW_TAG_base_type:
f792889a 21296 this_type = read_base_type (die, cu);
c906108c 21297 break;
81a17f79 21298 case DW_TAG_unspecified_type:
f792889a 21299 this_type = read_unspecified_type (die, cu);
81a17f79 21300 break;
0114d602
DJ
21301 case DW_TAG_namespace:
21302 this_type = read_namespace_type (die, cu);
21303 break;
f55ee35c
JK
21304 case DW_TAG_module:
21305 this_type = read_module_type (die, cu);
21306 break;
a2c2acaf
MW
21307 case DW_TAG_atomic_type:
21308 this_type = read_tag_atomic_type (die, cu);
21309 break;
c906108c 21310 default:
b98664d3 21311 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21312 dwarf_tag_name (die->tag));
c906108c
SS
21313 break;
21314 }
63d06c5c 21315
f792889a 21316 return this_type;
63d06c5c
DC
21317}
21318
abc72ce4
DE
21319/* See if we can figure out if the class lives in a namespace. We do
21320 this by looking for a member function; its demangled name will
21321 contain namespace info, if there is any.
21322 Return the computed name or NULL.
21323 Space for the result is allocated on the objfile's obstack.
21324 This is the full-die version of guess_partial_die_structure_name.
21325 In this case we know DIE has no useful parent. */
21326
43816ebc 21327static const char *
abc72ce4
DE
21328guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21329{
21330 struct die_info *spec_die;
21331 struct dwarf2_cu *spec_cu;
21332 struct die_info *child;
518817b3 21333 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21334
21335 spec_cu = cu;
21336 spec_die = die_specification (die, &spec_cu);
21337 if (spec_die != NULL)
21338 {
21339 die = spec_die;
21340 cu = spec_cu;
21341 }
21342
21343 for (child = die->child;
21344 child != NULL;
21345 child = child->sibling)
21346 {
21347 if (child->tag == DW_TAG_subprogram)
21348 {
73b9be8b 21349 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21350
7d45c7c3 21351 if (linkage_name != NULL)
abc72ce4 21352 {
43816ebc
TT
21353 gdb::unique_xmalloc_ptr<char> actual_name
21354 (language_class_name_from_physname (cu->language_defn,
21355 linkage_name));
21356 const char *name = NULL;
abc72ce4
DE
21357
21358 if (actual_name != NULL)
21359 {
15d034d0 21360 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21361
21362 if (die_name != NULL
43816ebc 21363 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21364 {
21365 /* Strip off the class name from the full name.
21366 We want the prefix. */
21367 int die_name_len = strlen (die_name);
43816ebc
TT
21368 int actual_name_len = strlen (actual_name.get ());
21369 const char *ptr = actual_name.get ();
abc72ce4
DE
21370
21371 /* Test for '::' as a sanity check. */
21372 if (actual_name_len > die_name_len + 2
43816ebc 21373 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21374 name = obstack_strndup (
e3b94546 21375 &objfile->per_bfd->storage_obstack,
43816ebc 21376 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21377 }
21378 }
abc72ce4
DE
21379 return name;
21380 }
21381 }
21382 }
21383
21384 return NULL;
21385}
21386
96408a79
SA
21387/* GCC might emit a nameless typedef that has a linkage name. Determine the
21388 prefix part in such case. See
21389 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21390
a121b7c1 21391static const char *
96408a79
SA
21392anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21393{
21394 struct attribute *attr;
e6a959d6 21395 const char *base;
96408a79
SA
21396
21397 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21398 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21399 return NULL;
21400
7d45c7c3 21401 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21402 return NULL;
21403
73b9be8b 21404 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21405 if (attr == NULL || DW_STRING (attr) == NULL)
21406 return NULL;
21407
21408 /* dwarf2_name had to be already called. */
21409 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21410
21411 /* Strip the base name, keep any leading namespaces/classes. */
21412 base = strrchr (DW_STRING (attr), ':');
21413 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21414 return "";
21415
518817b3 21416 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21417 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21418 DW_STRING (attr),
21419 &base[-1] - DW_STRING (attr));
96408a79
SA
21420}
21421
fdde2d81 21422/* Return the name of the namespace/class that DIE is defined within,
0114d602 21423 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21424
0114d602
DJ
21425 For example, if we're within the method foo() in the following
21426 code:
21427
21428 namespace N {
21429 class C {
21430 void foo () {
21431 }
21432 };
21433 }
21434
21435 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21436
0d5cff50 21437static const char *
e142c38c 21438determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21439{
518817b3
SM
21440 struct dwarf2_per_objfile *dwarf2_per_objfile
21441 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21442 struct die_info *parent, *spec_die;
21443 struct dwarf2_cu *spec_cu;
21444 struct type *parent_type;
a121b7c1 21445 const char *retval;
63d06c5c 21446
9c37b5ae 21447 if (cu->language != language_cplus
c44af4eb
TT
21448 && cu->language != language_fortran && cu->language != language_d
21449 && cu->language != language_rust)
0114d602
DJ
21450 return "";
21451
96408a79
SA
21452 retval = anonymous_struct_prefix (die, cu);
21453 if (retval)
21454 return retval;
21455
0114d602
DJ
21456 /* We have to be careful in the presence of DW_AT_specification.
21457 For example, with GCC 3.4, given the code
21458
21459 namespace N {
21460 void foo() {
21461 // Definition of N::foo.
21462 }
21463 }
21464
21465 then we'll have a tree of DIEs like this:
21466
21467 1: DW_TAG_compile_unit
21468 2: DW_TAG_namespace // N
21469 3: DW_TAG_subprogram // declaration of N::foo
21470 4: DW_TAG_subprogram // definition of N::foo
21471 DW_AT_specification // refers to die #3
21472
21473 Thus, when processing die #4, we have to pretend that we're in
21474 the context of its DW_AT_specification, namely the contex of die
21475 #3. */
21476 spec_cu = cu;
21477 spec_die = die_specification (die, &spec_cu);
21478 if (spec_die == NULL)
21479 parent = die->parent;
21480 else
63d06c5c 21481 {
0114d602
DJ
21482 parent = spec_die->parent;
21483 cu = spec_cu;
63d06c5c 21484 }
0114d602
DJ
21485
21486 if (parent == NULL)
21487 return "";
98bfdba5
PA
21488 else if (parent->building_fullname)
21489 {
21490 const char *name;
21491 const char *parent_name;
21492
21493 /* It has been seen on RealView 2.2 built binaries,
21494 DW_TAG_template_type_param types actually _defined_ as
21495 children of the parent class:
21496
21497 enum E {};
21498 template class <class Enum> Class{};
21499 Class<enum E> class_e;
21500
21501 1: DW_TAG_class_type (Class)
21502 2: DW_TAG_enumeration_type (E)
21503 3: DW_TAG_enumerator (enum1:0)
21504 3: DW_TAG_enumerator (enum2:1)
21505 ...
21506 2: DW_TAG_template_type_param
21507 DW_AT_type DW_FORM_ref_udata (E)
21508
21509 Besides being broken debug info, it can put GDB into an
21510 infinite loop. Consider:
21511
21512 When we're building the full name for Class<E>, we'll start
21513 at Class, and go look over its template type parameters,
21514 finding E. We'll then try to build the full name of E, and
21515 reach here. We're now trying to build the full name of E,
21516 and look over the parent DIE for containing scope. In the
21517 broken case, if we followed the parent DIE of E, we'd again
21518 find Class, and once again go look at its template type
21519 arguments, etc., etc. Simply don't consider such parent die
21520 as source-level parent of this die (it can't be, the language
21521 doesn't allow it), and break the loop here. */
21522 name = dwarf2_name (die, cu);
21523 parent_name = dwarf2_name (parent, cu);
b98664d3 21524 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21525 name ? name : "<unknown>",
21526 parent_name ? parent_name : "<unknown>");
21527 return "";
21528 }
63d06c5c 21529 else
0114d602
DJ
21530 switch (parent->tag)
21531 {
63d06c5c 21532 case DW_TAG_namespace:
0114d602 21533 parent_type = read_type_die (parent, cu);
acebe513
UW
21534 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21535 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21536 Work around this problem here. */
21537 if (cu->language == language_cplus
e86ca25f 21538 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21539 return "";
0114d602 21540 /* We give a name to even anonymous namespaces. */
e86ca25f 21541 return TYPE_NAME (parent_type);
63d06c5c 21542 case DW_TAG_class_type:
680b30c7 21543 case DW_TAG_interface_type:
63d06c5c 21544 case DW_TAG_structure_type:
0114d602 21545 case DW_TAG_union_type:
f55ee35c 21546 case DW_TAG_module:
0114d602 21547 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21548 if (TYPE_NAME (parent_type) != NULL)
21549 return TYPE_NAME (parent_type);
0114d602
DJ
21550 else
21551 /* An anonymous structure is only allowed non-static data
21552 members; no typedefs, no member functions, et cetera.
21553 So it does not need a prefix. */
21554 return "";
abc72ce4 21555 case DW_TAG_compile_unit:
95554aad 21556 case DW_TAG_partial_unit:
abc72ce4
DE
21557 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21558 if (cu->language == language_cplus
fd5866f6 21559 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21560 && die->child != NULL
21561 && (die->tag == DW_TAG_class_type
21562 || die->tag == DW_TAG_structure_type
21563 || die->tag == DW_TAG_union_type))
21564 {
43816ebc 21565 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21566 if (name != NULL)
21567 return name;
21568 }
21569 return "";
0a4b0913
AB
21570 case DW_TAG_subprogram:
21571 /* Nested subroutines in Fortran get a prefix with the name
21572 of the parent's subroutine. */
21573 if (cu->language == language_fortran)
21574 {
21575 if ((die->tag == DW_TAG_subprogram)
21576 && (dwarf2_name (parent, cu) != NULL))
21577 return dwarf2_name (parent, cu);
21578 }
21579 return determine_prefix (parent, cu);
3d567982
TT
21580 case DW_TAG_enumeration_type:
21581 parent_type = read_type_die (parent, cu);
21582 if (TYPE_DECLARED_CLASS (parent_type))
21583 {
e86ca25f
TT
21584 if (TYPE_NAME (parent_type) != NULL)
21585 return TYPE_NAME (parent_type);
3d567982
TT
21586 return "";
21587 }
21588 /* Fall through. */
63d06c5c 21589 default:
8176b9b8 21590 return determine_prefix (parent, cu);
63d06c5c 21591 }
63d06c5c
DC
21592}
21593
3e43a32a
MS
21594/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21595 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21596 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21597 an obconcat, otherwise allocate storage for the result. The CU argument is
21598 used to determine the language and hence, the appropriate separator. */
987504bb 21599
f55ee35c 21600#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21601
21602static char *
f55ee35c
JK
21603typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21604 int physname, struct dwarf2_cu *cu)
63d06c5c 21605{
f55ee35c 21606 const char *lead = "";
5c315b68 21607 const char *sep;
63d06c5c 21608
3e43a32a
MS
21609 if (suffix == NULL || suffix[0] == '\0'
21610 || prefix == NULL || prefix[0] == '\0')
987504bb 21611 sep = "";
45280282
IB
21612 else if (cu->language == language_d)
21613 {
21614 /* For D, the 'main' function could be defined in any module, but it
21615 should never be prefixed. */
21616 if (strcmp (suffix, "D main") == 0)
21617 {
21618 prefix = "";
21619 sep = "";
21620 }
21621 else
21622 sep = ".";
21623 }
f55ee35c
JK
21624 else if (cu->language == language_fortran && physname)
21625 {
21626 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21627 DW_AT_MIPS_linkage_name is preferred and used instead. */
21628
21629 lead = "__";
21630 sep = "_MOD_";
21631 }
987504bb
JJ
21632 else
21633 sep = "::";
63d06c5c 21634
6dd47d34
DE
21635 if (prefix == NULL)
21636 prefix = "";
21637 if (suffix == NULL)
21638 suffix = "";
21639
987504bb
JJ
21640 if (obs == NULL)
21641 {
3e43a32a 21642 char *retval
224c3ddb
SM
21643 = ((char *)
21644 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21645
f55ee35c
JK
21646 strcpy (retval, lead);
21647 strcat (retval, prefix);
6dd47d34
DE
21648 strcat (retval, sep);
21649 strcat (retval, suffix);
63d06c5c
DC
21650 return retval;
21651 }
987504bb
JJ
21652 else
21653 {
21654 /* We have an obstack. */
f55ee35c 21655 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21656 }
63d06c5c
DC
21657}
21658
c906108c
SS
21659/* Return sibling of die, NULL if no sibling. */
21660
f9aca02d 21661static struct die_info *
fba45db2 21662sibling_die (struct die_info *die)
c906108c 21663{
639d11d3 21664 return die->sibling;
c906108c
SS
21665}
21666
71c25dea
TT
21667/* Get name of a die, return NULL if not found. */
21668
15d034d0
TT
21669static const char *
21670dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
21671 struct obstack *obstack)
21672{
21673 if (name && cu->language == language_cplus)
21674 {
2f408ecb 21675 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21676
2f408ecb 21677 if (!canon_name.empty ())
71c25dea 21678 {
2f408ecb 21679 if (canon_name != name)
efba19b0 21680 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
21681 }
21682 }
21683
21684 return name;
c906108c
SS
21685}
21686
96553a0c
DE
21687/* Get name of a die, return NULL if not found.
21688 Anonymous namespaces are converted to their magic string. */
9219021c 21689
15d034d0 21690static const char *
e142c38c 21691dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21692{
21693 struct attribute *attr;
518817b3 21694 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21695
e142c38c 21696 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21697 if ((!attr || !DW_STRING (attr))
96553a0c 21698 && die->tag != DW_TAG_namespace
53832f31
TT
21699 && die->tag != DW_TAG_class_type
21700 && die->tag != DW_TAG_interface_type
21701 && die->tag != DW_TAG_structure_type
21702 && die->tag != DW_TAG_union_type)
71c25dea
TT
21703 return NULL;
21704
21705 switch (die->tag)
21706 {
21707 case DW_TAG_compile_unit:
95554aad 21708 case DW_TAG_partial_unit:
71c25dea
TT
21709 /* Compilation units have a DW_AT_name that is a filename, not
21710 a source language identifier. */
21711 case DW_TAG_enumeration_type:
21712 case DW_TAG_enumerator:
21713 /* These tags always have simple identifiers already; no need
21714 to canonicalize them. */
21715 return DW_STRING (attr);
907af001 21716
96553a0c
DE
21717 case DW_TAG_namespace:
21718 if (attr != NULL && DW_STRING (attr) != NULL)
21719 return DW_STRING (attr);
21720 return CP_ANONYMOUS_NAMESPACE_STR;
21721
907af001
UW
21722 case DW_TAG_class_type:
21723 case DW_TAG_interface_type:
21724 case DW_TAG_structure_type:
21725 case DW_TAG_union_type:
21726 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21727 structures or unions. These were of the form "._%d" in GCC 4.1,
21728 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21729 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21730 if (attr && DW_STRING (attr)
61012eef
GB
21731 && (startswith (DW_STRING (attr), "._")
21732 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21733 return NULL;
53832f31
TT
21734
21735 /* GCC might emit a nameless typedef that has a linkage name. See
21736 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21737 if (!attr || DW_STRING (attr) == NULL)
21738 {
73b9be8b 21739 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21740 if (attr == NULL || DW_STRING (attr) == NULL)
21741 return NULL;
21742
df5c6c50
JK
21743 /* Avoid demangling DW_STRING (attr) the second time on a second
21744 call for the same DIE. */
21745 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21746 {
43816ebc
TT
21747 gdb::unique_xmalloc_ptr<char> demangled
21748 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21749 if (demangled == nullptr)
21750 return nullptr;
43816ebc 21751
e6a959d6 21752 const char *base;
96408a79 21753
53832f31 21754 /* FIXME: we already did this for the partial symbol... */
34a68019 21755 DW_STRING (attr)
021887d8 21756 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 21757 demangled.get ());
53832f31 21758 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
21759
21760 /* Strip any leading namespaces/classes, keep only the base name.
21761 DW_AT_name for named DIEs does not contain the prefixes. */
21762 base = strrchr (DW_STRING (attr), ':');
21763 if (base && base > DW_STRING (attr) && base[-1] == ':')
21764 return &base[1];
21765 else
21766 return DW_STRING (attr);
53832f31
TT
21767 }
21768 }
907af001
UW
21769 break;
21770
71c25dea 21771 default:
907af001
UW
21772 break;
21773 }
21774
21775 if (!DW_STRING_IS_CANONICAL (attr))
21776 {
21777 DW_STRING (attr)
21778 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 21779 &objfile->per_bfd->storage_obstack);
907af001 21780 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21781 }
907af001 21782 return DW_STRING (attr);
9219021c
DC
21783}
21784
21785/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21786 is none. *EXT_CU is the CU containing DIE on input, and the CU
21787 containing the return value on output. */
9219021c
DC
21788
21789static struct die_info *
f2f0e013 21790dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21791{
21792 struct attribute *attr;
9219021c 21793
f2f0e013 21794 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21795 if (attr == NULL)
21796 return NULL;
21797
f2f0e013 21798 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21799}
21800
fa9c3fa0
TT
21801/* A convenience function that returns an "unknown" DWARF name,
21802 including the value of V. STR is the name of the entity being
21803 printed, e.g., "TAG". */
21804
21805static const char *
21806dwarf_unknown (const char *str, unsigned v)
21807{
21808 char *cell = get_print_cell ();
21809 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21810 return cell;
21811}
21812
c906108c
SS
21813/* Convert a DIE tag into its string name. */
21814
f39c6ffd 21815static const char *
aa1ee363 21816dwarf_tag_name (unsigned tag)
c906108c 21817{
f39c6ffd
TT
21818 const char *name = get_DW_TAG_name (tag);
21819
21820 if (name == NULL)
fa9c3fa0 21821 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
21822
21823 return name;
c906108c
SS
21824}
21825
21826/* Convert a DWARF attribute code into its string name. */
21827
f39c6ffd 21828static const char *
aa1ee363 21829dwarf_attr_name (unsigned attr)
c906108c 21830{
f39c6ffd
TT
21831 const char *name;
21832
c764a876 21833#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
21834 if (attr == DW_AT_MIPS_fde)
21835 return "DW_AT_MIPS_fde";
21836#else
21837 if (attr == DW_AT_HP_block_index)
21838 return "DW_AT_HP_block_index";
c764a876 21839#endif
f39c6ffd
TT
21840
21841 name = get_DW_AT_name (attr);
21842
21843 if (name == NULL)
fa9c3fa0 21844 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
21845
21846 return name;
c906108c
SS
21847}
21848
21849/* Convert a DWARF value form code into its string name. */
21850
f39c6ffd 21851static const char *
aa1ee363 21852dwarf_form_name (unsigned form)
c906108c 21853{
f39c6ffd
TT
21854 const char *name = get_DW_FORM_name (form);
21855
21856 if (name == NULL)
fa9c3fa0 21857 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
21858
21859 return name;
c906108c
SS
21860}
21861
a121b7c1 21862static const char *
fba45db2 21863dwarf_bool_name (unsigned mybool)
c906108c
SS
21864{
21865 if (mybool)
21866 return "TRUE";
21867 else
21868 return "FALSE";
21869}
21870
21871/* Convert a DWARF type code into its string name. */
21872
f39c6ffd 21873static const char *
aa1ee363 21874dwarf_type_encoding_name (unsigned enc)
c906108c 21875{
f39c6ffd 21876 const char *name = get_DW_ATE_name (enc);
c906108c 21877
f39c6ffd 21878 if (name == NULL)
fa9c3fa0 21879 return dwarf_unknown ("ATE", enc);
c906108c 21880
f39c6ffd 21881 return name;
c906108c 21882}
c906108c 21883
f9aca02d 21884static void
d97bc12b 21885dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21886{
21887 unsigned int i;
21888
d97bc12b 21889 print_spaces (indent, f);
9d8780f0 21890 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21891 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21892 sect_offset_str (die->sect_off));
d97bc12b
DE
21893
21894 if (die->parent != NULL)
21895 {
21896 print_spaces (indent, f);
9d8780f0
SM
21897 fprintf_unfiltered (f, " parent at offset: %s\n",
21898 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21899 }
21900
21901 print_spaces (indent, f);
21902 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21903 dwarf_bool_name (die->child != NULL));
c906108c 21904
d97bc12b
DE
21905 print_spaces (indent, f);
21906 fprintf_unfiltered (f, " attributes:\n");
21907
c906108c
SS
21908 for (i = 0; i < die->num_attrs; ++i)
21909 {
d97bc12b
DE
21910 print_spaces (indent, f);
21911 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21912 dwarf_attr_name (die->attrs[i].name),
21913 dwarf_form_name (die->attrs[i].form));
d97bc12b 21914
c906108c
SS
21915 switch (die->attrs[i].form)
21916 {
c906108c 21917 case DW_FORM_addr:
336d760d 21918 case DW_FORM_addrx:
3019eac3 21919 case DW_FORM_GNU_addr_index:
d97bc12b 21920 fprintf_unfiltered (f, "address: ");
5af949e3 21921 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21922 break;
21923 case DW_FORM_block2:
21924 case DW_FORM_block4:
21925 case DW_FORM_block:
21926 case DW_FORM_block1:
56eb65bd
SP
21927 fprintf_unfiltered (f, "block: size %s",
21928 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21929 break;
2dc7f7b3 21930 case DW_FORM_exprloc:
56eb65bd
SP
21931 fprintf_unfiltered (f, "expression: size %s",
21932 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21933 break;
0224619f
JK
21934 case DW_FORM_data16:
21935 fprintf_unfiltered (f, "constant of 16 bytes");
21936 break;
4568ecf9
DE
21937 case DW_FORM_ref_addr:
21938 fprintf_unfiltered (f, "ref address: ");
21939 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21940 break;
36586728
TT
21941 case DW_FORM_GNU_ref_alt:
21942 fprintf_unfiltered (f, "alt ref address: ");
21943 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21944 break;
10b3939b
DJ
21945 case DW_FORM_ref1:
21946 case DW_FORM_ref2:
21947 case DW_FORM_ref4:
4568ecf9
DE
21948 case DW_FORM_ref8:
21949 case DW_FORM_ref_udata:
d97bc12b 21950 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21951 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21952 break;
c906108c
SS
21953 case DW_FORM_data1:
21954 case DW_FORM_data2:
21955 case DW_FORM_data4:
ce5d95e1 21956 case DW_FORM_data8:
c906108c
SS
21957 case DW_FORM_udata:
21958 case DW_FORM_sdata:
43bbcdc2
PH
21959 fprintf_unfiltered (f, "constant: %s",
21960 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21961 break;
2dc7f7b3
TT
21962 case DW_FORM_sec_offset:
21963 fprintf_unfiltered (f, "section offset: %s",
21964 pulongest (DW_UNSND (&die->attrs[i])));
21965 break;
55f1336d 21966 case DW_FORM_ref_sig8:
ac9ec31b
DE
21967 fprintf_unfiltered (f, "signature: %s",
21968 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21969 break;
c906108c 21970 case DW_FORM_string:
4bdf3d34 21971 case DW_FORM_strp:
43988095 21972 case DW_FORM_line_strp:
cf532bd1 21973 case DW_FORM_strx:
3019eac3 21974 case DW_FORM_GNU_str_index:
36586728 21975 case DW_FORM_GNU_strp_alt:
8285870a 21976 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21977 DW_STRING (&die->attrs[i])
8285870a
JK
21978 ? DW_STRING (&die->attrs[i]) : "",
21979 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21980 break;
21981 case DW_FORM_flag:
21982 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21983 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21984 else
d97bc12b 21985 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21986 break;
2dc7f7b3
TT
21987 case DW_FORM_flag_present:
21988 fprintf_unfiltered (f, "flag: TRUE");
21989 break;
a8329558 21990 case DW_FORM_indirect:
0963b4bd
MS
21991 /* The reader will have reduced the indirect form to
21992 the "base form" so this form should not occur. */
5f48f8f3 21993 fprintf_unfiltered (f,
3e43a32a 21994 "unexpected attribute form: DW_FORM_indirect");
a8329558 21995 break;
663c44ac
JK
21996 case DW_FORM_implicit_const:
21997 fprintf_unfiltered (f, "constant: %s",
21998 plongest (DW_SND (&die->attrs[i])));
21999 break;
c906108c 22000 default:
d97bc12b 22001 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22002 die->attrs[i].form);
d97bc12b 22003 break;
c906108c 22004 }
d97bc12b 22005 fprintf_unfiltered (f, "\n");
c906108c
SS
22006 }
22007}
22008
f9aca02d 22009static void
d97bc12b 22010dump_die_for_error (struct die_info *die)
c906108c 22011{
d97bc12b
DE
22012 dump_die_shallow (gdb_stderr, 0, die);
22013}
22014
22015static void
22016dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22017{
22018 int indent = level * 4;
22019
22020 gdb_assert (die != NULL);
22021
22022 if (level >= max_level)
22023 return;
22024
22025 dump_die_shallow (f, indent, die);
22026
22027 if (die->child != NULL)
c906108c 22028 {
d97bc12b
DE
22029 print_spaces (indent, f);
22030 fprintf_unfiltered (f, " Children:");
22031 if (level + 1 < max_level)
22032 {
22033 fprintf_unfiltered (f, "\n");
22034 dump_die_1 (f, level + 1, max_level, die->child);
22035 }
22036 else
22037 {
3e43a32a
MS
22038 fprintf_unfiltered (f,
22039 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22040 }
22041 }
22042
22043 if (die->sibling != NULL && level > 0)
22044 {
22045 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22046 }
22047}
22048
d97bc12b
DE
22049/* This is called from the pdie macro in gdbinit.in.
22050 It's not static so gcc will keep a copy callable from gdb. */
22051
22052void
22053dump_die (struct die_info *die, int max_level)
22054{
22055 dump_die_1 (gdb_stdlog, 0, max_level, die);
22056}
22057
f9aca02d 22058static void
51545339 22059store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22060{
51545339 22061 void **slot;
c906108c 22062
9c541725
PA
22063 slot = htab_find_slot_with_hash (cu->die_hash, die,
22064 to_underlying (die->sect_off),
b64f50a1 22065 INSERT);
51545339
DJ
22066
22067 *slot = die;
c906108c
SS
22068}
22069
b64f50a1
JK
22070/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22071 required kind. */
22072
22073static sect_offset
ff39bb5e 22074dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22075{
cd6c91b4 22076 if (attr->form_is_ref ())
9c541725 22077 return (sect_offset) DW_UNSND (attr);
93311388 22078
b98664d3 22079 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22080 dwarf_form_name (attr->form));
9c541725 22081 return {};
c906108c
SS
22082}
22083
43bbcdc2
PH
22084/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22085 * the value held by the attribute is not constant. */
a02abb62 22086
43bbcdc2 22087static LONGEST
ff39bb5e 22088dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22089{
663c44ac 22090 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22091 return DW_SND (attr);
22092 else if (attr->form == DW_FORM_udata
22093 || attr->form == DW_FORM_data1
22094 || attr->form == DW_FORM_data2
22095 || attr->form == DW_FORM_data4
22096 || attr->form == DW_FORM_data8)
22097 return DW_UNSND (attr);
22098 else
22099 {
cd6c91b4 22100 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22101 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22102 dwarf_form_name (attr->form));
22103 return default_value;
22104 }
22105}
22106
348e048f
DE
22107/* Follow reference or signature attribute ATTR of SRC_DIE.
22108 On entry *REF_CU is the CU of SRC_DIE.
22109 On exit *REF_CU is the CU of the result. */
22110
22111static struct die_info *
ff39bb5e 22112follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22113 struct dwarf2_cu **ref_cu)
22114{
22115 struct die_info *die;
22116
cd6c91b4 22117 if (attr->form_is_ref ())
348e048f 22118 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22119 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22120 die = follow_die_sig (src_die, attr, ref_cu);
22121 else
22122 {
22123 dump_die_for_error (src_die);
22124 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22125 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22126 }
22127
22128 return die;
03dd20cc
DJ
22129}
22130
5c631832 22131/* Follow reference OFFSET.
673bfd45
DE
22132 On entry *REF_CU is the CU of the source die referencing OFFSET.
22133 On exit *REF_CU is the CU of the result.
22134 Returns NULL if OFFSET is invalid. */
f504f079 22135
f9aca02d 22136static struct die_info *
9c541725 22137follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22138 struct dwarf2_cu **ref_cu)
c906108c 22139{
10b3939b 22140 struct die_info temp_die;
f2f0e013 22141 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22142 struct dwarf2_per_objfile *dwarf2_per_objfile
22143 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22144
348e048f
DE
22145 gdb_assert (cu->per_cu != NULL);
22146
98bfdba5
PA
22147 target_cu = cu;
22148
3019eac3 22149 if (cu->per_cu->is_debug_types)
348e048f
DE
22150 {
22151 /* .debug_types CUs cannot reference anything outside their CU.
22152 If they need to, they have to reference a signatured type via
55f1336d 22153 DW_FORM_ref_sig8. */
4057dfde 22154 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22155 return NULL;
348e048f 22156 }
36586728 22157 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22158 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22159 {
22160 struct dwarf2_per_cu_data *per_cu;
9a619af0 22161
9c541725 22162 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22163 dwarf2_per_objfile);
03dd20cc
DJ
22164
22165 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22166 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22167 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22168
10b3939b
DJ
22169 target_cu = per_cu->cu;
22170 }
98bfdba5
PA
22171 else if (cu->dies == NULL)
22172 {
22173 /* We're loading full DIEs during partial symbol reading. */
22174 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22175 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22176 }
c906108c 22177
f2f0e013 22178 *ref_cu = target_cu;
9c541725 22179 temp_die.sect_off = sect_off;
c24bdb02
KS
22180
22181 if (target_cu != cu)
22182 target_cu->ancestor = cu;
22183
9a3c8263 22184 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22185 &temp_die,
22186 to_underlying (sect_off));
5c631832 22187}
10b3939b 22188
5c631832
JK
22189/* Follow reference attribute ATTR of SRC_DIE.
22190 On entry *REF_CU is the CU of SRC_DIE.
22191 On exit *REF_CU is the CU of the result. */
22192
22193static struct die_info *
ff39bb5e 22194follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22195 struct dwarf2_cu **ref_cu)
22196{
9c541725 22197 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22198 struct dwarf2_cu *cu = *ref_cu;
22199 struct die_info *die;
22200
9c541725 22201 die = follow_die_offset (sect_off,
36586728
TT
22202 (attr->form == DW_FORM_GNU_ref_alt
22203 || cu->per_cu->is_dwz),
22204 ref_cu);
5c631832 22205 if (!die)
9d8780f0
SM
22206 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22207 "at %s [in module %s]"),
22208 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22209 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22210
5c631832
JK
22211 return die;
22212}
22213
d4c9a4f8 22214/* See read.h. */
5c631832
JK
22215
22216struct dwarf2_locexpr_baton
9c541725 22217dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22218 dwarf2_per_cu_data *per_cu,
8b9737bf 22219 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22220 void *baton, bool resolve_abstract_p)
5c631832 22221{
918dd910 22222 struct dwarf2_cu *cu;
5c631832
JK
22223 struct die_info *die;
22224 struct attribute *attr;
22225 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22226 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22227 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22228
918dd910 22229 if (per_cu->cu == NULL)
58f0c718 22230 load_cu (per_cu, false);
918dd910 22231 cu = per_cu->cu;
cc12ce38
DE
22232 if (cu == NULL)
22233 {
22234 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22235 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22236 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22237 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22238 }
918dd910 22239
9c541725 22240 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22241 if (!die)
9d8780f0
SM
22242 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22243 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22244
22245 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22246 if (!attr && resolve_abstract_p
3360b6e7 22247 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22248 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22249 {
22250 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22251 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 22252 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 22253
3360b6e7
TV
22254 for (const auto &cand_off
22255 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22256 {
3360b6e7
TV
22257 struct dwarf2_cu *cand_cu = cu;
22258 struct die_info *cand
22259 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22260 if (!cand
22261 || !cand->parent
e4a62c65
TV
22262 || cand->parent->tag != DW_TAG_subprogram)
22263 continue;
22264
22265 CORE_ADDR pc_low, pc_high;
22266 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22267 if (pc_low == ((CORE_ADDR) -1))
22268 continue;
22269 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22270 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22271 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22272 continue;
22273
22274 die = cand;
22275 attr = dwarf2_attr (die, DW_AT_location, cu);
22276 break;
22277 }
22278 }
22279
5c631832
JK
22280 if (!attr)
22281 {
e103e986
JK
22282 /* DWARF: "If there is no such attribute, then there is no effect.".
22283 DATA is ignored if SIZE is 0. */
5c631832 22284
e103e986 22285 retval.data = NULL;
5c631832
JK
22286 retval.size = 0;
22287 }
cd6c91b4 22288 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22289 {
22290 struct dwarf2_loclist_baton loclist_baton;
22291 CORE_ADDR pc = (*get_frame_pc) (baton);
22292 size_t size;
22293
22294 fill_in_loclist_baton (cu, &loclist_baton, attr);
22295
22296 retval.data = dwarf2_find_location_expression (&loclist_baton,
22297 &size, pc);
22298 retval.size = size;
22299 }
5c631832
JK
22300 else
22301 {
4fc6c0d5 22302 if (!attr->form_is_block ())
9d8780f0 22303 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22304 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22305 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22306
22307 retval.data = DW_BLOCK (attr)->data;
22308 retval.size = DW_BLOCK (attr)->size;
22309 }
22310 retval.per_cu = cu->per_cu;
918dd910 22311
ed2dc618 22312 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22313
5c631832 22314 return retval;
348e048f
DE
22315}
22316
d4c9a4f8 22317/* See read.h. */
8b9737bf
TT
22318
22319struct dwarf2_locexpr_baton
22320dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22321 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22322 CORE_ADDR (*get_frame_pc) (void *baton),
22323 void *baton)
22324{
9c541725 22325 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22326
9c541725 22327 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22328}
22329
b6807d98
TT
22330/* Write a constant of a given type as target-ordered bytes into
22331 OBSTACK. */
22332
22333static const gdb_byte *
22334write_constant_as_bytes (struct obstack *obstack,
22335 enum bfd_endian byte_order,
22336 struct type *type,
22337 ULONGEST value,
22338 LONGEST *len)
22339{
22340 gdb_byte *result;
22341
22342 *len = TYPE_LENGTH (type);
224c3ddb 22343 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22344 store_unsigned_integer (result, *len, byte_order, value);
22345
22346 return result;
22347}
22348
d4c9a4f8 22349/* See read.h. */
b6807d98
TT
22350
22351const gdb_byte *
9c541725 22352dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22353 dwarf2_per_cu_data *per_cu,
22354 obstack *obstack,
b6807d98
TT
22355 LONGEST *len)
22356{
22357 struct dwarf2_cu *cu;
22358 struct die_info *die;
22359 struct attribute *attr;
22360 const gdb_byte *result = NULL;
22361 struct type *type;
22362 LONGEST value;
22363 enum bfd_endian byte_order;
e3b94546 22364 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22365
b6807d98 22366 if (per_cu->cu == NULL)
58f0c718 22367 load_cu (per_cu, false);
b6807d98 22368 cu = per_cu->cu;
cc12ce38
DE
22369 if (cu == NULL)
22370 {
22371 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22372 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22373 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22374 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22375 }
b6807d98 22376
9c541725 22377 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22378 if (!die)
9d8780f0
SM
22379 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22380 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22381
22382 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22383 if (attr == NULL)
22384 return NULL;
22385
e3b94546 22386 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22387 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22388
22389 switch (attr->form)
22390 {
22391 case DW_FORM_addr:
336d760d 22392 case DW_FORM_addrx:
b6807d98
TT
22393 case DW_FORM_GNU_addr_index:
22394 {
22395 gdb_byte *tem;
22396
22397 *len = cu->header.addr_size;
224c3ddb 22398 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22399 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22400 result = tem;
22401 }
22402 break;
22403 case DW_FORM_string:
22404 case DW_FORM_strp:
cf532bd1 22405 case DW_FORM_strx:
b6807d98
TT
22406 case DW_FORM_GNU_str_index:
22407 case DW_FORM_GNU_strp_alt:
22408 /* DW_STRING is already allocated on the objfile obstack, point
22409 directly to it. */
22410 result = (const gdb_byte *) DW_STRING (attr);
22411 *len = strlen (DW_STRING (attr));
22412 break;
22413 case DW_FORM_block1:
22414 case DW_FORM_block2:
22415 case DW_FORM_block4:
22416 case DW_FORM_block:
22417 case DW_FORM_exprloc:
0224619f 22418 case DW_FORM_data16:
b6807d98
TT
22419 result = DW_BLOCK (attr)->data;
22420 *len = DW_BLOCK (attr)->size;
22421 break;
22422
22423 /* The DW_AT_const_value attributes are supposed to carry the
22424 symbol's value "represented as it would be on the target
22425 architecture." By the time we get here, it's already been
22426 converted to host endianness, so we just need to sign- or
22427 zero-extend it as appropriate. */
22428 case DW_FORM_data1:
22429 type = die_type (die, cu);
22430 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22431 if (result == NULL)
22432 result = write_constant_as_bytes (obstack, byte_order,
22433 type, value, len);
22434 break;
22435 case DW_FORM_data2:
22436 type = die_type (die, cu);
22437 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22438 if (result == NULL)
22439 result = write_constant_as_bytes (obstack, byte_order,
22440 type, value, len);
22441 break;
22442 case DW_FORM_data4:
22443 type = die_type (die, cu);
22444 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22445 if (result == NULL)
22446 result = write_constant_as_bytes (obstack, byte_order,
22447 type, value, len);
22448 break;
22449 case DW_FORM_data8:
22450 type = die_type (die, cu);
22451 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22452 if (result == NULL)
22453 result = write_constant_as_bytes (obstack, byte_order,
22454 type, value, len);
22455 break;
22456
22457 case DW_FORM_sdata:
663c44ac 22458 case DW_FORM_implicit_const:
b6807d98
TT
22459 type = die_type (die, cu);
22460 result = write_constant_as_bytes (obstack, byte_order,
22461 type, DW_SND (attr), len);
22462 break;
22463
22464 case DW_FORM_udata:
22465 type = die_type (die, cu);
22466 result = write_constant_as_bytes (obstack, byte_order,
22467 type, DW_UNSND (attr), len);
22468 break;
22469
22470 default:
b98664d3 22471 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22472 dwarf_form_name (attr->form));
22473 break;
22474 }
22475
22476 return result;
22477}
22478
d4c9a4f8 22479/* See read.h. */
7942e96e
AA
22480
22481struct type *
9c541725 22482dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22483 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22484{
22485 struct dwarf2_cu *cu;
22486 struct die_info *die;
22487
7942e96e 22488 if (per_cu->cu == NULL)
58f0c718 22489 load_cu (per_cu, false);
7942e96e
AA
22490 cu = per_cu->cu;
22491 if (!cu)
22492 return NULL;
22493
9c541725 22494 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22495 if (!die)
22496 return NULL;
22497
22498 return die_type (die, cu);
22499}
22500
8cb5117c 22501/* See read.h. */
8a9b8146
TT
22502
22503struct type *
b64f50a1 22504dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22505 struct dwarf2_per_cu_data *per_cu)
22506{
9c541725 22507 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22508 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22509}
22510
ac9ec31b 22511/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22512 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22513 On exit *REF_CU is the CU of the result.
22514 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22515
22516static struct die_info *
ac9ec31b
DE
22517follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22518 struct dwarf2_cu **ref_cu)
348e048f 22519{
348e048f 22520 struct die_info temp_die;
c24bdb02 22521 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22522 struct die_info *die;
22523
ac9ec31b
DE
22524 /* While it might be nice to assert sig_type->type == NULL here,
22525 we can get here for DW_AT_imported_declaration where we need
22526 the DIE not the type. */
348e048f
DE
22527
22528 /* If necessary, add it to the queue and load its DIEs. */
22529
95554aad 22530 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22531 read_signatured_type (sig_type);
348e048f 22532
348e048f 22533 sig_cu = sig_type->per_cu.cu;
69d751e3 22534 gdb_assert (sig_cu != NULL);
9c541725
PA
22535 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22536 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22537 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22538 to_underlying (temp_die.sect_off));
348e048f
DE
22539 if (die)
22540 {
ed2dc618 22541 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22542 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22543
796a7ff8
DE
22544 /* For .gdb_index version 7 keep track of included TUs.
22545 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22546 if (dwarf2_per_objfile->index_table != NULL
22547 && dwarf2_per_objfile->index_table->version <= 7)
22548 {
ae640021 22549 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22550 }
22551
348e048f 22552 *ref_cu = sig_cu;
c24bdb02
KS
22553 if (sig_cu != cu)
22554 sig_cu->ancestor = cu;
22555
348e048f
DE
22556 return die;
22557 }
22558
ac9ec31b
DE
22559 return NULL;
22560}
22561
22562/* Follow signatured type referenced by ATTR in SRC_DIE.
22563 On entry *REF_CU is the CU of SRC_DIE.
22564 On exit *REF_CU is the CU of the result.
22565 The result is the DIE of the type.
22566 If the referenced type cannot be found an error is thrown. */
22567
22568static struct die_info *
ff39bb5e 22569follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22570 struct dwarf2_cu **ref_cu)
22571{
22572 ULONGEST signature = DW_SIGNATURE (attr);
22573 struct signatured_type *sig_type;
22574 struct die_info *die;
22575
22576 gdb_assert (attr->form == DW_FORM_ref_sig8);
22577
a2ce51a0 22578 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22579 /* sig_type will be NULL if the signatured type is missing from
22580 the debug info. */
22581 if (sig_type == NULL)
22582 {
22583 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22584 " from DIE at %s [in module %s]"),
22585 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22586 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22587 }
22588
22589 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22590 if (die == NULL)
22591 {
22592 dump_die_for_error (src_die);
22593 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22594 " from DIE at %s [in module %s]"),
22595 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22596 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22597 }
22598
22599 return die;
22600}
22601
22602/* Get the type specified by SIGNATURE referenced in DIE/CU,
22603 reading in and processing the type unit if necessary. */
22604
22605static struct type *
22606get_signatured_type (struct die_info *die, ULONGEST signature,
22607 struct dwarf2_cu *cu)
22608{
518817b3
SM
22609 struct dwarf2_per_objfile *dwarf2_per_objfile
22610 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22611 struct signatured_type *sig_type;
22612 struct dwarf2_cu *type_cu;
22613 struct die_info *type_die;
22614 struct type *type;
22615
a2ce51a0 22616 sig_type = lookup_signatured_type (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 {
b98664d3 22621 complaint (_("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 (die->sect_off),
4262abfb 22624 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22625 return build_error_marker_type (cu, die);
22626 }
22627
22628 /* If we already know the type we're done. */
22629 if (sig_type->type != NULL)
22630 return sig_type->type;
22631
22632 type_cu = cu;
22633 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22634 if (type_die != NULL)
22635 {
22636 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22637 is created. This is important, for example, because for c++ classes
22638 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22639 type = read_type_die (type_die, type_cu);
22640 if (type == NULL)
22641 {
b98664d3 22642 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22643 " referenced from DIE at %s [in module %s]"),
22644 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22645 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22646 type = build_error_marker_type (cu, die);
22647 }
22648 }
22649 else
22650 {
b98664d3 22651 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22652 " from DIE at %s [in module %s]"),
22653 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22654 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22655 type = build_error_marker_type (cu, die);
22656 }
22657 sig_type->type = type;
22658
22659 return type;
22660}
22661
22662/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22663 reading in and processing the type unit if necessary. */
22664
22665static struct type *
ff39bb5e 22666get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22667 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22668{
22669 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22670 if (attr->form_is_ref ())
ac9ec31b
DE
22671 {
22672 struct dwarf2_cu *type_cu = cu;
22673 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22674
22675 return read_type_die (type_die, type_cu);
22676 }
22677 else if (attr->form == DW_FORM_ref_sig8)
22678 {
22679 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22680 }
22681 else
22682 {
518817b3
SM
22683 struct dwarf2_per_objfile *dwarf2_per_objfile
22684 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22685
b98664d3 22686 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22687 " at %s [in module %s]"),
22688 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22689 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22690 return build_error_marker_type (cu, die);
22691 }
348e048f
DE
22692}
22693
e5fe5e75 22694/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22695
22696static void
e5fe5e75 22697load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22698{
52dc124a 22699 struct signatured_type *sig_type;
348e048f 22700
f4dc4d17 22701 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22702 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22703
6721b2ec
DE
22704 /* We have the per_cu, but we need the signatured_type.
22705 Fortunately this is an easy translation. */
22706 gdb_assert (per_cu->is_debug_types);
22707 sig_type = (struct signatured_type *) per_cu;
348e048f 22708
6721b2ec 22709 gdb_assert (per_cu->cu == NULL);
348e048f 22710
52dc124a 22711 read_signatured_type (sig_type);
348e048f 22712
6721b2ec 22713 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22714}
22715
3019eac3
DE
22716/* Read in a signatured type and build its CU and DIEs.
22717 If the type is a stub for the real type in a DWO file,
22718 read in the real type from the DWO file as well. */
dee91e82
DE
22719
22720static void
22721read_signatured_type (struct signatured_type *sig_type)
22722{
22723 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22724
3019eac3 22725 gdb_assert (per_cu->is_debug_types);
dee91e82 22726 gdb_assert (per_cu->cu == NULL);
348e048f 22727
6751ebae 22728 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22729
22730 if (!reader.dummy_p)
22731 {
22732 struct dwarf2_cu *cu = reader.cu;
22733 const gdb_byte *info_ptr = reader.info_ptr;
22734
22735 gdb_assert (cu->die_hash == NULL);
22736 cu->die_hash =
22737 htab_create_alloc_ex (cu->header.length / 12,
22738 die_hash,
22739 die_eq,
22740 NULL,
22741 &cu->comp_unit_obstack,
22742 hashtab_obstack_allocate,
22743 dummy_obstack_deallocate);
22744
3e225074 22745 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22746 reader.comp_unit_die->child
22747 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22748 reader.comp_unit_die);
22749 cu->dies = reader.comp_unit_die;
22750 /* comp_unit_die is not stored in die_hash, no need. */
22751
22752 /* We try not to read any attributes in this function, because
22753 not all CUs needed for references have been loaded yet, and
22754 symbol table processing isn't initialized. But we have to
22755 set the CU language, or we won't be able to build types
22756 correctly. Similarly, if we do not read the producer, we can
22757 not apply producer-specific interpretation. */
22758 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22759
22760 reader.keep ();
c0ab21c2
TT
22761 }
22762
7ee85ab1 22763 sig_type->per_cu.tu_read = 1;
c906108c
SS
22764}
22765
c906108c
SS
22766/* Decode simple location descriptions.
22767 Given a pointer to a dwarf block that defines a location, compute
22768 the location and return the value.
22769
4cecd739
DJ
22770 NOTE drow/2003-11-18: This function is called in two situations
22771 now: for the address of static or global variables (partial symbols
22772 only) and for offsets into structures which are expected to be
22773 (more or less) constant. The partial symbol case should go away,
22774 and only the constant case should remain. That will let this
22775 function complain more accurately. A few special modes are allowed
22776 without complaint for global variables (for instance, global
22777 register values and thread-local values).
c906108c
SS
22778
22779 A location description containing no operations indicates that the
4cecd739 22780 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22781 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22782 callers will only want a very basic result and this can become a
21ae7a4d
JK
22783 complaint.
22784
22785 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22786
22787static CORE_ADDR
e7c27a73 22788decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22789{
518817b3 22790 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22791 size_t i;
22792 size_t size = blk->size;
d521ce57 22793 const gdb_byte *data = blk->data;
21ae7a4d
JK
22794 CORE_ADDR stack[64];
22795 int stacki;
22796 unsigned int bytes_read, unsnd;
22797 gdb_byte op;
c906108c 22798
21ae7a4d
JK
22799 i = 0;
22800 stacki = 0;
22801 stack[stacki] = 0;
22802 stack[++stacki] = 0;
22803
22804 while (i < size)
22805 {
22806 op = data[i++];
22807 switch (op)
22808 {
22809 case DW_OP_lit0:
22810 case DW_OP_lit1:
22811 case DW_OP_lit2:
22812 case DW_OP_lit3:
22813 case DW_OP_lit4:
22814 case DW_OP_lit5:
22815 case DW_OP_lit6:
22816 case DW_OP_lit7:
22817 case DW_OP_lit8:
22818 case DW_OP_lit9:
22819 case DW_OP_lit10:
22820 case DW_OP_lit11:
22821 case DW_OP_lit12:
22822 case DW_OP_lit13:
22823 case DW_OP_lit14:
22824 case DW_OP_lit15:
22825 case DW_OP_lit16:
22826 case DW_OP_lit17:
22827 case DW_OP_lit18:
22828 case DW_OP_lit19:
22829 case DW_OP_lit20:
22830 case DW_OP_lit21:
22831 case DW_OP_lit22:
22832 case DW_OP_lit23:
22833 case DW_OP_lit24:
22834 case DW_OP_lit25:
22835 case DW_OP_lit26:
22836 case DW_OP_lit27:
22837 case DW_OP_lit28:
22838 case DW_OP_lit29:
22839 case DW_OP_lit30:
22840 case DW_OP_lit31:
22841 stack[++stacki] = op - DW_OP_lit0;
22842 break;
f1bea926 22843
21ae7a4d
JK
22844 case DW_OP_reg0:
22845 case DW_OP_reg1:
22846 case DW_OP_reg2:
22847 case DW_OP_reg3:
22848 case DW_OP_reg4:
22849 case DW_OP_reg5:
22850 case DW_OP_reg6:
22851 case DW_OP_reg7:
22852 case DW_OP_reg8:
22853 case DW_OP_reg9:
22854 case DW_OP_reg10:
22855 case DW_OP_reg11:
22856 case DW_OP_reg12:
22857 case DW_OP_reg13:
22858 case DW_OP_reg14:
22859 case DW_OP_reg15:
22860 case DW_OP_reg16:
22861 case DW_OP_reg17:
22862 case DW_OP_reg18:
22863 case DW_OP_reg19:
22864 case DW_OP_reg20:
22865 case DW_OP_reg21:
22866 case DW_OP_reg22:
22867 case DW_OP_reg23:
22868 case DW_OP_reg24:
22869 case DW_OP_reg25:
22870 case DW_OP_reg26:
22871 case DW_OP_reg27:
22872 case DW_OP_reg28:
22873 case DW_OP_reg29:
22874 case DW_OP_reg30:
22875 case DW_OP_reg31:
22876 stack[++stacki] = op - DW_OP_reg0;
22877 if (i < size)
22878 dwarf2_complex_location_expr_complaint ();
22879 break;
c906108c 22880
21ae7a4d
JK
22881 case DW_OP_regx:
22882 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22883 i += bytes_read;
22884 stack[++stacki] = unsnd;
22885 if (i < size)
22886 dwarf2_complex_location_expr_complaint ();
22887 break;
c906108c 22888
21ae7a4d 22889 case DW_OP_addr:
c8a7a66f
TT
22890 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22891 &bytes_read);
21ae7a4d
JK
22892 i += bytes_read;
22893 break;
d53d4ac5 22894
21ae7a4d
JK
22895 case DW_OP_const1u:
22896 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22897 i += 1;
22898 break;
22899
22900 case DW_OP_const1s:
22901 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22902 i += 1;
22903 break;
22904
22905 case DW_OP_const2u:
22906 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22907 i += 2;
22908 break;
22909
22910 case DW_OP_const2s:
22911 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22912 i += 2;
22913 break;
d53d4ac5 22914
21ae7a4d
JK
22915 case DW_OP_const4u:
22916 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22917 i += 4;
22918 break;
22919
22920 case DW_OP_const4s:
22921 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22922 i += 4;
22923 break;
22924
585861ea
JK
22925 case DW_OP_const8u:
22926 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22927 i += 8;
22928 break;
22929
21ae7a4d
JK
22930 case DW_OP_constu:
22931 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22932 &bytes_read);
22933 i += bytes_read;
22934 break;
22935
22936 case DW_OP_consts:
22937 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22938 i += bytes_read;
22939 break;
22940
22941 case DW_OP_dup:
22942 stack[stacki + 1] = stack[stacki];
22943 stacki++;
22944 break;
22945
22946 case DW_OP_plus:
22947 stack[stacki - 1] += stack[stacki];
22948 stacki--;
22949 break;
22950
22951 case DW_OP_plus_uconst:
22952 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22953 &bytes_read);
22954 i += bytes_read;
22955 break;
22956
22957 case DW_OP_minus:
22958 stack[stacki - 1] -= stack[stacki];
22959 stacki--;
22960 break;
22961
22962 case DW_OP_deref:
22963 /* If we're not the last op, then we definitely can't encode
22964 this using GDB's address_class enum. This is valid for partial
22965 global symbols, although the variable's address will be bogus
22966 in the psymtab. */
22967 if (i < size)
22968 dwarf2_complex_location_expr_complaint ();
22969 break;
22970
22971 case DW_OP_GNU_push_tls_address:
4aa4e28b 22972 case DW_OP_form_tls_address:
21ae7a4d
JK
22973 /* The top of the stack has the offset from the beginning
22974 of the thread control block at which the variable is located. */
22975 /* Nothing should follow this operator, so the top of stack would
22976 be returned. */
22977 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22978 address will be bogus in the psymtab. Make it always at least
22979 non-zero to not look as a variable garbage collected by linker
22980 which have DW_OP_addr 0. */
21ae7a4d
JK
22981 if (i < size)
22982 dwarf2_complex_location_expr_complaint ();
585861ea 22983 stack[stacki]++;
21ae7a4d
JK
22984 break;
22985
22986 case DW_OP_GNU_uninit:
22987 break;
22988
336d760d 22989 case DW_OP_addrx:
3019eac3 22990 case DW_OP_GNU_addr_index:
49f6c839 22991 case DW_OP_GNU_const_index:
3019eac3
DE
22992 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22993 &bytes_read);
22994 i += bytes_read;
22995 break;
22996
21ae7a4d
JK
22997 default:
22998 {
f39c6ffd 22999 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23000
23001 if (name)
b98664d3 23002 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23003 name);
23004 else
b98664d3 23005 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23006 op);
23007 }
23008
23009 return (stack[stacki]);
d53d4ac5 23010 }
3c6e0cb3 23011
21ae7a4d
JK
23012 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23013 outside of the allocated space. Also enforce minimum>0. */
23014 if (stacki >= ARRAY_SIZE (stack) - 1)
23015 {
b98664d3 23016 complaint (_("location description stack overflow"));
21ae7a4d
JK
23017 return 0;
23018 }
23019
23020 if (stacki <= 0)
23021 {
b98664d3 23022 complaint (_("location description stack underflow"));
21ae7a4d
JK
23023 return 0;
23024 }
23025 }
23026 return (stack[stacki]);
c906108c
SS
23027}
23028
23029/* memory allocation interface */
23030
c906108c 23031static struct dwarf_block *
7b5a2f43 23032dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23033{
8d749320 23034 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23035}
23036
c906108c 23037static struct die_info *
b60c80d6 23038dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23039{
23040 struct die_info *die;
b60c80d6
DJ
23041 size_t size = sizeof (struct die_info);
23042
23043 if (num_attrs > 1)
23044 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23045
b60c80d6 23046 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23047 memset (die, 0, sizeof (struct die_info));
23048 return (die);
23049}
2e276125
JB
23050
23051\f
23052/* Macro support. */
23053
2e276125 23054static struct macro_source_file *
804d2729
TT
23055macro_start_file (struct dwarf2_cu *cu,
23056 int file, int line,
2e276125 23057 struct macro_source_file *current_file,
43f3e411 23058 struct line_header *lh)
2e276125 23059{
233d95b5 23060 /* File name relative to the compilation directory of this source file. */
03075812 23061 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
2e276125 23062
2e276125 23063 if (! current_file)
abc9d0dc 23064 {
fc474241
DE
23065 /* Note: We don't create a macro table for this compilation unit
23066 at all until we actually get a filename. */
c24bdb02 23067 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 23068
abc9d0dc
TT
23069 /* If we have no current file, then this must be the start_file
23070 directive for the compilation unit's main source file. */
03075812 23071 current_file = macro_set_main (macro_table, file_name.get ());
fc474241 23072 macro_define_special (macro_table);
abc9d0dc 23073 }
2e276125 23074 else
03075812 23075 current_file = macro_include (current_file, line, file_name.get ());
6e70227d 23076
2e276125
JB
23077 return current_file;
23078}
23079
2e276125
JB
23080static const char *
23081consume_improper_spaces (const char *p, const char *body)
23082{
23083 if (*p == ' ')
23084 {
b98664d3 23085 complaint (_("macro definition contains spaces "
3e43a32a 23086 "in formal argument list:\n`%s'"),
4d3c2250 23087 body);
2e276125
JB
23088
23089 while (*p == ' ')
23090 p++;
23091 }
23092
23093 return p;
23094}
23095
23096
23097static void
23098parse_macro_definition (struct macro_source_file *file, int line,
23099 const char *body)
23100{
23101 const char *p;
23102
23103 /* The body string takes one of two forms. For object-like macro
23104 definitions, it should be:
23105
23106 <macro name> " " <definition>
23107
23108 For function-like macro definitions, it should be:
23109
23110 <macro name> "() " <definition>
23111 or
23112 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23113
23114 Spaces may appear only where explicitly indicated, and in the
23115 <definition>.
23116
23117 The Dwarf 2 spec says that an object-like macro's name is always
23118 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23119 the space when the macro's definition is the empty string.
2e276125
JB
23120
23121 The Dwarf 2 spec says that there should be no spaces between the
23122 formal arguments in a function-like macro's formal argument list,
23123 but versions of GCC around March 2002 include spaces after the
23124 commas. */
23125
23126
23127 /* Find the extent of the macro name. The macro name is terminated
23128 by either a space or null character (for an object-like macro) or
23129 an opening paren (for a function-like macro). */
23130 for (p = body; *p; p++)
23131 if (*p == ' ' || *p == '(')
23132 break;
23133
23134 if (*p == ' ' || *p == '\0')
23135 {
23136 /* It's an object-like macro. */
23137 int name_len = p - body;
456e800a 23138 std::string name (body, name_len);
2e276125
JB
23139 const char *replacement;
23140
23141 if (*p == ' ')
23142 replacement = body + name_len + 1;
23143 else
23144 {
4d3c2250 23145 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23146 replacement = body + name_len;
23147 }
6e70227d 23148
456e800a 23149 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
23150 }
23151 else if (*p == '(')
23152 {
23153 /* It's a function-like macro. */
456e800a 23154 std::string name (body, p - body);
2e276125
JB
23155 int argc = 0;
23156 int argv_size = 1;
8d749320 23157 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23158
23159 p++;
23160
23161 p = consume_improper_spaces (p, body);
23162
23163 /* Parse the formal argument list. */
23164 while (*p && *p != ')')
23165 {
23166 /* Find the extent of the current argument name. */
23167 const char *arg_start = p;
23168
23169 while (*p && *p != ',' && *p != ')' && *p != ' ')
23170 p++;
23171
23172 if (! *p || p == arg_start)
4d3c2250 23173 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23174 else
23175 {
23176 /* Make sure argv has room for the new argument. */
23177 if (argc >= argv_size)
23178 {
23179 argv_size *= 2;
224c3ddb 23180 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23181 }
23182
3f8a7804 23183 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23184 }
23185
23186 p = consume_improper_spaces (p, body);
23187
23188 /* Consume the comma, if present. */
23189 if (*p == ',')
23190 {
23191 p++;
23192
23193 p = consume_improper_spaces (p, body);
23194 }
23195 }
23196
23197 if (*p == ')')
23198 {
23199 p++;
23200
23201 if (*p == ' ')
23202 /* Perfectly formed definition, no complaints. */
456e800a 23203 macro_define_function (file, line, name.c_str (),
6e70227d 23204 argc, (const char **) argv,
2e276125
JB
23205 p + 1);
23206 else if (*p == '\0')
23207 {
23208 /* Complain, but do define it. */
4d3c2250 23209 dwarf2_macro_malformed_definition_complaint (body);
456e800a 23210 macro_define_function (file, line, name.c_str (),
6e70227d 23211 argc, (const char **) argv,
2e276125
JB
23212 p);
23213 }
23214 else
23215 /* Just complain. */
4d3c2250 23216 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23217 }
23218 else
23219 /* Just complain. */
4d3c2250 23220 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23221
2e276125
JB
23222 {
23223 int i;
23224
23225 for (i = 0; i < argc; i++)
23226 xfree (argv[i]);
23227 }
23228 xfree (argv);
23229 }
23230 else
4d3c2250 23231 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23232}
23233
cf2c3c16
TT
23234/* Skip some bytes from BYTES according to the form given in FORM.
23235 Returns the new pointer. */
2e276125 23236
d521ce57
TT
23237static const gdb_byte *
23238skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23239 enum dwarf_form form,
23240 unsigned int offset_size,
23241 struct dwarf2_section_info *section)
2e276125 23242{
cf2c3c16 23243 unsigned int bytes_read;
2e276125 23244
cf2c3c16 23245 switch (form)
2e276125 23246 {
cf2c3c16
TT
23247 case DW_FORM_data1:
23248 case DW_FORM_flag:
23249 ++bytes;
23250 break;
23251
23252 case DW_FORM_data2:
23253 bytes += 2;
23254 break;
23255
23256 case DW_FORM_data4:
23257 bytes += 4;
23258 break;
23259
23260 case DW_FORM_data8:
23261 bytes += 8;
23262 break;
23263
0224619f
JK
23264 case DW_FORM_data16:
23265 bytes += 16;
23266 break;
23267
cf2c3c16
TT
23268 case DW_FORM_string:
23269 read_direct_string (abfd, bytes, &bytes_read);
23270 bytes += bytes_read;
23271 break;
23272
23273 case DW_FORM_sec_offset:
23274 case DW_FORM_strp:
36586728 23275 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23276 bytes += offset_size;
23277 break;
23278
23279 case DW_FORM_block:
23280 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23281 bytes += bytes_read;
23282 break;
23283
23284 case DW_FORM_block1:
23285 bytes += 1 + read_1_byte (abfd, bytes);
23286 break;
23287 case DW_FORM_block2:
23288 bytes += 2 + read_2_bytes (abfd, bytes);
23289 break;
23290 case DW_FORM_block4:
23291 bytes += 4 + read_4_bytes (abfd, bytes);
23292 break;
23293
336d760d 23294 case DW_FORM_addrx:
cf2c3c16 23295 case DW_FORM_sdata:
cf532bd1 23296 case DW_FORM_strx:
cf2c3c16 23297 case DW_FORM_udata:
3019eac3
DE
23298 case DW_FORM_GNU_addr_index:
23299 case DW_FORM_GNU_str_index:
d521ce57 23300 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23301 if (bytes == NULL)
23302 {
23303 dwarf2_section_buffer_overflow_complaint (section);
23304 return NULL;
23305 }
cf2c3c16
TT
23306 break;
23307
663c44ac
JK
23308 case DW_FORM_implicit_const:
23309 break;
23310
cf2c3c16
TT
23311 default:
23312 {
b98664d3 23313 complaint (_("invalid form 0x%x in `%s'"),
96b79293 23314 form, section->get_name ());
cf2c3c16
TT
23315 return NULL;
23316 }
2e276125
JB
23317 }
23318
cf2c3c16
TT
23319 return bytes;
23320}
757a13d0 23321
cf2c3c16
TT
23322/* A helper for dwarf_decode_macros that handles skipping an unknown
23323 opcode. Returns an updated pointer to the macro data buffer; or,
23324 on error, issues a complaint and returns NULL. */
757a13d0 23325
d521ce57 23326static const gdb_byte *
cf2c3c16 23327skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23328 const gdb_byte **opcode_definitions,
23329 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23330 bfd *abfd,
23331 unsigned int offset_size,
23332 struct dwarf2_section_info *section)
23333{
23334 unsigned int bytes_read, i;
23335 unsigned long arg;
d521ce57 23336 const gdb_byte *defn;
2e276125 23337
cf2c3c16 23338 if (opcode_definitions[opcode] == NULL)
2e276125 23339 {
b98664d3 23340 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
23341 opcode);
23342 return NULL;
23343 }
2e276125 23344
cf2c3c16
TT
23345 defn = opcode_definitions[opcode];
23346 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23347 defn += bytes_read;
2e276125 23348
cf2c3c16
TT
23349 for (i = 0; i < arg; ++i)
23350 {
aead7601
SM
23351 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23352 (enum dwarf_form) defn[i], offset_size,
f664829e 23353 section);
cf2c3c16
TT
23354 if (mac_ptr == NULL)
23355 {
23356 /* skip_form_bytes already issued the complaint. */
23357 return NULL;
23358 }
23359 }
757a13d0 23360
cf2c3c16
TT
23361 return mac_ptr;
23362}
757a13d0 23363
cf2c3c16
TT
23364/* A helper function which parses the header of a macro section.
23365 If the macro section is the extended (for now called "GNU") type,
23366 then this updates *OFFSET_SIZE. Returns a pointer to just after
23367 the header, or issues a complaint and returns NULL on error. */
757a13d0 23368
d521ce57
TT
23369static const gdb_byte *
23370dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 23371 bfd *abfd,
d521ce57 23372 const gdb_byte *mac_ptr,
cf2c3c16
TT
23373 unsigned int *offset_size,
23374 int section_is_gnu)
23375{
23376 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 23377
cf2c3c16
TT
23378 if (section_is_gnu)
23379 {
23380 unsigned int version, flags;
757a13d0 23381
cf2c3c16 23382 version = read_2_bytes (abfd, mac_ptr);
0af92d60 23383 if (version != 4 && version != 5)
cf2c3c16 23384 {
b98664d3 23385 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
23386 version);
23387 return NULL;
23388 }
23389 mac_ptr += 2;
757a13d0 23390
cf2c3c16
TT
23391 flags = read_1_byte (abfd, mac_ptr);
23392 ++mac_ptr;
23393 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 23394
cf2c3c16
TT
23395 if ((flags & 2) != 0)
23396 /* We don't need the line table offset. */
23397 mac_ptr += *offset_size;
757a13d0 23398
cf2c3c16
TT
23399 /* Vendor opcode descriptions. */
23400 if ((flags & 4) != 0)
23401 {
23402 unsigned int i, count;
757a13d0 23403
cf2c3c16
TT
23404 count = read_1_byte (abfd, mac_ptr);
23405 ++mac_ptr;
23406 for (i = 0; i < count; ++i)
23407 {
23408 unsigned int opcode, bytes_read;
23409 unsigned long arg;
23410
23411 opcode = read_1_byte (abfd, mac_ptr);
23412 ++mac_ptr;
23413 opcode_definitions[opcode] = mac_ptr;
23414 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23415 mac_ptr += bytes_read;
23416 mac_ptr += arg;
23417 }
757a13d0 23418 }
cf2c3c16 23419 }
757a13d0 23420
cf2c3c16
TT
23421 return mac_ptr;
23422}
757a13d0 23423
cf2c3c16 23424/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 23425 including DW_MACRO_import. */
cf2c3c16
TT
23426
23427static void
804d2729 23428dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 23429 bfd *abfd,
d521ce57 23430 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 23431 struct macro_source_file *current_file,
43f3e411 23432 struct line_header *lh,
cf2c3c16 23433 struct dwarf2_section_info *section,
36586728 23434 int section_is_gnu, int section_is_dwz,
cf2c3c16 23435 unsigned int offset_size,
8fc3fc34 23436 htab_t include_hash)
cf2c3c16 23437{
804d2729
TT
23438 struct dwarf2_per_objfile *dwarf2_per_objfile
23439 = cu->per_cu->dwarf2_per_objfile;
4d663531 23440 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
23441 enum dwarf_macro_record_type macinfo_type;
23442 int at_commandline;
d521ce57 23443 const gdb_byte *opcode_definitions[256];
757a13d0 23444
cf2c3c16
TT
23445 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23446 &offset_size, section_is_gnu);
23447 if (mac_ptr == NULL)
23448 {
23449 /* We already issued a complaint. */
23450 return;
23451 }
757a13d0
JK
23452
23453 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23454 GDB is still reading the definitions from command line. First
23455 DW_MACINFO_start_file will need to be ignored as it was already executed
23456 to create CURRENT_FILE for the main source holding also the command line
23457 definitions. On first met DW_MACINFO_start_file this flag is reset to
23458 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23459
23460 at_commandline = 1;
23461
23462 do
23463 {
23464 /* Do we at least have room for a macinfo type byte? */
23465 if (mac_ptr >= mac_end)
23466 {
f664829e 23467 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
23468 break;
23469 }
23470
aead7601 23471 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
23472 mac_ptr++;
23473
cf2c3c16
TT
23474 /* Note that we rely on the fact that the corresponding GNU and
23475 DWARF constants are the same. */
132448f8
SM
23476 DIAGNOSTIC_PUSH
23477 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
23478 switch (macinfo_type)
23479 {
23480 /* A zero macinfo type indicates the end of the macro
23481 information. */
23482 case 0:
23483 break;
2e276125 23484
0af92d60
JK
23485 case DW_MACRO_define:
23486 case DW_MACRO_undef:
23487 case DW_MACRO_define_strp:
23488 case DW_MACRO_undef_strp:
23489 case DW_MACRO_define_sup:
23490 case DW_MACRO_undef_sup:
2e276125 23491 {
891d2f0b 23492 unsigned int bytes_read;
2e276125 23493 int line;
d521ce57 23494 const char *body;
cf2c3c16 23495 int is_define;
2e276125 23496
cf2c3c16
TT
23497 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23498 mac_ptr += bytes_read;
23499
0af92d60
JK
23500 if (macinfo_type == DW_MACRO_define
23501 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
23502 {
23503 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23504 mac_ptr += bytes_read;
23505 }
23506 else
23507 {
23508 LONGEST str_offset;
23509
24aa364d 23510 str_offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16 23511 mac_ptr += offset_size;
2e276125 23512
0af92d60
JK
23513 if (macinfo_type == DW_MACRO_define_sup
23514 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 23515 || section_is_dwz)
36586728 23516 {
ed2dc618
SM
23517 struct dwz_file *dwz
23518 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 23519
ed2dc618
SM
23520 body = read_indirect_string_from_dwz (objfile,
23521 dwz, str_offset);
36586728
TT
23522 }
23523 else
ed2dc618
SM
23524 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23525 abfd, str_offset);
cf2c3c16
TT
23526 }
23527
0af92d60
JK
23528 is_define = (macinfo_type == DW_MACRO_define
23529 || macinfo_type == DW_MACRO_define_strp
23530 || macinfo_type == DW_MACRO_define_sup);
2e276125 23531 if (! current_file)
757a13d0
JK
23532 {
23533 /* DWARF violation as no main source is present. */
b98664d3 23534 complaint (_("debug info with no main source gives macro %s "
757a13d0 23535 "on line %d: %s"),
cf2c3c16
TT
23536 is_define ? _("definition") : _("undefinition"),
23537 line, body);
757a13d0
JK
23538 break;
23539 }
3e43a32a
MS
23540 if ((line == 0 && !at_commandline)
23541 || (line != 0 && at_commandline))
b98664d3 23542 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 23543 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 23544 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
23545 line == 0 ? _("zero") : _("non-zero"), line, body);
23546
955b06fa 23547 if (body == NULL)
7bede828 23548 {
955b06fa
SDJ
23549 /* Fedora's rpm-build's "debugedit" binary
23550 corrupted .debug_macro sections.
23551
23552 For more info, see
23553 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23554 complaint (_("debug info gives %s invalid macro %s "
23555 "without body (corrupted?) at line %d "
23556 "on file %s"),
23557 at_commandline ? _("command-line") : _("in-file"),
23558 is_define ? _("definition") : _("undefinition"),
23559 line, current_file->filename);
7bede828 23560 }
955b06fa
SDJ
23561 else if (is_define)
23562 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
23563 else
23564 {
0af92d60
JK
23565 gdb_assert (macinfo_type == DW_MACRO_undef
23566 || macinfo_type == DW_MACRO_undef_strp
23567 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
23568 macro_undef (current_file, line, body);
23569 }
2e276125
JB
23570 }
23571 break;
23572
0af92d60 23573 case DW_MACRO_start_file:
2e276125 23574 {
891d2f0b 23575 unsigned int bytes_read;
2e276125
JB
23576 int line, file;
23577
23578 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23579 mac_ptr += bytes_read;
23580 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23581 mac_ptr += bytes_read;
23582
3e43a32a
MS
23583 if ((line == 0 && !at_commandline)
23584 || (line != 0 && at_commandline))
b98664d3 23585 complaint (_("debug info gives source %d included "
757a13d0
JK
23586 "from %s at %s line %d"),
23587 file, at_commandline ? _("command-line") : _("file"),
23588 line == 0 ? _("zero") : _("non-zero"), line);
23589
23590 if (at_commandline)
23591 {
0af92d60 23592 /* This DW_MACRO_start_file was executed in the
cf2c3c16 23593 pass one. */
757a13d0
JK
23594 at_commandline = 0;
23595 }
23596 else
804d2729
TT
23597 current_file = macro_start_file (cu, file, line, current_file,
23598 lh);
2e276125
JB
23599 }
23600 break;
23601
0af92d60 23602 case DW_MACRO_end_file:
2e276125 23603 if (! current_file)
b98664d3 23604 complaint (_("macro debug info has an unmatched "
3e43a32a 23605 "`close_file' directive"));
2e276125
JB
23606 else
23607 {
23608 current_file = current_file->included_by;
23609 if (! current_file)
23610 {
cf2c3c16 23611 enum dwarf_macro_record_type next_type;
2e276125
JB
23612
23613 /* GCC circa March 2002 doesn't produce the zero
23614 type byte marking the end of the compilation
23615 unit. Complain if it's not there, but exit no
23616 matter what. */
23617
23618 /* Do we at least have room for a macinfo type byte? */
23619 if (mac_ptr >= mac_end)
23620 {
f664829e 23621 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
23622 return;
23623 }
23624
23625 /* We don't increment mac_ptr here, so this is just
23626 a look-ahead. */
aead7601
SM
23627 next_type
23628 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23629 mac_ptr);
2e276125 23630 if (next_type != 0)
b98664d3 23631 complaint (_("no terminating 0-type entry for "
3e43a32a 23632 "macros in `.debug_macinfo' section"));
2e276125
JB
23633
23634 return;
23635 }
23636 }
23637 break;
23638
0af92d60
JK
23639 case DW_MACRO_import:
23640 case DW_MACRO_import_sup:
cf2c3c16
TT
23641 {
23642 LONGEST offset;
8fc3fc34 23643 void **slot;
a036ba48
TT
23644 bfd *include_bfd = abfd;
23645 struct dwarf2_section_info *include_section = section;
d521ce57 23646 const gdb_byte *include_mac_end = mac_end;
a036ba48 23647 int is_dwz = section_is_dwz;
d521ce57 23648 const gdb_byte *new_mac_ptr;
cf2c3c16 23649
24aa364d 23650 offset = read_offset (abfd, mac_ptr, offset_size);
cf2c3c16
TT
23651 mac_ptr += offset_size;
23652
0af92d60 23653 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 23654 {
ed2dc618 23655 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 23656
96b79293 23657 dwz->macro.read (objfile);
a036ba48 23658
a036ba48 23659 include_section = &dwz->macro;
96b79293 23660 include_bfd = include_section->get_bfd_owner ();
a036ba48
TT
23661 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23662 is_dwz = 1;
23663 }
23664
23665 new_mac_ptr = include_section->buffer + offset;
23666 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23667
8fc3fc34
TT
23668 if (*slot != NULL)
23669 {
23670 /* This has actually happened; see
23671 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 23672 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
23673 ".debug_macro section"));
23674 }
23675 else
23676 {
d521ce57 23677 *slot = (void *) new_mac_ptr;
36586728 23678
804d2729 23679 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 23680 include_mac_end, current_file, lh,
36586728 23681 section, section_is_gnu, is_dwz,
4d663531 23682 offset_size, include_hash);
8fc3fc34 23683
d521ce57 23684 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 23685 }
cf2c3c16
TT
23686 }
23687 break;
23688
2e276125 23689 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
23690 if (!section_is_gnu)
23691 {
23692 unsigned int bytes_read;
2e276125 23693
ac298888
TT
23694 /* This reads the constant, but since we don't recognize
23695 any vendor extensions, we ignore it. */
23696 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
23697 mac_ptr += bytes_read;
23698 read_direct_string (abfd, mac_ptr, &bytes_read);
23699 mac_ptr += bytes_read;
2e276125 23700
cf2c3c16
TT
23701 /* We don't recognize any vendor extensions. */
23702 break;
23703 }
23704 /* FALLTHROUGH */
23705
23706 default:
23707 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 23708 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
23709 section);
23710 if (mac_ptr == NULL)
23711 return;
23712 break;
2e276125 23713 }
132448f8 23714 DIAGNOSTIC_POP
757a13d0 23715 } while (macinfo_type != 0);
2e276125 23716}
8e19ed76 23717
cf2c3c16 23718static void
09262596 23719dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 23720 int section_is_gnu)
cf2c3c16 23721{
518817b3
SM
23722 struct dwarf2_per_objfile *dwarf2_per_objfile
23723 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23724 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
23725 struct line_header *lh = cu->line_header;
23726 bfd *abfd;
d521ce57 23727 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
23728 struct macro_source_file *current_file = 0;
23729 enum dwarf_macro_record_type macinfo_type;
23730 unsigned int offset_size = cu->header.offset_size;
d521ce57 23731 const gdb_byte *opcode_definitions[256];
8fc3fc34 23732 void **slot;
09262596
DE
23733 struct dwarf2_section_info *section;
23734 const char *section_name;
23735
23736 if (cu->dwo_unit != NULL)
23737 {
23738 if (section_is_gnu)
23739 {
23740 section = &cu->dwo_unit->dwo_file->sections.macro;
23741 section_name = ".debug_macro.dwo";
23742 }
23743 else
23744 {
23745 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23746 section_name = ".debug_macinfo.dwo";
23747 }
23748 }
23749 else
23750 {
23751 if (section_is_gnu)
23752 {
23753 section = &dwarf2_per_objfile->macro;
23754 section_name = ".debug_macro";
23755 }
23756 else
23757 {
23758 section = &dwarf2_per_objfile->macinfo;
23759 section_name = ".debug_macinfo";
23760 }
23761 }
cf2c3c16 23762
96b79293 23763 section->read (objfile);
cf2c3c16
TT
23764 if (section->buffer == NULL)
23765 {
b98664d3 23766 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
23767 return;
23768 }
96b79293 23769 abfd = section->get_bfd_owner ();
cf2c3c16
TT
23770
23771 /* First pass: Find the name of the base filename.
23772 This filename is needed in order to process all macros whose definition
23773 (or undefinition) comes from the command line. These macros are defined
23774 before the first DW_MACINFO_start_file entry, and yet still need to be
23775 associated to the base file.
23776
23777 To determine the base file name, we scan the macro definitions until we
23778 reach the first DW_MACINFO_start_file entry. We then initialize
23779 CURRENT_FILE accordingly so that any macro definition found before the
23780 first DW_MACINFO_start_file can still be associated to the base file. */
23781
23782 mac_ptr = section->buffer + offset;
23783 mac_end = section->buffer + section->size;
23784
23785 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23786 &offset_size, section_is_gnu);
23787 if (mac_ptr == NULL)
23788 {
23789 /* We already issued a complaint. */
23790 return;
23791 }
23792
23793 do
23794 {
23795 /* Do we at least have room for a macinfo type byte? */
23796 if (mac_ptr >= mac_end)
23797 {
23798 /* Complaint is printed during the second pass as GDB will probably
23799 stop the first pass earlier upon finding
23800 DW_MACINFO_start_file. */
23801 break;
23802 }
23803
aead7601 23804 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
23805 mac_ptr++;
23806
23807 /* Note that we rely on the fact that the corresponding GNU and
23808 DWARF constants are the same. */
132448f8
SM
23809 DIAGNOSTIC_PUSH
23810 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
23811 switch (macinfo_type)
23812 {
23813 /* A zero macinfo type indicates the end of the macro
23814 information. */
23815 case 0:
23816 break;
23817
0af92d60
JK
23818 case DW_MACRO_define:
23819 case DW_MACRO_undef:
cf2c3c16
TT
23820 /* Only skip the data by MAC_PTR. */
23821 {
23822 unsigned int bytes_read;
23823
23824 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23825 mac_ptr += bytes_read;
23826 read_direct_string (abfd, mac_ptr, &bytes_read);
23827 mac_ptr += bytes_read;
23828 }
23829 break;
23830
0af92d60 23831 case DW_MACRO_start_file:
cf2c3c16
TT
23832 {
23833 unsigned int bytes_read;
23834 int line, file;
23835
23836 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23837 mac_ptr += bytes_read;
23838 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23839 mac_ptr += bytes_read;
23840
804d2729 23841 current_file = macro_start_file (cu, file, line, 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;
804d2729 23906 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 23907 current_file, lh, section,
fc4007c9
TT
23908 section_is_gnu, 0, offset_size,
23909 include_hash.get ());
cf2c3c16
TT
23910}
23911
3019eac3
DE
23912/* Return the .debug_loc section to use for CU.
23913 For DWO files use .debug_loc.dwo. */
23914
23915static struct dwarf2_section_info *
23916cu_debug_loc_section (struct dwarf2_cu *cu)
23917{
518817b3
SM
23918 struct dwarf2_per_objfile *dwarf2_per_objfile
23919 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23920
3019eac3 23921 if (cu->dwo_unit)
43988095
JK
23922 {
23923 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23924
43988095
JK
23925 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23926 }
23927 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23928 : &dwarf2_per_objfile->loc);
3019eac3
DE
23929}
23930
8cf6f0b1
TT
23931/* A helper function that fills in a dwarf2_loclist_baton. */
23932
23933static void
23934fill_in_loclist_baton (struct dwarf2_cu *cu,
23935 struct dwarf2_loclist_baton *baton,
ff39bb5e 23936 const struct attribute *attr)
8cf6f0b1 23937{
518817b3
SM
23938 struct dwarf2_per_objfile *dwarf2_per_objfile
23939 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23940 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23941
96b79293 23942 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23943
23944 baton->per_cu = cu->per_cu;
23945 gdb_assert (baton->per_cu);
23946 /* We don't know how long the location list is, but make sure we
23947 don't run off the edge of the section. */
3019eac3
DE
23948 baton->size = section->size - DW_UNSND (attr);
23949 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 23950 baton->base_address = cu->base_address;
f664829e 23951 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23952}
23953
4c2df51b 23954static void
ff39bb5e 23955dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23956 struct dwarf2_cu *cu, int is_block)
4c2df51b 23957{
518817b3
SM
23958 struct dwarf2_per_objfile *dwarf2_per_objfile
23959 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23960 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23961 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23962
cd6c91b4 23963 if (attr->form_is_section_offset ()
3019eac3 23964 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23965 the section. If so, fall through to the complaint in the
23966 other branch. */
2c7d5afc 23967 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23968 {
0d53c4c4 23969 struct dwarf2_loclist_baton *baton;
4c2df51b 23970
8d749320 23971 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23972
8cf6f0b1 23973 fill_in_loclist_baton (cu, baton, attr);
be391dca 23974
d00adf39 23975 if (cu->base_known == 0)
b98664d3 23976 complaint (_("Location list used without "
3e43a32a 23977 "specifying the CU base address."));
4c2df51b 23978
f1e6e072
TT
23979 SYMBOL_ACLASS_INDEX (sym) = (is_block
23980 ? dwarf2_loclist_block_index
23981 : dwarf2_loclist_index);
0d53c4c4
DJ
23982 SYMBOL_LOCATION_BATON (sym) = baton;
23983 }
23984 else
23985 {
23986 struct dwarf2_locexpr_baton *baton;
23987
8d749320 23988 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23989 baton->per_cu = cu->per_cu;
23990 gdb_assert (baton->per_cu);
0d53c4c4 23991
4fc6c0d5 23992 if (attr->form_is_block ())
0d53c4c4
DJ
23993 {
23994 /* Note that we're just copying the block's data pointer
23995 here, not the actual data. We're still pointing into the
6502dd73
DJ
23996 info_buffer for SYM's objfile; right now we never release
23997 that buffer, but when we do clean up properly this may
23998 need to change. */
0d53c4c4
DJ
23999 baton->size = DW_BLOCK (attr)->size;
24000 baton->data = DW_BLOCK (attr)->data;
24001 }
24002 else
24003 {
24004 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24005 sym->natural_name ());
0d53c4c4 24006 baton->size = 0;
0d53c4c4 24007 }
6e70227d 24008
f1e6e072
TT
24009 SYMBOL_ACLASS_INDEX (sym) = (is_block
24010 ? dwarf2_locexpr_block_index
24011 : dwarf2_locexpr_index);
0d53c4c4
DJ
24012 SYMBOL_LOCATION_BATON (sym) = baton;
24013 }
4c2df51b 24014}
6502dd73 24015
09ba997f 24016/* See read.h. */
ae0d2f24
UW
24017
24018struct objfile *
09ba997f 24019dwarf2_per_cu_data::objfile () const
ae0d2f24 24020{
09ba997f 24021 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
24022
24023 /* Return the master objfile, so that we can report and look up the
24024 correct file containing this variable. */
24025 if (objfile->separate_debug_objfile_backlink)
24026 objfile = objfile->separate_debug_objfile_backlink;
24027
24028 return objfile;
24029}
24030
96408a79
SA
24031/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24032 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24033 CU_HEADERP first. */
24034
24035static const struct comp_unit_head *
24036per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 24037 const struct dwarf2_per_cu_data *per_cu)
96408a79 24038{
d521ce57 24039 const gdb_byte *info_ptr;
96408a79
SA
24040
24041 if (per_cu->cu)
24042 return &per_cu->cu->header;
24043
9c541725 24044 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24045
24046 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24047 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24048 rcuh_kind::COMPILE);
96408a79
SA
24049
24050 return cu_headerp;
24051}
24052
09ba997f 24053/* See read.h. */
ae0d2f24 24054
98714339 24055int
09ba997f 24056dwarf2_per_cu_data::addr_size () const
ae0d2f24 24057{
96408a79
SA
24058 struct comp_unit_head cu_header_local;
24059 const struct comp_unit_head *cu_headerp;
c471e790 24060
09ba997f 24061 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24062
24063 return cu_headerp->addr_size;
ae0d2f24
UW
24064}
24065
09ba997f 24066/* See read.h. */
9eae7c52
TT
24067
24068int
09ba997f 24069dwarf2_per_cu_data::offset_size () const
9eae7c52 24070{
96408a79
SA
24071 struct comp_unit_head cu_header_local;
24072 const struct comp_unit_head *cu_headerp;
9c6c53f7 24073
09ba997f 24074 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24075
24076 return cu_headerp->offset_size;
24077}
24078
09ba997f 24079/* See read.h. */
96408a79
SA
24080
24081int
09ba997f 24082dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
24083{
24084 struct comp_unit_head cu_header_local;
24085 const struct comp_unit_head *cu_headerp;
24086
09ba997f 24087 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
24088
24089 if (cu_headerp->version == 2)
24090 return cu_headerp->addr_size;
24091 else
24092 return cu_headerp->offset_size;
181cebd4
JK
24093}
24094
09ba997f 24095/* See read.h. */
9aa1f1e3
TT
24096
24097CORE_ADDR
09ba997f 24098dwarf2_per_cu_data::text_offset () const
9aa1f1e3 24099{
09ba997f
TT
24100 struct objfile *objfile = dwarf2_per_objfile->objfile;
24101
24102 return objfile->text_section_offset ();
9aa1f1e3
TT
24103}
24104
09ba997f
TT
24105/* See read.h. */
24106
24107struct type *
24108dwarf2_per_cu_data::addr_type () const
9a49df9d 24109{
09ba997f 24110 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
24111 struct type *void_type = objfile_type (objfile)->builtin_void;
24112 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 24113 int addr_size = this->addr_size ();
9a49df9d
AB
24114
24115 if (TYPE_LENGTH (addr_type) == addr_size)
24116 return addr_type;
24117
09ba997f 24118 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
24119 return addr_type;
24120}
24121
22b6cd70
TT
24122/* A helper function for dwarf2_find_containing_comp_unit that returns
24123 the index of the result, and that searches a vector. It will
24124 return a result even if the offset in question does not actually
24125 occur in any CU. This is separate so that it can be unit
24126 tested. */
ae038cb0 24127
22b6cd70
TT
24128static int
24129dwarf2_find_containing_comp_unit
24130 (sect_offset sect_off,
24131 unsigned int offset_in_dwz,
24132 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24133{
ae038cb0
DJ
24134 int low, high;
24135
ae038cb0 24136 low = 0;
22b6cd70 24137 high = all_comp_units.size () - 1;
ae038cb0
DJ
24138 while (high > low)
24139 {
36586728 24140 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24141 int mid = low + (high - low) / 2;
9a619af0 24142
22b6cd70 24143 mid_cu = all_comp_units[mid];
36586728 24144 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24145 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24146 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24147 high = mid;
24148 else
24149 low = mid + 1;
24150 }
24151 gdb_assert (low == high);
22b6cd70
TT
24152 return low;
24153}
24154
24155/* Locate the .debug_info compilation unit from CU's objfile which contains
24156 the DIE at OFFSET. Raises an error on failure. */
24157
24158static struct dwarf2_per_cu_data *
24159dwarf2_find_containing_comp_unit (sect_offset sect_off,
24160 unsigned int offset_in_dwz,
24161 struct dwarf2_per_objfile *dwarf2_per_objfile)
24162{
24163 int low
24164 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24165 dwarf2_per_objfile->all_comp_units);
24166 struct dwarf2_per_cu_data *this_cu
24167 = dwarf2_per_objfile->all_comp_units[low];
24168
45b8ae0c 24169 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24170 {
36586728 24171 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24172 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24173 "offset %s [in module %s]"),
24174 sect_offset_str (sect_off),
ed2dc618 24175 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24176
9c541725
PA
24177 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24178 <= sect_off);
ae038cb0
DJ
24179 return dwarf2_per_objfile->all_comp_units[low-1];
24180 }
24181 else
24182 {
b76e467d 24183 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24184 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24185 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24186 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24187 return this_cu;
24188 }
24189}
24190
22b6cd70
TT
24191#if GDB_SELF_TEST
24192
24193namespace selftests {
24194namespace find_containing_comp_unit {
24195
24196static void
24197run_test ()
24198{
24199 struct dwarf2_per_cu_data one {};
24200 struct dwarf2_per_cu_data two {};
24201 struct dwarf2_per_cu_data three {};
24202 struct dwarf2_per_cu_data four {};
24203
24204 one.length = 5;
24205 two.sect_off = sect_offset (one.length);
24206 two.length = 7;
24207
24208 three.length = 5;
24209 three.is_dwz = 1;
24210 four.sect_off = sect_offset (three.length);
24211 four.length = 7;
24212 four.is_dwz = 1;
24213
24214 std::vector<dwarf2_per_cu_data *> units;
24215 units.push_back (&one);
24216 units.push_back (&two);
24217 units.push_back (&three);
24218 units.push_back (&four);
24219
24220 int result;
24221
24222 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24223 SELF_CHECK (units[result] == &one);
24224 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24225 SELF_CHECK (units[result] == &one);
24226 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24227 SELF_CHECK (units[result] == &two);
24228
24229 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24230 SELF_CHECK (units[result] == &three);
24231 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24232 SELF_CHECK (units[result] == &three);
24233 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24234 SELF_CHECK (units[result] == &four);
24235}
24236
24237}
24238}
24239
24240#endif /* GDB_SELF_TEST */
24241
23745b47 24242/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24243
fcd3b13d
SM
24244dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24245 : per_cu (per_cu_),
9068261f
AB
24246 mark (false),
24247 has_loclist (false),
24248 checked_producer (false),
24249 producer_is_gxx_lt_4_6 (false),
24250 producer_is_gcc_lt_4_3 (false),
eb77c9df 24251 producer_is_icc (false),
9068261f 24252 producer_is_icc_lt_14 (false),
c258c396 24253 producer_is_codewarrior (false),
9068261f 24254 processing_has_namespace_info (false)
93311388 24255{
fcd3b13d
SM
24256 per_cu->cu = this;
24257}
24258
24259/* Destroy a dwarf2_cu. */
24260
24261dwarf2_cu::~dwarf2_cu ()
24262{
24263 per_cu->cu = NULL;
9816fde3
JK
24264}
24265
24266/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24267
24268static void
95554aad
TT
24269prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24270 enum language pretend_language)
9816fde3
JK
24271{
24272 struct attribute *attr;
24273
24274 /* Set the language we're debugging. */
24275 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24276 if (attr != nullptr)
9816fde3
JK
24277 set_cu_language (DW_UNSND (attr), cu);
24278 else
9cded63f 24279 {
95554aad 24280 cu->language = pretend_language;
9cded63f
TT
24281 cu->language_defn = language_def (cu->language);
24282 }
dee91e82 24283
7d45c7c3 24284 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24285}
24286
ae038cb0
DJ
24287/* Increase the age counter on each cached compilation unit, and free
24288 any that are too old. */
24289
24290static void
ed2dc618 24291age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24292{
24293 struct dwarf2_per_cu_data *per_cu, **last_chain;
24294
24295 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24296 per_cu = dwarf2_per_objfile->read_in_chain;
24297 while (per_cu != NULL)
24298 {
24299 per_cu->cu->last_used ++;
b4f54984 24300 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24301 dwarf2_mark (per_cu->cu);
24302 per_cu = per_cu->cu->read_in_chain;
24303 }
24304
24305 per_cu = dwarf2_per_objfile->read_in_chain;
24306 last_chain = &dwarf2_per_objfile->read_in_chain;
24307 while (per_cu != NULL)
24308 {
24309 struct dwarf2_per_cu_data *next_cu;
24310
24311 next_cu = per_cu->cu->read_in_chain;
24312
24313 if (!per_cu->cu->mark)
24314 {
fcd3b13d 24315 delete per_cu->cu;
ae038cb0
DJ
24316 *last_chain = next_cu;
24317 }
24318 else
24319 last_chain = &per_cu->cu->read_in_chain;
24320
24321 per_cu = next_cu;
24322 }
24323}
24324
24325/* Remove a single compilation unit from the cache. */
24326
24327static void
dee91e82 24328free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24329{
24330 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24331 struct dwarf2_per_objfile *dwarf2_per_objfile
24332 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24333
24334 per_cu = dwarf2_per_objfile->read_in_chain;
24335 last_chain = &dwarf2_per_objfile->read_in_chain;
24336 while (per_cu != NULL)
24337 {
24338 struct dwarf2_per_cu_data *next_cu;
24339
24340 next_cu = per_cu->cu->read_in_chain;
24341
dee91e82 24342 if (per_cu == target_per_cu)
ae038cb0 24343 {
fcd3b13d 24344 delete per_cu->cu;
dee91e82 24345 per_cu->cu = NULL;
ae038cb0
DJ
24346 *last_chain = next_cu;
24347 break;
24348 }
24349 else
24350 last_chain = &per_cu->cu->read_in_chain;
24351
24352 per_cu = next_cu;
24353 }
24354}
24355
dee91e82
DE
24356/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24357 We store these in a hash table separate from the DIEs, and preserve them
24358 when the DIEs are flushed out of cache.
24359
24360 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24361 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24362 or the type may come from a DWO file. Furthermore, while it's more logical
24363 to use per_cu->section+offset, with Fission the section with the data is in
24364 the DWO file but we don't know that section at the point we need it.
24365 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24366 because we can enter the lookup routine, get_die_type_at_offset, from
24367 outside this file, and thus won't necessarily have PER_CU->cu.
24368 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24369
dee91e82 24370struct dwarf2_per_cu_offset_and_type
1c379e20 24371{
dee91e82 24372 const struct dwarf2_per_cu_data *per_cu;
9c541725 24373 sect_offset sect_off;
1c379e20
DJ
24374 struct type *type;
24375};
24376
dee91e82 24377/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24378
24379static hashval_t
dee91e82 24380per_cu_offset_and_type_hash (const void *item)
1c379e20 24381{
9a3c8263
SM
24382 const struct dwarf2_per_cu_offset_and_type *ofs
24383 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24384
9c541725 24385 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24386}
24387
dee91e82 24388/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24389
24390static int
dee91e82 24391per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24392{
9a3c8263
SM
24393 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24394 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24395 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24396 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24397
dee91e82 24398 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24399 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24400}
24401
24402/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24403 table if necessary. For convenience, return TYPE.
24404
24405 The DIEs reading must have careful ordering to:
85102364 24406 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24407 reading current DIE.
24408 * Not trying to dereference contents of still incompletely read in types
24409 while reading in other DIEs.
24410 * Enable referencing still incompletely read in types just by a pointer to
24411 the type without accessing its fields.
24412
24413 Therefore caller should follow these rules:
24414 * Try to fetch any prerequisite types we may need to build this DIE type
24415 before building the type and calling set_die_type.
e71ec853 24416 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24417 possible before fetching more types to complete the current type.
24418 * Make the type as complete as possible before fetching more types. */
1c379e20 24419
f792889a 24420static struct type *
1c379e20
DJ
24421set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24422{
518817b3
SM
24423 struct dwarf2_per_objfile *dwarf2_per_objfile
24424 = cu->per_cu->dwarf2_per_objfile;
dee91e82 24425 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 24426 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
24427 struct attribute *attr;
24428 struct dynamic_prop prop;
1c379e20 24429
b4ba55a1
JB
24430 /* For Ada types, make sure that the gnat-specific data is always
24431 initialized (if not already set). There are a few types where
24432 we should not be doing so, because the type-specific area is
24433 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24434 where the type-specific area is used to store the floatformat).
24435 But this is not a problem, because the gnat-specific information
24436 is actually not needed for these types. */
24437 if (need_gnat_info (cu)
24438 && TYPE_CODE (type) != TYPE_CODE_FUNC
24439 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
24440 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24441 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24442 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
24443 && !HAVE_GNAT_AUX_INFO (type))
24444 INIT_GNAT_SPECIFIC (type);
24445
3f2f83dd
KB
24446 /* Read DW_AT_allocated and set in type. */
24447 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 24448 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24449 {
09ba997f 24450 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24451 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24452 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
24453 }
24454 else if (attr != NULL)
24455 {
b98664d3 24456 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 24457 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24458 sect_offset_str (die->sect_off));
3f2f83dd
KB
24459 }
24460
24461 /* Read DW_AT_associated and set in type. */
24462 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 24463 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24464 {
09ba997f 24465 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 24466 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24467 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
24468 }
24469 else if (attr != NULL)
24470 {
b98664d3 24471 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 24472 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24473 sect_offset_str (die->sect_off));
3f2f83dd
KB
24474 }
24475
3cdcd0ce
JB
24476 /* Read DW_AT_data_location and set in type. */
24477 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 24478 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 24479 cu->per_cu->addr_type ()))
50a82047 24480 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 24481
dee91e82 24482 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
24483 dwarf2_per_objfile->die_type_hash
24484 = htab_up (htab_create_alloc (127,
24485 per_cu_offset_and_type_hash,
24486 per_cu_offset_and_type_eq,
24487 NULL, xcalloc, xfree));
1c379e20 24488
dee91e82 24489 ofs.per_cu = cu->per_cu;
9c541725 24490 ofs.sect_off = die->sect_off;
1c379e20 24491 ofs.type = type;
dee91e82 24492 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 24493 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24494 if (*slot)
b98664d3 24495 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24496 sect_offset_str (die->sect_off));
8d749320
SM
24497 *slot = XOBNEW (&objfile->objfile_obstack,
24498 struct dwarf2_per_cu_offset_and_type);
1c379e20 24499 **slot = ofs;
f792889a 24500 return type;
1c379e20
DJ
24501}
24502
9c541725 24503/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24504 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24505
24506static struct type *
9c541725 24507get_die_type_at_offset (sect_offset sect_off,
673bfd45 24508 struct dwarf2_per_cu_data *per_cu)
1c379e20 24509{
dee91e82 24510 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 24511 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 24512
dee91e82 24513 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 24514 return NULL;
1c379e20 24515
dee91e82 24516 ofs.per_cu = per_cu;
9c541725 24517 ofs.sect_off = sect_off;
9a3c8263 24518 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 24519 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24520 if (slot)
24521 return slot->type;
24522 else
24523 return NULL;
24524}
24525
02142a6c 24526/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24527 or return NULL if DIE does not have a saved type. */
24528
24529static struct type *
24530get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24531{
9c541725 24532 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24533}
24534
10b3939b
DJ
24535/* Add a dependence relationship from CU to REF_PER_CU. */
24536
24537static void
24538dwarf2_add_dependence (struct dwarf2_cu *cu,
24539 struct dwarf2_per_cu_data *ref_per_cu)
24540{
24541 void **slot;
24542
24543 if (cu->dependencies == NULL)
24544 cu->dependencies
24545 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24546 NULL, &cu->comp_unit_obstack,
24547 hashtab_obstack_allocate,
24548 dummy_obstack_deallocate);
24549
24550 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24551 if (*slot == NULL)
24552 *slot = ref_per_cu;
24553}
1c379e20 24554
f504f079
DE
24555/* Subroutine of dwarf2_mark to pass to htab_traverse.
24556 Set the mark field in every compilation unit in the
ae038cb0
DJ
24557 cache that we must keep because we are keeping CU. */
24558
10b3939b
DJ
24559static int
24560dwarf2_mark_helper (void **slot, void *data)
24561{
24562 struct dwarf2_per_cu_data *per_cu;
24563
24564 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
24565
24566 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24567 reading of the chain. As such dependencies remain valid it is not much
24568 useful to track and undo them during QUIT cleanups. */
24569 if (per_cu->cu == NULL)
24570 return 1;
24571
10b3939b
DJ
24572 if (per_cu->cu->mark)
24573 return 1;
9068261f 24574 per_cu->cu->mark = true;
10b3939b
DJ
24575
24576 if (per_cu->cu->dependencies != NULL)
24577 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24578
24579 return 1;
24580}
24581
f504f079
DE
24582/* Set the mark field in CU and in every other compilation unit in the
24583 cache that we must keep because we are keeping CU. */
24584
ae038cb0
DJ
24585static void
24586dwarf2_mark (struct dwarf2_cu *cu)
24587{
24588 if (cu->mark)
24589 return;
9068261f 24590 cu->mark = true;
10b3939b
DJ
24591 if (cu->dependencies != NULL)
24592 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
24593}
24594
24595static void
24596dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24597{
24598 while (per_cu)
24599 {
9068261f 24600 per_cu->cu->mark = false;
ae038cb0
DJ
24601 per_cu = per_cu->cu->read_in_chain;
24602 }
72bf9492
DJ
24603}
24604
72bf9492
DJ
24605/* Trivial hash function for partial_die_info: the hash value of a DIE
24606 is its offset in .debug_info for this objfile. */
24607
24608static hashval_t
24609partial_die_hash (const void *item)
24610{
9a3c8263
SM
24611 const struct partial_die_info *part_die
24612 = (const struct partial_die_info *) item;
9a619af0 24613
9c541725 24614 return to_underlying (part_die->sect_off);
72bf9492
DJ
24615}
24616
24617/* Trivial comparison function for partial_die_info structures: two DIEs
24618 are equal if they have the same offset. */
24619
24620static int
24621partial_die_eq (const void *item_lhs, const void *item_rhs)
24622{
9a3c8263
SM
24623 const struct partial_die_info *part_die_lhs
24624 = (const struct partial_die_info *) item_lhs;
24625 const struct partial_die_info *part_die_rhs
24626 = (const struct partial_die_info *) item_rhs;
9a619af0 24627
9c541725 24628 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24629}
24630
3c3bb058
AB
24631struct cmd_list_element *set_dwarf_cmdlist;
24632struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
24633
24634static void
981a3fb3 24635set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 24636{
b4f54984 24637 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 24638 gdb_stdout);
ae038cb0
DJ
24639}
24640
24641static void
981a3fb3 24642show_dwarf_cmd (const char *args, int from_tty)
6e70227d 24643{
b4f54984 24644 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
24645}
24646
9291a0cd 24647static void
cd4fb1b2
SM
24648show_check_physname (struct ui_file *file, int from_tty,
24649 struct cmd_list_element *c, const char *value)
9291a0cd 24650{
cd4fb1b2
SM
24651 fprintf_filtered (file,
24652 _("Whether to check \"physname\" is %s.\n"),
24653 value);
9291a0cd
TT
24654}
24655
6c265988 24656void _initialize_dwarf2_read ();
cd4fb1b2 24657void
6c265988 24658_initialize_dwarf2_read ()
9291a0cd 24659{
cd4fb1b2
SM
24660 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24661Set DWARF specific variables.\n\
590042fc 24662Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
24663 &set_dwarf_cmdlist, "maintenance set dwarf ",
24664 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24665
cd4fb1b2 24666 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
24667Show DWARF specific variables.\n\
24668Show DWARF variables such as the cache size."),
cd4fb1b2
SM
24669 &show_dwarf_cmdlist, "maintenance show dwarf ",
24670 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24671
cd4fb1b2
SM
24672 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24673 &dwarf_max_cache_age, _("\
24674Set the upper bound on the age of cached DWARF compilation units."), _("\
24675Show the upper bound on the age of cached DWARF compilation units."), _("\
24676A higher limit means that cached compilation units will be stored\n\
24677in memory longer, and more total memory will be used. Zero disables\n\
24678caching, which can slow down startup."),
24679 NULL,
24680 show_dwarf_max_cache_age,
24681 &set_dwarf_cmdlist,
24682 &show_dwarf_cmdlist);
156942c7 24683
cd4fb1b2
SM
24684 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24685Set debugging of the DWARF reader."), _("\
24686Show debugging of the DWARF reader."), _("\
24687When enabled (non-zero), debugging messages are printed during DWARF\n\
24688reading and symtab expansion. A value of 1 (one) provides basic\n\
24689information. A value greater than 1 provides more verbose information."),
24690 NULL,
24691 NULL,
24692 &setdebuglist, &showdebuglist);
9291a0cd 24693
cd4fb1b2
SM
24694 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24695Set debugging of the DWARF DIE reader."), _("\
24696Show debugging of the DWARF DIE reader."), _("\
24697When enabled (non-zero), DIEs are dumped after they are read in.\n\
24698The value is the maximum depth to print."),
24699 NULL,
24700 NULL,
24701 &setdebuglist, &showdebuglist);
9291a0cd 24702
cd4fb1b2
SM
24703 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24704Set debugging of the dwarf line reader."), _("\
24705Show debugging of the dwarf line reader."), _("\
24706When enabled (non-zero), line number entries are dumped as they are read in.\n\
24707A value of 1 (one) provides basic information.\n\
24708A value greater than 1 provides more verbose information."),
24709 NULL,
24710 NULL,
24711 &setdebuglist, &showdebuglist);
437afbb8 24712
cd4fb1b2
SM
24713 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24714Set cross-checking of \"physname\" code against demangler."), _("\
24715Show cross-checking of \"physname\" code against demangler."), _("\
24716When enabled, GDB's internal \"physname\" code is checked against\n\
24717the demangler."),
24718 NULL, show_check_physname,
24719 &setdebuglist, &showdebuglist);
900e11f9 24720
e615022a
DE
24721 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24722 no_class, &use_deprecated_index_sections, _("\
24723Set whether to use deprecated gdb_index sections."), _("\
24724Show whether to use deprecated gdb_index sections."), _("\
24725When enabled, deprecated .gdb_index sections are used anyway.\n\
24726Normally they are ignored either because of a missing feature or\n\
24727performance issue.\n\
24728Warning: This option must be enabled before gdb reads the file."),
24729 NULL,
24730 NULL,
24731 &setlist, &showlist);
24732
f1e6e072
TT
24733 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24734 &dwarf2_locexpr_funcs);
24735 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24736 &dwarf2_loclist_funcs);
24737
24738 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24739 &dwarf2_block_frame_base_locexpr_funcs);
24740 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24741 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24742
24743#if GDB_SELF_TEST
24744 selftests::register_test ("dw2_expand_symtabs_matching",
24745 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24746 selftests::register_test ("dwarf2_find_containing_comp_unit",
24747 selftests::find_containing_comp_unit::run_test);
c62446b1 24748#endif
6502dd73 24749}
This page took 6.396382 seconds and 4 git commands to generate.